f1ow 1.0.0 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -4
- package/dist/components/Canvas/ConnectionPoints.d.ts +2 -0
- package/dist/components/Canvas/TextHtmlOverlay.d.ts +12 -0
- package/dist/components/shapes/TextLabel.d.ts +1 -1
- package/dist/components/shapes/TextShape.d.ts +1 -0
- package/dist/f1ow-collaboration.js +2603 -0
- package/dist/f1ow.js +9787 -12922
- package/dist/f1ow.umd.cjs +122 -15860
- package/dist/hooks/useFlowAnimation.d.ts +8 -0
- package/dist/lib/FlowCanvasProps.d.ts +31 -1
- package/dist/lib/collaboration.d.ts +10 -0
- package/dist/lib/index.d.ts +7 -10
- package/dist/store/CanvasStoreContext.d.ts +14 -0
- package/dist/store/useCanvasStore.d.ts +32 -0
- package/dist/syncBridge-BBEfPdNb.js +254 -0
- package/dist/types/index.d.ts +97 -1
- package/dist/utils/connection.d.ts +30 -2
- package/dist/utils/editable.d.ts +2 -0
- package/dist/utils/elbow.d.ts +41 -8
- package/dist/utils/labelMetrics.d.ts +21 -0
- package/dist/utils/markdown.d.ts +14 -0
- package/dist/utils/markdownEditing.d.ts +1 -0
- package/dist/utils/textBinding.d.ts +18 -0
- package/dist/utils/textStyleTargets.d.ts +6 -0
- package/dist/yjsProvider-CXmqUJXr.js +72 -0
- package/package.json +121 -107
|
@@ -0,0 +1,2603 @@
|
|
|
1
|
+
var ve = Object.defineProperty;
|
|
2
|
+
var Ie = (t, e, n) => e in t ? ve(t, e, { enumerable: !0, configurable: !0, writable: !0, value: n }) : t[e] = n;
|
|
3
|
+
var A = (t, e, n) => Ie(t, typeof e != "symbol" ? e + "" : e, n);
|
|
4
|
+
import * as R from "yjs";
|
|
5
|
+
import { WebsocketProvider as re } from "y-websocket";
|
|
6
|
+
import { create as _e } from "zustand";
|
|
7
|
+
import Se, { useState as Tt, useRef as tt, useEffect as Ae, useCallback as ht, useMemo as Ee } from "react";
|
|
8
|
+
import { jsx as G, Fragment as ke, jsxs as Ot } from "react/jsx-runtime";
|
|
9
|
+
import { Group as Pt, Rect as Bt, Line as Te, Text as Oe } from "react-konva";
|
|
10
|
+
let D = null, E = null, St = null, pt = /* @__PURE__ */ new Set();
|
|
11
|
+
function Pe(t) {
|
|
12
|
+
return E && ie(), St = t, D = new R.Doc(), E = new re(
|
|
13
|
+
t.serverUrl,
|
|
14
|
+
t.roomName,
|
|
15
|
+
D,
|
|
16
|
+
{
|
|
17
|
+
connect: !0,
|
|
18
|
+
params: t.authToken ? { token: t.authToken } : void 0
|
|
19
|
+
}
|
|
20
|
+
), E.awareness.setLocalState({
|
|
21
|
+
user: t.user,
|
|
22
|
+
cursor: null,
|
|
23
|
+
selectedIds: []
|
|
24
|
+
}), E.on("status", (e) => {
|
|
25
|
+
const n = e.status;
|
|
26
|
+
for (const s of pt)
|
|
27
|
+
s(n);
|
|
28
|
+
}), { doc: D, provider: E };
|
|
29
|
+
}
|
|
30
|
+
function ie() {
|
|
31
|
+
E && (E.awareness.setLocalState(null), E.disconnect(), E.destroy(), E = null), D && (D.destroy(), D = null), St = null;
|
|
32
|
+
}
|
|
33
|
+
function oe() {
|
|
34
|
+
return D;
|
|
35
|
+
}
|
|
36
|
+
function Be() {
|
|
37
|
+
return E;
|
|
38
|
+
}
|
|
39
|
+
function ce() {
|
|
40
|
+
return D?.getMap("elements");
|
|
41
|
+
}
|
|
42
|
+
function Re() {
|
|
43
|
+
return St;
|
|
44
|
+
}
|
|
45
|
+
function Ce() {
|
|
46
|
+
return E !== null && E.wsconnected;
|
|
47
|
+
}
|
|
48
|
+
function Ye(t) {
|
|
49
|
+
return pt.add(t), () => {
|
|
50
|
+
pt.delete(t);
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
function Le(t) {
|
|
54
|
+
if (!E) return;
|
|
55
|
+
const e = E.awareness.getLocalState();
|
|
56
|
+
E.awareness.setLocalState({
|
|
57
|
+
...e,
|
|
58
|
+
...t
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
function Xe() {
|
|
62
|
+
if (!E) return /* @__PURE__ */ new Map();
|
|
63
|
+
const t = E.awareness.getStates(), e = E.awareness.clientID, n = /* @__PURE__ */ new Map();
|
|
64
|
+
for (const [s, r] of t)
|
|
65
|
+
s !== e && r && r.user && n.set(s, r);
|
|
66
|
+
return n;
|
|
67
|
+
}
|
|
68
|
+
const Me = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
69
|
+
__proto__: null,
|
|
70
|
+
createCollaborationProvider: Pe,
|
|
71
|
+
destroyCollaborationProvider: ie,
|
|
72
|
+
getCollaborationConfig: Re,
|
|
73
|
+
getRemoteAwareness: Xe,
|
|
74
|
+
getYDoc: oe,
|
|
75
|
+
getYElements: ce,
|
|
76
|
+
getYProvider: Be,
|
|
77
|
+
isCollaborationActive: Ce,
|
|
78
|
+
onStatusChange: Ye,
|
|
79
|
+
updateAwareness: Le
|
|
80
|
+
}, Symbol.toStringTag, { value: "Module" })), De = {
|
|
81
|
+
strokeColor: "#1e1e1e",
|
|
82
|
+
fillColor: "transparent",
|
|
83
|
+
strokeWidth: 2,
|
|
84
|
+
opacity: 1,
|
|
85
|
+
strokeStyle: "solid",
|
|
86
|
+
roughness: 0,
|
|
87
|
+
fontSize: 20,
|
|
88
|
+
fontFamily: "system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, sans-serif",
|
|
89
|
+
freehandStyle: "standard"
|
|
90
|
+
}, At = 0.1, Et = 5, ae = 20, le = 25, de = 36, J = 40, Rt = 1e4, mt = 24, Ne = 1.5;
|
|
91
|
+
function dt(t) {
|
|
92
|
+
switch (t) {
|
|
93
|
+
case "up":
|
|
94
|
+
return { x: 0, y: -1 };
|
|
95
|
+
case "down":
|
|
96
|
+
return { x: 0, y: 1 };
|
|
97
|
+
case "left":
|
|
98
|
+
return { x: -1, y: 0 };
|
|
99
|
+
case "right":
|
|
100
|
+
return { x: 1, y: 0 };
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
function q(t) {
|
|
104
|
+
return t === "up" || t === "down";
|
|
105
|
+
}
|
|
106
|
+
function fe(t) {
|
|
107
|
+
return t >= 0 ? "right" : "left";
|
|
108
|
+
}
|
|
109
|
+
function ue(t) {
|
|
110
|
+
return t >= 0 ? "down" : "up";
|
|
111
|
+
}
|
|
112
|
+
function Fe(t, e, n) {
|
|
113
|
+
return q(t) ? fe(e) : ue(n);
|
|
114
|
+
}
|
|
115
|
+
function Ct(t, e, n, s) {
|
|
116
|
+
const r = t.x + t.width / 2, i = t.y + t.height / 2, o = e.x - r, c = e.y - i, a = fe(o), l = ue(c);
|
|
117
|
+
if (n) {
|
|
118
|
+
const g = t.x + t.width, p = t.y + t.height, x = n.x + n.width, w = n.y + n.height, b = g < n.x ? n.x - g : x < t.x ? t.x - x : 0, I = p < n.y ? n.y - p : w < t.y ? t.y - w : 0;
|
|
119
|
+
if (!!(s?.isPrecise && !(s.fixedPoint[0] === 0.5 && s.fixedPoint[1] === 0.5)) && b > 0 && I > 0) {
|
|
120
|
+
const _ = xt(s.fixedPoint);
|
|
121
|
+
return _ === "left" || _ === "right" ? { primary: a } : { primary: l };
|
|
122
|
+
}
|
|
123
|
+
if (b > 0 && I === 0)
|
|
124
|
+
return { primary: a };
|
|
125
|
+
if (I > 0 && b === 0)
|
|
126
|
+
return { primary: l };
|
|
127
|
+
if (b > 0 && I > 0) {
|
|
128
|
+
const _ = b < I ? a : l, S = Fe(_, o, c);
|
|
129
|
+
return Math.abs(b - I) <= mt ? { primary: _, alternate: S } : { primary: _ };
|
|
130
|
+
}
|
|
131
|
+
const y = Math.max(0, Math.min(g, x) - Math.max(t.x, n.x)), v = Math.max(0, Math.min(p, w) - Math.max(t.y, n.y));
|
|
132
|
+
if (y > v)
|
|
133
|
+
return Math.abs(y - v) <= mt ? { primary: l, alternate: a } : { primary: l };
|
|
134
|
+
if (v > y)
|
|
135
|
+
return Math.abs(v - y) <= mt ? { primary: a, alternate: l } : { primary: a };
|
|
136
|
+
}
|
|
137
|
+
const f = (t.width || 1) / 2, u = (t.height || 1) / 2, d = Math.abs(o) / f, m = Math.abs(c) / u;
|
|
138
|
+
return d > m * 3 ? { primary: a } : m > d * 3 ? { primary: l } : Math.max(d, m) / Math.max(1e-6, Math.min(d, m)) <= Ne ? { primary: l, alternate: a } : { primary: l };
|
|
139
|
+
}
|
|
140
|
+
function xt(t) {
|
|
141
|
+
const [e, n] = t, s = n, r = 1 - n, i = e, o = 1 - e, c = Math.min(s, r, i, o);
|
|
142
|
+
return c === s ? "up" : c === r ? "down" : c === i ? "left" : "right";
|
|
143
|
+
}
|
|
144
|
+
function Yt(t, e) {
|
|
145
|
+
const n = e.x - t.x, s = e.y - t.y;
|
|
146
|
+
return Math.abs(n) >= Math.abs(s) ? n >= 0 ? "right" : "left" : s >= 0 ? "down" : "up";
|
|
147
|
+
}
|
|
148
|
+
function Lt(t, e) {
|
|
149
|
+
const n = t.x + t.width / 2, s = t.y + t.height / 2, r = e.x - n, i = e.y - s, o = t.width / 2 || 1, c = t.height / 2 || 1, a = r / o, l = i / c;
|
|
150
|
+
return Math.abs(a) >= Math.abs(l) ? r >= 0 ? "right" : "left" : i >= 0 ? "down" : "up";
|
|
151
|
+
}
|
|
152
|
+
function k(t) {
|
|
153
|
+
return t.left + t.width;
|
|
154
|
+
}
|
|
155
|
+
function T(t) {
|
|
156
|
+
return t.top + t.height;
|
|
157
|
+
}
|
|
158
|
+
function $e(t, e) {
|
|
159
|
+
return e.x > t.left && e.x < k(t) && e.y > t.top && e.y < T(t);
|
|
160
|
+
}
|
|
161
|
+
function L(t, e, n) {
|
|
162
|
+
return {
|
|
163
|
+
left: t.left - e,
|
|
164
|
+
top: t.top - n,
|
|
165
|
+
width: t.width + e * 2,
|
|
166
|
+
height: t.height + n * 2
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
function et(t, e, n) {
|
|
170
|
+
const s = Math.min(e, le), r = n === "left" ? s : e, i = n === "right" ? s : e, o = n === "up" ? s : e, c = n === "down" ? s : e;
|
|
171
|
+
return {
|
|
172
|
+
left: t.left - r,
|
|
173
|
+
top: t.top - o,
|
|
174
|
+
width: t.width + r + i,
|
|
175
|
+
height: t.height + o + c
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
function Xt(t, e, n) {
|
|
179
|
+
const s = new Set(n), r = Math.min(e, le), i = s.has("left") ? r : e, o = s.has("right") ? r : e, c = s.has("up") ? r : e, a = s.has("down") ? r : e;
|
|
180
|
+
return {
|
|
181
|
+
left: t.left - i,
|
|
182
|
+
top: t.top - c,
|
|
183
|
+
width: t.width + i + o,
|
|
184
|
+
height: t.height + c + a
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
function Ue(t, e) {
|
|
188
|
+
const n = Math.min(t.left, e.left), s = Math.min(t.top, e.top), r = Math.max(k(t), k(e)), i = Math.max(T(t), T(e));
|
|
189
|
+
return { left: n, top: s, width: r - n, height: i - s };
|
|
190
|
+
}
|
|
191
|
+
function K(t) {
|
|
192
|
+
return { left: t.x, top: t.y, width: t.width, height: t.height };
|
|
193
|
+
}
|
|
194
|
+
function He(t) {
|
|
195
|
+
const e = t.rotation || 0;
|
|
196
|
+
if (e === 0)
|
|
197
|
+
return { x: t.x, y: t.y, width: t.width, height: t.height };
|
|
198
|
+
const n = t.x + t.width / 2, s = t.y + t.height / 2, r = t.width / 2, i = t.height / 2, o = e * Math.PI / 180, c = Math.cos(o), a = Math.sin(o), l = [
|
|
199
|
+
{ x: -r, y: -i },
|
|
200
|
+
{ x: r, y: -i },
|
|
201
|
+
{ x: r, y: i },
|
|
202
|
+
{ x: -r, y: i }
|
|
203
|
+
].map((h) => ({
|
|
204
|
+
x: n + h.x * c - h.y * a,
|
|
205
|
+
y: s + h.x * a + h.y * c
|
|
206
|
+
}));
|
|
207
|
+
let f = 1 / 0, u = 1 / 0, d = -1 / 0, m = -1 / 0;
|
|
208
|
+
for (const h of l)
|
|
209
|
+
h.x < f && (f = h.x), h.y < u && (u = h.y), h.x > d && (d = h.x), h.y > m && (m = h.y);
|
|
210
|
+
return { x: f, y: u, width: d - f, height: m - u };
|
|
211
|
+
}
|
|
212
|
+
function Je(t) {
|
|
213
|
+
switch (t) {
|
|
214
|
+
case "left":
|
|
215
|
+
return "right";
|
|
216
|
+
case "right":
|
|
217
|
+
return "left";
|
|
218
|
+
case "up":
|
|
219
|
+
return "down";
|
|
220
|
+
case "down":
|
|
221
|
+
return "up";
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
function je(t, e) {
|
|
225
|
+
return e.x < t.x ? "left" : e.x > t.x ? "right" : e.y < t.y ? "up" : e.y > t.y ? "down" : null;
|
|
226
|
+
}
|
|
227
|
+
function We(t, e) {
|
|
228
|
+
return t === null || e === null ? !1 : (t === "left" || t === "right" ? "h" : "v") !== (e === "left" || e === "right" ? "h" : "v");
|
|
229
|
+
}
|
|
230
|
+
class ze {
|
|
231
|
+
constructor(e) {
|
|
232
|
+
A(this, "adjacent", /* @__PURE__ */ new Map());
|
|
233
|
+
this.pt = e;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
class Ge {
|
|
237
|
+
constructor() {
|
|
238
|
+
A(this, "idx", {});
|
|
239
|
+
}
|
|
240
|
+
add(e) {
|
|
241
|
+
const n = String(e.x), s = String(e.y);
|
|
242
|
+
n in this.idx || (this.idx[n] = {}), s in this.idx[n] || (this.idx[n][s] = new ze(e));
|
|
243
|
+
}
|
|
244
|
+
get(e) {
|
|
245
|
+
const n = String(e.x), s = String(e.y);
|
|
246
|
+
return this.idx[n]?.[s] ?? null;
|
|
247
|
+
}
|
|
248
|
+
has(e) {
|
|
249
|
+
return this.get(e) !== null;
|
|
250
|
+
}
|
|
251
|
+
/** Create a bidirectional edge between two points */
|
|
252
|
+
connect(e, n) {
|
|
253
|
+
const s = this.get(e), r = this.get(n);
|
|
254
|
+
if (!s || !r) return;
|
|
255
|
+
const i = Math.abs(n.x - e.x) + Math.abs(n.y - e.y);
|
|
256
|
+
s.adjacent.set(r, i), r.adjacent.set(s, i);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
class Ve {
|
|
260
|
+
constructor() {
|
|
261
|
+
A(this, "data", []);
|
|
262
|
+
}
|
|
263
|
+
get size() {
|
|
264
|
+
return this.data.length;
|
|
265
|
+
}
|
|
266
|
+
push(e, n) {
|
|
267
|
+
this.data.push({ item: e, priority: n }), this.bubbleUp(this.data.length - 1);
|
|
268
|
+
}
|
|
269
|
+
pop() {
|
|
270
|
+
if (this.data.length === 0) return;
|
|
271
|
+
const e = this.data[0], n = this.data.pop();
|
|
272
|
+
return this.data.length > 0 && (this.data[0] = n, this.sinkDown(0)), e.item;
|
|
273
|
+
}
|
|
274
|
+
bubbleUp(e) {
|
|
275
|
+
for (; e > 0; ) {
|
|
276
|
+
const n = e - 1 >> 1;
|
|
277
|
+
if (this.data[n].priority <= this.data[e].priority) break;
|
|
278
|
+
[this.data[n], this.data[e]] = [this.data[e], this.data[n]], e = n;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
sinkDown(e) {
|
|
282
|
+
const n = this.data.length;
|
|
283
|
+
for (; ; ) {
|
|
284
|
+
let s = e;
|
|
285
|
+
const r = 2 * e + 1, i = 2 * e + 2;
|
|
286
|
+
if (r < n && this.data[r].priority < this.data[s].priority && (s = r), i < n && this.data[i].priority < this.data[s].priority && (s = i), s === e) break;
|
|
287
|
+
[this.data[s], this.data[e]] = [this.data[e], this.data[s]], e = s;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
function Ze(t, e, n) {
|
|
292
|
+
const s = t.get(e), r = t.get(n);
|
|
293
|
+
if (!s || !r) return null;
|
|
294
|
+
const i = (d) => Math.abs(d.x - n.x) + Math.abs(d.y - n.y), o = (d, m) => `${d.pt.x},${d.pt.y},${m ?? "n"}`, c = /* @__PURE__ */ new Map(), a = /* @__PURE__ */ new Map(), l = new Ve(), f = { node: s, dir: null, g: 0 }, u = o(s, null);
|
|
295
|
+
for (c.set(u, 0), a.set(u, null), l.push(f, i(s.pt)); l.size > 0; ) {
|
|
296
|
+
const d = l.pop(), m = o(d.node, d.dir);
|
|
297
|
+
if (!(d.g > (c.get(m) ?? 1 / 0))) {
|
|
298
|
+
if (d.node === r) {
|
|
299
|
+
const h = [];
|
|
300
|
+
let g = d;
|
|
301
|
+
for (; g; ) {
|
|
302
|
+
h.push(g.node.pt);
|
|
303
|
+
const p = o(g.node, g.dir);
|
|
304
|
+
if (g = a.get(p), g == null) break;
|
|
305
|
+
}
|
|
306
|
+
return h[h.length - 1] !== s.pt && h.push(s.pt), h.reverse(), h;
|
|
307
|
+
}
|
|
308
|
+
for (const [h, g] of d.node.adjacent) {
|
|
309
|
+
const p = je(d.node.pt, h.pt);
|
|
310
|
+
if (p === null) continue;
|
|
311
|
+
const x = d.dir !== null && p === Je(d.dir), w = We(d.dir, p), b = x ? Rt * 3 : w ? Rt : 0, I = d.g + g + b, O = o(h, p);
|
|
312
|
+
I < (c.get(O) ?? 1 / 0) && (c.set(O, I), a.set(O, d), l.push(
|
|
313
|
+
{ node: h, dir: p, g: I },
|
|
314
|
+
I + i(h.pt)
|
|
315
|
+
));
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
return null;
|
|
320
|
+
}
|
|
321
|
+
function qe(t) {
|
|
322
|
+
const e = /* @__PURE__ */ new Map(), n = [];
|
|
323
|
+
for (const s of t) {
|
|
324
|
+
let r = e.get(s.x);
|
|
325
|
+
r || (r = /* @__PURE__ */ new Set(), e.set(s.x, r)), r.has(s.y) || (r.add(s.y), n.push(s));
|
|
326
|
+
}
|
|
327
|
+
return n;
|
|
328
|
+
}
|
|
329
|
+
function Ke(t, e, n, s) {
|
|
330
|
+
const r = n.left, i = k(n), o = n.top, c = T(n), a = [.../* @__PURE__ */ new Set([r, ...t.filter((d) => d >= r && d <= i), i])].sort((d, m) => d - m), l = [.../* @__PURE__ */ new Set([o, ...e.filter((d) => d >= o && d <= c), c])].sort((d, m) => d - m), f = (d) => s.some((m) => $e(m, d)), u = [];
|
|
331
|
+
for (const d of l)
|
|
332
|
+
for (const m of a) {
|
|
333
|
+
const h = { x: m, y: d };
|
|
334
|
+
f(h) || u.push(h);
|
|
335
|
+
}
|
|
336
|
+
return u;
|
|
337
|
+
}
|
|
338
|
+
function Mt(t, e, n) {
|
|
339
|
+
if (t.y === e.y) {
|
|
340
|
+
const s = t.y, r = Math.min(t.x, e.x), i = Math.max(t.x, e.x);
|
|
341
|
+
for (const o of n)
|
|
342
|
+
if (o.top < s && s < T(o) && o.left < i && k(o) > r)
|
|
343
|
+
return !0;
|
|
344
|
+
} else if (t.x === e.x) {
|
|
345
|
+
const s = t.x, r = Math.min(t.y, e.y), i = Math.max(t.y, e.y);
|
|
346
|
+
for (const o of n)
|
|
347
|
+
if (o.left < s && s < k(o) && o.top < i && T(o) > r)
|
|
348
|
+
return !0;
|
|
349
|
+
}
|
|
350
|
+
return !1;
|
|
351
|
+
}
|
|
352
|
+
function Qe(t, e) {
|
|
353
|
+
const n = new Ge(), s = /* @__PURE__ */ new Set(), r = /* @__PURE__ */ new Set();
|
|
354
|
+
for (const c of t)
|
|
355
|
+
n.add(c), s.add(c.x), r.add(c.y);
|
|
356
|
+
const i = [...s].sort((c, a) => c - a), o = [...r].sort((c, a) => c - a);
|
|
357
|
+
for (let c = 0; c < o.length; c++)
|
|
358
|
+
for (let a = 0; a < i.length; a++) {
|
|
359
|
+
const l = { x: i[a], y: o[c] };
|
|
360
|
+
if (n.has(l)) {
|
|
361
|
+
for (let f = a - 1; f >= 0; f--) {
|
|
362
|
+
const u = { x: i[f], y: o[c] };
|
|
363
|
+
if (n.has(u)) {
|
|
364
|
+
Mt(u, l, e) || n.connect(u, l);
|
|
365
|
+
break;
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
for (let f = c - 1; f >= 0; f--) {
|
|
369
|
+
const u = { x: i[a], y: o[f] };
|
|
370
|
+
if (n.has(u)) {
|
|
371
|
+
Mt(u, l, e) || n.connect(u, l);
|
|
372
|
+
break;
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
return n;
|
|
378
|
+
}
|
|
379
|
+
function he(t) {
|
|
380
|
+
if (t.length <= 2) return t;
|
|
381
|
+
const e = [t[0]];
|
|
382
|
+
for (let n = 1; n < t.length - 1; n++) {
|
|
383
|
+
const s = t[n - 1], r = t[n], i = t[n + 1], o = s.x === r.x && r.x === i.x, c = s.y === r.y && r.y === i.y;
|
|
384
|
+
o || c || s.x === r.x && s.y === r.y || e.push(r);
|
|
385
|
+
}
|
|
386
|
+
return e.push(t[t.length - 1]), e;
|
|
387
|
+
}
|
|
388
|
+
function wt(t) {
|
|
389
|
+
if (t.length <= 2) return 0;
|
|
390
|
+
let e = 0;
|
|
391
|
+
for (let n = 1; n < t.length - 1; n++) {
|
|
392
|
+
const s = t[n - 1], r = t[n], i = t[n + 1], o = s.x === r.x && r.x === i.x, c = s.y === r.y && r.y === i.y;
|
|
393
|
+
!o && !c && e++;
|
|
394
|
+
}
|
|
395
|
+
return e;
|
|
396
|
+
}
|
|
397
|
+
function bt(t) {
|
|
398
|
+
let e = 0;
|
|
399
|
+
for (let n = 1; n < t.length; n++)
|
|
400
|
+
e += Math.abs(t[n].x - t[n - 1].x) + Math.abs(t[n].y - t[n - 1].y);
|
|
401
|
+
return e;
|
|
402
|
+
}
|
|
403
|
+
function tn(t) {
|
|
404
|
+
let e = t[0], n = wt(e), s = bt(e);
|
|
405
|
+
for (let r = 1; r < t.length; r++) {
|
|
406
|
+
const i = wt(t[r]), o = bt(t[r]);
|
|
407
|
+
(i < n || i === n && o < s) && (e = t[r], n = i, s = o);
|
|
408
|
+
}
|
|
409
|
+
return e;
|
|
410
|
+
}
|
|
411
|
+
function en(t, e) {
|
|
412
|
+
return Math.abs(e.x - t.x) + Math.abs(e.y - t.y);
|
|
413
|
+
}
|
|
414
|
+
function nn(t) {
|
|
415
|
+
if (t.length <= 3) return 0;
|
|
416
|
+
const e = de * 0.75;
|
|
417
|
+
let n = 0;
|
|
418
|
+
for (let s = 1; s < t.length - 2; s++) {
|
|
419
|
+
const r = en(t[s], t[s + 1]);
|
|
420
|
+
r < e && (n += e - r);
|
|
421
|
+
}
|
|
422
|
+
return n;
|
|
423
|
+
}
|
|
424
|
+
function sn(t, e, n) {
|
|
425
|
+
if (t.x === e.x) {
|
|
426
|
+
const a = t.x, l = Math.min(t.y, e.y), f = Math.max(t.y, e.y), u = a < n.left ? n.left - a : a > k(n) ? a - k(n) : 0, d = f < n.top ? n.top - f : l > T(n) ? l - T(n) : 0;
|
|
427
|
+
return Math.hypot(u, d);
|
|
428
|
+
}
|
|
429
|
+
const s = t.y, r = Math.min(t.x, e.x), i = Math.max(t.x, e.x), o = s < n.top ? n.top - s : s > T(n) ? s - T(n) : 0, c = i < n.left ? n.left - i : r > k(n) ? r - k(n) : 0;
|
|
430
|
+
return Math.hypot(c, o);
|
|
431
|
+
}
|
|
432
|
+
function rn(t, e) {
|
|
433
|
+
if (t.length <= 3 || e.length === 0) return Number.POSITIVE_INFINITY;
|
|
434
|
+
const n = e.map(K);
|
|
435
|
+
let s = Number.POSITIVE_INFINITY;
|
|
436
|
+
for (let r = 1; r < t.length - 2; r++)
|
|
437
|
+
for (const i of n)
|
|
438
|
+
s = Math.min(s, sn(t[r], t[r + 1], i));
|
|
439
|
+
return s;
|
|
440
|
+
}
|
|
441
|
+
function on(t, e) {
|
|
442
|
+
const n = t.alternate ? [t.primary, t.alternate] : [t.primary], s = e.alternate ? [e.primary, e.alternate] : [e.primary], r = /* @__PURE__ */ new Map();
|
|
443
|
+
for (let i = 0; i < n.length; i++)
|
|
444
|
+
for (let o = 0; o < s.length; o++) {
|
|
445
|
+
const c = {
|
|
446
|
+
startDir: n[i],
|
|
447
|
+
endDir: s[o],
|
|
448
|
+
preferencePenalty: i + o
|
|
449
|
+
}, a = `${c.startDir}|${c.endDir}`, l = r.get(a);
|
|
450
|
+
(!l || c.preferencePenalty < l.preferencePenalty) && r.set(a, c);
|
|
451
|
+
}
|
|
452
|
+
return [...r.values()].sort((i, o) => i.preferencePenalty - o.preferencePenalty);
|
|
453
|
+
}
|
|
454
|
+
function cn(t, e) {
|
|
455
|
+
return t.bends !== e.bends ? t.bends < e.bends : Math.abs(t.shortSegmentPenalty - e.shortSegmentPenalty) > 0.5 ? t.shortSegmentPenalty < e.shortSegmentPenalty : Math.abs(t.clearance - e.clearance) > 1 ? t.clearance > e.clearance : Math.abs(t.length - e.length) > 0.5 ? t.length < e.length : t.preferencePenalty < e.preferencePenalty;
|
|
456
|
+
}
|
|
457
|
+
function an(t) {
|
|
458
|
+
const {
|
|
459
|
+
startWorld: e,
|
|
460
|
+
endWorld: n,
|
|
461
|
+
startBinding: s,
|
|
462
|
+
endBinding: r,
|
|
463
|
+
startShape: i,
|
|
464
|
+
endShape: o,
|
|
465
|
+
intermediateObstacles: c = [],
|
|
466
|
+
minStubLength: a
|
|
467
|
+
} = t, l = (g) => !g.isPrecise || g.fixedPoint[0] === 0.5 && g.fixedPoint[1] === 0.5, f = s && l(s) ? Ct(i ?? { x: e.x, y: e.y, width: 1, height: 1 }, n, o, r) : { primary: s ? xt(s.fixedPoint) : Yt(e, n) }, u = r && l(r) ? Ct(o ?? { x: n.x, y: n.y, width: 1, height: 1 }, e, i, s) : { primary: r ? xt(r.fixedPoint) : Yt(n, e) }, d = on(f, u);
|
|
468
|
+
if (d.length === 1)
|
|
469
|
+
return { startDir: d[0].startDir, endDir: d[0].endDir };
|
|
470
|
+
const m = [
|
|
471
|
+
...i ? [i] : [],
|
|
472
|
+
...o ? [o] : [],
|
|
473
|
+
...c
|
|
474
|
+
];
|
|
475
|
+
let h = null;
|
|
476
|
+
for (const g of d) {
|
|
477
|
+
const p = ln(
|
|
478
|
+
e,
|
|
479
|
+
n,
|
|
480
|
+
g.startDir,
|
|
481
|
+
g.endDir,
|
|
482
|
+
i,
|
|
483
|
+
o,
|
|
484
|
+
a,
|
|
485
|
+
c
|
|
486
|
+
), x = {
|
|
487
|
+
...g,
|
|
488
|
+
path: p,
|
|
489
|
+
bends: wt(p),
|
|
490
|
+
length: bt(p),
|
|
491
|
+
shortSegmentPenalty: nn(p),
|
|
492
|
+
clearance: rn(p, m)
|
|
493
|
+
};
|
|
494
|
+
(!h || cn(x, h)) && (h = x);
|
|
495
|
+
}
|
|
496
|
+
return h ? { startDir: h.startDir, endDir: h.endDir } : { startDir: f.primary, endDir: u.primary };
|
|
497
|
+
}
|
|
498
|
+
function nt(t, e, n, s, r, i) {
|
|
499
|
+
let o = {
|
|
500
|
+
left: Math.min(t.x, e.x),
|
|
501
|
+
top: Math.min(t.y, e.y),
|
|
502
|
+
width: Math.abs(e.x - t.x) || 1,
|
|
503
|
+
height: Math.abs(e.y - t.y) || 1
|
|
504
|
+
};
|
|
505
|
+
for (const x of r)
|
|
506
|
+
o = Ue(o, x);
|
|
507
|
+
o = L(o, J, J);
|
|
508
|
+
const c = [], a = [];
|
|
509
|
+
for (const x of r)
|
|
510
|
+
c.push(x.left, k(x)), a.push(x.top, T(x)), c.push(x.left + x.width / 2), a.push(x.top + x.height / 2);
|
|
511
|
+
c.push(t.x, e.x), a.push(t.y, e.y);
|
|
512
|
+
const l = dt(n), f = dt(s);
|
|
513
|
+
let u = {
|
|
514
|
+
x: t.x + l.x * i,
|
|
515
|
+
y: t.y + l.y * i
|
|
516
|
+
}, d = {
|
|
517
|
+
x: e.x + f.x * i,
|
|
518
|
+
y: e.y + f.y * i
|
|
519
|
+
};
|
|
520
|
+
u = Dt(u, n, r), d = Dt(d, s, r), c.push(u.x, d.x), a.push(u.y, d.y), c.push((t.x + e.x) / 2), a.push((t.y + e.y) / 2);
|
|
521
|
+
const m = Ke(c, a, o, r), h = qe([u, d, ...m]), g = Qe(h, r), p = Ze(g, u, d);
|
|
522
|
+
if (p) {
|
|
523
|
+
const x = [t, ...p, e];
|
|
524
|
+
return he(x);
|
|
525
|
+
}
|
|
526
|
+
return null;
|
|
527
|
+
}
|
|
528
|
+
function ln(t, e, n, s, r, i, o, c) {
|
|
529
|
+
if (t.x === e.x && t.y === e.y)
|
|
530
|
+
return [t, e];
|
|
531
|
+
const a = r ? K(r) : { left: t.x, top: t.y, width: 0, height: 0 }, l = i ? K(i) : { left: e.x, top: e.y, width: 0, height: 0 }, f = ae, u = Math.max(de, o ?? 0), d = (c ?? []).map(
|
|
532
|
+
(y) => L(K(y), f, f)
|
|
533
|
+
), m = r ? et(a, f, n) : L(a, f, f), h = i ? et(l, f, s) : L(l, f, f), g = [n], p = [s];
|
|
534
|
+
let x = !1;
|
|
535
|
+
if (r) {
|
|
536
|
+
const y = Lt(r, e);
|
|
537
|
+
y !== n && (g.push(y), x = !0);
|
|
538
|
+
}
|
|
539
|
+
if (i) {
|
|
540
|
+
const y = Lt(i, t);
|
|
541
|
+
y !== s && (p.push(y), x = !0);
|
|
542
|
+
}
|
|
543
|
+
const w = r ? Xt(a, f, g) : L(a, f, f), b = i ? Xt(l, f, p) : L(l, f, f), I = [], O = nt(
|
|
544
|
+
t,
|
|
545
|
+
e,
|
|
546
|
+
n,
|
|
547
|
+
s,
|
|
548
|
+
[m, h, ...d],
|
|
549
|
+
u
|
|
550
|
+
);
|
|
551
|
+
if (O && I.push(O), x) {
|
|
552
|
+
const y = nt(
|
|
553
|
+
t,
|
|
554
|
+
e,
|
|
555
|
+
n,
|
|
556
|
+
s,
|
|
557
|
+
[w, b, ...d],
|
|
558
|
+
u
|
|
559
|
+
);
|
|
560
|
+
y && I.push(y);
|
|
561
|
+
}
|
|
562
|
+
if (I.length > 0)
|
|
563
|
+
return tn(I);
|
|
564
|
+
if (d.length > 0) {
|
|
565
|
+
const y = nt(
|
|
566
|
+
t,
|
|
567
|
+
e,
|
|
568
|
+
n,
|
|
569
|
+
s,
|
|
570
|
+
[m, h],
|
|
571
|
+
u
|
|
572
|
+
);
|
|
573
|
+
if (y) return y;
|
|
574
|
+
}
|
|
575
|
+
if (d.length > 0) {
|
|
576
|
+
const y = Math.max(4, f / 2), v = (c ?? []).map(
|
|
577
|
+
($) => L(K($), y, y)
|
|
578
|
+
), _ = r ? et(a, y, n) : L(a, y, y), S = i ? et(l, y, s) : L(l, y, y), B = nt(
|
|
579
|
+
t,
|
|
580
|
+
e,
|
|
581
|
+
n,
|
|
582
|
+
s,
|
|
583
|
+
[_, S, ...v],
|
|
584
|
+
u
|
|
585
|
+
);
|
|
586
|
+
if (B)
|
|
587
|
+
return B;
|
|
588
|
+
}
|
|
589
|
+
return he(dn(t, e, n, s, u));
|
|
590
|
+
}
|
|
591
|
+
function Dt(t, e, n) {
|
|
592
|
+
let { x: s, y: r } = t;
|
|
593
|
+
for (let i = 0; i < n.length; i++) {
|
|
594
|
+
let o = !0;
|
|
595
|
+
for (const c of n)
|
|
596
|
+
if (!(s <= c.left || s >= k(c) || r <= c.top || r >= T(c)))
|
|
597
|
+
switch (o = !1, e) {
|
|
598
|
+
case "left":
|
|
599
|
+
s = c.left - 1;
|
|
600
|
+
break;
|
|
601
|
+
case "right":
|
|
602
|
+
s = k(c) + 1;
|
|
603
|
+
break;
|
|
604
|
+
case "up":
|
|
605
|
+
r = c.top - 1;
|
|
606
|
+
break;
|
|
607
|
+
case "down":
|
|
608
|
+
r = T(c) + 1;
|
|
609
|
+
break;
|
|
610
|
+
}
|
|
611
|
+
if (o) break;
|
|
612
|
+
}
|
|
613
|
+
return { x: s, y: r };
|
|
614
|
+
}
|
|
615
|
+
function dn(t, e, n, s, r) {
|
|
616
|
+
const i = Math.max(r, ae), o = dt(n), c = dt(s), a = { x: t.x + o.x * i, y: t.y + o.y * i }, l = { x: e.x + c.x * i, y: e.y + c.y * i };
|
|
617
|
+
if (q(n) === q(s))
|
|
618
|
+
if (q(n)) {
|
|
619
|
+
const f = (a.y + l.y) / 2;
|
|
620
|
+
return [t, a, { x: a.x, y: f }, { x: l.x, y: f }, l, e];
|
|
621
|
+
} else {
|
|
622
|
+
const f = (a.x + l.x) / 2;
|
|
623
|
+
return [t, a, { x: f, y: a.y }, { x: f, y: l.y }, l, e];
|
|
624
|
+
}
|
|
625
|
+
else
|
|
626
|
+
return q(n) ? [t, a, { x: a.x, y: l.y }, l, e] : [t, a, { x: l.x, y: a.y }, l, e];
|
|
627
|
+
}
|
|
628
|
+
const fn = /* @__PURE__ */ new Set(["rectangle", "ellipse", "diamond", "text", "image"]);
|
|
629
|
+
function un(t, e, n, s) {
|
|
630
|
+
const r = [], i = Math.min(t.x, e.x) - J * 3, o = Math.max(t.x, e.x) + J * 3, c = Math.min(t.y, e.y) - J * 3, a = Math.max(t.y, e.y) + J * 3;
|
|
631
|
+
for (const l of n) {
|
|
632
|
+
if (!fn.has(l.type) || s.has(l.id) || !l.isVisible) continue;
|
|
633
|
+
const f = He(l);
|
|
634
|
+
f.x + f.width < i || f.x > o || f.y + f.height < c || f.y > a || r.push(f);
|
|
635
|
+
}
|
|
636
|
+
return r;
|
|
637
|
+
}
|
|
638
|
+
const me = 0.2;
|
|
639
|
+
function hn(t, e, n = me) {
|
|
640
|
+
const s = (t.x + e.x) / 2, r = (t.y + e.y) / 2, i = e.x - t.x, o = e.y - t.y, c = Math.sqrt(i * i + o * o);
|
|
641
|
+
if (c === 0) return { x: s, y: r };
|
|
642
|
+
const a = -o / c, l = i / c, f = c * n;
|
|
643
|
+
return { x: s + a * f, y: r + l * f };
|
|
644
|
+
}
|
|
645
|
+
function mn(t, e, n, s) {
|
|
646
|
+
const r = 1 - s;
|
|
647
|
+
return {
|
|
648
|
+
x: r * r * t.x + 2 * r * s * e.x + s * s * n.x,
|
|
649
|
+
y: r * r * t.y + 2 * r * s * e.y + s * s * n.y
|
|
650
|
+
};
|
|
651
|
+
}
|
|
652
|
+
function ye(t) {
|
|
653
|
+
return t * Math.PI / 180;
|
|
654
|
+
}
|
|
655
|
+
function ge(t, e, n) {
|
|
656
|
+
if (n === 0) return { x: t, y: e };
|
|
657
|
+
const s = Math.cos(n), r = Math.sin(n);
|
|
658
|
+
return { x: t * s - e * r, y: t * r + e * s };
|
|
659
|
+
}
|
|
660
|
+
function yn(t, e) {
|
|
661
|
+
const n = t.x + t.width / 2, s = t.y + t.height / 2, r = e.x - n, i = e.y - s, o = ye(t.rotation || 0);
|
|
662
|
+
return o === 0 ? { x: r, y: i } : ge(r, i, -o);
|
|
663
|
+
}
|
|
664
|
+
function Q(t, e) {
|
|
665
|
+
const n = t.x + t.width / 2, s = t.y + t.height / 2, r = ye(t.rotation || 0), i = ge(e.x, e.y, r);
|
|
666
|
+
return { x: n + i.x, y: s + i.y };
|
|
667
|
+
}
|
|
668
|
+
function gn(t) {
|
|
669
|
+
switch (t) {
|
|
670
|
+
case "n":
|
|
671
|
+
return [0.5, 0];
|
|
672
|
+
case "s":
|
|
673
|
+
return [0.5, 1];
|
|
674
|
+
case "e":
|
|
675
|
+
return [1, 0.5];
|
|
676
|
+
case "w":
|
|
677
|
+
return [0, 0.5];
|
|
678
|
+
case "ne":
|
|
679
|
+
return [1, 0];
|
|
680
|
+
case "nw":
|
|
681
|
+
return [0, 0];
|
|
682
|
+
case "se":
|
|
683
|
+
return [1, 1];
|
|
684
|
+
case "sw":
|
|
685
|
+
return [0, 1];
|
|
686
|
+
case "center":
|
|
687
|
+
return [0.5, 0.5];
|
|
688
|
+
case "auto":
|
|
689
|
+
return [0.5, 0.5];
|
|
690
|
+
// caller should resolve via nearest
|
|
691
|
+
default:
|
|
692
|
+
return [0.5, 0.5];
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
function pn(t, e) {
|
|
696
|
+
if (!t.ports) return null;
|
|
697
|
+
const n = t.ports.find((s) => s.id === e);
|
|
698
|
+
return n ? n.ratio : null;
|
|
699
|
+
}
|
|
700
|
+
function Nt(t, e) {
|
|
701
|
+
if (t.portId) {
|
|
702
|
+
const s = pn(e, t.portId);
|
|
703
|
+
if (s) return { fixedPoint: s, snapMode: "port" };
|
|
704
|
+
}
|
|
705
|
+
if (t.anchor && t.anchor !== "auto")
|
|
706
|
+
return { fixedPoint: gn(t.anchor), snapMode: "anchor" };
|
|
707
|
+
const n = t.fixedPoint[0] === 0.5 && t.fixedPoint[1] === 0.5 ? "center" : "edge";
|
|
708
|
+
return { fixedPoint: t.fixedPoint, snapMode: n };
|
|
709
|
+
}
|
|
710
|
+
function Ft(t, e, n = 0, s) {
|
|
711
|
+
const r = (e[0] - 0.5) * t.width, i = (e[1] - 0.5) * t.height;
|
|
712
|
+
if (r === 0 && i === 0)
|
|
713
|
+
return s ? ft(t, s, n) : Q(t, { x: 0, y: 0 });
|
|
714
|
+
const o = Q(t, { x: r, y: i });
|
|
715
|
+
return ft(t, o, n);
|
|
716
|
+
}
|
|
717
|
+
function ft(t, e, n = 0) {
|
|
718
|
+
const s = yn(t, e), r = s.x, i = s.y;
|
|
719
|
+
if (r === 0 && i === 0)
|
|
720
|
+
return t.width >= t.height ? Q(t, { x: t.width / 2 + n, y: 0 }) : Q(t, { x: 0, y: -(t.height / 2 + n) });
|
|
721
|
+
let o;
|
|
722
|
+
switch (t.type) {
|
|
723
|
+
case "ellipse": {
|
|
724
|
+
const c = t.width / 2, a = t.height / 2, l = Math.atan2(i, r);
|
|
725
|
+
o = {
|
|
726
|
+
x: (c + n) * Math.cos(l),
|
|
727
|
+
y: (a + n) * Math.sin(l)
|
|
728
|
+
};
|
|
729
|
+
break;
|
|
730
|
+
}
|
|
731
|
+
case "diamond": {
|
|
732
|
+
const c = t.width / 2, a = t.height / 2, l = Math.atan2(i, r), f = Math.cos(l), u = Math.sin(l), d = Math.abs(f), m = Math.abs(u), h = 1 / (d / c + m / a), g = h * f, p = h * u;
|
|
733
|
+
if (n === 0)
|
|
734
|
+
o = { x: g, y: p };
|
|
735
|
+
else {
|
|
736
|
+
const x = (f >= 0 ? 1 : -1) / c, w = (u >= 0 ? 1 : -1) / a, b = Math.sqrt(x * x + w * w);
|
|
737
|
+
o = {
|
|
738
|
+
x: g + x / b * n,
|
|
739
|
+
y: p + w / b * n
|
|
740
|
+
};
|
|
741
|
+
}
|
|
742
|
+
break;
|
|
743
|
+
}
|
|
744
|
+
default: {
|
|
745
|
+
const c = t.width / 2, a = t.height / 2, l = Math.atan2(i, r), f = Math.abs(Math.tan(l));
|
|
746
|
+
let u, d;
|
|
747
|
+
f <= a / c ? (u = r > 0 ? c : -c, d = u * Math.tan(l)) : (d = i > 0 ? a : -a, u = d / Math.tan(l)), n === 0 ? o = { x: u, y: d } : f <= a / c ? o = { x: u + (r > 0 ? n : -n), y: d } : o = { x: u, y: d + (i > 0 ? n : -n) };
|
|
748
|
+
break;
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
return Q(t, o);
|
|
752
|
+
}
|
|
753
|
+
function xn(t, e) {
|
|
754
|
+
const { startBinding: n, endBinding: s } = t;
|
|
755
|
+
if (!n && !s) return null;
|
|
756
|
+
const r = new Map(e.map((y) => [y.id, y])), i = t.points, o = i.length / 2;
|
|
757
|
+
let c = {
|
|
758
|
+
x: t.x + i[0],
|
|
759
|
+
y: t.y + i[1]
|
|
760
|
+
}, a = {
|
|
761
|
+
x: t.x + i[i.length - 2],
|
|
762
|
+
y: t.y + i[i.length - 1]
|
|
763
|
+
};
|
|
764
|
+
const l = n ? r.get(n.elementId) : void 0, f = s ? r.get(s.elementId) : void 0, u = t.lineType === "elbow", d = (y, v) => {
|
|
765
|
+
if (y.isPrecise) {
|
|
766
|
+
const { fixedPoint: _ } = Nt(y, v);
|
|
767
|
+
return Ft(v, _, 0);
|
|
768
|
+
}
|
|
769
|
+
return { x: v.x + v.width / 2, y: v.y + v.height / 2 };
|
|
770
|
+
}, m = (y, v, _) => {
|
|
771
|
+
const { fixedPoint: S } = Nt(y, v);
|
|
772
|
+
return Ft(v, S, y.gap, _);
|
|
773
|
+
}, h = n && l ? n.isPrecise ? m(n, l, a) : { x: l.x + l.width / 2, y: l.y + l.height / 2 } : c, g = s && f ? s.isPrecise ? m(s, f, c) : { x: f.x + f.width / 2, y: f.y + f.height / 2 } : a, p = u ? (() => {
|
|
774
|
+
const y = /* @__PURE__ */ new Set();
|
|
775
|
+
return n?.elementId && y.add(n.elementId), s?.elementId && y.add(s.elementId), an({
|
|
776
|
+
startWorld: h,
|
|
777
|
+
endWorld: g,
|
|
778
|
+
startBinding: n,
|
|
779
|
+
endBinding: s,
|
|
780
|
+
startShape: l ?? null,
|
|
781
|
+
endShape: f ?? null,
|
|
782
|
+
intermediateObstacles: un(h, g, e, y)
|
|
783
|
+
});
|
|
784
|
+
})() : null, x = (y, v, _) => {
|
|
785
|
+
const S = y.x + y.width / 2, B = y.y + y.height / 2, $ = Math.max(y.width, y.height) * 10;
|
|
786
|
+
let z;
|
|
787
|
+
switch (_) {
|
|
788
|
+
case "up":
|
|
789
|
+
z = { x: S, y: B - $ };
|
|
790
|
+
break;
|
|
791
|
+
case "down":
|
|
792
|
+
z = { x: S, y: B + $ };
|
|
793
|
+
break;
|
|
794
|
+
case "left":
|
|
795
|
+
z = { x: S - $, y: B };
|
|
796
|
+
break;
|
|
797
|
+
case "right":
|
|
798
|
+
z = { x: S + $, y: B };
|
|
799
|
+
break;
|
|
800
|
+
}
|
|
801
|
+
return ft(y, z, v);
|
|
802
|
+
}, w = u ? (y, v, _, S) => x(y, _, S) : ft;
|
|
803
|
+
if (n && l && s && f) {
|
|
804
|
+
if (n.isPrecise)
|
|
805
|
+
c = m(n, l, a);
|
|
806
|
+
else {
|
|
807
|
+
const y = d(s, f);
|
|
808
|
+
c = w(l, y, n.gap, p?.startDir ?? "right");
|
|
809
|
+
}
|
|
810
|
+
if (s.isPrecise)
|
|
811
|
+
a = m(s, f, c);
|
|
812
|
+
else {
|
|
813
|
+
const y = d(n, l);
|
|
814
|
+
a = w(f, y, s.gap, p?.endDir ?? "left");
|
|
815
|
+
}
|
|
816
|
+
n.isPrecise ? c = m(n, l, a) : n.isPrecise || (c = w(l, a, n.gap, p?.startDir ?? "right")), s.isPrecise ? a = m(s, f, c) : s.isPrecise || (a = w(f, c, s.gap, p?.endDir ?? "left"));
|
|
817
|
+
} else
|
|
818
|
+
n && l && (n.isPrecise ? c = m(n, l, a) : c = w(l, a, n.gap, p?.startDir ?? "right")), s && f && (s.isPrecise ? a = m(s, f, c) : a = w(f, c, s.gap, p?.endDir ?? "left"));
|
|
819
|
+
const b = [0, 0];
|
|
820
|
+
for (let y = 1; y < o - 1; y++) {
|
|
821
|
+
const v = t.x + i[y * 2], _ = t.y + i[y * 2 + 1];
|
|
822
|
+
b.push(v - c.x, _ - c.y);
|
|
823
|
+
}
|
|
824
|
+
b.push(a.x - c.x, a.y - c.y);
|
|
825
|
+
const I = a.x - c.x, O = a.y - c.y;
|
|
826
|
+
return {
|
|
827
|
+
x: c.x,
|
|
828
|
+
y: c.y,
|
|
829
|
+
points: b,
|
|
830
|
+
width: Math.abs(I),
|
|
831
|
+
height: Math.abs(O)
|
|
832
|
+
};
|
|
833
|
+
}
|
|
834
|
+
function wn(t, e) {
|
|
835
|
+
return e.filter((n) => {
|
|
836
|
+
if (n.type !== "line" && n.type !== "arrow") return !1;
|
|
837
|
+
const s = n;
|
|
838
|
+
return s.startBinding?.elementId === t || s.endBinding?.elementId === t;
|
|
839
|
+
});
|
|
840
|
+
}
|
|
841
|
+
function bn(t, e) {
|
|
842
|
+
return e.map((n) => {
|
|
843
|
+
let s = !1;
|
|
844
|
+
if (n.boundElements && n.boundElements.length > 0) {
|
|
845
|
+
const c = n.boundElements.filter((a) => !t.has(a.id));
|
|
846
|
+
c.length !== n.boundElements.length && (n = { ...n, boundElements: c.length > 0 ? c : null }, s = !0);
|
|
847
|
+
}
|
|
848
|
+
if (n.type !== "line" && n.type !== "arrow") return n;
|
|
849
|
+
const r = n;
|
|
850
|
+
let i = r.startBinding, o = r.endBinding;
|
|
851
|
+
return i && t.has(i.elementId) && (i = null, s = !0), o && t.has(o.elementId) && (o = null, s = !0), s ? { ...n, startBinding: i, endBinding: o } : n;
|
|
852
|
+
});
|
|
853
|
+
}
|
|
854
|
+
function vn(t, e, n) {
|
|
855
|
+
const s = t.points, r = { x: s[0], y: s[1] }, i = { x: s[s.length - 2], y: s[s.length - 1] };
|
|
856
|
+
let o, c;
|
|
857
|
+
if (t.lineType === "curved") {
|
|
858
|
+
const a = t.curvature ?? me, l = hn(r, i, a), f = mn(r, l, i, 0.5);
|
|
859
|
+
o = t.x + f.x, c = t.y + f.y;
|
|
860
|
+
} else if (t.lineType === "elbow" && s.length >= 4) {
|
|
861
|
+
const a = s.length / 2 - 1;
|
|
862
|
+
let l = 0;
|
|
863
|
+
for (let d = 0; d < a; d++) {
|
|
864
|
+
const m = s[(d + 1) * 2] - s[d * 2], h = s[(d + 1) * 2 + 1] - s[d * 2 + 1];
|
|
865
|
+
l += Math.sqrt(m * m + h * h);
|
|
866
|
+
}
|
|
867
|
+
const f = l / 2;
|
|
868
|
+
let u = 0;
|
|
869
|
+
o = t.x + (r.x + i.x) / 2, c = t.y + (r.y + i.y) / 2;
|
|
870
|
+
for (let d = 0; d < a; d++) {
|
|
871
|
+
const m = s[d * 2], h = s[d * 2 + 1], g = s[(d + 1) * 2], p = s[(d + 1) * 2 + 1], x = g - m, w = p - h, b = Math.sqrt(x * x + w * w);
|
|
872
|
+
if (u + b >= f && b > 0) {
|
|
873
|
+
const I = (f - u) / b;
|
|
874
|
+
o = t.x + m + x * I, c = t.y + h + w * I;
|
|
875
|
+
break;
|
|
876
|
+
}
|
|
877
|
+
u += b;
|
|
878
|
+
}
|
|
879
|
+
} else
|
|
880
|
+
o = t.x + (r.x + i.x) / 2, c = t.y + (r.y + i.y) / 2;
|
|
881
|
+
return {
|
|
882
|
+
x: o - e / 2,
|
|
883
|
+
y: c - n / 2
|
|
884
|
+
};
|
|
885
|
+
}
|
|
886
|
+
function In(t, e) {
|
|
887
|
+
const n = [];
|
|
888
|
+
for (const s of t) {
|
|
889
|
+
const r = e.get(s);
|
|
890
|
+
if (!r || r.type !== "arrow" && r.type !== "line" || !r.boundElements) continue;
|
|
891
|
+
const i = r;
|
|
892
|
+
for (const o of r.boundElements) {
|
|
893
|
+
if (o.type !== "text") continue;
|
|
894
|
+
const c = e.get(o.id);
|
|
895
|
+
if (!c) continue;
|
|
896
|
+
const a = Math.max(10, c.width || 60), l = c.height || 30, f = vn(i, a, l);
|
|
897
|
+
n.push({ id: c.id, updates: { x: f.x, y: f.y } });
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
return n;
|
|
901
|
+
}
|
|
902
|
+
const st = 4, _n = /* @__PURE__ */ new Set([
|
|
903
|
+
"rectangle",
|
|
904
|
+
"ellipse",
|
|
905
|
+
"diamond",
|
|
906
|
+
"image"
|
|
907
|
+
]);
|
|
908
|
+
function Sn(t, e) {
|
|
909
|
+
const n = Math.max(20, t.width - st * 2);
|
|
910
|
+
let s;
|
|
911
|
+
return e.verticalAlign === "top" ? s = t.y + st : e.verticalAlign === "bottom" ? s = t.y + t.height - e.height - st : s = t.y + (t.height - e.height) / 2, { x: t.x + st, y: s, width: n };
|
|
912
|
+
}
|
|
913
|
+
function An(t, e, n) {
|
|
914
|
+
const s = /* @__PURE__ */ new Map();
|
|
915
|
+
for (const o of e) s.set(o.id, o);
|
|
916
|
+
const r = [], i = /* @__PURE__ */ new Set();
|
|
917
|
+
for (const o of t) {
|
|
918
|
+
const c = wn(o, e);
|
|
919
|
+
for (const l of c) {
|
|
920
|
+
if (i.has(l.id)) continue;
|
|
921
|
+
i.add(l.id);
|
|
922
|
+
const f = s.get(l.id);
|
|
923
|
+
if (!f) continue;
|
|
924
|
+
const u = xn(f, e);
|
|
925
|
+
u && r.push({ id: f.id, updates: u });
|
|
926
|
+
}
|
|
927
|
+
const a = s.get(o);
|
|
928
|
+
if (a?.boundElements && _n.has(a.type))
|
|
929
|
+
for (const l of a.boundElements) {
|
|
930
|
+
if (l.type !== "text") continue;
|
|
931
|
+
const f = s.get(l.id);
|
|
932
|
+
f && r.push({ id: l.id, updates: Sn(a, f) });
|
|
933
|
+
}
|
|
934
|
+
}
|
|
935
|
+
if (i.size > 0 && r.length > 0) {
|
|
936
|
+
const o = new Map(s);
|
|
937
|
+
for (const a of r) {
|
|
938
|
+
const l = o.get(a.id);
|
|
939
|
+
l && o.set(a.id, { ...l, ...a.updates });
|
|
940
|
+
}
|
|
941
|
+
const c = In(
|
|
942
|
+
i,
|
|
943
|
+
o
|
|
944
|
+
);
|
|
945
|
+
for (const a of c)
|
|
946
|
+
r.push(a);
|
|
947
|
+
}
|
|
948
|
+
return { updates: r, processedConnectorIds: i };
|
|
949
|
+
}
|
|
950
|
+
let En = "useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict", kn = (t = 21) => {
|
|
951
|
+
let e = "", n = crypto.getRandomValues(new Uint8Array(t |= 0));
|
|
952
|
+
for (; t--; )
|
|
953
|
+
e += En[n[t] & 63];
|
|
954
|
+
return e;
|
|
955
|
+
};
|
|
956
|
+
function vt() {
|
|
957
|
+
return kn(12);
|
|
958
|
+
}
|
|
959
|
+
const rt = /* @__PURE__ */ new Set([
|
|
960
|
+
"rectangle",
|
|
961
|
+
"ellipse",
|
|
962
|
+
"diamond",
|
|
963
|
+
"line",
|
|
964
|
+
"arrow",
|
|
965
|
+
"freedraw",
|
|
966
|
+
"text",
|
|
967
|
+
"image"
|
|
968
|
+
]), $t = /* @__PURE__ */ new Set(["left", "center", "right"]), Ut = /* @__PURE__ */ new Set(["top", "middle", "bottom"]), Ht = /* @__PURE__ */ new Set(["fit", "fill", "stretch"]), Jt = /* @__PURE__ */ new Set(["sharp", "curved", "elbow"]);
|
|
969
|
+
class Tn {
|
|
970
|
+
constructor() {
|
|
971
|
+
A(this, "customs", /* @__PURE__ */ new Map());
|
|
972
|
+
}
|
|
973
|
+
// ── Registration ──────────────────────────────────────────
|
|
974
|
+
/**
|
|
975
|
+
* Register a custom element type.
|
|
976
|
+
*
|
|
977
|
+
* @throws If the type already exists and `allowOverride` is not `true`.
|
|
978
|
+
*/
|
|
979
|
+
register(e) {
|
|
980
|
+
if (rt.has(e.type) && !e.allowOverride)
|
|
981
|
+
throw new Error(
|
|
982
|
+
`[f1ow] Cannot register custom element type "${e.type}" — it conflicts with a built-in type. Set allowOverride: true if you intentionally want to replace the built-in validator.`
|
|
983
|
+
);
|
|
984
|
+
if (this.customs.has(e.type) && !e.allowOverride)
|
|
985
|
+
throw new Error(
|
|
986
|
+
`[f1ow] Element type "${e.type}" is already registered. Set allowOverride: true to replace the existing registration.`
|
|
987
|
+
);
|
|
988
|
+
this.customs.set(e.type, e);
|
|
989
|
+
}
|
|
990
|
+
// ── Queries ───────────────────────────────────────────────
|
|
991
|
+
/** Returns `true` if the type is known (built-in or custom). */
|
|
992
|
+
isRegistered(e) {
|
|
993
|
+
return rt.has(e) || this.customs.has(e);
|
|
994
|
+
}
|
|
995
|
+
/** Retrieve the custom config for a type (undefined for built-in types). */
|
|
996
|
+
getCustomConfig(e) {
|
|
997
|
+
return this.customs.get(e);
|
|
998
|
+
}
|
|
999
|
+
/** All registered type names, built-in first then custom. */
|
|
1000
|
+
getRegisteredTypes() {
|
|
1001
|
+
return [...rt, ...this.customs.keys()];
|
|
1002
|
+
}
|
|
1003
|
+
// ── Validation ────────────────────────────────────────────
|
|
1004
|
+
/**
|
|
1005
|
+
* Validate a full element before it enters the canvas store.
|
|
1006
|
+
*
|
|
1007
|
+
* Checks:
|
|
1008
|
+
* 1. Non-null object with required base fields (id, type, x, y, w, h, rotation, style)
|
|
1009
|
+
* 2. `type` is a known/registered type
|
|
1010
|
+
* 3. Type-specific field checks for all 8 built-in types
|
|
1011
|
+
* 4. Custom `validate` callback (custom types only)
|
|
1012
|
+
*/
|
|
1013
|
+
validateElement(e) {
|
|
1014
|
+
if (!e || typeof e != "object" || Array.isArray(e))
|
|
1015
|
+
return { valid: !1, error: "Element must be a non-null object" };
|
|
1016
|
+
const n = e;
|
|
1017
|
+
if (typeof n.id != "string" || n.id.trim() === "")
|
|
1018
|
+
return { valid: !1, error: "Element id must be a non-empty string" };
|
|
1019
|
+
const s = n.id;
|
|
1020
|
+
if (typeof n.type != "string" || !this.isRegistered(n.type))
|
|
1021
|
+
return {
|
|
1022
|
+
valid: !1,
|
|
1023
|
+
error: `Unknown element type "${n.type}". Valid types: ${this.getRegisteredTypes().join(", ")}`
|
|
1024
|
+
};
|
|
1025
|
+
if (typeof n.x != "number" || !isFinite(n.x))
|
|
1026
|
+
return { valid: !1, error: `"${s}": x must be a finite number` };
|
|
1027
|
+
if (typeof n.y != "number" || !isFinite(n.y))
|
|
1028
|
+
return { valid: !1, error: `"${s}": y must be a finite number` };
|
|
1029
|
+
if (typeof n.width != "number" || !isFinite(n.width) || n.width < 0)
|
|
1030
|
+
return { valid: !1, error: `"${s}": width must be a non-negative finite number` };
|
|
1031
|
+
if (typeof n.height != "number" || !isFinite(n.height) || n.height < 0)
|
|
1032
|
+
return { valid: !1, error: `"${s}": height must be a non-negative finite number` };
|
|
1033
|
+
if (typeof n.rotation != "number" || !isFinite(n.rotation))
|
|
1034
|
+
return { valid: !1, error: `"${s}": rotation must be a finite number` };
|
|
1035
|
+
const r = this._validateStyle(s, n.style);
|
|
1036
|
+
if (!r.valid) return r;
|
|
1037
|
+
if (rt.has(n.type)) {
|
|
1038
|
+
const o = this._validateBuiltinFields(n);
|
|
1039
|
+
if (!o.valid) return o;
|
|
1040
|
+
}
|
|
1041
|
+
const i = this.customs.get(n.type);
|
|
1042
|
+
if (i?.validate) {
|
|
1043
|
+
let o;
|
|
1044
|
+
try {
|
|
1045
|
+
o = i.validate(n);
|
|
1046
|
+
} catch (c) {
|
|
1047
|
+
return {
|
|
1048
|
+
valid: !1,
|
|
1049
|
+
error: `Custom element "${i.displayName ?? i.type}" validator threw for "${s}": ${c instanceof Error ? c.message : String(c)}`
|
|
1050
|
+
};
|
|
1051
|
+
}
|
|
1052
|
+
if (o !== !0)
|
|
1053
|
+
return {
|
|
1054
|
+
valid: !1,
|
|
1055
|
+
error: `Custom element "${i.displayName ?? i.type}" validation failed for "${s}": ${o}`
|
|
1056
|
+
};
|
|
1057
|
+
}
|
|
1058
|
+
return { valid: !0 };
|
|
1059
|
+
}
|
|
1060
|
+
/**
|
|
1061
|
+
* Validate a partial update before it is applied to an existing element.
|
|
1062
|
+
*
|
|
1063
|
+
* Prevents overwriting immutable fields (`id` and `type`), and checks
|
|
1064
|
+
* numeric fields for finiteness.
|
|
1065
|
+
*/
|
|
1066
|
+
validateUpdate(e) {
|
|
1067
|
+
if ("id" in e)
|
|
1068
|
+
return { valid: !1, error: "Cannot overwrite element id via updateElement" };
|
|
1069
|
+
if ("type" in e)
|
|
1070
|
+
return {
|
|
1071
|
+
valid: !1,
|
|
1072
|
+
error: "Cannot overwrite element type via updateElement — use convertElementType instead"
|
|
1073
|
+
};
|
|
1074
|
+
for (const n of ["x", "y", "width", "height", "rotation"])
|
|
1075
|
+
if (n in e) {
|
|
1076
|
+
const s = e[n];
|
|
1077
|
+
if (typeof s != "number" || !isFinite(s))
|
|
1078
|
+
return { valid: !1, error: `Update field "${n}" must be a finite number, got: ${s}` };
|
|
1079
|
+
}
|
|
1080
|
+
return { valid: !0 };
|
|
1081
|
+
}
|
|
1082
|
+
// ── Defaults ──────────────────────────────────────────────
|
|
1083
|
+
/**
|
|
1084
|
+
* Merge custom `defaults` into the element.
|
|
1085
|
+
* Existing fields on the element take priority — defaults only fill gaps.
|
|
1086
|
+
* Only active when the element's type has a custom config with `defaults`.
|
|
1087
|
+
*/
|
|
1088
|
+
applyDefaults(e) {
|
|
1089
|
+
const n = e.type;
|
|
1090
|
+
if (!n) return e;
|
|
1091
|
+
const s = this.customs.get(n);
|
|
1092
|
+
return s?.defaults ? { ...s.defaults, ...e } : e;
|
|
1093
|
+
}
|
|
1094
|
+
// ── Private helpers ───────────────────────────────────────
|
|
1095
|
+
_validateStyle(e, n) {
|
|
1096
|
+
if (!n || typeof n != "object" || Array.isArray(n))
|
|
1097
|
+
return { valid: !1, error: `"${e}": missing or invalid style object` };
|
|
1098
|
+
const s = n;
|
|
1099
|
+
return typeof s.strokeColor != "string" ? { valid: !1, error: `"${e}": style.strokeColor must be a string` } : typeof s.fillColor != "string" ? { valid: !1, error: `"${e}": style.fillColor must be a string` } : typeof s.strokeWidth != "number" || !isFinite(s.strokeWidth) || s.strokeWidth < 0 ? { valid: !1, error: `"${e}": style.strokeWidth must be a non-negative number` } : typeof s.opacity != "number" || !isFinite(s.opacity) || s.opacity < 0 || s.opacity > 1 ? { valid: !1, error: `"${e}": style.opacity must be between 0 and 1` } : typeof s.fontSize != "number" || !isFinite(s.fontSize) || s.fontSize <= 0 ? { valid: !1, error: `"${e}": style.fontSize must be a positive number` } : { valid: !0 };
|
|
1100
|
+
}
|
|
1101
|
+
_validateBuiltinFields(e) {
|
|
1102
|
+
const n = e.type, s = e.id;
|
|
1103
|
+
switch (n) {
|
|
1104
|
+
case "rectangle":
|
|
1105
|
+
if (typeof e.cornerRadius != "number" || !isFinite(e.cornerRadius) || e.cornerRadius < 0)
|
|
1106
|
+
return {
|
|
1107
|
+
valid: !1,
|
|
1108
|
+
error: `rectangle "${s}": cornerRadius must be a non-negative number`
|
|
1109
|
+
};
|
|
1110
|
+
break;
|
|
1111
|
+
case "line":
|
|
1112
|
+
case "arrow": {
|
|
1113
|
+
if (!Array.isArray(e.points) || e.points.length < 4 || e.points.some((r) => typeof r != "number" || !isFinite(r)))
|
|
1114
|
+
return {
|
|
1115
|
+
valid: !1,
|
|
1116
|
+
error: `${n} "${s}": points must be an array of at least 4 finite numbers [x1,y1,x2,y2,...]`
|
|
1117
|
+
};
|
|
1118
|
+
if (typeof e.lineType != "string" || !Jt.has(e.lineType))
|
|
1119
|
+
return {
|
|
1120
|
+
valid: !1,
|
|
1121
|
+
error: `${n} "${s}": lineType must be one of ${[...Jt].join(" | ")}`
|
|
1122
|
+
};
|
|
1123
|
+
break;
|
|
1124
|
+
}
|
|
1125
|
+
case "freedraw": {
|
|
1126
|
+
if (!Array.isArray(e.points) || e.points.length < 2 || e.points.some((r) => typeof r != "number" || !isFinite(r)))
|
|
1127
|
+
return {
|
|
1128
|
+
valid: !1,
|
|
1129
|
+
error: `freedraw "${s}": points must be an array of at least 2 finite numbers`
|
|
1130
|
+
};
|
|
1131
|
+
break;
|
|
1132
|
+
}
|
|
1133
|
+
case "text": {
|
|
1134
|
+
if (typeof e.text != "string")
|
|
1135
|
+
return { valid: !1, error: `text "${s}": text must be a string` };
|
|
1136
|
+
if (!$t.has(e.textAlign))
|
|
1137
|
+
return {
|
|
1138
|
+
valid: !1,
|
|
1139
|
+
error: `text "${s}": textAlign must be one of ${[...$t].join(" | ")}`
|
|
1140
|
+
};
|
|
1141
|
+
if (!Ut.has(e.verticalAlign))
|
|
1142
|
+
return {
|
|
1143
|
+
valid: !1,
|
|
1144
|
+
error: `text "${s}": verticalAlign must be one of ${[...Ut].join(" | ")}`
|
|
1145
|
+
};
|
|
1146
|
+
break;
|
|
1147
|
+
}
|
|
1148
|
+
case "image": {
|
|
1149
|
+
if (typeof e.src != "string" || e.src.trim() === "")
|
|
1150
|
+
return { valid: !1, error: `image "${s}": src must be a non-empty string` };
|
|
1151
|
+
if (!Ht.has(e.scaleMode))
|
|
1152
|
+
return {
|
|
1153
|
+
valid: !1,
|
|
1154
|
+
error: `image "${s}": scaleMode must be one of ${[...Ht].join(" | ")}`
|
|
1155
|
+
};
|
|
1156
|
+
break;
|
|
1157
|
+
}
|
|
1158
|
+
}
|
|
1159
|
+
return { valid: !0 };
|
|
1160
|
+
}
|
|
1161
|
+
}
|
|
1162
|
+
const V = new Tn();
|
|
1163
|
+
function On(t) {
|
|
1164
|
+
const e = { ...t };
|
|
1165
|
+
return e.style && (e.style = { ...e.style }), e.boundElements && (e.boundElements = e.boundElements.map((n) => ({ ...n }))), e.startBinding && (e.startBinding = { ...e.startBinding }), e.endBinding && (e.endBinding = { ...e.endBinding }), e.groupIds && (e.groupIds = [...e.groupIds]), Array.isArray(e.points) && (e.points = [...e.points]), Array.isArray(e.pressures) && (e.pressures = [...e.pressures]), e.crop && (e.crop = { ...e.crop }), e;
|
|
1166
|
+
}
|
|
1167
|
+
function Pn(t, e, n = 20) {
|
|
1168
|
+
const s = new Set(t.map((f) => f.id)), r = /* @__PURE__ */ new Set();
|
|
1169
|
+
for (const f of t)
|
|
1170
|
+
if (f.boundElements)
|
|
1171
|
+
for (const u of f.boundElements)
|
|
1172
|
+
u.type === "text" && !s.has(u.id) && r.add(u.id);
|
|
1173
|
+
const i = [
|
|
1174
|
+
...t,
|
|
1175
|
+
...e.filter((f) => r.has(f.id))
|
|
1176
|
+
], o = /* @__PURE__ */ new Map();
|
|
1177
|
+
for (const f of i)
|
|
1178
|
+
o.set(f.id, vt());
|
|
1179
|
+
const c = /* @__PURE__ */ new Map();
|
|
1180
|
+
for (const f of i)
|
|
1181
|
+
if (f.groupIds)
|
|
1182
|
+
for (const u of f.groupIds)
|
|
1183
|
+
c.has(u) || c.set(u, vt());
|
|
1184
|
+
const a = i.map((f) => {
|
|
1185
|
+
const u = o.get(f.id), d = On(f);
|
|
1186
|
+
return d.id = u, d.x = f.x + n, d.y = f.y + n, d.containerId && o.has(d.containerId) ? d.containerId = o.get(d.containerId) : d.containerId && (d.containerId = null), d.boundElements && (d.boundElements = d.boundElements.map(
|
|
1187
|
+
(m) => o.has(m.id) ? { ...m, id: o.get(m.id) } : null
|
|
1188
|
+
).filter(Boolean), d.boundElements.length === 0 && (d.boundElements = null)), d.startBinding && o.has(d.startBinding.elementId) ? d.startBinding = { ...d.startBinding, elementId: o.get(d.startBinding.elementId) } : d.startBinding && (d.startBinding = null), d.endBinding && o.has(d.endBinding.elementId) ? d.endBinding = { ...d.endBinding, elementId: o.get(d.endBinding.elementId) } : d.endBinding && (d.endBinding = null), d.groupIds && d.groupIds.length > 0 && (d.groupIds = d.groupIds.map((m) => c.get(m) ?? m)), d;
|
|
1189
|
+
}), l = a.filter((f) => {
|
|
1190
|
+
const u = [...o.entries()].find(([, d]) => d === f.id)?.[0];
|
|
1191
|
+
return u ? s.has(u) : !1;
|
|
1192
|
+
}).map((f) => f.id);
|
|
1193
|
+
return { clones: a, idMap: o, selectedCloneIds: l };
|
|
1194
|
+
}
|
|
1195
|
+
function Bn(t) {
|
|
1196
|
+
if ((t.type === "line" || t.type === "arrow") && "points" in t) {
|
|
1197
|
+
const e = t.points;
|
|
1198
|
+
let n = 0, s = 0, r = 0, i = 0;
|
|
1199
|
+
for (let o = 0; o < e.length; o += 2) {
|
|
1200
|
+
const c = e[o], a = e[o + 1];
|
|
1201
|
+
c < n && (n = c), c > s && (s = c), a < r && (r = a), a > i && (i = a);
|
|
1202
|
+
}
|
|
1203
|
+
return {
|
|
1204
|
+
minX: t.x + n,
|
|
1205
|
+
minY: t.y + r,
|
|
1206
|
+
maxX: t.x + s,
|
|
1207
|
+
maxY: t.y + i
|
|
1208
|
+
};
|
|
1209
|
+
}
|
|
1210
|
+
if (t.type === "freedraw" && "points" in t) {
|
|
1211
|
+
if (t.isComplete === !1) {
|
|
1212
|
+
const e = t.points;
|
|
1213
|
+
let n = 1 / 0, s = -1 / 0, r = 1 / 0, i = -1 / 0;
|
|
1214
|
+
for (let o = 0; o < e.length; o += 2) {
|
|
1215
|
+
const c = e[o], a = e[o + 1];
|
|
1216
|
+
c < n && (n = c), c > s && (s = c), a < r && (r = a), a > i && (i = a);
|
|
1217
|
+
}
|
|
1218
|
+
return n === 1 / 0 ? { minX: t.x, minY: t.y, maxX: t.x + 1, maxY: t.y + 1 } : { minX: n, minY: r, maxX: s, maxY: i };
|
|
1219
|
+
}
|
|
1220
|
+
return {
|
|
1221
|
+
minX: t.x,
|
|
1222
|
+
minY: t.y,
|
|
1223
|
+
maxX: t.x + t.width,
|
|
1224
|
+
maxY: t.y + t.height
|
|
1225
|
+
};
|
|
1226
|
+
}
|
|
1227
|
+
return {
|
|
1228
|
+
minX: t.x,
|
|
1229
|
+
minY: t.y,
|
|
1230
|
+
maxX: t.x + t.width,
|
|
1231
|
+
maxY: t.y + t.height
|
|
1232
|
+
};
|
|
1233
|
+
}
|
|
1234
|
+
const ct = [0.1, 0.25, 0.33, 0.5, 0.67, 0.75, 1, 1.25, 1.5, 1.75, 2, 2.5, 3, 4, 5], Rn = 280;
|
|
1235
|
+
function jt({ viewport: t, point: e, targetScale: n }) {
|
|
1236
|
+
const s = Math.min(Et, Math.max(At, n)), r = (e.x - t.x) / t.scale, i = (e.y - t.y) / t.scale;
|
|
1237
|
+
return {
|
|
1238
|
+
scale: s,
|
|
1239
|
+
x: e.x - r * s,
|
|
1240
|
+
y: e.y - i * s
|
|
1241
|
+
};
|
|
1242
|
+
}
|
|
1243
|
+
function Cn(t) {
|
|
1244
|
+
for (const e of ct)
|
|
1245
|
+
if (e > t + 0.01) return e;
|
|
1246
|
+
return Et;
|
|
1247
|
+
}
|
|
1248
|
+
function Yn(t) {
|
|
1249
|
+
for (let e = ct.length - 1; e >= 0; e--)
|
|
1250
|
+
if (ct[e] < t - 0.01) return ct[e];
|
|
1251
|
+
return At;
|
|
1252
|
+
}
|
|
1253
|
+
function Wt(t) {
|
|
1254
|
+
if (t.length === 0) return null;
|
|
1255
|
+
let e = 1 / 0, n = 1 / 0, s = -1 / 0, r = -1 / 0;
|
|
1256
|
+
for (const i of t) {
|
|
1257
|
+
const o = Bn(i);
|
|
1258
|
+
o.minX < e && (e = o.minX), o.minY < n && (n = o.minY), o.maxX > s && (s = o.maxX), o.maxY > r && (r = o.maxY);
|
|
1259
|
+
}
|
|
1260
|
+
return { minX: e, minY: n, maxX: s, maxY: r };
|
|
1261
|
+
}
|
|
1262
|
+
function zt(t, e, n, s = {}) {
|
|
1263
|
+
const r = s.padding ?? 50, i = s.maxZoom ?? 2, o = t.maxX - t.minX, c = t.maxY - t.minY;
|
|
1264
|
+
if (o === 0 && c === 0)
|
|
1265
|
+
return {
|
|
1266
|
+
scale: 1,
|
|
1267
|
+
x: e / 2 - t.minX,
|
|
1268
|
+
y: n / 2 - t.minY
|
|
1269
|
+
};
|
|
1270
|
+
const a = (e - r * 2) / (o || 1), l = (n - r * 2) / (c || 1), f = Math.min(Math.min(a, l), i), u = Math.min(Et, Math.max(At, f)), d = (t.minX + t.maxX) / 2, m = (t.minY + t.maxY) / 2;
|
|
1271
|
+
return {
|
|
1272
|
+
scale: u,
|
|
1273
|
+
x: e / 2 - d * u,
|
|
1274
|
+
y: n / 2 - m * u
|
|
1275
|
+
};
|
|
1276
|
+
}
|
|
1277
|
+
function Ln(t) {
|
|
1278
|
+
return 1 - Math.pow(1 - t, 3);
|
|
1279
|
+
}
|
|
1280
|
+
let P = null;
|
|
1281
|
+
function Z(t, e, n, s = Rn) {
|
|
1282
|
+
P !== null && (cancelAnimationFrame(P), P = null);
|
|
1283
|
+
const r = performance.now(), i = (o) => {
|
|
1284
|
+
const c = o - r, a = Math.min(c / s, 1), l = Ln(a), f = {
|
|
1285
|
+
x: t.x + (e.x - t.x) * l,
|
|
1286
|
+
y: t.y + (e.y - t.y) * l,
|
|
1287
|
+
scale: t.scale + (e.scale - t.scale) * l
|
|
1288
|
+
};
|
|
1289
|
+
n(f), a < 1 ? P = requestAnimationFrame(i) : (P = null, n(e));
|
|
1290
|
+
};
|
|
1291
|
+
return P = requestAnimationFrame(i), () => {
|
|
1292
|
+
P !== null && (cancelAnimationFrame(P), P = null);
|
|
1293
|
+
};
|
|
1294
|
+
}
|
|
1295
|
+
function U() {
|
|
1296
|
+
P !== null && (cancelAnimationFrame(P), P = null);
|
|
1297
|
+
}
|
|
1298
|
+
const Gt = /* @__PURE__ */ new Set([
|
|
1299
|
+
"x",
|
|
1300
|
+
"y",
|
|
1301
|
+
"width",
|
|
1302
|
+
"height",
|
|
1303
|
+
"rotation",
|
|
1304
|
+
"ports",
|
|
1305
|
+
"points",
|
|
1306
|
+
"cornerRadius",
|
|
1307
|
+
"radiusX",
|
|
1308
|
+
"radiusY"
|
|
1309
|
+
]);
|
|
1310
|
+
function it(t, e) {
|
|
1311
|
+
const n = new Set(t);
|
|
1312
|
+
for (const s of e)
|
|
1313
|
+
s.type === "text" && s.containerId && n.has(s.containerId) && n.add(s.id);
|
|
1314
|
+
return n;
|
|
1315
|
+
}
|
|
1316
|
+
const Xn = 100;
|
|
1317
|
+
function M(t) {
|
|
1318
|
+
if (t.type === "image" && "src" in t) {
|
|
1319
|
+
const { src: e, ...n } = t, s = structuredClone(n);
|
|
1320
|
+
return s.src = e, s;
|
|
1321
|
+
}
|
|
1322
|
+
return structuredClone(t);
|
|
1323
|
+
}
|
|
1324
|
+
function ot(t) {
|
|
1325
|
+
return t.map((e) => e.id);
|
|
1326
|
+
}
|
|
1327
|
+
function Mn(t, e) {
|
|
1328
|
+
return t.length === e.length && t.every((n, s) => n === e[s]);
|
|
1329
|
+
}
|
|
1330
|
+
function Vt(t, e) {
|
|
1331
|
+
const n = new Map(t.map((r) => [r.id, r])), s = [];
|
|
1332
|
+
for (const r of e) {
|
|
1333
|
+
const i = n.get(r);
|
|
1334
|
+
i && (s.push(i), n.delete(r));
|
|
1335
|
+
}
|
|
1336
|
+
for (const r of t)
|
|
1337
|
+
n.has(r.id) && s.push(r);
|
|
1338
|
+
return s;
|
|
1339
|
+
}
|
|
1340
|
+
function Zt(t, e) {
|
|
1341
|
+
const n = new Set(e.map((s) => s.id));
|
|
1342
|
+
return t.filter((s) => n.has(s));
|
|
1343
|
+
}
|
|
1344
|
+
function qt(t, e) {
|
|
1345
|
+
const n = new Set(t);
|
|
1346
|
+
return e.filter((s) => n.has(s.id) && !s.isLocked);
|
|
1347
|
+
}
|
|
1348
|
+
function It(t) {
|
|
1349
|
+
if ((t.type === "line" || t.type === "arrow" || t.type === "freedraw") && t.points.length >= 2) {
|
|
1350
|
+
let e = 1 / 0, n = 1 / 0, s = -1 / 0, r = -1 / 0;
|
|
1351
|
+
for (let i = 0; i < t.points.length; i += 2)
|
|
1352
|
+
e = Math.min(e, t.points[i]), n = Math.min(n, t.points[i + 1]), s = Math.max(s, t.points[i]), r = Math.max(r, t.points[i + 1]);
|
|
1353
|
+
return {
|
|
1354
|
+
minX: t.x + e,
|
|
1355
|
+
minY: t.y + n,
|
|
1356
|
+
maxX: t.x + s,
|
|
1357
|
+
maxY: t.y + r
|
|
1358
|
+
};
|
|
1359
|
+
}
|
|
1360
|
+
return {
|
|
1361
|
+
minX: t.x,
|
|
1362
|
+
minY: t.y,
|
|
1363
|
+
maxX: t.x + t.width,
|
|
1364
|
+
maxY: t.y + t.height
|
|
1365
|
+
};
|
|
1366
|
+
}
|
|
1367
|
+
function Kt(t) {
|
|
1368
|
+
if (t.length === 0) return null;
|
|
1369
|
+
let e = 1 / 0, n = 1 / 0, s = -1 / 0, r = -1 / 0;
|
|
1370
|
+
for (const i of t) {
|
|
1371
|
+
const o = It(i);
|
|
1372
|
+
e = Math.min(e, o.minX), n = Math.min(n, o.minY), s = Math.max(s, o.maxX), r = Math.max(r, o.maxY);
|
|
1373
|
+
}
|
|
1374
|
+
return { minX: e, minY: n, maxX: s, maxY: r };
|
|
1375
|
+
}
|
|
1376
|
+
function Dn(t) {
|
|
1377
|
+
return (t % 360 + 360) % 360;
|
|
1378
|
+
}
|
|
1379
|
+
function Qt(t, e, n, s) {
|
|
1380
|
+
if (t)
|
|
1381
|
+
return t.map((r, i) => i % 2 === s ? e + n - r : r);
|
|
1382
|
+
}
|
|
1383
|
+
function yt(t, e) {
|
|
1384
|
+
const n = e(), s = An(t, n.elements);
|
|
1385
|
+
s.updates.length > 0 && n.batchUpdateElements(s.updates);
|
|
1386
|
+
}
|
|
1387
|
+
function Nn() {
|
|
1388
|
+
return _e((t, e) => ({
|
|
1389
|
+
// ─── Initial State ────────────────────────────────────────
|
|
1390
|
+
elements: [],
|
|
1391
|
+
selectedIds: [],
|
|
1392
|
+
activeTool: "select",
|
|
1393
|
+
currentStyle: { ...De },
|
|
1394
|
+
currentLineType: "sharp",
|
|
1395
|
+
currentStartArrowhead: null,
|
|
1396
|
+
currentEndArrowhead: "arrow",
|
|
1397
|
+
viewport: { x: 0, y: 0, scale: 1 },
|
|
1398
|
+
isDrawing: !1,
|
|
1399
|
+
drawStart: null,
|
|
1400
|
+
history: [],
|
|
1401
|
+
historyIndex: -1,
|
|
1402
|
+
_historyBaseline: /* @__PURE__ */ new Map(),
|
|
1403
|
+
_historyOrderBaseline: [],
|
|
1404
|
+
_historyPaused: !1,
|
|
1405
|
+
showGrid: !1,
|
|
1406
|
+
// ─── Element Actions ──────────────────────────────────────
|
|
1407
|
+
addElement: (n) => {
|
|
1408
|
+
if (!V.validateElement(n).valid)
|
|
1409
|
+
return;
|
|
1410
|
+
const r = V.applyDefaults(n);
|
|
1411
|
+
t((i) => ({
|
|
1412
|
+
elements: [...i.elements, r]
|
|
1413
|
+
})), e().pushHistory();
|
|
1414
|
+
},
|
|
1415
|
+
updateElement: (n, s) => {
|
|
1416
|
+
if (!V.validateUpdate(s).valid)
|
|
1417
|
+
return;
|
|
1418
|
+
const i = Object.keys(s).some((o) => Gt.has(o));
|
|
1419
|
+
t((o) => {
|
|
1420
|
+
const c = o.elements, a = c.findIndex((m) => m.id === n);
|
|
1421
|
+
if (a === -1) return o;
|
|
1422
|
+
const l = c[a], f = i ? { version: (l.version ?? 0) + 1 } : {}, u = { ...l, ...s, ...f };
|
|
1423
|
+
if (u === l) return o;
|
|
1424
|
+
const d = c.slice();
|
|
1425
|
+
return d[a] = u, { elements: d };
|
|
1426
|
+
});
|
|
1427
|
+
},
|
|
1428
|
+
batchUpdateElements: (n) => {
|
|
1429
|
+
if (n.length === 0) return;
|
|
1430
|
+
if (n.length === 1) {
|
|
1431
|
+
e().updateElement(n[0].id, n[0].updates);
|
|
1432
|
+
return;
|
|
1433
|
+
}
|
|
1434
|
+
const s = n.filter(({ id: r, updates: i }) => !!V.validateUpdate(i).valid);
|
|
1435
|
+
s.length !== 0 && t((r) => {
|
|
1436
|
+
const i = r.elements, o = /* @__PURE__ */ new Map();
|
|
1437
|
+
for (let a = 0; a < i.length; a++)
|
|
1438
|
+
o.set(i[a].id, a);
|
|
1439
|
+
let c = null;
|
|
1440
|
+
for (const { id: a, updates: l } of s) {
|
|
1441
|
+
const f = o.get(a);
|
|
1442
|
+
if (f === void 0) continue;
|
|
1443
|
+
const u = c ? c[f] : i[f], m = Object.keys(l).some((g) => Gt.has(g)) ? { version: (u.version ?? 0) + 1 } : {}, h = { ...u, ...l, ...m };
|
|
1444
|
+
h !== u && (c || (c = i.slice()), c[f] = h);
|
|
1445
|
+
}
|
|
1446
|
+
return c ? { elements: c } : r;
|
|
1447
|
+
});
|
|
1448
|
+
},
|
|
1449
|
+
deleteElements: (n) => {
|
|
1450
|
+
const s = new Set(n), { elements: r } = e();
|
|
1451
|
+
for (const i of r)
|
|
1452
|
+
if (s.has(i.id) && i.boundElements)
|
|
1453
|
+
for (const o of i.boundElements)
|
|
1454
|
+
o.type === "text" && s.add(o.id);
|
|
1455
|
+
for (const i of r)
|
|
1456
|
+
i.type === "text" && "containerId" in i && i.containerId && s.has(i.containerId) && s.add(i.id);
|
|
1457
|
+
t((i) => {
|
|
1458
|
+
const o = i.elements.filter((a) => !s.has(a.id));
|
|
1459
|
+
return {
|
|
1460
|
+
elements: bn(s, o),
|
|
1461
|
+
selectedIds: i.selectedIds.filter((a) => !s.has(a))
|
|
1462
|
+
};
|
|
1463
|
+
}), e().pushHistory();
|
|
1464
|
+
},
|
|
1465
|
+
setElements: (n) => {
|
|
1466
|
+
if (n === e().elements) return;
|
|
1467
|
+
const s = n.filter((i) => {
|
|
1468
|
+
const o = V.validateElement(i);
|
|
1469
|
+
return o.valid, o.valid;
|
|
1470
|
+
}), r = /* @__PURE__ */ new Map();
|
|
1471
|
+
for (const i of s)
|
|
1472
|
+
r.set(i.id, i);
|
|
1473
|
+
t({ elements: s, _historyBaseline: r, _historyOrderBaseline: ot(s) });
|
|
1474
|
+
},
|
|
1475
|
+
duplicateElements: (n) => {
|
|
1476
|
+
const { elements: s } = e(), r = s.filter((c) => n.includes(c.id)), { clones: i, selectedCloneIds: o } = Pn(r, s);
|
|
1477
|
+
t((c) => ({
|
|
1478
|
+
elements: [...c.elements, ...i],
|
|
1479
|
+
selectedIds: o.length > 0 ? o : i.map((a) => a.id)
|
|
1480
|
+
})), e().pushHistory();
|
|
1481
|
+
},
|
|
1482
|
+
convertElementType: (n, s) => {
|
|
1483
|
+
const r = /* @__PURE__ */ new Set(["rectangle", "ellipse", "diamond"]);
|
|
1484
|
+
r.has(s) && (t((i) => ({
|
|
1485
|
+
elements: i.elements.map((o) => {
|
|
1486
|
+
if (!n.includes(o.id) || !r.has(o.type) || o.type === s) return o;
|
|
1487
|
+
const { cornerRadius: c, ...a } = o, l = { ...a, type: s };
|
|
1488
|
+
return s === "rectangle" ? { ...l, cornerRadius: 0 } : l;
|
|
1489
|
+
})
|
|
1490
|
+
})), e().pushHistory());
|
|
1491
|
+
},
|
|
1492
|
+
bringToFront: (n) => {
|
|
1493
|
+
t((s) => {
|
|
1494
|
+
const r = it(n, s.elements), i = s.elements.filter((c) => !r.has(c.id)), o = s.elements.filter((c) => r.has(c.id));
|
|
1495
|
+
return { elements: [...i, ...o] };
|
|
1496
|
+
}), e().pushHistory();
|
|
1497
|
+
},
|
|
1498
|
+
sendToBack: (n) => {
|
|
1499
|
+
t((s) => {
|
|
1500
|
+
const r = it(n, s.elements), i = s.elements.filter((c) => !r.has(c.id));
|
|
1501
|
+
return { elements: [...s.elements.filter((c) => r.has(c.id)), ...i] };
|
|
1502
|
+
}), e().pushHistory();
|
|
1503
|
+
},
|
|
1504
|
+
bringForward: (n) => {
|
|
1505
|
+
t((s) => {
|
|
1506
|
+
const r = [...s.elements], i = it(n, s.elements);
|
|
1507
|
+
for (let o = r.length - 2; o >= 0; o--)
|
|
1508
|
+
i.has(r[o].id) && !i.has(r[o + 1].id) && ([r[o], r[o + 1]] = [r[o + 1], r[o]]);
|
|
1509
|
+
return { elements: r };
|
|
1510
|
+
}), e().pushHistory();
|
|
1511
|
+
},
|
|
1512
|
+
sendBackward: (n) => {
|
|
1513
|
+
t((s) => {
|
|
1514
|
+
const r = [...s.elements], i = it(n, s.elements);
|
|
1515
|
+
for (let o = 1; o < r.length; o++)
|
|
1516
|
+
i.has(r[o].id) && !i.has(r[o - 1].id) && ([r[o], r[o - 1]] = [r[o - 1], r[o]]);
|
|
1517
|
+
return { elements: r };
|
|
1518
|
+
}), e().pushHistory();
|
|
1519
|
+
},
|
|
1520
|
+
// ─── Transforms ──────────────────────────────────────────
|
|
1521
|
+
alignElements: (n, s) => {
|
|
1522
|
+
const r = [];
|
|
1523
|
+
t((i) => {
|
|
1524
|
+
const o = qt(n, i.elements);
|
|
1525
|
+
if (o.length < 2) return i;
|
|
1526
|
+
const c = Kt(o);
|
|
1527
|
+
if (!c) return i;
|
|
1528
|
+
const a = (c.minX + c.maxX) / 2, l = (c.minY + c.maxY) / 2, f = new Set(o.map((h) => h.id)), u = new Map(o.map((h) => [h.id, It(h)]));
|
|
1529
|
+
let d = !1;
|
|
1530
|
+
const m = i.elements.map((h) => {
|
|
1531
|
+
if (!f.has(h.id)) return h;
|
|
1532
|
+
const g = u.get(h.id);
|
|
1533
|
+
if (!g) return h;
|
|
1534
|
+
let p = h.x, x = h.y;
|
|
1535
|
+
return s === "left" && (p += c.minX - g.minX), s === "centerH" && (p += a - (g.minX + g.maxX) / 2), s === "right" && (p += c.maxX - g.maxX), s === "top" && (x += c.minY - g.minY), s === "centerV" && (x += l - (g.minY + g.maxY) / 2), s === "bottom" && (x += c.maxY - g.maxY), p === h.x && x === h.y ? h : (d = !0, r.push(h.id), { ...h, x: p, y: x, version: (h.version ?? 0) + 1 });
|
|
1536
|
+
});
|
|
1537
|
+
return d ? { elements: m } : i;
|
|
1538
|
+
}), r.length > 0 && (yt(r, e), e().pushHistory());
|
|
1539
|
+
},
|
|
1540
|
+
rotateElements: (n, s) => {
|
|
1541
|
+
const r = [];
|
|
1542
|
+
t((i) => {
|
|
1543
|
+
const o = new Set(n);
|
|
1544
|
+
let c = !1;
|
|
1545
|
+
const a = i.elements.map((l) => {
|
|
1546
|
+
if (!o.has(l.id) || l.isLocked) return l;
|
|
1547
|
+
const f = Dn((l.rotation ?? 0) + s);
|
|
1548
|
+
return f === l.rotation ? l : (c = !0, r.push(l.id), { ...l, rotation: f, version: (l.version ?? 0) + 1 });
|
|
1549
|
+
});
|
|
1550
|
+
return c ? { elements: a } : i;
|
|
1551
|
+
}), r.length > 0 && (yt(r, e), e().pushHistory());
|
|
1552
|
+
},
|
|
1553
|
+
flipElements: (n, s) => {
|
|
1554
|
+
const r = [];
|
|
1555
|
+
t((i) => {
|
|
1556
|
+
const o = qt(n, i.elements);
|
|
1557
|
+
if (o.length === 0) return i;
|
|
1558
|
+
const c = Kt(o);
|
|
1559
|
+
if (!c) return i;
|
|
1560
|
+
const a = new Set(o.map((d) => d.id)), l = new Map(o.map((d) => [d.id, It(d)]));
|
|
1561
|
+
let f = !1;
|
|
1562
|
+
const u = i.elements.map((d) => {
|
|
1563
|
+
if (!a.has(d.id)) return d;
|
|
1564
|
+
const m = l.get(d.id);
|
|
1565
|
+
if (!m) return d;
|
|
1566
|
+
const h = {};
|
|
1567
|
+
return s === "horizontal" ? (h.x = d.x + c.minX + c.maxX - m.minX - m.maxX, (d.type === "line" || d.type === "arrow" || d.type === "freedraw") && (h.points = Qt(d.points, m.minX - d.x, m.maxX - d.x, 0))) : (h.y = d.y + c.minY + c.maxY - m.minY - m.maxY, (d.type === "line" || d.type === "arrow" || d.type === "freedraw") && (h.points = Qt(d.points, m.minY - d.y, m.maxY - d.y, 1))), f = !0, r.push(d.id), { ...d, ...h, version: (d.version ?? 0) + 1 };
|
|
1568
|
+
});
|
|
1569
|
+
return f ? { elements: u } : i;
|
|
1570
|
+
}), r.length > 0 && (yt(r, e), e().pushHistory());
|
|
1571
|
+
},
|
|
1572
|
+
// ─── Lock ─────────────────────────────────────────────────
|
|
1573
|
+
toggleLockElements: (n) => {
|
|
1574
|
+
t((s) => ({
|
|
1575
|
+
elements: s.elements.map(
|
|
1576
|
+
(r) => n.includes(r.id) ? { ...r, isLocked: !r.isLocked } : r
|
|
1577
|
+
)
|
|
1578
|
+
})), e().pushHistory();
|
|
1579
|
+
},
|
|
1580
|
+
// ─── Grouping ─────────────────────────────────────────────
|
|
1581
|
+
groupElements: (n) => {
|
|
1582
|
+
if (n.length < 2) return;
|
|
1583
|
+
const s = vt(), r = new Set(n), { elements: i } = e();
|
|
1584
|
+
for (const o of i)
|
|
1585
|
+
if (r.has(o.id) && o.boundElements)
|
|
1586
|
+
for (const c of o.boundElements)
|
|
1587
|
+
c.type === "text" && r.add(c.id);
|
|
1588
|
+
t((o) => ({
|
|
1589
|
+
elements: o.elements.map(
|
|
1590
|
+
(c) => r.has(c.id) ? { ...c, groupIds: [...c.groupIds ?? [], s] } : c
|
|
1591
|
+
)
|
|
1592
|
+
})), e().pushHistory();
|
|
1593
|
+
},
|
|
1594
|
+
ungroupElements: (n) => {
|
|
1595
|
+
const { elements: s } = e(), r = s.filter((o) => n.includes(o.id)), i = /* @__PURE__ */ new Set();
|
|
1596
|
+
for (const o of r)
|
|
1597
|
+
o.groupIds?.length && i.add(o.groupIds[o.groupIds.length - 1]);
|
|
1598
|
+
i.size !== 0 && (t((o) => ({
|
|
1599
|
+
elements: o.elements.map((c) => {
|
|
1600
|
+
if (!c.groupIds?.length) return c;
|
|
1601
|
+
const a = c.groupIds.filter((l) => !i.has(l));
|
|
1602
|
+
return {
|
|
1603
|
+
...c,
|
|
1604
|
+
groupIds: a.length > 0 ? a : void 0
|
|
1605
|
+
};
|
|
1606
|
+
})
|
|
1607
|
+
})), e().pushHistory());
|
|
1608
|
+
},
|
|
1609
|
+
// ─── Selection ────────────────────────────────────────────
|
|
1610
|
+
setSelectedIds: (n) => t({ selectedIds: n }),
|
|
1611
|
+
clearSelection: () => t({ selectedIds: [] }),
|
|
1612
|
+
// ─── Tool ─────────────────────────────────────────────────
|
|
1613
|
+
setActiveTool: (n) => t((s) => ({
|
|
1614
|
+
activeTool: n,
|
|
1615
|
+
// Keep selection when switching back to 'select' (e.g. after creating an element)
|
|
1616
|
+
selectedIds: n === "select" ? s.selectedIds : []
|
|
1617
|
+
})),
|
|
1618
|
+
setCurrentStyle: (n) => t((s) => ({
|
|
1619
|
+
currentStyle: { ...s.currentStyle, ...n }
|
|
1620
|
+
})),
|
|
1621
|
+
setCurrentLineType: (n) => t({ currentLineType: n }),
|
|
1622
|
+
setCurrentStartArrowhead: (n) => t({ currentStartArrowhead: n }),
|
|
1623
|
+
setCurrentEndArrowhead: (n) => t({ currentEndArrowhead: n }),
|
|
1624
|
+
// ─── Viewport ─────────────────────────────────────────────
|
|
1625
|
+
setViewport: (n) => {
|
|
1626
|
+
U(), t((s) => ({
|
|
1627
|
+
viewport: { ...s.viewport, ...n }
|
|
1628
|
+
}));
|
|
1629
|
+
},
|
|
1630
|
+
zoomIn: (n, s) => {
|
|
1631
|
+
const { viewport: r } = e(), i = Cn(r.scale), c = jt({ viewport: r, point: n ?? { x: 400, y: 300 }, targetScale: i });
|
|
1632
|
+
s?.animate ? Z(r, c, (a) => t((l) => ({ viewport: { ...l.viewport, ...a } }))) : (U(), t({ viewport: c }));
|
|
1633
|
+
},
|
|
1634
|
+
zoomOut: (n, s) => {
|
|
1635
|
+
const { viewport: r } = e(), i = Yn(r.scale), c = jt({ viewport: r, point: n ?? { x: 400, y: 300 }, targetScale: i });
|
|
1636
|
+
s?.animate ? Z(r, c, (a) => t((l) => ({ viewport: { ...l.viewport, ...a } }))) : (U(), t({ viewport: c }));
|
|
1637
|
+
},
|
|
1638
|
+
resetZoom: (n) => {
|
|
1639
|
+
const { viewport: s } = e(), r = { x: 0, y: 0, scale: 1 };
|
|
1640
|
+
n?.animate ? Z(s, r, (i) => t((o) => ({ viewport: { ...o.viewport, ...i } }))) : (U(), t({ viewport: r }));
|
|
1641
|
+
},
|
|
1642
|
+
zoomToFit: (n, s, r, i) => {
|
|
1643
|
+
const { elements: o, viewport: c } = e(), a = r ? o.filter((u) => r.includes(u.id)) : o, l = Wt(a);
|
|
1644
|
+
if (!l) return;
|
|
1645
|
+
const f = zt(l, n, s, {
|
|
1646
|
+
padding: i?.padding,
|
|
1647
|
+
maxZoom: i?.maxZoom
|
|
1648
|
+
});
|
|
1649
|
+
i?.animate ? Z(c, f, (u) => t((d) => ({ viewport: { ...d.viewport, ...u } }))) : (U(), t({ viewport: f }));
|
|
1650
|
+
},
|
|
1651
|
+
zoomToSelection: (n, s, r) => {
|
|
1652
|
+
const { elements: i, selectedIds: o, viewport: c } = e();
|
|
1653
|
+
if (o.length === 0) return;
|
|
1654
|
+
const a = i.filter((u) => o.includes(u.id)), l = Wt(a);
|
|
1655
|
+
if (!l) return;
|
|
1656
|
+
const f = zt(l, n, s, {
|
|
1657
|
+
padding: r?.padding ?? 80,
|
|
1658
|
+
maxZoom: r?.maxZoom ?? 2
|
|
1659
|
+
});
|
|
1660
|
+
r?.animate ? Z(c, f, (u) => t((d) => ({ viewport: { ...d.viewport, ...u } }))) : (U(), t({ viewport: f }));
|
|
1661
|
+
},
|
|
1662
|
+
// ─── Drawing ──────────────────────────────────────────────
|
|
1663
|
+
setIsDrawing: (n) => t({ isDrawing: n }),
|
|
1664
|
+
setDrawStart: (n) => t({ drawStart: n }),
|
|
1665
|
+
// ─── History (Diff-based) ───────────────────────────────────
|
|
1666
|
+
pushHistory: (n) => {
|
|
1667
|
+
const { elements: s, _historyBaseline: r, _historyOrderBaseline: i, _historyPaused: o } = e();
|
|
1668
|
+
if (o) return;
|
|
1669
|
+
const c = [], a = /* @__PURE__ */ new Map();
|
|
1670
|
+
for (const u of s)
|
|
1671
|
+
a.set(u.id, u);
|
|
1672
|
+
const l = ot(s), f = !Mn(i, l);
|
|
1673
|
+
for (const u of s) {
|
|
1674
|
+
const d = r.get(u.id);
|
|
1675
|
+
d ? d !== u && c.push({
|
|
1676
|
+
type: "modify",
|
|
1677
|
+
elementId: u.id,
|
|
1678
|
+
before: M(d),
|
|
1679
|
+
after: M(u)
|
|
1680
|
+
}) : c.push({ type: "add", elementId: u.id, after: M(u) });
|
|
1681
|
+
}
|
|
1682
|
+
for (const [u, d] of r)
|
|
1683
|
+
a.has(u) || c.push({ type: "delete", elementId: u, before: M(d) });
|
|
1684
|
+
c.length === 0 && !f || t((u) => {
|
|
1685
|
+
const d = u.history.slice(0, u.historyIndex + 1);
|
|
1686
|
+
return d.push({
|
|
1687
|
+
diffs: c,
|
|
1688
|
+
beforeOrder: f ? i : void 0,
|
|
1689
|
+
afterOrder: f ? l : void 0,
|
|
1690
|
+
mark: n,
|
|
1691
|
+
timestamp: Date.now()
|
|
1692
|
+
}), d.length > Xn && d.shift(), {
|
|
1693
|
+
history: d,
|
|
1694
|
+
historyIndex: d.length - 1,
|
|
1695
|
+
// Update baseline to current state
|
|
1696
|
+
_historyBaseline: new Map(a),
|
|
1697
|
+
_historyOrderBaseline: l
|
|
1698
|
+
};
|
|
1699
|
+
});
|
|
1700
|
+
},
|
|
1701
|
+
undo: () => {
|
|
1702
|
+
const { historyIndex: n, history: s } = e();
|
|
1703
|
+
if (n < 0) return;
|
|
1704
|
+
const r = s[n];
|
|
1705
|
+
t((i) => {
|
|
1706
|
+
let o = [...i.elements];
|
|
1707
|
+
for (let a = r.diffs.length - 1; a >= 0; a--) {
|
|
1708
|
+
const l = r.diffs[a];
|
|
1709
|
+
switch (l.type) {
|
|
1710
|
+
case "add":
|
|
1711
|
+
o = o.filter((f) => f.id !== l.elementId);
|
|
1712
|
+
break;
|
|
1713
|
+
case "modify":
|
|
1714
|
+
o = o.map(
|
|
1715
|
+
(f) => f.id === l.elementId ? M(l.before) : f
|
|
1716
|
+
);
|
|
1717
|
+
break;
|
|
1718
|
+
case "delete":
|
|
1719
|
+
o.push(M(l.before));
|
|
1720
|
+
break;
|
|
1721
|
+
}
|
|
1722
|
+
}
|
|
1723
|
+
r.beforeOrder && (o = Vt(o, r.beforeOrder));
|
|
1724
|
+
const c = /* @__PURE__ */ new Map();
|
|
1725
|
+
for (const a of o)
|
|
1726
|
+
c.set(a.id, a);
|
|
1727
|
+
return {
|
|
1728
|
+
historyIndex: n - 1,
|
|
1729
|
+
elements: o,
|
|
1730
|
+
selectedIds: Zt(i.selectedIds, o),
|
|
1731
|
+
_historyBaseline: c,
|
|
1732
|
+
_historyOrderBaseline: ot(o)
|
|
1733
|
+
};
|
|
1734
|
+
});
|
|
1735
|
+
},
|
|
1736
|
+
redo: () => {
|
|
1737
|
+
const { historyIndex: n, history: s } = e();
|
|
1738
|
+
if (n >= s.length - 1) return;
|
|
1739
|
+
const r = n + 1, i = s[r];
|
|
1740
|
+
t((o) => {
|
|
1741
|
+
let c = [...o.elements];
|
|
1742
|
+
for (const l of i.diffs)
|
|
1743
|
+
switch (l.type) {
|
|
1744
|
+
case "add":
|
|
1745
|
+
c.push(M(l.after));
|
|
1746
|
+
break;
|
|
1747
|
+
case "modify":
|
|
1748
|
+
c = c.map(
|
|
1749
|
+
(f) => f.id === l.elementId ? M(l.after) : f
|
|
1750
|
+
);
|
|
1751
|
+
break;
|
|
1752
|
+
case "delete":
|
|
1753
|
+
c = c.filter((f) => f.id !== l.elementId);
|
|
1754
|
+
break;
|
|
1755
|
+
}
|
|
1756
|
+
i.afterOrder && (c = Vt(c, i.afterOrder));
|
|
1757
|
+
const a = /* @__PURE__ */ new Map();
|
|
1758
|
+
for (const l of c)
|
|
1759
|
+
a.set(l.id, l);
|
|
1760
|
+
return {
|
|
1761
|
+
historyIndex: r,
|
|
1762
|
+
elements: c,
|
|
1763
|
+
selectedIds: Zt(o.selectedIds, c),
|
|
1764
|
+
_historyBaseline: a,
|
|
1765
|
+
_historyOrderBaseline: ot(c)
|
|
1766
|
+
};
|
|
1767
|
+
});
|
|
1768
|
+
},
|
|
1769
|
+
squashHistory: (n = 2) => {
|
|
1770
|
+
t((s) => {
|
|
1771
|
+
if (s.history.length < n) return s;
|
|
1772
|
+
const r = Math.max(0, s.history.length - n), i = s.history.slice(r), o = /* @__PURE__ */ new Map();
|
|
1773
|
+
for (const l of i)
|
|
1774
|
+
for (const f of l.diffs) {
|
|
1775
|
+
const u = o.get(f.elementId);
|
|
1776
|
+
u ? f.type === "delete" ? u.type === "add" ? o.delete(f.elementId) : o.set(f.elementId, {
|
|
1777
|
+
type: "delete",
|
|
1778
|
+
elementId: f.elementId,
|
|
1779
|
+
before: u.before
|
|
1780
|
+
}) : f.type === "modify" && o.set(f.elementId, {
|
|
1781
|
+
type: u.type === "add" ? "add" : "modify",
|
|
1782
|
+
elementId: f.elementId,
|
|
1783
|
+
before: u.before,
|
|
1784
|
+
after: f.after
|
|
1785
|
+
}) : o.set(f.elementId, { ...f });
|
|
1786
|
+
}
|
|
1787
|
+
const c = {
|
|
1788
|
+
diffs: Array.from(o.values()),
|
|
1789
|
+
beforeOrder: i.find((l) => l.beforeOrder)?.beforeOrder,
|
|
1790
|
+
afterOrder: [...i].reverse().find((l) => l.afterOrder)?.afterOrder,
|
|
1791
|
+
mark: i[i.length - 1].mark,
|
|
1792
|
+
timestamp: Date.now()
|
|
1793
|
+
}, a = [...s.history.slice(0, r), c];
|
|
1794
|
+
return {
|
|
1795
|
+
history: a,
|
|
1796
|
+
historyIndex: a.length - 1
|
|
1797
|
+
};
|
|
1798
|
+
});
|
|
1799
|
+
},
|
|
1800
|
+
pauseHistory: () => t({ _historyPaused: !0 }),
|
|
1801
|
+
resumeHistory: () => t({ _historyPaused: !1 }),
|
|
1802
|
+
canUndo: () => e().historyIndex >= 0,
|
|
1803
|
+
canRedo: () => {
|
|
1804
|
+
const { historyIndex: n, history: s } = e();
|
|
1805
|
+
return n < s.length - 1;
|
|
1806
|
+
},
|
|
1807
|
+
// ─── Grid ─────────────────────────────────────────────────
|
|
1808
|
+
toggleGrid: () => t((n) => ({ showGrid: !n.showGrid }))
|
|
1809
|
+
}));
|
|
1810
|
+
}
|
|
1811
|
+
const H = Nn(), pe = [
|
|
1812
|
+
"id",
|
|
1813
|
+
"type",
|
|
1814
|
+
"x",
|
|
1815
|
+
"y",
|
|
1816
|
+
"width",
|
|
1817
|
+
"height",
|
|
1818
|
+
"rotation",
|
|
1819
|
+
"isLocked",
|
|
1820
|
+
"isVisible",
|
|
1821
|
+
"sortOrder",
|
|
1822
|
+
"version"
|
|
1823
|
+
], kt = [
|
|
1824
|
+
"strokeColor",
|
|
1825
|
+
"fillColor",
|
|
1826
|
+
"strokeWidth",
|
|
1827
|
+
"opacity",
|
|
1828
|
+
"strokeStyle",
|
|
1829
|
+
"roughness",
|
|
1830
|
+
"fontSize",
|
|
1831
|
+
"fontFamily"
|
|
1832
|
+
];
|
|
1833
|
+
function ut(t, e) {
|
|
1834
|
+
const n = t;
|
|
1835
|
+
for (const s of pe) {
|
|
1836
|
+
const r = n[s];
|
|
1837
|
+
r !== void 0 && e.set(s, r);
|
|
1838
|
+
}
|
|
1839
|
+
if (t.style)
|
|
1840
|
+
for (const s of kt)
|
|
1841
|
+
e.set(`style.${s}`, t.style[s]);
|
|
1842
|
+
switch (t.boundElements ? e.set("boundElements", JSON.stringify(t.boundElements)) : e.set("boundElements", null), t.ports && e.set("ports", JSON.stringify(t.ports)), "lineStyle" in t && t.lineStyle && e.set("lineStyle", JSON.stringify(t.lineStyle)), t.ports && e.set("ports", JSON.stringify(t.ports)), "lineStyle" in t && t.lineStyle && e.set("lineStyle", JSON.stringify(t.lineStyle)), t.groupIds && e.set("groupIds", JSON.stringify(t.groupIds)), t.type) {
|
|
1843
|
+
case "rectangle":
|
|
1844
|
+
e.set("cornerRadius", t.cornerRadius);
|
|
1845
|
+
break;
|
|
1846
|
+
case "line":
|
|
1847
|
+
case "arrow":
|
|
1848
|
+
e.set("points", JSON.stringify(t.points)), e.set("lineType", t.lineType), t.curvature !== void 0 && e.set("curvature", t.curvature), e.set("startBinding", t.startBinding ? JSON.stringify(t.startBinding) : null), e.set("endBinding", t.endBinding ? JSON.stringify(t.endBinding) : null), t.type === "arrow" && (e.set("startArrowhead", t.startArrowhead), e.set("endArrowhead", t.endArrowhead));
|
|
1849
|
+
break;
|
|
1850
|
+
case "freedraw":
|
|
1851
|
+
e.set("points", JSON.stringify(t.points));
|
|
1852
|
+
break;
|
|
1853
|
+
case "text":
|
|
1854
|
+
e.set("text", t.text), e.set("containerId", t.containerId), e.set("textAlign", t.textAlign), e.set("verticalAlign", t.verticalAlign);
|
|
1855
|
+
break;
|
|
1856
|
+
case "image":
|
|
1857
|
+
e.set("src", t.src), e.set("naturalWidth", t.naturalWidth), e.set("naturalHeight", t.naturalHeight), e.set("scaleMode", t.scaleMode), e.set("crop", t.crop ? JSON.stringify(t.crop) : null), e.set("cornerRadius", t.cornerRadius), e.set("alt", t.alt);
|
|
1858
|
+
break;
|
|
1859
|
+
}
|
|
1860
|
+
}
|
|
1861
|
+
function j(t) {
|
|
1862
|
+
const e = t.get("type"), n = t.get("id");
|
|
1863
|
+
if (!e || !n) return null;
|
|
1864
|
+
const s = {};
|
|
1865
|
+
for (const i of kt) {
|
|
1866
|
+
const o = t.get(`style.${i}`);
|
|
1867
|
+
o !== void 0 && (s[i] = o);
|
|
1868
|
+
}
|
|
1869
|
+
const r = {
|
|
1870
|
+
id: n,
|
|
1871
|
+
type: e,
|
|
1872
|
+
x: t.get("x") ?? 0,
|
|
1873
|
+
y: t.get("y") ?? 0,
|
|
1874
|
+
width: t.get("width") ?? 100,
|
|
1875
|
+
height: t.get("height") ?? 100,
|
|
1876
|
+
rotation: t.get("rotation") ?? 0,
|
|
1877
|
+
isLocked: t.get("isLocked") ?? !1,
|
|
1878
|
+
isVisible: t.get("isVisible") ?? !0,
|
|
1879
|
+
version: t.get("version") ?? 0,
|
|
1880
|
+
style: s,
|
|
1881
|
+
boundElements: C(t.get("boundElements")) ?? null,
|
|
1882
|
+
groupIds: C(t.get("groupIds")) ?? void 0,
|
|
1883
|
+
sortOrder: t.get("sortOrder") ?? void 0,
|
|
1884
|
+
ports: C(t.get("ports")) ?? void 0
|
|
1885
|
+
};
|
|
1886
|
+
switch (e) {
|
|
1887
|
+
case "rectangle":
|
|
1888
|
+
r.cornerRadius = t.get("cornerRadius") ?? 0;
|
|
1889
|
+
break;
|
|
1890
|
+
case "line":
|
|
1891
|
+
case "arrow":
|
|
1892
|
+
r.points = C(t.get("points")) ?? [0, 0, 100, 0], r.lineType = t.get("lineType") ?? "sharp", r.curvature = t.get("curvature") ?? void 0, r.startBinding = C(t.get("startBinding")), r.endBinding = C(t.get("endBinding")), e === "arrow" && (r.startArrowhead = t.get("startArrowhead") ?? null, r.endArrowhead = t.get("endArrowhead") ?? "arrow");
|
|
1893
|
+
{
|
|
1894
|
+
const i = C(t.get("lineStyle"));
|
|
1895
|
+
i && (r.lineStyle = i);
|
|
1896
|
+
}
|
|
1897
|
+
break;
|
|
1898
|
+
case "freedraw":
|
|
1899
|
+
r.points = C(t.get("points")) ?? [];
|
|
1900
|
+
break;
|
|
1901
|
+
case "text":
|
|
1902
|
+
r.text = t.get("text") ?? "", r.containerId = t.get("containerId") ?? null, r.textAlign = t.get("textAlign") ?? "center", r.verticalAlign = t.get("verticalAlign") ?? "middle";
|
|
1903
|
+
break;
|
|
1904
|
+
case "image":
|
|
1905
|
+
r.src = t.get("src") ?? "", r.naturalWidth = t.get("naturalWidth") ?? 0, r.naturalHeight = t.get("naturalHeight") ?? 0, r.scaleMode = t.get("scaleMode") ?? "fit", r.crop = C(t.get("crop")) ?? null, r.cornerRadius = t.get("cornerRadius") ?? 0, r.alt = t.get("alt") ?? "";
|
|
1906
|
+
break;
|
|
1907
|
+
}
|
|
1908
|
+
return r;
|
|
1909
|
+
}
|
|
1910
|
+
function C(t) {
|
|
1911
|
+
if (t == null) return null;
|
|
1912
|
+
try {
|
|
1913
|
+
return JSON.parse(t);
|
|
1914
|
+
} catch {
|
|
1915
|
+
return null;
|
|
1916
|
+
}
|
|
1917
|
+
}
|
|
1918
|
+
let N = !1, F = !1, at = null, lt = null, W = null, X = [];
|
|
1919
|
+
function Fn(t = 50) {
|
|
1920
|
+
const e = oe(), n = ce();
|
|
1921
|
+
if (!e || !n)
|
|
1922
|
+
return;
|
|
1923
|
+
if (xe(), n.size > 0) {
|
|
1924
|
+
N = !0;
|
|
1925
|
+
const c = Hn(n);
|
|
1926
|
+
H.getState().setElements(c), X = c, N = !1;
|
|
1927
|
+
} else {
|
|
1928
|
+
const c = H.getState().elements;
|
|
1929
|
+
c.length > 0 && (F = !0, e.transact(() => {
|
|
1930
|
+
for (const a of c) {
|
|
1931
|
+
const l = new R.Map();
|
|
1932
|
+
ut(a, l), n.set(a.id, l);
|
|
1933
|
+
}
|
|
1934
|
+
}, "local-init"), F = !1), X = c;
|
|
1935
|
+
}
|
|
1936
|
+
const s = (c, a) => {
|
|
1937
|
+
if (a.origin === "local-sync" || a.origin === "local-init" || F) return;
|
|
1938
|
+
N = !0;
|
|
1939
|
+
const l = H.getState();
|
|
1940
|
+
let f = [...X], u = !1;
|
|
1941
|
+
for (const [d, m] of c.keys)
|
|
1942
|
+
if (m.action === "add" || m.action === "update") {
|
|
1943
|
+
const h = n.get(d);
|
|
1944
|
+
if (h) {
|
|
1945
|
+
const g = j(h);
|
|
1946
|
+
if (g) {
|
|
1947
|
+
const p = f.findIndex((x) => x.id === d);
|
|
1948
|
+
p >= 0 ? f[p] = g : f.push(g), u = !0;
|
|
1949
|
+
}
|
|
1950
|
+
}
|
|
1951
|
+
} else m.action === "delete" && (f = f.filter((h) => h.id !== d), u = !0);
|
|
1952
|
+
u && (f.sort((d, m) => d.sortOrder && m.sortOrder ? d.sortOrder < m.sortOrder ? -1 : d.sortOrder > m.sortOrder ? 1 : 0 : 0), l.setElements(f), X = f), N = !1;
|
|
1953
|
+
};
|
|
1954
|
+
let r = null;
|
|
1955
|
+
const i = /* @__PURE__ */ new Set(), o = (c) => {
|
|
1956
|
+
if (!F) {
|
|
1957
|
+
for (const a of c) {
|
|
1958
|
+
let l = a.target;
|
|
1959
|
+
for (; l && !(l instanceof R.Map && l.parent === n); )
|
|
1960
|
+
l = l.parent;
|
|
1961
|
+
if (l instanceof R.Map) {
|
|
1962
|
+
const f = l.get("id");
|
|
1963
|
+
f && i.add(f);
|
|
1964
|
+
}
|
|
1965
|
+
}
|
|
1966
|
+
r && clearTimeout(r), r = setTimeout(() => {
|
|
1967
|
+
if (i.size === 0 || F) return;
|
|
1968
|
+
N = !0;
|
|
1969
|
+
let a = [...X], l = !1;
|
|
1970
|
+
for (const f of i) {
|
|
1971
|
+
const u = n.get(f);
|
|
1972
|
+
if (!u) continue;
|
|
1973
|
+
const d = j(u);
|
|
1974
|
+
if (!d) continue;
|
|
1975
|
+
const m = a.findIndex((h) => h.id === f);
|
|
1976
|
+
m >= 0 && (a[m] = d, l = !0);
|
|
1977
|
+
}
|
|
1978
|
+
i.clear(), l && (H.getState().setElements(a), X = a), N = !1;
|
|
1979
|
+
}, 16);
|
|
1980
|
+
}
|
|
1981
|
+
};
|
|
1982
|
+
n.observe(s), n.observeDeep(o), lt = () => {
|
|
1983
|
+
n.unobserve(s), n.unobserveDeep(o), r && clearTimeout(r), i.clear();
|
|
1984
|
+
}, at = H.subscribe(
|
|
1985
|
+
(c) => {
|
|
1986
|
+
N || c.elements !== X && (W && clearTimeout(W), W = setTimeout(() => {
|
|
1987
|
+
$n(c.elements, n, e);
|
|
1988
|
+
}, t));
|
|
1989
|
+
}
|
|
1990
|
+
);
|
|
1991
|
+
}
|
|
1992
|
+
function xe() {
|
|
1993
|
+
at && (at(), at = null), lt && (lt(), lt = null), W && (clearTimeout(W), W = null), X = [];
|
|
1994
|
+
}
|
|
1995
|
+
function $n(t, e, n) {
|
|
1996
|
+
F = !0, X = t;
|
|
1997
|
+
const s = /* @__PURE__ */ new Map();
|
|
1998
|
+
for (const r of t)
|
|
1999
|
+
s.set(r.id, r);
|
|
2000
|
+
n.transact(() => {
|
|
2001
|
+
for (const [r] of e.entries())
|
|
2002
|
+
s.has(r) || e.delete(r);
|
|
2003
|
+
for (const r of t) {
|
|
2004
|
+
let i = e.get(r.id);
|
|
2005
|
+
i ? Un(r, i) : (i = new R.Map(), ut(r, i), e.set(r.id, i));
|
|
2006
|
+
}
|
|
2007
|
+
}, "local-sync"), F = !1;
|
|
2008
|
+
}
|
|
2009
|
+
function Un(t, e) {
|
|
2010
|
+
const n = t;
|
|
2011
|
+
for (const r of pe) {
|
|
2012
|
+
const i = n[r];
|
|
2013
|
+
i !== e.get(r) && e.set(r, i);
|
|
2014
|
+
}
|
|
2015
|
+
if (t.style)
|
|
2016
|
+
for (const r of kt) {
|
|
2017
|
+
const i = t.style[r];
|
|
2018
|
+
i !== e.get(`style.${r}`) && e.set(`style.${r}`, i);
|
|
2019
|
+
}
|
|
2020
|
+
const s = t.boundElements ? JSON.stringify(t.boundElements) : null;
|
|
2021
|
+
switch (s !== e.get("boundElements") && e.set("boundElements", s), t.type) {
|
|
2022
|
+
case "rectangle":
|
|
2023
|
+
t.cornerRadius !== e.get("cornerRadius") && e.set("cornerRadius", t.cornerRadius);
|
|
2024
|
+
break;
|
|
2025
|
+
case "line":
|
|
2026
|
+
case "arrow": {
|
|
2027
|
+
const i = JSON.stringify(t.points);
|
|
2028
|
+
i !== e.get("points") && e.set("points", i), t.lineType !== e.get("lineType") && e.set("lineType", t.lineType), t.curvature !== e.get("curvature") && e.set("curvature", t.curvature);
|
|
2029
|
+
const o = t.startBinding ? JSON.stringify(t.startBinding) : null;
|
|
2030
|
+
o !== e.get("startBinding") && e.set("startBinding", o);
|
|
2031
|
+
const c = t.endBinding ? JSON.stringify(t.endBinding) : null;
|
|
2032
|
+
c !== e.get("endBinding") && e.set("endBinding", c), t.type === "arrow" && (t.startArrowhead !== e.get("startArrowhead") && e.set("startArrowhead", t.startArrowhead), t.endArrowhead !== e.get("endArrowhead") && e.set("endArrowhead", t.endArrowhead));
|
|
2033
|
+
break;
|
|
2034
|
+
}
|
|
2035
|
+
case "freedraw": {
|
|
2036
|
+
const i = JSON.stringify(t.points);
|
|
2037
|
+
i !== e.get("points") && e.set("points", i);
|
|
2038
|
+
break;
|
|
2039
|
+
}
|
|
2040
|
+
case "text":
|
|
2041
|
+
t.text !== e.get("text") && e.set("text", t.text), t.containerId !== e.get("containerId") && e.set("containerId", t.containerId), t.textAlign !== e.get("textAlign") && e.set("textAlign", t.textAlign), t.verticalAlign !== e.get("verticalAlign") && e.set("verticalAlign", t.verticalAlign);
|
|
2042
|
+
break;
|
|
2043
|
+
case "image":
|
|
2044
|
+
t.src !== e.get("src") && e.set("src", t.src), t.naturalWidth !== e.get("naturalWidth") && e.set("naturalWidth", t.naturalWidth), t.naturalHeight !== e.get("naturalHeight") && e.set("naturalHeight", t.naturalHeight), t.scaleMode !== e.get("scaleMode") && e.set("scaleMode", t.scaleMode);
|
|
2045
|
+
const r = t.crop ? JSON.stringify(t.crop) : null;
|
|
2046
|
+
r !== e.get("crop") && e.set("crop", r), t.cornerRadius !== e.get("cornerRadius") && e.set("cornerRadius", t.cornerRadius), t.alt !== e.get("alt") && e.set("alt", t.alt);
|
|
2047
|
+
break;
|
|
2048
|
+
}
|
|
2049
|
+
}
|
|
2050
|
+
function Hn(t) {
|
|
2051
|
+
const e = [];
|
|
2052
|
+
for (const [, n] of t.entries()) {
|
|
2053
|
+
const s = j(n);
|
|
2054
|
+
s && e.push(s);
|
|
2055
|
+
}
|
|
2056
|
+
return e.sort((n, s) => n.sortOrder && s.sortOrder ? n.sortOrder < s.sortOrder ? -1 : n.sortOrder > s.sortOrder ? 1 : 0 : 0), e;
|
|
2057
|
+
}
|
|
2058
|
+
const Jn = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
2059
|
+
__proto__: null,
|
|
2060
|
+
startSync: Fn,
|
|
2061
|
+
stopSync: xe
|
|
2062
|
+
}, Symbol.toStringTag, { value: "Module" }));
|
|
2063
|
+
class rs {
|
|
2064
|
+
constructor() {
|
|
2065
|
+
// Provider state
|
|
2066
|
+
A(this, "_doc", null);
|
|
2067
|
+
A(this, "_provider", null);
|
|
2068
|
+
A(this, "_config", null);
|
|
2069
|
+
// Sync state
|
|
2070
|
+
A(this, "_isApplyingRemote", !1);
|
|
2071
|
+
A(this, "_isApplyingLocal", !1);
|
|
2072
|
+
A(this, "_lastElements", []);
|
|
2073
|
+
A(this, "_syncTimer", null);
|
|
2074
|
+
A(this, "_deepTimer", null);
|
|
2075
|
+
A(this, "_dirtyIds", /* @__PURE__ */ new Set());
|
|
2076
|
+
A(this, "_storeUnsub", null);
|
|
2077
|
+
A(this, "_yObserverCleanup", null);
|
|
2078
|
+
// Status listeners
|
|
2079
|
+
A(this, "_statusListeners", /* @__PURE__ */ new Set());
|
|
2080
|
+
}
|
|
2081
|
+
// ─── Provider Lifecycle ───────────────────────────────────
|
|
2082
|
+
get doc() {
|
|
2083
|
+
return this._doc;
|
|
2084
|
+
}
|
|
2085
|
+
get provider() {
|
|
2086
|
+
return this._provider;
|
|
2087
|
+
}
|
|
2088
|
+
get config() {
|
|
2089
|
+
return this._config;
|
|
2090
|
+
}
|
|
2091
|
+
get isActive() {
|
|
2092
|
+
return this._provider !== null && this._provider.wsconnected;
|
|
2093
|
+
}
|
|
2094
|
+
/**
|
|
2095
|
+
* Connect to a collaboration room.
|
|
2096
|
+
* If already connected, disconnects first.
|
|
2097
|
+
*/
|
|
2098
|
+
connect(e) {
|
|
2099
|
+
return this.dispose(), this._config = e, this._doc = new R.Doc(), this._provider = new re(
|
|
2100
|
+
e.serverUrl,
|
|
2101
|
+
e.roomName,
|
|
2102
|
+
this._doc,
|
|
2103
|
+
{
|
|
2104
|
+
connect: !0,
|
|
2105
|
+
params: e.authToken ? { token: e.authToken } : void 0
|
|
2106
|
+
}
|
|
2107
|
+
), this._provider.awareness.setLocalState({
|
|
2108
|
+
user: e.user,
|
|
2109
|
+
cursor: null,
|
|
2110
|
+
selectedIds: []
|
|
2111
|
+
}), this._provider.on("status", (n) => {
|
|
2112
|
+
const s = n.status;
|
|
2113
|
+
for (const r of this._statusListeners)
|
|
2114
|
+
r(s);
|
|
2115
|
+
}), { doc: this._doc, provider: this._provider };
|
|
2116
|
+
}
|
|
2117
|
+
/**
|
|
2118
|
+
* Get the shared Y.Map for elements.
|
|
2119
|
+
*/
|
|
2120
|
+
getYElements() {
|
|
2121
|
+
return this._doc?.getMap("elements");
|
|
2122
|
+
}
|
|
2123
|
+
// ─── Awareness ────────────────────────────────────────────
|
|
2124
|
+
updateAwareness(e) {
|
|
2125
|
+
if (!this._provider) return;
|
|
2126
|
+
const n = this._provider.awareness.getLocalState();
|
|
2127
|
+
this._provider.awareness.setLocalState({ ...n, ...e });
|
|
2128
|
+
}
|
|
2129
|
+
getRemoteAwareness() {
|
|
2130
|
+
if (!this._provider) return /* @__PURE__ */ new Map();
|
|
2131
|
+
const e = this._provider.awareness.getStates(), n = this._provider.awareness.clientID, s = /* @__PURE__ */ new Map();
|
|
2132
|
+
for (const [r, i] of e)
|
|
2133
|
+
r !== n && i && i.user && s.set(r, i);
|
|
2134
|
+
return s;
|
|
2135
|
+
}
|
|
2136
|
+
// ─── Status Listeners ─────────────────────────────────────
|
|
2137
|
+
onStatusChange(e) {
|
|
2138
|
+
return this._statusListeners.add(e), () => {
|
|
2139
|
+
this._statusListeners.delete(e);
|
|
2140
|
+
};
|
|
2141
|
+
}
|
|
2142
|
+
// ─── Sync Bridge ─────────────────────────────────────────
|
|
2143
|
+
/**
|
|
2144
|
+
* Start bidirectional sync between Yjs and the provided store.
|
|
2145
|
+
*/
|
|
2146
|
+
startSync(e, n = 50) {
|
|
2147
|
+
const s = this._doc, r = this.getYElements();
|
|
2148
|
+
if (!s || !r)
|
|
2149
|
+
return;
|
|
2150
|
+
if (this.stopSync(), r.size > 0) {
|
|
2151
|
+
this._isApplyingRemote = !0;
|
|
2152
|
+
const c = this._yMapCollectionToElements(r);
|
|
2153
|
+
e.setElements(c), this._lastElements = c, this._isApplyingRemote = !1;
|
|
2154
|
+
} else {
|
|
2155
|
+
const c = e.getState().elements;
|
|
2156
|
+
c.length > 0 && (this._isApplyingLocal = !0, s.transact(() => {
|
|
2157
|
+
for (const a of c) {
|
|
2158
|
+
const l = new R.Map();
|
|
2159
|
+
ut(a, l), r.set(a.id, l);
|
|
2160
|
+
}
|
|
2161
|
+
}, "local-init"), this._isApplyingLocal = !1), this._lastElements = c;
|
|
2162
|
+
}
|
|
2163
|
+
const i = (c, a) => {
|
|
2164
|
+
if (a.origin === "local-sync" || a.origin === "local-init" || this._isApplyingLocal) return;
|
|
2165
|
+
this._isApplyingRemote = !0;
|
|
2166
|
+
let l = [...this._lastElements], f = !1;
|
|
2167
|
+
for (const [u, d] of c.keys)
|
|
2168
|
+
if (d.action === "add" || d.action === "update") {
|
|
2169
|
+
const m = r.get(u);
|
|
2170
|
+
if (m) {
|
|
2171
|
+
const h = j(m);
|
|
2172
|
+
if (h) {
|
|
2173
|
+
const g = l.findIndex((p) => p.id === u);
|
|
2174
|
+
g >= 0 ? l[g] = h : l.push(h), f = !0;
|
|
2175
|
+
}
|
|
2176
|
+
}
|
|
2177
|
+
} else d.action === "delete" && (l = l.filter((m) => m.id !== u), f = !0);
|
|
2178
|
+
f && (l.sort((u, d) => u.sortOrder && d.sortOrder ? u.sortOrder < d.sortOrder ? -1 : u.sortOrder > d.sortOrder ? 1 : 0 : 0), e.setElements(l), this._lastElements = l), this._isApplyingRemote = !1;
|
|
2179
|
+
}, o = (c) => {
|
|
2180
|
+
if (!this._isApplyingLocal) {
|
|
2181
|
+
for (const a of c) {
|
|
2182
|
+
let l = a.target;
|
|
2183
|
+
for (; l && !(l instanceof R.Map && l.parent === r); )
|
|
2184
|
+
l = l.parent;
|
|
2185
|
+
if (l instanceof R.Map) {
|
|
2186
|
+
const f = l.get("id");
|
|
2187
|
+
f && this._dirtyIds.add(f);
|
|
2188
|
+
}
|
|
2189
|
+
}
|
|
2190
|
+
this._deepTimer && clearTimeout(this._deepTimer), this._deepTimer = setTimeout(() => {
|
|
2191
|
+
if (this._dirtyIds.size === 0 || this._isApplyingLocal) return;
|
|
2192
|
+
this._isApplyingRemote = !0;
|
|
2193
|
+
let a = [...this._lastElements], l = !1;
|
|
2194
|
+
for (const f of this._dirtyIds) {
|
|
2195
|
+
const u = r.get(f);
|
|
2196
|
+
if (!u) continue;
|
|
2197
|
+
const d = j(u);
|
|
2198
|
+
if (!d) continue;
|
|
2199
|
+
const m = a.findIndex((h) => h.id === f);
|
|
2200
|
+
m >= 0 && (a[m] = d, l = !0);
|
|
2201
|
+
}
|
|
2202
|
+
this._dirtyIds.clear(), l && (e.setElements(a), this._lastElements = a), this._isApplyingRemote = !1;
|
|
2203
|
+
}, 16);
|
|
2204
|
+
}
|
|
2205
|
+
};
|
|
2206
|
+
r.observe(i), r.observeDeep(o), this._yObserverCleanup = () => {
|
|
2207
|
+
r.unobserve(i), r.unobserveDeep(o), this._deepTimer && clearTimeout(this._deepTimer), this._dirtyIds.clear();
|
|
2208
|
+
}, this._storeUnsub = e.subscribe((c) => {
|
|
2209
|
+
this._isApplyingRemote || c.elements !== this._lastElements && (this._syncTimer && clearTimeout(this._syncTimer), this._syncTimer = setTimeout(() => {
|
|
2210
|
+
this._syncLocalToYjs(c.elements, r, s);
|
|
2211
|
+
}, n));
|
|
2212
|
+
});
|
|
2213
|
+
}
|
|
2214
|
+
stopSync() {
|
|
2215
|
+
this._storeUnsub?.(), this._storeUnsub = null, this._yObserverCleanup?.(), this._yObserverCleanup = null, this._syncTimer && (clearTimeout(this._syncTimer), this._syncTimer = null), this._lastElements = [];
|
|
2216
|
+
}
|
|
2217
|
+
// ─── Dispose ──────────────────────────────────────────────
|
|
2218
|
+
dispose() {
|
|
2219
|
+
this.stopSync(), this._provider && (this._provider.awareness.setLocalState(null), this._provider.disconnect(), this._provider.destroy(), this._provider = null), this._doc && (this._doc.destroy(), this._doc = null), this._config = null, this._statusListeners.clear();
|
|
2220
|
+
}
|
|
2221
|
+
// ─── Private ──────────────────────────────────────────────
|
|
2222
|
+
_syncLocalToYjs(e, n, s) {
|
|
2223
|
+
this._isApplyingLocal = !0, this._lastElements = e;
|
|
2224
|
+
const r = /* @__PURE__ */ new Map();
|
|
2225
|
+
for (const i of e) r.set(i.id, i);
|
|
2226
|
+
s.transact(() => {
|
|
2227
|
+
for (const [i] of n.entries())
|
|
2228
|
+
r.has(i) || n.delete(i);
|
|
2229
|
+
for (const i of e) {
|
|
2230
|
+
let o = n.get(i.id);
|
|
2231
|
+
o ? this._updateYMapFromElement(i, o) : (o = new R.Map(), ut(i, o), n.set(i.id, o));
|
|
2232
|
+
}
|
|
2233
|
+
}, "local-sync"), this._isApplyingLocal = !1;
|
|
2234
|
+
}
|
|
2235
|
+
_updateYMapFromElement(e, n) {
|
|
2236
|
+
const s = [
|
|
2237
|
+
"id",
|
|
2238
|
+
"type",
|
|
2239
|
+
"x",
|
|
2240
|
+
"y",
|
|
2241
|
+
"width",
|
|
2242
|
+
"height",
|
|
2243
|
+
"rotation",
|
|
2244
|
+
"isLocked",
|
|
2245
|
+
"isVisible",
|
|
2246
|
+
"sortOrder"
|
|
2247
|
+
], r = [
|
|
2248
|
+
"strokeColor",
|
|
2249
|
+
"fillColor",
|
|
2250
|
+
"strokeWidth",
|
|
2251
|
+
"opacity",
|
|
2252
|
+
"strokeStyle",
|
|
2253
|
+
"roughness",
|
|
2254
|
+
"fontSize",
|
|
2255
|
+
"fontFamily"
|
|
2256
|
+
], i = e;
|
|
2257
|
+
for (const c of s) {
|
|
2258
|
+
const a = i[c];
|
|
2259
|
+
a !== n.get(c) && n.set(c, a);
|
|
2260
|
+
}
|
|
2261
|
+
if (e.style)
|
|
2262
|
+
for (const c of r) {
|
|
2263
|
+
const a = e.style[c];
|
|
2264
|
+
a !== n.get(`style.${c}`) && n.set(`style.${c}`, a);
|
|
2265
|
+
}
|
|
2266
|
+
const o = e.boundElements ? JSON.stringify(e.boundElements) : null;
|
|
2267
|
+
switch (o !== n.get("boundElements") && n.set("boundElements", o), e.type) {
|
|
2268
|
+
case "rectangle":
|
|
2269
|
+
e.cornerRadius !== n.get("cornerRadius") && n.set("cornerRadius", e.cornerRadius);
|
|
2270
|
+
break;
|
|
2271
|
+
case "line":
|
|
2272
|
+
case "arrow": {
|
|
2273
|
+
const a = JSON.stringify(e.points);
|
|
2274
|
+
a !== n.get("points") && n.set("points", a), e.lineType !== n.get("lineType") && n.set("lineType", e.lineType), e.curvature !== n.get("curvature") && n.set("curvature", e.curvature);
|
|
2275
|
+
const l = e.startBinding ? JSON.stringify(e.startBinding) : null;
|
|
2276
|
+
l !== n.get("startBinding") && n.set("startBinding", l);
|
|
2277
|
+
const f = e.endBinding ? JSON.stringify(e.endBinding) : null;
|
|
2278
|
+
f !== n.get("endBinding") && n.set("endBinding", f), e.type === "arrow" && (e.startArrowhead !== n.get("startArrowhead") && n.set("startArrowhead", e.startArrowhead), e.endArrowhead !== n.get("endArrowhead") && n.set("endArrowhead", e.endArrowhead));
|
|
2279
|
+
break;
|
|
2280
|
+
}
|
|
2281
|
+
case "freedraw": {
|
|
2282
|
+
const a = JSON.stringify(e.points);
|
|
2283
|
+
a !== n.get("points") && n.set("points", a);
|
|
2284
|
+
break;
|
|
2285
|
+
}
|
|
2286
|
+
case "text":
|
|
2287
|
+
e.text !== n.get("text") && n.set("text", e.text), e.containerId !== n.get("containerId") && n.set("containerId", e.containerId), e.textAlign !== n.get("textAlign") && n.set("textAlign", e.textAlign), e.verticalAlign !== n.get("verticalAlign") && n.set("verticalAlign", e.verticalAlign);
|
|
2288
|
+
break;
|
|
2289
|
+
case "image":
|
|
2290
|
+
e.src !== n.get("src") && n.set("src", e.src), e.naturalWidth !== n.get("naturalWidth") && n.set("naturalWidth", e.naturalWidth), e.naturalHeight !== n.get("naturalHeight") && n.set("naturalHeight", e.naturalHeight), e.scaleMode !== n.get("scaleMode") && n.set("scaleMode", e.scaleMode);
|
|
2291
|
+
const c = e.crop ? JSON.stringify(e.crop) : null;
|
|
2292
|
+
c !== n.get("crop") && n.set("crop", c), e.cornerRadius !== n.get("cornerRadius") && n.set("cornerRadius", e.cornerRadius), e.alt !== n.get("alt") && n.set("alt", e.alt);
|
|
2293
|
+
break;
|
|
2294
|
+
}
|
|
2295
|
+
}
|
|
2296
|
+
_yMapCollectionToElements(e) {
|
|
2297
|
+
const n = [];
|
|
2298
|
+
for (const [, s] of e.entries()) {
|
|
2299
|
+
const r = j(s);
|
|
2300
|
+
r && n.push(r);
|
|
2301
|
+
}
|
|
2302
|
+
return n.sort((s, r) => s.sortOrder && r.sortOrder ? s.sortOrder < r.sortOrder ? -1 : s.sortOrder > r.sortOrder ? 1 : 0 : 0), n;
|
|
2303
|
+
}
|
|
2304
|
+
}
|
|
2305
|
+
function jn(t) {
|
|
2306
|
+
const e = t.indexOf(",");
|
|
2307
|
+
if (e === -1) throw new Error("Invalid data URL");
|
|
2308
|
+
const s = t.slice(5, e).endsWith(";base64"), r = t.slice(e + 1);
|
|
2309
|
+
let i;
|
|
2310
|
+
if (s) {
|
|
2311
|
+
const c = atob(r);
|
|
2312
|
+
i = new Uint8Array(c.length);
|
|
2313
|
+
for (let a = 0; a < c.length; a++)
|
|
2314
|
+
i[a] = c.charCodeAt(a);
|
|
2315
|
+
} else {
|
|
2316
|
+
const c = decodeURIComponent(r);
|
|
2317
|
+
i = new TextEncoder().encode(c);
|
|
2318
|
+
}
|
|
2319
|
+
const o = new Blob([i], { type: "application/javascript" });
|
|
2320
|
+
return URL.createObjectURL(o);
|
|
2321
|
+
}
|
|
2322
|
+
function Wn(t, e) {
|
|
2323
|
+
if (typeof Worker > "u") return null;
|
|
2324
|
+
try {
|
|
2325
|
+
const n = typeof t == "function" ? t() : t, s = n instanceof URL ? n.href : String(n);
|
|
2326
|
+
if (s.startsWith("data:")) {
|
|
2327
|
+
const r = jn(s);
|
|
2328
|
+
return new Worker(r);
|
|
2329
|
+
}
|
|
2330
|
+
return new Worker(n instanceof URL ? n : s, { type: "module" });
|
|
2331
|
+
} catch {
|
|
2332
|
+
return null;
|
|
2333
|
+
}
|
|
2334
|
+
}
|
|
2335
|
+
class is {
|
|
2336
|
+
constructor(e) {
|
|
2337
|
+
A(this, "_worker", null);
|
|
2338
|
+
A(this, "_callbacks");
|
|
2339
|
+
this._callbacks = e;
|
|
2340
|
+
}
|
|
2341
|
+
/**
|
|
2342
|
+
* Initialize and connect the sync worker.
|
|
2343
|
+
* Uses Vite's `?worker` import syntax for bundling.
|
|
2344
|
+
*/
|
|
2345
|
+
connect(e, n) {
|
|
2346
|
+
if (this.dispose(), this._worker = Wn(
|
|
2347
|
+
() => "__worker_stub__"
|
|
2348
|
+
), !this._worker) {
|
|
2349
|
+
this._callbacks.onStatus("disconnected");
|
|
2350
|
+
return;
|
|
2351
|
+
}
|
|
2352
|
+
this._worker.onmessage = (s) => {
|
|
2353
|
+
const r = s.data;
|
|
2354
|
+
switch (r.type) {
|
|
2355
|
+
case "status":
|
|
2356
|
+
this._callbacks.onStatus(r.status);
|
|
2357
|
+
break;
|
|
2358
|
+
case "remote-update":
|
|
2359
|
+
this._callbacks.onRemoteUpdate(r.elements);
|
|
2360
|
+
break;
|
|
2361
|
+
case "peers":
|
|
2362
|
+
this._callbacks.onPeers(r.peers);
|
|
2363
|
+
break;
|
|
2364
|
+
case "error":
|
|
2365
|
+
this._callbacks.onError(r.message);
|
|
2366
|
+
break;
|
|
2367
|
+
}
|
|
2368
|
+
}, this._worker.onerror = (s) => {
|
|
2369
|
+
this._callbacks.onError(`Worker error: ${s.message}`);
|
|
2370
|
+
}, this._post({ type: "connect", config: e, syncDebounceMs: n });
|
|
2371
|
+
}
|
|
2372
|
+
disconnect() {
|
|
2373
|
+
this._post({ type: "disconnect" });
|
|
2374
|
+
}
|
|
2375
|
+
sendLocalUpdate(e) {
|
|
2376
|
+
this._post({ type: "local-update", elements: e });
|
|
2377
|
+
}
|
|
2378
|
+
sendAwareness(e) {
|
|
2379
|
+
this._post({
|
|
2380
|
+
type: "awareness",
|
|
2381
|
+
cursor: e.cursor ?? null,
|
|
2382
|
+
selectedIds: e.selectedIds,
|
|
2383
|
+
activeTool: e.activeTool
|
|
2384
|
+
});
|
|
2385
|
+
}
|
|
2386
|
+
dispose() {
|
|
2387
|
+
this._worker && (this._worker.terminate(), this._worker = null);
|
|
2388
|
+
}
|
|
2389
|
+
_post(e) {
|
|
2390
|
+
this._worker?.postMessage(e);
|
|
2391
|
+
}
|
|
2392
|
+
}
|
|
2393
|
+
let gt = null;
|
|
2394
|
+
function zn() {
|
|
2395
|
+
return gt || (gt = Promise.all([
|
|
2396
|
+
Promise.resolve().then(() => Me),
|
|
2397
|
+
Promise.resolve().then(() => Jn)
|
|
2398
|
+
]).then(([t, e]) => ({ yjsProvider: t, syncBridge: e }))), gt;
|
|
2399
|
+
}
|
|
2400
|
+
function os(t) {
|
|
2401
|
+
const [e, n] = Tt("disconnected"), [s, r] = Tt([]), i = tt(t);
|
|
2402
|
+
i.current = t;
|
|
2403
|
+
const o = tt(t?.awarenessThrottleMs ?? 100), c = tt(0), a = tt(null);
|
|
2404
|
+
Ae(() => {
|
|
2405
|
+
if (!t) {
|
|
2406
|
+
const h = a.current;
|
|
2407
|
+
h && (h.yjsProvider.destroyCollaborationProvider(), h.syncBridge.stopSync()), n("disconnected"), r([]);
|
|
2408
|
+
return;
|
|
2409
|
+
}
|
|
2410
|
+
let d = !1, m = null;
|
|
2411
|
+
return zn().then((h) => {
|
|
2412
|
+
if (d) return;
|
|
2413
|
+
a.current = h;
|
|
2414
|
+
const { createCollaborationProvider: g, onStatusChange: p, getRemoteAwareness: x, updateAwareness: w } = h.yjsProvider, { startSync: b, stopSync: I } = h.syncBridge, { provider: O } = g(t);
|
|
2415
|
+
b(t.syncDebounceMs ?? 50);
|
|
2416
|
+
const y = p(n), v = () => {
|
|
2417
|
+
const S = x();
|
|
2418
|
+
r(Array.from(S.values()));
|
|
2419
|
+
};
|
|
2420
|
+
O.awareness.on("change", v);
|
|
2421
|
+
const _ = H.subscribe((S, B) => {
|
|
2422
|
+
S.selectedIds !== B.selectedIds && w({ selectedIds: S.selectedIds }), S.activeTool !== B.activeTool && w({ activeTool: S.activeTool });
|
|
2423
|
+
});
|
|
2424
|
+
m = () => {
|
|
2425
|
+
y(), _(), O.awareness.off("change", v), I(), h.yjsProvider.destroyCollaborationProvider();
|
|
2426
|
+
};
|
|
2427
|
+
}).catch((h) => {
|
|
2428
|
+
n("disconnected");
|
|
2429
|
+
}), () => {
|
|
2430
|
+
d = !0, m && m(), n("disconnected"), r([]);
|
|
2431
|
+
};
|
|
2432
|
+
}, [
|
|
2433
|
+
// Re-create if server/room/user changes
|
|
2434
|
+
t?.serverUrl,
|
|
2435
|
+
t?.roomName,
|
|
2436
|
+
t?.user.id,
|
|
2437
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
2438
|
+
t?.syncDebounceMs
|
|
2439
|
+
]);
|
|
2440
|
+
const l = ht((d) => {
|
|
2441
|
+
const m = Date.now();
|
|
2442
|
+
m - c.current < o.current || (c.current = m, a.current?.yjsProvider.updateAwareness({ cursor: d }));
|
|
2443
|
+
}, []), f = ht(() => {
|
|
2444
|
+
const d = a.current?.yjsProvider.getYProvider();
|
|
2445
|
+
d && d.disconnect();
|
|
2446
|
+
}, []), u = ht(() => {
|
|
2447
|
+
const d = a.current?.yjsProvider.getYProvider();
|
|
2448
|
+
d && d.connect();
|
|
2449
|
+
}, []);
|
|
2450
|
+
return {
|
|
2451
|
+
isConnected: e === "connected",
|
|
2452
|
+
connectionStatus: e,
|
|
2453
|
+
peers: s,
|
|
2454
|
+
disconnect: f,
|
|
2455
|
+
reconnect: u,
|
|
2456
|
+
updateCursor: l
|
|
2457
|
+
};
|
|
2458
|
+
}
|
|
2459
|
+
const Gn = [0, 0, 0, 18, 4.5, 14.5, 9, 20, 12, 18, 7.5, 12.5, 14, 12.5, 0, 0], _t = 12, we = "system-ui, -apple-system, sans-serif", te = 6, ee = 4, ne = 14, se = 18, Y = {
|
|
2460
|
+
ctx: null,
|
|
2461
|
+
cache: /* @__PURE__ */ new Map()
|
|
2462
|
+
};
|
|
2463
|
+
function Vn(t) {
|
|
2464
|
+
const e = Y.cache.get(t);
|
|
2465
|
+
if (e !== void 0) return e;
|
|
2466
|
+
if (!Y.ctx)
|
|
2467
|
+
try {
|
|
2468
|
+
const s = document.createElement("canvas");
|
|
2469
|
+
Y.ctx = s.getContext("2d");
|
|
2470
|
+
} catch {
|
|
2471
|
+
}
|
|
2472
|
+
let n;
|
|
2473
|
+
return Y.ctx ? (Y.ctx.font = `${_t}px ${we}`, n = Y.ctx.measureText(t).width) : n = t.length * 7, Y.cache.size > 200 && Y.cache.clear(), Y.cache.set(t, n), n;
|
|
2474
|
+
}
|
|
2475
|
+
function be(t, e, n, s, r, i) {
|
|
2476
|
+
const o = (t - n.x) * n.scale, c = (e - n.y) * n.scale;
|
|
2477
|
+
return o >= -i && o <= s + i && c >= -i && c <= r + i;
|
|
2478
|
+
}
|
|
2479
|
+
function Zn(t, e, n, s, r) {
|
|
2480
|
+
const i = t.x + t.width / 2, o = t.y + t.height / 2, c = Math.sqrt(t.width * t.width + t.height * t.height) / 2;
|
|
2481
|
+
return be(i, o, e, n, s, r + c * e.scale);
|
|
2482
|
+
}
|
|
2483
|
+
const qn = ({
|
|
2484
|
+
peers: t,
|
|
2485
|
+
viewport: e,
|
|
2486
|
+
stageWidth: n,
|
|
2487
|
+
stageHeight: s,
|
|
2488
|
+
elements: r
|
|
2489
|
+
}) => {
|
|
2490
|
+
const i = Ee(() => {
|
|
2491
|
+
const a = /* @__PURE__ */ new Map();
|
|
2492
|
+
for (const l of r) a.set(l.id, l);
|
|
2493
|
+
return a;
|
|
2494
|
+
}, [r]), o = 1 / e.scale, c = 150;
|
|
2495
|
+
return /* @__PURE__ */ G(ke, { children: t.map((a) => {
|
|
2496
|
+
if (!a.cursor && a.selectedIds.length === 0) return null;
|
|
2497
|
+
const { user: l } = a;
|
|
2498
|
+
return /* @__PURE__ */ Ot(Pt, { children: [
|
|
2499
|
+
a.selectedIds.map((f) => {
|
|
2500
|
+
const u = i.get(f);
|
|
2501
|
+
if (!u || !Zn(u, e, n, s, c))
|
|
2502
|
+
return null;
|
|
2503
|
+
const d = 3 * o;
|
|
2504
|
+
return /* @__PURE__ */ G(
|
|
2505
|
+
Bt,
|
|
2506
|
+
{
|
|
2507
|
+
x: u.x + u.width / 2,
|
|
2508
|
+
y: u.y + u.height / 2,
|
|
2509
|
+
offsetX: u.width / 2 + d,
|
|
2510
|
+
offsetY: u.height / 2 + d,
|
|
2511
|
+
width: u.width + d * 2,
|
|
2512
|
+
height: u.height + d * 2,
|
|
2513
|
+
rotation: u.rotation ?? 0,
|
|
2514
|
+
stroke: l.color,
|
|
2515
|
+
strokeWidth: 2 * o,
|
|
2516
|
+
dash: [6 * o, 4 * o],
|
|
2517
|
+
cornerRadius: 3 * o,
|
|
2518
|
+
listening: !1,
|
|
2519
|
+
perfectDrawEnabled: !1
|
|
2520
|
+
},
|
|
2521
|
+
f
|
|
2522
|
+
);
|
|
2523
|
+
}),
|
|
2524
|
+
a.cursor && be(
|
|
2525
|
+
a.cursor.x,
|
|
2526
|
+
a.cursor.y,
|
|
2527
|
+
e,
|
|
2528
|
+
n,
|
|
2529
|
+
s,
|
|
2530
|
+
c
|
|
2531
|
+
) && /* @__PURE__ */ Ot(
|
|
2532
|
+
Pt,
|
|
2533
|
+
{
|
|
2534
|
+
x: a.cursor.x,
|
|
2535
|
+
y: a.cursor.y,
|
|
2536
|
+
scaleX: o,
|
|
2537
|
+
scaleY: o,
|
|
2538
|
+
listening: !1,
|
|
2539
|
+
children: [
|
|
2540
|
+
/* @__PURE__ */ G(
|
|
2541
|
+
Te,
|
|
2542
|
+
{
|
|
2543
|
+
points: Gn,
|
|
2544
|
+
fill: l.color,
|
|
2545
|
+
stroke: "#ffffff",
|
|
2546
|
+
strokeWidth: 1,
|
|
2547
|
+
closed: !0,
|
|
2548
|
+
perfectDrawEnabled: !1
|
|
2549
|
+
}
|
|
2550
|
+
),
|
|
2551
|
+
/* @__PURE__ */ G(
|
|
2552
|
+
Bt,
|
|
2553
|
+
{
|
|
2554
|
+
x: ne,
|
|
2555
|
+
y: se,
|
|
2556
|
+
width: Vn(l.name) + te * 2,
|
|
2557
|
+
height: _t + ee * 2,
|
|
2558
|
+
fill: l.color,
|
|
2559
|
+
cornerRadius: 4,
|
|
2560
|
+
listening: !1,
|
|
2561
|
+
perfectDrawEnabled: !1
|
|
2562
|
+
}
|
|
2563
|
+
),
|
|
2564
|
+
/* @__PURE__ */ G(
|
|
2565
|
+
Oe,
|
|
2566
|
+
{
|
|
2567
|
+
x: ne + te,
|
|
2568
|
+
y: se + ee,
|
|
2569
|
+
text: l.name,
|
|
2570
|
+
fontSize: _t,
|
|
2571
|
+
fontFamily: we,
|
|
2572
|
+
fill: "#ffffff",
|
|
2573
|
+
listening: !1,
|
|
2574
|
+
perfectDrawEnabled: !1
|
|
2575
|
+
}
|
|
2576
|
+
)
|
|
2577
|
+
]
|
|
2578
|
+
}
|
|
2579
|
+
)
|
|
2580
|
+
] }, l.id);
|
|
2581
|
+
}) });
|
|
2582
|
+
}, cs = Se.memo(qn);
|
|
2583
|
+
export {
|
|
2584
|
+
rs as CollaborationManager,
|
|
2585
|
+
cs as CursorOverlay,
|
|
2586
|
+
kt as STYLE_FIELDS,
|
|
2587
|
+
pe as SYNC_FIELDS,
|
|
2588
|
+
is as SyncWorkerAdapter,
|
|
2589
|
+
Pe as createCollaborationProvider,
|
|
2590
|
+
ie as destroyCollaborationProvider,
|
|
2591
|
+
ut as elementToYMap,
|
|
2592
|
+
Xe as getRemoteAwareness,
|
|
2593
|
+
oe as getYDoc,
|
|
2594
|
+
ce as getYElements,
|
|
2595
|
+
Be as getYProvider,
|
|
2596
|
+
Ce as isCollaborationActive,
|
|
2597
|
+
Ye as onStatusChange,
|
|
2598
|
+
Fn as startSync,
|
|
2599
|
+
xe as stopSync,
|
|
2600
|
+
Le as updateAwareness,
|
|
2601
|
+
os as useCollaboration,
|
|
2602
|
+
j as yMapToElement
|
|
2603
|
+
};
|