Skip to content

Instantly share code, notes, and snippets.

@nestarz
Created November 19, 2019 19:57
Show Gist options
  • Save nestarz/d9152afc128ef9c85848453b52b2b9c7 to your computer and use it in GitHub Desktop.
Save nestarz/d9152afc128ef9c85848453b52b2b9c7 to your computer and use it in GitHub Desktop.
Editable Vue Code Component with Syntax Highlight using Prism.js (global)
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