@dxos/plugin-mermaid 0.8.3 → 0.8.4-main.03d5cd7b56

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/dist/lib/neutral/MermaidPlugin.mjs +179 -0
  2. package/dist/lib/neutral/MermaidPlugin.mjs.map +7 -0
  3. package/dist/lib/neutral/chunk-JGZ2QEV6.mjs +17 -0
  4. package/dist/lib/neutral/chunk-JGZ2QEV6.mjs.map +7 -0
  5. package/dist/lib/neutral/index.mjs +7 -0
  6. package/dist/lib/neutral/index.mjs.map +7 -0
  7. package/dist/lib/neutral/meta.json +1 -0
  8. package/dist/lib/neutral/meta.mjs +7 -0
  9. package/dist/lib/neutral/meta.mjs.map +7 -0
  10. package/dist/lib/neutral/plugin.mjs +11 -0
  11. package/dist/lib/neutral/plugin.mjs.map +7 -0
  12. package/dist/types/src/MermaidPlugin.d.ts +3 -1
  13. package/dist/types/src/MermaidPlugin.d.ts.map +1 -1
  14. package/dist/types/src/MermaidPlugin.test.d.ts +2 -0
  15. package/dist/types/src/MermaidPlugin.test.d.ts.map +1 -0
  16. package/dist/types/src/extensions/index.d.ts +1 -1
  17. package/dist/types/src/extensions/index.d.ts.map +1 -1
  18. package/dist/types/src/extensions/mermaid-extension.d.ts +7 -0
  19. package/dist/types/src/extensions/mermaid-extension.d.ts.map +1 -0
  20. package/dist/types/src/extensions/mermaid.stories.d.ts +11 -11
  21. package/dist/types/src/extensions/mermaid.stories.d.ts.map +1 -1
  22. package/dist/types/src/index.d.ts +0 -1
  23. package/dist/types/src/index.d.ts.map +1 -1
  24. package/dist/types/src/meta.d.ts +2 -3
  25. package/dist/types/src/meta.d.ts.map +1 -1
  26. package/dist/types/src/plugin.d.ts +3 -0
  27. package/dist/types/src/plugin.d.ts.map +1 -0
  28. package/dist/types/tsconfig.tsbuildinfo +1 -1
  29. package/package.json +50 -31
  30. package/src/MermaidPlugin.test.ts +30 -0
  31. package/src/MermaidPlugin.tsx +15 -10
  32. package/src/extensions/index.ts +1 -1
  33. package/src/extensions/mermaid-extension.ts +215 -0
  34. package/src/extensions/mermaid.stories.tsx +20 -21
  35. package/src/index.ts +0 -1
  36. package/src/meta.ts +8 -6
  37. package/src/plugin.ts +9 -0
  38. package/dist/lib/browser/index.mjs +0 -132
  39. package/dist/lib/browser/index.mjs.map +0 -7
  40. package/dist/lib/browser/meta.json +0 -1
  41. package/dist/types/src/extensions/mermaid.d.ts +0 -4
  42. package/dist/types/src/extensions/mermaid.d.ts.map +0 -1
  43. package/src/extensions/mermaid.ts +0 -155
package/package.json CHANGED
@@ -1,58 +1,77 @@
1
1
  {
2
2
  "name": "@dxos/plugin-mermaid",
3
- "version": "0.8.3",
3
+ "version": "0.8.4-main.03d5cd7b56",
4
4
  "description": "Mermaid markdown plugin",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/dxos/dxos"
10
+ },
7
11
  "license": "MIT",
8
12
  "author": "DXOS.org",
9
13
  "sideEffects": true,
10
14
  "type": "module",
15
+ "imports": {
16
+ "#meta": {
17
+ "source": "./src/meta.ts",
18
+ "types": "./dist/types/src/meta.d.ts",
19
+ "default": "./dist/lib/neutral/meta.mjs"
20
+ },
21
+ "#plugin": {
22
+ "source": "./src/MermaidPlugin.tsx",
23
+ "types": "./dist/types/src/MermaidPlugin.d.ts",
24
+ "default": "./dist/lib/neutral/MermaidPlugin.mjs"
25
+ }
26
+ },
11
27
  "exports": {
12
28
  ".": {
29
+ "source": "./src/index.ts",
13
30
  "types": "./dist/types/src/index.d.ts",
14
- "browser": "./dist/lib/browser/index.mjs"
31
+ "default": "./dist/lib/neutral/index.mjs"
32
+ },
33
+ "./plugin": {
34
+ "source": "./src/plugin.ts",
35
+ "types": "./dist/types/src/plugin.d.ts",
36
+ "default": "./dist/lib/neutral/plugin.mjs"
15
37
  }
16
38
  },
17
39
  "types": "dist/types/src/index.d.ts",
18
- "typesVersions": {
19
- "*": {}
20
- },
21
40
  "files": [
22
41
  "dist",
23
42
  "src"
24
43
  ],
25
44
  "dependencies": {
26
- "@codemirror/language": "^6.10.3",
27
- "@codemirror/state": "^6.4.1",
28
- "@codemirror/view": "^6.34.1",
29
- "@preact-signals/safe-react": "^0.9.0",
30
- "effect": "^3.13.3",
31
- "mermaid": "^10.6.1",
32
- "@dxos/app-framework": "0.8.3",
33
- "@dxos/plugin-markdown": "0.8.3",
34
- "@dxos/plugin-space": "0.8.3",
35
- "@dxos/react-ui-editor": "0.8.3"
45
+ "@codemirror/language": "^6.11.3",
46
+ "@codemirror/state": "^6.5.2",
47
+ "@codemirror/view": "^6.38.5",
48
+ "mermaid": "^10.9.4",
49
+ "@dxos/app-framework": "0.8.4-main.03d5cd7b56",
50
+ "@dxos/plugin-markdown": "0.8.4-main.03d5cd7b56",
51
+ "@dxos/operation": "0.8.4-main.03d5cd7b56",
52
+ "@dxos/plugin-space": "0.8.4-main.03d5cd7b56",
53
+ "@dxos/react-ui-editor": "0.8.4-main.03d5cd7b56",
54
+ "@dxos/util": "0.8.4-main.03d5cd7b56",
55
+ "@dxos/ui-editor": "0.8.4-main.03d5cd7b56"
36
56
  },
37
57
  "devDependencies": {
38
- "@phosphor-icons/react": "^2.1.5",
39
- "@types/react": "~18.2.0",
40
- "@types/react-dom": "~18.2.0",
41
- "react": "~18.2.0",
42
- "react-dom": "~18.2.0",
43
- "vite": "5.4.7",
44
- "@dxos/echo-schema": "0.8.3",
45
- "@dxos/react-ui": "0.8.3",
46
- "@dxos/react-ui-theme": "0.8.3",
47
- "@dxos/storybook-utils": "0.8.3"
58
+ "@types/react": "~19.2.7",
59
+ "@types/react-dom": "~19.2.3",
60
+ "effect": "3.20.0",
61
+ "react": "~19.2.3",
62
+ "react-dom": "~19.2.3",
63
+ "vite": "^8.0.10",
64
+ "@dxos/react-ui": "0.8.4-main.03d5cd7b56",
65
+ "@dxos/echo": "0.8.4-main.03d5cd7b56",
66
+ "@dxos/storybook-utils": "0.8.4-main.03d5cd7b56",
67
+ "@dxos/ui-theme": "0.8.4-main.03d5cd7b56"
48
68
  },
49
69
  "peerDependencies": {
50
- "@phosphor-icons/react": "^2.1.5",
51
- "effect": "^3.13.3",
52
- "react": "~18.2.0",
53
- "react-dom": "~18.2.0",
54
- "@dxos/react-ui": "0.8.3",
55
- "@dxos/react-ui-theme": "0.8.3"
70
+ "effect": "3.20.0",
71
+ "react": "~19.2.3",
72
+ "react-dom": "~19.2.3",
73
+ "@dxos/react-ui": "0.8.4-main.03d5cd7b56",
74
+ "@dxos/ui-theme": "0.8.4-main.03d5cd7b56"
56
75
  },
57
76
  "publishConfig": {
58
77
  "access": "public"
@@ -0,0 +1,30 @@
1
+ //
2
+ // Copyright 2026 DXOS.org
3
+ //
4
+
5
+ import { describe, test } from 'vitest';
6
+
7
+ import { OperationPlugin, RuntimePlugin } from '@dxos/app-framework';
8
+ import { createTestApp } from '@dxos/app-framework/testing';
9
+ import { MarkdownEvents } from '@dxos/plugin-markdown';
10
+
11
+ import { MermaidPlugin } from '#plugin';
12
+
13
+ import { meta } from './meta';
14
+
15
+ const moduleId = (name: string) => `${meta.id}.module.${name}`;
16
+
17
+ describe('MermaidPlugin', () => {
18
+ test('modules activate on the expected events', async ({ expect }) => {
19
+ // Use createTestApp directly — only RuntimePlugin and OperationPlugin are needed.
20
+ // MermaidPlugin has no surface module so SetupReactSurface firing harmlessly is fine.
21
+ await using harness = await createTestApp({
22
+ plugins: [OperationPlugin(), RuntimePlugin(), MermaidPlugin()],
23
+ });
24
+
25
+ // The markdown module activates on MarkdownEvents.SetupExtensions,
26
+ // which is fired by MarkdownPlugin before its ReactSurface activates.
27
+ await harness.fire(MarkdownEvents.SetupExtensions);
28
+ expect(harness.manager.getActive()).toContain(moduleId('markdown'));
29
+ });
30
+ });
@@ -2,17 +2,22 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- import { contributes, defineModule, definePlugin } from '@dxos/app-framework';
5
+ import * as Effect from 'effect/Effect';
6
+
7
+ import { Capability, Plugin } from '@dxos/app-framework';
6
8
  import { MarkdownCapabilities, MarkdownEvents } from '@dxos/plugin-markdown';
7
9
 
10
+ import { meta } from '#meta';
11
+
8
12
  import { mermaid } from './extensions';
9
- import { meta } from './meta';
10
13
 
11
- export const MermaidPlugin = () =>
12
- definePlugin(meta, [
13
- defineModule({
14
- id: `${meta.id}/module/markdown`,
15
- activatesOn: MarkdownEvents.SetupExtensions,
16
- activate: () => contributes(MarkdownCapabilities.Extensions, [mermaid]),
17
- }),
18
- ]);
14
+ export const MermaidPlugin = Plugin.define(meta).pipe(
15
+ Plugin.addModule({
16
+ id: 'markdown',
17
+ activatesOn: MarkdownEvents.SetupExtensions,
18
+ activate: () => Effect.succeed(Capability.contributes(MarkdownCapabilities.Extensions, [mermaid])),
19
+ }),
20
+ Plugin.make,
21
+ );
22
+
23
+ export default MermaidPlugin;
@@ -2,4 +2,4 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- export * from './mermaid';
5
+ export * from './mermaid-extension';
@@ -0,0 +1,215 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import { syntaxTree } from '@codemirror/language';
6
+ import { type Extension } from '@codemirror/state';
7
+ import { Decoration, type DecorationSet, EditorView, ViewPlugin, type ViewUpdate, WidgetType } from '@codemirror/view';
8
+ import _mermaid from 'mermaid';
9
+
10
+ export type MermaidOptions = {};
11
+
12
+ /**
13
+ * Extension to create mermaid diagrams.
14
+ */
15
+ export const mermaid = (_options: MermaidOptions = {}): Extension => {
16
+ return [
17
+ ViewPlugin.fromClass(
18
+ class {
19
+ decorations: DecorationSet;
20
+
21
+ constructor(view: EditorView) {
22
+ this.decorations = this.buildDecorations(view);
23
+ }
24
+
25
+ update(update: ViewUpdate) {
26
+ // Always rebuild decorations when selection changes to handle arrow key navigation.
27
+ if (update.docChanged || update.viewportChanged || update.selectionSet || update.focusChanged) {
28
+ this.decorations = this.buildDecorations(update.view);
29
+ }
30
+ }
31
+
32
+ buildDecorations(view: EditorView): DecorationSet {
33
+ const decorations: Array<{ from: number; to: number; decoration: Decoration }> = [];
34
+
35
+ syntaxTree(view.state).iterate({
36
+ enter: (node) => {
37
+ if (node.name === 'FencedCode') {
38
+ const cursor = view.state.selection.main.head;
39
+ // Show widget when cursor is outside the code block.
40
+ // Add a buffer of 1 position before and after to handle edge cases.
41
+ const showWidget = view.state.readOnly || cursor < node.from - 1 || cursor > node.to + 1;
42
+ if (showWidget) {
43
+ const info = node.node.getChild('CodeInfo');
44
+ if (info) {
45
+ const type = view.state.sliceDoc(info.from, info.to);
46
+ const text = node.node.getChild('CodeText');
47
+ if (type === 'mermaid' && text) {
48
+ const content = view.state.sliceDoc(text.from, text.to).trim();
49
+ const label = content.split(' ')[0];
50
+
51
+ // Create widget.
52
+ const widget = new MermaidWidget(`mermaid-${node.from}`, content, label);
53
+
54
+ // Find the line after the code block to place the widget.
55
+ const endLine = view.state.doc.lineAt(node.to);
56
+ const nextLinePos = endLine.to + 1;
57
+
58
+ // Add widget after the code block.
59
+ if (nextLinePos <= view.state.doc.length) {
60
+ decorations.push({
61
+ from: nextLinePos,
62
+ to: nextLinePos,
63
+ decoration: Decoration.widget({
64
+ widget,
65
+ }),
66
+ });
67
+ } else {
68
+ // Check if at the end of the document.
69
+ decorations.push({
70
+ from: endLine.to,
71
+ to: endLine.to,
72
+ decoration: Decoration.widget({
73
+ widget,
74
+ side: 1,
75
+ }),
76
+ });
77
+ }
78
+
79
+ // Hide each line of the code block.
80
+ const startLine = view.state.doc.lineAt(node.from);
81
+ for (let lineNum = startLine.number; lineNum <= endLine.number; lineNum++) {
82
+ const line = view.state.doc.line(lineNum);
83
+ decorations.push({
84
+ from: line.from,
85
+ to: line.from,
86
+ decoration: Decoration.line({
87
+ class: 'cm-mermaid-hidden',
88
+ }),
89
+ });
90
+ }
91
+ }
92
+ }
93
+ }
94
+ }
95
+ },
96
+ });
97
+
98
+ return Decoration.set(
99
+ decorations.sort((a, b) => a.from - b.from || a.to - b.to).map((d) => d.decoration.range(d.from, d.to)),
100
+ );
101
+ }
102
+ },
103
+ {
104
+ decorations: (v) => v.decorations,
105
+ },
106
+ ),
107
+ EditorView.theme({
108
+ '& .cm-mermaid': {
109
+ position: 'relative',
110
+ display: 'inline-flex',
111
+ width: '100%',
112
+ maring: '4px 0',
113
+ padding: '16px',
114
+ justifyContent: 'center',
115
+ backgroundColor: 'var(--color-group-surface)',
116
+ borderRadius: '8px',
117
+ },
118
+ '& .cm-mermaid-label': {
119
+ position: 'absolute',
120
+ right: '16px',
121
+ fontFamily: 'unset',
122
+ color: 'var(--color-subdued)',
123
+ },
124
+ '& .cm-mermaid-error': {
125
+ display: 'inline-block',
126
+ color: 'var(--color-error)',
127
+ },
128
+ '& .cm-mermaid-hidden': {
129
+ display: 'none !important',
130
+ },
131
+ }),
132
+ ];
133
+ };
134
+
135
+ class MermaidWidget extends WidgetType {
136
+ _svg: string | undefined;
137
+ _error: string | undefined;
138
+
139
+ // TODO(burdon): Mermaid API requires unique id.
140
+ constructor(
141
+ private readonly _id: string,
142
+ private readonly _source: string,
143
+ private readonly _label?: string,
144
+ ) {
145
+ super();
146
+ }
147
+
148
+ override eq(other: this) {
149
+ return this._source === other._source;
150
+ }
151
+
152
+ override ignoreEvent(ev: Event) {
153
+ return !/^mouse/.test(ev.type);
154
+ }
155
+
156
+ override toDOM(view: EditorView) {
157
+ const div = document.createElement('div');
158
+ div.className = 'cm-mermaid';
159
+
160
+ setTimeout(async () => {
161
+ // https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/config.type.ts
162
+ _mermaid.initialize({
163
+ darkMode: view.state.facet(EditorView.darkTheme),
164
+ theme: 'neutral',
165
+ // TODO(burdon): Styles.
166
+ // NOTE: Must specify 'base' in order to override.
167
+ // theme: 'base',
168
+ // themeVariables: {
169
+ // primaryColor: getToken('extend.colors.red.100'),
170
+ // primaryBorderColor: getToken('extend.colors.neutral.200'),
171
+ // },
172
+ // https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/diagrams/flowchart/styles.ts
173
+ // https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/diagrams/sequence/styles.js
174
+ // https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/diagrams/state/styles.js
175
+ // themeCSS: '.node rect { fill: red; }',
176
+ });
177
+
178
+ // TODO(burdon): Cache?
179
+ const svg = await this.render(div);
180
+ if (this._error) {
181
+ div.className = 'cm-mermaid-error';
182
+ div.innerText = this._error;
183
+ } else {
184
+ div.className = 'cm-mermaid';
185
+ div.innerHTML = svg!;
186
+
187
+ if (this._label) {
188
+ const label = document.createElement('span');
189
+ label.innerText = this._label;
190
+ label.className = 'cm-mermaid-label';
191
+ div.appendChild(label);
192
+ view.requestMeasure();
193
+ }
194
+ }
195
+ });
196
+
197
+ return div;
198
+ }
199
+
200
+ async render(_container: Element): Promise<string | undefined> {
201
+ try {
202
+ // https://github.com/mermaid-js/mermaid
203
+ const valid = await _mermaid.parse(this._source);
204
+ if (valid) {
205
+ const result = await _mermaid.render(this._id, this._source);
206
+ this._error = undefined;
207
+ this._svg = result.svg;
208
+ return result.svg;
209
+ }
210
+ } catch (err: any) {
211
+ this._error = String(err);
212
+ this._svg = undefined;
213
+ }
214
+ }
215
+ }
@@ -2,39 +2,36 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- import '@dxos-theme';
6
-
7
- import { type Meta } from '@storybook/react';
5
+ import { type Meta, type StoryObj } from '@storybook/react-vite';
8
6
  import React from 'react';
9
7
 
10
8
  import { useThemeContext } from '@dxos/react-ui';
9
+ import { useTextEditor } from '@dxos/react-ui-editor';
10
+ import { withLayout, withTheme } from '@dxos/react-ui/testing';
11
11
  import {
12
12
  createBasicExtensions,
13
13
  createMarkdownExtensions,
14
14
  createThemeExtensions,
15
15
  decorateMarkdown,
16
- useTextEditor,
17
- } from '@dxos/react-ui-editor';
18
- import { withTheme, withLayout } from '@dxos/storybook-utils';
16
+ } from '@dxos/ui-editor';
19
17
 
20
- import { mermaid } from './mermaid';
18
+ import { mermaid } from './mermaid-extension';
21
19
 
22
20
  const str = (...lines: string[]) => lines.join('\n');
23
21
 
24
- type StoryProps = {
22
+ type DefaultStoryProps = {
25
23
  text?: string;
26
24
  };
27
25
 
28
- const DefaultStory = ({ text }: StoryProps) => {
26
+ const DefaultStory = ({ text }: DefaultStoryProps) => {
29
27
  const { themeMode } = useThemeContext();
30
28
  const { parentRef, focusAttributes } = useTextEditor(
31
29
  () => ({
32
30
  initialValue: text,
33
31
  extensions: [
34
32
  createBasicExtensions(),
35
- createMarkdownExtensions({ themeMode }),
33
+ createMarkdownExtensions(),
36
34
  createThemeExtensions({ themeMode, syntaxHighlighting: true }),
37
- // TODO(burdon): Bug if mermaid extension is provided after decorateMarkdown.
38
35
  mermaid(),
39
36
  decorateMarkdown(),
40
37
  ],
@@ -45,7 +42,17 @@ const DefaultStory = ({ text }: StoryProps) => {
45
42
  return <div className='w-[50rem]' ref={parentRef} {...focusAttributes} />;
46
43
  };
47
44
 
48
- export const Default = {
45
+ const meta = {
46
+ title: 'plugins/plugin-mermaid/extensions/mermaid',
47
+ render: DefaultStory,
48
+ decorators: [withTheme(), withLayout({ layout: 'column' })],
49
+ } satisfies Meta<typeof DefaultStory>;
50
+
51
+ export default meta;
52
+
53
+ type Story = StoryObj<typeof meta>;
54
+
55
+ export const Default: Story = {
49
56
  args: {
50
57
  text: str(
51
58
  '# Mermaid',
@@ -68,7 +75,7 @@ export const Default = {
68
75
  },
69
76
  };
70
77
 
71
- export const Error = {
78
+ export const Error: Story = {
72
79
  args: {
73
80
  text: str(
74
81
  '# Mermaid',
@@ -85,11 +92,3 @@ export const Error = {
85
92
  ),
86
93
  },
87
94
  };
88
-
89
- const meta: Meta = {
90
- title: 'plugins/plugin-mermaid/extensions',
91
- render: DefaultStory,
92
- decorators: [withTheme, withLayout({ fullscreen: true, classNames: 'justify-center' })],
93
- };
94
-
95
- export default meta;
package/src/index.ts CHANGED
@@ -2,5 +2,4 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- export * from './MermaidPlugin';
6
5
  export * from './meta';
package/src/meta.ts CHANGED
@@ -2,14 +2,16 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- import { type PluginMeta } from '@dxos/app-framework';
5
+ import { type Plugin } from '@dxos/app-framework';
6
+ import { trim } from '@dxos/util';
6
7
 
7
- export const MERMAID_PLUGIN = 'dxos.org/plugin/mermaid';
8
-
9
- export const meta: PluginMeta = {
10
- id: MERMAID_PLUGIN,
8
+ export const meta: Plugin.Meta = {
9
+ id: 'org.dxos.plugin.mermaid',
11
10
  name: 'Mermaid',
12
- description: 'Tool that renders text definitions to create diagrams.',
11
+ description: trim`
12
+ Generate diagrams from simple text-based definitions using Mermaid syntax.
13
+ Create flowcharts, sequence diagrams, and other visualizations that stay in sync with your documentation.
14
+ `,
13
15
  icon: 'ph--anchor-simple--regular',
14
16
  source: 'https://github.com/dxos/dxos/tree/main/packages/plugins/plugin-mermaid',
15
17
  };
package/src/plugin.ts ADDED
@@ -0,0 +1,9 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ import { Plugin } from '@dxos/app-framework';
6
+
7
+ import { meta } from './meta';
8
+
9
+ export const MermaidPlugin = Plugin.lazy(meta, () => import('#plugin'));
@@ -1,132 +0,0 @@
1
- // packages/plugins/plugin-mermaid/src/MermaidPlugin.tsx
2
- import { contributes, defineModule, definePlugin } from "@dxos/app-framework";
3
- import { MarkdownCapabilities, MarkdownEvents } from "@dxos/plugin-markdown";
4
-
5
- // packages/plugins/plugin-mermaid/src/extensions/mermaid.ts
6
- import { syntaxTree } from "@codemirror/language";
7
- import { RangeSetBuilder, StateField } from "@codemirror/state";
8
- import { Decoration, EditorView, WidgetType } from "@codemirror/view";
9
- import _mermaid from "mermaid";
10
- var mermaid = (options = {}) => {
11
- return [
12
- StateField.define({
13
- create: (state) => update(state, options),
14
- update: (_, tr) => update(tr.state, options),
15
- provide: (field) => EditorView.decorations.from(field)
16
- }),
17
- styles
18
- ];
19
- };
20
- var update = (state, options) => {
21
- const builder = new RangeSetBuilder();
22
- syntaxTree(state).iterate({
23
- enter: (node) => {
24
- if (node.name === "FencedCode") {
25
- const cursor = state.selection.main.head;
26
- if (state.readOnly || cursor < node.from || cursor > node.to) {
27
- const info = node.node.getChild("CodeInfo");
28
- if (info) {
29
- const type = state.sliceDoc(info.from, info.to);
30
- const text = node.node.getChild("CodeText");
31
- if (type === "mermaid" && text) {
32
- const content = state.sliceDoc(text.from, text.to);
33
- builder.add(node.from, node.to, Decoration.replace({
34
- widget: new MermaidWidget(`mermaid-${node.from}`, content)
35
- }));
36
- }
37
- }
38
- }
39
- }
40
- }
41
- });
42
- return builder.finish();
43
- };
44
- var MermaidWidget = class extends WidgetType {
45
- // TODO(burdon): Mermaid API requires unique id.
46
- constructor(_id, _source) {
47
- super(), this._id = _id, this._source = _source;
48
- }
49
- eq(other) {
50
- return this._source === other._source;
51
- }
52
- toDOM(view) {
53
- const div = document.createElement("div");
54
- setTimeout(async () => {
55
- _mermaid.initialize({
56
- darkMode: view.state.facet(EditorView.darkTheme),
57
- theme: "neutral"
58
- });
59
- const svg = await this.render(div);
60
- if (this._error) {
61
- div.className = "cm-mermaid-error";
62
- div.innerText = this._error;
63
- } else {
64
- div.className = "cm-mermaid";
65
- div.innerHTML = svg;
66
- view.requestMeasure();
67
- }
68
- });
69
- return div;
70
- }
71
- async render(container) {
72
- try {
73
- const valid = await _mermaid.parse(this._source);
74
- if (valid) {
75
- const result = await _mermaid.render(this._id, this._source);
76
- this._error = void 0;
77
- this._svg = result.svg;
78
- return result.svg;
79
- }
80
- } catch (err) {
81
- this._error = String(err);
82
- this._svg = void 0;
83
- }
84
- }
85
- ignoreEvent(e) {
86
- return !/^mouse/.test(e.type);
87
- }
88
- };
89
- var styles = EditorView.theme({
90
- "& .cm-mermaid": {
91
- position: "relative",
92
- display: "inline-flex",
93
- width: "100%",
94
- justifyContent: "center"
95
- },
96
- // '& .cm-mermaid-label': {
97
- // position: 'absolute',
98
- // right: 0,
99
- // textSize: 10,
100
- // },
101
- "& .cm-mermaid-error": {
102
- display: "inline-block",
103
- color: "var(--dx-errorText)"
104
- }
105
- });
106
-
107
- // packages/plugins/plugin-mermaid/src/meta.ts
108
- var MERMAID_PLUGIN = "dxos.org/plugin/mermaid";
109
- var meta = {
110
- id: MERMAID_PLUGIN,
111
- name: "Mermaid",
112
- description: "Tool that renders text definitions to create diagrams.",
113
- icon: "ph--anchor-simple--regular",
114
- source: "https://github.com/dxos/dxos/tree/main/packages/plugins/plugin-mermaid"
115
- };
116
-
117
- // packages/plugins/plugin-mermaid/src/MermaidPlugin.tsx
118
- var MermaidPlugin = () => definePlugin(meta, [
119
- defineModule({
120
- id: `${meta.id}/module/markdown`,
121
- activatesOn: MarkdownEvents.SetupExtensions,
122
- activate: () => contributes(MarkdownCapabilities.Extensions, [
123
- mermaid
124
- ])
125
- })
126
- ]);
127
- export {
128
- MERMAID_PLUGIN,
129
- MermaidPlugin,
130
- meta
131
- };
132
- //# sourceMappingURL=index.mjs.map