From 32ca0b97fc0c08bf1f13b215b624cacc8aaf1eca Mon Sep 17 00:00:00 2001 From: Sidharth Vinod Date: Mon, 11 Mar 2024 14:07:41 +0530 Subject: [PATCH] feat: Make the examples interactive in the documentation site. Ctrl/Cmd + Enter and a run button is added. The feature was first implemented in https://github.com/mermaid-js/mermaid/pull/5330. This is a simplified version without introducing additional dependencies. Co-authored-by: Anton --- .../docs/.vitepress/mermaid-markdown-all.ts | 15 +---- .../src/docs/.vitepress/theme/Mermaid.vue | 64 +++++++++++++++++-- 2 files changed, 60 insertions(+), 19 deletions(-) diff --git a/packages/mermaid/src/docs/.vitepress/mermaid-markdown-all.ts b/packages/mermaid/src/docs/.vitepress/mermaid-markdown-all.ts index 30f044d98..643ce395e 100644 --- a/packages/mermaid/src/docs/.vitepress/mermaid-markdown-all.ts +++ b/packages/mermaid/src/docs/.vitepress/mermaid-markdown-all.ts @@ -18,20 +18,7 @@ const MermaidExample = async (md: MarkdownRenderer) => { 'Missing MarkdownIt highlight function (should be automatically created by vitepress' ); } - - // doing ```mermaid-example {line-numbers=5 highlight=14-17} is not supported - const langAttrs = ''; - return ` -
Code:
-
- - mermaid - ${ - // html is pre-escaped by the highlight function - // (it also adds `v-pre` to ignore Vue template syntax) - md.options.highlight(token.content, 'mermaid', langAttrs) - } -
`; + return ''; } else if (token.info.trim() === 'mermaid') { const key = index; return ` diff --git a/packages/mermaid/src/docs/.vitepress/theme/Mermaid.vue b/packages/mermaid/src/docs/.vitepress/theme/Mermaid.vue index 5012d3067..ee99bc884 100644 --- a/packages/mermaid/src/docs/.vitepress/theme/Mermaid.vue +++ b/packages/mermaid/src/docs/.vitepress/theme/Mermaid.vue @@ -1,4 +1,14 @@ @@ -18,15 +28,29 @@ const props = defineProps({ }); const svg = ref(''); +const code = ref(decodeURIComponent(props.graph)); +const ctrlSymbol = ref(navigator.platform.includes('Mac') ? '⌘' : 'Ctrl'); +const editableContent = ref(null); + let mut = null; +const updateCode = (event) => { + code.value = event.target.innerText; +}; + onMounted(async () => { mut = new MutationObserver(() => renderChart()); mut.observe(document.documentElement, { attributes: true }); + + // Set the initial value of the contenteditable element + // We cannot bind using `{{ code }}` because it will rerender the whole component + // when the value changes, shifting the cursor when enter is used + editableContent.value.textContent = code.value; + await renderChart(); //refresh images on first render - const hasImages = //.exec(decodeURIComponent(props.graph))?.length > 0; + const hasImages = //.exec(code.value)?.length > 0; if (hasImages) setTimeout(() => { let imgElements = document.getElementsByTagName('img'); @@ -51,16 +75,14 @@ onMounted(async () => { onUnmounted(() => mut.disconnect()); const renderChart = async () => { - console.log('rendering chart' + props.id + props.graph); + console.log('rendering chart' + props.id + code.value); const hasDarkClass = document.documentElement.classList.contains('dark'); const mermaidConfig = { securityLevel: 'loose', startOnLoad: false, theme: hasDarkClass ? 'dark' : 'default', }; - - console.log({ mermaidConfig }); - let svgCode = await render(props.id, decodeURIComponent(props.graph), mermaidConfig); + let svgCode = await render(props.id, code.value, mermaidConfig); // This is a hack to force v-html to re-render, otherwise the diagram disappears // when **switching themes** or **reloading the page**. // The cause is that the diagram is deleted during rendering (out of Vue's knowledge). @@ -70,3 +92,35 @@ const renderChart = async () => { svg.value = `${svgCode} ${salt}`; }; + +