63 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",
});
2023-04-17 21:20:38 -04:00
mermaid.default
.render(chartID.value!, decodeURI(rawGraph.value!))
.then(({ svg, bindFunctions }) => {
2023-04-09 11:53:11 -04:00
html.innerHtml = svg;
2023-04-17 21:20:38 -04:00
});
2021-09-11 17:43:07 +08:00
});
});
watch(dark, async () => {
const mermaid = await import("mermaid");
mermaid.default.initialize({
startOnLoad: false,
theme: dark.value ? "dark" : "default",
});
2023-04-17 21:20:38 -04:00
mermaid.default
.render(chartID.value!, decodeURI(rawGraph.value!))
.then(({ svg, bindFunctions }) => {
2023-04-09 11:53:11 -04:00
html.innerHtml = svg;
2023-04-17 21:20:38 -04:00
});
2021-09-11 17:43:07 +08:00
});
return {
tag: chartID,
payload: html,
};
},
});
</script>
<style scoped></style>