Skip to content

Commit 50d1966

Browse files
committed
feat: Create Mermaid wrapper component
Adds Neo.component.wrapper.Mermaid, a declarative wrapper for the Mermaid Main thread addon. This component allows developers to easily embed Mermaid diagrams using a 'value' config, handling lifecycle management and remote rendering automatically. Closes #8377
1 parent 047f56b commit 50d1966

1 file changed

Lines changed: 109 additions & 0 deletions

File tree

src/component/wrapper/Mermaid.mjs

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import Component from '../Base.mjs';
2+
3+
/**
4+
* @summary A wrapper component for rendering Mermaid diagrams.
5+
*
6+
* This class abstracts the communication with the Main thread `Mermaid` addon, allowing developers
7+
* to use a declarative component to display Mermaid diagrams. It handles the lifecycle management,
8+
* ensuring the addon is loaded and the diagram is re-rendered when the `value` config changes.
9+
*
10+
* It uses the `Neo.main.addon.Mermaid` remote API to perform the actual rendering on the Main thread,
11+
* passing the diagram code and the target element ID.
12+
*
13+
* @class Neo.component.wrapper.Mermaid
14+
* @extends Neo.component.Base
15+
*/
16+
class Mermaid extends Component {
17+
static config = {
18+
/**
19+
* @member {String} className='Neo.component.wrapper.Mermaid'
20+
* @protected
21+
*/
22+
className: 'Neo.component.wrapper.Mermaid',
23+
/**
24+
* @member {String} ntype='mermaid'
25+
* @protected
26+
*/
27+
ntype: 'mermaid',
28+
/**
29+
* The mermaid diagram code.
30+
* Changing this value will automatically trigger a re-render of the diagram.
31+
* @member {String|null} value_=null
32+
* @reactive
33+
*/
34+
value_: null,
35+
/**
36+
* @member {Object} _vdom={cls: ['neo-mermaid']}
37+
*/
38+
_vdom: {cls: ['neo-mermaid']}
39+
}
40+
41+
/**
42+
* Stores the remote proxy instance of the Mermaid addon.
43+
* This proxy is retrieved during `initAsync` and is used to invoke methods on the Main thread.
44+
* @member {Object|null} addon=null
45+
* @protected
46+
*/
47+
addon = null
48+
49+
/**
50+
* Triggered after the component is mounted.
51+
* Checks if there is a value to render and triggers the rendering process.
52+
* @param {Boolean} value
53+
* @param {Boolean} oldValue
54+
* @protected
55+
*/
56+
afterSetMounted(value, oldValue) {
57+
super.afterSetMounted(value, oldValue);
58+
59+
if (value) {
60+
this.render()
61+
}
62+
}
63+
64+
/**
65+
* Triggered when the mermaid code value changes.
66+
* Triggers a re-render to update the diagram.
67+
* @param {String|null} value
68+
* @param {String|null} oldValue
69+
* @protected
70+
*/
71+
afterSetValue(value, oldValue) {
72+
if (value) {
73+
this.render()
74+
}
75+
}
76+
77+
/**
78+
* Initializes the component asynchronously.
79+
* Retrieves the Mermaid addon proxy from the current worker for the specific window.
80+
* This ensures the addon is loaded and ready for communication.
81+
* @returns {Promise<void>}
82+
*/
83+
async initAsync() {
84+
await super.initAsync();
85+
this.addon = await Neo.currentWorker.getAddon('Mermaid', this.windowId)
86+
}
87+
88+
/**
89+
* Renders the Mermaid diagram.
90+
* This method waits for the component to be fully initialized (ready) and then invokes
91+
* the `render` method on the remote addon proxy.
92+
* @returns {Promise<void>}
93+
*/
94+
async render() {
95+
let me = this;
96+
97+
if (me.mounted && me.value) {
98+
await me.ready();
99+
100+
await me.addon.render({
101+
code : me.value,
102+
id : me.id,
103+
windowId: me.windowId
104+
})
105+
}
106+
}
107+
}
108+
109+
export default Neo.setupClass(Mermaid);

0 commit comments

Comments
 (0)