Created
November 19, 2019 19:57
-
-
Save nestarz/d9152afc128ef9c85848453b52b2b9c7 to your computer and use it in GitHub Desktop.
Editable Vue Code Component with Syntax Highlight using Prism.js (global)
This file contains hidden or 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
import { computed, ref, createElement as h } from "/vue.js"; | |
export default { | |
props: { | |
lang: { type: String, default: "js" }, | |
content: { type: String, default: "your code..." } | |
}, | |
setup(props, { emit, refs }) { | |
const content = ref(props.content); | |
const highlighted = computed(() => | |
Prism.highlight(props.content, Prism.languages[props.lang]) | |
); | |
return () => | |
h("div", { style: { position: "relative" } }, [ | |
h("pre", { | |
ref: "element", | |
style: { position: "absolute", "pointer-events": "none", inset: 0, margin: 0 }, | |
domProps: { innerHTML: highlighted.value } | |
}), | |
h( | |
"pre", | |
{ | |
ref: "element", | |
style: { opacity: 1, margin: 0 }, | |
attrs: { contenteditable: true, tabindex: 0 }, | |
on: { | |
input: event => emit("update:content", event.target.innerText) | |
} | |
}, | |
content.value | |
) | |
]); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment