@node-projects/web-component-designer-mermaid 0.1.1 → 0.3.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.
@@ -34,7 +34,9 @@ export function rerouteConnectedMermaidEdges(instanceServiceContainer, changedIt
34
34
  if (!changedNodeIds.size)
35
35
  return;
36
36
  const designerCanvas = instanceServiceContainer.designerCanvas;
37
- const rootItems = collectCanvasDesignItems(designerCanvas.rootDesignItem);
37
+ if (!designerCanvas?.isConnected)
38
+ return;
39
+ const rootItems = collectCanvasDesignItems(instanceServiceContainer.rootDesignItem);
38
40
  const nodeItemsById = createNodeItemsById(rootItems);
39
41
  const affectedEdges = rootItems.filter(item => {
40
42
  if (!isEdgeItem(item))
@@ -5,12 +5,16 @@ export declare class MermaidElementsService implements IElementsService {
5
5
  readonly onElementsChanged: TypedEvent<void>;
6
6
  private _allElements;
7
7
  private _instanceServiceContainer;
8
+ private _contentSubscription;
8
9
  private _currentDiagramType;
9
10
  private _resolveStored;
10
11
  private _rejectStored;
11
12
  constructor(_name: string, file: string | URL);
12
13
  get name(): string;
13
- setInstanceServiceContainer(instanceServiceContainer: InstanceServiceContainer): void;
14
+ setInstanceServiceContainer(instanceServiceContainer: InstanceServiceContainer): {
15
+ dispose: () => void;
16
+ };
17
+ dispose(): void;
14
18
  getElements(): Promise<IElementDefinition[]>;
15
19
  private _filterElements;
16
20
  private _refreshCurrentDiagramType;
@@ -5,6 +5,7 @@ export class MermaidElementsService {
5
5
  onElementsChanged = new TypedEvent();
6
6
  _allElements;
7
7
  _instanceServiceContainer;
8
+ _contentSubscription;
8
9
  _currentDiagramType = "flowchart";
9
10
  _resolveStored;
10
11
  _rejectStored;
@@ -20,9 +21,24 @@ export class MermaidElementsService {
20
21
  return this._name;
21
22
  }
22
23
  setInstanceServiceContainer(instanceServiceContainer) {
24
+ this._contentSubscription?.dispose();
23
25
  this._instanceServiceContainer = instanceServiceContainer;
24
26
  this._refreshCurrentDiagramType();
25
- instanceServiceContainer.onContentChanged.on(() => this._refreshCurrentDiagramType());
27
+ const subscription = instanceServiceContainer?.onContentChanged.on(() => this._refreshCurrentDiagramType());
28
+ this._contentSubscription = subscription;
29
+ return { dispose: () => {
30
+ subscription?.dispose();
31
+ if (this._contentSubscription === subscription) {
32
+ this._contentSubscription = null;
33
+ this._instanceServiceContainer = null;
34
+ this._refreshCurrentDiagramType();
35
+ }
36
+ } };
37
+ }
38
+ dispose() {
39
+ this._contentSubscription?.dispose();
40
+ this._contentSubscription = null;
41
+ this._instanceServiceContainer = null;
26
42
  }
27
43
  getElements() {
28
44
  if (this._allElements)
@@ -61,6 +61,7 @@ async function parseFlowchart(html, serviceContainer, instanceServiceContainer)
61
61
  const edges = [];
62
62
  const subgraphs = new Map();
63
63
  const directives = [];
64
+ const edgeDirectives = new Map();
64
65
  const subgraphStack = [];
65
66
  let direction = "TD";
66
67
  const scannedNodes = new Map();
@@ -96,6 +97,11 @@ async function parseFlowchart(html, serviceContainer, instanceServiceContainer)
96
97
  subgraphStack[subgraphStack.length - 1].direction = parsedSubgraphDirection;
97
98
  continue;
98
99
  }
100
+ const edgeDirective = parseFlowchartEdgeDirective(line.trimmed, line.start);
101
+ if (edgeDirective) {
102
+ edgeDirectives.set(edgeDirective.edgeId, edgeDirective);
103
+ continue;
104
+ }
99
105
  const edge = parseEdge(line.trimmed, line.start);
100
106
  if (edge) {
101
107
  scannedEdges.push(edge);
@@ -118,13 +124,17 @@ async function parseFlowchart(html, serviceContainer, instanceServiceContainer)
118
124
  }
119
125
  for (const edge of parsed.edges) {
120
126
  const scanned = getScannedEdge(scannedEdges, edge);
121
- edges.push({ ...edge, ...scanned });
127
+ const edgeDirective = scanned.edgeId ? edgeDirectives.get(scanned.edgeId) : null;
128
+ edges.push({ ...edge, ...scanned, animation: edgeDirective?.animation ?? scanned.animation });
122
129
  }
123
130
  }
124
131
  else {
125
132
  for (const node of scannedNodes.values())
126
133
  mergeNode(nodes, node);
127
- edges.push(...scannedEdges);
134
+ edges.push(...scannedEdges.map(edge => {
135
+ const edgeDirective = edge.edgeId ? edgeDirectives.get(edge.edgeId) : null;
136
+ return { ...edge, animation: edgeDirective?.animation ?? edge.animation };
137
+ }));
128
138
  }
129
139
  const mermaidLayout = await getMermaidLayout(html, nodes);
130
140
  const designItemsById = new Map();
@@ -277,6 +287,9 @@ function writeFlowchartEdgeLine(textWriter, designItem, element, nodeItemsById,
277
287
  const start = textWriter.position;
278
288
  const line = indentEmbeddedNewlines(" " + element.createMermaid());
279
289
  textWriter.writeLine(line);
290
+ const directive = element.createMermaidDirective?.();
291
+ if (directive)
292
+ textWriter.writeLine(" " + directive);
280
293
  if (!updatePositions)
281
294
  return;
282
295
  const from = element.getAttribute("from");
@@ -790,6 +803,22 @@ function parseFlowchartDirective(line, sourceStart) {
790
803
  return null;
791
804
  return null;
792
805
  }
806
+ function parseFlowchartEdgeDirective(line, sourceStart) {
807
+ const cursor = new SourceCursor(line);
808
+ const edgeId = cursor.readIdentifier();
809
+ if (!edgeId)
810
+ return null;
811
+ cursor.skipWhitespace();
812
+ if (!cursor.consume("@{"))
813
+ return null;
814
+ const contentEnd = line.lastIndexOf("}");
815
+ const content = contentEnd >= 0 ? line.substring(cursor.position, contentEnd) : line.substring(cursor.position);
816
+ const properties = parseObjectLiteralProperties(content);
817
+ const animation = properties.get("animation") ?? (properties.get("animate") === "true" ? "fast" : "");
818
+ if (!animation)
819
+ return null;
820
+ return { edgeId, animation, sourceRange: { start: sourceStart, length: line.length } };
821
+ }
793
822
  function parseBinaryStatement(line, operators) {
794
823
  const parsed = findOperator(line, operators);
795
824
  if (!parsed)
@@ -1758,7 +1787,7 @@ function isRequirementDirection(value) {
1758
1787
  return value === "TB" || value === "BT" || value === "RL" || value === "LR";
1759
1788
  }
1760
1789
  function setMermaidDocumentAttributes(instanceServiceContainer, diagramType, direction, title, frontmatter) {
1761
- const rootDesignItem = instanceServiceContainer?.designerCanvas?.rootDesignItem;
1790
+ const rootDesignItem = instanceServiceContainer?.rootDesignItem;
1762
1791
  rootDesignItem?._withoutUndoSetAttribute(mermaidDiagramTypeAttribute, diagramType);
1763
1792
  if (direction)
1764
1793
  rootDesignItem?._withoutUndoSetAttribute(mermaidFlowchartDirectionAttribute, direction);
@@ -1812,7 +1841,7 @@ function getWritableDocumentDiagramType(designItems) {
1812
1841
  return "flowchart";
1813
1842
  }
1814
1843
  function getRootDesignItem(designItems) {
1815
- return designItems[0]?.isRootItem ? designItems[0] : designItems[0]?.instanceServiceContainer?.designerCanvas?.rootDesignItem;
1844
+ return designItems[0]?.isRootItem ? designItems[0] : designItems[0]?.instanceServiceContainer?.rootDesignItem;
1816
1845
  }
1817
1846
  function getMermaidDesignItems(designItems) {
1818
1847
  if (designItems[0]?.isRootItem)
@@ -28,19 +28,30 @@ export function createMermaidDesignerServiceContainer() {
28
28
  serviceContainer.register("propertyService", new BaseCustomWebComponentPropertiesService(true));
29
29
  serviceContainer.register("designItemService", new DesignItemService());
30
30
  serviceContainer.register("deletionService", new DeletionService());
31
- serviceContainer.register("undoService", (designerCanvas) => new UndoService(designerCanvas));
32
- serviceContainer.register("selectionService", (designerCanvas) => new SelectionService(designerCanvas, false));
33
- serviceContainer.register("designItemDocumentPositionService", (designerCanvas) => new DesignItemDocumentPositionService(designerCanvas));
31
+ serviceContainer.registerDocumentService("undoService", container => new UndoService(container));
32
+ serviceContainer.registerDocumentService("selectionService", container => new SelectionService(container, false));
33
+ serviceContainer.registerDocumentService("designItemDocumentPositionService", () => new DesignItemDocumentPositionService());
34
34
  serviceContainer.instanceServiceContainerCreatedCallbacks.push(instanceServiceContainer => {
35
- const designerCanvas = instanceServiceContainer.designerCanvas;
36
- elementsService.setInstanceServiceContainer(instanceServiceContainer);
37
- instanceServiceContainer.selectionService.setSelectedElements([designerCanvas.rootDesignItem]);
38
- const previousRaiseDesignItemsChanged = designerCanvas.raiseDesignItemsChanged.bind(designerCanvas);
39
- designerCanvas.raiseDesignItemsChanged = (designItems, action, operationFinished) => {
40
- previousRaiseDesignItemsChanged(designItems, action, operationFinished);
41
- if (action === "place" || action === "resize")
42
- rerouteConnectedMermaidEdges(instanceServiceContainer, designItems, operationFinished);
35
+ instanceServiceContainer.selectionService.setSelectedElements([instanceServiceContainer.rootDesignItem]);
36
+ let restoreCanvas = () => { };
37
+ const attach = () => {
38
+ restoreCanvas();
39
+ restoreCanvas = () => { };
40
+ const canvas = instanceServiceContainer.designerCanvas;
41
+ if (!canvas)
42
+ return;
43
+ const paletteBinding = elementsService.setInstanceServiceContainer(instanceServiceContainer);
44
+ const previous = canvas.raiseDesignItemsChanged;
45
+ canvas.raiseDesignItemsChanged = (items, action, finished) => {
46
+ previous.call(canvas, items, action, finished);
47
+ if (action === "place" || action === "resize")
48
+ rerouteConnectedMermaidEdges(instanceServiceContainer, items, finished);
49
+ };
50
+ restoreCanvas = () => { canvas.raiseDesignItemsChanged = previous; paletteBinding.dispose(); };
43
51
  };
52
+ attach();
53
+ const subscription = instanceServiceContainer.onDesignerCanvasChanged.on(attach);
54
+ instanceServiceContainer.editingDocument.addDisposable({ dispose() { subscription.dispose(); restoreCanvas(); } });
44
55
  });
45
56
  serviceContainer.designerExtensions.set(ExtensionType.Permanent, []);
46
57
  serviceContainer.designerExtensions.set(ExtensionType.PrimarySelection, [
@@ -41,5 +41,6 @@ export declare class MermaidEdge extends BaseCustomWebComponentConstructorAppend
41
41
  setPreviewWaypoints(waypoints: string | null): void;
42
42
  private _render;
43
43
  createMermaid(): string;
44
+ createMermaidDirective(): string;
44
45
  }
45
46
  export {};
@@ -51,6 +51,12 @@ export class MermaidEdge extends BaseCustomWebComponentConstructorAppend {
51
51
  font-size: 12px;
52
52
  pointer-events: none;
53
53
  }
54
+
55
+ @keyframes mermaid-edge-flow {
56
+ to {
57
+ stroke-dashoffset: -28;
58
+ }
59
+ }
54
60
  `;
55
61
  static template = html `
56
62
  <svg id="svg" viewBox="0 0 10 10" preserveAspectRatio="none">
@@ -139,7 +145,9 @@ export class MermaidEdge extends BaseCustomWebComponentConstructorAppend {
139
145
  const label = this.getAttribute("label") ?? this.label ?? "";
140
146
  this._path.setAttribute("marker-start", connector.startsWith("<") || edgeType === MermaidEdgeType.multi ? "url(#marker-start)" : "");
141
147
  this._path.setAttribute("marker-end", getMarkerEnd(edgeType, connector));
142
- this._path.style.strokeDasharray = edgeType === MermaidEdgeType.dotted ? "4 4" : "";
148
+ const animation = this.getAttribute("animation") ?? this.animation;
149
+ this._path.style.strokeDasharray = animation ? "8 6" : edgeType === MermaidEdgeType.dotted ? "4 4" : "";
150
+ this._path.style.animation = animation ? `mermaid-edge-flow ${getAnimationDuration(animation)} linear infinite` : "";
143
151
  this._path.style.strokeWidth = edgeType === MermaidEdgeType.thick ? "4" : "2.5";
144
152
  this._path.style.opacity = edgeType === MermaidEdgeType.invisible ? "0" : "";
145
153
  this._label.textContent = label;
@@ -161,6 +169,21 @@ export class MermaidEdge extends BaseCustomWebComponentConstructorAppend {
161
169
  return `${from} ${prefix}${connector.label}|${escapeLabel(label)}| ${to}`;
162
170
  return `${from} ${prefix}${connector.plain} ${to}`;
163
171
  }
172
+ createMermaidDirective() {
173
+ const rawEdgeId = this.getAttribute("edge-id") ?? this.edgeId;
174
+ const edgeId = rawEdgeId ? sanitizeId(rawEdgeId) : "";
175
+ const animation = this.getAttribute("animation") ?? this.animation;
176
+ if (!edgeId || !animation)
177
+ return "";
178
+ return `${edgeId}@{ animation: ${animation} }`;
179
+ }
180
+ }
181
+ function getAnimationDuration(animation) {
182
+ if (animation === "fast")
183
+ return "0.8s";
184
+ if (animation === "slow")
185
+ return "2s";
186
+ return "1.3s";
164
187
  }
165
188
  function getMarkerEnd(edgeType, connector) {
166
189
  if (edgeType === MermaidEdgeType.open || edgeType === MermaidEdgeType.invisible || connector.endsWith("---") || connector.endsWith("===") || connector.endsWith("-.-"))
@@ -1,6 +1,6 @@
1
1
  import { BaseCustomWebComponentConstructorAppend } from "@node-projects/base-custom-webcomponent";
2
2
  import { IUiCommand, InstanceServiceContainer, ServiceContainer } from "@node-projects/web-component-designer";
3
- import { IDemoView } from "@node-projects/web-component-designer/src/elements/widgets/demoView/IDemoView.js";
3
+ import { IDemoView } from "@node-projects/web-component-designer";
4
4
  export declare class MermaidDemoView extends BaseCustomWebComponentConstructorAppend implements IDemoView {
5
5
  static readonly template: HTMLTemplateElement;
6
6
  static readonly style: CSSStyleSheet;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "description": "web-component-designer addon: Widgets and Services for creating a Mermaid Designer",
3
3
  "name": "@node-projects/web-component-designer-mermaid",
4
- "version": "0.1.1",
4
+ "version": "0.3.0",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
7
7
  "types": "./dist/index.d.ts",
@@ -19,7 +19,7 @@
19
19
  },
20
20
  "dependencies": {
21
21
  "@node-projects/base-custom-webcomponent": ">=0.27.8",
22
- "@node-projects/web-component-designer": ">=0.2.0",
22
+ "@node-projects/web-component-designer": ">=0.3.0",
23
23
  "mermaid": "^11.12.0"
24
24
  },
25
25
  "repository": {