Skip to content

Instantly share code, notes, and snippets.

@blasterpistol
Last active October 28, 2020 06:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blasterpistol/21d2bb1964a8f042dde4c189a28d9d94 to your computer and use it in GitHub Desktop.
Save blasterpistol/21d2bb1964a8f042dde4c189a28d9d94 to your computer and use it in GitHub Desktop.
TinyMCE formatting for contentEditable=false elements
// To resolve all quirks need to use custom elements
// Demo: http://fiddle.tinymce.com/7whaab
tinymce.init({
selector: 'textarea',
// custom html elements will force tinymce to wrap var-inline when formatting
// and apply formatting inside of var-block (as it should be)
custom_elements: "~var-inline,var-block",
setup: (editor) => {
// temporary remove content editable false before commands and apply back then
editor.on("BeforeExecCommand", () => {
editor.selection.editor
.getBody()
.querySelectorAll("[contenteditable=false]")
.forEach((n) => {
n.setAttribyte("noneditable", "true");
n.removeAttribute("contenteditable");
});
});
editor.on("ExecCommand", () => {
editor
.getBody()
.querySelectorAll("[noneditable="true"]")
.forEach((n) => {
n.setAttribute("contenteditable", "false");
n.removeAttribute("noneditable");
});
});
},
});
<textarea>
<var-inline contenteditable="false">Some inline placeholder</var-inline>
<var-block contenteditable="false">Some block placeholder (tables or images)</var-block>
</textarea>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment