@superdoc-dev/template-builder 1.1.0 → 1.2.0-next.2

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.mjs CHANGED
@@ -1,1105 +1,1164 @@
1
- import { jsxs as o, jsx as r } from "react/jsx-runtime";
2
- import { useState as E, useEffect as oe, useMemo as pe, forwardRef as We, useRef as q, useCallback as S, useImperativeHandle as He } from "react";
3
- const Le = ({
4
- isVisible: n,
5
- position: l,
6
- availableFields: m,
7
- filteredFields: s,
8
- filterQuery: p,
9
- allowCreate: c,
10
- onSelect: N,
11
- onClose: H,
12
- onCreateField: G,
13
- existingFields: f = [],
14
- onSelectExisting: T
15
- }) => {
16
- const [y, I] = E(!1), [u, L] = E(""), [P, z] = E("inline"), [Q, ie] = E(!0), [X, g] = E(!0);
17
- oe(() => {
18
- n || (I(!1), L(""), z("inline"));
19
- }, [n]);
20
- const A = pe(() => ({
21
- position: "absolute",
22
- left: l?.left,
23
- top: l?.top,
24
- zIndex: 1e3,
25
- background: "white",
26
- border: "1px solid #ddd",
27
- borderRadius: "4px",
28
- boxShadow: "0 2px 8px rgba(0,0,0,0.1)",
29
- padding: "8px 0",
30
- width: "280px"
31
- }), [l]), R = s ?? m, O = !!p;
32
- if (oe(() => {
33
- O && g(!0);
34
- }, [O]), !n) return null;
35
- const _ = async () => {
36
- const a = u.trim();
37
- if (!a) return;
38
- const V = {
39
- id: `custom_${Date.now()}`,
40
- label: a,
41
- mode: P
42
- };
43
- try {
44
- if (G) {
45
- const b = await G(V);
46
- N(b || V);
47
- } else
48
- N(V);
49
- } finally {
50
- I(!1), L(""), z("inline");
51
- }
52
- };
53
- return /* @__PURE__ */ o("div", { className: "superdoc-field-menu", style: A, children: [
54
- O && /* @__PURE__ */ r(
55
- "div",
56
- {
57
- style: {
58
- padding: "8px 16px",
59
- borderBottom: "1px solid #f0f0f0",
60
- marginBottom: "4px"
61
- },
62
- children: /* @__PURE__ */ o("div", { style: { fontSize: "12px", color: "#6b7280" }, children: [
63
- "Filtering results for",
64
- /* @__PURE__ */ r("span", { style: { fontWeight: 600, color: "#111827", marginLeft: "4px" }, children: p })
65
- ] })
66
- }
67
- ),
68
- c && !y && /* @__PURE__ */ r(
69
- "div",
70
- {
71
- className: "field-menu-item",
72
- onClick: () => I(!0),
73
- style: {
74
- padding: "8px 16px",
75
- cursor: "pointer",
76
- color: "#0066cc",
77
- fontWeight: 500
78
- },
79
- children: "+ Create New Field"
80
- }
81
- ),
82
- c && y && /* @__PURE__ */ o("div", { style: { padding: "8px 16px" }, children: [
83
- /* @__PURE__ */ r(
84
- "input",
85
- {
86
- type: "text",
87
- value: u,
88
- placeholder: "Field name...",
89
- onChange: (a) => L(a.target.value),
90
- onKeyDown: (a) => {
91
- a.key === "Enter" && _(), a.key === "Escape" && (I(!1), L(""), z("inline"));
92
- },
93
- autoFocus: !0,
94
- style: {
95
- width: "100%",
96
- padding: "4px 8px",
97
- border: "1px solid #ddd",
98
- borderRadius: "3px"
99
- }
100
- }
101
- ),
102
- /* @__PURE__ */ o(
103
- "div",
104
- {
105
- style: {
106
- marginTop: "8px",
107
- display: "flex",
108
- gap: "12px",
109
- fontSize: "13px"
110
- },
111
- children: [
112
- /* @__PURE__ */ o(
113
- "label",
114
- {
115
- style: {
116
- display: "flex",
117
- alignItems: "center",
118
- gap: "4px",
119
- cursor: "pointer"
120
- },
121
- children: [
122
- /* @__PURE__ */ r(
123
- "input",
124
- {
125
- type: "radio",
126
- value: "inline",
127
- checked: P === "inline",
128
- onChange: () => z("inline")
129
- }
130
- ),
131
- "Inline"
132
- ]
133
- }
134
- ),
135
- /* @__PURE__ */ o(
136
- "label",
137
- {
138
- style: {
139
- display: "flex",
140
- alignItems: "center",
141
- gap: "4px",
142
- cursor: "pointer"
143
- },
144
- children: [
145
- /* @__PURE__ */ r(
146
- "input",
147
- {
148
- type: "radio",
149
- value: "block",
150
- checked: P === "block",
151
- onChange: () => z("block")
152
- }
153
- ),
154
- "Block"
155
- ]
156
- }
157
- )
158
- ]
159
- }
160
- ),
161
- /* @__PURE__ */ o(
162
- "div",
163
- {
164
- style: {
165
- marginTop: "8px",
166
- display: "flex",
167
- gap: "8px"
168
- },
169
- children: [
170
- /* @__PURE__ */ r(
171
- "button",
172
- {
173
- onClick: _,
174
- disabled: !u.trim(),
175
- style: {
176
- padding: "4px 12px",
177
- background: u.trim() ? "#0066cc" : "#ccc",
178
- color: "white",
179
- border: "none",
180
- borderRadius: "3px",
181
- cursor: u.trim() ? "pointer" : "not-allowed"
182
- },
183
- children: "Create"
184
- }
185
- ),
186
- /* @__PURE__ */ r(
187
- "button",
188
- {
189
- onClick: () => {
190
- I(!1), L(""), z("inline");
191
- },
192
- style: {
193
- padding: "4px 12px",
194
- background: "white",
195
- border: "1px solid #ddd",
196
- borderRadius: "3px",
197
- cursor: "pointer"
198
- },
199
- children: "Cancel"
200
- }
201
- )
202
- ]
203
- }
204
- )
205
- ] }),
206
- c && m.length > 0 && /* @__PURE__ */ r(
207
- "div",
208
- {
209
- style: {
210
- borderTop: "1px solid #eee",
211
- margin: "4px 0"
212
- }
213
- }
214
- ),
215
- f.length > 0 && (() => {
216
- const a = /* @__PURE__ */ new Map();
217
- f.forEach((b) => {
218
- const Y = b.group || `individual-${b.id}`, ee = a.get(Y) || [];
219
- ee.push(b), a.set(Y, ee);
220
- });
221
- const V = Array.from(a.values()).map((b) => ({
222
- ...b[0],
223
- count: b.length
224
- }));
225
- return /* @__PURE__ */ o("div", { style: { borderBottom: "1px solid #f0f0f0" }, children: [
226
- /* @__PURE__ */ o(
227
- "button",
228
- {
229
- type: "button",
230
- onClick: () => ie(!Q),
231
- style: {
232
- width: "100%",
233
- display: "flex",
234
- alignItems: "center",
235
- justifyContent: "space-between",
236
- padding: "8px 16px",
237
- background: "transparent",
238
- border: "none",
239
- cursor: "pointer",
240
- fontWeight: 500,
241
- fontSize: "13px",
242
- color: "#374151",
243
- textAlign: "left"
244
- },
245
- children: [
246
- /* @__PURE__ */ o("span", { children: [
247
- "Existing Fields (",
248
- V.length,
249
- ")"
250
- ] }),
251
- /* @__PURE__ */ r(
252
- "span",
253
- {
254
- "aria-hidden": !0,
255
- style: {
256
- display: "inline-block",
257
- width: "8px",
258
- height: "8px",
259
- borderRight: "2px solid #666",
260
- borderBottom: "2px solid #666",
261
- transform: Q ? "rotate(45deg)" : "rotate(-45deg)",
262
- transition: "transform 0.2s ease"
263
- }
264
- }
265
- )
266
- ]
267
- }
268
- ),
269
- Q && /* @__PURE__ */ r("div", { style: { maxHeight: "300px", overflowY: "auto" }, children: V.map((b) => /* @__PURE__ */ o(
270
- "div",
271
- {
272
- className: "field-menu-item",
273
- onClick: () => T?.(b),
274
- style: {
275
- padding: "8px 16px",
276
- cursor: "pointer",
277
- display: "flex",
278
- alignItems: "flex-start",
279
- justifyContent: "space-between",
280
- gap: "8px"
281
- },
282
- children: [
283
- /* @__PURE__ */ o("div", { style: { flex: 1, minWidth: 0 }, children: [
284
- /* @__PURE__ */ r("div", { style: { fontWeight: 500, fontSize: "13px" }, children: b.alias || b.id }),
285
- /* @__PURE__ */ r(
286
- "div",
287
- {
288
- style: {
289
- fontSize: "11px",
290
- color: "#9ca3af",
291
- marginTop: "2px"
292
- },
293
- children: b.group ? `group (${b.count} fields)` : `ID: ${b.id}`
294
- }
295
- )
296
- ] }),
297
- /* @__PURE__ */ r(
298
- "span",
299
- {
300
- style: {
301
- fontSize: "11px",
302
- color: "#6b7280",
303
- padding: "2px 6px",
304
- background: "#f3f4f6",
305
- borderRadius: "3px",
306
- textTransform: "capitalize",
307
- flexShrink: 0
308
- },
309
- children: b.mode || "inline"
310
- }
311
- )
312
- ]
313
- },
314
- b.group || b.id
315
- )) })
316
- ] });
317
- })(),
318
- R.length === 0 ? /* @__PURE__ */ r(
319
- "div",
320
- {
321
- style: {
322
- padding: "16px",
323
- fontSize: "13px",
324
- color: "#6b7280",
325
- textAlign: "center"
326
- },
327
- children: "No matching fields"
328
- }
329
- ) : /* @__PURE__ */ o("div", { style: { borderBottom: "1px solid #f0f0f0" }, children: [
330
- /* @__PURE__ */ o(
331
- "button",
332
- {
333
- type: "button",
334
- onClick: () => g(!X),
335
- style: {
336
- width: "100%",
337
- display: "flex",
338
- alignItems: "center",
339
- justifyContent: "space-between",
340
- padding: "8px 16px",
341
- background: "transparent",
342
- border: "none",
343
- cursor: "pointer",
344
- fontWeight: 500,
345
- fontSize: "13px",
346
- color: "#374151",
347
- textAlign: "left"
348
- },
349
- children: [
350
- /* @__PURE__ */ o("span", { children: [
351
- "Available Fields (",
352
- R.length,
353
- ")"
354
- ] }),
355
- /* @__PURE__ */ r(
356
- "span",
357
- {
358
- "aria-hidden": !0,
359
- style: {
360
- display: "inline-block",
361
- width: "8px",
362
- height: "8px",
363
- borderRight: "2px solid #666",
364
- borderBottom: "2px solid #666",
365
- transform: X ? "rotate(45deg)" : "rotate(-45deg)",
366
- transition: "transform 0.2s ease"
367
- }
368
- }
369
- )
370
- ]
371
- }
372
- ),
373
- X && /* @__PURE__ */ r("div", { style: { maxHeight: "300px", overflowY: "auto" }, children: R.map((a) => /* @__PURE__ */ o(
374
- "div",
375
- {
376
- className: "field-menu-item",
377
- onClick: () => N(a),
378
- style: {
379
- padding: "8px 16px",
380
- cursor: "pointer",
381
- display: "flex",
382
- alignItems: "flex-start",
383
- justifyContent: "space-between",
384
- gap: "8px"
385
- },
386
- children: [
387
- /* @__PURE__ */ o("div", { style: { flex: 1, minWidth: 0 }, children: [
388
- /* @__PURE__ */ r("div", { style: { fontWeight: 500, fontSize: "13px" }, children: a.label || a.id }),
389
- /* @__PURE__ */ o(
390
- "div",
391
- {
392
- style: {
393
- fontSize: "11px",
394
- color: "#9ca3af",
395
- marginTop: "2px"
396
- },
397
- children: [
398
- "ID: ",
399
- a.id
400
- ]
401
- }
402
- )
403
- ] }),
404
- /* @__PURE__ */ r(
405
- "span",
406
- {
407
- style: {
408
- fontSize: "11px",
409
- color: "#6b7280",
410
- padding: "2px 6px",
411
- background: "#f3f4f6",
412
- borderRadius: "3px",
413
- textTransform: "capitalize",
414
- flexShrink: 0
415
- },
416
- children: a.mode || "inline"
417
- }
418
- )
419
- ]
420
- },
421
- a.id
422
- )) })
423
- ] }),
424
- /* @__PURE__ */ r(
425
- "div",
426
- {
427
- style: {
428
- borderTop: "1px solid #eee",
429
- marginTop: "4px"
430
- },
431
- children: /* @__PURE__ */ r(
432
- "button",
433
- {
434
- onClick: H,
435
- style: {
436
- width: "100%",
437
- padding: "6px 16px",
438
- background: "#f3f4f6",
439
- border: "none",
440
- borderRadius: "0 0 4px 4px",
441
- cursor: "pointer"
442
- },
443
- children: "Close"
444
- }
445
- )
446
- }
447
- )
448
- ] });
449
- }, Pe = (n) => {
450
- const l = n.split("-");
451
- return l.length > 2 ? l[l.length - 1].substring(0, 6) : n.substring(0, 6);
452
- }, ve = ({ field: n, onSelect: l, onDelete: m, isSelected: s, isGrouped: p = !1 }) => /* @__PURE__ */ o(
453
- "div",
454
- {
455
- onClick: () => l(n),
456
- style: {
457
- position: "relative",
458
- padding: "10px 12px",
459
- background: s ? "#eff6ff" : "#f9fafb",
460
- border: s ? "1px solid #3b82f6" : "1px solid #e5e7eb",
461
- borderRadius: "6px",
462
- cursor: "pointer",
463
- transition: "all 0.2s",
464
- fontSize: p ? "13px" : "14px"
465
- },
466
- onMouseEnter: (c) => {
467
- s || (c.currentTarget.style.background = "#f3f4f6");
468
- },
469
- onMouseLeave: (c) => {
470
- s || (c.currentTarget.style.background = "#f9fafb");
471
- },
472
- title: n.alias,
473
- children: [
474
- /* @__PURE__ */ r(
475
- "button",
476
- {
477
- onClick: (c) => {
478
- c.stopPropagation(), m(n.id);
479
- },
480
- style: {
481
- position: "absolute",
482
- top: "6px",
483
- right: "6px",
484
- padding: "4px",
485
- background: "transparent",
486
- border: "none",
487
- cursor: "pointer",
488
- color: "#9ca3af",
489
- transition: "color 0.2s",
490
- display: "flex",
491
- alignItems: "center",
492
- justifyContent: "center"
493
- },
494
- onMouseEnter: (c) => {
495
- c.currentTarget.style.color = "#ef4444";
496
- },
497
- onMouseLeave: (c) => {
498
- c.currentTarget.style.color = "#9ca3af";
499
- },
500
- title: "Delete field",
501
- children: /* @__PURE__ */ r("svg", { width: "14", height: "14", viewBox: "0 0 16 16", fill: "none", xmlns: "http://www.w3.org/2000/svg", children: /* @__PURE__ */ r(
502
- "path",
503
- {
504
- d: "M6 2V1.5C6 1.22386 6.22386 1 6.5 1H9.5C9.77614 1 10 1.22386 10 1.5V2M2 4H14M12.6667 4L12.1991 11.0129C12.129 12.065 12.0939 12.5911 11.8667 12.99C11.6666 13.3412 11.3648 13.6235 11.0011 13.7998C10.588 14 10.0607 14 9.00623 14H6.99377C5.93927 14 5.41202 14 4.99889 13.7998C4.63517 13.6235 4.33339 13.3412 4.13332 12.99C3.90607 12.5911 3.871 12.065 3.80086 11.0129L3.33333 4",
505
- stroke: "currentColor",
506
- strokeWidth: "1.5",
507
- strokeLinecap: "round",
508
- strokeLinejoin: "round"
509
- }
510
- ) })
511
- }
512
- ),
513
- /* @__PURE__ */ o("div", { style: { paddingRight: "24px" }, children: [
514
- /* @__PURE__ */ r(
515
- "div",
516
- {
517
- style: {
518
- fontWeight: "500",
519
- fontSize: p ? "12px" : "14px",
520
- color: p ? "#6b7280" : "#111827"
521
- },
522
- children: n.alias || n.id
523
- }
524
- ),
525
- /* @__PURE__ */ o(
526
- "div",
527
- {
528
- style: {
529
- display: "flex",
530
- alignItems: "center",
531
- gap: "6px",
532
- fontSize: "11px",
533
- color: "#9ca3af",
534
- marginTop: "2px"
535
- },
536
- children: [
537
- /* @__PURE__ */ o("span", { children: [
538
- "ID: ",
539
- n.id
540
- ] }),
541
- n.mode && /* @__PURE__ */ r(
542
- "span",
543
- {
544
- style: {
545
- fontSize: "9px",
546
- padding: "2px 5px",
547
- borderRadius: "3px",
548
- background: n.mode === "block" ? "#dbeafe" : "#f3f4f6",
549
- color: n.mode === "block" ? "#1e40af" : "#4b5563",
550
- fontWeight: "500"
551
- },
552
- children: n.mode
553
- }
554
- )
555
- ]
556
- }
557
- )
558
- ] })
559
- ]
560
- }
561
- ), Ae = ({ fields: n, onSelect: l, onDelete: m, selectedFieldId: s }) => {
562
- const [p, c] = E(/* @__PURE__ */ new Set()), { groupedFields: N, ungroupedFields: H } = pe(() => {
563
- const f = {}, T = [];
564
- return n.forEach((y) => {
565
- y.group ? (f[y.group] || (f[y.group] = []), f[y.group].push(y)) : T.push(y);
566
- }), { groupedFields: f, ungroupedFields: T };
567
- }, [n]), G = (f) => {
568
- c((T) => {
569
- const y = new Set(T);
570
- return y.has(f) ? y.delete(f) : y.add(f), y;
571
- });
572
- };
573
- return /* @__PURE__ */ o(
574
- "div",
575
- {
576
- className: "superdoc-field-list",
577
- style: {
578
- width: "250px",
579
- background: "white",
580
- border: "1px solid #e5e7eb",
581
- borderRadius: "8px",
582
- padding: "16px"
583
- },
584
- children: [
585
- /* @__PURE__ */ o("h3", { style: { margin: "0 0 16px 0", fontSize: "16px", fontWeight: "600" }, children: [
586
- "Template Fields (",
587
- n.length,
588
- ")"
589
- ] }),
590
- n.length === 0 ? /* @__PURE__ */ o(
591
- "div",
592
- {
593
- style: {
594
- color: "#9ca3af",
595
- fontSize: "14px",
596
- textAlign: "center",
597
- padding: "20px 0"
598
- },
599
- children: [
600
- "No fields yet. Type ",
601
- "{{",
602
- " to add a field."
603
- ]
604
- }
605
- ) : /* @__PURE__ */ o("div", { style: { display: "flex", flexDirection: "column", gap: "8px" }, children: [
606
- H.map((f) => /* @__PURE__ */ r(
607
- ve,
608
- {
609
- field: f,
610
- onSelect: l,
611
- onDelete: m,
612
- isSelected: s === f.id
613
- },
614
- f.id
615
- )),
616
- Object.entries(N).map(([f, T]) => {
617
- const y = p.has(f), I = T[0];
618
- return /* @__PURE__ */ o("div", { children: [
619
- /* @__PURE__ */ r(
620
- "div",
621
- {
622
- style: {
623
- position: "relative",
624
- padding: "12px",
625
- background: "#f9fafb",
626
- border: "1px solid #e5e7eb",
627
- borderRadius: "6px",
628
- cursor: "pointer",
629
- transition: "all 0.2s"
630
- },
631
- onClick: () => G(f),
632
- onMouseEnter: (u) => {
633
- u.currentTarget.style.background = "#f3f4f6";
634
- },
635
- onMouseLeave: (u) => {
636
- u.currentTarget.style.background = "#f9fafb";
637
- },
638
- children: /* @__PURE__ */ o("div", { style: { display: "flex", alignItems: "center", gap: "8px" }, children: [
639
- /* @__PURE__ */ r("span", { style: { fontSize: "12px", color: "#6b7280" }, children: y ? "▼" : "▶" }),
640
- /* @__PURE__ */ o("div", { style: { flex: 1 }, children: [
641
- /* @__PURE__ */ r("div", { style: { fontWeight: "500", fontSize: "14px" }, children: I.alias }),
642
- /* @__PURE__ */ o(
643
- "div",
644
- {
645
- style: {
646
- fontSize: "11px",
647
- color: "#9ca3af",
648
- marginTop: "2px"
649
- },
650
- children: [
651
- "group: ",
652
- Pe(f),
653
- " (",
654
- T.length,
655
- " fields)"
656
- ]
657
- }
658
- )
659
- ] })
660
- ] })
661
- }
662
- ),
663
- y && /* @__PURE__ */ r(
664
- "div",
665
- {
666
- style: {
667
- marginLeft: "16px",
668
- marginTop: "4px",
669
- display: "flex",
670
- flexDirection: "column",
671
- gap: "4px"
672
- },
673
- children: T.map((u) => /* @__PURE__ */ r(
674
- ve,
675
- {
676
- field: u,
677
- onSelect: l,
678
- onDelete: m,
679
- isSelected: s === u.id,
680
- isGrouped: !0
681
- },
682
- u.id
683
- ))
684
- }
685
- )
686
- ] }, f);
687
- })
688
- ] })
689
- ]
690
- }
691
- );
692
- }, J = (n) => {
693
- const l = n.helpers?.structuredContentCommands;
694
- return l?.getStructuredContentTags ? (l.getStructuredContentTags(n.state) || []).map((s) => {
695
- const p = s?.node ?? s, c = p?.attrs ?? {}, H = (p?.type?.name || "").includes("Block") ? "block" : "inline";
696
- return {
697
- id: c.id,
698
- alias: c.alias || c.label || "",
699
- tag: c.tag,
700
- mode: H,
701
- group: l.getGroup?.(c.tag) ?? void 0
702
- };
703
- }) : [];
704
- }, Fe = (n, l) => {
705
- if (n === l) return !0;
706
- if (n.length !== l.length) return !1;
707
- for (let m = 0; m < n.length; m += 1) {
708
- const s = n[m], p = l[m];
709
- if (!p || s.id !== p.id || s.alias !== p.alias || s.tag !== p.tag || s.position !== p.position || s.mode !== p.mode || s.group !== p.group)
710
- return !1;
711
- }
712
- return !0;
713
- }, Ve = (n) => {
714
- if (!n) return null;
715
- if (n === !0)
716
- return {
717
- selector: "#superdoc-toolbar",
718
- config: {},
719
- renderDefaultContainer: !0
720
- };
721
- if (typeof n == "string")
722
- return {
723
- selector: n,
724
- config: {},
725
- renderDefaultContainer: !1
726
- };
727
- const { selector: l, ...m } = n;
728
- return {
729
- selector: l || "#superdoc-toolbar",
730
- config: m,
731
- renderDefaultContainer: l === void 0
732
- };
733
- }, re = 10, je = 250, Ge = 300, Ce = (n) => {
734
- const l = window.innerWidth - je - re, m = window.innerHeight - Ge - re, s = Math.min(n.left, l), p = Math.min(n.top, m);
735
- return new DOMRect(
736
- Math.max(s, re),
737
- Math.max(p, re),
738
- n.width,
739
- n.height
740
- );
741
- }, Oe = We(
742
- (n, l) => {
743
- const {
744
- document: m,
745
- fields: s = {},
746
- menu: p = {},
747
- list: c = {},
748
- toolbar: N,
749
- cspNonce: H,
750
- onReady: G,
751
- onTrigger: f,
752
- onFieldInsert: T,
753
- onFieldUpdate: y,
754
- onFieldDelete: I,
755
- onFieldsChange: u,
756
- onFieldSelect: L,
757
- onFieldCreate: P,
758
- onExport: z,
759
- className: Q,
760
- style: ie,
761
- documentHeight: X = "600px"
762
- } = n, [g, A] = E(s.initial || []), [R, O] = E(null), [_, a] = E(!1), [V, b] = E(), [Y, ee] = E(""), [we, ke] = E(() => s.available || []), se = q(null), w = q(null), M = q(null), fe = q(s);
763
- fe.current = s;
764
- const B = q(null), ge = q(_);
765
- oe(() => {
766
- ge.current = _;
767
- }, [_]);
768
- const te = p.trigger || "{{", le = fe.current.available || [], j = pe(() => Ve(N), [N]), me = S(
769
- (e) => {
770
- const t = e.trim().toLowerCase();
771
- return t ? le.filter((i) => i.label.toLowerCase().includes(t)) : le;
772
- },
773
- [le]
774
- ), de = S(
775
- (e) => {
776
- ee(e), ke(me(e));
777
- },
778
- [me]
779
- ), W = S(() => {
780
- de("");
781
- }, [de]), K = S(
782
- (e, t) => {
783
- if (!w.current?.activeEditor) return !1;
784
- const i = w.current.activeEditor, v = g, C = e === "inline" ? i.commands.insertStructuredContentInline?.({
785
- attrs: {
786
- alias: t.alias,
787
- tag: t.metadata ? JSON.stringify(t.metadata) : void 0
788
- },
789
- text: t.defaultValue || t.alias
790
- }) : i.commands.insertStructuredContentBlock?.({
791
- attrs: {
792
- alias: t.alias,
793
- tag: t.metadata ? JSON.stringify(t.metadata) : void 0
794
- },
795
- text: t.defaultValue || t.alias
796
- });
797
- if (C) {
798
- const x = J(i);
799
- A(x), u?.(x);
800
- const k = x.find(
801
- (F) => !v.some((d) => d.id === F.id)
802
- );
803
- k && T?.(k);
804
- }
805
- return C ?? !1;
806
- },
807
- [T, u, g]
808
- ), Z = S(
809
- (e, t) => {
810
- if (!w.current?.activeEditor) return !1;
811
- const v = w.current.activeEditor.commands.updateStructuredContentById?.(e, {
812
- attrs: t
813
- });
814
- return v && A((C) => {
815
- const x = C.map((F) => F.id === e ? { ...F, ...t } : F);
816
- u?.(x);
817
- const k = x.find((F) => F.id === e);
818
- return k && y?.(k), x;
819
- }), v ?? !1;
820
- },
821
- [y, u]
822
- ), ae = S(
823
- (e) => {
824
- const t = w.current?.activeEditor;
825
- if (!t) {
826
- let d = !1;
827
- return A((h) => {
828
- if (!h.some((D) => D.id === e)) return h;
829
- const U = h.filter((D) => D.id !== e);
830
- return d = !0, u?.(U), I?.(e), U;
831
- }), d && O((h) => h === e ? null : h), d;
832
- }
833
- const v = g.find((d) => d.id === e)?.group;
834
- let C = !1;
835
- try {
836
- C = t.commands.deleteStructuredContentById?.(e) ?? !1;
837
- } catch (d) {
838
- console.warn("[TemplateBuilder] Failed to delete structured content:", e, d), C = !1;
839
- }
840
- let x = J(t);
841
- const k = x.some((d) => d.id === e);
842
- if (!C && k && (x = x.filter((d) => d.id !== e)), v) {
843
- const d = x.filter((h) => h.group === v);
844
- if (d.length === 1) {
845
- const h = d[0];
846
- t.commands.updateStructuredContentById?.(h.id, {
847
- attrs: { tag: void 0 }
848
- }), x = J(t);
849
- }
850
- }
851
- let F = !1;
852
- return A((d) => {
853
- if (Fe(d, x))
854
- return d;
855
- const h = d.some((D) => D.id === e), U = x.some((D) => D.id === e);
856
- return h && !U && (F = !0), u?.(x), F && I?.(e), x;
857
- }), F && O((d) => d === e ? null : d), C || F;
858
- },
859
- [I, u, g]
860
- ), $ = S(
861
- (e) => {
862
- if (!w.current?.activeEditor) return;
863
- w.current.activeEditor.commands.selectStructuredContentById?.(e), O(e);
864
- const i = g.find((v) => v.id === e);
865
- i && L?.(i);
866
- },
867
- [g, L]
868
- ), ce = S(
869
- (e) => {
870
- if (!e) return;
871
- const t = J(e);
872
- A((i) => Fe(i, t) ? i : (u?.(t), t));
873
- },
874
- [u]
875
- );
876
- oe(() => {
877
- if (!se.current) return;
878
- let e = !1, t = null;
879
- return (async () => {
880
- const { SuperDoc: v } = await import("superdoc");
881
- if (e) return;
882
- const C = {
883
- comments: !1,
884
- ...j && {
885
- toolbar: {
886
- selector: j.selector,
887
- toolbarGroups: j.config.toolbarGroups || ["center"],
888
- excludeItems: j.config.excludeItems || [],
889
- ...j.config
890
- }
891
- }
892
- }, x = () => {
893
- if (!e) {
894
- if (t?.activeEditor) {
895
- const k = t.activeEditor;
896
- k.on("update", ({ editor: F }) => {
897
- const { state: d } = F, { from: h } = d.selection;
898
- if (h >= te.length) {
899
- const ue = h - te.length;
900
- if (d.doc.textBetween(ue, h) === te) {
901
- const he = F.view.coordsAtPos(h), be = Ce(new DOMRect(he.left, he.top, 0, 0)), ye = () => {
902
- const ne = w.current?.activeEditor;
903
- if (!ne) return;
904
- const ze = ne.state.selection.from, Be = ne.state.tr.delete(ue, ze);
905
- ne.view.dispatch(Be);
906
- };
907
- M.current = ye, B.current = h, b(be), a(!0), W(), f?.({
908
- position: { from: ue, to: h },
909
- bounds: be,
910
- cleanup: ye
911
- });
912
- return;
913
- }
914
- }
915
- if (!ge.current)
916
- return;
917
- if (B.current == null) {
918
- a(!1), W();
919
- return;
920
- }
921
- if (h < B.current) {
922
- a(!1), B.current = null, W();
923
- return;
924
- }
925
- const U = d.doc.textBetween(B.current, h);
926
- de(U);
927
- const D = F.view.coordsAtPos(h), Ne = Ce(new DOMRect(D.left, D.top, 0, 0));
928
- b(Ne);
929
- }), k.on("update", () => {
930
- ce(k);
931
- }), ce(k);
932
- }
933
- G?.();
934
- }
935
- };
936
- t = new v({
937
- selector: se.current,
938
- document: m?.source,
939
- documentMode: m?.mode || "editing",
940
- modules: C,
941
- toolbar: j?.selector,
942
- cspNonce: H,
943
- onReady: x
944
- }), w.current = t;
945
- })(), () => {
946
- e = !0, M.current = null, B.current = null, t && typeof t.destroy == "function" && t.destroy(), w.current = null;
947
- };
948
- }, [m?.source, m?.mode, te, ce, G, f, j, H]);
949
- const Se = S(
950
- async (e) => {
951
- M.current && (M.current(), M.current = null), B.current = null, W();
952
- const t = e.mode || "inline";
953
- if (e.id.startsWith("custom_") && P) {
954
- const i = await P(e);
955
- if (i) {
956
- const v = i.mode || t;
957
- K(v, {
958
- alias: i.label,
959
- metadata: i.metadata,
960
- defaultValue: i.defaultValue
961
- }), a(!1);
962
- return;
963
- }
964
- }
965
- K(t, {
966
- alias: e.label,
967
- metadata: e.metadata,
968
- defaultValue: e.defaultValue
969
- }), a(!1);
970
- },
971
- [K, P, W]
972
- ), Te = S(
973
- (e) => {
974
- M.current && (M.current(), M.current = null), B.current = null, W();
975
- const t = w.current?.activeEditor;
976
- if (!t) return;
977
- const i = t.helpers?.structuredContentCommands;
978
- if (!i) return;
979
- const v = e.group || `group-${Date.now()}-${Math.random().toString(36).substring(2, 11)}`, C = i.createTagObject?.({
980
- group: v
981
- });
982
- if ((e.mode || "inline") === "inline" ? t.commands.insertStructuredContentInline?.({
983
- attrs: {
984
- alias: e.alias,
985
- tag: C
986
- },
987
- text: e.alias
988
- }) : t.commands.insertStructuredContentBlock?.({
989
- attrs: {
990
- alias: e.alias,
991
- tag: C
992
- },
993
- text: e.alias
994
- })) {
995
- e.group || Z(e.id, { tag: C }), a(!1);
996
- const F = J(t);
997
- A(F), u?.(F);
998
- }
999
- },
1000
- [Z, W, u]
1001
- ), Ee = S(() => {
1002
- a(!1), B.current = null, W(), M.current && (M.current(), M.current = null);
1003
- }, [W]), Ie = S(() => {
1004
- if (!w.current?.activeEditor || g.length === 0) return;
1005
- const e = g.findIndex((i) => i.id === R), t = e >= 0 ? (e + 1) % g.length : 0;
1006
- $(g[t].id);
1007
- }, [g, R, $]), Me = S(() => {
1008
- if (!w.current?.activeEditor || g.length === 0) return;
1009
- const e = g.findIndex((i) => i.id === R), t = e > 0 ? e - 1 : g.length - 1;
1010
- $(g[t].id);
1011
- }, [g, R, $]), Re = S(
1012
- async (e) => {
1013
- const { fileName: t = "document", triggerDownload: i = !0 } = e || {}, v = await w.current?.export({
1014
- exportType: ["docx"],
1015
- exportedName: t,
1016
- triggerDownload: i
1017
- }), C = w.current?.activeEditor;
1018
- if (C) {
1019
- const x = J(C);
1020
- z?.({ fields: x, blob: i ? void 0 : v, fileName: t });
1021
- }
1022
- return v;
1023
- },
1024
- [z]
1025
- );
1026
- He(l, () => ({
1027
- insertField: (e) => K("inline", e),
1028
- insertBlockField: (e) => K("block", e),
1029
- updateField: Z,
1030
- deleteField: ae,
1031
- selectField: $,
1032
- nextField: Ie,
1033
- previousField: Me,
1034
- getFields: () => g,
1035
- exportTemplate: Re,
1036
- getSuperDoc: () => w.current
1037
- }));
1038
- const De = p.component || Le, xe = c.component || Ae;
1039
- return /* @__PURE__ */ o("div", { className: `superdoc-template-builder ${Q || ""}`, style: ie, children: [
1040
- /* @__PURE__ */ o("div", { style: { display: "flex", gap: "20px" }, children: [
1041
- c.position === "left" && /* @__PURE__ */ r("div", { className: "superdoc-template-builder-sidebar", children: /* @__PURE__ */ r(
1042
- xe,
1043
- {
1044
- fields: g,
1045
- onSelect: (e) => $(e.id),
1046
- onDelete: ae,
1047
- onUpdate: (e) => Z(e.id, e),
1048
- selectedFieldId: R || void 0
1049
- }
1050
- ) }),
1051
- /* @__PURE__ */ o("div", { className: "superdoc-template-builder-document", style: { flex: 1 }, children: [
1052
- j?.renderDefaultContainer && /* @__PURE__ */ r(
1053
- "div",
1054
- {
1055
- id: "superdoc-toolbar",
1056
- className: "superdoc-template-builder-toolbar",
1057
- "data-testid": "template-builder-toolbar"
1058
- }
1059
- ),
1060
- /* @__PURE__ */ r(
1061
- "div",
1062
- {
1063
- ref: se,
1064
- className: "superdoc-template-builder-editor",
1065
- style: { height: X },
1066
- "data-testid": "template-builder-editor"
1067
- }
1068
- )
1069
- ] }),
1070
- c.position === "right" && /* @__PURE__ */ r("div", { className: "superdoc-template-builder-sidebar", children: /* @__PURE__ */ r(
1071
- xe,
1072
- {
1073
- fields: g,
1074
- onSelect: (e) => $(e.id),
1075
- onDelete: ae,
1076
- onUpdate: (e) => Z(e.id, e),
1077
- selectedFieldId: R || void 0
1078
- }
1079
- ) })
1080
- ] }),
1081
- /* @__PURE__ */ r(
1082
- De,
1083
- {
1084
- isVisible: _,
1085
- position: V,
1086
- availableFields: s.available || [],
1087
- filteredFields: we,
1088
- filterQuery: Y,
1089
- allowCreate: s.allowCreate || !1,
1090
- onSelect: Se,
1091
- onClose: Ee,
1092
- onCreateField: P,
1093
- existingFields: g,
1094
- onSelectExisting: Te
1095
- }
1096
- )
1097
- ] });
1098
- }
1099
- );
1100
- Oe.displayName = "SuperDocTemplateBuilder";
1101
- export {
1102
- Ae as FieldList,
1103
- Le as FieldMenu,
1104
- Oe as default
1
+ import { forwardRef, useCallback, useEffect, useImperativeHandle, useMemo, useRef, useState } from "react";
2
+ import { jsx, jsxs } from "react/jsx-runtime";
3
+ const areTemplateFieldsEqual = (e, t) => {
4
+ if (e === t) return !0;
5
+ if (e.length !== t.length) return !1;
6
+ for (let n = 0; n < e.length; n += 1) {
7
+ let r = e[n], i = t[n];
8
+ if (!i || r.id !== i.id || r.alias !== i.alias || r.tag !== i.tag || r.position !== i.position || r.mode !== i.mode || r.group !== i.group || r.fieldType !== i.fieldType) return !1;
9
+ }
10
+ return !0;
11
+ }, resolveToolbar = (e) => {
12
+ if (!e) return null;
13
+ if (e === !0) return {
14
+ selector: "#superdoc-toolbar",
15
+ config: {},
16
+ renderDefaultContainer: !0
17
+ };
18
+ if (typeof e == "string") return {
19
+ selector: e,
20
+ config: {},
21
+ renderDefaultContainer: !1
22
+ };
23
+ let { selector: t, ...n } = e;
24
+ return {
25
+ selector: t || "#superdoc-toolbar",
26
+ config: n,
27
+ renderDefaultContainer: t === void 0
28
+ };
29
+ }, getPresentationEditor = (e) => {
30
+ let t = e?.superdocStore?.documents;
31
+ return !Array.isArray(t) || t.length === 0 ? null : t[0].getPresentationEditor?.() ?? null;
1105
32
  };
33
+ var FIELD_TYPE_STYLES = { signer: {
34
+ background: "#fef3c7",
35
+ color: "#b45309"
36
+ } }, DEFAULT_FIELD_TYPE_STYLE = {
37
+ background: "#f3f4f6",
38
+ color: "#6b7280"
39
+ };
40
+ const getFieldTypeStyle = (e) => FIELD_TYPE_STYLES[e] ?? DEFAULT_FIELD_TYPE_STYLE, clampToViewport = (e) => {
41
+ let t = window.innerWidth - 250 - 10, n = window.innerHeight - 300 - 10, r = Math.min(e.left, t), i = Math.min(e.top, n);
42
+ return new DOMRect(Math.max(r, 10), Math.max(i, 10), e.width, e.height);
43
+ }, InfoTooltip = ({ text: e }) => {
44
+ let [t, n] = useState(!1);
45
+ return /* @__PURE__ */ jsxs("span", {
46
+ style: {
47
+ position: "relative",
48
+ display: "inline-flex"
49
+ },
50
+ onMouseEnter: () => n(!0),
51
+ onMouseLeave: () => n(!1),
52
+ onClick: (e) => e.stopPropagation(),
53
+ children: [/* @__PURE__ */ jsxs("svg", {
54
+ width: "14",
55
+ height: "14",
56
+ viewBox: "0 0 16 16",
57
+ fill: "none",
58
+ xmlns: "http://www.w3.org/2000/svg",
59
+ style: {
60
+ cursor: "help",
61
+ flexShrink: 0
62
+ },
63
+ children: [/* @__PURE__ */ jsx("circle", {
64
+ cx: "8",
65
+ cy: "8",
66
+ r: "7",
67
+ stroke: "#9ca3af",
68
+ strokeWidth: "1.5"
69
+ }), /* @__PURE__ */ jsx("text", {
70
+ x: "8",
71
+ y: "11.5",
72
+ textAnchor: "middle",
73
+ fontSize: "10",
74
+ fontWeight: "600",
75
+ fontFamily: "system-ui, sans-serif",
76
+ fill: "#6b7280",
77
+ children: "?"
78
+ })]
79
+ }), t && /* @__PURE__ */ jsx("span", {
80
+ style: {
81
+ position: "absolute",
82
+ bottom: "100%",
83
+ left: "50%",
84
+ transform: "translateX(-50%)",
85
+ marginBottom: "6px",
86
+ padding: "6px 10px",
87
+ background: "#1f2937",
88
+ color: "#fff",
89
+ fontSize: "11px",
90
+ lineHeight: "1.4",
91
+ borderRadius: "4px",
92
+ whiteSpace: "normal",
93
+ width: "200px",
94
+ textAlign: "center",
95
+ zIndex: 1001,
96
+ pointerEvents: "none",
97
+ fontWeight: 400
98
+ },
99
+ children: e
100
+ })]
101
+ });
102
+ }, FieldMenu = ({ isVisible: e, position: t, availableFields: r, filteredFields: a, filterQuery: s, allowCreate: c, onSelect: l, onClose: u, onCreateField: d, existingFields: p = [], onSelectExisting: ne }) => {
103
+ let [h, g] = useState(!1), [_, v] = useState(""), [y, b] = useState("inline"), [x, S] = useState("owner"), [C, w] = useState(!0), [T, E] = useState(!0);
104
+ useEffect(() => {
105
+ e || (g(!1), v(""), b("inline"), S("owner"));
106
+ }, [e]);
107
+ let D = useMemo(() => ({
108
+ position: "fixed",
109
+ left: t?.left,
110
+ top: t?.top,
111
+ zIndex: 1e3,
112
+ background: "white",
113
+ border: "1px solid #ddd",
114
+ borderRadius: "4px",
115
+ boxShadow: "0 2px 8px rgba(0,0,0,0.1)",
116
+ padding: "8px 0",
117
+ width: "280px",
118
+ maxHeight: `calc(100vh - ${(t?.top ?? 0) + 10}px)`,
119
+ overflowY: "auto"
120
+ }), [t]), O = a ?? r, k = !!s;
121
+ if (useEffect(() => {
122
+ k && E(!0);
123
+ }, [k]), !e) return null;
124
+ let A = async () => {
125
+ let e = _.trim();
126
+ if (!e) return;
127
+ let t = {
128
+ id: `custom_${Date.now()}`,
129
+ label: e,
130
+ mode: y,
131
+ fieldType: x
132
+ };
133
+ try {
134
+ l(d && await d(t) || t);
135
+ } finally {
136
+ g(!1), v(""), b("inline"), S("owner");
137
+ }
138
+ };
139
+ return /* @__PURE__ */ jsxs("div", {
140
+ className: "superdoc-field-menu",
141
+ style: D,
142
+ children: [
143
+ k && /* @__PURE__ */ jsx("div", {
144
+ style: {
145
+ padding: "8px 16px",
146
+ borderBottom: "1px solid #f0f0f0",
147
+ marginBottom: "4px"
148
+ },
149
+ children: /* @__PURE__ */ jsxs("div", {
150
+ style: {
151
+ fontSize: "12px",
152
+ color: "#6b7280"
153
+ },
154
+ children: ["Filtering results for", /* @__PURE__ */ jsx("span", {
155
+ style: {
156
+ fontWeight: 600,
157
+ color: "#111827",
158
+ marginLeft: "4px"
159
+ },
160
+ children: s
161
+ })]
162
+ })
163
+ }),
164
+ c && !h && /* @__PURE__ */ jsx("div", {
165
+ className: "field-menu-item",
166
+ onClick: () => g(!0),
167
+ style: {
168
+ padding: "8px 16px",
169
+ cursor: "pointer",
170
+ color: "#0066cc",
171
+ fontWeight: 500
172
+ },
173
+ children: "+ Create New Field"
174
+ }),
175
+ c && h && /* @__PURE__ */ jsxs("div", {
176
+ style: { padding: "8px 16px" },
177
+ children: [
178
+ /* @__PURE__ */ jsx("input", {
179
+ type: "text",
180
+ value: _,
181
+ placeholder: "Field name...",
182
+ onChange: (e) => v(e.target.value),
183
+ onKeyDown: (e) => {
184
+ e.key === "Enter" && A(), e.key === "Escape" && (g(!1), v(""), b("inline"));
185
+ },
186
+ autoFocus: !0,
187
+ style: {
188
+ width: "100%",
189
+ padding: "4px 8px",
190
+ border: "1px solid #ddd",
191
+ borderRadius: "3px"
192
+ }
193
+ }),
194
+ /* @__PURE__ */ jsxs("div", {
195
+ style: {
196
+ marginTop: "8px",
197
+ display: "flex",
198
+ gap: "12px",
199
+ fontSize: "13px"
200
+ },
201
+ children: [/* @__PURE__ */ jsxs("label", {
202
+ style: {
203
+ display: "flex",
204
+ alignItems: "center",
205
+ gap: "4px",
206
+ cursor: "pointer"
207
+ },
208
+ children: [/* @__PURE__ */ jsx("input", {
209
+ type: "radio",
210
+ name: "fieldMode",
211
+ value: "inline",
212
+ checked: y === "inline",
213
+ onChange: () => b("inline")
214
+ }), "Inline"]
215
+ }), /* @__PURE__ */ jsxs("label", {
216
+ style: {
217
+ display: "flex",
218
+ alignItems: "center",
219
+ gap: "4px",
220
+ cursor: "pointer"
221
+ },
222
+ children: [/* @__PURE__ */ jsx("input", {
223
+ type: "radio",
224
+ name: "fieldMode",
225
+ value: "block",
226
+ checked: y === "block",
227
+ onChange: () => b("block")
228
+ }), "Block"]
229
+ })]
230
+ }),
231
+ /* @__PURE__ */ jsxs("div", {
232
+ style: {
233
+ marginTop: "8px",
234
+ display: "flex",
235
+ gap: "12px",
236
+ fontSize: "13px"
237
+ },
238
+ children: [/* @__PURE__ */ jsxs("label", {
239
+ style: {
240
+ display: "flex",
241
+ alignItems: "center",
242
+ gap: "4px",
243
+ cursor: "pointer"
244
+ },
245
+ children: [/* @__PURE__ */ jsx("input", {
246
+ type: "radio",
247
+ name: "fieldType",
248
+ value: "owner",
249
+ checked: x === "owner",
250
+ onChange: () => S("owner")
251
+ }), "Owner"]
252
+ }), /* @__PURE__ */ jsxs("label", {
253
+ style: {
254
+ display: "flex",
255
+ alignItems: "center",
256
+ gap: "4px",
257
+ cursor: "pointer"
258
+ },
259
+ children: [/* @__PURE__ */ jsx("input", {
260
+ type: "radio",
261
+ name: "fieldType",
262
+ value: "signer",
263
+ checked: x === "signer",
264
+ onChange: () => S("signer")
265
+ }), "Signer"]
266
+ })]
267
+ }),
268
+ /* @__PURE__ */ jsxs("div", {
269
+ style: {
270
+ marginTop: "8px",
271
+ display: "flex",
272
+ gap: "8px"
273
+ },
274
+ children: [/* @__PURE__ */ jsx("button", {
275
+ onClick: A,
276
+ disabled: !_.trim(),
277
+ style: {
278
+ padding: "4px 12px",
279
+ background: _.trim() ? "#0066cc" : "#ccc",
280
+ color: "white",
281
+ border: "none",
282
+ borderRadius: "3px",
283
+ cursor: _.trim() ? "pointer" : "not-allowed"
284
+ },
285
+ children: "Create"
286
+ }), /* @__PURE__ */ jsx("button", {
287
+ onClick: () => {
288
+ g(!1), v(""), b("inline"), S("owner");
289
+ },
290
+ style: {
291
+ padding: "4px 12px",
292
+ background: "white",
293
+ border: "1px solid #ddd",
294
+ borderRadius: "3px",
295
+ cursor: "pointer"
296
+ },
297
+ children: "Cancel"
298
+ })]
299
+ })
300
+ ]
301
+ }),
302
+ c && r.length > 0 && /* @__PURE__ */ jsx("div", { style: {
303
+ borderTop: "1px solid #eee",
304
+ margin: "4px 0"
305
+ } }),
306
+ p.length > 0 && (() => {
307
+ let e = /* @__PURE__ */ new Map();
308
+ p.forEach((t) => {
309
+ let n = t.group || `individual-${t.id}`, r = e.get(n) || [];
310
+ r.push(t), e.set(n, r);
311
+ });
312
+ let t = Array.from(e.values()).map((e) => ({
313
+ ...e[0],
314
+ count: e.length
315
+ }));
316
+ return /* @__PURE__ */ jsxs("div", {
317
+ style: { borderBottom: "1px solid #f0f0f0" },
318
+ children: [/* @__PURE__ */ jsxs("button", {
319
+ type: "button",
320
+ onClick: () => w(!C),
321
+ style: {
322
+ width: "100%",
323
+ display: "flex",
324
+ alignItems: "center",
325
+ justifyContent: "space-between",
326
+ padding: "8px 16px",
327
+ background: "transparent",
328
+ border: "none",
329
+ cursor: "pointer",
330
+ fontWeight: 500,
331
+ fontSize: "13px",
332
+ color: "#374151",
333
+ textAlign: "left"
334
+ },
335
+ children: [/* @__PURE__ */ jsxs("span", {
336
+ style: {
337
+ display: "flex",
338
+ alignItems: "center",
339
+ gap: "4px"
340
+ },
341
+ children: [
342
+ "Existing Fields (",
343
+ t.length,
344
+ ")",
345
+ /* @__PURE__ */ jsx(InfoTooltip, { text: "Insert a linked copy of a field already in the document. Linked fields share the same group and stay in sync." })
346
+ ]
347
+ }), /* @__PURE__ */ jsx("span", {
348
+ "aria-hidden": !0,
349
+ style: {
350
+ display: "inline-block",
351
+ width: "8px",
352
+ height: "8px",
353
+ borderRight: "2px solid #666",
354
+ borderBottom: "2px solid #666",
355
+ transform: C ? "rotate(45deg)" : "rotate(-45deg)",
356
+ transition: "transform 0.2s ease"
357
+ }
358
+ })]
359
+ }), C && /* @__PURE__ */ jsx("div", { children: t.map((e) => /* @__PURE__ */ jsxs("div", {
360
+ className: "field-menu-item",
361
+ onClick: () => ne?.(e),
362
+ style: {
363
+ padding: "8px 16px",
364
+ cursor: "pointer",
365
+ display: "flex",
366
+ alignItems: "flex-start",
367
+ justifyContent: "space-between",
368
+ gap: "8px"
369
+ },
370
+ children: [/* @__PURE__ */ jsxs("div", {
371
+ style: {
372
+ flex: 1,
373
+ minWidth: 0
374
+ },
375
+ children: [/* @__PURE__ */ jsx("div", {
376
+ style: {
377
+ fontWeight: 500,
378
+ fontSize: "13px"
379
+ },
380
+ children: e.alias || e.id
381
+ }), /* @__PURE__ */ jsx("div", {
382
+ style: {
383
+ fontSize: "11px",
384
+ color: "#9ca3af",
385
+ marginTop: "2px"
386
+ },
387
+ children: e.group ? `group (${e.count} fields)` : `ID: ${e.id}`
388
+ })]
389
+ }), /* @__PURE__ */ jsxs("div", {
390
+ style: {
391
+ display: "flex",
392
+ gap: "4px",
393
+ flexShrink: 0
394
+ },
395
+ children: [e.fieldType && /* @__PURE__ */ jsx("span", {
396
+ style: {
397
+ fontSize: "11px",
398
+ padding: "2px 6px",
399
+ borderRadius: "3px",
400
+ textTransform: "capitalize",
401
+ ...getFieldTypeStyle(e.fieldType),
402
+ fontWeight: 500
403
+ },
404
+ children: e.fieldType
405
+ }), /* @__PURE__ */ jsx("span", {
406
+ style: {
407
+ fontSize: "11px",
408
+ color: "#6b7280",
409
+ padding: "2px 6px",
410
+ background: "#f3f4f6",
411
+ borderRadius: "3px",
412
+ textTransform: "capitalize"
413
+ },
414
+ children: e.mode || "inline"
415
+ })]
416
+ })]
417
+ }, e.group || e.id)) })]
418
+ });
419
+ })(),
420
+ O.length === 0 ? /* @__PURE__ */ jsx("div", {
421
+ style: {
422
+ padding: "16px",
423
+ fontSize: "13px",
424
+ color: "#6b7280",
425
+ textAlign: "center"
426
+ },
427
+ children: "No matching fields"
428
+ }) : /* @__PURE__ */ jsxs("div", {
429
+ style: { borderBottom: "1px solid #f0f0f0" },
430
+ children: [/* @__PURE__ */ jsxs("button", {
431
+ type: "button",
432
+ onClick: () => E(!T),
433
+ style: {
434
+ width: "100%",
435
+ display: "flex",
436
+ alignItems: "center",
437
+ justifyContent: "space-between",
438
+ padding: "8px 16px",
439
+ background: "transparent",
440
+ border: "none",
441
+ cursor: "pointer",
442
+ fontWeight: 500,
443
+ fontSize: "13px",
444
+ color: "#374151",
445
+ textAlign: "left"
446
+ },
447
+ children: [/* @__PURE__ */ jsxs("span", {
448
+ style: {
449
+ display: "flex",
450
+ alignItems: "center",
451
+ gap: "4px"
452
+ },
453
+ children: [
454
+ "Available Fields (",
455
+ O.length,
456
+ ")",
457
+ /* @__PURE__ */ jsx(InfoTooltip, { text: "Insert a new, independent field instance into the document." })
458
+ ]
459
+ }), /* @__PURE__ */ jsx("span", {
460
+ "aria-hidden": !0,
461
+ style: {
462
+ display: "inline-block",
463
+ width: "8px",
464
+ height: "8px",
465
+ borderRight: "2px solid #666",
466
+ borderBottom: "2px solid #666",
467
+ transform: T ? "rotate(45deg)" : "rotate(-45deg)",
468
+ transition: "transform 0.2s ease"
469
+ }
470
+ })]
471
+ }), T && /* @__PURE__ */ jsx("div", { children: O.map((e) => /* @__PURE__ */ jsxs("div", {
472
+ className: "field-menu-item",
473
+ onClick: () => l(e),
474
+ style: {
475
+ padding: "8px 16px",
476
+ cursor: "pointer",
477
+ display: "flex",
478
+ alignItems: "flex-start",
479
+ justifyContent: "space-between",
480
+ gap: "8px"
481
+ },
482
+ children: [/* @__PURE__ */ jsxs("div", {
483
+ style: {
484
+ flex: 1,
485
+ minWidth: 0
486
+ },
487
+ children: [/* @__PURE__ */ jsx("div", {
488
+ style: {
489
+ fontWeight: 500,
490
+ fontSize: "13px"
491
+ },
492
+ children: e.label || e.id
493
+ }), /* @__PURE__ */ jsxs("div", {
494
+ style: {
495
+ fontSize: "11px",
496
+ color: "#9ca3af",
497
+ marginTop: "2px"
498
+ },
499
+ children: ["ID: ", e.id]
500
+ })]
501
+ }), /* @__PURE__ */ jsxs("div", {
502
+ style: {
503
+ display: "flex",
504
+ gap: "4px",
505
+ flexShrink: 0
506
+ },
507
+ children: [e.fieldType && /* @__PURE__ */ jsx("span", {
508
+ style: {
509
+ fontSize: "11px",
510
+ padding: "2px 6px",
511
+ borderRadius: "3px",
512
+ textTransform: "capitalize",
513
+ ...getFieldTypeStyle(e.fieldType),
514
+ fontWeight: 500
515
+ },
516
+ children: e.fieldType
517
+ }), /* @__PURE__ */ jsx("span", {
518
+ style: {
519
+ fontSize: "11px",
520
+ color: "#6b7280",
521
+ padding: "2px 6px",
522
+ background: "#f3f4f6",
523
+ borderRadius: "3px",
524
+ textTransform: "capitalize"
525
+ },
526
+ children: e.mode || "inline"
527
+ })]
528
+ })]
529
+ }, e.id)) })]
530
+ }),
531
+ /* @__PURE__ */ jsx("div", {
532
+ style: {
533
+ borderTop: "1px solid #eee",
534
+ marginTop: "4px"
535
+ },
536
+ children: /* @__PURE__ */ jsx("button", {
537
+ onClick: u,
538
+ style: {
539
+ width: "100%",
540
+ padding: "6px 16px",
541
+ background: "#f3f4f6",
542
+ border: "none",
543
+ borderRadius: "0 0 4px 4px",
544
+ cursor: "pointer"
545
+ },
546
+ children: "Close"
547
+ })
548
+ })
549
+ ]
550
+ });
551
+ };
552
+ var shortenGroupId = (e) => {
553
+ let t = e.split("-");
554
+ return t.length > 2 ? t[t.length - 1].substring(0, 6) : e.substring(0, 6);
555
+ }, FieldItem = ({ field: e, onSelect: t, onDelete: n, isSelected: r, isGrouped: i = !1 }) => /* @__PURE__ */ jsxs("div", {
556
+ onClick: () => t(e),
557
+ style: {
558
+ position: "relative",
559
+ padding: "10px 12px",
560
+ background: r ? "#eff6ff" : "#f9fafb",
561
+ border: r ? "1px solid #3b82f6" : "1px solid #e5e7eb",
562
+ borderRadius: "6px",
563
+ cursor: "pointer",
564
+ transition: "all 0.2s",
565
+ fontSize: i ? "13px" : "14px"
566
+ },
567
+ onMouseEnter: (e) => {
568
+ r || (e.currentTarget.style.background = "#f3f4f6");
569
+ },
570
+ onMouseLeave: (e) => {
571
+ r || (e.currentTarget.style.background = "#f9fafb");
572
+ },
573
+ title: e.alias,
574
+ children: [/* @__PURE__ */ jsx("button", {
575
+ onClick: (t) => {
576
+ t.stopPropagation(), n(e.id);
577
+ },
578
+ style: {
579
+ position: "absolute",
580
+ top: "6px",
581
+ right: "6px",
582
+ padding: "4px",
583
+ background: "transparent",
584
+ border: "none",
585
+ cursor: "pointer",
586
+ color: "#9ca3af",
587
+ transition: "color 0.2s",
588
+ display: "flex",
589
+ alignItems: "center",
590
+ justifyContent: "center"
591
+ },
592
+ onMouseEnter: (e) => {
593
+ e.currentTarget.style.color = "#ef4444";
594
+ },
595
+ onMouseLeave: (e) => {
596
+ e.currentTarget.style.color = "#9ca3af";
597
+ },
598
+ title: "Delete field",
599
+ children: /* @__PURE__ */ jsx("svg", {
600
+ width: "14",
601
+ height: "14",
602
+ viewBox: "0 0 16 16",
603
+ fill: "none",
604
+ xmlns: "http://www.w3.org/2000/svg",
605
+ children: /* @__PURE__ */ jsx("path", {
606
+ d: "M6 2V1.5C6 1.22386 6.22386 1 6.5 1H9.5C9.77614 1 10 1.22386 10 1.5V2M2 4H14M12.6667 4L12.1991 11.0129C12.129 12.065 12.0939 12.5911 11.8667 12.99C11.6666 13.3412 11.3648 13.6235 11.0011 13.7998C10.588 14 10.0607 14 9.00623 14H6.99377C5.93927 14 5.41202 14 4.99889 13.7998C4.63517 13.6235 4.33339 13.3412 4.13332 12.99C3.90607 12.5911 3.871 12.065 3.80086 11.0129L3.33333 4",
607
+ stroke: "currentColor",
608
+ strokeWidth: "1.5",
609
+ strokeLinecap: "round",
610
+ strokeLinejoin: "round"
611
+ })
612
+ })
613
+ }), /* @__PURE__ */ jsxs("div", {
614
+ style: { paddingRight: "24px" },
615
+ children: [/* @__PURE__ */ jsx("div", {
616
+ style: {
617
+ fontWeight: "500",
618
+ fontSize: i ? "12px" : "14px",
619
+ color: i ? "#6b7280" : "#111827"
620
+ },
621
+ children: e.alias || e.id
622
+ }), /* @__PURE__ */ jsxs("div", {
623
+ style: {
624
+ display: "flex",
625
+ alignItems: "center",
626
+ gap: "6px",
627
+ fontSize: "11px",
628
+ color: "#9ca3af",
629
+ marginTop: "2px"
630
+ },
631
+ children: [
632
+ /* @__PURE__ */ jsxs("span", { children: ["ID: ", e.id] }),
633
+ e.mode && /* @__PURE__ */ jsx("span", {
634
+ style: {
635
+ fontSize: "9px",
636
+ padding: "2px 5px",
637
+ borderRadius: "3px",
638
+ background: e.mode === "block" ? "#dbeafe" : "#f3f4f6",
639
+ color: e.mode === "block" ? "#1e40af" : "#4b5563",
640
+ fontWeight: "500"
641
+ },
642
+ children: e.mode
643
+ }),
644
+ e.fieldType && /* @__PURE__ */ jsx("span", {
645
+ style: {
646
+ fontSize: "9px",
647
+ padding: "2px 5px",
648
+ borderRadius: "3px",
649
+ ...getFieldTypeStyle(e.fieldType),
650
+ fontWeight: "500"
651
+ },
652
+ children: e.fieldType
653
+ })
654
+ ]
655
+ })]
656
+ })]
657
+ });
658
+ const FieldList = ({ fields: e, onSelect: t, onDelete: n, selectedFieldId: r }) => {
659
+ let [a, s] = useState(/* @__PURE__ */ new Set()), { groupedFields: c, ungroupedFields: l } = useMemo(() => {
660
+ let t = {}, n = [];
661
+ return e.forEach((e) => {
662
+ e.group ? (t[e.group] || (t[e.group] = []), t[e.group].push(e)) : n.push(e);
663
+ }), {
664
+ groupedFields: t,
665
+ ungroupedFields: n
666
+ };
667
+ }, [e]), u = (e) => {
668
+ s((t) => {
669
+ let n = new Set(t);
670
+ return n.has(e) ? n.delete(e) : n.add(e), n;
671
+ });
672
+ };
673
+ return /* @__PURE__ */ jsxs("div", {
674
+ className: "superdoc-field-list",
675
+ style: {
676
+ width: "250px",
677
+ background: "white",
678
+ border: "1px solid #e5e7eb",
679
+ borderRadius: "8px",
680
+ padding: "16px"
681
+ },
682
+ children: [/* @__PURE__ */ jsxs("h3", {
683
+ style: {
684
+ margin: "0 0 16px 0",
685
+ fontSize: "16px",
686
+ fontWeight: "600"
687
+ },
688
+ children: [
689
+ "Template Fields (",
690
+ e.length,
691
+ ")"
692
+ ]
693
+ }), e.length === 0 ? /* @__PURE__ */ jsxs("div", {
694
+ style: {
695
+ color: "#9ca3af",
696
+ fontSize: "14px",
697
+ textAlign: "center",
698
+ padding: "20px 0"
699
+ },
700
+ children: [
701
+ "No fields yet. Type ",
702
+ "{{",
703
+ " to add a field."
704
+ ]
705
+ }) : /* @__PURE__ */ jsxs("div", {
706
+ style: {
707
+ display: "flex",
708
+ flexDirection: "column",
709
+ gap: "8px"
710
+ },
711
+ children: [l.map((e) => /* @__PURE__ */ jsx(FieldItem, {
712
+ field: e,
713
+ onSelect: t,
714
+ onDelete: n,
715
+ isSelected: r === e.id
716
+ }, e.id)), Object.entries(c).map(([e, i]) => {
717
+ let o = a.has(e), s = i[0];
718
+ return /* @__PURE__ */ jsxs("div", { children: [/* @__PURE__ */ jsx("div", {
719
+ style: {
720
+ position: "relative",
721
+ padding: "12px",
722
+ background: "#f9fafb",
723
+ border: "1px solid #e5e7eb",
724
+ borderRadius: "6px",
725
+ cursor: "pointer",
726
+ transition: "all 0.2s"
727
+ },
728
+ onClick: () => u(e),
729
+ onMouseEnter: (e) => {
730
+ e.currentTarget.style.background = "#f3f4f6";
731
+ },
732
+ onMouseLeave: (e) => {
733
+ e.currentTarget.style.background = "#f9fafb";
734
+ },
735
+ children: /* @__PURE__ */ jsxs("div", {
736
+ style: {
737
+ display: "flex",
738
+ alignItems: "center",
739
+ gap: "8px"
740
+ },
741
+ children: [/* @__PURE__ */ jsx("span", {
742
+ style: {
743
+ fontSize: "12px",
744
+ color: "#6b7280"
745
+ },
746
+ children: o ? "▼" : "▶"
747
+ }), /* @__PURE__ */ jsxs("div", {
748
+ style: { flex: 1 },
749
+ children: [/* @__PURE__ */ jsx("div", {
750
+ style: {
751
+ fontWeight: "500",
752
+ fontSize: "14px"
753
+ },
754
+ children: s.alias
755
+ }), /* @__PURE__ */ jsxs("div", {
756
+ style: {
757
+ fontSize: "11px",
758
+ color: "#9ca3af",
759
+ marginTop: "2px"
760
+ },
761
+ children: [
762
+ "group: ",
763
+ shortenGroupId(e),
764
+ " (",
765
+ i.length,
766
+ " fields)"
767
+ ]
768
+ })]
769
+ })]
770
+ })
771
+ }), o && /* @__PURE__ */ jsx("div", {
772
+ style: {
773
+ marginLeft: "16px",
774
+ marginTop: "4px",
775
+ display: "flex",
776
+ flexDirection: "column",
777
+ gap: "4px"
778
+ },
779
+ children: i.map((e) => /* @__PURE__ */ jsx(FieldItem, {
780
+ field: e,
781
+ onSelect: t,
782
+ onDelete: n,
783
+ isSelected: r === e.id,
784
+ isGrouped: !0
785
+ }, e.id))
786
+ })] }, e);
787
+ })]
788
+ })]
789
+ });
790
+ };
791
+ var getTemplateFieldsFromEditor = (e) => {
792
+ let t = e.helpers?.structuredContentCommands;
793
+ return t?.getStructuredContentTags ? (t.getStructuredContentTags(e.state) || []).map((e) => {
794
+ let n = e?.node ?? e, r = n?.attrs ?? {}, i = (n?.type?.name || "").includes("Block") ? "block" : "inline", a = (() => {
795
+ try {
796
+ return typeof r.tag == "string" && r.tag.startsWith("{") ? JSON.parse(r.tag) : null;
797
+ } catch {
798
+ return null;
799
+ }
800
+ })();
801
+ return {
802
+ id: r.id,
803
+ alias: r.alias || r.label || "",
804
+ tag: r.tag,
805
+ mode: i,
806
+ group: t.getGroup?.(r.tag) ?? void 0,
807
+ fieldType: a?.fieldType ?? "owner"
808
+ };
809
+ }) : [];
810
+ }, SuperDocTemplateBuilder = forwardRef((e, u) => {
811
+ let { document: d, fields: f = {}, menu: m = {}, list: h = {}, toolbar: g, cspNonce: y, telemetry: b, licenseKey: x, onReady: S, onTrigger: C, onFieldInsert: w, onFieldUpdate: T, onFieldDelete: E, onFieldsChange: D, onFieldSelect: O, onFieldCreate: k, onExport: A, className: re, style: ie, documentHeight: ae = "600px" } = e, [j, M] = useState(f.initial || []), [N, P] = useState(null), [F, I] = useState(!1), [oe, L] = useState(), [se, ce] = useState(""), [le, ue] = useState(() => f.available || []), R = useRef(null), z = useRef(null), B = useRef(null), de = useRef(f);
812
+ de.current = f;
813
+ let V = useRef(null), H = useRef(F);
814
+ useEffect(() => {
815
+ H.current = F;
816
+ }, [F]);
817
+ let U = m.trigger || "{{", W = de.current.available || [], G = useMemo(() => resolveToolbar(g), [g]), K = useMemo(() => ({
818
+ enabled: b?.enabled ?? !0,
819
+ metadata: {
820
+ source: "template-builder",
821
+ ...b?.metadata
822
+ }
823
+ }), [b?.enabled, JSON.stringify(b?.metadata)]), fe = useCallback((e) => {
824
+ let t = e.trim().toLowerCase();
825
+ return t ? W.filter((e) => e.label.toLowerCase().includes(t)) : W;
826
+ }, [W]), q = useCallback((e) => {
827
+ ce(e), ue(fe(e));
828
+ }, [fe]), J = useCallback(() => {
829
+ q("");
830
+ }, [q]), Y = useCallback((e, t) => {
831
+ if (!z.current?.activeEditor) return !1;
832
+ let n = z.current.activeEditor, r = j, i = t.metadata || t.fieldType ? JSON.stringify({
833
+ ...t.metadata,
834
+ ...t.fieldType ? { fieldType: t.fieldType } : {}
835
+ }) : void 0, a = e === "inline" ? n.commands.insertStructuredContentInline?.({
836
+ attrs: {
837
+ alias: t.alias,
838
+ tag: i
839
+ },
840
+ text: t.defaultValue || t.alias
841
+ }) : n.commands.insertStructuredContentBlock?.({
842
+ attrs: {
843
+ alias: t.alias,
844
+ tag: i
845
+ },
846
+ text: t.defaultValue || t.alias
847
+ });
848
+ if (a) {
849
+ let e = getTemplateFieldsFromEditor(n);
850
+ M(e), D?.(e);
851
+ let t = e.find((e) => !r.some((t) => t.id === e.id));
852
+ t && w?.(t);
853
+ }
854
+ return a ?? !1;
855
+ }, [
856
+ w,
857
+ D,
858
+ j
859
+ ]), X = useCallback((e, t) => {
860
+ if (!z.current?.activeEditor) return !1;
861
+ let n = z.current.activeEditor.commands.updateStructuredContentById?.(e, { attrs: t });
862
+ return n && M((n) => {
863
+ let r = n.map((n) => n.id === e ? {
864
+ ...n,
865
+ ...t
866
+ } : n);
867
+ D?.(r);
868
+ let i = r.find((t) => t.id === e);
869
+ return i && T?.(i), r;
870
+ }), n ?? !1;
871
+ }, [T, D]), Z = useCallback((e) => {
872
+ let t = z.current?.activeEditor;
873
+ if (!t) {
874
+ let t = !1;
875
+ return M((n) => {
876
+ if (!n.some((t) => t.id === e)) return n;
877
+ let r = n.filter((t) => t.id !== e);
878
+ return t = !0, D?.(r), E?.(e), r;
879
+ }), t && P((t) => t === e ? null : t), t;
880
+ }
881
+ let n = j.find((t) => t.id === e)?.group, r = !1;
882
+ try {
883
+ r = t.commands.deleteStructuredContentById?.(e) ?? !1;
884
+ } catch (t) {
885
+ console.warn("[TemplateBuilder] Failed to delete structured content:", e, t), r = !1;
886
+ }
887
+ let i = getTemplateFieldsFromEditor(t), a = i.some((t) => t.id === e);
888
+ if (!r && a && (i = i.filter((t) => t.id !== e)), n) {
889
+ let e = i.filter((e) => e.group === n);
890
+ if (e.length === 1) {
891
+ let n = e[0];
892
+ t.commands.updateStructuredContentById?.(n.id, { attrs: { tag: void 0 } }), i = getTemplateFieldsFromEditor(t);
893
+ }
894
+ }
895
+ let o = !1;
896
+ return M((t) => {
897
+ if (areTemplateFieldsEqual(t, i)) return t;
898
+ let n = t.some((t) => t.id === e), r = i.some((t) => t.id === e);
899
+ return n && !r && (o = !0), D?.(i), o && E?.(e), i;
900
+ }), o && P((t) => t === e ? null : t), r || o;
901
+ }, [
902
+ E,
903
+ D,
904
+ j
905
+ ]), Q = useCallback((e) => {
906
+ if (!z.current?.activeEditor) return;
907
+ z.current.activeEditor.commands.selectStructuredContentById?.(e), P(e);
908
+ let t = j.find((t) => t.id === e);
909
+ t && O?.(t);
910
+ }, [j, O]), $ = useCallback((e) => {
911
+ if (!e) return;
912
+ let t = getTemplateFieldsFromEditor(e);
913
+ M((e) => areTemplateFieldsEqual(e, t) ? e : (D?.(t), t));
914
+ }, [D]);
915
+ useEffect(() => {
916
+ if (!R.current) return;
917
+ let e = !1, t = null;
918
+ return (async () => {
919
+ let { SuperDoc: n } = await import("superdoc");
920
+ if (e) return;
921
+ let r = {
922
+ comments: !1,
923
+ ...G && { toolbar: {
924
+ selector: G.selector,
925
+ toolbarGroups: G.config.toolbarGroups || ["center"],
926
+ excludeItems: G.config.excludeItems || [],
927
+ ...G.config
928
+ } }
929
+ }, i = () => {
930
+ if (!e) {
931
+ if (t?.activeEditor) {
932
+ let e = t.activeEditor, n = getPresentationEditor(t);
933
+ e.on("update", ({ editor: e }) => {
934
+ let { state: t } = e, { from: r } = t.selection;
935
+ if (r >= U.length) {
936
+ let i = r - U.length;
937
+ if (t.doc.textBetween(i, r) === U) {
938
+ let t = n?.coordsAtPos(r) ?? e.view.coordsAtPos(r), a = clampToViewport(new DOMRect(t.left, t.bottom ?? t.top, 0, 0)), o = () => {
939
+ let e = z.current?.activeEditor;
940
+ if (!e) return;
941
+ let t = e.state.selection.from, n = e.state.tr.delete(i, t);
942
+ e.view.dispatch(n);
943
+ };
944
+ B.current = o, V.current = r, L(a), I(!0), J(), C?.({
945
+ position: {
946
+ from: i,
947
+ to: r
948
+ },
949
+ bounds: a,
950
+ cleanup: o
951
+ });
952
+ return;
953
+ }
954
+ }
955
+ if (!H.current) return;
956
+ if (V.current == null) {
957
+ I(!1), J();
958
+ return;
959
+ }
960
+ if (r < V.current) {
961
+ I(!1), V.current = null, J();
962
+ return;
963
+ }
964
+ q(t.doc.textBetween(V.current, r));
965
+ let i = n?.coordsAtPos(r) ?? e.view.coordsAtPos(r);
966
+ L(clampToViewport(new DOMRect(i.left, i.bottom ?? i.top, 0, 0)));
967
+ }), e.on("update", () => {
968
+ $(e);
969
+ }), $(e);
970
+ }
971
+ S?.();
972
+ }
973
+ };
974
+ t = new n({
975
+ selector: R.current,
976
+ document: d?.source,
977
+ documentMode: d?.mode || "editing",
978
+ modules: r,
979
+ toolbar: G?.selector,
980
+ cspNonce: y,
981
+ telemetry: K,
982
+ ...x && { licenseKey: x },
983
+ onReady: i
984
+ }), z.current = t;
985
+ })(), () => {
986
+ e = !0, B.current = null, V.current = null, t && typeof t.destroy == "function" && t.destroy(), z.current = null;
987
+ };
988
+ }, [
989
+ d?.source,
990
+ d?.mode,
991
+ U,
992
+ $,
993
+ S,
994
+ C,
995
+ G,
996
+ y,
997
+ K,
998
+ x
999
+ ]);
1000
+ let pe = useCallback(async (e) => {
1001
+ B.current &&= (B.current(), null), V.current = null, J();
1002
+ let t = e.mode || "inline";
1003
+ if (e.id.startsWith("custom_") && k) {
1004
+ let n = await k(e);
1005
+ if (n) {
1006
+ Y(n.mode || t, {
1007
+ alias: n.label,
1008
+ metadata: n.metadata,
1009
+ defaultValue: n.defaultValue,
1010
+ fieldType: n.fieldType
1011
+ }), I(!1);
1012
+ return;
1013
+ }
1014
+ }
1015
+ Y(t, {
1016
+ alias: e.label,
1017
+ metadata: e.metadata,
1018
+ defaultValue: e.defaultValue,
1019
+ fieldType: e.fieldType
1020
+ }), I(!1);
1021
+ }, [
1022
+ Y,
1023
+ k,
1024
+ J
1025
+ ]), me = useCallback((e) => {
1026
+ B.current &&= (B.current(), null), V.current = null, J();
1027
+ let t = z.current?.activeEditor;
1028
+ if (!t) return;
1029
+ let n = t.helpers?.structuredContentCommands;
1030
+ if (!n) return;
1031
+ let r = e.group || `group-${Date.now()}-${Math.random().toString(36).substring(2, 11)}`, i = n.createTagObject?.({
1032
+ group: r,
1033
+ ...e.fieldType ? { fieldType: e.fieldType } : {}
1034
+ });
1035
+ if ((e.mode || "inline") === "inline" ? t.commands.insertStructuredContentInline?.({
1036
+ attrs: {
1037
+ alias: e.alias,
1038
+ tag: i
1039
+ },
1040
+ text: e.alias
1041
+ }) : t.commands.insertStructuredContentBlock?.({
1042
+ attrs: {
1043
+ alias: e.alias,
1044
+ tag: i
1045
+ },
1046
+ text: e.alias
1047
+ })) {
1048
+ e.group || X(e.id, { tag: i }), I(!1);
1049
+ let n = getTemplateFieldsFromEditor(t);
1050
+ M(n), D?.(n);
1051
+ }
1052
+ }, [
1053
+ X,
1054
+ J,
1055
+ D
1056
+ ]), he = useCallback(() => {
1057
+ I(!1), V.current = null, J(), B.current &&= (B.current(), null);
1058
+ }, [J]), ge = useCallback(() => {
1059
+ if (!z.current?.activeEditor || j.length === 0) return;
1060
+ let e = j.findIndex((e) => e.id === N);
1061
+ Q(j[e >= 0 ? (e + 1) % j.length : 0].id);
1062
+ }, [
1063
+ j,
1064
+ N,
1065
+ Q
1066
+ ]), _e = useCallback(() => {
1067
+ if (!z.current?.activeEditor || j.length === 0) return;
1068
+ let e = j.findIndex((e) => e.id === N);
1069
+ Q(j[e > 0 ? e - 1 : j.length - 1].id);
1070
+ }, [
1071
+ j,
1072
+ N,
1073
+ Q
1074
+ ]), ve = useCallback(async (e) => {
1075
+ let { fileName: t = "document", triggerDownload: n = !0 } = e || {}, r = await z.current?.export({
1076
+ exportType: ["docx"],
1077
+ exportedName: t,
1078
+ triggerDownload: n
1079
+ }), i = z.current?.activeEditor;
1080
+ if (i) {
1081
+ let e = getTemplateFieldsFromEditor(i);
1082
+ A?.({
1083
+ fields: e,
1084
+ blob: n ? void 0 : r,
1085
+ fileName: t
1086
+ });
1087
+ }
1088
+ return r;
1089
+ }, [A]);
1090
+ useImperativeHandle(u, () => ({
1091
+ insertField: (e) => Y("inline", e),
1092
+ insertBlockField: (e) => Y("block", e),
1093
+ updateField: X,
1094
+ deleteField: Z,
1095
+ selectField: Q,
1096
+ nextField: ge,
1097
+ previousField: _e,
1098
+ getFields: () => j,
1099
+ exportTemplate: ve,
1100
+ getSuperDoc: () => z.current
1101
+ }));
1102
+ let ye = m.component || FieldMenu, be = h.component || FieldList;
1103
+ return /* @__PURE__ */ jsxs("div", {
1104
+ className: `superdoc-template-builder ${re || ""}`,
1105
+ style: ie,
1106
+ children: [/* @__PURE__ */ jsxs("div", {
1107
+ style: {
1108
+ display: "flex",
1109
+ gap: "20px"
1110
+ },
1111
+ children: [
1112
+ h.position === "left" && /* @__PURE__ */ jsx("div", {
1113
+ className: "superdoc-template-builder-sidebar",
1114
+ children: /* @__PURE__ */ jsx(be, {
1115
+ fields: j,
1116
+ onSelect: (e) => Q(e.id),
1117
+ onDelete: Z,
1118
+ onUpdate: (e) => X(e.id, e),
1119
+ selectedFieldId: N || void 0
1120
+ })
1121
+ }),
1122
+ /* @__PURE__ */ jsxs("div", {
1123
+ className: "superdoc-template-builder-document",
1124
+ style: { flex: 1 },
1125
+ children: [G?.renderDefaultContainer && /* @__PURE__ */ jsx("div", {
1126
+ id: "superdoc-toolbar",
1127
+ className: "superdoc-template-builder-toolbar",
1128
+ "data-testid": "template-builder-toolbar"
1129
+ }), /* @__PURE__ */ jsx("div", {
1130
+ ref: R,
1131
+ className: "superdoc-template-builder-editor",
1132
+ style: { height: ae },
1133
+ "data-testid": "template-builder-editor"
1134
+ })]
1135
+ }),
1136
+ h.position === "right" && /* @__PURE__ */ jsx("div", {
1137
+ className: "superdoc-template-builder-sidebar",
1138
+ children: /* @__PURE__ */ jsx(be, {
1139
+ fields: j,
1140
+ onSelect: (e) => Q(e.id),
1141
+ onDelete: Z,
1142
+ onUpdate: (e) => X(e.id, e),
1143
+ selectedFieldId: N || void 0
1144
+ })
1145
+ })
1146
+ ]
1147
+ }), /* @__PURE__ */ jsx(ye, {
1148
+ isVisible: F,
1149
+ position: oe,
1150
+ availableFields: f.available || [],
1151
+ filteredFields: le,
1152
+ filterQuery: se,
1153
+ allowCreate: f.allowCreate || !1,
1154
+ onSelect: pe,
1155
+ onClose: he,
1156
+ onCreateField: k,
1157
+ existingFields: j,
1158
+ onSelectExisting: me
1159
+ })]
1160
+ });
1161
+ });
1162
+ SuperDocTemplateBuilder.displayName = "SuperDocTemplateBuilder";
1163
+ var src_default = SuperDocTemplateBuilder;
1164
+ export { FieldList, FieldMenu, src_default as default };