@stocksharp/diagram 1.3.0 → 1.5.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 +84 -1
- package/dist/esm/canvas-renderer.js +178 -31
- package/dist/esm/canvas-renderer.js.map +1 -1
- package/dist/esm/color.js +110 -0
- package/dist/esm/color.js.map +1 -1
- package/dist/esm/core/document.js +11 -0
- package/dist/esm/core/document.js.map +1 -1
- package/dist/esm/core/model.js.map +1 -1
- package/dist/esm/diagram/stocksharp-diagram.js +23 -3
- package/dist/esm/diagram/stocksharp-diagram.js.map +1 -1
- package/dist/esm/embed.js +104 -20
- package/dist/esm/embed.js.map +1 -1
- package/dist/esm/index.js +1 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/ssdiagram-headless.js +247 -26
- package/dist/ssdiagram-headless.js.map +2 -2
- package/dist/ssdiagram.js +352 -48
- package/dist/ssdiagram.js.map +3 -3
- package/dist/types/canvas-renderer.d.ts +26 -3
- package/dist/types/canvas-renderer.d.ts.map +1 -1
- package/dist/types/color.d.ts +9 -0
- package/dist/types/color.d.ts.map +1 -1
- package/dist/types/core/model.d.ts +7 -0
- package/dist/types/core/model.d.ts.map +1 -1
- package/dist/types/diagram/api.d.ts +6 -0
- package/dist/types/diagram/api.d.ts.map +1 -1
- package/dist/types/diagram/stocksharp-diagram.d.ts +11 -0
- package/dist/types/diagram/stocksharp-diagram.d.ts.map +1 -1
- package/dist/types/embed.d.ts +21 -0
- package/dist/types/embed.d.ts.map +1 -1
- package/dist/types/index.d.ts +2 -2
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/canvas-renderer.ts +182 -30
- package/src/color.ts +130 -0
- package/src/core/document.ts +12 -0
- package/src/core/model.ts +7 -0
- package/src/diagram/api.ts +6 -0
- package/src/diagram/stocksharp-diagram.ts +27 -3
- package/src/embed.ts +147 -29
- package/src/index.ts +2 -0
package/src/core/document.ts
CHANGED
|
@@ -132,10 +132,12 @@ export function parseDiagramDocument(source: string | unknown): DiagramDocument
|
|
|
132
132
|
function normalizeNode(value: unknown, path: string): DiagramDocumentNode {
|
|
133
133
|
const input = requireStructure(value, path);
|
|
134
134
|
const id = requireIdentifier(input.id, `${path}.id`);
|
|
135
|
+
const subtitle = requireOptionalText(input.subtitle, `${path}.subtitle`);
|
|
135
136
|
return {
|
|
136
137
|
id,
|
|
137
138
|
typeId: requireIdentifier(input.typeId ?? id, `${path}.typeId`),
|
|
138
139
|
name: requireString(input.name, `${path}.name`),
|
|
140
|
+
...(subtitle === '' ? {} : { subtitle }),
|
|
139
141
|
description: requireString(input.description ?? '', `${path}.description`),
|
|
140
142
|
groupName: requireString(input.groupName ?? 'Common', `${path}.groupName`),
|
|
141
143
|
x: requireFiniteNumber(input.x ?? 0, `${path}.x`),
|
|
@@ -193,15 +195,23 @@ function normalizeParameter(value: unknown, path: string): DiagramParameterSchem
|
|
|
193
195
|
|
|
194
196
|
function normalizeLink(value: unknown, path: string): DiagramDocumentLink {
|
|
195
197
|
const input = requireStructure(value, path);
|
|
198
|
+
const label = requireOptionalText(input.label, `${path}.label`);
|
|
196
199
|
return {
|
|
197
200
|
id: requireIdentifier(input.id, `${path}.id`),
|
|
198
201
|
from: normalizeEndpoint(input.from, `${path}.from`),
|
|
199
202
|
to: normalizeEndpoint(input.to, `${path}.to`),
|
|
200
203
|
style: requireLinkStyle(input.style, `${path}.style`),
|
|
204
|
+
...(label === '' ? {} : { label }),
|
|
201
205
|
metadata: cloneJsonObject(input.metadata ?? {}, `${path}.metadata`),
|
|
202
206
|
};
|
|
203
207
|
}
|
|
204
208
|
|
|
209
|
+
// Text a document may leave out. Absent, null and empty all mean "none", and none is written as
|
|
210
|
+
// nothing at all, so a document that never used the field serializes exactly as it did before.
|
|
211
|
+
function requireOptionalText(value: unknown, path: string): string {
|
|
212
|
+
return value === undefined || value === null ? '' : requireString(value, path);
|
|
213
|
+
}
|
|
214
|
+
|
|
205
215
|
// A style the caller misspelled is a drawing that quietly loses its meaning, so it is refused
|
|
206
216
|
// rather than defaulted - unlike an absent one, which simply means a plain line.
|
|
207
217
|
function requireLinkStyle(value: unknown, path: string): DiagramLinkStyle {
|
|
@@ -251,6 +261,7 @@ function parseNode(value: unknown, path: string): DiagramDocumentNode {
|
|
|
251
261
|
id: requireString(node.id, `${path}.id`),
|
|
252
262
|
typeId: requireString(node.typeId, `${path}.typeId`),
|
|
253
263
|
name: requireString(node.name, `${path}.name`),
|
|
264
|
+
subtitle: node.subtitle,
|
|
254
265
|
description: requireString(node.description, `${path}.description`),
|
|
255
266
|
groupName: requireString(node.groupName, `${path}.groupName`),
|
|
256
267
|
x: requireNumber(node.x, `${path}.x`),
|
|
@@ -309,6 +320,7 @@ function parseLink(value: unknown, path: string): DiagramDocumentLink {
|
|
|
309
320
|
from: parseEndpoint(link.from, `${path}.from`),
|
|
310
321
|
to: parseEndpoint(link.to, `${path}.to`),
|
|
311
322
|
style: link.style === undefined ? undefined : requireLinkStyle(link.style, `${path}.style`),
|
|
323
|
+
label: link.label,
|
|
312
324
|
metadata: cloneJsonObject(link.metadata, `${path}.metadata`),
|
|
313
325
|
}, path);
|
|
314
326
|
}
|
package/src/core/model.ts
CHANGED
|
@@ -50,6 +50,11 @@ export interface DiagramDocumentNode {
|
|
|
50
50
|
id: string;
|
|
51
51
|
typeId: string;
|
|
52
52
|
name: string;
|
|
53
|
+
/**
|
|
54
|
+
* Second, smaller line drawn under the name - what the element does, where the name says what it
|
|
55
|
+
* is. Absent rather than empty when there is none, so a document written without it keeps its shape.
|
|
56
|
+
*/
|
|
57
|
+
subtitle?: string;
|
|
53
58
|
description: string;
|
|
54
59
|
groupName: string;
|
|
55
60
|
x: number;
|
|
@@ -91,6 +96,8 @@ export interface DiagramDocumentLink {
|
|
|
91
96
|
from: DiagramDocumentEndpoint;
|
|
92
97
|
to: DiagramDocumentEndpoint;
|
|
93
98
|
style: DiagramLinkStyle;
|
|
99
|
+
/** Short text drawn at the middle of the link, such as a protocol name. Absent when there is none. */
|
|
100
|
+
label?: string;
|
|
94
101
|
metadata: JsonObject;
|
|
95
102
|
}
|
|
96
103
|
|
package/src/diagram/api.ts
CHANGED
|
@@ -41,6 +41,12 @@ export interface DiagramOptions {
|
|
|
41
41
|
gridSnap?: boolean;
|
|
42
42
|
/** Positive world-space grid step. Defaults to 28. */
|
|
43
43
|
gridSize?: number;
|
|
44
|
+
/**
|
|
45
|
+
* Leave page scrolling to the page: a plain wheel scrolls it and only Ctrl/Meta+wheel zooms, and a
|
|
46
|
+
* one-finger vertical swipe scrolls it on touch screens. Defaults to false, where the diagram keeps
|
|
47
|
+
* every wheel and touch gesture for itself - right for an editor, wrong for a picture on a long page.
|
|
48
|
+
*/
|
|
49
|
+
pageScroll?: boolean;
|
|
44
50
|
}
|
|
45
51
|
|
|
46
52
|
export interface DiagramGridSettings {
|
|
@@ -111,6 +111,7 @@ export class StockSharpDiagram extends EventEmitter<DiagramEvents> {
|
|
|
111
111
|
private fullscreen = false;
|
|
112
112
|
private fullscreenButtonVisible = true;
|
|
113
113
|
private downloadButtonVisible = false;
|
|
114
|
+
private exportEnabled = true;
|
|
114
115
|
private downloadLabel: string | null = null;
|
|
115
116
|
// null means the host never named the button, so its wording comes from the shared bundle.
|
|
116
117
|
// Keeping the two apart is what lets an explicit label win while an untouched control still
|
|
@@ -131,6 +132,7 @@ export class StockSharpDiagram extends EventEmitter<DiagramEvents> {
|
|
|
131
132
|
typeColors: this.portTypeColors(),
|
|
132
133
|
gridSnap: options.gridSnap ?? true,
|
|
133
134
|
gridSize: options.gridSize,
|
|
135
|
+
pageScroll: options.pageScroll ?? false,
|
|
134
136
|
});
|
|
135
137
|
this.fullscreenButtonVisible = options.showFullscreenButton ?? true;
|
|
136
138
|
this.fullscreenLabels = options.fullscreenLabels ?? null;
|
|
@@ -228,6 +230,15 @@ export class StockSharpDiagram extends EventEmitter<DiagramEvents> {
|
|
|
228
230
|
return this.canvas.getGridSnap();
|
|
229
231
|
}
|
|
230
232
|
|
|
233
|
+
/** See DiagramOptions.pageScroll. */
|
|
234
|
+
setPageScroll(enabled: boolean): void {
|
|
235
|
+
this.canvas.setPageScroll(enabled);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
isPageScrollEnabled(): boolean {
|
|
239
|
+
return this.canvas.isPageScrollEnabled();
|
|
240
|
+
}
|
|
241
|
+
|
|
231
242
|
nudgeSelection(dx: number, dy: number): boolean {
|
|
232
243
|
return this.canvas.nudgeSelection(dx, dy);
|
|
233
244
|
}
|
|
@@ -466,6 +477,19 @@ export class StockSharpDiagram extends EventEmitter<DiagramEvents> {
|
|
|
466
477
|
this.downloadButton.style.display = 'inline-flex';
|
|
467
478
|
}
|
|
468
479
|
|
|
480
|
+
/**
|
|
481
|
+
* Turns the export commands of the context menu on or off. Off, the menu offers them greyed out and
|
|
482
|
+
* `executeContextCommand` refuses them, for a page that does not want its diagram saved. The
|
|
483
|
+
* download button is separate: hide it with `setDownloadButtonVisible(false)`.
|
|
484
|
+
*/
|
|
485
|
+
setExportEnabled(enabled: boolean): void {
|
|
486
|
+
this.exportEnabled = enabled;
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
isExportEnabled(): boolean {
|
|
490
|
+
return this.exportEnabled;
|
|
491
|
+
}
|
|
492
|
+
|
|
469
493
|
isDownloadButtonVisible(): boolean {
|
|
470
494
|
return this.downloadButtonVisible;
|
|
471
495
|
}
|
|
@@ -1005,15 +1029,15 @@ export class StockSharpDiagram extends EventEmitter<DiagramEvents> {
|
|
|
1005
1029
|
// picture, and an empty diagram has none to give. They stay on in read-only mode,
|
|
1006
1030
|
// which forbids changing the diagram, not looking at it.
|
|
1007
1031
|
exportDocument: {
|
|
1008
|
-
canExecute: ({ document }) => document.nodes.length > 0,
|
|
1032
|
+
canExecute: ({ document }) => this.exportEnabled && document.nodes.length > 0,
|
|
1009
1033
|
execute: () => this.emit('exportRequested', { format: 'document' }),
|
|
1010
1034
|
},
|
|
1011
1035
|
exportPng: {
|
|
1012
|
-
canExecute: ({ document }) => document.nodes.length > 0,
|
|
1036
|
+
canExecute: ({ document }) => this.exportEnabled && document.nodes.length > 0,
|
|
1013
1037
|
execute: () => this.emit('exportRequested', { format: 'png' }),
|
|
1014
1038
|
},
|
|
1015
1039
|
exportSvg: {
|
|
1016
|
-
canExecute: ({ document }) => document.nodes.length > 0,
|
|
1040
|
+
canExecute: ({ document }) => this.exportEnabled && document.nodes.length > 0,
|
|
1017
1041
|
execute: () => this.emit('exportRequested', { format: 'svg' }),
|
|
1018
1042
|
},
|
|
1019
1043
|
// Always available: it shows the overview as readily as it hides it, and an empty
|
package/src/embed.ts
CHANGED
|
@@ -3,18 +3,19 @@ import { luminance } from './color.js';
|
|
|
3
3
|
// Shared read-only diagram embed layer. Rendering, palette loading and theming
|
|
4
4
|
// live next to the diagram engine so web applications do not copy that logic.
|
|
5
5
|
//
|
|
6
|
-
// renderScheme draws a scheme supplied by the caller
|
|
7
|
-
//
|
|
8
|
-
// load
|
|
9
|
-
// inline note instead of throwing.
|
|
6
|
+
// renderScheme draws a strategy scheme supplied by the caller, renderDocument a
|
|
7
|
+
// generic diagram document. renderAll, renderFromSource and renderFromInline
|
|
8
|
+
// discover .ss-diagram-host elements and load either from a URL or embedded
|
|
9
|
+
// JSON. Every failure degrades to a short inline note instead of throwing.
|
|
10
10
|
// StockSharpDiagram uses the canvas renderer directly, so this module is
|
|
11
11
|
// self-contained and uses the same public component as the editor.
|
|
12
12
|
import { StockSharpDiagram } from './diagram/stocksharp-diagram.js';
|
|
13
13
|
import { StockSharpCatalog } from './diagram/catalog.js';
|
|
14
14
|
import { DiagramNode, Link, Node, Port, PortType } from './diagram/types.js';
|
|
15
15
|
import type { ExportRequestedPayload, FullscreenRequestedPayload } from './diagram/api.js';
|
|
16
|
-
import {
|
|
17
|
-
import
|
|
16
|
+
import { DiagramDocumentError, createDiagramDocument } from './core/document.js';
|
|
17
|
+
import { DIAGRAM_DOCUMENT_VERSION, toPortDynamicMode } from './core/model.js';
|
|
18
|
+
import type { DiagramDocument, DiagramDocumentInput, PortDynamicMode } from './core/model.js';
|
|
18
19
|
|
|
19
20
|
interface PalettePort {
|
|
20
21
|
key: string;
|
|
@@ -68,6 +69,18 @@ export interface DiagramEmbedOptions {
|
|
|
68
69
|
handle: DiagramEmbedHandle,
|
|
69
70
|
) => void;
|
|
70
71
|
onDestroyed?: (handle: DiagramEmbedHandle) => void;
|
|
72
|
+
/**
|
|
73
|
+
* Leave page scrolling to the page: a plain wheel scrolls it, Ctrl/Meta+wheel zooms the diagram, and
|
|
74
|
+
* a one-finger vertical swipe scrolls it on touch screens. Defaults to true for a document and to
|
|
75
|
+
* false for a strategy scheme.
|
|
76
|
+
*/
|
|
77
|
+
pageScroll?: boolean;
|
|
78
|
+
/**
|
|
79
|
+
* False turns saving off for this diagram: no download button and no export entries in the menu,
|
|
80
|
+
* even when `onExportRequested` is passed. A host element says the same with
|
|
81
|
+
* `data-diagram-export="off"`. Defaults to true.
|
|
82
|
+
*/
|
|
83
|
+
allowExport?: boolean;
|
|
71
84
|
}
|
|
72
85
|
|
|
73
86
|
const PALETTE_URL = '/data/designer-palette.json';
|
|
@@ -329,22 +342,94 @@ async function renderSchemeAtRevision(
|
|
|
329
342
|
return null;
|
|
330
343
|
const catalog = buildCatalog(palette);
|
|
331
344
|
|
|
345
|
+
return mountDiagram(div, catalog, options, false, (diagram) => {
|
|
346
|
+
const missingTypeTemplate = div.dataset.diagramMissingElement
|
|
347
|
+
?? t('embedMissingElement', 'Element type "{typeId}" is missing from the palette.');
|
|
348
|
+
const converted = toDiagramNodes(
|
|
349
|
+
scheme,
|
|
350
|
+
catalog,
|
|
351
|
+
(typeId) => missingTypeTemplate.replace('{typeId}', typeId || '(empty)'),
|
|
352
|
+
);
|
|
353
|
+
diagram.load(
|
|
354
|
+
converted.nodes,
|
|
355
|
+
scheme.links.map((link) => new Link({
|
|
356
|
+
outNode: link.from,
|
|
357
|
+
outPort: link.fromPort,
|
|
358
|
+
inNode: link.to,
|
|
359
|
+
inPort: link.toPort,
|
|
360
|
+
})),
|
|
361
|
+
{ nodeErrors: converted.nodeErrors },
|
|
362
|
+
);
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Draws a generic diagram document - an architecture or data-flow picture rather than a strategy - as a
|
|
368
|
+
* read-only viewer. The document names its own nodes, colours and zones, so no palette is fetched. It may
|
|
369
|
+
* be a saved document or one written by hand, leaving out whatever has a default (the version included).
|
|
370
|
+
* Page scrolling stays with the page unless `pageScroll: false` is passed. Throws DiagramDocumentError
|
|
371
|
+
* when the source is not a document; the discovery helpers degrade to an inline note instead.
|
|
372
|
+
*/
|
|
373
|
+
export function renderDocument(
|
|
374
|
+
div: HTMLElement,
|
|
375
|
+
source: DiagramDocument | DiagramDocumentInput | string,
|
|
376
|
+
options: DiagramEmbedOptions = {},
|
|
377
|
+
): DiagramEmbedHandle {
|
|
378
|
+
const parsed = readDocument(source);
|
|
379
|
+
beginRender(div);
|
|
380
|
+
return mountDocument(div, parsed, options);
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
// A document written into a page by hand leaves out what has a default, which the strict reader for
|
|
384
|
+
// saved files refuses; the builder fills those in and checks everything else the same way.
|
|
385
|
+
function readDocument(source: unknown): DiagramDocument {
|
|
386
|
+
let value = source;
|
|
387
|
+
if (typeof source === 'string') {
|
|
388
|
+
try {
|
|
389
|
+
value = JSON.parse(source) as unknown;
|
|
390
|
+
} catch (error) {
|
|
391
|
+
throw new DiagramDocumentError(error instanceof Error ? error.message : 'invalid JSON');
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
if (!isRecord(value))
|
|
395
|
+
throw new DiagramDocumentError('expected an object');
|
|
396
|
+
if (value.version !== undefined && value.version !== DIAGRAM_DOCUMENT_VERSION)
|
|
397
|
+
throw new DiagramDocumentError(`unsupported document version ${String(value.version)}`, '$.version');
|
|
398
|
+
// The builder validates every element it is handed, so the cast only names the shape it checks.
|
|
399
|
+
return createDiagramDocument(value as DiagramDocumentInput);
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
function mountDocument(div: HTMLElement, source: DiagramDocument, options: DiagramEmbedOptions): DiagramEmbedHandle {
|
|
403
|
+
return mountDiagram(div, new StockSharpCatalog(), options, true, (diagram) => diagram.loadDocument(source));
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
// Everything a read-only embed shares, whatever it draws: the control, its theme, its size, its
|
|
407
|
+
// buttons and its disposal. `populate` puts the content in.
|
|
408
|
+
function mountDiagram(
|
|
409
|
+
div: HTMLElement,
|
|
410
|
+
catalog: StockSharpCatalog,
|
|
411
|
+
options: DiagramEmbedOptions,
|
|
412
|
+
pageScrollByDefault: boolean,
|
|
413
|
+
populate: (diagram: StockSharpDiagram) => void,
|
|
414
|
+
): DiagramEmbedHandle {
|
|
332
415
|
disposeActiveRender(div);
|
|
333
416
|
div.classList.remove('ss-diagram-error');
|
|
334
417
|
div.replaceChildren();
|
|
335
418
|
// The host page passes its wording the same way it passes the error texts: through the dataset,
|
|
336
419
|
// "enter|exit". Left out, the button keeps its English defaults.
|
|
337
420
|
const [enterLabel, exitLabel] = (div.dataset.diagramFullscreen ?? '').split('|');
|
|
338
|
-
// The download button appears only where a host listens for the request
|
|
339
|
-
// way as the rest, and its absence leaves the button hidden.
|
|
421
|
+
// The download button appears only where a host listens for the request and has not turned export
|
|
422
|
+
// off; its wording comes the same way as the rest, and its absence leaves the button hidden.
|
|
340
423
|
const downloadLabel = div.dataset.diagramDownload;
|
|
341
|
-
const
|
|
424
|
+
const exportAllowed = options.allowExport !== false && div.dataset.diagramExport !== 'off';
|
|
425
|
+
const showDownloadButton = exportAllowed && options.onExportRequested !== undefined;
|
|
342
426
|
|
|
343
427
|
let diagram: StockSharpDiagram;
|
|
344
428
|
try {
|
|
345
429
|
diagram = new StockSharpDiagram({
|
|
346
430
|
div,
|
|
347
431
|
catalog,
|
|
432
|
+
pageScroll: options.pageScroll ?? pageScrollByDefault,
|
|
348
433
|
...(enterLabel && exitLabel ? { fullscreenLabels: { enter: enterLabel, exit: exitLabel } } : {}),
|
|
349
434
|
...(showDownloadButton ? { showDownloadButton: true } : {}),
|
|
350
435
|
...(downloadLabel ? { downloadLabel } : {}),
|
|
@@ -389,7 +474,9 @@ async function renderSchemeAtRevision(
|
|
|
389
474
|
activeRenders.set(div, handle);
|
|
390
475
|
connectedRenders.add(handle);
|
|
391
476
|
ensureDisconnectedHostObserver();
|
|
392
|
-
if (
|
|
477
|
+
if (!exportAllowed) {
|
|
478
|
+
diagram.setExportEnabled(false);
|
|
479
|
+
} else if (options.onExportRequested !== undefined) {
|
|
393
480
|
const notifyHost = options.onExportRequested;
|
|
394
481
|
cleanups.push(diagram.on('exportRequested', (request) => notifyHost(request, handle)));
|
|
395
482
|
}
|
|
@@ -401,7 +488,8 @@ async function renderSchemeAtRevision(
|
|
|
401
488
|
|
|
402
489
|
try {
|
|
403
490
|
// Follow the site theme: the canvas colour comes from the live --diagram-bg CSS token; a theme toggle
|
|
404
|
-
// repaints it.
|
|
491
|
+
// repaints it. The repaint also re-reads every var(--token) colour the content names, so nodes and
|
|
492
|
+
// zones written in the page's tokens follow the switch too.
|
|
405
493
|
const applyDiagramTheme = () => {
|
|
406
494
|
const cs = getComputedStyle(document.documentElement);
|
|
407
495
|
const bg = cs.getPropertyValue('--diagram-bg').trim() || '#1b1b1f';
|
|
@@ -421,23 +509,7 @@ async function renderSchemeAtRevision(
|
|
|
421
509
|
|
|
422
510
|
diagram.setLinkValidator(() => true);
|
|
423
511
|
diagram.setOverviewVisible(false);
|
|
424
|
-
|
|
425
|
-
?? t('embedMissingElement', 'Element type "{typeId}" is missing from the palette.');
|
|
426
|
-
const converted = toDiagramNodes(
|
|
427
|
-
scheme,
|
|
428
|
-
catalog,
|
|
429
|
-
(typeId) => missingTypeTemplate.replace('{typeId}', typeId || '(empty)'),
|
|
430
|
-
);
|
|
431
|
-
diagram.load(
|
|
432
|
-
converted.nodes,
|
|
433
|
-
scheme.links.map((link) => new Link({
|
|
434
|
-
outNode: link.from,
|
|
435
|
-
outPort: link.fromPort,
|
|
436
|
-
inNode: link.to,
|
|
437
|
-
inPort: link.toPort,
|
|
438
|
-
})),
|
|
439
|
-
{ nodeErrors: converted.nodeErrors },
|
|
440
|
-
);
|
|
512
|
+
populate(diagram);
|
|
441
513
|
|
|
442
514
|
const fit = () => {
|
|
443
515
|
if (!div.isConnected) {
|
|
@@ -562,6 +634,9 @@ export async function renderFromSource(
|
|
|
562
634
|
return null;
|
|
563
635
|
}
|
|
564
636
|
|
|
637
|
+
if (holdsDocument(div, raw))
|
|
638
|
+
return drawDocumentAtRevision(div, raw, revision, options, errors);
|
|
639
|
+
|
|
565
640
|
const scheme = parseRawScheme(raw);
|
|
566
641
|
if (scheme.nodes.length === 0) {
|
|
567
642
|
note(div, errors.empty, revision);
|
|
@@ -596,6 +671,9 @@ export async function renderFromInline(
|
|
|
596
671
|
return null;
|
|
597
672
|
}
|
|
598
673
|
|
|
674
|
+
if (holdsDocument(div, raw))
|
|
675
|
+
return drawDocumentAtRevision(div, raw, revision, options, errors);
|
|
676
|
+
|
|
599
677
|
const scheme = parseRawScheme(raw);
|
|
600
678
|
if (scheme.nodes.length === 0) {
|
|
601
679
|
note(div, errors.empty, revision);
|
|
@@ -610,9 +688,49 @@ export async function renderFromInline(
|
|
|
610
688
|
}
|
|
611
689
|
}
|
|
612
690
|
|
|
691
|
+
// A host says it holds a generic document with data-diagram-kind="document". Without the attribute the
|
|
692
|
+
// shape decides: a document carries a numeric version and a node array at its root, which a saved
|
|
693
|
+
// strategy (Content.Value.Scheme.Model) never does.
|
|
694
|
+
function holdsDocument(div: HTMLElement, raw: unknown): boolean {
|
|
695
|
+
if (div.dataset.diagramKind === 'document')
|
|
696
|
+
return true;
|
|
697
|
+
return isRecord(raw) && typeof raw.version === 'number' && Array.isArray(raw.nodes);
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
function drawDocumentAtRevision(
|
|
701
|
+
div: HTMLElement,
|
|
702
|
+
raw: unknown,
|
|
703
|
+
revision: number,
|
|
704
|
+
options: DiagramEmbedOptions,
|
|
705
|
+
errors: EmbedErrorTexts,
|
|
706
|
+
): DiagramEmbedHandle | null {
|
|
707
|
+
if (renderRevisions.get(div) !== revision)
|
|
708
|
+
return null;
|
|
709
|
+
|
|
710
|
+
let parsed: DiagramDocument;
|
|
711
|
+
try {
|
|
712
|
+
parsed = readDocument(raw);
|
|
713
|
+
} catch {
|
|
714
|
+
note(div, errors.empty, revision);
|
|
715
|
+
return null;
|
|
716
|
+
}
|
|
717
|
+
if (parsed.nodes.length === 0 && parsed.zones.length === 0) {
|
|
718
|
+
note(div, errors.empty, revision);
|
|
719
|
+
return null;
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
try {
|
|
723
|
+
return mountDocument(div, parsed, options);
|
|
724
|
+
} catch {
|
|
725
|
+
note(div, errors.draw, revision);
|
|
726
|
+
return null;
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
|
|
613
730
|
// Render every not-yet-rendered diagram host under root (default: the whole document). Callable again after
|
|
614
731
|
// dynamic HTML is injected (e.g. the editor preview) to draw hosts that just appeared. A host either points
|
|
615
|
-
// at a source URL (data-diagram-src) or embeds its
|
|
732
|
+
// at a source URL (data-diagram-src) or embeds its JSON inline (a <script type="application/json">); the
|
|
733
|
+
// JSON is a strategy scheme or, marked by data-diagram-kind="document" or by its shape, a generic document.
|
|
616
734
|
export function renderAll(root: ParentNode = document, options: DiagramEmbedOptions = {}): void {
|
|
617
735
|
root.querySelectorAll<HTMLElement>('.ss-diagram-host').forEach((host) => {
|
|
618
736
|
if (host.dataset.rendered === '1')
|
package/src/index.ts
CHANGED
|
@@ -179,12 +179,14 @@ export {
|
|
|
179
179
|
destroyRenderedDiagram,
|
|
180
180
|
renderAll,
|
|
181
181
|
renderFromInline,
|
|
182
|
+
renderDocument,
|
|
182
183
|
renderFromSource,
|
|
183
184
|
renderScheme,
|
|
184
185
|
} from './embed.js';
|
|
185
186
|
|
|
186
187
|
export type {
|
|
187
188
|
DiagramEmbedHandle,
|
|
189
|
+
DiagramEmbedOptions,
|
|
188
190
|
DiagramEmbedScheme,
|
|
189
191
|
DiagramEmbedSchemeLink,
|
|
190
192
|
DiagramEmbedSchemeNode,
|