@dxos/plugin-mermaid 0.8.4-main.f9ba587 → 0.8.4-main.fcfe5033a5
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.
- package/dist/lib/browser/index.mjs +133 -74
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/types/src/MermaidPlugin.d.ts +2 -1
- package/dist/types/src/MermaidPlugin.d.ts.map +1 -1
- package/dist/types/src/extensions/index.d.ts +1 -1
- package/dist/types/src/extensions/index.d.ts.map +1 -1
- package/dist/types/src/extensions/mermaid-extension.d.ts +7 -0
- package/dist/types/src/extensions/mermaid-extension.d.ts.map +1 -0
- package/dist/types/src/extensions/mermaid.stories.d.ts +11 -11
- package/dist/types/src/extensions/mermaid.stories.d.ts.map +1 -1
- package/dist/types/src/meta.d.ts +2 -3
- package/dist/types/src/meta.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +35 -25
- package/src/MermaidPlugin.tsx +13 -10
- package/src/extensions/index.ts +1 -1
- package/src/extensions/mermaid-extension.ts +215 -0
- package/src/extensions/mermaid.stories.tsx +20 -21
- package/src/meta.ts +8 -6
- package/dist/lib/node-esm/index.mjs +0 -134
- package/dist/lib/node-esm/index.mjs.map +0 -7
- package/dist/lib/node-esm/meta.json +0 -1
- package/dist/types/src/extensions/mermaid.d.ts +0 -4
- package/dist/types/src/extensions/mermaid.d.ts.map +0 -1
- package/src/extensions/mermaid.ts +0 -155
|
@@ -1,155 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2024 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { syntaxTree } from '@codemirror/language';
|
|
6
|
-
import {
|
|
7
|
-
type EditorState,
|
|
8
|
-
type Extension,
|
|
9
|
-
type RangeSet,
|
|
10
|
-
RangeSetBuilder,
|
|
11
|
-
StateField,
|
|
12
|
-
type Transaction,
|
|
13
|
-
} from '@codemirror/state';
|
|
14
|
-
import { Decoration, EditorView, WidgetType } from '@codemirror/view';
|
|
15
|
-
import _mermaid from 'mermaid';
|
|
16
|
-
|
|
17
|
-
export type MermaidOptions = {};
|
|
18
|
-
|
|
19
|
-
export const mermaid = (options: MermaidOptions = {}): Extension => {
|
|
20
|
-
return [
|
|
21
|
-
StateField.define<RangeSet<Decoration>>({
|
|
22
|
-
create: (state) => update(state, options),
|
|
23
|
-
update: (_: RangeSet<Decoration>, tr: Transaction) => update(tr.state, options),
|
|
24
|
-
provide: (field) => EditorView.decorations.from(field),
|
|
25
|
-
}),
|
|
26
|
-
styles,
|
|
27
|
-
];
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
const update = (state: EditorState, options: MermaidOptions) => {
|
|
31
|
-
const builder = new RangeSetBuilder<Decoration>();
|
|
32
|
-
syntaxTree(state).iterate({
|
|
33
|
-
enter: (node) => {
|
|
34
|
-
if (node.name === 'FencedCode') {
|
|
35
|
-
const cursor = state.selection.main.head;
|
|
36
|
-
if (state.readOnly || cursor < node.from || cursor > node.to) {
|
|
37
|
-
const info = node.node.getChild('CodeInfo');
|
|
38
|
-
if (info) {
|
|
39
|
-
const type = state.sliceDoc(info.from, info.to);
|
|
40
|
-
const text = node.node.getChild('CodeText');
|
|
41
|
-
if (type === 'mermaid' && text) {
|
|
42
|
-
const content = state.sliceDoc(text.from, text.to);
|
|
43
|
-
// TODO(burdon): Error if extension defined AFTER decorateMarkdown.
|
|
44
|
-
// Decorations that replace line breaks may not be specified via plugins
|
|
45
|
-
builder.add(
|
|
46
|
-
node.from,
|
|
47
|
-
node.to,
|
|
48
|
-
Decoration.replace({
|
|
49
|
-
widget: new MermaidWidget(`mermaid-${node.from}`, content),
|
|
50
|
-
}),
|
|
51
|
-
);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
},
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
return builder.finish();
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
class MermaidWidget extends WidgetType {
|
|
63
|
-
_svg: string | undefined;
|
|
64
|
-
_error: string | undefined;
|
|
65
|
-
|
|
66
|
-
// TODO(burdon): Mermaid API requires unique id.
|
|
67
|
-
constructor(
|
|
68
|
-
private readonly _id: string,
|
|
69
|
-
private readonly _source: string,
|
|
70
|
-
) {
|
|
71
|
-
super();
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
override eq(other: this): boolean {
|
|
75
|
-
return this._source === other._source;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
override toDOM(view: EditorView): HTMLDivElement {
|
|
79
|
-
const div = document.createElement('div');
|
|
80
|
-
setTimeout(async () => {
|
|
81
|
-
// https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/config.type.ts
|
|
82
|
-
_mermaid.initialize({
|
|
83
|
-
darkMode: view.state.facet(EditorView.darkTheme),
|
|
84
|
-
theme: 'neutral',
|
|
85
|
-
// TODO(burdon): Styles.
|
|
86
|
-
// NOTE: Must specify 'base' in order to override.
|
|
87
|
-
// theme: 'base',
|
|
88
|
-
// themeVariables: {
|
|
89
|
-
// primaryColor: getToken('extend.colors.red.100'),
|
|
90
|
-
// primaryBorderColor: getToken('extend.colors.neutral.200'),
|
|
91
|
-
// },
|
|
92
|
-
// https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/diagrams/flowchart/styles.ts
|
|
93
|
-
// https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/diagrams/sequence/styles.js
|
|
94
|
-
// https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/diagrams/state/styles.js
|
|
95
|
-
// themeCSS: '.node rect { fill: red; }',
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
// TODO(burdon): Cache?
|
|
99
|
-
const svg = await this.render(div);
|
|
100
|
-
if (this._error) {
|
|
101
|
-
div.className = 'cm-mermaid-error';
|
|
102
|
-
div.innerText = this._error;
|
|
103
|
-
} else {
|
|
104
|
-
div.className = 'cm-mermaid';
|
|
105
|
-
div.innerHTML = svg!;
|
|
106
|
-
|
|
107
|
-
// const label = document.createElement('span');
|
|
108
|
-
// label.innerText = 'Mermaid';
|
|
109
|
-
// label.className = 'cm-mermaid-label';
|
|
110
|
-
// wrapper.appendChild(label);
|
|
111
|
-
view.requestMeasure();
|
|
112
|
-
}
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
return div;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
async render(container: Element): Promise<string | undefined> {
|
|
119
|
-
try {
|
|
120
|
-
// https://github.com/mermaid-js/mermaid
|
|
121
|
-
const valid = await _mermaid.parse(this._source);
|
|
122
|
-
if (valid) {
|
|
123
|
-
const result = await _mermaid.render(this._id, this._source);
|
|
124
|
-
this._error = undefined;
|
|
125
|
-
this._svg = result.svg;
|
|
126
|
-
return result.svg;
|
|
127
|
-
}
|
|
128
|
-
} catch (err: any) {
|
|
129
|
-
this._error = String(err);
|
|
130
|
-
this._svg = undefined;
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
override ignoreEvent(e: Event): boolean {
|
|
135
|
-
return !/^mouse/.test(e.type);
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
const styles = EditorView.theme({
|
|
140
|
-
'& .cm-mermaid': {
|
|
141
|
-
position: 'relative',
|
|
142
|
-
display: 'inline-flex',
|
|
143
|
-
width: '100%',
|
|
144
|
-
justifyContent: 'center',
|
|
145
|
-
},
|
|
146
|
-
// '& .cm-mermaid-label': {
|
|
147
|
-
// position: 'absolute',
|
|
148
|
-
// right: 0,
|
|
149
|
-
// textSize: 10,
|
|
150
|
-
// },
|
|
151
|
-
'& .cm-mermaid-error': {
|
|
152
|
-
display: 'inline-block',
|
|
153
|
-
color: 'var(--dx-errorText)',
|
|
154
|
-
},
|
|
155
|
-
});
|