@affino/diagram-core 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 +133 -0
- package/dist/DiagramEngine.d.ts +72 -0
- package/dist/DiagramEngine.d.ts.map +1 -0
- package/dist/DiagramEngine.js +1545 -0
- package/dist/geometry.d.ts +14 -0
- package/dist/geometry.d.ts.map +1 -0
- package/dist/geometry.js +131 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/interaction.d.ts +48 -0
- package/dist/interaction.d.ts.map +1 -0
- package/dist/interaction.js +462 -0
- package/dist/internal/DiagramClipboardService.d.ts +10 -0
- package/dist/internal/DiagramClipboardService.d.ts.map +1 -0
- package/dist/internal/DiagramClipboardService.js +103 -0
- package/dist/internal/DiagramGeometryService.d.ts +9 -0
- package/dist/internal/DiagramGeometryService.d.ts.map +1 -0
- package/dist/internal/DiagramGeometryService.js +38 -0
- package/dist/internal/DiagramHistory.d.ts +15 -0
- package/dist/internal/DiagramHistory.d.ts.map +1 -0
- package/dist/internal/DiagramHistory.js +53 -0
- package/dist/internal/DiagramSceneStore.d.ts +18 -0
- package/dist/internal/DiagramSceneStore.d.ts.map +1 -0
- package/dist/internal/DiagramSceneStore.js +159 -0
- package/dist/internal/DiagramSpatialIndex.d.ts +13 -0
- package/dist/internal/DiagramSpatialIndex.d.ts.map +1 -0
- package/dist/internal/DiagramSpatialIndex.js +51 -0
- package/dist/internal/DiagramViewportService.d.ts +5 -0
- package/dist/internal/DiagramViewportService.d.ts.map +1 -0
- package/dist/internal/DiagramViewportService.js +16 -0
- package/dist/internal/model.d.ts +43 -0
- package/dist/internal/model.d.ts.map +1 -0
- package/dist/internal/model.js +2 -0
- package/dist/spatialIndex.d.ts +12 -0
- package/dist/spatialIndex.d.ts.map +1 -0
- package/dist/spatialIndex.js +71 -0
- package/dist/types.d.ts +337 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +1 -0
- package/package.json +44 -0
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { DiagramClipboard, DiagramId, DiagramPoint, DiagramSelection } from "../types.js";
|
|
2
|
+
import type { InternalState, Patch } from "./model.js";
|
|
3
|
+
type DeletePatchFactory = (ids: ReadonlyArray<DiagramId>) => Patch;
|
|
4
|
+
type NormalizeSelection = (selection: Partial<DiagramSelection> | undefined) => DiagramSelection;
|
|
5
|
+
export declare class DiagramClipboardService {
|
|
6
|
+
exportSubgraph(state: InternalState, ids: ReadonlyArray<DiagramId>, normalizeSelection: NormalizeSelection): DiagramClipboard;
|
|
7
|
+
createPastePatch(state: InternalState, clipboard: DiagramClipboard, offset: DiagramPoint, createDeletePatch: DeletePatchFactory, normalizeSelection: NormalizeSelection): Patch | null;
|
|
8
|
+
}
|
|
9
|
+
export {};
|
|
10
|
+
//# sourceMappingURL=DiagramClipboardService.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DiagramClipboardService.d.ts","sourceRoot":"","sources":["../../src/internal/DiagramClipboardService.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAoC,SAAS,EAAe,YAAY,EAAE,gBAAgB,EAA6B,MAAM,aAAa,CAAA;AACxK,OAAO,KAAK,EAAE,aAAa,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;AAEtD,KAAK,kBAAkB,GAAG,CAAC,GAAG,EAAE,aAAa,CAAC,SAAS,CAAC,KAAK,KAAK,CAAA;AAClE,KAAK,kBAAkB,GAAG,CAAC,SAAS,EAAE,OAAO,CAAC,gBAAgB,CAAC,GAAG,SAAS,KAAK,gBAAgB,CAAA;AAEhG,qBAAa,uBAAuB;IAClC,cAAc,CAAC,KAAK,EAAE,aAAa,EAAE,GAAG,EAAE,aAAa,CAAC,SAAS,CAAC,EAAE,kBAAkB,EAAE,kBAAkB,GAAG,gBAAgB;IAyB7H,gBAAgB,CAAC,KAAK,EAAE,aAAa,EAAE,SAAS,EAAE,gBAAgB,EAAE,MAAM,EAAE,YAAY,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,kBAAkB,GAAG,KAAK,GAAG,IAAI;CA4BvL"}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
export class DiagramClipboardService {
|
|
2
|
+
exportSubgraph(state, ids, normalizeSelection) {
|
|
3
|
+
const selected = new Set(ids);
|
|
4
|
+
const nodes = state.order.nodeIds
|
|
5
|
+
.map((id) => state.entities.nodesById.get(id))
|
|
6
|
+
.filter((entity) => entity !== undefined && selected.has(entity.id));
|
|
7
|
+
const ports = [...state.entities.portsById.values()].filter((port) => selected.has(port.id) || selected.has(port.nodeId));
|
|
8
|
+
const included = new Set([...selected, ...ports.map((port) => port.id)]);
|
|
9
|
+
const edges = state.order.edgeIds
|
|
10
|
+
.map((id) => state.entities.edgesById.get(id))
|
|
11
|
+
.filter((edge) => edge !== undefined && (selected.has(edge.id) || (endpointIncluded(edge.source, included) && endpointIncluded(edge.target, included))));
|
|
12
|
+
return {
|
|
13
|
+
nodes,
|
|
14
|
+
ports,
|
|
15
|
+
edges,
|
|
16
|
+
texts: state.order.textIds
|
|
17
|
+
.map((id) => state.entities.textsById.get(id))
|
|
18
|
+
.filter((entity) => entity !== undefined && selected.has(entity.id)),
|
|
19
|
+
shapes: state.order.shapeIds
|
|
20
|
+
.map((id) => state.entities.shapesById.get(id))
|
|
21
|
+
.filter((entity) => entity !== undefined && selected.has(entity.id)),
|
|
22
|
+
selection: normalizeSelection({ ids: [...selected], primaryId: state.selection.primaryId }),
|
|
23
|
+
viewport: state.viewport,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
createPastePatch(state, clipboard, offset, createDeletePatch, normalizeSelection) {
|
|
27
|
+
const remap = new Map();
|
|
28
|
+
const reserve = (id) => {
|
|
29
|
+
const next = uniqueId(state, `${id}-copy`, remap.size + 1);
|
|
30
|
+
remap.set(id, next);
|
|
31
|
+
return next;
|
|
32
|
+
};
|
|
33
|
+
for (const entity of [...clipboard.nodes, ...clipboard.ports, ...clipboard.edges, ...clipboard.texts, ...clipboard.shapes])
|
|
34
|
+
reserve(entity.id);
|
|
35
|
+
const nodes = clipboard.nodes.map((node) => { var _a; return ({ ...node, id: remap.get(node.id), x: node.x + offset.x, y: node.y + offset.y, portIds: (_a = node.portIds) === null || _a === void 0 ? void 0 : _a.map((id) => { var _a; return (_a = remap.get(id)) !== null && _a !== void 0 ? _a : id; }) }); });
|
|
36
|
+
const ports = clipboard.ports.map((port) => { var _a; return ({ ...port, id: remap.get(port.id), nodeId: (_a = remap.get(port.nodeId)) !== null && _a !== void 0 ? _a : port.nodeId }); });
|
|
37
|
+
const edges = clipboard.edges.map((edge) => { var _a; return ({ ...edge, id: remap.get(edge.id), source: remapEndpoint(edge.source, remap), target: remapEndpoint(edge.target, remap), points: (_a = edge.points) === null || _a === void 0 ? void 0 : _a.map((point) => ({ x: point.x + offset.x, y: point.y + offset.y })) }); });
|
|
38
|
+
const texts = clipboard.texts.map((text) => ({ ...text, id: remap.get(text.id), x: text.x + offset.x, y: text.y + offset.y }));
|
|
39
|
+
const shapes = clipboard.shapes.map((shape) => ({ ...shape, id: remap.get(shape.id), x: shape.x + offset.x, y: shape.y + offset.y }));
|
|
40
|
+
const pastedIds = [...nodes, ...ports, ...edges, ...texts, ...shapes].map((entity) => entity.id);
|
|
41
|
+
if (!pastedIds.length)
|
|
42
|
+
return null;
|
|
43
|
+
return {
|
|
44
|
+
apply: (next) => {
|
|
45
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
46
|
+
for (const node of nodes) {
|
|
47
|
+
next.entities.nodesById.set(node.id, node);
|
|
48
|
+
next.order.nodeIds.push(node.id);
|
|
49
|
+
next.versions.set(node.id, 0);
|
|
50
|
+
}
|
|
51
|
+
for (const port of ports) {
|
|
52
|
+
next.entities.portsById.set(port.id, port);
|
|
53
|
+
next.versions.set(port.id, 0);
|
|
54
|
+
}
|
|
55
|
+
for (const edge of edges) {
|
|
56
|
+
next.entities.edgesById.set(edge.id, edge);
|
|
57
|
+
next.order.edgeIds.push(edge.id);
|
|
58
|
+
next.versions.set(edge.id, 0);
|
|
59
|
+
}
|
|
60
|
+
for (const text of texts) {
|
|
61
|
+
next.entities.textsById.set(text.id, text);
|
|
62
|
+
next.order.textIds.push(text.id);
|
|
63
|
+
next.versions.set(text.id, 0);
|
|
64
|
+
}
|
|
65
|
+
for (const shape of shapes) {
|
|
66
|
+
next.entities.shapesById.set(shape.id, shape);
|
|
67
|
+
next.order.shapeIds.push(shape.id);
|
|
68
|
+
next.versions.set(shape.id, 0);
|
|
69
|
+
}
|
|
70
|
+
next.selection = normalizeSelection({ ids: pastedIds.filter((id) => !next.entities.portsById.has(id)), primaryId: (_h = (_f = (_d = (_b = (_a = nodes[0]) === null || _a === void 0 ? void 0 : _a.id) !== null && _b !== void 0 ? _b : (_c = texts[0]) === null || _c === void 0 ? void 0 : _c.id) !== null && _d !== void 0 ? _d : (_e = shapes[0]) === null || _e === void 0 ? void 0 : _e.id) !== null && _f !== void 0 ? _f : (_g = edges[0]) === null || _g === void 0 ? void 0 : _g.id) !== null && _h !== void 0 ? _h : null });
|
|
71
|
+
return new Set([...pastedIds, "selection"]);
|
|
72
|
+
},
|
|
73
|
+
inverse: createDeletePatch(pastedIds),
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
function endpointIncluded(endpoint, ids) {
|
|
78
|
+
if (endpoint.kind === "point")
|
|
79
|
+
return true;
|
|
80
|
+
if (endpoint.kind === "node")
|
|
81
|
+
return ids.has(endpoint.nodeId);
|
|
82
|
+
return ids.has(endpoint.portId);
|
|
83
|
+
}
|
|
84
|
+
function remapEndpoint(endpoint, remap) {
|
|
85
|
+
var _a, _b;
|
|
86
|
+
if (endpoint.kind === "point")
|
|
87
|
+
return { kind: "point", point: endpoint.point };
|
|
88
|
+
if (endpoint.kind === "node")
|
|
89
|
+
return { kind: "node", nodeId: (_a = remap.get(endpoint.nodeId)) !== null && _a !== void 0 ? _a : endpoint.nodeId };
|
|
90
|
+
return { kind: "port", portId: (_b = remap.get(endpoint.portId)) !== null && _b !== void 0 ? _b : endpoint.portId };
|
|
91
|
+
}
|
|
92
|
+
function uniqueId(state, base, seed) {
|
|
93
|
+
let id = `${base}-${seed}`;
|
|
94
|
+
let index = seed;
|
|
95
|
+
while (hasEntity(state, id)) {
|
|
96
|
+
index += 1;
|
|
97
|
+
id = `${base}-${index}`;
|
|
98
|
+
}
|
|
99
|
+
return id;
|
|
100
|
+
}
|
|
101
|
+
function hasEntity(state, id) {
|
|
102
|
+
return state.entities.nodesById.has(id) || state.entities.edgesById.has(id) || state.entities.textsById.has(id) || state.entities.shapesById.has(id) || state.entities.portsById.has(id);
|
|
103
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { DiagramEntities, DiagramGeometry, DiagramId } from "../types.js";
|
|
2
|
+
export declare class DiagramGeometryService {
|
|
3
|
+
private cache;
|
|
4
|
+
private readCount;
|
|
5
|
+
getReadCount(): number;
|
|
6
|
+
get(id: DiagramId, entities: DiagramEntities, version: number): DiagramGeometry | null;
|
|
7
|
+
invalidate(ids: Iterable<DiagramId>): void;
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=DiagramGeometryService.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DiagramGeometryService.d.ts","sourceRoot":"","sources":["../../src/internal/DiagramGeometryService.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,eAAe,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AAG9E,qBAAa,sBAAsB;IACjC,OAAO,CAAC,KAAK,CAA2C;IACxD,OAAO,CAAC,SAAS,CAAI;IAErB,YAAY,IAAI,MAAM;IAItB,GAAG,CAAC,EAAE,EAAE,SAAS,EAAE,QAAQ,EAAE,eAAe,EAAE,OAAO,EAAE,MAAM,GAAG,eAAe,GAAG,IAAI;IActF,UAAU,CAAC,GAAG,EAAE,QAAQ,CAAC,SAAS,CAAC,GAAG,IAAI;CAK3C"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { createEntityGeometry } from "../geometry.js";
|
|
2
|
+
export class DiagramGeometryService {
|
|
3
|
+
constructor() {
|
|
4
|
+
Object.defineProperty(this, "cache", {
|
|
5
|
+
enumerable: true,
|
|
6
|
+
configurable: true,
|
|
7
|
+
writable: true,
|
|
8
|
+
value: new Map()
|
|
9
|
+
});
|
|
10
|
+
Object.defineProperty(this, "readCount", {
|
|
11
|
+
enumerable: true,
|
|
12
|
+
configurable: true,
|
|
13
|
+
writable: true,
|
|
14
|
+
value: 0
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
getReadCount() {
|
|
18
|
+
return this.readCount;
|
|
19
|
+
}
|
|
20
|
+
get(id, entities, version) {
|
|
21
|
+
const cached = this.cache.get(id);
|
|
22
|
+
if ((cached === null || cached === void 0 ? void 0 : cached.version) === version) {
|
|
23
|
+
return cached.geometry;
|
|
24
|
+
}
|
|
25
|
+
const geometry = createEntityGeometry(id, entities);
|
|
26
|
+
if (!geometry) {
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
this.readCount += 1;
|
|
30
|
+
this.cache.set(id, { version, geometry });
|
|
31
|
+
return geometry;
|
|
32
|
+
}
|
|
33
|
+
invalidate(ids) {
|
|
34
|
+
for (const id of ids) {
|
|
35
|
+
this.cache.delete(id);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { HistoryEntry, Patch } from "./model.js";
|
|
2
|
+
export declare class DiagramHistory {
|
|
3
|
+
private undoStack;
|
|
4
|
+
private redoStack;
|
|
5
|
+
canUndo(): boolean;
|
|
6
|
+
canRedo(): boolean;
|
|
7
|
+
undoDepth(): number;
|
|
8
|
+
redoDepth(): number;
|
|
9
|
+
popUndo(): HistoryEntry | null;
|
|
10
|
+
pushUndo(entry: HistoryEntry): void;
|
|
11
|
+
popRedo(): HistoryEntry | null;
|
|
12
|
+
pushRedo(entry: HistoryEntry): void;
|
|
13
|
+
record(patch: Patch, historyKey: string | null, compose: (first: Patch, second: Patch) => Patch): void;
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=DiagramHistory.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DiagramHistory.d.ts","sourceRoot":"","sources":["../../src/internal/DiagramHistory.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;AAErD,qBAAa,cAAc;IACzB,OAAO,CAAC,SAAS,CAAqB;IACtC,OAAO,CAAC,SAAS,CAAqB;IAEtC,OAAO,IAAI,OAAO;IAIlB,OAAO,IAAI,OAAO;IAIlB,SAAS,IAAI,MAAM;IAInB,SAAS,IAAI,MAAM;IAInB,OAAO,IAAI,YAAY,GAAG,IAAI;IAI9B,QAAQ,CAAC,KAAK,EAAE,YAAY,GAAG,IAAI;IAInC,OAAO,IAAI,YAAY,GAAG,IAAI;IAI9B,QAAQ,CAAC,KAAK,EAAE,YAAY,GAAG,IAAI;IAInC,MAAM,CAAC,KAAK,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM,GAAG,IAAI,EAAE,OAAO,EAAE,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,KAAK,KAAK,GAAG,IAAI;CAUvG"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
export class DiagramHistory {
|
|
2
|
+
constructor() {
|
|
3
|
+
Object.defineProperty(this, "undoStack", {
|
|
4
|
+
enumerable: true,
|
|
5
|
+
configurable: true,
|
|
6
|
+
writable: true,
|
|
7
|
+
value: []
|
|
8
|
+
});
|
|
9
|
+
Object.defineProperty(this, "redoStack", {
|
|
10
|
+
enumerable: true,
|
|
11
|
+
configurable: true,
|
|
12
|
+
writable: true,
|
|
13
|
+
value: []
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
canUndo() {
|
|
17
|
+
return this.undoStack.length > 0;
|
|
18
|
+
}
|
|
19
|
+
canRedo() {
|
|
20
|
+
return this.redoStack.length > 0;
|
|
21
|
+
}
|
|
22
|
+
undoDepth() {
|
|
23
|
+
return this.undoStack.length;
|
|
24
|
+
}
|
|
25
|
+
redoDepth() {
|
|
26
|
+
return this.redoStack.length;
|
|
27
|
+
}
|
|
28
|
+
popUndo() {
|
|
29
|
+
var _a;
|
|
30
|
+
return (_a = this.undoStack.pop()) !== null && _a !== void 0 ? _a : null;
|
|
31
|
+
}
|
|
32
|
+
pushUndo(entry) {
|
|
33
|
+
this.undoStack.push(entry);
|
|
34
|
+
}
|
|
35
|
+
popRedo() {
|
|
36
|
+
var _a;
|
|
37
|
+
return (_a = this.redoStack.pop()) !== null && _a !== void 0 ? _a : null;
|
|
38
|
+
}
|
|
39
|
+
pushRedo(entry) {
|
|
40
|
+
this.redoStack.push(entry);
|
|
41
|
+
}
|
|
42
|
+
record(patch, historyKey, compose) {
|
|
43
|
+
const previous = historyKey ? this.undoStack[this.undoStack.length - 1] : null;
|
|
44
|
+
if ((previous === null || previous === void 0 ? void 0 : previous.key) === historyKey) {
|
|
45
|
+
previous.patch = compose(previous.patch, patch);
|
|
46
|
+
previous.inverse = compose(patch.inverse, previous.inverse);
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
this.undoStack.push({ patch, inverse: patch.inverse, key: historyKey });
|
|
50
|
+
}
|
|
51
|
+
this.redoStack = [];
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { DiagramChange, DiagramId, DiagramScene, DiagramSceneInput, DiagramSubscriber } from "../types.js";
|
|
2
|
+
import type { InternalState } from "./model.js";
|
|
3
|
+
export declare class DiagramSceneStore {
|
|
4
|
+
readonly state: InternalState;
|
|
5
|
+
private snapshot;
|
|
6
|
+
private subscribers;
|
|
7
|
+
private lastChange;
|
|
8
|
+
constructor(initialScene?: DiagramSceneInput);
|
|
9
|
+
getSnapshot(): DiagramScene;
|
|
10
|
+
getLastChange(): DiagramChange;
|
|
11
|
+
publish(changedIds: ReadonlySet<DiagramId>, invalidatedIds: ReadonlySet<DiagramId>): void;
|
|
12
|
+
subscribe(listener: DiagramSubscriber): {
|
|
13
|
+
unsubscribe: () => void;
|
|
14
|
+
};
|
|
15
|
+
private createSnapshot;
|
|
16
|
+
}
|
|
17
|
+
export declare function createInternalState(input: DiagramSceneInput): InternalState;
|
|
18
|
+
//# sourceMappingURL=DiagramSceneStore.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DiagramSceneStore.d.ts","sourceRoot":"","sources":["../../src/internal/DiagramSceneStore.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,SAAS,EAAE,YAAY,EAAE,iBAAiB,EAAoB,iBAAiB,EAAE,MAAM,aAAa,CAAA;AAEjI,OAAO,KAAK,EAAE,aAAa,EAAmB,MAAM,YAAY,CAAA;AAIhE,qBAAa,iBAAiB;IAC5B,QAAQ,CAAC,KAAK,EAAE,aAAa,CAAA;IAC7B,OAAO,CAAC,QAAQ,CAAc;IAC9B,OAAO,CAAC,WAAW,CAA+B;IAClD,OAAO,CAAC,UAAU,CAIjB;gBAEW,YAAY,GAAE,iBAAsB;IAKhD,WAAW,IAAI,YAAY;IAI3B,aAAa,IAAI,aAAa;IAI9B,OAAO,CAAC,UAAU,EAAE,WAAW,CAAC,SAAS,CAAC,EAAE,cAAc,EAAE,WAAW,CAAC,SAAS,CAAC,GAAG,IAAI;IAYzF,SAAS,CAAC,QAAQ,EAAE,iBAAiB,GAAG;QAAE,WAAW,EAAE,MAAM,IAAI,CAAA;KAAE;IAUnE,OAAO,CAAC,cAAc;CAuBvB;AAED,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,iBAAiB,GAAG,aAAa,CA4B3E"}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { DEFAULT_VIEWPORT } from "./model.js";
|
|
2
|
+
const EMPTY_SELECTION = Object.freeze({ ids: Object.freeze([]), primaryId: null });
|
|
3
|
+
export class DiagramSceneStore {
|
|
4
|
+
constructor(initialScene = {}) {
|
|
5
|
+
Object.defineProperty(this, "state", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
configurable: true,
|
|
8
|
+
writable: true,
|
|
9
|
+
value: void 0
|
|
10
|
+
});
|
|
11
|
+
Object.defineProperty(this, "snapshot", {
|
|
12
|
+
enumerable: true,
|
|
13
|
+
configurable: true,
|
|
14
|
+
writable: true,
|
|
15
|
+
value: void 0
|
|
16
|
+
});
|
|
17
|
+
Object.defineProperty(this, "subscribers", {
|
|
18
|
+
enumerable: true,
|
|
19
|
+
configurable: true,
|
|
20
|
+
writable: true,
|
|
21
|
+
value: new Set()
|
|
22
|
+
});
|
|
23
|
+
Object.defineProperty(this, "lastChange", {
|
|
24
|
+
enumerable: true,
|
|
25
|
+
configurable: true,
|
|
26
|
+
writable: true,
|
|
27
|
+
value: {
|
|
28
|
+
revision: 0,
|
|
29
|
+
changedIds: Object.freeze(new Set()),
|
|
30
|
+
invalidatedIds: Object.freeze(new Set()),
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
this.state = createInternalState(initialScene);
|
|
34
|
+
this.snapshot = this.createSnapshot();
|
|
35
|
+
}
|
|
36
|
+
getSnapshot() {
|
|
37
|
+
return this.snapshot;
|
|
38
|
+
}
|
|
39
|
+
getLastChange() {
|
|
40
|
+
return this.lastChange;
|
|
41
|
+
}
|
|
42
|
+
publish(changedIds, invalidatedIds) {
|
|
43
|
+
this.snapshot = this.createSnapshot();
|
|
44
|
+
this.lastChange = {
|
|
45
|
+
revision: this.state.revision,
|
|
46
|
+
changedIds: Object.freeze(new Set(changedIds)),
|
|
47
|
+
invalidatedIds: Object.freeze(new Set(invalidatedIds)),
|
|
48
|
+
};
|
|
49
|
+
for (const listener of this.subscribers) {
|
|
50
|
+
listener(this.snapshot, this.lastChange);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
subscribe(listener) {
|
|
54
|
+
this.subscribers.add(listener);
|
|
55
|
+
listener(this.snapshot, this.lastChange);
|
|
56
|
+
return {
|
|
57
|
+
unsubscribe: () => {
|
|
58
|
+
this.subscribers.delete(listener);
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
createSnapshot() {
|
|
63
|
+
return deepFreeze({
|
|
64
|
+
entities: {
|
|
65
|
+
nodesById: createReadonlyMap(this.state.entities.nodesById),
|
|
66
|
+
edgesById: createReadonlyMap(this.state.entities.edgesById),
|
|
67
|
+
textsById: createReadonlyMap(this.state.entities.textsById),
|
|
68
|
+
shapesById: createReadonlyMap(this.state.entities.shapesById),
|
|
69
|
+
portsById: createReadonlyMap(this.state.entities.portsById),
|
|
70
|
+
},
|
|
71
|
+
order: {
|
|
72
|
+
nodeIds: [...this.state.order.nodeIds],
|
|
73
|
+
edgeIds: [...this.state.order.edgeIds],
|
|
74
|
+
textIds: [...this.state.order.textIds],
|
|
75
|
+
shapeIds: [...this.state.order.shapeIds],
|
|
76
|
+
},
|
|
77
|
+
selection: {
|
|
78
|
+
ids: [...this.state.selection.ids],
|
|
79
|
+
primaryId: this.state.selection.primaryId,
|
|
80
|
+
},
|
|
81
|
+
viewport: { ...this.state.viewport },
|
|
82
|
+
revision: this.state.revision,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
export function createInternalState(input) {
|
|
87
|
+
var _a, _b, _c, _d, _e;
|
|
88
|
+
const entities = {
|
|
89
|
+
nodesById: toMap((_a = input.nodes) !== null && _a !== void 0 ? _a : []),
|
|
90
|
+
edgesById: toMap((_b = input.edges) !== null && _b !== void 0 ? _b : []),
|
|
91
|
+
textsById: toMap((_c = input.texts) !== null && _c !== void 0 ? _c : []),
|
|
92
|
+
shapesById: toMap((_d = input.shapes) !== null && _d !== void 0 ? _d : []),
|
|
93
|
+
portsById: toMap((_e = input.ports) !== null && _e !== void 0 ? _e : []),
|
|
94
|
+
};
|
|
95
|
+
const ids = [
|
|
96
|
+
...entities.nodesById.keys(),
|
|
97
|
+
...entities.edgesById.keys(),
|
|
98
|
+
...entities.textsById.keys(),
|
|
99
|
+
...entities.shapesById.keys(),
|
|
100
|
+
...entities.portsById.keys(),
|
|
101
|
+
];
|
|
102
|
+
return {
|
|
103
|
+
entities,
|
|
104
|
+
order: {
|
|
105
|
+
nodeIds: [...entities.nodesById.keys()],
|
|
106
|
+
edgeIds: [...entities.edgesById.keys()],
|
|
107
|
+
textIds: [...entities.textsById.keys()],
|
|
108
|
+
shapeIds: [...entities.shapesById.keys()],
|
|
109
|
+
},
|
|
110
|
+
selection: normalizeSelection(input.selection),
|
|
111
|
+
viewport: { ...DEFAULT_VIEWPORT, ...input.viewport },
|
|
112
|
+
revision: 0,
|
|
113
|
+
versions: new Map(ids.map((id) => [id, 0])),
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
function normalizeSelection(selection) {
|
|
117
|
+
var _a, _b;
|
|
118
|
+
if (!((_a = selection === null || selection === void 0 ? void 0 : selection.ids) === null || _a === void 0 ? void 0 : _a.length))
|
|
119
|
+
return EMPTY_SELECTION;
|
|
120
|
+
const ids = [...new Set(selection.ids)];
|
|
121
|
+
return { ids, primaryId: selection.primaryId && ids.includes(selection.primaryId) ? selection.primaryId : (_b = ids[0]) !== null && _b !== void 0 ? _b : null };
|
|
122
|
+
}
|
|
123
|
+
function toMap(entities) {
|
|
124
|
+
return new Map(entities.map((entity) => [entity.id, entity]));
|
|
125
|
+
}
|
|
126
|
+
function createReadonlyMap(source) {
|
|
127
|
+
const target = new Map();
|
|
128
|
+
for (const [id, entity] of source) {
|
|
129
|
+
Map.prototype.set.call(target, id, deepFreeze(cloneValue(entity)));
|
|
130
|
+
}
|
|
131
|
+
Object.defineProperties(target, {
|
|
132
|
+
set: { value: readonlyMapMutation, writable: false },
|
|
133
|
+
delete: { value: readonlyMapMutation, writable: false },
|
|
134
|
+
clear: { value: readonlyMapMutation, writable: false },
|
|
135
|
+
});
|
|
136
|
+
return Object.freeze(target);
|
|
137
|
+
}
|
|
138
|
+
function readonlyMapMutation() {
|
|
139
|
+
throw new TypeError("Diagram snapshots are readonly");
|
|
140
|
+
}
|
|
141
|
+
function cloneValue(value) {
|
|
142
|
+
if (Array.isArray(value)) {
|
|
143
|
+
return value.map((item) => cloneValue(item));
|
|
144
|
+
}
|
|
145
|
+
if (value && typeof value === "object") {
|
|
146
|
+
return Object.fromEntries(Object.entries(value).map(([key, child]) => [key, cloneValue(child)]));
|
|
147
|
+
}
|
|
148
|
+
return value;
|
|
149
|
+
}
|
|
150
|
+
function deepFreeze(value) {
|
|
151
|
+
if (!value || typeof value !== "object" || Object.isFrozen(value)) {
|
|
152
|
+
return value;
|
|
153
|
+
}
|
|
154
|
+
Object.freeze(value);
|
|
155
|
+
for (const child of Object.values(value)) {
|
|
156
|
+
deepFreeze(child);
|
|
157
|
+
}
|
|
158
|
+
return value;
|
|
159
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { DiagramGeometry, DiagramId, DiagramRect } from "../types.js";
|
|
2
|
+
export declare class DiagramSpatialIndex {
|
|
3
|
+
private visualBoundsIndex;
|
|
4
|
+
private hitBoundsIndex;
|
|
5
|
+
private portIndex;
|
|
6
|
+
private dirty;
|
|
7
|
+
markDirty(): void;
|
|
8
|
+
ensure(rebuild: () => ReadonlyArray<DiagramGeometry>): void;
|
|
9
|
+
queryVisible(bounds: DiagramRect): DiagramId[];
|
|
10
|
+
queryHit(bounds: DiagramRect): DiagramId[];
|
|
11
|
+
queryPorts(bounds: DiagramRect): DiagramId[];
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=DiagramSpatialIndex.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DiagramSpatialIndex.d.ts","sourceRoot":"","sources":["../../src/internal/DiagramSpatialIndex.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;AAE1E,qBAAa,mBAAmB;IAC9B,OAAO,CAAC,iBAAiB,CAAyB;IAClD,OAAO,CAAC,cAAc,CAAyB;IAC/C,OAAO,CAAC,SAAS,CAA4B;IAC7C,OAAO,CAAC,KAAK,CAAO;IAEpB,SAAS,IAAI,IAAI;IAIjB,MAAM,CAAC,OAAO,EAAE,MAAM,aAAa,CAAC,eAAe,CAAC,GAAG,IAAI;IAW3D,YAAY,CAAC,MAAM,EAAE,WAAW,GAAG,SAAS,EAAE;IAI9C,QAAQ,CAAC,MAAM,EAAE,WAAW,GAAG,SAAS,EAAE;IAI1C,UAAU,CAAC,MAAM,EAAE,WAAW,GAAG,SAAS,EAAE;CAG7C"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { UniformGridIndex } from "../spatialIndex.js";
|
|
2
|
+
export class DiagramSpatialIndex {
|
|
3
|
+
constructor() {
|
|
4
|
+
Object.defineProperty(this, "visualBoundsIndex", {
|
|
5
|
+
enumerable: true,
|
|
6
|
+
configurable: true,
|
|
7
|
+
writable: true,
|
|
8
|
+
value: new UniformGridIndex()
|
|
9
|
+
});
|
|
10
|
+
Object.defineProperty(this, "hitBoundsIndex", {
|
|
11
|
+
enumerable: true,
|
|
12
|
+
configurable: true,
|
|
13
|
+
writable: true,
|
|
14
|
+
value: new UniformGridIndex()
|
|
15
|
+
});
|
|
16
|
+
Object.defineProperty(this, "portIndex", {
|
|
17
|
+
enumerable: true,
|
|
18
|
+
configurable: true,
|
|
19
|
+
writable: true,
|
|
20
|
+
value: new UniformGridIndex(128)
|
|
21
|
+
});
|
|
22
|
+
Object.defineProperty(this, "dirty", {
|
|
23
|
+
enumerable: true,
|
|
24
|
+
configurable: true,
|
|
25
|
+
writable: true,
|
|
26
|
+
value: true
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
markDirty() {
|
|
30
|
+
this.dirty = true;
|
|
31
|
+
}
|
|
32
|
+
ensure(rebuild) {
|
|
33
|
+
if (!this.dirty) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
const geometries = rebuild();
|
|
37
|
+
this.visualBoundsIndex.rebuild(geometries);
|
|
38
|
+
this.hitBoundsIndex.rebuild(geometries, true);
|
|
39
|
+
this.portIndex.rebuild(geometries.filter((geometry) => geometry.kind === "port"), true);
|
|
40
|
+
this.dirty = false;
|
|
41
|
+
}
|
|
42
|
+
queryVisible(bounds) {
|
|
43
|
+
return this.visualBoundsIndex.query(bounds);
|
|
44
|
+
}
|
|
45
|
+
queryHit(bounds) {
|
|
46
|
+
return this.hitBoundsIndex.query(bounds);
|
|
47
|
+
}
|
|
48
|
+
queryPorts(bounds) {
|
|
49
|
+
return this.portIndex.query(bounds);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DiagramViewportService.d.ts","sourceRoot":"","sources":["../../src/internal/DiagramViewportService.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AAE/D,qBAAa,sBAAsB;IACjC,SAAS,CAAC,QAAQ,EAAE,eAAe,EAAE,MAAM,EAAE,WAAW,EAAE,OAAO,SAAK,GAAG,eAAe;CAczF"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export class DiagramViewportService {
|
|
2
|
+
fitBounds(viewport, bounds, padding = 24) {
|
|
3
|
+
const targetWidth = Math.max(1, bounds.width + padding * 2);
|
|
4
|
+
const targetHeight = Math.max(1, bounds.height + padding * 2);
|
|
5
|
+
const screenWidth = Math.max(1, viewport.width * viewport.zoom);
|
|
6
|
+
const screenHeight = Math.max(1, viewport.height * viewport.zoom);
|
|
7
|
+
const zoom = Math.min(screenWidth / targetWidth, screenHeight / targetHeight);
|
|
8
|
+
return {
|
|
9
|
+
x: bounds.x - (screenWidth / zoom - bounds.width) / 2,
|
|
10
|
+
y: bounds.y - (screenHeight / zoom - bounds.height) / 2,
|
|
11
|
+
width: screenWidth / zoom,
|
|
12
|
+
height: screenHeight / zoom,
|
|
13
|
+
zoom,
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { DiagramEdge, DiagramGeometry, DiagramId, DiagramNode, DiagramPort, DiagramSceneInput, DiagramSelection, DiagramShape, DiagramText, DiagramViewport } from "../types.js";
|
|
2
|
+
export type MutableEntities = {
|
|
3
|
+
nodesById: Map<DiagramId, DiagramNode>;
|
|
4
|
+
edgesById: Map<DiagramId, DiagramEdge>;
|
|
5
|
+
textsById: Map<DiagramId, DiagramText>;
|
|
6
|
+
shapesById: Map<DiagramId, DiagramShape>;
|
|
7
|
+
portsById: Map<DiagramId, DiagramPort>;
|
|
8
|
+
};
|
|
9
|
+
export type MutableOrder = {
|
|
10
|
+
nodeIds: DiagramId[];
|
|
11
|
+
edgeIds: DiagramId[];
|
|
12
|
+
textIds: DiagramId[];
|
|
13
|
+
shapeIds: DiagramId[];
|
|
14
|
+
};
|
|
15
|
+
export type InternalState = {
|
|
16
|
+
entities: MutableEntities;
|
|
17
|
+
order: MutableOrder;
|
|
18
|
+
selection: DiagramSelection;
|
|
19
|
+
viewport: DiagramViewport;
|
|
20
|
+
revision: number;
|
|
21
|
+
versions: Map<DiagramId, number>;
|
|
22
|
+
};
|
|
23
|
+
export type Patch = {
|
|
24
|
+
apply: (state: InternalState) => Set<DiagramId>;
|
|
25
|
+
inverse: Patch;
|
|
26
|
+
};
|
|
27
|
+
export type HistoryEntry = {
|
|
28
|
+
patch: Patch;
|
|
29
|
+
inverse: Patch;
|
|
30
|
+
key: string | null;
|
|
31
|
+
};
|
|
32
|
+
export type GeometryCacheEntry = {
|
|
33
|
+
version: number;
|
|
34
|
+
geometry: DiagramGeometry;
|
|
35
|
+
};
|
|
36
|
+
export type TransactionResult = {
|
|
37
|
+
changedIds: Set<DiagramId>;
|
|
38
|
+
invalidatedIds: Set<DiagramId>;
|
|
39
|
+
};
|
|
40
|
+
export declare const DEFAULT_VIEWPORT: DiagramViewport;
|
|
41
|
+
export declare const EMPTY_SELECTION: DiagramSelection;
|
|
42
|
+
export type DiagramSceneStoreInput = DiagramSceneInput;
|
|
43
|
+
//# sourceMappingURL=model.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"model.d.ts","sourceRoot":"","sources":["../../src/internal/model.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAiB,WAAW,EAAE,eAAe,EAAE,SAAS,EAAE,WAAW,EAAE,WAAW,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,YAAY,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AAEpM,MAAM,MAAM,eAAe,GAAG;IAC5B,SAAS,EAAE,GAAG,CAAC,SAAS,EAAE,WAAW,CAAC,CAAA;IACtC,SAAS,EAAE,GAAG,CAAC,SAAS,EAAE,WAAW,CAAC,CAAA;IACtC,SAAS,EAAE,GAAG,CAAC,SAAS,EAAE,WAAW,CAAC,CAAA;IACtC,UAAU,EAAE,GAAG,CAAC,SAAS,EAAE,YAAY,CAAC,CAAA;IACxC,SAAS,EAAE,GAAG,CAAC,SAAS,EAAE,WAAW,CAAC,CAAA;CACvC,CAAA;AAED,MAAM,MAAM,YAAY,GAAG;IACzB,OAAO,EAAE,SAAS,EAAE,CAAA;IACpB,OAAO,EAAE,SAAS,EAAE,CAAA;IACpB,OAAO,EAAE,SAAS,EAAE,CAAA;IACpB,QAAQ,EAAE,SAAS,EAAE,CAAA;CACtB,CAAA;AAED,MAAM,MAAM,aAAa,GAAG;IAC1B,QAAQ,EAAE,eAAe,CAAA;IACzB,KAAK,EAAE,YAAY,CAAA;IACnB,SAAS,EAAE,gBAAgB,CAAA;IAC3B,QAAQ,EAAE,eAAe,CAAA;IACzB,QAAQ,EAAE,MAAM,CAAA;IAChB,QAAQ,EAAE,GAAG,CAAC,SAAS,EAAE,MAAM,CAAC,CAAA;CACjC,CAAA;AAED,MAAM,MAAM,KAAK,GAAG;IAClB,KAAK,EAAE,CAAC,KAAK,EAAE,aAAa,KAAK,GAAG,CAAC,SAAS,CAAC,CAAA;IAC/C,OAAO,EAAE,KAAK,CAAA;CACf,CAAA;AAED,MAAM,MAAM,YAAY,GAAG;IACzB,KAAK,EAAE,KAAK,CAAA;IACZ,OAAO,EAAE,KAAK,CAAA;IACd,GAAG,EAAE,MAAM,GAAG,IAAI,CAAA;CACnB,CAAA;AAED,MAAM,MAAM,kBAAkB,GAAG;IAC/B,OAAO,EAAE,MAAM,CAAA;IACf,QAAQ,EAAE,eAAe,CAAA;CAC1B,CAAA;AAED,MAAM,MAAM,iBAAiB,GAAG;IAC9B,UAAU,EAAE,GAAG,CAAC,SAAS,CAAC,CAAA;IAC1B,cAAc,EAAE,GAAG,CAAC,SAAS,CAAC,CAAA;CAC/B,CAAA;AAED,eAAO,MAAM,gBAAgB,EAAE,eAA6E,CAAA;AAC5G,eAAO,MAAM,eAAe,EAAE,gBAA6E,CAAA;AAE3G,MAAM,MAAM,sBAAsB,GAAG,iBAAiB,CAAA"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { DiagramGeometry, DiagramId, DiagramRect } from "./types.js";
|
|
2
|
+
export declare class UniformGridIndex {
|
|
3
|
+
private readonly cellSize;
|
|
4
|
+
private cells;
|
|
5
|
+
private entries;
|
|
6
|
+
constructor(cellSize?: number);
|
|
7
|
+
rebuild(geometries: Iterable<DiagramGeometry>, useHitBounds?: boolean): void;
|
|
8
|
+
query(rect: DiagramRect): DiagramId[];
|
|
9
|
+
private insert;
|
|
10
|
+
private keysForRect;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=spatialIndex.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"spatialIndex.d.ts","sourceRoot":"","sources":["../src/spatialIndex.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AAOzE,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAQ;IACjC,OAAO,CAAC,KAAK,CAAoC;IACjD,OAAO,CAAC,OAAO,CAAkC;gBAErC,QAAQ,SAAM;IAI1B,OAAO,CAAC,UAAU,EAAE,QAAQ,CAAC,eAAe,CAAC,EAAE,YAAY,UAAQ,GAAG,IAAI;IAQ1E,KAAK,CAAC,IAAI,EAAE,WAAW,GAAG,SAAS,EAAE;IAiBrC,OAAO,CAAC,MAAM;IAYd,OAAO,CAAC,WAAW;CAapB"}
|