bililiteRange
to handle some of the subtleties of dealing with contenteditable
elements, a syntax-highlighting editor is relatively easy.
Simplest usage: bililiteRange.fancyText(element, Prism.highlightelement);
.
See the demo.
See the code on github.
There's a fair amount of flakiness; contenteditable
elements are still handled inconsistently. Newlines are changed into actual elements (Firefox adds <br>
s; Chrome adds a whole new <div>
), even though a <pre>
doesn't need that; it would interpret newlines the way we want. So we have to intercept those and just insert the newline. Firefox insists on changing newlines into <br>
's even in pasted text, and Chrome's clipboard implementation includes the carriage return character, so we have to intercept paste events and insert the text appropriately mangled.