@node-projects/web-component-designer-mermaid 0.1.0
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/README.md +9 -0
- package/package.json +24 -0
- package/src/index.ts +20 -0
- package/src/monaco/MermaidLanguage.ts +34 -0
- package/src/services/MermaidConnectionRouting.ts +92 -0
- package/src/services/MermaidDocumentPropertiesService.ts +179 -0
- package/src/services/MermaidElementsService.ts +81 -0
- package/src/services/MermaidLayoutCopyPasteService.ts +18 -0
- package/src/services/MermaidLayoutPlacementService.ts +30 -0
- package/src/services/MermaidParserService.ts +2473 -0
- package/src/services/MermaidPropertyGroupsService.ts +18 -0
- package/src/services/mermaidGeometry.ts +131 -0
- package/src/setupMermaidServiceContainer.ts +123 -0
- package/src/toolbar/ConnectMermaidNodesTool.ts +197 -0
- package/src/widgets/elements.json +198 -0
- package/src/widgets/mermaid-edge.ts +215 -0
- package/src/widgets/mermaid-flowchart-directive.ts +58 -0
- package/src/widgets/mermaid-mindmap-node.ts +218 -0
- package/src/widgets/mermaid-node.ts +456 -0
- package/src/widgets/mermaid-requirement-node.ts +202 -0
- package/src/widgets/mermaid-requirement-relationship.ts +139 -0
- package/src/widgets/mermaid-sequence-message.ts +145 -0
- package/src/widgets/mermaid-sequence-participant.ts +80 -0
- package/src/widgets/mermaid-subgraph.ts +80 -0
- package/src/widgets/views/mermaid-demo-view.ts +47 -0
- package/tsconfig.json +20 -0
package/README.md
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"description": "web-component-designer addon: Widgets and Services for creating a Mermaid Designer",
|
|
3
|
+
"name": "@node-projects/web-component-designer-mermaid",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"author": "jochen.kuehner@gmx.de",
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"scripts": {
|
|
10
|
+
"tsc": "tsc",
|
|
11
|
+
"build": "tsc",
|
|
12
|
+
"link": "npm link",
|
|
13
|
+
"prepublishOnly": "npm run build"
|
|
14
|
+
},
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"@node-projects/base-custom-webcomponent": ">=0.27.8",
|
|
17
|
+
"@node-projects/web-component-designer": ">=0.2.0",
|
|
18
|
+
"mermaid": "^11.12.0"
|
|
19
|
+
},
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "git+https://github.com/node-projects/web-component-designer.git"
|
|
23
|
+
}
|
|
24
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export * from "./widgets/mermaid-node.js";
|
|
2
|
+
export * from "./widgets/mermaid-edge.js";
|
|
3
|
+
export * from "./widgets/mermaid-sequence-message.js";
|
|
4
|
+
export * from "./widgets/mermaid-sequence-participant.js";
|
|
5
|
+
export * from "./widgets/mermaid-mindmap-node.js";
|
|
6
|
+
export * from "./widgets/mermaid-requirement-node.js";
|
|
7
|
+
export * from "./widgets/mermaid-requirement-relationship.js";
|
|
8
|
+
export * from "./widgets/mermaid-subgraph.js";
|
|
9
|
+
export * from "./widgets/mermaid-flowchart-directive.js";
|
|
10
|
+
|
|
11
|
+
export * from "./monaco/MermaidLanguage.js";
|
|
12
|
+
|
|
13
|
+
export * from "./services/MermaidLayoutCopyPasteService.js";
|
|
14
|
+
export * from "./services/MermaidLayoutPlacementService.js";
|
|
15
|
+
export * from "./services/MermaidParserService.js";
|
|
16
|
+
export * from "./services/MermaidConnectionRouting.js";
|
|
17
|
+
export * from "./services/MermaidElementsService.js";
|
|
18
|
+
export * from "./toolbar/ConnectMermaidNodesTool.js";
|
|
19
|
+
|
|
20
|
+
export * from "./setupMermaidServiceContainer.js";
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export function addMermaidLanguageToMonaco(monaco?: any) {
|
|
2
|
+
if (!monaco)
|
|
3
|
+
//@ts-ignore
|
|
4
|
+
monaco = window.monaco;
|
|
5
|
+
|
|
6
|
+
monaco.languages.register({ id: "mermaidLanguage" });
|
|
7
|
+
|
|
8
|
+
monaco.languages.setMonarchTokensProvider("mermaidLanguage", {
|
|
9
|
+
tokenizer: {
|
|
10
|
+
root: [
|
|
11
|
+
[/%%.*$/, "comment"],
|
|
12
|
+
[/\b(graph|flowchart|sequenceDiagram|classDiagram|stateDiagram-v2|erDiagram|journey|gantt|pie|mindmap|requirementDiagram|requirement|functionalRequirement|interfaceRequirement|performanceRequirement|physicalRequirement|designConstraint|element)\b/, "keyword"],
|
|
13
|
+
[/[a-zA-Z][\w-]*(?=\s*(\[|\(|--|==|-\.))/, "identifier"],
|
|
14
|
+
[/(-->|---|-.->|-.-|==>|===)/, "operator"],
|
|
15
|
+
[/\|[^|]*\|/, "string"],
|
|
16
|
+
],
|
|
17
|
+
},
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
monaco.editor.defineTheme("mermaidTheme", {
|
|
21
|
+
base: "vs",
|
|
22
|
+
inherit: false,
|
|
23
|
+
rules: [
|
|
24
|
+
{ token: "comment", foreground: "008000" },
|
|
25
|
+
{ token: "keyword", foreground: "0000FF", fontStyle: "bold" },
|
|
26
|
+
{ token: "identifier", foreground: "795E26" },
|
|
27
|
+
{ token: "operator", foreground: "AF00DB" },
|
|
28
|
+
{ token: "string", foreground: "A31515" },
|
|
29
|
+
],
|
|
30
|
+
colors: {
|
|
31
|
+
"editor.foreground": "#000000",
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import type { IDesignItem, InstanceServiceContainer } from "@node-projects/web-component-designer";
|
|
2
|
+
import { encodeWaypoints, FlowchartDirection, routeBetweenBounds } from "./mermaidGeometry.js";
|
|
3
|
+
|
|
4
|
+
function isEdgeItem(designItem: IDesignItem) {
|
|
5
|
+
return designItem.element.localName === "mermaid-edge" || designItem.element.localName === "mermaid-requirement-relationship";
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
function isNodeItem(designItem: IDesignItem) {
|
|
9
|
+
return designItem.element.localName === "mermaid-node" || designItem.element.localName === "mermaid-requirement-node";
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function getNodeId(designItem: IDesignItem) {
|
|
13
|
+
if (designItem.element.localName === "mermaid-requirement-node")
|
|
14
|
+
return designItem.element.getAttribute("label");
|
|
15
|
+
return designItem.element.getAttribute("node-id");
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function collectCanvasDesignItems(rootDesignItem: IDesignItem) {
|
|
19
|
+
const allItems: IDesignItem[] = [];
|
|
20
|
+
const stack = Array.from(rootDesignItem.children());
|
|
21
|
+
|
|
22
|
+
while (stack.length) {
|
|
23
|
+
const item = stack.pop()!;
|
|
24
|
+
allItems.push(item);
|
|
25
|
+
stack.push(...item.children());
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return allItems;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function createNodeItemsById(rootItems: IDesignItem[]) {
|
|
32
|
+
return new Map(
|
|
33
|
+
rootItems
|
|
34
|
+
.filter(isNodeItem)
|
|
35
|
+
.map(item => [getNodeId(item), item] as const)
|
|
36
|
+
.filter((entry): entry is [string, IDesignItem] => !!entry[0])
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function rerouteConnectedMermaidEdges(instanceServiceContainer: InstanceServiceContainer, changedItems: IDesignItem[], operationFinished: boolean) {
|
|
41
|
+
const changedNodeIds = new Set(
|
|
42
|
+
changedItems
|
|
43
|
+
.filter(isNodeItem)
|
|
44
|
+
.map(getNodeId)
|
|
45
|
+
.filter((id): id is string => !!id)
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
if (!changedNodeIds.size)
|
|
49
|
+
return;
|
|
50
|
+
|
|
51
|
+
const designerCanvas = instanceServiceContainer.designerCanvas;
|
|
52
|
+
const rootItems = collectCanvasDesignItems(designerCanvas.rootDesignItem);
|
|
53
|
+
const nodeItemsById = createNodeItemsById(rootItems);
|
|
54
|
+
const affectedEdges = rootItems.filter(item => {
|
|
55
|
+
if (!isEdgeItem(item))
|
|
56
|
+
return false;
|
|
57
|
+
|
|
58
|
+
const from = item.element.getAttribute("from");
|
|
59
|
+
const to = item.element.getAttribute("to");
|
|
60
|
+
return !!from && !!to && (changedNodeIds.has(from) || changedNodeIds.has(to));
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
for (const edgeItem of affectedEdges) {
|
|
64
|
+
const sourceItem = nodeItemsById.get(edgeItem.element.getAttribute("from"));
|
|
65
|
+
const targetItem = nodeItemsById.get(edgeItem.element.getAttribute("to"));
|
|
66
|
+
if (!sourceItem || !targetItem)
|
|
67
|
+
continue;
|
|
68
|
+
|
|
69
|
+
const waypoints = encodeWaypoints(routeBetweenBounds(
|
|
70
|
+
designerCanvas.getNormalizedElementCoordinates(sourceItem.element),
|
|
71
|
+
designerCanvas.getNormalizedElementCoordinates(targetItem.element),
|
|
72
|
+
getDiagramDirection(edgeItem)
|
|
73
|
+
));
|
|
74
|
+
const edgeElement = edgeItem.element as HTMLElement & { setPreviewWaypoints?: (waypoints: string | null) => void };
|
|
75
|
+
|
|
76
|
+
if (operationFinished) {
|
|
77
|
+
edgeElement.setPreviewWaypoints?.(waypoints);
|
|
78
|
+
if (edgeItem.getAttribute("waypoints") !== waypoints)
|
|
79
|
+
edgeItem.setAttribute("waypoints", waypoints);
|
|
80
|
+
queueMicrotask(() => edgeElement.setPreviewWaypoints?.(null));
|
|
81
|
+
} else {
|
|
82
|
+
edgeElement.setPreviewWaypoints?.(waypoints);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function getDiagramDirection(designItem: IDesignItem): FlowchartDirection {
|
|
88
|
+
const direction = designItem.element.getAttribute("diagram-direction");
|
|
89
|
+
if (direction === "TB" || direction === "TD" || direction === "BT" || direction === "RL" || direction === "LR")
|
|
90
|
+
return direction;
|
|
91
|
+
return undefined;
|
|
92
|
+
}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import { AbstractPropertiesService, BindingTarget, IDesignItem, IProperty, PropertyType, RefreshMode, ValueType } from "@node-projects/web-component-designer";
|
|
2
|
+
import { rerouteConnectedMermaidEdges } from "./MermaidConnectionRouting.js";
|
|
3
|
+
import { FlowchartDirection } from "./mermaidGeometry.js";
|
|
4
|
+
|
|
5
|
+
export const mermaidDiagramTypeAttribute = "data-mermaid-diagram-type";
|
|
6
|
+
export const mermaidFlowchartDirectionAttribute = "data-mermaid-flowchart-direction";
|
|
7
|
+
export const mermaidTitleAttribute = "data-mermaid-title";
|
|
8
|
+
export const mermaidFrontmatterAttribute = "data-mermaid-frontmatter";
|
|
9
|
+
|
|
10
|
+
export type MermaidDocumentDiagramType = "flowchart" | "sequenceDiagram" | "mindmap" | "requirementDiagram";
|
|
11
|
+
|
|
12
|
+
export class MermaidDocumentPropertiesService extends AbstractPropertiesService {
|
|
13
|
+
private readonly _diagramTypeProperty: IProperty = {
|
|
14
|
+
name: "diagramType",
|
|
15
|
+
displayName: "diagram type",
|
|
16
|
+
type: "list",
|
|
17
|
+
values: ["flowchart", "sequenceDiagram", "mindmap", "requirementDiagram"],
|
|
18
|
+
attributeName: mermaidDiagramTypeAttribute,
|
|
19
|
+
service: this,
|
|
20
|
+
propertyType: PropertyType.attribute,
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
private readonly _flowchartDirectionProperty: IProperty = {
|
|
24
|
+
name: "flowchartDirection",
|
|
25
|
+
displayName: "flowchart direction",
|
|
26
|
+
type: "list",
|
|
27
|
+
values: ["TD", "TB", "BT", "LR", "RL"],
|
|
28
|
+
attributeName: mermaidFlowchartDirectionAttribute,
|
|
29
|
+
service: this,
|
|
30
|
+
propertyType: PropertyType.attribute,
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
private readonly _requirementDirectionProperty: IProperty = {
|
|
34
|
+
name: "requirementDirection",
|
|
35
|
+
displayName: "direction",
|
|
36
|
+
type: "list",
|
|
37
|
+
values: ["TB", "BT", "LR", "RL"],
|
|
38
|
+
attributeName: mermaidFlowchartDirectionAttribute,
|
|
39
|
+
service: this,
|
|
40
|
+
propertyType: PropertyType.attribute,
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
private readonly _titleProperty: IProperty = {
|
|
44
|
+
name: "title",
|
|
45
|
+
displayName: "title",
|
|
46
|
+
type: "string",
|
|
47
|
+
attributeName: mermaidTitleAttribute,
|
|
48
|
+
service: this,
|
|
49
|
+
propertyType: PropertyType.attribute,
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
override getRefreshMode() {
|
|
53
|
+
return RefreshMode.fullOnValueChange;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
override isHandledElement(designItem: IDesignItem): boolean {
|
|
57
|
+
return designItem.isRootItem;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
override async getProperties(designItem: IDesignItem): Promise<IProperty[]> {
|
|
61
|
+
const diagramType = getMermaidDocumentDiagramType(designItem);
|
|
62
|
+
if (diagramType === "requirementDiagram")
|
|
63
|
+
return [this._diagramTypeProperty, this._requirementDirectionProperty, this._titleProperty];
|
|
64
|
+
if (diagramType === "sequenceDiagram" || diagramType === "mindmap")
|
|
65
|
+
return [this._diagramTypeProperty, this._titleProperty];
|
|
66
|
+
|
|
67
|
+
return [this._diagramTypeProperty, this._flowchartDirectionProperty, this._titleProperty];
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
override async getProperty(designItem: IDesignItem, name: string): Promise<IProperty> {
|
|
71
|
+
if (name === this._diagramTypeProperty.name)
|
|
72
|
+
return this._diagramTypeProperty;
|
|
73
|
+
if (name === this._flowchartDirectionProperty.name)
|
|
74
|
+
return this._flowchartDirectionProperty;
|
|
75
|
+
if (name === this._requirementDirectionProperty.name)
|
|
76
|
+
return this._requirementDirectionProperty;
|
|
77
|
+
if (name === this._titleProperty.name)
|
|
78
|
+
return this._titleProperty;
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
override async setValue(designItems: IDesignItem[], property: IProperty, value: any): Promise<void> {
|
|
83
|
+
const rootDesignItems = designItems.filter(designItem => this.isHandledElement(designItem));
|
|
84
|
+
if (!rootDesignItems.length)
|
|
85
|
+
return;
|
|
86
|
+
|
|
87
|
+
const changeGroup = rootDesignItems[0].openGroup("mermaid document property changed: " + property.name + " to " + value);
|
|
88
|
+
try {
|
|
89
|
+
for (const designItem of rootDesignItems)
|
|
90
|
+
setAttribute(designItem, property.attributeName, value);
|
|
91
|
+
|
|
92
|
+
if (property.name === this._diagramTypeProperty.name && (value === "flowchart" || value === "requirementDiagram")) {
|
|
93
|
+
for (const designItem of rootDesignItems) {
|
|
94
|
+
if (!designItem.hasAttribute(mermaidFlowchartDirectionAttribute))
|
|
95
|
+
setAttribute(designItem, mermaidFlowchartDirectionAttribute, value === "requirementDiagram" ? "TB" : "TD");
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (property.name === this._flowchartDirectionProperty.name && isFlowchartDirection(value)) {
|
|
100
|
+
for (const designItem of rootDesignItems)
|
|
101
|
+
updateFlowchartDirection(designItem, value);
|
|
102
|
+
}
|
|
103
|
+
changeGroup.commit();
|
|
104
|
+
} catch (error) {
|
|
105
|
+
changeGroup.abort();
|
|
106
|
+
throw error;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
override getPropertyTarget(): BindingTarget {
|
|
111
|
+
return BindingTarget.attribute;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
override getValue(designItems: IDesignItem[], property: IProperty): any {
|
|
115
|
+
const designItem = designItems?.[0];
|
|
116
|
+
if (!designItem)
|
|
117
|
+
return null;
|
|
118
|
+
|
|
119
|
+
if (property.name === this._diagramTypeProperty.name)
|
|
120
|
+
return getMermaidDocumentDiagramType(designItem);
|
|
121
|
+
|
|
122
|
+
if (property.name === this._flowchartDirectionProperty.name)
|
|
123
|
+
return getMermaidDocumentFlowchartDirection(designItem);
|
|
124
|
+
if (property.name === this._requirementDirectionProperty.name)
|
|
125
|
+
return getMermaidDocumentFlowchartDirection(designItem);
|
|
126
|
+
|
|
127
|
+
if (property.name === this._titleProperty.name)
|
|
128
|
+
return getMermaidDocumentTitle(designItem);
|
|
129
|
+
|
|
130
|
+
return super.getValue(designItems, property);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
override isSet(designItems: IDesignItem[], property: IProperty): ValueType {
|
|
134
|
+
return this.getValue(designItems, property) != null ? ValueType.all : ValueType.none;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export function getMermaidDocumentDiagramType(rootDesignItem: IDesignItem): MermaidDocumentDiagramType {
|
|
139
|
+
const value = rootDesignItem?.getAttribute(mermaidDiagramTypeAttribute);
|
|
140
|
+
if (value === "sequenceDiagram" || value === "mindmap" || value === "requirementDiagram")
|
|
141
|
+
return value;
|
|
142
|
+
return "flowchart";
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export function getMermaidDocumentFlowchartDirection(rootDesignItem: IDesignItem) {
|
|
146
|
+
return rootDesignItem?.getAttribute(mermaidFlowchartDirectionAttribute) ?? "TD";
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export function getMermaidDocumentTitle(rootDesignItem: IDesignItem) {
|
|
150
|
+
return rootDesignItem?.getAttribute(mermaidTitleAttribute) ?? "";
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export function getMermaidDocumentFrontmatter(rootDesignItem: IDesignItem) {
|
|
154
|
+
return rootDesignItem?.getAttribute(mermaidFrontmatterAttribute) ?? "";
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function setAttribute(designItem: IDesignItem, attributeName: string, value: any) {
|
|
158
|
+
if (value == null || value === "")
|
|
159
|
+
designItem.removeAttribute(attributeName);
|
|
160
|
+
else
|
|
161
|
+
designItem.setAttribute(attributeName, value.toString());
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function updateFlowchartDirection(rootDesignItem: IDesignItem, direction: FlowchartDirection) {
|
|
165
|
+
const flowchartNodes: IDesignItem[] = [];
|
|
166
|
+
for (const child of rootDesignItem.children(true)) {
|
|
167
|
+
if (child.element.localName === "mermaid-node") {
|
|
168
|
+
child.setAttribute("diagram-direction", direction);
|
|
169
|
+
flowchartNodes.push(child);
|
|
170
|
+
} else if (child.element.localName === "mermaid-edge") {
|
|
171
|
+
child.setAttribute("diagram-direction", direction);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
rerouteConnectedMermaidEdges(rootDesignItem.instanceServiceContainer, flowchartNodes, true);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function isFlowchartDirection(value: string): value is FlowchartDirection {
|
|
178
|
+
return value === "TB" || value === "TD" || value === "BT" || value === "RL" || value === "LR";
|
|
179
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { IElementDefinition, IElementsService, InstanceServiceContainer } from "@node-projects/web-component-designer";
|
|
2
|
+
import { LazyLoader, TypedEvent } from "@node-projects/base-custom-webcomponent";
|
|
3
|
+
import { getMermaidDocumentDiagramType, MermaidDocumentDiagramType } from "./MermaidDocumentPropertiesService.js";
|
|
4
|
+
|
|
5
|
+
type MermaidElementDefinition = IElementDefinition & {
|
|
6
|
+
diagramTypes?: MermaidDocumentDiagramType[];
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
type MermaidElementsJson = {
|
|
10
|
+
elements: Array<string | MermaidElementDefinition>;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export class MermaidElementsService implements IElementsService {
|
|
14
|
+
readonly onElementsChanged = new TypedEvent<void>();
|
|
15
|
+
|
|
16
|
+
private _allElements: MermaidElementDefinition[];
|
|
17
|
+
private _instanceServiceContainer: InstanceServiceContainer;
|
|
18
|
+
private _currentDiagramType: MermaidDocumentDiagramType = "flowchart";
|
|
19
|
+
private _resolveStored: ((value: IElementDefinition[]) => void)[];
|
|
20
|
+
private _rejectStored: ((errorCode: number) => void)[];
|
|
21
|
+
|
|
22
|
+
constructor(private readonly _name: string, file: string | URL) {
|
|
23
|
+
LazyLoader.LoadText(file.toString()).then(data => {
|
|
24
|
+
const parsed = JSON.parse(data) as MermaidElementsJson;
|
|
25
|
+
this._allElements = parsed.elements.map(element => typeof element === "string" ? { tag: element } : element);
|
|
26
|
+
this._resolveWaiting();
|
|
27
|
+
}).catch(error => this._rejectWaiting(error));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
get name() {
|
|
31
|
+
return this._name;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
setInstanceServiceContainer(instanceServiceContainer: InstanceServiceContainer) {
|
|
35
|
+
this._instanceServiceContainer = instanceServiceContainer;
|
|
36
|
+
this._refreshCurrentDiagramType();
|
|
37
|
+
instanceServiceContainer.onContentChanged.on(() => this._refreshCurrentDiagramType());
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
getElements(): Promise<IElementDefinition[]> {
|
|
41
|
+
if (this._allElements)
|
|
42
|
+
return Promise.resolve(this._filterElements());
|
|
43
|
+
if (!this._resolveStored) {
|
|
44
|
+
this._resolveStored = [];
|
|
45
|
+
this._rejectStored = [];
|
|
46
|
+
}
|
|
47
|
+
return new Promise((resolve, reject) => {
|
|
48
|
+
this._resolveStored.push(resolve);
|
|
49
|
+
this._rejectStored.push(reject);
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
private _filterElements() {
|
|
54
|
+
return this._allElements.filter(element => !element.diagramTypes || element.diagramTypes.includes(this._currentDiagramType));
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
private _refreshCurrentDiagramType() {
|
|
58
|
+
const nextDiagramType = getMermaidDocumentDiagramType(this._instanceServiceContainer?.rootDesignItem);
|
|
59
|
+
if (nextDiagramType === this._currentDiagramType)
|
|
60
|
+
return;
|
|
61
|
+
this._currentDiagramType = nextDiagramType;
|
|
62
|
+
this.onElementsChanged.emit();
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
private _resolveWaiting() {
|
|
66
|
+
if (!this._resolveStored)
|
|
67
|
+
return;
|
|
68
|
+
const elements = this._filterElements();
|
|
69
|
+
this._resolveStored.forEach(resolve => resolve(elements));
|
|
70
|
+
this._resolveStored = null;
|
|
71
|
+
this._rejectStored = null;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
private _rejectWaiting(error: number) {
|
|
75
|
+
if (!this._rejectStored)
|
|
76
|
+
return;
|
|
77
|
+
this._rejectStored.forEach(reject => reject(error));
|
|
78
|
+
this._resolveStored = null;
|
|
79
|
+
this._rejectStored = null;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { copyTextToClipboard, DefaultHtmlParserService, getTextFromClipboard, ICopyPasteService, IDesignItem, IRect, InstanceServiceContainer, ServiceContainer } from "@node-projects/web-component-designer";
|
|
2
|
+
|
|
3
|
+
export class MermaidLayoutCopyPasteService implements ICopyPasteService {
|
|
4
|
+
async copyItems(designItems: IDesignItem[]): Promise<void> {
|
|
5
|
+
let savedata = "";
|
|
6
|
+
for (const designItem of designItems) {
|
|
7
|
+
savedata += designItem.element.outerHTML;
|
|
8
|
+
}
|
|
9
|
+
await copyTextToClipboard(savedata);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
async getPasteItems(serviceContainer: ServiceContainer, instanceServiceContainer: InstanceServiceContainer): Promise<[designItems: IDesignItem[], positions?: IRect[]]> {
|
|
13
|
+
const text = await getTextFromClipboard();
|
|
14
|
+
const htmlParser = new DefaultHtmlParserService();
|
|
15
|
+
const result = await htmlParser.parse(text, serviceContainer, instanceServiceContainer, true);
|
|
16
|
+
return [result, null];
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { DefaultPlacementService, IDesignItem, IDesignerCanvas, IPoint, filterChildPlaceItems } from "@node-projects/web-component-designer";
|
|
2
|
+
|
|
3
|
+
export class MermaidLayoutPlacementService extends DefaultPlacementService {
|
|
4
|
+
override serviceForContainer(container: IDesignItem) {
|
|
5
|
+
return container.element.localName === "mermaid-mindmap-node" || container.element.localName === "mermaid-subgraph";
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
override canEnter(container: IDesignItem, items: IDesignItem[]) {
|
|
9
|
+
if (container.element.localName === "mermaid-mindmap-node")
|
|
10
|
+
return items.every(item => item.element.localName === "mermaid-mindmap-node" && !item.element.contains(container.element) && item !== container);
|
|
11
|
+
if (container.element.localName === "mermaid-subgraph")
|
|
12
|
+
return items.every(item => (item.element.localName === "mermaid-node" || item.element.localName === "mermaid-subgraph") && !item.element.contains(container.element) && item !== container);
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
override enterContainer(container: IDesignItem, items: IDesignItem[]) {
|
|
17
|
+
const filteredItems = filterChildPlaceItems(items);
|
|
18
|
+
for (const item of filteredItems) {
|
|
19
|
+
if (item.element.localName === "mermaid-mindmap-node" || item.element.localName === "mermaid-node" || item.element.localName === "mermaid-subgraph")
|
|
20
|
+
container.insertChild(item);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
override leaveContainer(container: IDesignItem, items: IDesignItem[]) {
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
override finishPlace(event: MouseEvent, designerCanvas: IDesignerCanvas, container: IDesignItem, startPoint: IPoint, offsetInControl: IPoint, newPoint: IPoint, items: IDesignItem[]) {
|
|
28
|
+
super.finishPlace(event, designerCanvas, container, startPoint, offsetInControl, newPoint, items);
|
|
29
|
+
}
|
|
30
|
+
}
|