67 lines
1.4 KiB
Vue
Raw Normal View History

2021-09-11 17:43:07 +08:00
<template>
<div v-html="payload.innerHtml"></div>
</template>
<script lang="ts">
import {
defineComponent,
onMounted,
nextTick,
toRef,
watch,
reactive,
} from "vue";
import { useDarkMode } from "@vuepress/theme-default/lib/client";
export default defineComponent({
name: "Mermaid",
props: {
identifier: String,
graph: String,
},
setup(props) {
const dark = useDarkMode();
const chartID = toRef(props, "identifier");
const rawGraph = toRef(props, "graph");
const html = reactive({ innerHtml: "" });
onMounted(() => {
nextTick(async function () {
const mermaid = await import("mermaid");
mermaid.default.initialize({
startOnLoad: false,
theme: dark.value ? "dark" : "default",
});
mermaid.default.render(
chartID.value,
2021-09-12 20:39:48 +08:00
decodeURI(rawGraph.value),
2021-09-11 17:43:07 +08:00
(svgCode, bindFunc) => {
html.innerHtml = svgCode;
}
);
});
});
watch(dark, async () => {
const mermaid = await import("mermaid");
mermaid.default.initialize({
startOnLoad: false,
theme: dark.value ? "dark" : "default",
});
mermaid.default.render(
chartID.value,
2021-09-12 20:39:48 +08:00
decodeURI(rawGraph.value),
2021-09-11 17:43:07 +08:00
(svgCode, bindFunc) => {
html.innerHtml = svgCode;
}
);
});
return {
tag: chartID,
payload: html,
};
},
});
</script>
<style scoped></style>