A Pen by Christopher Pitt on CodePen.
Created
December 15, 2017 17:05
-
-
Save assertchris/3208cfe5f5f3864cfa5b5cfb024c6505 to your computer and use it in GitHub Desktop.
XVmWBV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="tweet-box-home-timeline" contenteditable="true"> | |
What's happening? | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const insertAfter = (newNode, referenceNode) => { | |
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling) | |
} | |
const replaceRange = (transformer) => { | |
const selected = window.getSelection() | |
if (selected.rangeCount) { | |
const range = selected.getRangeAt(0) | |
const transformed = transformer(selected.toString()) | |
const span = document.createElement("span") | |
span.innerHTML = transformed | |
range.deleteContents() | |
range.insertNode(span) | |
} | |
} | |
const replaceArray = (text, find, replace) => { | |
let replaced = text | |
for (let i = 0; i < find.length; i++) { | |
replaced = replaced.replace(find[i], replace[i]) | |
} | |
return replaced | |
} | |
// http://qaz.wtf/u/convert.cgi?text=abcdefghijklmnopqrstuvwxyz | |
const alpha = "abcdefghijklmnopqrstuvwxyz".split("") | |
const italic = runes("๐ข๐ฃ๐ค๐ฅ๐ฆ๐ง๐จ๐ฉ๐ช๐ซ๐ฌ๐ญ๐ฎ๐ฏ๐ฐ๐ฑ๐ฒ๐ณ๐ด๐ต๐ถ๐ท๐ธ๐น๐บ๐ป") | |
const bold = runes("๐ฎ๐ฏ๐ฐ๐ฑ๐ฒ๐ณ๐ด๐ต๐ถ๐ท๐ธ๐น๐บ๐ป๐ผ๐ฝ๐พ๐ฟ๐๐๐๐๐๐ ๐๐") | |
const withItalic = (text) => { | |
return replaceArray(text, alpha, italic) | |
} | |
const withBold = (text) => { | |
return replaceArray(text, alpha, bold) | |
} | |
const editor = document.querySelector("#tweet-box-home-timeline") | |
if (editor) { | |
editor.addEventListener("focus", () => { | |
if (editor.innerText === "What's happening?") { | |
editor.innerText = "" | |
} | |
}) | |
editor.addEventListener("blur", () => { | |
if (editor.innerText === "") { | |
editor.innerText = "What's happening?" | |
} | |
}) | |
const italicButton = document.createElement("button") | |
italicButton.innerText = "๐ฐ" | |
italicButton.addEventListener( | |
"click", () => replaceRange(withItalic) | |
) | |
const boldButton = document.createElement("button") | |
boldButton.innerText = "๐" | |
boldButton.addEventListener( | |
"click", () => replaceRange(withBold) | |
) | |
insertAfter(italicButton, editor) | |
insertAfter(boldButton, editor) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="https://rawgit.com/dotcypress/runes/develop/index.js"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* DEBUG */ | |
#tweet-box-home-timeline { | |
border: solid 2px #ccc; | |
width: 200px; | |
height: 40px; | |
line-height: 40px; | |
margin: 5px; | |
border-radius: 5px; | |
padding: 0 10px; | |
outline: 0; | |
font-family: sans-serif; | |
font-size: 14px; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment