84 lines
1.6 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">
2024-01-26 05:07:42 +08:00
import { useMutationObserver } from "@vueuse/core";
2021-09-11 17:43:07 +08:00
import {
2024-01-26 05:07:42 +08:00
computed,
2021-09-11 17:43:07 +08:00
defineComponent,
2024-01-26 05:07:42 +08:00
h,
2021-09-11 17:43:07 +08:00
onMounted,
2024-01-26 05:07:42 +08:00
ref,
shallowRef,
2021-09-11 17:43:07 +08:00
watch,
reactive,
2024-01-26 05:07:42 +08:00
nextTick,
toRef
2021-09-11 17:43:07 +08:00
} from "vue";
2024-01-26 05:07:42 +08:00
import { getDarkmodeStatus } from "../../plugins/mermaid/helpers/darkmode.js";
2021-09-11 17:43:07 +08:00
export default defineComponent({
name: "Mermaid",
2024-01-26 05:07:42 +08:00
2021-09-11 17:43:07 +08:00
props: {
2024-01-26 05:07:42 +08:00
id: { type: String, required: true },
code: { type: String, required: true },
2021-09-11 17:43:07 +08:00
},
2024-01-26 05:07:42 +08:00
2021-09-11 17:43:07 +08:00
setup(props) {
const html = reactive({ innerHtml: "" });
2024-01-26 05:07:42 +08:00
const chartID = toRef(props, "id");
const rawGraph = toRef(props, "code");
const isDarkmode = ref(false);
const renderMermaid = async (): Promise<void> => {
2021-09-11 17:43:07 +08:00
const mermaid = await import("mermaid");
2024-01-26 05:07:42 +08:00
2021-09-11 17:43:07 +08:00
mermaid.default.initialize({
2024-01-26 05:07:42 +08:00
theme: isDarkmode.value ? "dark" : "default",
2021-09-11 17:43:07 +08:00
startOnLoad: false,
});
2024-01-26 05:07:42 +08:00
mermaid.default.render(chartID.value!, decodeURI(rawGraph.value!)).then(({ svg, bindFunctions }) => {
html.innerHtml = svg;
});
};
onMounted(() => {
isDarkmode.value = getDarkmodeStatus()
nextTick(renderMermaid)
})
// watch darkmode change
if (typeof document !== 'undefined') {
useMutationObserver(
document.documentElement,
() => {
isDarkmode.value = getDarkmodeStatus();
},
{
attributeFilter: ["class", "data-theme"],
attributes: true,
},
);
}
watch(isDarkmode, () => renderMermaid());
2021-09-11 17:43:07 +08:00
return {
tag: chartID,
2024-01-26 05:07:42 +08:00
payload: html
}
2021-09-11 17:43:07 +08:00
},
});
</script>
<style scoped></style>
2024-01-26 05:07:42 +08:00