Created
June 29, 2016 17:06
-
-
Save CYBAI/e63974a8b57013059e0191c1c6ac741a to your computer and use it in GitHub Desktop.
[WIP] Make `textarea` syntax highlight
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
var script = document.createElement('script'); | |
script.src = 'https://cdnjs.cloudflare.com/ajax/libs/prism/1.5.1/prism.js'; | |
document.body.appendChild(script); | |
var csslink = document.createElement('link'); | |
csslink.setAttribute('rel', 'stylesheet'); | |
csslink.setAttribute('type', 'text/css'); | |
csslink.setAttribute('href', 'https://cdnjs.cloudflare.com/ajax/libs/prism/1.5.1/themes/prism-solarizedlight.css'); | |
document.getElementsByTagName('head')[0].appendChild(csslink); | |
function checkLoadPrism() { | |
if (!window.Prism) { | |
setTimeout(() => { | |
checkLoadPrism(); | |
}, 100); | |
} else { | |
renderPrism(); | |
} | |
} | |
function renderPrism() { | |
var textareas = document.getElementsByTagName('textarea'); | |
Array.from(textareas).forEach((textarea) => { | |
let preEl = document.createElement('pre'); | |
preEl.setAttribute('contenteditable', true); | |
preEl.setAttribute('class', 'language-html'); | |
preEl.textContent = textarea.value; | |
textarea.parentNode.replaceChild(preEl, textarea); | |
Prism.highlightElement(preEl); | |
preEl.addEventListener('input', (e) => { | |
preEl = e.target; | |
Prism.highlightElement(preEl); | |
}); | |
}); | |
} | |
checkLoadPrism(); |
works fine, but how to handle it to save ?
I have the same problem as shark0der
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The caret position is reset after each keypress =/. Thanks anyway