|
| 1 | +<!-- |
| 2 | + ~ Copyright 2023 Exactpro (Exactpro Systems Limited) |
| 3 | + ~ |
| 4 | + ~ Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + ~ you may not use this file except in compliance with the License. |
| 6 | + ~ You may obtain a copy of the License at |
| 7 | + ~ |
| 8 | + ~ http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + ~ |
| 10 | + ~ Unless required by applicable law or agreed to in writing, software |
| 11 | + ~ distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + ~ See the License for the specific language governing permissions and |
| 14 | + ~ limitations under the License. |
| 15 | + --> |
| 16 | + |
| 17 | +<script setup lang="ts"> |
| 18 | +const { $mermaid } = useNuxtApp() |
| 19 | +
|
| 20 | +const props = defineProps<{ |
| 21 | + code: string |
| 22 | +}>() |
| 23 | +
|
| 24 | +const decodedCode = computed(() => { |
| 25 | + return atob(props.code) |
| 26 | +}) |
| 27 | +
|
| 28 | +const root = ref<HTMLElement | null>(null) |
| 29 | +const codeBlock = ref<HTMLElement | null>(null) |
| 30 | +const isDiagramLoading = ref(true) |
| 31 | +
|
| 32 | +async function renderMermaidDiagram() { |
| 33 | + isDiagramLoading.value = true |
| 34 | + if (codeBlock.value && $mermaid) { |
| 35 | + try { |
| 36 | + await $mermaid.run({ |
| 37 | + nodes: [codeBlock.value], |
| 38 | + suppressErrors: true |
| 39 | + }) |
| 40 | + } catch (e) {} |
| 41 | +
|
| 42 | + isDiagramLoading.value = false |
| 43 | + } |
| 44 | +} |
| 45 | +
|
| 46 | +onMounted(() => { |
| 47 | + renderMermaidDiagram() |
| 48 | +}) |
| 49 | +</script> |
| 50 | + |
| 51 | +<template> |
| 52 | + <figure ref="root" class="relative"> |
| 53 | + <pre |
| 54 | + ref="codeBlock" |
| 55 | + style="background: none" |
| 56 | + :class="{ |
| 57 | + 'opacity-0': isDiagramLoading |
| 58 | + }" |
| 59 | + v-text="decodedCode" |
| 60 | + ></pre> |
| 61 | + <div> |
| 62 | + <div v-if="isDiagramLoading" class="absolute inset-0 font-serif"> |
| 63 | + Mermaid diagram is loading... |
| 64 | + </div> |
| 65 | + </div> |
| 66 | + </figure> |
| 67 | +</template> |
0 commit comments