@html-graph/html-graph 0.0.43

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/main.js ADDED
@@ -0,0 +1,1308 @@
1
+ var V = Object.defineProperty;
2
+ var k = (n, t, e) => t in n ? V(n, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : n[t] = e;
3
+ var r = (n, t, e) => k(n, typeof t != "symbol" ? t + "" : t, e);
4
+ class nt {
5
+ constructor() {
6
+ r(this, "coreOptions");
7
+ r(this, "transformOptions");
8
+ r(this, "isDraggable", !1);
9
+ r(this, "isTransformable", !1);
10
+ }
11
+ options(t) {
12
+ return this.coreOptions = t, this;
13
+ }
14
+ draggableNodes() {
15
+ return this.isDraggable = !0, this;
16
+ }
17
+ transformableCanvas(t) {
18
+ return this.isTransformable = !0, this.transformOptions = t, this;
19
+ }
20
+ build() {
21
+ let t = new I(this.coreOptions);
22
+ return this.isDraggable && (t = new tt(t)), this.isTransformable && (t = new et(t, this.transformOptions)), t;
23
+ }
24
+ }
25
+ class j {
26
+ constructor(t) {
27
+ r(this, "canvasWrapper", null);
28
+ r(this, "host");
29
+ r(this, "nodesContainer");
30
+ r(this, "connectionsContainer");
31
+ r(this, "canvas");
32
+ r(this, "canvasCtx");
33
+ r(this, "hostResizeObserver");
34
+ r(this, "nodesResizeObserver");
35
+ r(this, "nodeElementToIdMap", /* @__PURE__ */ new Map());
36
+ r(this, "nodeWrapperElementToIdMap", /* @__PURE__ */ new Map());
37
+ r(this, "nodeIdToWrapperElementMap", /* @__PURE__ */ new Map());
38
+ r(this, "connectionIdToElementMap", /* @__PURE__ */ new Map());
39
+ r(this, "currentZIndex", 0);
40
+ r(this, "layers", {
41
+ "connections-on-top": {
42
+ create: () => {
43
+ this.host.appendChild(this.nodesContainer), this.host.appendChild(this.connectionsContainer);
44
+ },
45
+ update: (t, e, o) => {
46
+ this.nodesContainer.style.transform = `matrix(${t}, 0, 0, ${t}, ${e}, ${o})`, this.connectionsContainer.style.transform = `matrix(${t}, 0, 0, ${t}, ${e}, ${o})`;
47
+ },
48
+ moveOnTop: (t) => {
49
+ this.currentZIndex += 1;
50
+ const e = this.nodeIdToWrapperElementMap.get(t);
51
+ e.style.zIndex = `${this.currentZIndex}`;
52
+ }
53
+ },
54
+ "connections-follow-node": {
55
+ create: () => {
56
+ this.host.appendChild(this.nodesContainer), this.connectionsContainer = this.nodesContainer;
57
+ },
58
+ update: (t, e, o) => {
59
+ this.nodesContainer.style.transform = `matrix(${t}, 0, 0, ${t}, ${e}, ${o})`;
60
+ },
61
+ moveOnTop: (t) => {
62
+ const e = this.nodeIdToWrapperElementMap.get(t);
63
+ this.currentZIndex += 2, e.style.zIndex = `${this.currentZIndex}`, this.di.graphStore.getNodeAdjacentConnections(t).forEach((s) => {
64
+ this.connectionIdToElementMap.get(s).style.zIndex = `${this.currentZIndex - 1}`;
65
+ });
66
+ }
67
+ },
68
+ "nodes-on-top": {
69
+ create: () => {
70
+ this.host.appendChild(this.connectionsContainer), this.host.appendChild(this.nodesContainer);
71
+ },
72
+ update: (t, e, o) => {
73
+ this.nodesContainer.style.transform = `matrix(${t}, 0, 0, ${t}, ${e}, ${o})`, this.connectionsContainer.style.transform = `matrix(${t}, 0, 0, ${t}, ${e}, ${o})`;
74
+ },
75
+ moveOnTop: (t) => {
76
+ this.currentZIndex += 1;
77
+ const e = this.nodeIdToWrapperElementMap.get(t);
78
+ e.style.zIndex = `${this.currentZIndex}`;
79
+ }
80
+ }
81
+ });
82
+ this.di = t, this.host = this.createHost(), this.canvas = this.createCanvas(), this.nodesContainer = this.createNodesContainer(), this.connectionsContainer = this.createConnectionsContainer();
83
+ const e = this.canvas.getContext("2d");
84
+ if (e === null)
85
+ throw new Error("unable to get canvas context");
86
+ this.canvasCtx = e, this.host.appendChild(this.canvas);
87
+ const o = this.di.options.layers.mode;
88
+ this.layers[o].create(), this.hostResizeObserver = this.createHostResizeObserver(), this.hostResizeObserver.observe(this.host), this.nodesResizeObserver = this.createNodesResizeObserver();
89
+ }
90
+ clear() {
91
+ Array.from(this.connectionIdToElementMap.keys()).forEach((t) => {
92
+ this.detachConnection(t);
93
+ }), Array.from(this.nodeElementToIdMap.values()).forEach((t) => {
94
+ this.detachNode(t);
95
+ });
96
+ }
97
+ attach(t) {
98
+ this.canvasWrapper = t, this.canvasWrapper.appendChild(this.host);
99
+ }
100
+ detach() {
101
+ this.canvasWrapper !== null && this.canvasWrapper.removeChild(this.host);
102
+ }
103
+ destroy() {
104
+ this.hostResizeObserver.disconnect(), this.nodesResizeObserver.disconnect(), this.host.removeChild(this.canvas), this.host.removeChild(this.connectionsContainer), this.host.removeChild(this.nodesContainer), this.canvasWrapper !== null && this.canvasWrapper.removeChild(this.host);
105
+ }
106
+ applyTransform() {
107
+ this.canvasCtx.clearRect(
108
+ 0,
109
+ 0,
110
+ this.canvasCtx.canvas.width,
111
+ this.canvasCtx.canvas.height
112
+ ), this.canvasCtx.save(), this.di.options.background.drawingFn(
113
+ this.canvasCtx,
114
+ this.di.publicViewportTransformer
115
+ ), this.canvasCtx.restore();
116
+ const [t, e] = this.di.viewportTransformer.getViewCoords(0, 0), o = this.di.viewportTransformer.getViewScale();
117
+ this.layers[this.di.options.layers.mode].update(o, t, e);
118
+ }
119
+ attachNode(t) {
120
+ const e = this.di.graphStore.getNode(t), o = document.createElement("div");
121
+ o.appendChild(e.element), o.style.position = "absolute", o.style.top = "0", o.style.left = "0", o.style.zIndex = `${this.currentZIndex}`, this.currentZIndex += 1, o.style.visibility = "hidden", this.nodesContainer.appendChild(o), this.nodeElementToIdMap.set(e.element, t), this.nodeWrapperElementToIdMap.set(o, t), this.nodeIdToWrapperElementMap.set(t, o), this.updateNodeCoords(t, e.x, e.y), this.nodesResizeObserver.observe(o), o.style.visibility = "visible";
122
+ }
123
+ detachNode(t) {
124
+ const e = this.di.graphStore.getNode(t);
125
+ this.nodesResizeObserver.unobserve(e.element), this.nodesContainer.removeChild(e.element);
126
+ const o = this.nodeIdToWrapperElementMap.get(t);
127
+ o.removeChild(e.element), this.nodeElementToIdMap.delete(e.element), this.nodeWrapperElementToIdMap.delete(o), this.nodeIdToWrapperElementMap.delete(t);
128
+ }
129
+ attachConnection(t) {
130
+ const o = this.di.graphStore.getConnection(t).controller.svg;
131
+ o.style.transformOrigin = "50% 50%", o.style.position = "absolute", o.style.top = "0", o.style.left = "0", o.style.zIndex = `${this.currentZIndex}`, this.currentZIndex += 1, this.connectionIdToElementMap.set(t, o), this.updateConnectionCoords(t), this.connectionsContainer.appendChild(o);
132
+ }
133
+ detachConnection(t) {
134
+ const e = this.connectionIdToElementMap.get(t);
135
+ this.connectionIdToElementMap.delete(t), this.connectionsContainer.removeChild(e);
136
+ }
137
+ moveNodeOnTop(t) {
138
+ this.layers[this.di.options.layers.mode].moveOnTop(t);
139
+ }
140
+ updateNodePosition(t) {
141
+ const e = this.di.graphStore.getNode(t), o = this.di.graphStore.getNodeAdjacentConnections(t);
142
+ this.updateNodeCoords(t, e.x, e.y), o.forEach((s) => {
143
+ this.updateConnectionCoords(s);
144
+ });
145
+ }
146
+ updatePortConnections(t) {
147
+ this.di.graphStore.getPortAdjacentConnections(t).forEach((o) => {
148
+ this.updateConnectionCoords(o);
149
+ });
150
+ }
151
+ getViewportDimensions() {
152
+ const t = this.host.getBoundingClientRect();
153
+ return [t.width, t.height];
154
+ }
155
+ createHost() {
156
+ const t = document.createElement("div");
157
+ return t.style.width = "100%", t.style.height = "100%", t.style.position = "relative", t.style.overflow = "hidden", t;
158
+ }
159
+ createCanvas() {
160
+ const t = document.createElement("canvas");
161
+ return t.style.position = "absolute", t.style.inset = "0", t;
162
+ }
163
+ createNodesContainer() {
164
+ const t = document.createElement("div");
165
+ return t.style.position = "absolute", t.style.top = "0", t.style.left = "0", t.style.width = "0", t.style.height = "0", t;
166
+ }
167
+ createConnectionsContainer() {
168
+ const t = document.createElement("div");
169
+ return t.style.position = "absolute", t.style.pointerEvents = "none", t.style.top = "0", t.style.left = "0", t.style.width = "0", t.style.height = "0", t;
170
+ }
171
+ createHostResizeObserver() {
172
+ return new ResizeObserver(() => {
173
+ this.updateCanvasDimensions(), this.applyTransform();
174
+ });
175
+ }
176
+ createNodesResizeObserver() {
177
+ return new ResizeObserver((t) => {
178
+ t.forEach((e) => {
179
+ const o = e.target, s = this.nodeWrapperElementToIdMap.get(o), i = this.di.graphStore.getNode(s);
180
+ this.updateNodeCoords(s, i.x, i.y), this.di.graphStore.getNodeAdjacentConnections(s).forEach((a) => {
181
+ this.updateConnectionCoords(a);
182
+ });
183
+ });
184
+ });
185
+ }
186
+ updateCanvasDimensions() {
187
+ const { width: t, height: e } = this.host.getBoundingClientRect();
188
+ this.canvas.width = t, this.canvas.height = e;
189
+ }
190
+ updateNodeCoords(t, e, o) {
191
+ const s = this.nodeIdToWrapperElementMap.get(t), { width: i, height: c } = s.getBoundingClientRect(), a = this.di.viewportTransformer.getAbsScale(), h = this.di.graphStore.getNode(t), [l, m] = h.centerFn(i, c);
192
+ s.style.transform = `matrix(1, 0, 0, 1, ${e - a * l}, ${o - a * m})`;
193
+ }
194
+ updateConnectionCoords(t) {
195
+ const e = this.di.graphStore.getConnection(t), o = this.di.graphStore.getPort(e.from), s = this.di.graphStore.getPort(e.to), i = o.element.getBoundingClientRect(), c = s.element.getBoundingClientRect(), a = this.host.getBoundingClientRect(), [h, l] = this.di.viewportTransformer.getAbsCoords(
196
+ i.left - a.left,
197
+ i.top - a.top
198
+ ), [m, d] = this.di.viewportTransformer.getAbsCoords(
199
+ c.left - a.left,
200
+ c.top - a.top
201
+ ), g = this.di.viewportTransformer.getAbsScale(), [u, p] = o.centerFn(
202
+ i.width * g,
203
+ i.height * g
204
+ ), [f, y] = s.centerFn(
205
+ c.width * g,
206
+ c.height * g
207
+ ), b = u + h, S = p + l, N = f + m, C = y + d, w = Math.min(b, N), T = Math.min(S, C), E = Math.abs(N - b), O = Math.abs(C - S), x = this.connectionIdToElementMap.get(t), M = b <= N, L = S <= C;
208
+ x.style.transform = `matrix(${M ? 1 : -1}, 0, 0, ${L ? 1 : -1}, ${w}, ${T})`, e.controller.update(w, T, E, O, o, s);
209
+ }
210
+ }
211
+ class F {
212
+ constructor() {
213
+ r(this, "state", {
214
+ scale: 1,
215
+ x: 0,
216
+ y: 0
217
+ });
218
+ }
219
+ /**
220
+ * dx2 - traslate x
221
+ * dy2 - traslate y
222
+ *
223
+ * direct transform
224
+ * s1 0 dx1 1 0 dx2
225
+ * 0 s1 dy1 0 1 dy2
226
+ * 0 0 1 0 0 1
227
+ *
228
+ * [s, dx, dy] = [s1, s * dx2 + dx1, s * dy2 + dy1]
229
+ */
230
+ applyShift(t, e) {
231
+ this.state = {
232
+ scale: this.state.scale,
233
+ x: this.state.scale * t + this.state.x,
234
+ y: this.state.scale * e + this.state.y
235
+ };
236
+ }
237
+ /**
238
+ * s2 - scale
239
+ * cx - scale pivot x
240
+ * cy - scale pivot y
241
+ *
242
+ * s1 0 dx1 s2 0 (1 - s2) * cx
243
+ * 0 s1 dy1 0 s2 (1 - s2) * cy
244
+ * 0 0 1 0 0 1
245
+ *
246
+ * [s, dx, dy] = [s1 * s2, s1 * (1 - s2) * cx + dx1, s1 * (1 - s2) * cy + dy1]
247
+ */
248
+ applyScale(t, e, o) {
249
+ this.state = {
250
+ scale: this.state.scale * t,
251
+ x: this.state.scale * (1 - t) * e + this.state.x,
252
+ y: this.state.scale * (1 - t) * o + this.state.y
253
+ };
254
+ }
255
+ getViewCoords(t, e) {
256
+ return [
257
+ (t - this.state.x) / this.state.scale,
258
+ (e - this.state.y) / this.state.scale
259
+ ];
260
+ }
261
+ getViewScale() {
262
+ return 1 / this.state.scale;
263
+ }
264
+ getAbsCoords(t, e) {
265
+ return [
266
+ t * this.state.scale + this.state.x,
267
+ e * this.state.scale + this.state.y
268
+ ];
269
+ }
270
+ getAbsScale() {
271
+ return this.state.scale;
272
+ }
273
+ patchState(t, e, o) {
274
+ this.state = {
275
+ scale: t ?? this.state.scale,
276
+ x: e ?? this.state.x,
277
+ y: o ?? this.state.y
278
+ };
279
+ }
280
+ }
281
+ class W {
282
+ constructor(t) {
283
+ this.transformer = t;
284
+ }
285
+ /**
286
+ * for given absolute coordinates returns viewport coordinates
287
+ * viewport coordinated represent actual coordinates on screen for given absolute coordinates
288
+ */
289
+ getViewCoords(t, e) {
290
+ return this.transformer.getViewCoords(t, e);
291
+ }
292
+ /**
293
+ * returns viewport scale
294
+ * it represents scale at which entities on canvas should be scaled to fit current scale
295
+ */
296
+ getViewScale() {
297
+ return this.transformer.getViewScale();
298
+ }
299
+ /**
300
+ * for given viewport coordinates returns absolute coordinates
301
+ * absolute coordinates represent actual coordinates, which stay constant even for transformed canvas
302
+ */
303
+ getAbsCoords(t, e) {
304
+ return this.transformer.getAbsCoords(t, e);
305
+ }
306
+ /**
307
+ * returns absolute scale
308
+ * it represents scale at which current viewport was scaled compared to initial state
309
+ */
310
+ getAbsScale() {
311
+ return this.transformer.getAbsScale();
312
+ }
313
+ }
314
+ class v {
315
+ static getPortCenter(t) {
316
+ const { top: e, left: o, width: s, height: i } = t.element.getBoundingClientRect(), c = t.centerFn(s, i);
317
+ return [o + c[0], e + c[1]];
318
+ }
319
+ static rotate(t, e, o) {
320
+ return [
321
+ e[0] * t[0] - e[1] * t[1] + ((1 - e[0]) * o[0] + e[1] * o[1]),
322
+ e[1] * t[0] + e[0] * t[1] + ((1 - e[0]) * o[1] - e[1] * o[0])
323
+ ];
324
+ }
325
+ static getDirectionVector(t, e, o) {
326
+ return [e * Math.cos(t ?? 0), o * Math.sin(t ?? 0)];
327
+ }
328
+ static getArrowPath(t, e, o, s, i) {
329
+ const a = [
330
+ [0, 0],
331
+ [s, i],
332
+ [s, -i]
333
+ ].map((d) => this.rotate(d, t, [0, 0])).map((d) => [d[0] + e, d[1] + o]), h = `M ${a[0][0]} ${a[0][1]}`, l = `L ${a[1][0]} ${a[1][1]}`, m = `L ${a[2][0]} ${a[2][1]}`;
334
+ return `${h} ${l} ${m}`;
335
+ }
336
+ static getArrowOffsetPath(t, e, o, s, i) {
337
+ const a = [
338
+ [s, 0],
339
+ [s + i, 0]
340
+ ].map((h) => this.rotate(h, t, [0, 0])).map((h) => [h[0] + e, h[1] + o]);
341
+ return `M ${a[0][0]} ${a[0][1]} L ${a[1][0]} ${a[1][1]}`;
342
+ }
343
+ }
344
+ class D {
345
+ constructor(t, e, o, s, i, c, a) {
346
+ r(this, "svg");
347
+ r(this, "group");
348
+ r(this, "line");
349
+ r(this, "sourceArrow", null);
350
+ r(this, "targetArrow", null);
351
+ this.color = t, this.width = e, this.curvature = o, this.arrowLength = s, this.arrowWidth = i, this.hasSourceArrow = c, this.hasTargetArrow = a, this.svg = document.createElementNS("http://www.w3.org/2000/svg", "svg"), this.svg.style.pointerEvents = "none", this.group = document.createElementNS("http://www.w3.org/2000/svg", "g"), this.svg.appendChild(this.group), this.line = document.createElementNS("http://www.w3.org/2000/svg", "path"), this.line.setAttribute("stroke", this.color), this.line.setAttribute("stroke-width", `${this.width}`), this.line.setAttribute("fill", "none"), this.group.appendChild(this.line), this.group.style.transformOrigin = "50% 50%", this.hasSourceArrow && (this.sourceArrow = document.createElementNS(
352
+ "http://www.w3.org/2000/svg",
353
+ "path"
354
+ ), this.sourceArrow.setAttribute("fill", this.color), this.group.appendChild(this.sourceArrow)), this.hasTargetArrow && (this.targetArrow = document.createElementNS(
355
+ "http://www.w3.org/2000/svg",
356
+ "path"
357
+ ), this.targetArrow.setAttribute("fill", this.color), this.group.appendChild(this.targetArrow)), this.svg.style.overflow = "visible";
358
+ }
359
+ update(t, e, o, s, i, c) {
360
+ this.svg.style.width = `${o}px`, this.svg.style.height = `${s}px`;
361
+ const a = v.getPortCenter(i), h = v.getPortCenter(c), l = a[0] <= h[0] ? 1 : -1, m = a[1] <= h[1] ? 1 : -1;
362
+ this.svg.style.transform = `translate(${t}px, ${e}px)`, this.group.style.transform = `scale(${l}, ${m})`;
363
+ const d = v.getDirectionVector(
364
+ i.direction,
365
+ l,
366
+ m
367
+ ), g = v.getDirectionVector(
368
+ c.direction,
369
+ l,
370
+ m
371
+ ), u = v.rotate(
372
+ [this.arrowLength, 0],
373
+ d,
374
+ [0, 0]
375
+ ), p = v.rotate(
376
+ [o - this.arrowLength, s],
377
+ g,
378
+ [o, s]
379
+ ), f = [
380
+ u[0] + d[0] * this.curvature,
381
+ u[1] + d[1] * this.curvature
382
+ ], y = [
383
+ p[0] - g[0] * this.curvature,
384
+ p[1] - g[1] * this.curvature
385
+ ], b = `M ${u[0]} ${u[1]}`, S = `C ${f[0]} ${f[1]}, ${y[0]} ${y[1]}, ${p[0]} ${p[1]}`, N = `M 0 0 L ${u[0]} ${u[1]} `, C = ` M ${p[0]} ${p[1]} L ${o} ${s}`, w = `${this.sourceArrow ? "" : N}${b} ${S}${this.targetArrow ? "" : C}`;
386
+ if (this.line.setAttribute("d", w), this.sourceArrow) {
387
+ const T = v.getArrowPath(
388
+ d,
389
+ 0,
390
+ 0,
391
+ this.arrowLength,
392
+ this.arrowWidth
393
+ );
394
+ this.sourceArrow.setAttribute("d", T);
395
+ }
396
+ if (this.targetArrow) {
397
+ const T = v.getArrowPath(
398
+ g,
399
+ o,
400
+ s,
401
+ -this.arrowLength,
402
+ this.arrowWidth
403
+ );
404
+ this.targetArrow.setAttribute("d", T);
405
+ }
406
+ }
407
+ }
408
+ const R = (n) => () => new D(
409
+ n.color ?? "#5c5c5c",
410
+ n.width ?? 1,
411
+ n.curvature ?? 90,
412
+ n.arrowLength ?? 15,
413
+ n.arrowWidth ?? 4,
414
+ n.hasSourceArrow ?? !1,
415
+ n.hasTargetArrow ?? !1
416
+ );
417
+ class z {
418
+ constructor(t, e, o, s, i, c, a) {
419
+ r(this, "svg");
420
+ r(this, "group");
421
+ r(this, "line");
422
+ r(this, "sourceArrow", null);
423
+ r(this, "targetArrow", null);
424
+ this.color = t, this.width = e, this.arrowLength = o, this.arrowWidth = s, this.minPortOffset = i, this.hasSourceArrow = c, this.hasTargetArrow = a, this.svg = document.createElementNS("http://www.w3.org/2000/svg", "svg"), this.svg.style.pointerEvents = "none", this.group = document.createElementNS("http://www.w3.org/2000/svg", "g"), this.svg.appendChild(this.group), this.line = document.createElementNS("http://www.w3.org/2000/svg", "path"), this.line.setAttribute("stroke", this.color), this.line.setAttribute("stroke-width", `${this.width}`), this.line.setAttribute("fill", "none"), this.group.appendChild(this.line), this.group.style.transformOrigin = "50% 50%", this.hasSourceArrow && (this.sourceArrow = document.createElementNS(
425
+ "http://www.w3.org/2000/svg",
426
+ "path"
427
+ ), this.sourceArrow.setAttribute("fill", this.color), this.group.appendChild(this.sourceArrow)), this.hasTargetArrow && (this.targetArrow = document.createElementNS(
428
+ "http://www.w3.org/2000/svg",
429
+ "path"
430
+ ), this.targetArrow.setAttribute("fill", this.color), this.group.appendChild(this.targetArrow)), this.svg.style.overflow = "visible";
431
+ }
432
+ update(t, e, o, s, i, c) {
433
+ this.svg.style.width = `${o}px`, this.svg.style.height = `${s}px`;
434
+ const a = v.getPortCenter(i), h = v.getPortCenter(c), l = a[0] <= h[0] ? 1 : -1, m = a[1] <= h[1] ? 1 : -1;
435
+ this.svg.style.transform = `translate(${t}px, ${e}px)`, this.group.style.transform = `scale(${l}, ${m})`;
436
+ const d = v.getDirectionVector(
437
+ i.direction,
438
+ l,
439
+ m
440
+ ), g = v.getDirectionVector(
441
+ c.direction,
442
+ l,
443
+ m
444
+ ), u = v.rotate(
445
+ [this.arrowLength + this.minPortOffset, 0],
446
+ d,
447
+ [0, 0]
448
+ ), p = v.rotate(
449
+ [o - this.arrowLength - this.minPortOffset, s],
450
+ g,
451
+ [o, s]
452
+ ), [f, y] = [o / 2, s / 2], b = l * (p[0] - u[0]) > 0 ? `M ${u[0]} ${u[1]} L ${f} ${u[1]} L ${f} ${p[1]} L ${p[0]} ${p[1]}` : `M ${u[0]} ${u[1]} L ${u[0]} ${y} L ${p[0]} ${y} L ${p[0]} ${p[1]}`, S = `M 0 0 L ${u[0]} ${u[1]} `, N = ` M ${p[0]} ${p[1]} L ${o} ${s}`, C = v.getArrowOffsetPath(
453
+ d,
454
+ 0,
455
+ 0,
456
+ this.arrowLength,
457
+ this.minPortOffset
458
+ ), w = v.getArrowOffsetPath(
459
+ g,
460
+ o,
461
+ s,
462
+ -this.arrowLength,
463
+ -this.minPortOffset
464
+ ), T = `${this.sourceArrow ? C : S}${b}${this.targetArrow ? w : N}`;
465
+ if (this.line.setAttribute("d", T), this.sourceArrow) {
466
+ const E = v.getArrowPath(
467
+ d,
468
+ 0,
469
+ 0,
470
+ this.arrowLength,
471
+ this.arrowWidth
472
+ );
473
+ this.sourceArrow.setAttribute("d", E);
474
+ }
475
+ if (this.targetArrow) {
476
+ const E = v.getArrowPath(
477
+ g,
478
+ o,
479
+ s,
480
+ -this.arrowLength,
481
+ this.arrowWidth
482
+ );
483
+ this.targetArrow.setAttribute("d", E);
484
+ }
485
+ }
486
+ }
487
+ const G = (n) => () => new z(
488
+ n.color,
489
+ n.width,
490
+ n.arrowLength,
491
+ n.arrowWidth,
492
+ n.minPortOffset,
493
+ n.hasSourceArrow,
494
+ n.hasTargetArrow
495
+ ), $ = (n) => {
496
+ switch (n == null ? void 0 : n.type) {
497
+ case "custom":
498
+ return n.controllerFactory;
499
+ case "straight":
500
+ return G({
501
+ color: n.color ?? "#5c5c5c",
502
+ width: n.width ?? 1,
503
+ arrowLength: n.arrowLength ?? 15,
504
+ arrowWidth: n.arrowWidth ?? 4,
505
+ minPortOffset: n.minPortOffset ?? 15,
506
+ hasSourceArrow: n.hasSourceArrow ?? !1,
507
+ hasTargetArrow: n.hasTargetArrow ?? !1
508
+ });
509
+ default:
510
+ return R({
511
+ color: n.color ?? "#5c5c5c",
512
+ width: n.width ?? 1,
513
+ curvature: n.curvature ?? 90,
514
+ arrowLength: n.arrowLength ?? 15,
515
+ arrowWidth: n.arrowWidth ?? 4,
516
+ hasSourceArrow: n.hasSourceArrow ?? !1,
517
+ hasTargetArrow: n.hasTargetArrow ?? !1
518
+ });
519
+ }
520
+ };
521
+ class B {
522
+ constructor(t) {
523
+ this.di = t;
524
+ }
525
+ moveNodeOnTop(t) {
526
+ if (!this.di.graphStore.hasNode(t))
527
+ throw new Error("failed to move on top nonexisting node");
528
+ this.di.htmlController.moveNodeOnTop(t);
529
+ }
530
+ dragNode(t, e, o) {
531
+ if (!this.di.graphStore.hasNode(t))
532
+ throw new Error("failed to drag nonexisting node");
533
+ const s = this.di.graphStore.getNode(t), [i, c] = this.di.viewportTransformer.getViewCoords(s.x, s.y), a = i + e, h = c + o, [l, m] = this.di.viewportTransformer.getAbsCoords(a, h);
534
+ this.di.graphStore.updateNodeCoords(t, l, m), this.di.htmlController.updateNodePosition(t);
535
+ }
536
+ addNode(t, e, o, s, i, c) {
537
+ if (t === void 0)
538
+ do
539
+ t = this.di.nodeIdGenerator.next();
540
+ while (this.di.graphStore.hasNode(t));
541
+ if (this.di.graphStore.hasNode(t))
542
+ throw new Error("failed to add node with existing id");
543
+ this.di.graphStore.addNode(
544
+ t,
545
+ e,
546
+ o,
547
+ s,
548
+ c ?? this.di.options.nodes.centerFn
549
+ ), this.di.htmlController.attachNode(t), i !== void 0 && Object.entries(i).forEach(([a, h]) => {
550
+ h instanceof HTMLElement ? this.di.controller.markPort(
551
+ a,
552
+ h,
553
+ t,
554
+ this.di.options.ports.centerFn,
555
+ null
556
+ ) : this.di.controller.markPort(
557
+ a,
558
+ h.element,
559
+ t,
560
+ h.centerFn ?? this.di.options.ports.centerFn,
561
+ h.direction ?? null
562
+ );
563
+ });
564
+ }
565
+ markPort(t, e, o, s, i) {
566
+ if (t === void 0)
567
+ do
568
+ t = this.di.portIdGenerator.next();
569
+ while (this.di.graphStore.hasPort(t));
570
+ if (!this.di.graphStore.hasNode(o))
571
+ throw new Error("failed to set port on nonexisting node");
572
+ if (this.di.graphStore.hasPort(t))
573
+ throw new Error("failed to add port with existing id");
574
+ this.di.graphStore.addPort(
575
+ t,
576
+ e,
577
+ o,
578
+ s ?? this.di.options.ports.centerFn,
579
+ i ?? null
580
+ );
581
+ }
582
+ updatePortConnections(t) {
583
+ if (!this.di.graphStore.hasPort(t))
584
+ throw new Error("failed to unset nonexisting port");
585
+ this.di.htmlController.updatePortConnections(t);
586
+ }
587
+ unmarkPort(t) {
588
+ if (!this.di.graphStore.hasPort(t))
589
+ throw new Error("failed to unset nonexisting port");
590
+ this.di.graphStore.getPortAdjacentConnections(t).forEach((e) => {
591
+ this.removeConnection(e);
592
+ }), this.di.graphStore.removePort(t);
593
+ }
594
+ addConnection(t, e, o, s) {
595
+ if (t === void 0)
596
+ do
597
+ t = this.di.connectionIdGenerator.next();
598
+ while (this.di.graphStore.hasConnection(t));
599
+ if (!this.di.graphStore.hasPort(e))
600
+ throw new Error("failed to add connection from nonexisting port");
601
+ if (!this.di.graphStore.hasPort(o))
602
+ throw new Error("failed to add connection to nonexisting port");
603
+ const i = s !== void 0 ? $(s) : this.di.options.connections.controllerFactory;
604
+ this.di.graphStore.addConnection(
605
+ t,
606
+ e,
607
+ o,
608
+ i()
609
+ ), this.di.htmlController.attachConnection(t);
610
+ }
611
+ removeConnection(t) {
612
+ if (!this.di.graphStore.hasConnection(t))
613
+ throw new Error("failed to remove nonexisting connection");
614
+ this.di.htmlController.detachConnection(t), this.di.graphStore.removeConnection(t);
615
+ }
616
+ removeNode(t) {
617
+ if (!this.di.graphStore.hasNode(t))
618
+ throw new Error("failed to remove nonexisting node");
619
+ this.di.htmlController.detachNode(t), this.di.graphStore.removeNode(t);
620
+ }
621
+ patchViewportTransform(t, e, o) {
622
+ this.di.viewportTransformer.patchState(t, e, o), this.di.htmlController.applyTransform();
623
+ }
624
+ moveContent(t, e) {
625
+ this.di.viewportTransformer.applyShift(-t, -e), this.di.htmlController.applyTransform();
626
+ }
627
+ scaleContent(t, e, o) {
628
+ this.di.viewportTransformer.applyScale(1 / t, e, o), this.di.htmlController.applyTransform();
629
+ }
630
+ moveToNodes(t) {
631
+ if (t.length === 0)
632
+ return;
633
+ const e = t.map((g) => this.di.graphStore.getNode(g)).filter((g) => g !== void 0);
634
+ if (e.length < t.length)
635
+ throw new Error("failed to move to nonexisting node");
636
+ const [o, s] = e.reduce(
637
+ (g, u) => [g[0] + u.x, g[1] + u.y],
638
+ [0, 0]
639
+ ), i = o / e.length, c = s / e.length, [a, h] = this.di.htmlController.getViewportDimensions(), l = this.di.viewportTransformer.getAbsScale(), m = i - l * a / 2, d = c - l * h / 2;
640
+ this.patchViewportTransform(null, m, d);
641
+ }
642
+ updateNodeCoords(t, e, o) {
643
+ if (!this.di.graphStore.hasNode(t))
644
+ throw new Error("failed to update coordinates of nonexisting node");
645
+ this.di.graphStore.updateNodeCoords(t, e, o), this.di.htmlController.updateNodePosition(t);
646
+ }
647
+ updateConnectionOptions(t, e) {
648
+ if (e.controller !== void 0) {
649
+ if (!this.di.graphStore.hasConnection(t))
650
+ throw new Error("failed to update nonexisting connection");
651
+ this.di.htmlController.detachConnection(t), this.di.graphStore.updateConnectionController(
652
+ t,
653
+ e.controller
654
+ ), this.di.htmlController.attachConnection(t);
655
+ }
656
+ }
657
+ clear() {
658
+ this.di.htmlController.clear(), this.di.graphStore.clear(), this.di.nodeIdGenerator.reset(), this.di.portIdGenerator.reset(), this.di.connectionIdGenerator.reset();
659
+ }
660
+ destroy() {
661
+ this.di.htmlController.destroy();
662
+ }
663
+ }
664
+ class Y {
665
+ constructor() {
666
+ r(this, "nodes", /* @__PURE__ */ Object.create(null));
667
+ r(this, "ports", /* @__PURE__ */ Object.create(null));
668
+ r(this, "nodePorts", /* @__PURE__ */ Object.create(null));
669
+ r(this, "portNodeId", /* @__PURE__ */ Object.create(null));
670
+ r(this, "connections", /* @__PURE__ */ Object.create(null));
671
+ r(this, "incommingConnections", /* @__PURE__ */ Object.create(null));
672
+ r(this, "outcommingConnections", /* @__PURE__ */ Object.create(null));
673
+ r(this, "cycleConnections", /* @__PURE__ */ Object.create(null));
674
+ }
675
+ getAllNodes() {
676
+ return Object.keys(this.nodes);
677
+ }
678
+ getAllPorts() {
679
+ return Object.keys(this.ports);
680
+ }
681
+ getAllConnections() {
682
+ return Object.keys(this.connections);
683
+ }
684
+ addNode(t, e, o, s, i) {
685
+ this.nodes[t] = { element: e, x: o, y: s, centerFn: i }, this.nodePorts[t] = /* @__PURE__ */ Object.create(null);
686
+ }
687
+ hasNode(t) {
688
+ return this.nodes[t] !== void 0;
689
+ }
690
+ getNode(t) {
691
+ return this.nodes[t];
692
+ }
693
+ updateNodeCoords(t, e, o) {
694
+ this.nodes[t].x = e, this.nodes[t].y = o;
695
+ }
696
+ updateConnectionController(t, e) {
697
+ this.connections[t].controller = e;
698
+ }
699
+ removeNode(t) {
700
+ this.getNodeAdjacentConnections(t).forEach((s) => {
701
+ this.removeConnection(s);
702
+ }), delete this.nodes[t];
703
+ const o = this.nodePorts[t];
704
+ Object.keys(o).forEach((s) => {
705
+ delete this.portNodeId[s];
706
+ }), delete this.nodePorts[t];
707
+ }
708
+ addPort(t, e, o, s, i) {
709
+ this.ports[t] = { element: e, centerFn: s, direction: i }, this.cycleConnections[t] = {}, this.incommingConnections[t] = {}, this.outcommingConnections[t] = {}, this.portNodeId[t] = o;
710
+ const c = this.nodePorts[o];
711
+ c !== void 0 && (c[t] = e);
712
+ }
713
+ getPort(t) {
714
+ return this.ports[t];
715
+ }
716
+ getPortNode(t) {
717
+ return this.portNodeId[t];
718
+ }
719
+ hasPort(t) {
720
+ return this.portNodeId[t] !== void 0;
721
+ }
722
+ removePort(t) {
723
+ Object.keys(this.cycleConnections[t]).forEach((s) => {
724
+ this.removeConnection(s);
725
+ }), delete this.cycleConnections[t], Object.keys(this.incommingConnections[t]).forEach((s) => {
726
+ this.removeConnection(s);
727
+ }), delete this.incommingConnections[t], Object.keys(this.outcommingConnections[t]).forEach((s) => {
728
+ this.removeConnection(s);
729
+ }), delete this.outcommingConnections[t];
730
+ const e = this.portNodeId[t];
731
+ delete this.portNodeId[t];
732
+ const o = this.nodePorts[e];
733
+ delete o[t], delete this.ports[t];
734
+ }
735
+ addConnection(t, e, o, s) {
736
+ this.connections[t] = {
737
+ from: e,
738
+ to: o,
739
+ controller: s
740
+ }, e !== o ? (this.outcommingConnections[e][t] = !0, this.incommingConnections[o][t] = !0) : this.cycleConnections[e][t] = !0;
741
+ }
742
+ getConnection(t) {
743
+ return this.connections[t];
744
+ }
745
+ hasConnection(t) {
746
+ return this.connections[t] !== void 0;
747
+ }
748
+ removeConnection(t) {
749
+ const e = this.connections[t], o = e.from, s = e.to;
750
+ delete this.cycleConnections[o][t], delete this.cycleConnections[s][t], delete this.incommingConnections[o][t], delete this.incommingConnections[s][t], delete this.outcommingConnections[o][t], delete this.outcommingConnections[s][t], delete this.connections[t];
751
+ }
752
+ getPortIncomingConnections(t) {
753
+ return Object.keys(this.incommingConnections[t] ?? {});
754
+ }
755
+ getPortOutcomingConnections(t) {
756
+ return Object.keys(this.outcommingConnections[t] ?? {});
757
+ }
758
+ getPortCycleConnections(t) {
759
+ return Object.keys(this.cycleConnections[t] ?? {});
760
+ }
761
+ getPortAdjacentConnections(t) {
762
+ return [
763
+ ...this.getPortIncomingConnections(t),
764
+ ...this.getPortOutcomingConnections(t),
765
+ ...this.getPortCycleConnections(t)
766
+ ];
767
+ }
768
+ getNodeIncomingConnections(t) {
769
+ const e = Object.keys(this.nodePorts[t]);
770
+ let o = [];
771
+ return e.forEach((s) => {
772
+ o = [...o, ...this.getPortIncomingConnections(s)];
773
+ }), o;
774
+ }
775
+ getNodeOutcomingConnections(t) {
776
+ const e = Object.keys(this.nodePorts[t]);
777
+ let o = [];
778
+ return e.forEach((s) => {
779
+ o = [...o, ...this.getPortOutcomingConnections(s)];
780
+ }), o;
781
+ }
782
+ getNodeCycleConnections(t) {
783
+ const e = Object.keys(this.nodePorts[t]);
784
+ let o = [];
785
+ return e.forEach((s) => {
786
+ o = [...o, ...this.getPortCycleConnections(s)];
787
+ }), o;
788
+ }
789
+ getNodeAdjacentConnections(t) {
790
+ return [
791
+ ...this.getNodeIncomingConnections(t),
792
+ ...this.getNodeOutcomingConnections(t),
793
+ ...this.getNodeCycleConnections(t)
794
+ ];
795
+ }
796
+ clear() {
797
+ this.nodes = /* @__PURE__ */ Object.create(null), this.ports = /* @__PURE__ */ Object.create(null), this.nodePorts = /* @__PURE__ */ Object.create(null), this.portNodeId = /* @__PURE__ */ Object.create(null), this.connections = /* @__PURE__ */ Object.create(null), this.incommingConnections = /* @__PURE__ */ Object.create(null), this.outcommingConnections = /* @__PURE__ */ Object.create(null), this.cycleConnections = /* @__PURE__ */ Object.create(null);
798
+ }
799
+ }
800
+ class A {
801
+ constructor() {
802
+ r(this, "counter", 0);
803
+ }
804
+ next() {
805
+ const t = `${this.counter}`;
806
+ return this.counter++, t;
807
+ }
808
+ reset() {
809
+ this.counter = 0;
810
+ }
811
+ }
812
+ class X {
813
+ constructor(t) {
814
+ this.graphStore = t;
815
+ }
816
+ getNode(t) {
817
+ const e = this.graphStore.getNode(t);
818
+ return e === void 0 ? null : { x: e.x, y: e.y };
819
+ }
820
+ getPort(t) {
821
+ const e = this.graphStore.getPortNode(t);
822
+ return e === void 0 ? null : { nodeId: e };
823
+ }
824
+ getConnection(t) {
825
+ const e = this.graphStore.getConnection(t);
826
+ return e === void 0 ? null : { from: e.from, to: e.to };
827
+ }
828
+ getAllNodes() {
829
+ return this.graphStore.getAllNodes();
830
+ }
831
+ getAllPorts() {
832
+ return this.graphStore.getAllPorts();
833
+ }
834
+ getAllConnections() {
835
+ return this.graphStore.getAllConnections();
836
+ }
837
+ hasNode(t) {
838
+ return this.graphStore.hasNode(t);
839
+ }
840
+ hasPort(t) {
841
+ return this.graphStore.hasPort(t);
842
+ }
843
+ hasConnection(t) {
844
+ return this.graphStore.hasConnection(t);
845
+ }
846
+ getPortIncomingConnections(t) {
847
+ return this.graphStore.getPortIncomingConnections(t);
848
+ }
849
+ getPortOutcomingConnections(t) {
850
+ return this.graphStore.getPortOutcomingConnections(t);
851
+ }
852
+ getPortCycleConnections(t) {
853
+ return this.graphStore.getPortCycleConnections(t);
854
+ }
855
+ getPortAdjacentConnections(t) {
856
+ return this.graphStore.getPortAdjacentConnections(t);
857
+ }
858
+ getNodeIncomingConnections(t) {
859
+ return this.graphStore.getNodeIncomingConnections(t);
860
+ }
861
+ getNodeOutcomingConnections(t) {
862
+ return this.graphStore.getNodeOutcomingConnections(t);
863
+ }
864
+ getNodeCycleConnections(t) {
865
+ return this.graphStore.getNodeCycleConnections(t);
866
+ }
867
+ getNodeAdjacentConnections(t) {
868
+ return this.graphStore.getNodeAdjacentConnections(t);
869
+ }
870
+ }
871
+ class Z {
872
+ constructor(t) {
873
+ r(this, "htmlController");
874
+ r(this, "viewportTransformer");
875
+ r(this, "publicViewportTransformer");
876
+ r(this, "controller");
877
+ r(this, "graphStore");
878
+ r(this, "publicGraphStore");
879
+ r(this, "nodeIdGenerator");
880
+ r(this, "portIdGenerator");
881
+ r(this, "connectionIdGenerator");
882
+ this.options = t, this.htmlController = new j(this), this.viewportTransformer = new F(), this.publicViewportTransformer = new W(
883
+ this.viewportTransformer
884
+ ), this.controller = new B(this), this.graphStore = new Y(), this.publicGraphStore = new X(this.graphStore), this.nodeIdGenerator = new A(), this.portIdGenerator = new A(), this.connectionIdGenerator = new A();
885
+ }
886
+ }
887
+ const P = (n, t) => [
888
+ n / 2,
889
+ t / 2
890
+ ], H = (n, t, e, o, s, i) => {
891
+ n.fillStyle = i, n.fillRect(0, 0, n.canvas.width, n.canvas.height);
892
+ const c = t.getViewCoords(0, 0), a = t.getViewScale(), h = o * a;
893
+ let l = 0, m = 0, d = h / 2;
894
+ do
895
+ d *= 2, l = n.canvas.width / d, m = n.canvas.height / d;
896
+ while (l * m > 1e4);
897
+ const g = c[0] - Math.floor(c[0] / d) * d, u = c[1] - Math.floor(c[1] / d) * d, p = s * a, f = 2 * Math.PI, y = g - d, b = u - d, S = n.canvas.width + g, N = n.canvas.height + u;
898
+ n.fillStyle = e;
899
+ for (let C = y; C <= S; C += d)
900
+ for (let w = b; w <= N; w += d)
901
+ n.beginPath(), n.arc(C, w, p, 0, f), n.closePath(), n.fill();
902
+ }, U = (n, t) => {
903
+ n.fillStyle = t, n.fillRect(0, 0, n.canvas.width, n.canvas.height);
904
+ }, q = (n, t, e, o) => (s, i) => {
905
+ H(
906
+ s,
907
+ i,
908
+ n,
909
+ t,
910
+ e,
911
+ o
912
+ );
913
+ }, J = () => () => {
914
+ }, K = (n) => (t) => {
915
+ U(t, n);
916
+ }, Q = (n) => {
917
+ switch (n == null ? void 0 : n.type) {
918
+ case "custom":
919
+ return n.drawingFn;
920
+ case "dots":
921
+ return q(
922
+ n.dotColor ?? "#d8d8d8",
923
+ n.dotGap ?? 25,
924
+ n.dotRadius ?? 1.5,
925
+ n.color ?? "#ffffff"
926
+ );
927
+ case "color":
928
+ return K(n.color ?? "#ffffff");
929
+ default:
930
+ return J();
931
+ }
932
+ }, _ = (n) => {
933
+ var t, e, o;
934
+ return {
935
+ background: {
936
+ drawingFn: Q(n.background)
937
+ },
938
+ nodes: {
939
+ centerFn: ((t = n.nodes) == null ? void 0 : t.centerFn) ?? P
940
+ },
941
+ ports: {
942
+ centerFn: ((e = n.ports) == null ? void 0 : e.centerFn) ?? P
943
+ },
944
+ connections: {
945
+ controllerFactory: $(
946
+ n.connections ?? {}
947
+ )
948
+ },
949
+ layers: {
950
+ mode: ((o = n.layers) == null ? void 0 : o.mode) ?? "connections-follow-node"
951
+ }
952
+ };
953
+ };
954
+ class I {
955
+ constructor(t) {
956
+ r(this, "transformation");
957
+ r(this, "model");
958
+ r(this, "options");
959
+ r(this, "di");
960
+ this.apiOptions = t, this.options = _(this.apiOptions ?? {}), this.di = new Z(this.options), this.transformation = this.di.publicViewportTransformer, this.model = this.di.publicGraphStore;
961
+ }
962
+ addNode(t) {
963
+ return this.di.controller.addNode(
964
+ t.id,
965
+ t.element,
966
+ t.x,
967
+ t.y,
968
+ t.ports,
969
+ t.centerFn
970
+ ), this;
971
+ }
972
+ moveNodeOnTop(t) {
973
+ return this.di.controller.moveNodeOnTop(t), this;
974
+ }
975
+ removeNode(t) {
976
+ return this.di.controller.removeNode(t), this;
977
+ }
978
+ markPort(t) {
979
+ return this.di.controller.markPort(
980
+ t.id,
981
+ t.element,
982
+ t.nodeId,
983
+ t.centerFn,
984
+ t.direction
985
+ ), this;
986
+ }
987
+ updatePortConnections(t) {
988
+ return this.di.controller.updatePortConnections(t), this;
989
+ }
990
+ unmarkPort(t) {
991
+ return this.di.controller.unmarkPort(t), this;
992
+ }
993
+ addConnection(t) {
994
+ return this.di.controller.addConnection(
995
+ t.id,
996
+ t.from,
997
+ t.to,
998
+ t.options
999
+ ), this;
1000
+ }
1001
+ removeConnection(t) {
1002
+ return this.di.controller.removeConnection(t), this;
1003
+ }
1004
+ patchViewportTransform(t) {
1005
+ return this.di.controller.patchViewportTransform(
1006
+ t.scale ?? null,
1007
+ t.x ?? null,
1008
+ t.y ?? null
1009
+ ), this;
1010
+ }
1011
+ moveContent(t) {
1012
+ return this.di.controller.moveContent(t.x ?? 0, t.y ?? 0), this;
1013
+ }
1014
+ scaleContent(t) {
1015
+ return this.di.controller.scaleContent(
1016
+ t.scale,
1017
+ t.x ?? 0,
1018
+ t.y ?? 0
1019
+ ), this;
1020
+ }
1021
+ moveToNodes(t) {
1022
+ return this.di.controller.moveToNodes(t), this;
1023
+ }
1024
+ updateNodeCoords(t, e, o) {
1025
+ return this.di.controller.updateNodeCoords(t, e, o), this;
1026
+ }
1027
+ updateConnectionOptions(t, e) {
1028
+ return this.di.controller.updateConnectionOptions(t, e), this;
1029
+ }
1030
+ dragNode(t, e, o) {
1031
+ return this.di.controller.dragNode(t, e, o), this;
1032
+ }
1033
+ clear() {
1034
+ return this.di.controller.clear(), this;
1035
+ }
1036
+ attach(t) {
1037
+ return this.di.htmlController.attach(t), this;
1038
+ }
1039
+ detach() {
1040
+ return this.di.htmlController.detach(), this;
1041
+ }
1042
+ destroy() {
1043
+ this.di.controller.destroy();
1044
+ }
1045
+ }
1046
+ class tt {
1047
+ constructor(t) {
1048
+ r(this, "transformation");
1049
+ r(this, "model");
1050
+ r(this, "nodes", /* @__PURE__ */ new Map());
1051
+ r(this, "grabbedNodeId", null);
1052
+ r(this, "nodeIdGenerator", new A());
1053
+ r(this, "element", null);
1054
+ r(this, "onCanvasMouseUp", () => {
1055
+ this.setCursor(null), this.grabbedNodeId = null;
1056
+ });
1057
+ r(this, "onCanvasMouseMove", (t) => {
1058
+ this.grabbedNodeId !== null && (t.stopPropagation(), this.canvas.dragNode(
1059
+ this.grabbedNodeId,
1060
+ t.movementX,
1061
+ t.movementY
1062
+ ));
1063
+ });
1064
+ r(this, "onCanvasTouchStart", (t) => {
1065
+ this.previousTouchCoords = [
1066
+ t.touches[0].clientX,
1067
+ t.touches[0].clientY
1068
+ ];
1069
+ });
1070
+ r(this, "onCanvasTouchMove", (t) => {
1071
+ if (this.grabbedNodeId === null || t.touches.length !== 1 || this.previousTouchCoords === null)
1072
+ return;
1073
+ t.stopImmediatePropagation();
1074
+ const [e, o] = [
1075
+ t.touches[0].clientX - this.previousTouchCoords[0],
1076
+ t.touches[0].clientY - this.previousTouchCoords[1]
1077
+ ];
1078
+ this.canvas.dragNode(this.grabbedNodeId, e, o), this.previousTouchCoords = [
1079
+ t.touches[0].clientX,
1080
+ t.touches[0].clientY
1081
+ ];
1082
+ });
1083
+ r(this, "onCanvasTouchEnd", () => {
1084
+ this.previousTouchCoords = null, this.grabbedNodeId = null;
1085
+ });
1086
+ r(this, "previousTouchCoords", null);
1087
+ this.canvas = t, this.transformation = this.canvas.transformation, this.model = this.canvas.model;
1088
+ }
1089
+ addNode(t) {
1090
+ let e = t.id;
1091
+ if (e === void 0)
1092
+ do
1093
+ e = this.nodeIdGenerator.next();
1094
+ while (this.nodes.has(e));
1095
+ this.canvas.addNode(t);
1096
+ const o = (i) => {
1097
+ i.stopImmediatePropagation(), this.grabbedNodeId = e, this.setCursor("grab"), this.canvas.moveNodeOnTop(e);
1098
+ }, s = (i) => {
1099
+ i.touches.length === 1 && (this.grabbedNodeId = e, this.canvas.moveNodeOnTop(e));
1100
+ };
1101
+ return this.nodes.set(e, {
1102
+ el: t.element,
1103
+ onMouseDown: o,
1104
+ onTouchStart: s
1105
+ }), t.element.addEventListener("mousedown", o), t.element.addEventListener("touchstart", s), this;
1106
+ }
1107
+ removeNode(t) {
1108
+ const e = this.nodes.get(t);
1109
+ return e !== void 0 && (e.el.removeEventListener("mousedown", e.onMouseDown), e.el.removeEventListener("touchstart", e.onTouchStart)), this.canvas.removeNode(t), this.nodes.delete(t), this;
1110
+ }
1111
+ markPort(t) {
1112
+ return this.canvas.markPort(t), this;
1113
+ }
1114
+ updatePortConnections(t) {
1115
+ return this.canvas.updatePortConnections(t), this;
1116
+ }
1117
+ unmarkPort(t) {
1118
+ return this.canvas.unmarkPort(t), this;
1119
+ }
1120
+ addConnection(t) {
1121
+ return this.canvas.addConnection(t), this;
1122
+ }
1123
+ removeConnection(t) {
1124
+ return this.canvas.removeConnection(t), this;
1125
+ }
1126
+ patchViewportTransform(t) {
1127
+ return this.canvas.patchViewportTransform(t), this;
1128
+ }
1129
+ moveContent(t) {
1130
+ return this.canvas.moveContent(t), this;
1131
+ }
1132
+ scaleContent(t) {
1133
+ return this.canvas.scaleContent(t), this;
1134
+ }
1135
+ moveToNodes(t) {
1136
+ return this.canvas.moveToNodes(t), this;
1137
+ }
1138
+ updateNodeCoords(t, e, o) {
1139
+ return this.canvas.updateNodeCoords(t, e, o), this;
1140
+ }
1141
+ updateConnectionOptions(t, e) {
1142
+ return this.canvas.updateConnectionOptions(t, e), this;
1143
+ }
1144
+ dragNode(t, e, o) {
1145
+ return this.canvas.dragNode(t, e, o), this;
1146
+ }
1147
+ moveNodeOnTop(t) {
1148
+ return this.canvas.moveNodeOnTop(t), this;
1149
+ }
1150
+ clear() {
1151
+ return this.canvas.clear(), this.nodes.forEach((t) => {
1152
+ t.el.removeEventListener("mousedown", t.onMouseDown), t.el.removeEventListener("touchstart", t.onTouchStart);
1153
+ }), this.nodes.clear(), this;
1154
+ }
1155
+ attach(t) {
1156
+ return this.canvas.attach(t), this.element = t, t.addEventListener("mouseup", this.onCanvasMouseUp), t.addEventListener("mousemove", this.onCanvasMouseMove), t.addEventListener("touchstart", this.onCanvasTouchStart), t.addEventListener("touchmove", this.onCanvasTouchMove), t.addEventListener("touchend", this.onCanvasTouchEnd), t.addEventListener("touchcancel", this.onCanvasTouchEnd), this;
1157
+ }
1158
+ detach() {
1159
+ return this.canvas.detach(), this.element !== null && (this.element.removeEventListener("mouseup", this.onCanvasMouseUp), this.element.removeEventListener("mousemove", this.onCanvasMouseMove), this.element.removeEventListener("touchstart", this.onCanvasTouchStart), this.element.removeEventListener("touchmove", this.onCanvasTouchMove), this.element.removeEventListener("touchend", this.onCanvasTouchEnd), this.element.removeEventListener("touchcancel", this.onCanvasTouchEnd), this.element = null), this;
1160
+ }
1161
+ destroy() {
1162
+ this.detach(), this.nodes.forEach((t) => {
1163
+ t.el.removeEventListener("mousedown", t.onMouseDown), t.el.removeEventListener("touchstart", t.onTouchStart);
1164
+ }), this.canvas.destroy();
1165
+ }
1166
+ setCursor(t) {
1167
+ this.element !== null && (t !== null ? this.element.style.cursor = t : this.element.style.removeProperty("cursor"));
1168
+ }
1169
+ }
1170
+ class et {
1171
+ constructor(t, e) {
1172
+ r(this, "transformation");
1173
+ r(this, "model");
1174
+ r(this, "element", null);
1175
+ r(this, "isMoving", !1);
1176
+ r(this, "prevTouches", null);
1177
+ r(this, "isScalable");
1178
+ r(this, "isShiftable");
1179
+ r(this, "minContentScale");
1180
+ r(this, "maxContentScale");
1181
+ r(this, "wheelSensitivity");
1182
+ r(this, "onMouseDown", () => {
1183
+ this.setCursor("grab"), this.isMoving = !0;
1184
+ });
1185
+ r(this, "onMouseMove", (t) => {
1186
+ !this.isMoving || !this.isShiftable || this.canvas.moveContent({ x: t.movementX, y: t.movementY });
1187
+ });
1188
+ r(this, "onMouseUp", () => {
1189
+ this.setCursor(null), this.isMoving = !1;
1190
+ });
1191
+ r(this, "onWheelScroll", (t) => {
1192
+ if (this.element === null || this.isScalable === !1)
1193
+ return;
1194
+ t.preventDefault();
1195
+ const { left: e, top: o } = this.element.getBoundingClientRect(), s = t.clientX - e, i = t.clientY - o, c = t.deltaY < 0 ? this.wheelSensitivity : 1 / this.wheelSensitivity, a = this.canvas.transformation.getViewScale() * c;
1196
+ this.checkNextScaleValid(a) && this.canvas.scaleContent({ scale: c, x: s, y: i });
1197
+ });
1198
+ r(this, "onTouchStart", (t) => {
1199
+ this.prevTouches = this.getAverageTouch(t);
1200
+ });
1201
+ r(this, "onTouchMove", (t) => {
1202
+ if (this.prevTouches === null || this.element === null || !this.isShiftable)
1203
+ return;
1204
+ const e = this.getAverageTouch(t);
1205
+ if ((e.touchesCnt === 1 || e.touchesCnt === 2) && this.canvas.moveContent({
1206
+ x: e.x - this.prevTouches.x,
1207
+ y: e.y - this.prevTouches.y
1208
+ }), e.touchesCnt === 2 && this.isScalable) {
1209
+ const { left: o, top: s } = this.element.getBoundingClientRect(), i = this.prevTouches.x - o, c = this.prevTouches.y - s, a = e.scale / this.prevTouches.scale, h = this.canvas.transformation.getViewScale() * a;
1210
+ this.checkNextScaleValid(h) && this.canvas.scaleContent({ scale: a, x: i, y: c });
1211
+ }
1212
+ this.prevTouches = e, t.preventDefault();
1213
+ });
1214
+ r(this, "onTouchEnd", () => {
1215
+ this.prevTouches = null;
1216
+ });
1217
+ var s, i, c, a, h, l, m, d, g, u;
1218
+ this.canvas = t, this.options = e, this.transformation = this.canvas.transformation, this.model = this.canvas.model, this.isScalable = ((i = (s = this.options) == null ? void 0 : s.scale) == null ? void 0 : i.enabled) !== !1, this.minContentScale = ((a = (c = this.options) == null ? void 0 : c.scale) == null ? void 0 : a.minContent) ?? null, this.maxContentScale = ((l = (h = this.options) == null ? void 0 : h.scale) == null ? void 0 : l.maxContent) ?? null, this.isShiftable = ((d = (m = this.options) == null ? void 0 : m.shift) == null ? void 0 : d.enabled) !== !1;
1219
+ const o = (u = (g = this.options) == null ? void 0 : g.scale) == null ? void 0 : u.wheelSensitivity;
1220
+ this.wheelSensitivity = o !== void 0 ? o : 1.2;
1221
+ }
1222
+ addNode(t) {
1223
+ return this.canvas.addNode(t), this;
1224
+ }
1225
+ removeNode(t) {
1226
+ return this.canvas.removeNode(t), this;
1227
+ }
1228
+ markPort(t) {
1229
+ return this.canvas.markPort(t), this;
1230
+ }
1231
+ updatePortConnections(t) {
1232
+ return this.canvas.updatePortConnections(t), this;
1233
+ }
1234
+ unmarkPort(t) {
1235
+ return this.canvas.unmarkPort(t), this;
1236
+ }
1237
+ addConnection(t) {
1238
+ return this.canvas.addConnection(t), this;
1239
+ }
1240
+ removeConnection(t) {
1241
+ return this.canvas.removeConnection(t), this;
1242
+ }
1243
+ patchViewportTransform(t) {
1244
+ return this.canvas.patchViewportTransform(t), this;
1245
+ }
1246
+ moveContent(t) {
1247
+ return this.canvas.moveContent(t), this;
1248
+ }
1249
+ scaleContent(t) {
1250
+ return this.canvas.scaleContent(t), this;
1251
+ }
1252
+ moveToNodes(t) {
1253
+ return this.canvas.moveToNodes(t), this;
1254
+ }
1255
+ updateNodeCoords(t, e, o) {
1256
+ return this.canvas.updateNodeCoords(t, e, o), this;
1257
+ }
1258
+ updateConnectionOptions(t, e) {
1259
+ return this.canvas.updateConnectionOptions(t, e), this;
1260
+ }
1261
+ dragNode(t, e, o) {
1262
+ return this.canvas.dragNode(t, e, o), this;
1263
+ }
1264
+ moveNodeOnTop(t) {
1265
+ return this.canvas.moveNodeOnTop(t), this;
1266
+ }
1267
+ clear() {
1268
+ return this.canvas.clear(), this;
1269
+ }
1270
+ attach(t) {
1271
+ return this.canvas.attach(t), this.element = t, t.addEventListener("mousedown", this.onMouseDown), t.addEventListener("mousemove", this.onMouseMove), t.addEventListener("mouseup", this.onMouseUp), t.addEventListener("wheel", this.onWheelScroll), t.addEventListener("touchstart", this.onTouchStart), t.addEventListener("touchmove", this.onTouchMove), t.addEventListener("touchend", this.onTouchEnd), t.addEventListener("touchcancel", this.onTouchEnd), this;
1272
+ }
1273
+ detach() {
1274
+ return this.canvas.detach(), this.element !== null && (this.element.removeEventListener("mousedown", this.onMouseDown), this.element.removeEventListener("mousemove", this.onMouseMove), this.element.removeEventListener("mouseup", this.onMouseUp), this.element.removeEventListener("wheel", this.onWheelScroll), this.element.removeEventListener("touchstart", this.onTouchStart), this.element.removeEventListener("touchmove", this.onTouchMove), this.element.removeEventListener("touchend", this.onTouchEnd), this.element.removeEventListener("touchcancel", this.onTouchEnd)), this.element = null, this;
1275
+ }
1276
+ destroy() {
1277
+ this.detach(), this.canvas.destroy();
1278
+ }
1279
+ getAverageTouch(t) {
1280
+ const e = [], o = t.touches.length;
1281
+ for (let h = 0; h < o; h++)
1282
+ e.push([t.touches[h].clientX, t.touches[h].clientY]);
1283
+ const s = e.reduce(
1284
+ (h, l) => [h[0] + l[0], h[1] + l[1]],
1285
+ [0, 0]
1286
+ ), i = [s[0] / o, s[1] / o], a = e.map((h) => [h[0] - i[0], h[1] - i[1]]).reduce(
1287
+ (h, l) => h + Math.sqrt(l[0] * l[0] + l[1] * l[1]),
1288
+ 0
1289
+ );
1290
+ return { x: i[0], y: i[1], scale: a / o, touchesCnt: o };
1291
+ }
1292
+ checkNextScaleValid(t) {
1293
+ const e = this.canvas.transformation.getViewScale();
1294
+ return !(this.maxContentScale !== null && t > this.maxContentScale && t > e || this.minContentScale !== null && t < this.minContentScale && t < e);
1295
+ }
1296
+ setCursor(t) {
1297
+ this.element !== null && (t !== null ? this.element.style.cursor = t : this.element.style.removeProperty("cursor"));
1298
+ }
1299
+ }
1300
+ export {
1301
+ D as BezierConnectionController,
1302
+ nt as CanvasBuilder,
1303
+ I as CanvasCore,
1304
+ v as ConnectionUtils,
1305
+ tt as DraggableNodesCanvas,
1306
+ z as StraightConnectionController,
1307
+ et as TransformableCanvas
1308
+ };