@forevka/wordcanvas 0.1.0 → 0.3.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.
@@ -0,0 +1,972 @@
1
+ const $ = [
2
+ "header",
3
+ "footer",
4
+ "headerFirst",
5
+ "headerEven",
6
+ "footerFirst",
7
+ "footerEven"
8
+ ], J = new Intl.Segmenter(void 0, { granularity: "grapheme" }), K = new Intl.Segmenter(void 0, { granularity: "word" }), me = (n) => n.runs.some((e) => e.text.length > 0) && n.runs.every((e) => e.text.length === 0 || e.style.hidden === !0), A = (n, e) => {
9
+ const t = [];
10
+ for (const s of n)
11
+ if (s.kind === "paragraph") t.push(s);
12
+ else if (s.kind === "table" && e)
13
+ for (const r of s.rows)
14
+ for (const i of r.cells) t.push(...A(i.blocks, e));
15
+ return t;
16
+ }, Z = (n, e) => A(n.section[e] ?? [], !0), ge = (n) => A(n.blocks, !0), H = (n) => [
17
+ ...A(n.blocks, !0),
18
+ ...$.flatMap((e) => Z(n, e)),
19
+ ...Object.values(n.footnotes ?? {}).flat()
20
+ ], ee = (n, e) => H(n).find((t) => t.id === e), Ie = (n, e) => H(n).findIndex((t) => t.id === e), V = (n, e) => e === "body" ? n.blocks : n.section[e] ?? [];
21
+ function U(n, e, t) {
22
+ for (let s = 0; s < n.length; s++) {
23
+ const r = n[s];
24
+ if (r.kind === "paragraph") {
25
+ if (r.id === t)
26
+ return e === "body" ? { kind: "top", bi: s } : { kind: "band", band: e, bi: s };
27
+ } else if (r.kind === "table")
28
+ for (let i = 0; i < r.rows.length; i++) {
29
+ const o = r.rows[i];
30
+ for (let a = 0; a < o.cells.length; a++) {
31
+ const l = o.cells[a];
32
+ for (let c = 0; c < l.blocks.length; c++) {
33
+ const d = l.blocks[c];
34
+ if (d.kind === "paragraph" && d.id === t)
35
+ return { kind: "cell", where: e, bi: s, ri: i, ci: a, pi: c };
36
+ }
37
+ }
38
+ }
39
+ }
40
+ return null;
41
+ }
42
+ function E(n, e) {
43
+ const t = U(n.blocks, "body", e);
44
+ if (t) return t;
45
+ for (const s of $) {
46
+ const r = U(n.section[s] ?? [], s, e);
47
+ if (r) return r;
48
+ }
49
+ for (const [s, r] of Object.entries(n.footnotes ?? {})) {
50
+ const i = r.findIndex((o) => o.id === e);
51
+ if (i >= 0) return { kind: "footnote", noteId: s, pi: i };
52
+ }
53
+ return null;
54
+ }
55
+ function C(n, e) {
56
+ return e.kind === "top" ? n.blocks[e.bi] : e.kind === "band" ? n.section[e.band][e.bi] : e.kind === "footnote" ? n.footnotes[e.noteId][e.pi] : V(n, e.where)[e.bi].rows[e.ri].cells[e.ci].blocks[e.pi];
57
+ }
58
+ function R(n, e, t) {
59
+ if (e.kind === "footnote") {
60
+ const l = n.footnotes[e.noteId].slice();
61
+ return l[e.pi] = t, { ...n, footnotes: { ...n.footnotes, [e.noteId]: l } };
62
+ }
63
+ if (e.kind === "band") {
64
+ const l = (n.section[e.band] ?? []).slice();
65
+ return l[e.bi] = t, { ...n, section: { ...n.section, [e.band]: l } };
66
+ }
67
+ if (e.kind === "top") {
68
+ const l = n.blocks.slice();
69
+ return l[e.bi] = t, { ...n, blocks: l };
70
+ }
71
+ const s = V(n, e.where).slice(), r = s[e.bi], i = r.rows.slice(), o = { cells: i[e.ri].cells.slice() }, a = { ...o.cells[e.ci], blocks: o.cells[e.ci].blocks.slice() };
72
+ return a.blocks[e.pi] = t, o.cells[e.ci] = a, i[e.ri] = o, s[e.bi] = { ...r, rows: i, revision: r.revision + 1 }, e.where === "body" ? { ...n, blocks: s } : { ...n, section: { ...n.section, [e.where]: s } };
73
+ }
74
+ const ye = (n, e) => E(n, e)?.kind === "cell", M = (n) => n.map((e) => e.text).join(""), we = (n, e) => {
75
+ const t = ee(n, e);
76
+ return t ? M(t.runs) : "";
77
+ };
78
+ function xe(n, e) {
79
+ let t = 0;
80
+ for (const s of J.segment(n)) {
81
+ if (s.index >= e) break;
82
+ t = s.index;
83
+ }
84
+ return t;
85
+ }
86
+ function pe(n, e) {
87
+ for (const t of J.segment(n)) {
88
+ const s = t.index + t.segment.length;
89
+ if (s > e) return s;
90
+ }
91
+ return n.length;
92
+ }
93
+ function Pe(n, e) {
94
+ let t = 0;
95
+ for (const s of K.segment(n)) {
96
+ if (s.index >= e) break;
97
+ s.isWordLike && (t = s.index);
98
+ }
99
+ return t;
100
+ }
101
+ function ve(n, e) {
102
+ for (const t of K.segment(n)) {
103
+ const s = t.index + t.segment.length;
104
+ if (t.isWordLike && s > e) return s;
105
+ }
106
+ return n.length;
107
+ }
108
+ function te(n, e) {
109
+ let t = 0;
110
+ for (const s of n) {
111
+ const r = t + s.text.length;
112
+ if (e > t && e <= r && s.text.length > 0) return s.style;
113
+ t = r;
114
+ }
115
+ return n[0]?.style;
116
+ }
117
+ function D(n, e) {
118
+ return n.styles.find((t) => t.id === e);
119
+ }
120
+ function _(n, e) {
121
+ const t = [], s = /* @__PURE__ */ new Set();
122
+ for (let o = D(n, e); o && !s.has(o.id); o = o.basedOn ? D(n, o.basedOn) : void 0)
123
+ s.add(o.id), t.unshift(o);
124
+ const r = {}, i = {};
125
+ for (const o of t)
126
+ Object.assign(r, G(o.char)), Object.assign(i, G(o.para));
127
+ return { char: r, para: i };
128
+ }
129
+ function G(n) {
130
+ const e = {};
131
+ for (const [t, s] of Object.entries(n))
132
+ s !== void 0 && (e[t] = s);
133
+ return e;
134
+ }
135
+ function Se(n, e) {
136
+ return H(n).filter((t) => t.style.namedStyle === e);
137
+ }
138
+ function Be() {
139
+ return {
140
+ defaultStyleId: "Normal",
141
+ styles: [
142
+ {
143
+ id: "Normal",
144
+ name: "Normal",
145
+ char: { fontFamily: "Georgia, serif", fontSizePx: 16, bold: !1, italic: !1, color: "#202124" },
146
+ para: { ...{ lineHeight: 1.5, spaceBeforePx: 0, spaceAfterPx: 12 }, align: "left", indentFirstLinePx: 0 }
147
+ },
148
+ {
149
+ id: "Title",
150
+ name: "Title",
151
+ basedOn: "Normal",
152
+ char: { fontFamily: "Arial, sans-serif", fontSizePx: 32, bold: !0, color: "#1a1a2e" },
153
+ para: { align: "center", spaceAfterPx: 4 }
154
+ },
155
+ {
156
+ id: "Subtitle",
157
+ name: "Subtitle",
158
+ basedOn: "Normal",
159
+ char: { italic: !0, color: "#5f6368" },
160
+ para: { align: "center", spaceAfterPx: 28 }
161
+ },
162
+ {
163
+ id: "Heading1",
164
+ name: "Heading 1",
165
+ basedOn: "Normal",
166
+ char: { fontFamily: "Arial, sans-serif", fontSizePx: 24, bold: !0, color: "#1a1a2e" },
167
+ para: { spaceBeforePx: 18, spaceAfterPx: 8, keepWithNext: !0 }
168
+ },
169
+ {
170
+ id: "Heading2",
171
+ name: "Heading 2",
172
+ basedOn: "Heading1",
173
+ char: { fontSizePx: 19 },
174
+ para: { spaceBeforePx: 14, spaceAfterPx: 6 }
175
+ },
176
+ {
177
+ id: "Quote",
178
+ name: "Quote",
179
+ basedOn: "Normal",
180
+ char: { italic: !0, color: "#5f6368" },
181
+ para: { indentLeftPx: 36, spaceBeforePx: 8, spaceAfterPx: 8 }
182
+ },
183
+ {
184
+ id: "Code",
185
+ name: "Code",
186
+ basedOn: "Normal",
187
+ char: { fontFamily: "Consolas, monospace", fontSizePx: 14, color: "#0b57d0" },
188
+ para: { lineHeight: 1.35 }
189
+ }
190
+ ]
191
+ };
192
+ }
193
+ const ne = [
194
+ [1e3, "m"],
195
+ [900, "cm"],
196
+ [500, "d"],
197
+ [400, "cd"],
198
+ [100, "c"],
199
+ [90, "xc"],
200
+ [50, "l"],
201
+ [40, "xl"],
202
+ [10, "x"],
203
+ [9, "ix"],
204
+ [5, "v"],
205
+ [4, "iv"],
206
+ [1, "i"]
207
+ ];
208
+ function Q(n) {
209
+ let e = "", t = Math.max(1, n);
210
+ for (const [s, r] of ne)
211
+ for (; t >= s; )
212
+ e += r, t -= s;
213
+ return e;
214
+ }
215
+ function q(n) {
216
+ let e = "", t = Math.max(1, n);
217
+ for (; t > 0; )
218
+ t -= 1, e = String.fromCharCode(97 + t % 26) + e, t = Math.floor(t / 26);
219
+ return e;
220
+ }
221
+ function se(n, e) {
222
+ switch (e) {
223
+ case "decimal":
224
+ return String(n);
225
+ case "lowerLetter":
226
+ return q(n);
227
+ case "upperLetter":
228
+ return q(n).toUpperCase();
229
+ case "lowerRoman":
230
+ return Q(n);
231
+ case "upperRoman":
232
+ return Q(n).toUpperCase();
233
+ case "bullet":
234
+ return "";
235
+ }
236
+ }
237
+ function Re(n, e, t) {
238
+ const s = n.levels[Math.min(e, n.levels.length - 1)];
239
+ return s ? s.format === "bullet" ? s.bulletChar ?? "•" : s.text.replace(/%(\d)/g, (r, i) => {
240
+ const o = Number(i) - 1, a = n.levels[Math.min(o, n.levels.length - 1)], l = t[o] ?? a?.start ?? 1;
241
+ return se(l, a?.format ?? "decimal");
242
+ }) : "";
243
+ }
244
+ const re = "bullets", oe = "numbers", ie = "multilevel", le = ["•", "◦", "▪"], ae = ["decimal", "lowerLetter", "lowerRoman"];
245
+ function Fe(n) {
246
+ const e = [];
247
+ for (let t = 0; t < 9; t++)
248
+ n === "bullet" ? e.push({
249
+ format: "bullet",
250
+ text: "",
251
+ bulletChar: le[t % 3],
252
+ indentLeftPx: 24 + t * 24,
253
+ hangingPx: 18,
254
+ start: 1
255
+ }) : e.push({
256
+ format: ae[t % 3],
257
+ text: `%${t + 1}.`,
258
+ indentLeftPx: 24 + t * 24,
259
+ hangingPx: 22,
260
+ start: 1
261
+ });
262
+ return { id: n === "bullet" ? re : oe, levels: e };
263
+ }
264
+ function Le(n, e) {
265
+ const t = [];
266
+ for (let s = 0; s < 9; s++)
267
+ t.push({ format: "bullet", text: "", bulletChar: e, indentLeftPx: 24 + s * 24, hangingPx: 18, start: 1 });
268
+ return { id: n, levels: t };
269
+ }
270
+ function Te(n, e, t) {
271
+ const s = [];
272
+ for (let r = 0; r < 9; r++)
273
+ s.push({ format: e, text: `%${r + 1}${t}`, indentLeftPx: 24 + r * 24, hangingPx: 22, start: 1 });
274
+ return { id: n, levels: s };
275
+ }
276
+ function Ee() {
277
+ const n = [];
278
+ for (let e = 0; e < 9; e++) {
279
+ const t = Array.from({ length: e + 1 }, (s, r) => `%${r + 1}`).join(".") + ".";
280
+ n.push({
281
+ format: "decimal",
282
+ text: t,
283
+ // "%1.", "%1.%2.", "%1.%2.%3.", …
284
+ indentLeftPx: 24 + e * 24,
285
+ hangingPx: 24 + e * 8,
286
+ // compound markers widen with depth
287
+ start: 1
288
+ });
289
+ }
290
+ return { id: ie, levels: n };
291
+ }
292
+ function ce(n, e) {
293
+ return n.fontFamily === e.fontFamily && n.fontSizePx === e.fontSizePx && n.bold === e.bold && n.italic === e.italic && n.underline === e.underline && n.strikethrough === e.strikethrough && n.color === e.color && !!n.hidden == !!e.hidden && // hidden runs must never merge with visible ones
294
+ (n.letterSpacingPx ?? 0) === (e.letterSpacingPx ?? 0) && n.highlightColor === e.highlightColor && n.verticalAlign === e.verticalAlign && n.link === e.link && n.footnoteRef === e.footnoteRef && // adjacent refs must never merge into one run
295
+ n.sdtId === e.sdtId;
296
+ }
297
+ function I(n, e) {
298
+ const t = n.filter((r) => r.text.length > 0);
299
+ if (t.length === 0) return [{ text: "", style: n[0]?.style ?? e }];
300
+ const s = [{ ...t[0] }];
301
+ for (let r = 1; r < t.length; r++) {
302
+ const i = t[r], o = s[s.length - 1];
303
+ ce(o.style, i.style) ? o.text += i.text : s.push({ ...i });
304
+ }
305
+ return s;
306
+ }
307
+ function g(n, e) {
308
+ const t = [], s = [];
309
+ let r = 0;
310
+ for (const i of n) {
311
+ const o = r + i.text.length;
312
+ o <= e ? t.push(i) : r >= e ? s.push(i) : (t.push({ text: i.text.slice(0, e - r), style: i.style }), s.push({ text: i.text.slice(e - r), style: i.style })), r = o;
313
+ }
314
+ return [t, s];
315
+ }
316
+ function de(n, e, t) {
317
+ const [, s] = g(n, e), [r] = g(s, t - e);
318
+ return r;
319
+ }
320
+ function p(n, e) {
321
+ return te(n, e) ?? {
322
+ fontFamily: "Georgia, serif",
323
+ fontSizePx: 16,
324
+ bold: !1,
325
+ italic: !1,
326
+ underline: !1,
327
+ strikethrough: !1,
328
+ color: "#202124"
329
+ };
330
+ }
331
+ function fe(n, e, t, s) {
332
+ const r = s ?? p(n, e), [i, o] = g(n, e);
333
+ return I([...i, { text: t, style: r }, ...o], r);
334
+ }
335
+ function ue(n, e, t) {
336
+ const [s, r] = g(n, e);
337
+ return I([...s, ...t, ...r], p(n, e));
338
+ }
339
+ function be(n, e, t) {
340
+ const [s, r] = g(n, e), [, i] = g(r, t - e);
341
+ return I([...s, ...i], p(n, e));
342
+ }
343
+ function Ae(n, e, t, s) {
344
+ const [r, i] = g(n, e), [o, a] = g(i, t - e), l = o.map((c) => ({ text: c.text, style: { ...c.style, ...s } }));
345
+ return I([...r, ...l, ...a], p(n, e));
346
+ }
347
+ function Ne(n, e, t, s) {
348
+ const [r, i] = g(n, e), [o, a] = g(i, t - e), l = o.map((b) => b.text).join(""), c = s(l);
349
+ let d = 0;
350
+ const f = c.length === l.length ? o.map((b) => {
351
+ const u = c.slice(d, d + b.text.length);
352
+ return d += b.text.length, { text: u, style: b.style };
353
+ }) : o;
354
+ return I([...r, ...f, ...a], p(n, e));
355
+ }
356
+ const k = (n) => n;
357
+ function F(n, e) {
358
+ const t = E(n, e);
359
+ if (!t) throw new Error(`paragraph ${e} not found`);
360
+ return { loc: t, block: C(n, t) };
361
+ }
362
+ function h(n, e) {
363
+ return e === "body" ? n.blocks : n.section[e] ?? [];
364
+ }
365
+ function x(n, e, t) {
366
+ return e === "body" ? { ...n, blocks: t } : { ...n, section: { ...n.section, [e]: t } };
367
+ }
368
+ function W(n, e) {
369
+ for (const t of ["body", ...$]) {
370
+ const s = h(n, t).findIndex((r) => r.id === e);
371
+ if (s >= 0) return { where: t, index: s };
372
+ }
373
+ return null;
374
+ }
375
+ function y(n, e) {
376
+ const t = W(n, e), s = t ? h(n, t.where)[t.index] : void 0;
377
+ if (!t || !s || s.kind !== "table") throw new Error(`table ${e} not found`);
378
+ return { where: t.where, bi: t.index, block: s };
379
+ }
380
+ function w(n, e, t, s) {
381
+ const r = h(n, e).slice();
382
+ return r[t] = { ...s, revision: s.revision + 1 }, x(n, e, r);
383
+ }
384
+ function N(n, e, t, s, r, i) {
385
+ const o = h(n, e).slice(), a = o[t], l = a.rows.slice(), c = { cells: l[s].cells.slice() };
386
+ return c.cells[r] = { ...c.cells[r], blocks: i }, l[s] = c, o[t] = { ...a, rows: l, revision: a.revision + 1 }, x(n, e, o);
387
+ }
388
+ function T(n) {
389
+ for (const e of n)
390
+ for (const t of e.cells)
391
+ for (const s of t.blocks) if (s.kind === "paragraph") return s;
392
+ }
393
+ function he(n, e) {
394
+ const t = W(n, e);
395
+ if (t) {
396
+ const s = h(n, t.where)[t.index];
397
+ return s?.kind === "image" ? { kind: "top", where: t.where, index: t.index, image: s } : null;
398
+ }
399
+ for (let s = 0; s < n.blocks.length; s++) {
400
+ const r = n.blocks[s];
401
+ if (r.kind === "table")
402
+ for (let i = 0; i < r.rows.length; i++) {
403
+ const o = r.rows[i];
404
+ for (let a = 0; a < o.cells.length; a++) {
405
+ const l = o.cells[a];
406
+ for (let c = 0; c < l.blocks.length; c++) {
407
+ const d = l.blocks[c];
408
+ if (d.kind === "image" && d.id === e) return { kind: "cell", bi: s, ri: i, ci: a, ii: c, image: d };
409
+ }
410
+ }
411
+ }
412
+ }
413
+ return null;
414
+ }
415
+ function ke(n) {
416
+ let e = 1;
417
+ const t = [];
418
+ for (const s of n.rows) {
419
+ let r = 0;
420
+ for (const i of s.cells) {
421
+ for (; (t[r] ?? 0) > 0; ) r++;
422
+ const o = Math.max(1, i.colSpan ?? 1), a = Math.max(1, i.rowSpan ?? 1);
423
+ if (a > 1) for (let l = 0; l < o; l++) t[r + l] = a;
424
+ r += o;
425
+ }
426
+ e = Math.max(e, r);
427
+ for (let i = 0; i < t.length; i++) t[i] > 0 && t[i]--;
428
+ }
429
+ return e;
430
+ }
431
+ function O(n) {
432
+ const e = ke(n);
433
+ return n.colFractions && n.colFractions.length === e ? n.colFractions : Array.from({ length: e }, () => 1 / e);
434
+ }
435
+ function v(n, e) {
436
+ return { ...n, runs: e, revision: n.revision + 1 };
437
+ }
438
+ function Oe(n, e) {
439
+ switch (e.type) {
440
+ case "insertText": {
441
+ const { loc: t, block: s } = F(n, e.at.blockId), r = fe(s.runs, e.at.offset, e.text, e.style), i = e.text.length;
442
+ return {
443
+ doc: R(n, t, v(s, r)),
444
+ inverse: { type: "deleteRange", blockId: s.id, start: e.at.offset, end: e.at.offset + i },
445
+ mapPosition: (o) => o.blockId === s.id && o.offset >= e.at.offset ? { blockId: o.blockId, offset: o.offset + i } : o,
446
+ dirtyBlockIds: [s.id]
447
+ };
448
+ }
449
+ case "insertRuns": {
450
+ const { loc: t, block: s } = F(n, e.at.blockId), r = M(e.runs).length, i = ue(s.runs, e.at.offset, e.runs);
451
+ return {
452
+ doc: R(n, t, v(s, i)),
453
+ inverse: { type: "deleteRange", blockId: s.id, start: e.at.offset, end: e.at.offset + r },
454
+ mapPosition: (o) => o.blockId === s.id && o.offset >= e.at.offset ? { blockId: o.blockId, offset: o.offset + r } : o,
455
+ dirtyBlockIds: [s.id]
456
+ };
457
+ }
458
+ case "deleteRange": {
459
+ const { loc: t, block: s } = F(n, e.blockId), r = de(s.runs, e.start, e.end), i = be(s.runs, e.start, e.end), o = e.end - e.start;
460
+ return {
461
+ doc: R(n, t, v(s, i)),
462
+ inverse: { type: "insertRuns", at: { blockId: s.id, offset: e.start }, runs: r },
463
+ mapPosition: (a) => a.blockId !== s.id || a.offset <= e.start ? a : { blockId: a.blockId, offset: a.offset >= e.end ? a.offset - o : e.start },
464
+ dirtyBlockIds: [s.id]
465
+ };
466
+ }
467
+ case "setRuns": {
468
+ const { loc: t, block: s } = F(n, e.blockId);
469
+ return {
470
+ doc: R(n, t, v(s, e.runs)),
471
+ inverse: { type: "setRuns", blockId: s.id, runs: s.runs },
472
+ mapPosition: k,
473
+ // callers use setRuns for length-preserving restyles
474
+ dirtyBlockIds: [s.id]
475
+ };
476
+ }
477
+ case "setParaStyle": {
478
+ const { loc: t, block: s } = F(n, e.blockId), r = {};
479
+ for (const o of Object.keys(e.patch))
480
+ r[o] = s.style[o];
481
+ const i = {
482
+ ...s,
483
+ style: { ...s.style, ...e.patch },
484
+ revision: s.revision + 1
485
+ };
486
+ return {
487
+ doc: R(n, t, i),
488
+ inverse: { type: "setParaStyle", blockId: s.id, patch: r },
489
+ mapPosition: k,
490
+ dirtyBlockIds: [s.id]
491
+ };
492
+ }
493
+ case "splitParagraph": {
494
+ const t = E(n, e.at.blockId);
495
+ if (!t) throw new Error(`paragraph ${e.at.blockId} not found`);
496
+ const s = C(n, t), [r, i] = g(s.runs, e.at.offset), o = p(s.runs, e.at.offset), a = v(s, I(r, o)), l = {
497
+ kind: "paragraph",
498
+ id: e.newBlockId,
499
+ revision: 0,
500
+ runs: I(i, o),
501
+ style: e.newStyle ?? { ...s.style }
502
+ };
503
+ let c;
504
+ if (t.kind === "cell") {
505
+ const f = h(n, t.where)[t.bi].rows[t.ri].cells[t.ci].blocks.slice();
506
+ f.splice(t.pi, 1, a, l), c = N(n, t.where, t.bi, t.ri, t.ci, f);
507
+ } else if (t.kind === "footnote") {
508
+ const d = n.footnotes[t.noteId].slice();
509
+ d.splice(t.pi, 1, a, l), c = { ...n, footnotes: { ...n.footnotes, [t.noteId]: d } };
510
+ } else {
511
+ const d = t.kind === "band" ? t.band : "body", f = h(n, d).slice();
512
+ f.splice(t.bi, 1, a, l), c = x(n, d, f);
513
+ }
514
+ return {
515
+ doc: c,
516
+ inverse: { type: "mergeParagraphs", firstBlockId: s.id },
517
+ mapPosition: (d) => d.blockId === s.id && d.offset >= e.at.offset ? { blockId: e.newBlockId, offset: d.offset - e.at.offset } : d,
518
+ dirtyBlockIds: [s.id, e.newBlockId]
519
+ };
520
+ }
521
+ case "mergeParagraphs": {
522
+ const t = E(n, e.firstBlockId);
523
+ if (!t) throw new Error(`paragraph ${e.firstBlockId} not found`);
524
+ const s = C(n, t);
525
+ let r;
526
+ if (t.kind === "cell") {
527
+ const c = h(n, t.where)[t.bi].rows[t.ri].cells[t.ci].blocks[t.pi + 1];
528
+ if (!c || c.kind !== "paragraph")
529
+ throw new Error("mergeParagraphs: no next paragraph in cell");
530
+ r = c;
531
+ } else if (t.kind === "footnote") {
532
+ const l = n.footnotes[t.noteId][t.pi + 1];
533
+ if (!l) throw new Error("mergeParagraphs: no next paragraph in footnote");
534
+ r = l;
535
+ } else {
536
+ const l = t.kind === "band" ? t.band : "body", c = h(n, l)[t.bi + 1];
537
+ if (!c || c.kind !== "paragraph")
538
+ throw new Error("mergeParagraphs: no next paragraph");
539
+ r = c;
540
+ }
541
+ const i = M(s.runs).length, o = v(
542
+ s,
543
+ I([...s.runs, ...r.runs], p(s.runs, i))
544
+ );
545
+ let a;
546
+ if (t.kind === "cell") {
547
+ const c = h(n, t.where)[t.bi].rows[t.ri].cells[t.ci].blocks.slice();
548
+ c.splice(t.pi, 2, o), a = N(n, t.where, t.bi, t.ri, t.ci, c);
549
+ } else if (t.kind === "footnote") {
550
+ const l = n.footnotes[t.noteId].slice();
551
+ l.splice(t.pi, 2, o), a = { ...n, footnotes: { ...n.footnotes, [t.noteId]: l } };
552
+ } else {
553
+ const l = t.kind === "band" ? t.band : "body", c = h(n, l).slice();
554
+ c.splice(t.bi, 2, o), a = x(n, l, c);
555
+ }
556
+ return {
557
+ doc: a,
558
+ inverse: {
559
+ type: "splitParagraph",
560
+ at: { blockId: s.id, offset: i },
561
+ newBlockId: r.id,
562
+ newStyle: r.style
563
+ },
564
+ mapPosition: (l) => l.blockId === r.id ? { blockId: s.id, offset: i + l.offset } : l,
565
+ dirtyBlockIds: [s.id, r.id]
566
+ };
567
+ }
568
+ case "insertBlock": {
569
+ const t = e.where ?? "body", s = h(n, t).slice();
570
+ return s.splice(e.index, 0, e.block), {
571
+ doc: x(n, t, s),
572
+ inverse: { type: "removeBlock", blockId: e.block.id },
573
+ mapPosition: k,
574
+ dirtyBlockIds: [e.block.id]
575
+ };
576
+ }
577
+ case "removeBlock": {
578
+ const t = W(n, e.blockId);
579
+ if (!t) throw new Error(`block ${e.blockId} not found`);
580
+ const s = h(n, t.where).slice(), r = s[t.index];
581
+ s.splice(t.index, 1);
582
+ const i = s[Math.min(t.index, s.length - 1)];
583
+ return {
584
+ doc: x(n, t.where, s),
585
+ inverse: { type: "insertBlock", index: t.index, block: r, where: t.where },
586
+ mapPosition: (o) => o.blockId === e.blockId && i ? { blockId: i.id, offset: 0 } : o,
587
+ dirtyBlockIds: [e.blockId]
588
+ };
589
+ }
590
+ case "setImageProps": {
591
+ const t = he(n, e.blockId);
592
+ if (!t) throw new Error(`image ${e.blockId} not found`);
593
+ const s = t.image, r = {};
594
+ for (const a of Object.keys(e.patch))
595
+ r[a] = s[a];
596
+ const i = { ...s, ...e.patch, revision: s.revision + 1 };
597
+ let o;
598
+ if (t.kind === "top") {
599
+ const a = h(n, t.where).slice();
600
+ a[t.index] = i, o = x(n, t.where, a);
601
+ } else {
602
+ const l = n.blocks[t.bi].rows[t.ri].cells[t.ci].blocks.slice();
603
+ l[t.ii] = i, o = N(n, "body", t.bi, t.ri, t.ci, l);
604
+ }
605
+ return {
606
+ doc: o,
607
+ inverse: { type: "setImageProps", blockId: e.blockId, patch: r },
608
+ mapPosition: k,
609
+ dirtyBlockIds: [e.blockId]
610
+ };
611
+ }
612
+ case "setTableColFractions": {
613
+ const { where: t, bi: s, block: r } = y(n, e.blockId), i = O(r);
614
+ return {
615
+ doc: w(n, t, s, { ...r, colFractions: e.fractions }),
616
+ inverse: { type: "setTableColFractions", blockId: e.blockId, fractions: i },
617
+ mapPosition: k,
618
+ dirtyBlockIds: [e.blockId]
619
+ };
620
+ }
621
+ case "setTableRow": {
622
+ const { where: t, bi: s, block: r } = y(n, e.tableId), i = r.rows[e.rowIndex];
623
+ if (!i) throw new Error("setTableRow: no such row");
624
+ const o = new Set(i.cells.flatMap((c) => c.blocks.map((d) => d.id))), a = r.rows.slice();
625
+ a[e.rowIndex] = e.row;
626
+ const l = T([e.row]);
627
+ return {
628
+ doc: w(n, t, s, { ...r, rows: a }),
629
+ inverse: { type: "setTableRow", tableId: e.tableId, rowIndex: e.rowIndex, row: i },
630
+ mapPosition: (c) => o.has(c.blockId) ? e.row.cells.some((f) => f.blocks.some((b) => b.id === c.blockId)) || !l ? c : { blockId: l.id, offset: 0 } : c,
631
+ dirtyBlockIds: [e.tableId]
632
+ };
633
+ }
634
+ case "insertTableRow": {
635
+ const { where: t, bi: s, block: r } = y(n, e.tableId), i = r.rows.slice();
636
+ return i.splice(e.rowIndex, 0, e.row), {
637
+ doc: w(n, t, s, { ...r, rows: i }),
638
+ inverse: { type: "removeTableRow", tableId: e.tableId, rowIndex: e.rowIndex },
639
+ mapPosition: k,
640
+ dirtyBlockIds: [e.tableId]
641
+ };
642
+ }
643
+ case "removeTableRow": {
644
+ const { where: t, bi: s, block: r } = y(n, e.tableId), i = r.rows[e.rowIndex];
645
+ if (!i) throw new Error("removeTableRow: no such row");
646
+ const o = r.rows.slice();
647
+ o.splice(e.rowIndex, 1);
648
+ const a = new Set(i.cells.flatMap((d) => d.blocks.map((f) => f.id))), l = o[Math.min(e.rowIndex, o.length - 1)], c = l ? T([l]) : void 0;
649
+ return {
650
+ doc: w(n, t, s, { ...r, rows: o }),
651
+ inverse: { type: "insertTableRow", tableId: e.tableId, rowIndex: e.rowIndex, row: i },
652
+ mapPosition: (d) => a.has(d.blockId) && c ? { blockId: c.id, offset: 0 } : d,
653
+ dirtyBlockIds: [e.tableId]
654
+ };
655
+ }
656
+ case "setListDefinition": {
657
+ const t = n.lists?.[e.listId] ?? null, s = { ...n.lists ?? {} };
658
+ return e.def ? s[e.listId] = e.def : delete s[e.listId], {
659
+ doc: { ...n, lists: s },
660
+ inverse: { type: "setListDefinition", listId: e.listId, def: t },
661
+ mapPosition: k,
662
+ // Indents come from the definition → every paragraph in the list must
663
+ // re-measure; their (revision,width) line-cache keys change with width.
664
+ dirtyBlockIds: []
665
+ };
666
+ }
667
+ case "setTableStructure": {
668
+ const { where: t, bi: s, block: r } = y(n, e.tableId), i = new Set(
669
+ r.rows.flatMap((c) => c.cells.flatMap((d) => d.blocks.map((f) => f.id)))
670
+ ), o = T(e.rows), a = { ...r, rows: e.rows };
671
+ e.colFractions ? a.colFractions = e.colFractions : delete a.colFractions;
672
+ const l = { type: "setTableStructure", tableId: e.tableId, rows: r.rows };
673
+ return r.colFractions && (l.colFractions = r.colFractions), {
674
+ doc: w(n, t, s, a),
675
+ inverse: l,
676
+ mapPosition: (c) => i.has(c.blockId) ? e.rows.some(
677
+ (f) => f.cells.some((b) => b.blocks.some((u) => u.id === c.blockId))
678
+ ) || !o ? c : { blockId: o.id, offset: 0 } : c,
679
+ dirtyBlockIds: [e.tableId]
680
+ };
681
+ }
682
+ case "insertTableColumn": {
683
+ const { where: t, bi: s, block: r } = y(n, e.tableId), i = { type: "setTableStructure", tableId: e.tableId, rows: r.rows };
684
+ r.colFractions && (i.colFractions = r.colFractions);
685
+ const o = r.rows.map((l, c) => {
686
+ const d = [];
687
+ let f = 0, b = !1;
688
+ for (const u of l.cells) {
689
+ const m = u.colSpan ?? 1;
690
+ if (!b && e.colIndex > f && e.colIndex < f + m)
691
+ d.push({ ...u, colSpan: m + 1 }), b = !0;
692
+ else if (!b && e.colIndex === f) {
693
+ const P = e.cells[c];
694
+ P && d.push(P), d.push(u), b = !0;
695
+ } else
696
+ d.push(u);
697
+ f += m;
698
+ }
699
+ if (!b) {
700
+ const u = e.cells[c];
701
+ u && d.push(u);
702
+ }
703
+ return { cells: d };
704
+ });
705
+ let a = e.fractions;
706
+ if (!a) {
707
+ const l = O(r), c = 1 / (l.length + 1);
708
+ a = l.map((d) => d * (1 - c)), a.splice(e.colIndex, 0, c);
709
+ }
710
+ return {
711
+ doc: w(n, t, s, { ...r, rows: o, colFractions: a }),
712
+ inverse: i,
713
+ mapPosition: k,
714
+ dirtyBlockIds: [e.tableId]
715
+ };
716
+ }
717
+ case "setStylesheet": {
718
+ const t = n.stylesheet ?? { styles: [], defaultStyleId: "Normal" };
719
+ return {
720
+ doc: { ...n, stylesheet: e.stylesheet },
721
+ inverse: { type: "setStylesheet", stylesheet: t },
722
+ mapPosition: k,
723
+ dirtyBlockIds: []
724
+ // restyling paragraphs happens via setRuns/setParaStyle ops
725
+ };
726
+ }
727
+ case "setSectionProps": {
728
+ const t = n.section, s = {
729
+ pageWidthPx: t.pageWidthPx,
730
+ pageHeightPx: t.pageHeightPx,
731
+ marginPx: { ...t.marginPx },
732
+ columns: t.columns ? { ...t.columns } : null,
733
+ pageNumberStart: t.pageNumberStart ?? null
734
+ }, r = {
735
+ ...t,
736
+ pageWidthPx: e.geometry.pageWidthPx,
737
+ pageHeightPx: e.geometry.pageHeightPx,
738
+ marginPx: e.geometry.marginPx
739
+ };
740
+ return e.geometry.columns ? r.columns = e.geometry.columns : delete r.columns, e.geometry.pageNumberStart !== null ? r.pageNumberStart = e.geometry.pageNumberStart : delete r.pageNumberStart, {
741
+ doc: { ...n, section: r },
742
+ inverse: { type: "setSectionProps", geometry: s },
743
+ mapPosition: k,
744
+ dirtyBlockIds: []
745
+ // geometry change → full re-walk; line caches keyed by width
746
+ };
747
+ }
748
+ case "setFootnote": {
749
+ const t = n.footnotes?.[e.noteId] ?? null, s = { ...n.footnotes ?? {} };
750
+ e.paras ? s[e.noteId] = e.paras : delete s[e.noteId];
751
+ const r = new Set((t ?? []).map((o) => o.id)), i = n.blocks.find((o) => o.kind === "paragraph");
752
+ return {
753
+ doc: { ...n, footnotes: s },
754
+ inverse: { type: "setFootnote", noteId: e.noteId, paras: t },
755
+ mapPosition: (o) => r.has(o.blockId) ? (e.paras ?? []).some((l) => l.id === o.blockId) || !i ? o : { blockId: i.id, offset: 0 } : o,
756
+ dirtyBlockIds: []
757
+ };
758
+ }
759
+ case "setSdtProps": {
760
+ const t = n.sdts?.[e.id] ?? null, s = { ...n.sdts ?? {} };
761
+ return e.props ? s[e.id] = e.props : delete s[e.id], {
762
+ doc: { ...n, sdts: s },
763
+ inverse: { type: "setSdtProps", id: e.id, props: t },
764
+ mapPosition: k,
765
+ dirtyBlockIds: []
766
+ // run markers change via setRuns/applyStylePatch ops
767
+ };
768
+ }
769
+ case "setBookmark": {
770
+ const t = n.bookmarks?.[e.name] ?? null, s = { ...n.bookmarks ?? {} };
771
+ return e.range ? s[e.name] = e.range : delete s[e.name], {
772
+ doc: { ...n, bookmarks: s },
773
+ inverse: { type: "setBookmark", name: e.name, range: t },
774
+ mapPosition: k,
775
+ dirtyBlockIds: []
776
+ };
777
+ }
778
+ case "setSectionBand": {
779
+ const t = n.section[e.band] ?? null, s = { ...n.section };
780
+ e.blocks ? s[e.band] = e.blocks : delete s[e.band];
781
+ const r = new Set(
782
+ (t ?? []).filter((o) => o.kind === "paragraph").map((o) => o.id)
783
+ ), i = n.blocks.find((o) => o.kind === "paragraph");
784
+ return {
785
+ doc: { ...n, section: s },
786
+ inverse: { type: "setSectionBand", band: e.band, blocks: t },
787
+ mapPosition: (o) => r.has(o.blockId) ? (e.blocks ?? []).some((l) => l.id === o.blockId) || !i ? o : { blockId: i.id, offset: 0 } : o,
788
+ dirtyBlockIds: []
789
+ };
790
+ }
791
+ case "removeTableColumn": {
792
+ const { where: t, bi: s, block: r } = y(n, e.tableId), i = { type: "setTableStructure", tableId: e.tableId, rows: r.rows };
793
+ r.colFractions && (i.colFractions = r.colFractions);
794
+ const o = O(r), a = /* @__PURE__ */ new Set(), l = r.rows.map((u) => {
795
+ const m = [];
796
+ let P = 0;
797
+ for (const L of u.cells) {
798
+ const S = L.colSpan ?? 1;
799
+ if (e.colIndex >= P && e.colIndex < P + S)
800
+ if (S > 1) {
801
+ const B = { ...L };
802
+ S - 1 > 1 ? B.colSpan = S - 1 : delete B.colSpan, m.push(B);
803
+ } else
804
+ for (const B of L.blocks) a.add(B.id);
805
+ else
806
+ m.push(L);
807
+ P += S;
808
+ }
809
+ return { cells: m };
810
+ }), c = o.filter((u, m) => m !== e.colIndex), d = c.reduce((u, m) => u + m, 0) || 1, f = c.map((u) => u / d), b = T(l);
811
+ return {
812
+ doc: w(n, t, s, { ...r, rows: l, colFractions: f }),
813
+ inverse: i,
814
+ mapPosition: (u) => a.has(u.blockId) && b ? { blockId: b.id, offset: 0 } : u,
815
+ dirtyBlockIds: [e.tableId]
816
+ };
817
+ }
818
+ }
819
+ }
820
+ function j(n, e = 0) {
821
+ let t = e;
822
+ return {
823
+ siteId: n,
824
+ next: () => `${n}-${(t++).toString(36)}`,
825
+ count: () => t
826
+ };
827
+ }
828
+ let z = j("local");
829
+ function Ce(n, e = 0) {
830
+ z = j(n, e);
831
+ }
832
+ function Me() {
833
+ return z.next();
834
+ }
835
+ function De() {
836
+ return z.siteId;
837
+ }
838
+ const X = {
839
+ fontFamily: "Georgia, serif",
840
+ fontSizePx: 16,
841
+ bold: !1,
842
+ italic: !1,
843
+ underline: !1,
844
+ strikethrough: !1,
845
+ color: "#202124"
846
+ }, Y = {
847
+ align: "left",
848
+ lineHeight: 1.5,
849
+ spaceBeforePx: 0,
850
+ spaceAfterPx: 12,
851
+ indentFirstLinePx: 0,
852
+ indentLeftPx: 0
853
+ };
854
+ function _e(n, e) {
855
+ const t = n.stylesheet ? _(n.stylesheet, n.stylesheet.defaultStyleId) : { char: {}, para: {} };
856
+ return {
857
+ kind: "paragraph",
858
+ id: e,
859
+ revision: 0,
860
+ runs: [{ text: "", style: { ...X, ...t.char } }],
861
+ style: { ...Y, ...t.para }
862
+ };
863
+ }
864
+ class $e {
865
+ ids;
866
+ doc;
867
+ warnings = [];
868
+ seenWarnings = /* @__PURE__ */ new Set();
869
+ /** Concrete defaults for new runs/paragraphs (default style resolved). */
870
+ charDefault;
871
+ paraDefault;
872
+ constructor(e, t) {
873
+ this.doc = e, this.ids = j(t ?? `bld${Math.random().toString(36).slice(2, 6)}`);
874
+ const s = e.stylesheet ? _(e.stylesheet, e.stylesheet.defaultStyleId) : { char: {}, para: {} };
875
+ this.charDefault = { ...X, ...s.char }, this.paraDefault = { ...Y, ...s.para };
876
+ }
877
+ warn(e, t) {
878
+ this.seenWarnings.has(e) || (this.seenWarnings.add(e), this.warnings.push({ code: e, message: t }));
879
+ }
880
+ run(e, t = {}) {
881
+ return { text: e, style: { ...this.charDefault, ...t } };
882
+ }
883
+ paragraph(e, t = {}) {
884
+ return {
885
+ kind: "paragraph",
886
+ id: this.ids.next(),
887
+ revision: 0,
888
+ runs: e.length > 0 ? e : [this.run("")],
889
+ style: { ...this.paraDefault, ...t }
890
+ };
891
+ }
892
+ image(e, t, s, r, i) {
893
+ const o = { kind: "image", id: this.ids.next(), revision: 0, src: e, widthPx: t, heightPx: s, align: r };
894
+ return i && (o.wrap = i), o;
895
+ }
896
+ /** The stylesheet, creating an empty one on demand for style registration. */
897
+ stylesheet() {
898
+ return this.doc.stylesheet || (this.doc.stylesheet = { styles: [], defaultStyleId: "Normal" }), this.doc.stylesheet;
899
+ }
900
+ /** Resolved char+para templates for a named style, or undefined (+ warning). */
901
+ lookupStyle(e) {
902
+ const t = this.doc.stylesheet;
903
+ if (!t || !D(t, e)) {
904
+ this.warn(`style-missing:${e}`, `Named style "${e}" is not in the stylesheet — .withStyle("${e}") was ignored.`);
905
+ return;
906
+ }
907
+ return _(t, e);
908
+ }
909
+ }
910
+ class He extends Error {
911
+ code;
912
+ constructor(e, t) {
913
+ super(t), this.name = "ImportError", this.code = e;
914
+ }
915
+ }
916
+ class We {
917
+ list = [];
918
+ seen = /* @__PURE__ */ new Set();
919
+ add(e, t) {
920
+ this.seen.has(e) || (this.seen.add(e), this.list.push({ code: e, message: t }));
921
+ }
922
+ }
923
+ export {
924
+ Ae as A,
925
+ $ as B,
926
+ te as C,
927
+ oe as D,
928
+ h as E,
929
+ ye as F,
930
+ D as G,
931
+ _ as H,
932
+ He as I,
933
+ V as J,
934
+ we as K,
935
+ W as L,
936
+ he as M,
937
+ Ee as N,
938
+ ie as O,
939
+ Se as P,
940
+ ge as Q,
941
+ Ce as R,
942
+ _e as S,
943
+ Le as T,
944
+ Te as U,
945
+ We as W,
946
+ re as a,
947
+ Be as b,
948
+ $e as c,
949
+ Fe as d,
950
+ Oe as e,
951
+ De as f,
952
+ ke as g,
953
+ Me as h,
954
+ me as i,
955
+ O as j,
956
+ se as k,
957
+ Z as l,
958
+ Re as m,
959
+ I as n,
960
+ Pe as o,
961
+ H as p,
962
+ xe as q,
963
+ ve as r,
964
+ de as s,
965
+ M as t,
966
+ pe as u,
967
+ Ne as v,
968
+ K as w,
969
+ E as x,
970
+ Ie as y,
971
+ ee as z
972
+ };