mathlean-canvas 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js ADDED
@@ -0,0 +1,2201 @@
1
+ import { useEffect as e, useLayoutEffect as t, useRef as n, useState as r } from "react";
2
+ import i from "katex";
3
+ import { Arrow as a, Circle as o, Group as s, Layer as c, Line as l, Rect as u, Stage as ee, Text as te, Transformer as ne } from "react-konva";
4
+ import { Fragment as re, jsx as d, jsxs as f } from "react/jsx-runtime";
5
+ //#region src/MathCanvas.tsx
6
+ var p = 44, m = 56, ie = 24, ae = 96, oe = 2, se = 14, ce = 6, le = 12, ue = 10, de = 14, fe = 34, pe = .9, me = 2.2, he = 56, ge = 56, _e = 48, ve = 48, ye = 160, be = 8, xe = 2, h = 8, Se = 26, Ce = 30, g = 14, we = 8, _ = 16, v = "KaTeX_Main, Computer Modern Serif, Latin Modern Roman, Times New Roman, serif", Te = 24, Ee = 18, De = 180, Oe = 280, ke = 92;
7
+ function Ae(e) {
8
+ let t = [], n = /(\$\$[\s\S]+?\$\$|\$[^$]+\$)/g, r = 0, i = n.exec(e), a = 0;
9
+ for (; i;) {
10
+ i.index > r && t.push({
11
+ id: `segment-${a++}`,
12
+ type: "text",
13
+ value: e.slice(r, i.index)
14
+ });
15
+ let o = i[0], s = o.startsWith("$$");
16
+ t.push({
17
+ id: `segment-${a++}`,
18
+ type: "math",
19
+ value: o.slice(s ? 2 : 1, s ? -2 : -1),
20
+ fallback: o,
21
+ displayMode: s
22
+ }), r = i.index + o.length, i = n.exec(e);
23
+ }
24
+ return r < e.length && t.push({
25
+ id: `segment-${a}`,
26
+ type: "text",
27
+ value: e.slice(r)
28
+ }), t;
29
+ }
30
+ function y({ expression: t, fallback: r, displayMode: a = !1 }) {
31
+ let o = n(null);
32
+ return e(() => {
33
+ let e = o.current;
34
+ if (e) {
35
+ e.textContent = "";
36
+ try {
37
+ i.render(t, e, {
38
+ displayMode: a,
39
+ throwOnError: !1,
40
+ strict: "ignore"
41
+ });
42
+ } catch {
43
+ e.textContent = r;
44
+ }
45
+ }
46
+ }, [
47
+ a,
48
+ t,
49
+ r
50
+ ]), /* @__PURE__ */ d("span", {
51
+ ref: o,
52
+ style: a ? {
53
+ display: "block",
54
+ textAlign: "left"
55
+ } : void 0
56
+ });
57
+ }
58
+ function je({ content: e, renderMode: t }) {
59
+ return t === "plain" ? /* @__PURE__ */ d(re, { children: e }) : /* @__PURE__ */ d(re, { children: Ae(e).map((e) => e.type === "math" ? /* @__PURE__ */ d(y, {
60
+ expression: e.value,
61
+ fallback: e.fallback,
62
+ displayMode: e.displayMode
63
+ }, e.id) : /* @__PURE__ */ d("span", { children: e.value }, e.id)) });
64
+ }
65
+ function Me() {
66
+ return /* @__PURE__ */ d("svg", {
67
+ width: "14",
68
+ height: "14",
69
+ viewBox: "0 0 16 16",
70
+ "aria-hidden": "true",
71
+ children: /* @__PURE__ */ d("path", {
72
+ d: "M4.5 4.5 11.5 11.5M11.5 4.5 4.5 11.5",
73
+ fill: "none",
74
+ stroke: "#b42318",
75
+ strokeWidth: "1.6",
76
+ strokeLinecap: "round"
77
+ })
78
+ });
79
+ }
80
+ function Ne({ active: e }) {
81
+ return /* @__PURE__ */ f("svg", {
82
+ width: "16",
83
+ height: "16",
84
+ viewBox: "0 0 16 16",
85
+ "aria-hidden": "true",
86
+ children: [/* @__PURE__ */ d("path", {
87
+ d: "M11.8 1.8a1.6 1.6 0 0 1 2.3 2.3l-7.9 7.9-3.3 1 1-3.3 7.9-7.9Z",
88
+ fill: "none",
89
+ stroke: e ? "#fff" : "#111",
90
+ strokeWidth: "1.4",
91
+ strokeLinecap: "round",
92
+ strokeLinejoin: "round"
93
+ }), /* @__PURE__ */ d("path", {
94
+ d: "M9.9 3.7l2.4 2.4",
95
+ fill: "none",
96
+ stroke: e ? "#fff" : "#111",
97
+ strokeWidth: "1.4",
98
+ strokeLinecap: "round"
99
+ })]
100
+ });
101
+ }
102
+ function Pe({ active: e }) {
103
+ return /* @__PURE__ */ f("svg", {
104
+ width: "16",
105
+ height: "16",
106
+ viewBox: "0 0 16 16",
107
+ "aria-hidden": "true",
108
+ children: [/* @__PURE__ */ d("path", {
109
+ d: "M6.2 2.5a1.8 1.8 0 0 1 2.5 0l4.8 4.8a1.8 1.8 0 0 1 0 2.5l-3.2 3.2H5.5L2.5 10a1.8 1.8 0 0 1 0-2.5l3.7-5Z",
110
+ fill: "none",
111
+ stroke: e ? "#fff" : "#111",
112
+ strokeWidth: "1.4",
113
+ strokeLinejoin: "round"
114
+ }), /* @__PURE__ */ d("path", {
115
+ d: "M8.4 13h5.1",
116
+ fill: "none",
117
+ stroke: e ? "#fff" : "#111",
118
+ strokeWidth: "1.4",
119
+ strokeLinecap: "round"
120
+ })]
121
+ });
122
+ }
123
+ function Fe({ active: e }) {
124
+ return /* @__PURE__ */ d("svg", {
125
+ width: "16",
126
+ height: "16",
127
+ viewBox: "0 0 16 16",
128
+ "aria-hidden": "true",
129
+ children: /* @__PURE__ */ d("path", {
130
+ d: "M3 2.5v10.8l2.9-3.1 2.2 3.3 1.8-1.1-2.2-3.3H12L3 2.5Z",
131
+ fill: "none",
132
+ stroke: e ? "#fff" : "#111",
133
+ strokeWidth: "1.4",
134
+ strokeLinecap: "round",
135
+ strokeLinejoin: "round"
136
+ })
137
+ });
138
+ }
139
+ function Ie() {
140
+ return /* @__PURE__ */ d("svg", {
141
+ width: "16",
142
+ height: "16",
143
+ viewBox: "0 0 16 16",
144
+ "aria-hidden": "true",
145
+ children: /* @__PURE__ */ d("path", {
146
+ d: "M3.5 4.5h9M6 4.5V3.4c0-.5.4-.9.9-.9h2.2c.5 0 .9.4.9.9v1.1M5 6.5v5.2M8 6.5v5.2M11 6.5v5.2M4.4 4.5l.5 8c0 .6.5 1 1.1 1h4c.6 0 1.1-.4 1.1-1l.5-8",
147
+ fill: "none",
148
+ stroke: "#111",
149
+ strokeWidth: "1.4",
150
+ strokeLinecap: "round",
151
+ strokeLinejoin: "round"
152
+ })
153
+ });
154
+ }
155
+ function Le({ active: e }) {
156
+ return /* @__PURE__ */ d("svg", {
157
+ width: "16",
158
+ height: "16",
159
+ viewBox: "0 0 16 16",
160
+ "aria-hidden": "true",
161
+ children: /* @__PURE__ */ d("path", {
162
+ d: "M3 4h10M8 4v8M5.5 12h5",
163
+ fill: "none",
164
+ stroke: e ? "#fff" : "#111",
165
+ strokeWidth: "1.5",
166
+ strokeLinecap: "round",
167
+ strokeLinejoin: "round"
168
+ })
169
+ });
170
+ }
171
+ function Re({ expression: t, fallback: r, displayMode: a = !1 }) {
172
+ let o = n(null);
173
+ return e(() => {
174
+ let e = o.current;
175
+ if (e) {
176
+ e.textContent = "";
177
+ try {
178
+ i.render(t, e, {
179
+ displayMode: a,
180
+ throwOnError: !1,
181
+ strict: "ignore"
182
+ });
183
+ } catch {
184
+ e.textContent = r;
185
+ }
186
+ }
187
+ }, [
188
+ a,
189
+ t,
190
+ r
191
+ ]), /* @__PURE__ */ d("span", {
192
+ ref: o,
193
+ style: a ? {
194
+ display: "block",
195
+ textAlign: "left"
196
+ } : void 0
197
+ });
198
+ }
199
+ function ze(e, t) {
200
+ let n = /\$([^$]+)\$/g, r = n.exec(e);
201
+ for (; r;) {
202
+ let i = r.index, a = i + r[0].length;
203
+ if (t > i && t <= a) return {
204
+ expression: r[1],
205
+ fallback: r[1]
206
+ };
207
+ r = n.exec(e);
208
+ }
209
+ return null;
210
+ }
211
+ function Be(e, t) {
212
+ let n = window.getComputedStyle(e), r = document.createElement("div"), i = document.createElement("span");
213
+ r.style.position = "absolute", r.style.visibility = "hidden", r.style.pointerEvents = "none", r.style.whiteSpace = n.whiteSpace, r.style.wordBreak = n.wordBreak, r.style.overflowWrap = n.overflowWrap, r.style.font = n.font, r.style.fontFamily = n.fontFamily, r.style.fontSize = n.fontSize, r.style.fontWeight = n.fontWeight, r.style.fontStyle = n.fontStyle, r.style.letterSpacing = n.letterSpacing, r.style.textTransform = n.textTransform, r.style.textIndent = n.textIndent, r.style.padding = n.padding, r.style.border = n.border, r.style.boxSizing = n.boxSizing, r.style.lineHeight = n.lineHeight, r.style.width = n.width, r.style.minHeight = n.height, r.style.overflow = "hidden", r.textContent = e.value.slice(0, t), i.textContent = e.value.slice(t, t + 1) || " ", r.appendChild(i), document.body.appendChild(r);
214
+ let a = i.offsetLeft - e.scrollLeft, o = i.offsetTop - e.scrollTop;
215
+ return document.body.removeChild(r), {
216
+ left: a,
217
+ top: o
218
+ };
219
+ }
220
+ function Ve(e) {
221
+ if (typeof document > "u") return ae;
222
+ let t = document.createElement("canvas").getContext("2d");
223
+ if (!t) return ae;
224
+ t.font = `${Te}px ${v}`;
225
+ let n = Math.max(...e.split("\n").map((e) => t.measureText(e || " ").width), m);
226
+ return Math.ceil(n + oe + se + le);
227
+ }
228
+ function He({ active: e }) {
229
+ let t = e ? "#fff" : "#111";
230
+ return /* @__PURE__ */ f("svg", {
231
+ width: "16",
232
+ height: "16",
233
+ viewBox: "0 0 16 16",
234
+ "aria-hidden": "true",
235
+ children: [
236
+ /* @__PURE__ */ d("path", {
237
+ d: "M4 4.5h8M4.8 11.2 8 4.8M11.2 11.2 8 4.8",
238
+ fill: "none",
239
+ stroke: t,
240
+ strokeWidth: "1.25",
241
+ strokeLinecap: "round"
242
+ }),
243
+ /* @__PURE__ */ d("circle", {
244
+ cx: "8",
245
+ cy: "4",
246
+ r: "1.8",
247
+ fill: "none",
248
+ stroke: t,
249
+ strokeWidth: "1.25"
250
+ }),
251
+ /* @__PURE__ */ d("circle", {
252
+ cx: "4.5",
253
+ cy: "11.5",
254
+ r: "1.8",
255
+ fill: "none",
256
+ stroke: t,
257
+ strokeWidth: "1.25"
258
+ }),
259
+ /* @__PURE__ */ d("circle", {
260
+ cx: "11.5",
261
+ cy: "11.5",
262
+ r: "1.8",
263
+ fill: "none",
264
+ stroke: t,
265
+ strokeWidth: "1.25"
266
+ })
267
+ ]
268
+ });
269
+ }
270
+ function Ue({ active: e }) {
271
+ let t = e ? "#fff" : "#111";
272
+ return /* @__PURE__ */ f("svg", {
273
+ width: "16",
274
+ height: "16",
275
+ viewBox: "0 0 16 16",
276
+ "aria-hidden": "true",
277
+ children: [/* @__PURE__ */ d("path", {
278
+ d: "M4 2.5H2.8v11H4M12 2.5h1.2v11H12",
279
+ fill: "none",
280
+ stroke: t,
281
+ strokeWidth: "1.3",
282
+ strokeLinecap: "round"
283
+ }), /* @__PURE__ */ d("path", {
284
+ d: "M6 5.2h1.6M8.4 5.2H10M6 8h1.6M8.4 8H10M6 10.8h1.6M8.4 10.8H10",
285
+ fill: "none",
286
+ stroke: t,
287
+ strokeWidth: "1.3",
288
+ strokeLinecap: "round"
289
+ })]
290
+ });
291
+ }
292
+ function We({ active: e }) {
293
+ let t = e ? "#fff" : "#111";
294
+ return /* @__PURE__ */ f("svg", {
295
+ width: "16",
296
+ height: "16",
297
+ viewBox: "0 0 16 16",
298
+ "aria-hidden": "true",
299
+ children: [/* @__PURE__ */ d("path", {
300
+ d: "M3 4.5h10M3 8h5.5M9.8 8h3.2M3 11.5h10",
301
+ fill: "none",
302
+ stroke: t,
303
+ strokeWidth: "1.3",
304
+ strokeLinecap: "round"
305
+ }), /* @__PURE__ */ d("circle", {
306
+ cx: "8",
307
+ cy: "8",
308
+ r: "0.8",
309
+ fill: t
310
+ })]
311
+ });
312
+ }
313
+ function Ge({ active: e }) {
314
+ return /* @__PURE__ */ d("svg", {
315
+ width: "16",
316
+ height: "16",
317
+ viewBox: "0 0 16 16",
318
+ "aria-hidden": "true",
319
+ children: /* @__PURE__ */ d("text", {
320
+ x: "8",
321
+ y: "11.5",
322
+ textAnchor: "middle",
323
+ fontSize: "12",
324
+ fontStyle: "italic",
325
+ fontFamily: v,
326
+ fill: e ? "#fff" : "#111",
327
+ children: "x"
328
+ })
329
+ });
330
+ }
331
+ function Ke({ active: e }) {
332
+ return /* @__PURE__ */ d("svg", {
333
+ width: "16",
334
+ height: "16",
335
+ viewBox: "0 0 16 16",
336
+ "aria-hidden": "true",
337
+ children: /* @__PURE__ */ d("path", {
338
+ d: "M6 4 3 7l3 3M3.5 7H9a3.5 3.5 0 1 1 0 7H7",
339
+ fill: "none",
340
+ stroke: e ? "#fff" : "#111",
341
+ strokeWidth: "1.4",
342
+ strokeLinecap: "round",
343
+ strokeLinejoin: "round"
344
+ })
345
+ });
346
+ }
347
+ function qe({ active: e }) {
348
+ return /* @__PURE__ */ d("svg", {
349
+ width: "16",
350
+ height: "16",
351
+ viewBox: "0 0 16 16",
352
+ "aria-hidden": "true",
353
+ children: /* @__PURE__ */ d("path", {
354
+ d: "m10 4 3 3-3 3M12.5 7H7a3.5 3.5 0 1 0 0 7h2",
355
+ fill: "none",
356
+ stroke: e ? "#fff" : "#111",
357
+ strokeWidth: "1.4",
358
+ strokeLinecap: "round",
359
+ strokeLinejoin: "round"
360
+ })
361
+ });
362
+ }
363
+ function b({ active: e, disabled: t = !1, label: n, onClick: r, children: i }) {
364
+ return /* @__PURE__ */ d("button", {
365
+ type: "button",
366
+ disabled: t,
367
+ onClick: r,
368
+ "aria-label": n,
369
+ title: n,
370
+ style: {
371
+ padding: "6px 10px",
372
+ border: "1px solid #cfcfcf",
373
+ borderRadius: 6,
374
+ background: e ? "#111" : "#fff",
375
+ color: e ? "#fff" : "#111",
376
+ cursor: t ? "not-allowed" : "pointer",
377
+ opacity: t ? .45 : 1,
378
+ display: "flex",
379
+ alignItems: "center",
380
+ justifyContent: "center"
381
+ },
382
+ children: i
383
+ });
384
+ }
385
+ function x() {
386
+ let [i, Ae] = r([]), [y, x] = r([]), [Je, Ye] = r([]), [Xe, Ze] = r([]), [S, Qe] = r([]), [$e, et] = r([]), [C, tt] = r("pen"), [nt, rt] = r("plain"), [w, T] = r(null), [E, D] = r(null), [O, k] = r(null), [A, j] = r(null), [M, N] = r(null), [P, it] = r(null), [at, ot] = r(""), [st, ct] = r(0), [lt, ut] = r(null), [F, dt] = r(null), [ft, pt] = r(""), [I, mt] = r(null), [ht, L] = r(null), [R, gt] = r(null), [_t, vt] = r(""), [z, B] = r(null), [yt, bt] = r(null), [V, xt] = r({
387
+ width: window.innerWidth,
388
+ height: window.innerHeight
389
+ }), [St, Ct] = r(p), [wt, Tt] = r(ae), [Et, Dt] = r({}), [Ot, kt] = r({}), [At, jt] = r([]), [Mt, Nt] = r([]), [Pt, Ft] = r({
390
+ x: 0,
391
+ y: 0,
392
+ radius: de,
393
+ visible: !1
394
+ }), H = n(!1), U = n(!1), It = n(1), Lt = n(null), Rt = n(null), zt = n(null), Bt = n(null), Vt = n(null), Ht = n({}), Ut = n({}), Wt = n({}), Gt = n({}), Kt = n({}), qt = n({}), Jt = n(null), Yt = n(null), Xt = n({}), Zt = n(null);
395
+ e(() => {
396
+ let e = () => {
397
+ xt({
398
+ width: window.innerWidth,
399
+ height: window.innerHeight
400
+ });
401
+ };
402
+ return window.addEventListener("resize", e), () => window.removeEventListener("resize", e);
403
+ }, []), t(() => {
404
+ if (!P) return;
405
+ let e = Jt.current;
406
+ if (!e) return;
407
+ let t = window.requestAnimationFrame(() => {
408
+ e.focus();
409
+ let t = e.value.length;
410
+ e.setSelectionRange(t, t), ct(t), Ct(Math.max(e.scrollHeight, p));
411
+ });
412
+ return () => window.cancelAnimationFrame(t);
413
+ }, [P]), t(() => {
414
+ if (!F) return;
415
+ let e = Yt.current;
416
+ if (!e) return;
417
+ let t = window.requestAnimationFrame(() => {
418
+ e.focus();
419
+ let t = e.value.length;
420
+ e.setSelectionRange(t, t);
421
+ });
422
+ return () => window.cancelAnimationFrame(t);
423
+ }, [F]), t(() => {
424
+ if (!I || !ht) return;
425
+ let e = Xt.current[`${I}-${ht.row}-${ht.col}`];
426
+ if (!e) return;
427
+ let t = window.requestAnimationFrame(() => {
428
+ e.focus();
429
+ let t = e.value.length;
430
+ e.setSelectionRange(t, t);
431
+ });
432
+ return () => window.cancelAnimationFrame(t);
433
+ }, [I, ht]), t(() => {
434
+ if (!R) return;
435
+ let e = Zt.current;
436
+ if (!e) return;
437
+ let t = window.requestAnimationFrame(() => {
438
+ e.focus();
439
+ let t = e.value.length;
440
+ e.setSelectionRange(t, t);
441
+ });
442
+ return () => window.cancelAnimationFrame(t);
443
+ }, [R]), e(() => {
444
+ let e = Jt.current;
445
+ !e || !P || (Ct(Math.max(e.scrollHeight, p)), Tt(Math.max(m, e.scrollWidth + le)));
446
+ }, [at, P]), t(() => {
447
+ if (!P || !Jt.current) {
448
+ ut(null);
449
+ return;
450
+ }
451
+ let e = y.find((e) => e.id === P);
452
+ if (!e || e.renderMode !== "latex") {
453
+ ut(null);
454
+ return;
455
+ }
456
+ let t = ze(at, st);
457
+ if (!t) {
458
+ ut(null);
459
+ return;
460
+ }
461
+ let n = Be(Jt.current, st);
462
+ ut({
463
+ expression: t.expression,
464
+ fallback: t.fallback,
465
+ left: e.x + Math.max(12, n.left),
466
+ top: Math.max(8, e.y - ue)
467
+ });
468
+ }, [
469
+ y,
470
+ P,
471
+ st,
472
+ at
473
+ ]), t(() => {
474
+ let e = {}, t = {};
475
+ for (let n of y) {
476
+ let r = Math.max(m, V.width - n.x - ie);
477
+ if (n.id === P) {
478
+ e[n.id] = Math.max(St, p), t[n.id] = Math.min(Math.max(wt, m), r);
479
+ continue;
480
+ }
481
+ let i = Kt.current[n.id];
482
+ e[n.id] = Math.max(i?.offsetHeight ?? p, n.height, p), t[n.id] = Math.min(Math.max((i?.scrollWidth ?? n.width) + le, m), r);
483
+ }
484
+ Dt((t) => Object.keys(t).length === Object.keys(e).length && Object.entries(e).every(([e, n]) => t[e] === n) ? t : e), kt((e) => Object.keys(e).length === Object.keys(t).length && Object.entries(t).every(([t, n]) => e[t] === n) ? e : t);
485
+ }, [
486
+ y,
487
+ P,
488
+ St,
489
+ wt,
490
+ V.width
491
+ ]), e(() => {
492
+ let e = Rt.current;
493
+ e && (e.nodes([]), e.getLayer()?.batchDraw());
494
+ }, [
495
+ w,
496
+ C,
497
+ y,
498
+ Et
499
+ ]), e(() => {
500
+ let e = zt.current;
501
+ if (!e) return;
502
+ if (C !== "graph" && C !== "select" || !E) {
503
+ e.nodes([]), e.getLayer()?.batchDraw();
504
+ return;
505
+ }
506
+ let t = Ut.current[E];
507
+ e.nodes(t ? [t] : []), e.getLayer()?.batchDraw();
508
+ }, [
509
+ Je,
510
+ E,
511
+ C
512
+ ]), e(() => {
513
+ let e = Bt.current;
514
+ if (!e) return;
515
+ if (C !== "matrix" && C !== "select" || !A) {
516
+ e.nodes([]), e.getLayer()?.batchDraw();
517
+ return;
518
+ }
519
+ let t = Wt.current[A];
520
+ e.nodes(t ? [t] : []), e.getLayer()?.batchDraw();
521
+ }, [
522
+ S,
523
+ A,
524
+ C
525
+ ]), e(() => {
526
+ let e = Vt.current;
527
+ if (!e) return;
528
+ if (C !== "align" && C !== "select" || !M) {
529
+ e.nodes([]), e.getLayer()?.batchDraw();
530
+ return;
531
+ }
532
+ let t = Gt.current[M];
533
+ e.nodes(t ? [t] : []), e.getLayer()?.batchDraw();
534
+ }, [
535
+ $e,
536
+ M,
537
+ C
538
+ ]);
539
+ let W = () => `block-${It.current++}`, Qt = () => ({
540
+ strokes: i,
541
+ blocks: y,
542
+ graphNodes: Je,
543
+ graphEdges: Xe,
544
+ matrices: S,
545
+ alignBlocks: $e,
546
+ renderMode: nt
547
+ }), G = () => {
548
+ dt(null), pt("");
549
+ }, K = () => {
550
+ mt(null), L(null);
551
+ }, q = () => {
552
+ gt(null), vt("");
553
+ }, J = () => {
554
+ D(null), k(null), B(null);
555
+ }, Y = () => {
556
+ j(null);
557
+ }, X = () => {
558
+ N(null);
559
+ }, $t = (e) => {
560
+ Ae(e.strokes), x(e.blocks), Ye(e.graphNodes), Ze(e.graphEdges), Qe(e.matrices), et(e.alignBlocks), rt(e.renderMode), T(null), Q(), G(), J(), K(), Y(), q(), X(), H.current = !1;
561
+ }, Z = () => {
562
+ jt((e) => [...e, Qt()]), Nt([]);
563
+ }, en = (e) => e ? y.find((t) => t.id === e) ?? null : null, tn = (e, t) => {
564
+ x((n) => n.map((n) => n.id === e ? t(n) : n));
565
+ }, nn = (e) => e ? Je.find((t) => t.id === e) ?? null : null, rn = (e) => e ? S.find((t) => t.id === e) ?? null : null, an = (e) => e ? $e.find((t) => t.id === e) ?? null : null, on = (e, t) => {
566
+ et((n) => n.map((n) => n.id === e ? t(n) : n));
567
+ }, sn = (e) => `\\begin{aligned}[t]${e.content.split("\n").map((e) => e.trim()).filter(Boolean).map((t) => {
568
+ if (e.alignMode === "token" && e.alignToken) {
569
+ let n = t.indexOf(e.alignToken);
570
+ if (n >= 0) return `${t.slice(0, n)}&${t.slice(n)}`;
571
+ }
572
+ return t;
573
+ }).join(" \\\\ ") || "\\,"}\\end{aligned}`, cn = (e, t) => Array.from({ length: e }, () => Array.from({ length: t }, () => "")), ln = (e, t, n, r = "") => Array.from({ length: t }, (t, i) => Array.from({ length: n }, (t, n) => e[i]?.[n] ?? r)), un = (e, t) => e.filter((e) => e >= 0 && e < t), dn = (e) => Array.from({ length: e.cols }, (t, n) => {
574
+ let r = Math.max(1, ...e.cells.map((e) => e[n]?.trim().length ?? 0));
575
+ return Math.max(Se, r * 9 + we * 2);
576
+ }), fn = (e) => dn(e).reduce((e, t) => e + t, 0), pn = (e) => `\\left[\\begin{array}{${Array.from({ length: e.cols }, (t, n) => e.colSeparators.includes(n) ? "c|" : "c").join("")}}${e.cells.map((t, n) => {
577
+ let r = t.map((e) => e.trim() || "\\,").join(" & ");
578
+ return e.rowSeparators.includes(n) ? `${r} \\\\ \\hline` : r;
579
+ }).join(" \\\\ ")}\\end{array}\\right]`, mn = (e) => fn(e) + g * 2, hn = (e) => e.rows * Ce + g * 2, gn = (e) => e.y + hn(e) / 2, _n = (e) => e.renderMode === "latex" ? e.y + Math.max(Et[e.id] ?? p, e.height) / 2 : null, vn = (e, t) => {
580
+ for (let n = S.length - 1; n >= 0; --n) {
581
+ let r = S[n], i = mn(r), a = hn(r);
582
+ if (e >= r.x - 12 && e <= r.x + i + 12 && t >= r.y - 12 && t <= r.y + a + 12) return r;
583
+ }
584
+ return null;
585
+ }, yn = (e, t) => {
586
+ Qe((n) => n.map((n) => n.id === e ? t(n) : n));
587
+ }, bn = (e, t, n) => {
588
+ yn(e, (e) => ({
589
+ ...e,
590
+ rows: Math.max(1, Math.min(h, t)),
591
+ cols: Math.max(1, Math.min(h, n)),
592
+ cells: ln(e.cells, Math.max(1, Math.min(h, t)), Math.max(1, Math.min(h, n))),
593
+ rowSeparators: un(e.rowSeparators, Math.max(1, Math.min(h, t)) - 1),
594
+ colSeparators: un(e.colSeparators, Math.max(1, Math.min(h, n)) - 1)
595
+ }));
596
+ }, xn = (e, t) => {
597
+ Z(), yn(e, (e) => ({
598
+ ...e,
599
+ rowSeparators: e.rowSeparators.includes(t) ? e.rowSeparators.filter((e) => e !== t) : [...e.rowSeparators, t].sort((e, t) => e - t)
600
+ }));
601
+ }, Sn = (e, t) => {
602
+ Z(), yn(e, (e) => ({
603
+ ...e,
604
+ colSeparators: e.colSeparators.includes(t) ? e.colSeparators.filter((e) => e !== t) : [...e.colSeparators, t].sort((e, t) => e - t)
605
+ }));
606
+ }, Cn = (e) => {
607
+ Z(), yn(e, (e) => ({
608
+ ...e,
609
+ cells: e.cells.map((e) => e.map((e) => e.trim() ? e : "0"))
610
+ }));
611
+ }, wn = (e, t = {
612
+ row: 0,
613
+ col: 0
614
+ }) => {
615
+ j(e), T(null), J(), Q(), G(), K(), X(), q(), mt(e), L(t);
616
+ }, Tn = () => {
617
+ K();
618
+ }, En = (e, t, n) => {
619
+ let r = hn(e), i = n + r / 2, a = null, o = Infinity;
620
+ for (let n of S) {
621
+ if (n.id === e.id || Math.abs(n.x - t) > De) continue;
622
+ let r = gn(n), s = Math.abs(r - i);
623
+ s <= Ee && s < o && (a = r, o = s);
624
+ }
625
+ for (let e of y) {
626
+ let n = _n(e);
627
+ if (n === null || Math.abs(e.x - t) > De) continue;
628
+ let r = Math.abs(n - i);
629
+ r <= Ee && r < o && (a = n, o = r);
630
+ }
631
+ return a === null ? {
632
+ x: t,
633
+ y: n
634
+ } : {
635
+ x: t,
636
+ y: a - r / 2
637
+ };
638
+ }, Dn = (e, t, n = xe, r = xe) => {
639
+ let i = W(), a = {
640
+ id: i,
641
+ x: e,
642
+ y: t,
643
+ rows: n,
644
+ cols: r,
645
+ cells: cn(n, r),
646
+ rowSeparators: [],
647
+ colSeparators: []
648
+ };
649
+ Z(), Qe((e) => [...e, a]), T(null), J(), Q(), G(), X(), q(), j(i), mt(i), L({
650
+ row: 0,
651
+ col: 0
652
+ });
653
+ }, On = (e, t) => {
654
+ let n = W();
655
+ Z(), et((r) => [...r, {
656
+ id: n,
657
+ x: e,
658
+ y: t,
659
+ width: Oe,
660
+ height: ke,
661
+ content: "",
662
+ alignMode: "left",
663
+ alignToken: "="
664
+ }]), T(null), J(), Y(), Q(), G(), K(), q(), N(n), gt(n), vt("");
665
+ }, kn = (e) => {
666
+ let t = an(e);
667
+ t && (N(e), T(null), J(), Y(), Q(), G(), K(), q(), gt(e), vt(t.content));
668
+ }, An = () => {
669
+ if (R) {
670
+ if (Z(), !_t.trim()) {
671
+ et((e) => e.filter((e) => e.id !== R)), M === R && N(null), q();
672
+ return;
673
+ }
674
+ on(R, (e) => ({
675
+ ...e,
676
+ content: _t
677
+ })), q();
678
+ }
679
+ }, jn = () => {
680
+ M && (Z(), et((e) => e.filter((e) => e.id !== M)), R === M && q(), N(null));
681
+ }, Mn = (e, t, n, r) => {
682
+ yn(e, (e) => ({
683
+ ...e,
684
+ cells: e.cells.map((e, i) => i === t ? e.map((e, t) => t === n ? r : e) : e)
685
+ }));
686
+ }, Nn = () => {
687
+ A && (Z(), Qe((e) => e.filter((e) => e.id !== A)), I === A && K(), j(null));
688
+ }, Pn = (e, t) => {
689
+ for (let n = Je.length - 1; n >= 0; --n) {
690
+ let r = Je[n], i = r.x + r.width / 2, a = r.y + r.height / 2, o = Math.min(r.width, r.height);
691
+ if (Math.hypot(e - i, t - a) <= o) return r;
692
+ }
693
+ return null;
694
+ }, Fn = (e, t) => {
695
+ Ye((n) => n.map((n) => n.id === e ? t(n) : n));
696
+ }, In = (e, t) => {
697
+ let n = e.x + e.width / 2, r = e.y + e.height / 2, i = t.x + t.width / 2, a = t.y + t.height / 2, o = i - n, s = a - r, c = Math.hypot(o, s);
698
+ if (c === 0) return [
699
+ n,
700
+ r,
701
+ i,
702
+ a
703
+ ];
704
+ let l = o / c, u = s / c, ee = Math.min(e.width, e.height) / 2, te = Math.min(t.width, t.height) / 2;
705
+ return [
706
+ n + l * ee,
707
+ r + u * ee,
708
+ i - l * te,
709
+ a - u * te
710
+ ];
711
+ }, Ln = (e, t) => Xe.some((n) => n.sourceId === e && n.targetId === t || n.sourceId === t && n.targetId === e), Rn = (e) => {
712
+ let t = nn(e);
713
+ t && (Y(), K(), X(), q(), D(e), k(null), dt(e), pt(t.label));
714
+ }, zn = () => {
715
+ if (!F) return;
716
+ let e = ft.trim();
717
+ Z(), Fn(F, (t) => ({
718
+ ...t,
719
+ label: e
720
+ })), G();
721
+ }, Bn = () => {
722
+ G();
723
+ }, Vn = (e, t, n) => {
724
+ let r = W(), i = {
725
+ id: r,
726
+ x: e,
727
+ y: t,
728
+ width: he,
729
+ height: ge,
730
+ label: n?.label ?? "",
731
+ shape: "circle"
732
+ };
733
+ Z(), Ye((e) => [...e, i]), n?.connectFromId && n.connectFromId !== r && !Ln(n.connectFromId, r) ? (Ze((e) => [...e, {
734
+ id: W(),
735
+ sourceId: n.connectFromId,
736
+ targetId: r
737
+ }]), B(n.connectFromId)) : B(r), D(r), k(null), T(null), Y(), K(), X(), q(), Q(), dt(r), pt(i.label);
738
+ }, Hn = (e, t) => {
739
+ if (e === t || Ln(e, t)) {
740
+ D(t), k(null);
741
+ return;
742
+ }
743
+ Z(), Ze((n) => [...n, {
744
+ id: W(),
745
+ sourceId: e,
746
+ targetId: t
747
+ }]), D(t), k(null), B(e);
748
+ }, Un = () => {
749
+ E && (Z(), Ye((e) => e.filter((e) => e.id !== E)), Ze((e) => e.filter((e) => e.sourceId !== E && e.targetId !== E)), F === E && G(), z === E && B(null), D(null), k(null));
750
+ }, Wn = () => {
751
+ O && (Z(), Ze((e) => e.filter((e) => e.id !== O)), k(null));
752
+ }, Gn = (e, t) => {
753
+ E && (Z(), Fn(E, (n) => ({
754
+ ...n,
755
+ x: n.x + e,
756
+ y: n.y + t
757
+ })));
758
+ }, Kn = (e, t) => {
759
+ let n = nn(E);
760
+ n && Vn(n.x + e, n.y + t, { connectFromId: n.id });
761
+ }, qn = (e) => {
762
+ let t = nn(E);
763
+ if (!t) return;
764
+ let n = t.x + t.width / 2, r = t.y + t.height / 2, i = Xe.map((e) => e.sourceId === t.id ? nn(e.targetId) : e.targetId === t.id ? nn(e.sourceId) : null).filter((e) => !!e).map((t) => {
765
+ let i = t.x + t.width / 2, a = t.y + t.height / 2, o = i - n, s = a - r;
766
+ if (e === "left" && o >= 0 || e === "right" && o <= 0 || e === "up" && s >= 0 || e === "down" && s <= 0) return null;
767
+ let c = Math.abs(e === "left" || e === "right" ? o : s), l = Math.abs(e === "left" || e === "right" ? s : o);
768
+ return {
769
+ id: t.id,
770
+ score: l * 10 + c
771
+ };
772
+ }).filter((e) => !!e).sort((e, t) => e.score - t.score)[0];
773
+ i && (D(i.id), k(null), B(i.id));
774
+ }, Q = () => {
775
+ it(null), ot(""), ct(0), Ct(p), Tt(ae), ut(null);
776
+ }, Jn = (e, t, n, r, i, a) => {
777
+ let o = i - n, s = a - r;
778
+ if (o === 0 && s === 0) return Math.hypot(e - n, t - r);
779
+ let c = Math.max(0, Math.min(1, ((e - n) * o + (t - r) * s) / (o * o + s * s))), l = n + c * o, u = r + c * s;
780
+ return Math.hypot(e - l, t - u);
781
+ }, Yn = (e, t, n) => {
782
+ Ae((r) => {
783
+ let i = [];
784
+ for (let a of r) {
785
+ let r = [], o = [];
786
+ for (let i = 0; i < a.points.length; i += 2) {
787
+ let s = a.points[i], c = a.points[i + 1], l = Math.hypot(s - e, c - t) <= n;
788
+ if (l || o.push(s, c), i < a.points.length - 2) {
789
+ let u = a.points[i + 2], ee = a.points[i + 3];
790
+ (l || Math.hypot(u - e, ee - t) <= n || Jn(e, t, s, c, u, ee) <= n) && (o.length >= 4 && r.push(o), o = []);
791
+ }
792
+ }
793
+ if (o.length >= 4 && r.push(o), r.length !== 0) for (let e of r) i.push({
794
+ ...a,
795
+ id: W(),
796
+ points: e
797
+ });
798
+ }
799
+ return i;
800
+ });
801
+ }, Xn = (e, t) => {
802
+ let n = performance.now(), r = Lt.current, i = de;
803
+ if (r) {
804
+ let a = Math.hypot(e - r.x, t - r.y) / Math.max(n - r.time, 1);
805
+ i = de + Math.max(0, Math.min(1, (a - pe) / me)) * (fe - de);
806
+ }
807
+ return Lt.current = {
808
+ x: e,
809
+ y: t,
810
+ time: n
811
+ }, Ft({
812
+ x: e,
813
+ y: t,
814
+ radius: i,
815
+ visible: !0
816
+ }), i;
817
+ }, Zn = (e) => {
818
+ let t = en(e);
819
+ t && (J(), G(), Y(), K(), X(), q(), T(e), rt(t.renderMode), it(e), ot(t.content), ct(t.content.length), Tt(Math.min(Math.max(Ve(t.content), m), Math.max(m, V.width - t.x - ie))));
820
+ }, Qn = () => {
821
+ if (!P) return;
822
+ let e = at.trim();
823
+ if (Z(), !e) {
824
+ x((e) => e.filter((e) => e.id !== P)), w === P && T(null), Q();
825
+ return;
826
+ }
827
+ tn(P, (e) => ({
828
+ ...e,
829
+ content: at,
830
+ width: e.renderMode === "latex" ? m : Math.min(Math.max(wt, m), Math.max(m, V.width - e.x - ie)),
831
+ height: Math.max(e.height, St)
832
+ })), Q();
833
+ }, $n = (e, t) => {
834
+ let n = W(), r = Math.max(m, Math.min(ae, V.width - e - ie));
835
+ Z(), x((i) => [...i, {
836
+ id: n,
837
+ x: e,
838
+ y: t,
839
+ width: r,
840
+ height: p,
841
+ content: "",
842
+ renderMode: nt
843
+ }]), J(), G(), Y(), K(), X(), q(), T(n), it(n), ot(""), ct(0), Ct(p), Tt(r);
844
+ }, er = (e, t) => {
845
+ C !== "pen" && C !== "eraser" || (Z(), Ae((n) => [...n, {
846
+ id: W(),
847
+ points: [e, t],
848
+ tool: C
849
+ }]));
850
+ }, tr = (e, t) => {
851
+ Ae((n) => {
852
+ let r = [...n], i = r[r.length - 1];
853
+ return i ? (r[r.length - 1] = {
854
+ ...i,
855
+ points: [
856
+ ...i.points,
857
+ e,
858
+ t
859
+ ]
860
+ }, r) : n;
861
+ });
862
+ }, nr = (e) => {
863
+ let t = e.target.getStage(), n = t?.getPointerPosition();
864
+ if (!n) return;
865
+ let r = e.target === t || e.target.getClassName() === "Rect";
866
+ if (C === "text") {
867
+ H.current = !1, U.current = !1;
868
+ return;
869
+ }
870
+ if (C === "graph") {
871
+ P && Qn(), F && r && zn(), r && (T(null), k(null)), H.current = !1, U.current = !1;
872
+ return;
873
+ }
874
+ if (C === "matrix") {
875
+ P && Qn(), F && r && zn(), I && r && Tn(), r && (T(null), J()), H.current = !1, U.current = !1;
876
+ return;
877
+ }
878
+ if (C === "align") {
879
+ P && Qn(), F && r && zn(), I && r && Tn(), R && r && An(), r && (T(null), J(), Y()), H.current = !1, U.current = !1;
880
+ return;
881
+ }
882
+ if (P && Qn(), F && zn(), I && Tn(), R && An(), C === "pen" || C === "eraser") {
883
+ if (T(null), J(), Y(), X(), C === "pen") H.current = !0, U.current = !1, er(n.x, n.y);
884
+ else {
885
+ Z(), H.current = !1, U.current = !0;
886
+ let e = Xn(n.x, n.y);
887
+ Yn(n.x, n.y, e);
888
+ }
889
+ return;
890
+ }
891
+ C === "select" && r && (T(null), J(), Y(), X()), H.current = !1, U.current = !1;
892
+ }, rr = (e) => {
893
+ if (C !== "text" && C !== "graph" && C !== "matrix" && C !== "align") return;
894
+ let t = e.target.getStage(), n = t?.getPointerPosition();
895
+ if (n && (e.target === t || e.target.getClassName() === "Rect")) {
896
+ if (C === "text") {
897
+ $n(n.x, n.y);
898
+ return;
899
+ }
900
+ if (C === "graph") {
901
+ let e = Pn(n.x, n.y);
902
+ if (e) {
903
+ E === e.id ? Rn(e.id) : (D(e.id), k(null), B(e.id), T(null));
904
+ return;
905
+ }
906
+ Vn(n.x - he / 2, n.y - ge / 2);
907
+ return;
908
+ }
909
+ if (C === "matrix") {
910
+ let e = vn(n.x, n.y);
911
+ if (e) {
912
+ A === e.id ? wn(e.id) : (j(e.id), T(null), J());
913
+ return;
914
+ }
915
+ Dn(n.x - (xe * Se + g * 2 + 24) / 2, n.y - (xe * Ce + g * 2) / 2);
916
+ return;
917
+ }
918
+ C === "align" && On(n.x, n.y);
919
+ }
920
+ }, ir = (e) => {
921
+ let t = e.target.getStage()?.getPointerPosition();
922
+ if (t) {
923
+ if (C === "eraser") {
924
+ let e = Xn(t.x, t.y);
925
+ U.current && Yn(t.x, t.y, e);
926
+ return;
927
+ }
928
+ H.current && tr(t.x, t.y);
929
+ }
930
+ }, ar = () => {
931
+ H.current = !1, U.current = !1, Lt.current = null;
932
+ }, or = (e) => {
933
+ if (nt !== e) {
934
+ if (Z(), rt(e), P) {
935
+ tn(P, (t) => ({
936
+ ...t,
937
+ renderMode: e
938
+ }));
939
+ return;
940
+ }
941
+ w && tn(w, (t) => ({
942
+ ...t,
943
+ renderMode: e
944
+ }));
945
+ }
946
+ }, sr = () => {
947
+ or(nt === "latex" ? "plain" : "latex");
948
+ }, cr = () => {
949
+ jt((e) => {
950
+ let t = e[e.length - 1];
951
+ return t ? (Nt((e) => [Qt(), ...e]), $t(t), e.slice(0, -1)) : e;
952
+ });
953
+ }, lr = () => {
954
+ Nt((e) => {
955
+ let t = e[0];
956
+ return t ? (jt((e) => [...e, Qt()]), $t(t), e.slice(1)) : e;
957
+ });
958
+ };
959
+ e(() => {
960
+ let e = (e) => {
961
+ let t = e.target, n = t instanceof HTMLInputElement && !!t.dataset.matrixId;
962
+ if (!(t instanceof HTMLTextAreaElement || t instanceof HTMLInputElement && !n || t instanceof HTMLElement && t.isContentEditable)) {
963
+ if (e.key === "Escape" && B(null), (e.ctrlKey || e.metaKey) && e.key.toLowerCase() === "z" && !e.shiftKey) {
964
+ e.preventDefault(), cr();
965
+ return;
966
+ }
967
+ if ((e.ctrlKey || e.metaKey) && (e.key.toLowerCase() === "y" || e.shiftKey && e.key.toLowerCase() === "z")) {
968
+ e.preventDefault(), lr();
969
+ return;
970
+ }
971
+ if (C === "matrix") {
972
+ if (A && (e.ctrlKey || e.metaKey)) {
973
+ let t = rn(A);
974
+ if (!t) return;
975
+ if (e.key === "ArrowUp") {
976
+ e.preventDefault(), Z(), bn(A, t.rows - 1, t.cols);
977
+ return;
978
+ }
979
+ if (e.key === "ArrowDown") {
980
+ e.preventDefault(), Z(), bn(A, t.rows + 1, t.cols);
981
+ return;
982
+ }
983
+ if (e.key === "ArrowLeft") {
984
+ e.preventDefault(), Z(), bn(A, t.rows, t.cols - 1);
985
+ return;
986
+ }
987
+ if (e.key === "ArrowRight") {
988
+ e.preventDefault(), Z(), bn(A, t.rows, t.cols + 1);
989
+ return;
990
+ }
991
+ }
992
+ if (!I && A && (e.key === "Backspace" || e.key === "Delete")) {
993
+ e.preventDefault(), Nn();
994
+ return;
995
+ }
996
+ if (A && e.key === "Enter" && !I) {
997
+ e.preventDefault(), wn(A);
998
+ return;
999
+ }
1000
+ }
1001
+ if (C === "align") {
1002
+ if (!R && M && (e.key === "Backspace" || e.key === "Delete")) {
1003
+ e.preventDefault(), jn();
1004
+ return;
1005
+ }
1006
+ if (M && e.key === "Enter" && !R) {
1007
+ e.preventDefault(), kn(M);
1008
+ return;
1009
+ }
1010
+ }
1011
+ if (!(C !== "graph" || F)) {
1012
+ if ((e.ctrlKey || e.altKey) && E) {
1013
+ if (e.key === "ArrowLeft") {
1014
+ e.preventDefault(), qn("left");
1015
+ return;
1016
+ }
1017
+ if (e.key === "ArrowRight") {
1018
+ e.preventDefault(), qn("right");
1019
+ return;
1020
+ }
1021
+ if (e.key === "ArrowUp") {
1022
+ e.preventDefault(), qn("up");
1023
+ return;
1024
+ }
1025
+ if (e.key === "ArrowDown") {
1026
+ e.preventDefault(), qn("down");
1027
+ return;
1028
+ }
1029
+ }
1030
+ if (!(!E && !O)) {
1031
+ if (e.key === "Enter" && E) {
1032
+ e.preventDefault(), Kn(e.shiftKey ? 0 : ye, e.shiftKey ? ye : 0);
1033
+ return;
1034
+ }
1035
+ if (e.key === "Tab" && E) {
1036
+ e.preventDefault(), Kn(ye, 0);
1037
+ return;
1038
+ }
1039
+ if (e.key === "Backspace" || e.key === "Delete") {
1040
+ e.preventDefault(), E ? Un() : O && Wn();
1041
+ return;
1042
+ }
1043
+ if (e.key === "ArrowLeft" && E) {
1044
+ e.preventDefault(), Gn(-be, 0);
1045
+ return;
1046
+ }
1047
+ if (e.key === "ArrowRight" && E) {
1048
+ e.preventDefault(), Gn(be, 0);
1049
+ return;
1050
+ }
1051
+ if (e.key === "ArrowUp" && E) {
1052
+ e.preventDefault(), Gn(0, -be);
1053
+ return;
1054
+ }
1055
+ e.key === "ArrowDown" && E && (e.preventDefault(), Gn(0, be));
1056
+ }
1057
+ }
1058
+ }
1059
+ };
1060
+ return window.addEventListener("keydown", e), () => window.removeEventListener("keydown", e);
1061
+ }, [
1062
+ F,
1063
+ I,
1064
+ Xe,
1065
+ Je,
1066
+ S,
1067
+ $e,
1068
+ R,
1069
+ O,
1070
+ E,
1071
+ M,
1072
+ A,
1073
+ C
1074
+ ]);
1075
+ let ur = en(P), dr = en(w), fr = nn(F), pr = nn(E), mr = rn(I), hr = rn(A), $ = an(R), gr = an(M), _r = new Map(Je.map((e) => [e.id, e]));
1076
+ return /* @__PURE__ */ f("div", {
1077
+ style: {
1078
+ position: "relative",
1079
+ width: V.width,
1080
+ height: V.height,
1081
+ overflow: "hidden"
1082
+ },
1083
+ children: [
1084
+ /* @__PURE__ */ f("div", {
1085
+ style: {
1086
+ position: "fixed",
1087
+ top: 12,
1088
+ left: 12,
1089
+ zIndex: 30,
1090
+ display: "flex",
1091
+ gap: 8,
1092
+ padding: 8,
1093
+ background: "rgba(255, 255, 255, 0.96)",
1094
+ border: "1px solid #d4d4d4",
1095
+ borderRadius: 8,
1096
+ boxShadow: "0 2px 8px rgba(0, 0, 0, 0.08)"
1097
+ },
1098
+ children: [
1099
+ /* @__PURE__ */ d(b, {
1100
+ active: C === "select",
1101
+ label: "Select",
1102
+ onClick: () => tt("select"),
1103
+ children: /* @__PURE__ */ d(Fe, { active: C === "select" })
1104
+ }),
1105
+ /* @__PURE__ */ d(b, {
1106
+ active: C === "pen",
1107
+ label: "Pen",
1108
+ onClick: () => tt("pen"),
1109
+ children: /* @__PURE__ */ d(Ne, { active: C === "pen" })
1110
+ }),
1111
+ /* @__PURE__ */ d(b, {
1112
+ active: C === "eraser",
1113
+ label: "Eraser",
1114
+ onClick: () => tt("eraser"),
1115
+ children: /* @__PURE__ */ d(Pe, { active: C === "eraser" })
1116
+ }),
1117
+ /* @__PURE__ */ d(b, {
1118
+ active: C === "text",
1119
+ label: "Text",
1120
+ onClick: () => tt("text"),
1121
+ children: /* @__PURE__ */ d(Le, { active: C === "text" })
1122
+ }),
1123
+ /* @__PURE__ */ d(b, {
1124
+ active: C === "graph",
1125
+ label: "Graph",
1126
+ onClick: () => tt("graph"),
1127
+ children: /* @__PURE__ */ d(He, { active: C === "graph" })
1128
+ }),
1129
+ /* @__PURE__ */ d(b, {
1130
+ active: C === "matrix",
1131
+ label: "Matrix",
1132
+ onClick: () => tt("matrix"),
1133
+ children: /* @__PURE__ */ d(Ue, { active: C === "matrix" })
1134
+ }),
1135
+ /* @__PURE__ */ d(b, {
1136
+ active: C === "align",
1137
+ label: "Align Math",
1138
+ onClick: () => tt("align"),
1139
+ children: /* @__PURE__ */ d(We, { active: C === "align" })
1140
+ }),
1141
+ /* @__PURE__ */ d(b, {
1142
+ active: !1,
1143
+ disabled: At.length === 0,
1144
+ label: "Undo",
1145
+ onClick: cr,
1146
+ children: /* @__PURE__ */ d(Ke, { active: !1 })
1147
+ }),
1148
+ /* @__PURE__ */ d(b, {
1149
+ active: !1,
1150
+ disabled: Mt.length === 0,
1151
+ label: "Redo",
1152
+ onClick: lr,
1153
+ children: /* @__PURE__ */ d(qe, { active: !1 })
1154
+ }),
1155
+ /* @__PURE__ */ d(b, {
1156
+ active: nt === "latex",
1157
+ label: "Toggle LaTeX",
1158
+ onClick: sr,
1159
+ children: /* @__PURE__ */ d(Ge, { active: nt === "latex" })
1160
+ }),
1161
+ /* @__PURE__ */ d(b, {
1162
+ active: !1,
1163
+ label: "Clear",
1164
+ onClick: () => {
1165
+ Z(), Ae([]), x([]), Ye([]), Ze([]), Qe([]), et([]), T(null), J(), Y(), X(), Q(), G(), K(), q(), H.current = !1;
1166
+ },
1167
+ children: /* @__PURE__ */ d(Ie, {})
1168
+ })
1169
+ ]
1170
+ }),
1171
+ /* @__PURE__ */ d(ee, {
1172
+ width: V.width,
1173
+ height: V.height,
1174
+ onMouseDown: nr,
1175
+ onClick: rr,
1176
+ onMousemove: ir,
1177
+ onMouseup: ar,
1178
+ onTouchStart: nr,
1179
+ onTap: rr,
1180
+ onTouchMove: ir,
1181
+ onTouchEnd: ar,
1182
+ children: /* @__PURE__ */ f(c, { children: [
1183
+ /* @__PURE__ */ d(u, {
1184
+ x: 0,
1185
+ y: 0,
1186
+ width: V.width,
1187
+ height: V.height,
1188
+ fill: "white"
1189
+ }),
1190
+ i.map((e) => /* @__PURE__ */ d(l, {
1191
+ points: e.points,
1192
+ stroke: e.tool === "pen" ? "black" : "white",
1193
+ strokeWidth: e.tool === "pen" ? 2 : 14,
1194
+ tension: .5,
1195
+ lineCap: "round",
1196
+ lineJoin: "round"
1197
+ }, e.id)),
1198
+ Xe.map((e) => {
1199
+ let t = _r.get(e.sourceId), n = _r.get(e.targetId);
1200
+ if (!t || !n) return null;
1201
+ let r = In(t, n);
1202
+ return e.directed ? /* @__PURE__ */ d(a, {
1203
+ points: r,
1204
+ stroke: O === e.id ? "#111" : "#5f5f5f",
1205
+ fill: O === e.id ? "#111" : "#5f5f5f",
1206
+ strokeWidth: O === e.id ? 2.5 : 1.8,
1207
+ pointerLength: 8,
1208
+ pointerWidth: 8,
1209
+ onClick: () => {
1210
+ k(e.id), D(null), T(null), Y();
1211
+ },
1212
+ onTap: () => {
1213
+ k(e.id), D(null), T(null), Y();
1214
+ }
1215
+ }, e.id) : /* @__PURE__ */ d(l, {
1216
+ points: r,
1217
+ stroke: O === e.id ? "#111" : "#5f5f5f",
1218
+ strokeWidth: O === e.id ? 2.5 : 1.8,
1219
+ lineCap: "round",
1220
+ onClick: () => {
1221
+ k(e.id), D(null), T(null), Y();
1222
+ },
1223
+ onTap: () => {
1224
+ k(e.id), D(null), T(null), Y();
1225
+ }
1226
+ }, e.id);
1227
+ }),
1228
+ Je.map((e) => {
1229
+ let t = E === e.id, n = z === e.id, r = yt === e.id, i = F === e.id, a = n || t || i ? "#8c8c8c" : r ? "#b8b8b8" : "#555";
1230
+ return /* @__PURE__ */ f(s, {
1231
+ ref: (t) => {
1232
+ Ut.current[e.id] = t;
1233
+ },
1234
+ x: e.x,
1235
+ y: e.y,
1236
+ draggable: C === "graph" || C === "select",
1237
+ onMouseEnter: () => bt(e.id),
1238
+ onMouseLeave: () => bt((t) => t === e.id ? null : t),
1239
+ onClick: (t) => {
1240
+ if (t.cancelBubble = !0, D(e.id), k(null), T(null), Y(), C === "graph") {
1241
+ if (z && z !== e.id) {
1242
+ Hn(z, e.id);
1243
+ return;
1244
+ }
1245
+ if (E === e.id && z === e.id) {
1246
+ Rn(e.id);
1247
+ return;
1248
+ }
1249
+ B(e.id);
1250
+ }
1251
+ },
1252
+ onTap: (t) => {
1253
+ if (t.cancelBubble = !0, D(e.id), k(null), T(null), Y(), C === "graph") {
1254
+ if (z && z !== e.id) {
1255
+ Hn(z, e.id);
1256
+ return;
1257
+ }
1258
+ if (E === e.id && z === e.id) {
1259
+ Rn(e.id);
1260
+ return;
1261
+ }
1262
+ B(e.id);
1263
+ }
1264
+ },
1265
+ onDblClick: (t) => {
1266
+ t.cancelBubble = !0, Rn(e.id);
1267
+ },
1268
+ onDblTap: (t) => {
1269
+ t.cancelBubble = !0, Rn(e.id);
1270
+ },
1271
+ onDragStart: (t) => {
1272
+ t.cancelBubble = !0, D(e.id), k(null);
1273
+ },
1274
+ onDragEnd: (t) => {
1275
+ Z(), Fn(e.id, (e) => ({
1276
+ ...e,
1277
+ x: t.target.x(),
1278
+ y: t.target.y()
1279
+ }));
1280
+ },
1281
+ onTransformEnd: (t) => {
1282
+ Z();
1283
+ let n = t.target, r = Math.max(_e, Math.max(e.width * n.scaleX(), e.height * n.scaleY()));
1284
+ n.scaleX(1), n.scaleY(1), Fn(e.id, (e) => ({
1285
+ ...e,
1286
+ x: n.x(),
1287
+ y: n.y(),
1288
+ width: r,
1289
+ height: r
1290
+ }));
1291
+ },
1292
+ children: [/* @__PURE__ */ d(o, {
1293
+ x: e.width / 2,
1294
+ y: e.height / 2,
1295
+ radius: Math.min(e.width, e.height) / 2,
1296
+ fill: "transparent",
1297
+ stroke: a,
1298
+ strokeWidth: t || n ? 2.5 : 1.8
1299
+ }), /* @__PURE__ */ d(te, {
1300
+ x: 10,
1301
+ y: e.height / 2 - 10,
1302
+ width: Math.max(e.width - 20, 20),
1303
+ align: "center",
1304
+ verticalAlign: "middle",
1305
+ text: e.label || " ",
1306
+ fontSize: 18,
1307
+ fill: "#111",
1308
+ fontFamily: v,
1309
+ listening: !1
1310
+ })]
1311
+ }, e.id);
1312
+ }),
1313
+ S.map((e) => /* @__PURE__ */ d(u, {
1314
+ ref: (t) => {
1315
+ Wt.current[e.id] = t;
1316
+ },
1317
+ x: e.x,
1318
+ y: e.y,
1319
+ width: mn(e),
1320
+ height: hn(e),
1321
+ fill: "transparent",
1322
+ stroke: "transparent",
1323
+ strokeWidth: 1,
1324
+ draggable: C === "matrix" || C === "select",
1325
+ onClick: () => {
1326
+ j(e.id), T(null), J();
1327
+ },
1328
+ onTap: () => {
1329
+ j(e.id), T(null), J();
1330
+ },
1331
+ onDblClick: () => wn(e.id),
1332
+ onDblTap: () => wn(e.id),
1333
+ onDragStart: () => {
1334
+ j(e.id), T(null), J();
1335
+ },
1336
+ onDragEnd: (t) => {
1337
+ Z();
1338
+ let n = En(e, t.target.x(), t.target.y());
1339
+ yn(e.id, (e) => ({
1340
+ ...e,
1341
+ x: n.x,
1342
+ y: n.y
1343
+ }));
1344
+ },
1345
+ onTransformEnd: (t) => {
1346
+ Z();
1347
+ let n = t.target, r = Math.max(1, Math.min(h, Math.round((n.width() * n.scaleX() - g * 2) / (Se + we * 2)))), i = Math.max(1, Math.min(h, Math.round((n.height() * n.scaleY() - g * 2) / Ce)));
1348
+ n.scaleX(1), n.scaleY(1);
1349
+ let a = En(e, n.x(), n.y());
1350
+ yn(e.id, (e) => ({
1351
+ ...e,
1352
+ x: a.x,
1353
+ y: a.y,
1354
+ rows: i,
1355
+ cols: r,
1356
+ cells: ln(e.cells, i, r),
1357
+ rowSeparators: un(e.rowSeparators, i - 1),
1358
+ colSeparators: un(e.colSeparators, r - 1)
1359
+ }));
1360
+ }
1361
+ }, e.id)),
1362
+ $e.map((e) => /* @__PURE__ */ d(u, {
1363
+ ref: (t) => {
1364
+ Gt.current[e.id] = t;
1365
+ },
1366
+ x: e.x,
1367
+ y: e.y,
1368
+ width: e.width,
1369
+ height: e.height,
1370
+ fill: "transparent",
1371
+ stroke: "transparent",
1372
+ draggable: C === "align" || C === "select",
1373
+ onClick: () => {
1374
+ N(e.id), T(null), J(), Y();
1375
+ },
1376
+ onTap: () => {
1377
+ N(e.id), T(null), J(), Y();
1378
+ },
1379
+ onDblClick: () => kn(e.id),
1380
+ onDblTap: () => kn(e.id),
1381
+ onDragEnd: (t) => {
1382
+ Z(), on(e.id, (e) => ({
1383
+ ...e,
1384
+ x: t.target.x(),
1385
+ y: t.target.y()
1386
+ }));
1387
+ },
1388
+ onTransformEnd: (t) => {
1389
+ Z();
1390
+ let n = t.target, r = Math.max(180, n.width() * n.scaleX()), i = Math.max(60, n.height() * n.scaleY());
1391
+ n.scaleX(1), n.scaleY(1), on(e.id, (e) => ({
1392
+ ...e,
1393
+ x: n.x(),
1394
+ y: n.y(),
1395
+ width: r,
1396
+ height: i
1397
+ }));
1398
+ }
1399
+ }, e.id)),
1400
+ y.map((e) => /* @__PURE__ */ d(u, {
1401
+ ref: (t) => {
1402
+ Ht.current[e.id] = t;
1403
+ },
1404
+ x: e.x,
1405
+ y: e.y,
1406
+ width: Ot[e.id] ?? e.width,
1407
+ height: Math.max(Et[e.id] ?? p, e.height),
1408
+ fill: "transparent",
1409
+ stroke: "transparent",
1410
+ dash: [],
1411
+ strokeWidth: 1,
1412
+ draggable: C === "select",
1413
+ onClick: () => {
1414
+ C === "select" && (J(), Y(), T(e.id), rt(e.renderMode));
1415
+ },
1416
+ onTap: () => {
1417
+ C === "select" && (J(), Y(), T(e.id), rt(e.renderMode));
1418
+ },
1419
+ onDblClick: () => Zn(e.id),
1420
+ onDblTap: () => Zn(e.id),
1421
+ onDragStart: () => {
1422
+ C === "select" && T(e.id);
1423
+ },
1424
+ onDragEnd: (t) => {
1425
+ Z(), tn(e.id, (e) => ({
1426
+ ...e,
1427
+ x: t.target.x(),
1428
+ y: t.target.y()
1429
+ }));
1430
+ }
1431
+ }, e.id)),
1432
+ /* @__PURE__ */ d(ne, {
1433
+ ref: Rt,
1434
+ enabledAnchors: [
1435
+ "top-left",
1436
+ "top-center",
1437
+ "top-right",
1438
+ "middle-left",
1439
+ "middle-right",
1440
+ "bottom-left",
1441
+ "bottom-center",
1442
+ "bottom-right"
1443
+ ],
1444
+ rotateEnabled: !1,
1445
+ boundBoxFunc: (e, t) => ({
1446
+ ...t,
1447
+ width: Math.max(m, t.width),
1448
+ height: Math.max(p, t.height)
1449
+ })
1450
+ }),
1451
+ /* @__PURE__ */ d(ne, {
1452
+ ref: zt,
1453
+ enabledAnchors: [
1454
+ "top-left",
1455
+ "top-center",
1456
+ "top-right",
1457
+ "middle-left",
1458
+ "middle-right",
1459
+ "bottom-left",
1460
+ "bottom-center",
1461
+ "bottom-right"
1462
+ ],
1463
+ rotateEnabled: !1,
1464
+ boundBoxFunc: (e, t) => {
1465
+ let n = Math.max(_e, ve, t.width, t.height);
1466
+ return {
1467
+ ...t,
1468
+ width: n,
1469
+ height: n
1470
+ };
1471
+ }
1472
+ }),
1473
+ /* @__PURE__ */ d(ne, {
1474
+ ref: Bt,
1475
+ enabledAnchors: [
1476
+ "top-left",
1477
+ "top-center",
1478
+ "top-right",
1479
+ "middle-left",
1480
+ "middle-right",
1481
+ "bottom-left",
1482
+ "bottom-center",
1483
+ "bottom-right"
1484
+ ],
1485
+ rotateEnabled: !1,
1486
+ boundBoxFunc: (e, t) => ({
1487
+ ...t,
1488
+ width: Math.max(Se + we * 2 + g * 2, t.width),
1489
+ height: Math.max(Ce + g * 2, t.height)
1490
+ })
1491
+ }),
1492
+ /* @__PURE__ */ d(ne, {
1493
+ ref: Vt,
1494
+ enabledAnchors: [
1495
+ "top-left",
1496
+ "top-right",
1497
+ "bottom-left",
1498
+ "bottom-right",
1499
+ "middle-left",
1500
+ "middle-right"
1501
+ ],
1502
+ rotateEnabled: !1,
1503
+ boundBoxFunc: (e, t) => ({
1504
+ ...t,
1505
+ width: Math.max(180, t.width),
1506
+ height: Math.max(60, t.height)
1507
+ })
1508
+ })
1509
+ ] })
1510
+ }),
1511
+ C === "eraser" && Pt.visible && /* @__PURE__ */ d("div", { style: {
1512
+ position: "absolute",
1513
+ left: `${Pt.x - Pt.radius}px`,
1514
+ top: `${Pt.y - Pt.radius}px`,
1515
+ width: `${Pt.radius * 2}px`,
1516
+ height: `${Pt.radius * 2}px`,
1517
+ border: "2px dotted #7a7a7a",
1518
+ borderRadius: "50%",
1519
+ pointerEvents: "none",
1520
+ boxSizing: "border-box",
1521
+ zIndex: 1100
1522
+ } }),
1523
+ dr && !ur && /* @__PURE__ */ d("button", {
1524
+ type: "button",
1525
+ "aria-label": "Delete text block",
1526
+ title: "Delete text block",
1527
+ onClick: () => {
1528
+ Z(), x((e) => e.filter((e) => e.id !== dr.id)), T(null);
1529
+ },
1530
+ style: {
1531
+ position: "absolute",
1532
+ left: `${dr.x + (Ot[dr.id] ?? dr.width) / 2 - 14}px`,
1533
+ top: `${dr.y - 30}px`,
1534
+ width: "28px",
1535
+ height: "28px",
1536
+ display: "flex",
1537
+ alignItems: "center",
1538
+ justifyContent: "center",
1539
+ border: "1px solid #f5c2c7",
1540
+ borderRadius: "999px",
1541
+ background: "#fff5f5",
1542
+ cursor: "pointer",
1543
+ zIndex: 1150
1544
+ },
1545
+ children: /* @__PURE__ */ d(Me, {})
1546
+ }),
1547
+ pr && !fr && /* @__PURE__ */ d("button", {
1548
+ type: "button",
1549
+ "aria-label": "Delete graph node",
1550
+ title: "Delete graph node",
1551
+ onClick: () => {
1552
+ Un();
1553
+ },
1554
+ style: {
1555
+ position: "absolute",
1556
+ left: `${pr.x + pr.width / 2 - 14}px`,
1557
+ top: `${pr.y - 30}px`,
1558
+ width: "28px",
1559
+ height: "28px",
1560
+ display: "flex",
1561
+ alignItems: "center",
1562
+ justifyContent: "center",
1563
+ border: "1px solid #f5c2c7",
1564
+ borderRadius: "999px",
1565
+ background: "#fff5f5",
1566
+ cursor: "pointer",
1567
+ zIndex: 1150
1568
+ },
1569
+ children: /* @__PURE__ */ d(Me, {})
1570
+ }),
1571
+ hr && !mr && /* @__PURE__ */ f(re, { children: [/* @__PURE__ */ d("button", {
1572
+ type: "button",
1573
+ "aria-label": "Fill empty matrix cells with zeros",
1574
+ title: "Fill empty matrix cells with zeros",
1575
+ onClick: () => Cn(hr.id),
1576
+ style: {
1577
+ position: "absolute",
1578
+ left: `${hr.x + mn(hr) / 2 - 64}px`,
1579
+ top: `${hr.y - 32}px`,
1580
+ height: "28px",
1581
+ padding: "0 10px",
1582
+ border: "1px solid #d9d9d9",
1583
+ borderRadius: "999px",
1584
+ background: "#fff",
1585
+ cursor: "pointer",
1586
+ zIndex: 1150,
1587
+ fontSize: 12
1588
+ },
1589
+ children: "Fill 0s"
1590
+ }), /* @__PURE__ */ d("button", {
1591
+ type: "button",
1592
+ "aria-label": "Delete matrix",
1593
+ title: "Delete matrix",
1594
+ onClick: () => Nn(),
1595
+ style: {
1596
+ position: "absolute",
1597
+ left: `${hr.x + mn(hr) / 2 + 36}px`,
1598
+ top: `${hr.y - 30}px`,
1599
+ width: "28px",
1600
+ height: "28px",
1601
+ display: "flex",
1602
+ alignItems: "center",
1603
+ justifyContent: "center",
1604
+ border: "1px solid #f5c2c7",
1605
+ borderRadius: "999px",
1606
+ background: "#fff5f5",
1607
+ cursor: "pointer",
1608
+ zIndex: 1150
1609
+ },
1610
+ children: /* @__PURE__ */ d(Me, {})
1611
+ })] }),
1612
+ gr && !$ && /* @__PURE__ */ d("button", {
1613
+ type: "button",
1614
+ "aria-label": "Delete align block",
1615
+ title: "Delete align block",
1616
+ onClick: () => jn(),
1617
+ style: {
1618
+ position: "absolute",
1619
+ left: `${gr.x + gr.width / 2 - 14}px`,
1620
+ top: `${gr.y - 30}px`,
1621
+ width: "28px",
1622
+ height: "28px",
1623
+ display: "flex",
1624
+ alignItems: "center",
1625
+ justifyContent: "center",
1626
+ border: "1px solid #f5c2c7",
1627
+ borderRadius: "999px",
1628
+ background: "#fff5f5",
1629
+ cursor: "pointer",
1630
+ zIndex: 1150
1631
+ },
1632
+ children: /* @__PURE__ */ d(Me, {})
1633
+ }),
1634
+ /* @__PURE__ */ f("div", {
1635
+ style: {
1636
+ position: "absolute",
1637
+ inset: 0,
1638
+ zIndex: 1e3,
1639
+ pointerEvents: "none"
1640
+ },
1641
+ children: [
1642
+ lt && /* @__PURE__ */ d("div", {
1643
+ style: {
1644
+ position: "absolute",
1645
+ left: `${lt.left}px`,
1646
+ top: `${lt.top}px`,
1647
+ transform: "translate(-10%, -100%)",
1648
+ maxWidth: "240px",
1649
+ padding: "6px 12px",
1650
+ border: "1px solid #d9d9d9",
1651
+ borderRadius: "999px",
1652
+ background: "rgba(255, 255, 255, 0.96)",
1653
+ color: "#111",
1654
+ boxShadow: "0 2px 8px rgba(0, 0, 0, 0.08)",
1655
+ whiteSpace: "nowrap",
1656
+ overflow: "hidden",
1657
+ textOverflow: "ellipsis",
1658
+ pointerEvents: "none"
1659
+ },
1660
+ children: /* @__PURE__ */ d(Re, {
1661
+ expression: lt.expression,
1662
+ fallback: lt.fallback
1663
+ })
1664
+ }),
1665
+ S.map((e) => {
1666
+ let t = I === e.id, n = dn(e), r = mn(e), i = hn(e);
1667
+ return t ? /* @__PURE__ */ f("div", {
1668
+ style: {
1669
+ position: "absolute",
1670
+ top: `${e.y}px`,
1671
+ left: `${e.x}px`,
1672
+ width: `${r}px`,
1673
+ minHeight: `${i}px`,
1674
+ padding: `${g}px ${g}px`,
1675
+ border: "1px solid #d9d9d9",
1676
+ background: "transparent",
1677
+ boxSizing: "border-box",
1678
+ pointerEvents: "auto"
1679
+ },
1680
+ children: [
1681
+ /* @__PURE__ */ d("div", { style: {
1682
+ position: "absolute",
1683
+ top: `${g - 2}px`,
1684
+ bottom: `${g - 2}px`,
1685
+ left: "4px",
1686
+ width: "10px",
1687
+ borderLeft: "2px solid #111",
1688
+ borderTop: "2px solid #111",
1689
+ borderBottom: "2px solid #111",
1690
+ boxSizing: "border-box",
1691
+ pointerEvents: "none"
1692
+ } }),
1693
+ /* @__PURE__ */ d("div", { style: {
1694
+ position: "absolute",
1695
+ top: `${g - 2}px`,
1696
+ bottom: `${g - 2}px`,
1697
+ right: "4px",
1698
+ width: "10px",
1699
+ borderRight: "2px solid #111",
1700
+ borderTop: "2px solid #111",
1701
+ borderBottom: "2px solid #111",
1702
+ boxSizing: "border-box",
1703
+ pointerEvents: "none"
1704
+ } }),
1705
+ /* @__PURE__ */ f("div", {
1706
+ style: {
1707
+ display: "grid",
1708
+ gridTemplateColumns: n.map((e) => `${e}px`).join(" "),
1709
+ gap: 6,
1710
+ alignItems: "center",
1711
+ justifyContent: "center",
1712
+ position: "relative"
1713
+ },
1714
+ children: [
1715
+ e.cols > 1 && Array.from({ length: e.cols - 1 }, (t, r) => {
1716
+ let i = n.slice(0, r + 1).reduce((e, t) => e + t, 0) + r * 6 + 3;
1717
+ return /* @__PURE__ */ d("button", {
1718
+ type: "button",
1719
+ onMouseDown: (e) => e.stopPropagation(),
1720
+ onClick: (t) => {
1721
+ t.stopPropagation(), Sn(e.id, r);
1722
+ },
1723
+ style: {
1724
+ position: "absolute",
1725
+ top: `${-_ - 4}px`,
1726
+ left: `${i - _ / 2}px`,
1727
+ width: `${_}px`,
1728
+ height: `${_}px`,
1729
+ border: "1px solid #d9d9d9",
1730
+ borderRadius: "999px",
1731
+ background: e.colSeparators.includes(r) ? "#111" : "transparent",
1732
+ color: e.colSeparators.includes(r) ? "#fff" : "#111",
1733
+ fontSize: 12,
1734
+ lineHeight: 1,
1735
+ cursor: "pointer"
1736
+ },
1737
+ children: "|"
1738
+ }, `col-separator-${e.id}-${r}`);
1739
+ }),
1740
+ e.rowSeparators.map((t) => /* @__PURE__ */ d("div", { style: {
1741
+ position: "absolute",
1742
+ left: "0",
1743
+ right: "0",
1744
+ top: `${(t + 1) * Ce + t * 6 + 3}px`,
1745
+ borderTop: "1px solid #111",
1746
+ pointerEvents: "none"
1747
+ } }, `row-line-${e.id}-${t}`)),
1748
+ e.colSeparators.map((t) => /* @__PURE__ */ d("div", { style: {
1749
+ position: "absolute",
1750
+ top: "0",
1751
+ bottom: "0",
1752
+ left: `${n.slice(0, t + 1).reduce((e, t) => e + t, 0) + t * 6 + 6}px`,
1753
+ borderLeft: "1px solid #111",
1754
+ pointerEvents: "none"
1755
+ } }, `col-line-${e.id}-${t}`)),
1756
+ e.cells.map((t, r) => t.map((t, i) => /* @__PURE__ */ d("input", {
1757
+ ref: (t) => {
1758
+ Xt.current[`${e.id}-${r}-${i}`] = t;
1759
+ },
1760
+ "data-matrix-id": e.id,
1761
+ value: t,
1762
+ onChange: (t) => {
1763
+ Mn(e.id, r, i, t.target.value);
1764
+ },
1765
+ onBlur: (e) => {
1766
+ e.currentTarget.parentElement?.contains(e.relatedTarget) || Tn();
1767
+ },
1768
+ onMouseDown: (e) => e.stopPropagation(),
1769
+ onClick: (e) => {
1770
+ e.stopPropagation(), L({
1771
+ row: r,
1772
+ col: i
1773
+ });
1774
+ },
1775
+ onFocus: () => L({
1776
+ row: r,
1777
+ col: i
1778
+ }),
1779
+ onKeyDown: (t) => {
1780
+ if ((t.ctrlKey || t.metaKey) && t.key === "ArrowUp") {
1781
+ t.preventDefault(), Z(), bn(e.id, e.rows - 1, e.cols), L({
1782
+ row: Math.min(r, Math.max(0, e.rows - 2)),
1783
+ col: i
1784
+ });
1785
+ return;
1786
+ }
1787
+ if ((t.ctrlKey || t.metaKey) && t.key === "ArrowDown") {
1788
+ t.preventDefault(), Z(), bn(e.id, e.rows + 1, e.cols), L({
1789
+ row: r,
1790
+ col: i
1791
+ });
1792
+ return;
1793
+ }
1794
+ if ((t.ctrlKey || t.metaKey) && t.key === "ArrowLeft") {
1795
+ t.preventDefault(), Z(), bn(e.id, e.rows, e.cols - 1), L({
1796
+ row: r,
1797
+ col: Math.min(i, Math.max(0, e.cols - 2))
1798
+ });
1799
+ return;
1800
+ }
1801
+ if ((t.ctrlKey || t.metaKey) && t.key === "ArrowRight") {
1802
+ t.preventDefault(), Z(), bn(e.id, e.rows, e.cols + 1), L({
1803
+ row: r,
1804
+ col: i
1805
+ });
1806
+ return;
1807
+ }
1808
+ if (t.key === "ArrowUp") {
1809
+ t.preventDefault(), L({
1810
+ row: Math.max(0, r - 1),
1811
+ col: i
1812
+ });
1813
+ return;
1814
+ }
1815
+ if (t.key === "ArrowDown") {
1816
+ t.preventDefault(), L({
1817
+ row: Math.min(e.rows - 1, r + 1),
1818
+ col: i
1819
+ });
1820
+ return;
1821
+ }
1822
+ if (t.key === "ArrowLeft") {
1823
+ t.preventDefault(), L({
1824
+ row: r,
1825
+ col: Math.max(0, i - 1)
1826
+ });
1827
+ return;
1828
+ }
1829
+ if (t.key === "ArrowRight") {
1830
+ t.preventDefault(), L({
1831
+ row: r,
1832
+ col: Math.min(e.cols - 1, i + 1)
1833
+ });
1834
+ return;
1835
+ }
1836
+ if (t.key === "Escape") {
1837
+ t.preventDefault(), Tn();
1838
+ return;
1839
+ }
1840
+ t.key === "Enter" && (t.preventDefault(), L({
1841
+ row: Math.min(e.rows - 1, r + 1),
1842
+ col: i
1843
+ }));
1844
+ },
1845
+ style: {
1846
+ width: "100%",
1847
+ minWidth: `${n[i]}px`,
1848
+ height: `${Ce}px`,
1849
+ border: "none",
1850
+ outline: "none",
1851
+ textAlign: "center",
1852
+ fontSize: 16,
1853
+ fontFamily: v,
1854
+ boxSizing: "border-box",
1855
+ background: "transparent",
1856
+ padding: `0 ${we}px`
1857
+ }
1858
+ }, `${e.id}-${r}-${i}`)))
1859
+ ]
1860
+ }),
1861
+ e.rows > 1 && Array.from({ length: e.rows - 1 }, (t, n) => /* @__PURE__ */ d("button", {
1862
+ type: "button",
1863
+ onMouseDown: (e) => e.stopPropagation(),
1864
+ onClick: (t) => {
1865
+ t.stopPropagation(), xn(e.id, n);
1866
+ },
1867
+ style: {
1868
+ position: "absolute",
1869
+ left: `${-_ / 2 + 2}px`,
1870
+ top: `${g + (n + 1) * Ce + n * 6 - _ / 2 + 3}px`,
1871
+ width: `${_}px`,
1872
+ height: `${_}px`,
1873
+ border: "1px solid #d9d9d9",
1874
+ borderRadius: "999px",
1875
+ background: e.rowSeparators.includes(n) ? "#111" : "transparent",
1876
+ color: e.rowSeparators.includes(n) ? "#fff" : "#111",
1877
+ fontSize: 12,
1878
+ lineHeight: 1,
1879
+ cursor: "pointer"
1880
+ },
1881
+ children: "-"
1882
+ }, `row-separator-${e.id}-${n}`))
1883
+ ]
1884
+ }, e.id) : /* @__PURE__ */ d("div", {
1885
+ ref: (t) => {
1886
+ qt.current[e.id] = t;
1887
+ },
1888
+ style: {
1889
+ position: "absolute",
1890
+ top: `${e.y}px`,
1891
+ left: `${e.x}px`,
1892
+ width: `${r}px`,
1893
+ minHeight: `${i}px`,
1894
+ display: "flex",
1895
+ alignItems: "center",
1896
+ justifyContent: "center",
1897
+ boxSizing: "border-box",
1898
+ pointerEvents: C === "select" ? "none" : "auto",
1899
+ cursor: C === "matrix" ? "text" : C === "select" ? "move" : "default",
1900
+ border: A === e.id ? "1px solid #d9d9d9" : "none",
1901
+ background: "transparent"
1902
+ },
1903
+ onMouseDown: (e) => e.stopPropagation(),
1904
+ onClick: (t) => {
1905
+ if (t.stopPropagation(), C === "matrix") {
1906
+ A === e.id ? wn(e.id) : (j(e.id), T(null), J());
1907
+ return;
1908
+ }
1909
+ C === "select" && (j(e.id), T(null), J());
1910
+ },
1911
+ onDoubleClick: (t) => {
1912
+ t.stopPropagation(), wn(e.id);
1913
+ },
1914
+ children: /* @__PURE__ */ d(Re, {
1915
+ expression: pn(e),
1916
+ fallback: e.cells.map((e) => `[${e.join(", ")}]`).join(" ")
1917
+ })
1918
+ }, e.id);
1919
+ }),
1920
+ $e.map((e) => e.id === R ? null : /* @__PURE__ */ d("div", {
1921
+ style: {
1922
+ position: "absolute",
1923
+ top: `${e.y}px`,
1924
+ left: `${e.x}px`,
1925
+ width: `${e.width}px`,
1926
+ minHeight: `${e.height}px`,
1927
+ pointerEvents: C === "select" ? "none" : "auto",
1928
+ boxSizing: "border-box",
1929
+ cursor: C === "align" ? "text" : C === "select" ? "move" : "default",
1930
+ border: M === e.id ? "1px solid #d9d9d9" : "none",
1931
+ background: "transparent",
1932
+ padding: "6px 8px"
1933
+ },
1934
+ onMouseDown: (e) => e.stopPropagation(),
1935
+ onClick: (t) => {
1936
+ if (t.stopPropagation(), C === "align") {
1937
+ M === e.id ? kn(e.id) : (N(e.id), T(null), J(), Y());
1938
+ return;
1939
+ }
1940
+ C === "select" && (N(e.id), T(null), J(), Y());
1941
+ },
1942
+ onDoubleClick: (t) => {
1943
+ t.stopPropagation(), kn(e.id);
1944
+ },
1945
+ children: /* @__PURE__ */ d(Re, {
1946
+ expression: sn(e),
1947
+ fallback: e.content
1948
+ })
1949
+ }, e.id)),
1950
+ fr && /* @__PURE__ */ d("div", {
1951
+ style: {
1952
+ position: "absolute",
1953
+ top: `${fr.y}px`,
1954
+ left: `${fr.x}px`,
1955
+ width: `${fr.width}px`,
1956
+ height: `${fr.height}px`,
1957
+ display: "flex",
1958
+ alignItems: "center",
1959
+ justifyContent: "center",
1960
+ pointerEvents: "auto"
1961
+ },
1962
+ children: /* @__PURE__ */ d("input", {
1963
+ ref: Yt,
1964
+ autoFocus: !0,
1965
+ value: ft,
1966
+ placeholder: "",
1967
+ onChange: (e) => pt(e.target.value),
1968
+ onBlur: zn,
1969
+ onMouseDown: (e) => e.stopPropagation(),
1970
+ onClick: (e) => e.stopPropagation(),
1971
+ onKeyDown: (e) => {
1972
+ e.key === "Enter" && (e.preventDefault(), zn()), e.key === "Escape" && (e.preventDefault(), Bn());
1973
+ },
1974
+ style: {
1975
+ width: "100%",
1976
+ height: "100%",
1977
+ padding: "0 10px",
1978
+ border: "1px solid #d9d9d9",
1979
+ borderRadius: 999,
1980
+ outline: "none",
1981
+ background: "transparent",
1982
+ color: "#111",
1983
+ fontSize: 18,
1984
+ textAlign: "center",
1985
+ fontFamily: v,
1986
+ boxSizing: "border-box"
1987
+ }
1988
+ })
1989
+ }),
1990
+ $ && /* @__PURE__ */ f("div", {
1991
+ style: {
1992
+ position: "absolute",
1993
+ top: `${$.y}px`,
1994
+ left: `${$.x}px`,
1995
+ width: `${$.width}px`,
1996
+ minHeight: `${$.height}px`,
1997
+ border: "1px solid #d9d9d9",
1998
+ background: "transparent",
1999
+ boxSizing: "border-box",
2000
+ pointerEvents: "auto",
2001
+ padding: "6px 8px"
2002
+ },
2003
+ children: [/* @__PURE__ */ f("div", {
2004
+ style: {
2005
+ display: "flex",
2006
+ gap: 6,
2007
+ marginBottom: 6
2008
+ },
2009
+ children: [
2010
+ /* @__PURE__ */ d("button", {
2011
+ type: "button",
2012
+ onMouseDown: (e) => e.stopPropagation(),
2013
+ onClick: () => {
2014
+ on($.id, (e) => ({
2015
+ ...e,
2016
+ alignMode: "left"
2017
+ }));
2018
+ },
2019
+ style: {
2020
+ border: "1px solid #d9d9d9",
2021
+ background: $.alignMode === "left" ? "#111" : "#fff",
2022
+ color: $.alignMode === "left" ? "#fff" : "#111",
2023
+ borderRadius: 999,
2024
+ padding: "2px 8px",
2025
+ cursor: "pointer",
2026
+ fontSize: 12
2027
+ },
2028
+ children: "Left"
2029
+ }),
2030
+ /* @__PURE__ */ d("button", {
2031
+ type: "button",
2032
+ onMouseDown: (e) => e.stopPropagation(),
2033
+ onClick: () => {
2034
+ on($.id, (e) => ({
2035
+ ...e,
2036
+ alignMode: "token"
2037
+ }));
2038
+ },
2039
+ style: {
2040
+ border: "1px solid #d9d9d9",
2041
+ background: $.alignMode === "token" ? "#111" : "#fff",
2042
+ color: $.alignMode === "token" ? "#fff" : "#111",
2043
+ borderRadius: 999,
2044
+ padding: "2px 8px",
2045
+ cursor: "pointer",
2046
+ fontSize: 12
2047
+ },
2048
+ children: "Align Token"
2049
+ }),
2050
+ /* @__PURE__ */ d("input", {
2051
+ value: $.alignToken,
2052
+ onChange: (e) => {
2053
+ on($.id, (t) => ({
2054
+ ...t,
2055
+ alignToken: e.target.value || "="
2056
+ }));
2057
+ },
2058
+ onMouseDown: (e) => e.stopPropagation(),
2059
+ style: {
2060
+ width: 28,
2061
+ border: "1px solid #d9d9d9",
2062
+ background: "transparent",
2063
+ textAlign: "center"
2064
+ }
2065
+ })
2066
+ ]
2067
+ }), /* @__PURE__ */ d("textarea", {
2068
+ ref: Zt,
2069
+ value: _t,
2070
+ onChange: (e) => vt(e.target.value),
2071
+ onBlur: An,
2072
+ onMouseDown: (e) => e.stopPropagation(),
2073
+ onClick: (e) => e.stopPropagation(),
2074
+ onKeyDown: (e) => {
2075
+ e.key === "Escape" && (e.preventDefault(), q()), (e.ctrlKey || e.metaKey) && e.key === "Enter" && (e.preventDefault(), An());
2076
+ },
2077
+ style: {
2078
+ width: "100%",
2079
+ minHeight: `${Math.max($.height - 36, 56)}px`,
2080
+ border: "none",
2081
+ outline: "none",
2082
+ resize: "none",
2083
+ fontSize: Te,
2084
+ lineHeight: 1.4,
2085
+ fontFamily: v,
2086
+ color: "#111",
2087
+ background: "transparent",
2088
+ boxSizing: "border-box",
2089
+ whiteSpace: "pre"
2090
+ }
2091
+ })]
2092
+ }),
2093
+ y.map((e) => e.id === P ? null : /* @__PURE__ */ d("div", {
2094
+ ref: (t) => {
2095
+ Kt.current[e.id] = t;
2096
+ },
2097
+ style: {
2098
+ position: "absolute",
2099
+ top: `${e.y}px`,
2100
+ left: `${e.x}px`,
2101
+ width: `${Ot[e.id] ?? e.width}px`,
2102
+ minHeight: `${e.height}px`,
2103
+ padding: `${ce}px ${se}px ${ce}px ${oe}px`,
2104
+ color: "#111",
2105
+ fontSize: Te,
2106
+ lineHeight: 1.35,
2107
+ fontFamily: v,
2108
+ whiteSpace: "pre",
2109
+ wordBreak: "normal",
2110
+ pointerEvents: C === "select" ? "none" : "auto",
2111
+ boxSizing: "border-box",
2112
+ overflow: "visible",
2113
+ cursor: C === "text" ? "text" : C === "select" ? "move" : "default",
2114
+ border: w === e.id ? "1px solid #d9d9d9" : "none",
2115
+ background: "transparent",
2116
+ transition: "border-color 120ms ease"
2117
+ },
2118
+ onMouseDown: (e) => e.stopPropagation(),
2119
+ onClick: (t) => {
2120
+ if (t.stopPropagation(), C === "text") {
2121
+ Zn(e.id);
2122
+ return;
2123
+ }
2124
+ C === "select" && (J(), Y(), T(e.id), rt(e.renderMode));
2125
+ },
2126
+ onDoubleClick: (t) => {
2127
+ t.stopPropagation(), Zn(e.id);
2128
+ },
2129
+ onMouseEnter: (t) => {
2130
+ w !== e.id && (t.currentTarget.style.border = "1px solid #d9d9d9");
2131
+ },
2132
+ onMouseLeave: (t) => {
2133
+ w !== e.id && (t.currentTarget.style.border = "none");
2134
+ },
2135
+ children: /* @__PURE__ */ d(je, {
2136
+ content: e.content,
2137
+ renderMode: e.renderMode
2138
+ })
2139
+ }, e.id)),
2140
+ ur && /* @__PURE__ */ d("div", {
2141
+ style: {
2142
+ position: "absolute",
2143
+ top: `${ur.y}px`,
2144
+ left: `${ur.x}px`,
2145
+ width: `${Math.min(Math.max(wt, m), Math.max(m, V.width - ur.x - ie))}px`,
2146
+ minHeight: `${ur.height}px`,
2147
+ pointerEvents: "auto",
2148
+ cursor: "text",
2149
+ border: "1px solid #d9d9d9",
2150
+ boxSizing: "border-box",
2151
+ background: "transparent"
2152
+ },
2153
+ children: /* @__PURE__ */ d("textarea", {
2154
+ ref: Jt,
2155
+ autoFocus: !0,
2156
+ value: at,
2157
+ placeholder: "",
2158
+ onChange: (e) => {
2159
+ ot(e.target.value), ct(e.target.selectionStart);
2160
+ },
2161
+ onSelect: (e) => ct(e.currentTarget.selectionStart),
2162
+ onBlur: Qn,
2163
+ onMouseDown: (e) => e.stopPropagation(),
2164
+ onClick: (e) => {
2165
+ e.stopPropagation(), ct(e.currentTarget.selectionStart);
2166
+ },
2167
+ onKeyUp: (e) => ct(e.currentTarget.selectionStart),
2168
+ onKeyDown: (e) => {
2169
+ e.key === "Escape" && (e.preventDefault(), Q()), (e.ctrlKey || e.metaKey) && e.key === "Enter" && (e.preventDefault(), Qn()), (e.ctrlKey || e.metaKey) && e.key.toLowerCase() === "m" && (e.preventDefault(), sr());
2170
+ },
2171
+ style: {
2172
+ width: "100%",
2173
+ minHeight: `${ur.height}px`,
2174
+ height: `${Math.max(St, ur.height)}px`,
2175
+ padding: `${ce}px ${se}px ${ce}px ${oe}px`,
2176
+ border: "none",
2177
+ borderRadius: 0,
2178
+ outline: "none",
2179
+ fontSize: Te,
2180
+ lineHeight: 1.35,
2181
+ fontFamily: v,
2182
+ color: "#111",
2183
+ background: "transparent",
2184
+ boxSizing: "border-box",
2185
+ resize: "none",
2186
+ overflow: "hidden",
2187
+ whiteSpace: "pre",
2188
+ wordBreak: "normal",
2189
+ boxShadow: "none",
2190
+ appearance: "none",
2191
+ overflowX: "hidden"
2192
+ }
2193
+ })
2194
+ })
2195
+ ]
2196
+ })
2197
+ ]
2198
+ });
2199
+ }
2200
+ //#endregion
2201
+ export { x as MathCanvas, x as default };