Skip to content

Commit 98f8a64

Browse files
authored
fix: allow to change diagram (#61)
1 parent b725d7c commit 98f8a64

File tree

3 files changed

+66
-3
lines changed

3 files changed

+66
-3
lines changed
Lines changed: 3 additions & 0 deletions
Loading

src/index.spec.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,46 @@ import testerPluginPuppeteer from '@dword-design/tester-plugin-puppeteer'
55

66
export default tester(
77
{
8+
'change value': {
9+
page: endent`
10+
<template>
11+
<div>
12+
<self class="foo" :value="diagram" />
13+
<button @click="change" />
14+
</div>
15+
</template>
16+
17+
<script>
18+
import { endent } from '@dword-design/functions'
19+
20+
export default {
21+
data: () => ({
22+
diagram: endent\`
23+
graph TD
24+
A --> B
25+
\`,
26+
}),
27+
methods: {
28+
change() {
29+
this.diagram = endent\`
30+
graph TD
31+
B --> C
32+
\`
33+
},
34+
},
35+
}
36+
</script>
37+
38+
`,
39+
async test() {
40+
await this.page.goto('http://localhost:3000')
41+
await this.page.waitForSelector('.foo')
42+
await this.page.click('button')
43+
expect(
44+
await this.page.screenshot({ fullPage: true })
45+
).toMatchImageSnapshot(this)
46+
},
47+
},
848
click: {
949
page: endent`
1050
<template>

src/index.vue

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,33 @@ export default {
1313
},
1414
id: () => nanoid(),
1515
},
16+
methods: {
17+
update() {
18+
if (typeof window !== 'undefined') {
19+
const mermaid = window.mermaid || require('mermaid').default
20+
mermaid.parseError = error => this.$emit('parse-error', error)
21+
this.$el.removeAttribute('data-processed')
22+
this.$el.replaceChild(
23+
document.createTextNode(this.finalValue),
24+
this.$el.firstChild
25+
)
26+
mermaid.init(this.finalValue, this.$el)
27+
}
28+
},
29+
},
1630
mounted() {
1731
if (typeof window !== 'undefined') {
18-
const mermaid = window.mermaid || require('mermaid').default
1932
window[`mermaidClick_${this.id}`] = id => this.$emit('node-click', id)
20-
mermaid.parseError = error => this.$emit('parse-error', error)
33+
34+
const mermaid = window.mermaid || require('mermaid').default
2135
mermaid.initialize({
2236
securityLevel: 'loose',
2337
startOnLoad: false,
2438
theme: 'default',
2539
})
26-
mermaid.init(this.finalValue, this.$el)
2740
}
41+
42+
return this.update()
2843
},
2944
name: 'VueMermaidString',
3045
props: {
@@ -33,5 +48,10 @@ export default {
3348
render() {
3449
return <div>{this.finalValue}</div>
3550
},
51+
watch: {
52+
finalValue() {
53+
return this.update()
54+
},
55+
},
3656
}
3757
</script>

0 commit comments

Comments
 (0)