@univerjs/docs 0.25.1 → 1.0.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1408 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
+ let _univerjs_core = require("@univerjs/core");
3
+ let _univerjs_core_facade = require("@univerjs/core/facade");
4
+ let _univerjs_docs = require("@univerjs/docs");
5
+
6
+ //#region src/facade/f-document-element.ts
7
+ /**
8
+ * A generic top-level document body element.
9
+ *
10
+ * Use this wrapper when you need to inspect an element type first, navigate to
11
+ * neighboring elements, or cast the element to a more specific facade wrapper.
12
+ *
13
+ * Paragraph keys are persisted `paragraphId` values. Tables, block ranges, and
14
+ * custom blocks use their persisted ids.
15
+ *
16
+ * @hideconstructor
17
+ */
18
+ var FDocumentElement = class extends _univerjs_core_facade.FBase {
19
+ constructor(_body, _bodyEdit, _info, _injector) {
20
+ super();
21
+ this._body = _body;
22
+ this._bodyEdit = _bodyEdit;
23
+ this._info = _info;
24
+ this._injector = _injector;
25
+ }
26
+ /**
27
+ * Get the document element type.
28
+ * @returns {DocumentBlockType} The element type, such as `paragraph`, `table`, `blockRange`, or `customBlock`.
29
+ * @example
30
+ * ```ts
31
+ * const fDocument = univerAPI.getActiveDocument();
32
+ * const fDocumentBody = fDocument.getBody();
33
+ * const element = fDocumentBody.getElement(0);
34
+ * console.log(element?.getType());
35
+ * ```
36
+ */
37
+ getType() {
38
+ return this._info.type;
39
+ }
40
+ /**
41
+ * Whether this element is a paragraph.
42
+ * @returns {boolean} `true` if this element is a paragraph.
43
+ * @example
44
+ * ```ts
45
+ * const fDocument = univerAPI.getActiveDocument();
46
+ * const fDocumentBody = fDocument.getBody();
47
+ * const element = fDocumentBody.getElement(0);
48
+ * console.log(element?.isParagraph());
49
+ * ```
50
+ */
51
+ isParagraph() {
52
+ return this._info.type === _univerjs_core.DocumentBlockType.PARAGRAPH;
53
+ }
54
+ /**
55
+ * Whether this element is a table.
56
+ * @returns {boolean} `true` if this element is a table.
57
+ * @example
58
+ * ```ts
59
+ * const fDocument = univerAPI.getActiveDocument();
60
+ * const fDocumentBody = fDocument.getBody();
61
+ * const element = fDocumentBody.getElement(0);
62
+ * console.log(element?.isTable());
63
+ * ```
64
+ */
65
+ isTable() {
66
+ return this._info.type === _univerjs_core.DocumentBlockType.TABLE;
67
+ }
68
+ /**
69
+ * Whether this element is a block range, such as a callout, quote, or code block.
70
+ * @returns {boolean} `true` if this element is a block range.
71
+ * @example
72
+ * ```ts
73
+ * const fDocument = univerAPI.getActiveDocument();
74
+ * const fDocumentBody = fDocument.getBody();
75
+ * const element = fDocumentBody.getElement(0);
76
+ * console.log(element?.isBlockRange());
77
+ * ```
78
+ */
79
+ isBlockRange() {
80
+ return this._info.type === _univerjs_core.DocumentBlockType.BLOCK_RANGE;
81
+ }
82
+ /**
83
+ * Whether this element is a custom block.
84
+ * @returns {boolean} `true` if this element is a custom block.
85
+ * @example
86
+ * ```ts
87
+ * const fDocument = univerAPI.getActiveDocument();
88
+ * const fDocumentBody = fDocument.getBody();
89
+ * const element = fDocumentBody.getElement(0);
90
+ * console.log(element?.isCustomBlock());
91
+ * ```
92
+ */
93
+ isCustomBlock() {
94
+ return this._info.type === _univerjs_core.DocumentBlockType.CUSTOM_BLOCK;
95
+ }
96
+ /**
97
+ * Get the facade key used to resolve this element.
98
+ * @returns {string} The paragraph `paragraphId` or persisted table/block/custom block id.
99
+ * @example
100
+ * ```ts
101
+ * const fDocument = univerAPI.getActiveDocument();
102
+ * const fDocumentBody = fDocument.getBody();
103
+ * const element = fDocumentBody.getElement(0);
104
+ * console.log(element?.getKey());
105
+ * ```
106
+ */
107
+ getKey() {
108
+ return this._info.key;
109
+ }
110
+ /**
111
+ * Get the parent body facade that owns this element.
112
+ * @returns {FDocumentBody} The document body facade.
113
+ * @example
114
+ * ```ts
115
+ * const fDocument = univerAPI.getActiveDocument();
116
+ * const fDocumentBody = fDocument.getBody();
117
+ * const element = fDocumentBody.getElement(0);
118
+ * console.log(element?.getParent());
119
+ * ```
120
+ */
121
+ getParent() {
122
+ return this._body;
123
+ }
124
+ /**
125
+ * Get the resolved element info for this wrapper.
126
+ * @returns {IFDocumentElementInfo} The resolved element info, including type, key, position, and priority.
127
+ * @example
128
+ * ```ts
129
+ * const fDocument = univerAPI.getActiveDocument();
130
+ * const fDocumentBody = fDocument.getBody();
131
+ * const element = fDocumentBody.getElement(0);
132
+ * console.log(element?.getResolvedInfo());
133
+ * ```
134
+ */
135
+ getResolvedInfo() {
136
+ return this._info;
137
+ }
138
+ /**
139
+ * Get the next sibling element in the current body order.
140
+ * @returns {FDocumentElement | null} The next sibling wrapper, or `null` when this is the last child.
141
+ * @example
142
+ * ```ts
143
+ * const fDocument = univerAPI.getActiveDocument();
144
+ * const fDocumentBody = fDocument.getBody();
145
+ * const element = fDocumentBody.getElement(0);
146
+ * console.log(element?.getNextSibling());
147
+ * ```
148
+ */
149
+ getNextSibling() {
150
+ return this._createSibling(1);
151
+ }
152
+ /**
153
+ * Get the previous sibling element in the current body order.
154
+ * @returns {FDocumentElement | null} The previous sibling wrapper, or `null` when this is the first child.
155
+ * @example
156
+ * ```ts
157
+ * const fDocument = univerAPI.getActiveDocument();
158
+ * const fDocumentBody = fDocument.getBody();
159
+ * const element = fDocumentBody.getElement(1);
160
+ * console.log(element?.getPreviousSibling());
161
+ * ```
162
+ */
163
+ getPreviousSibling() {
164
+ return this._createSibling(-1);
165
+ }
166
+ /**
167
+ * Get the sibling element at a relative offset from this element.
168
+ * @param {number} offset The relative offset from this element. Use `1` for the next sibling, `-1` for the previous sibling, and so on.
169
+ * @returns {FDocumentElement | null} The sibling wrapper at the specified offset, or `null` when the offset is out of range.
170
+ * @example
171
+ * ```ts
172
+ * const fDocument = univerAPI.getActiveDocument();
173
+ * const fDocumentBody = fDocument.getBody();
174
+ * const element = fDocumentBody.getElement(0);
175
+ *
176
+ * // Get the third sibling after this element
177
+ * const nextThirdSibling = element?.getSibling(3);
178
+ * console.log(nextThirdSibling?.getType());
179
+ * ```
180
+ */
181
+ getSibling(offset) {
182
+ return this._createSibling(offset);
183
+ }
184
+ /**
185
+ * Remove this element from its parent body.
186
+ * @returns {boolean} `true` if the element content was removed.
187
+ * @example
188
+ * ```ts
189
+ * const fDocument = univerAPI.getActiveDocument();
190
+ * const fDocumentBody = fDocument.getBody();
191
+ * const element = fDocumentBody.getElement(0);
192
+ * const removed = element?.remove();
193
+ * console.log(removed);
194
+ * ```
195
+ */
196
+ remove() {
197
+ if (this.isParagraph()) return this._body.removeParagraph(this.asParagraph());
198
+ if (this.isBlockRange()) return this._body.removeBlockRange(this.asBlockRange());
199
+ if (this.isTable()) return this._body.removeTable(this.asTable());
200
+ return this._body.removeCustomBlock(this.asCustomBlock());
201
+ }
202
+ _createSibling(offset) {
203
+ if (offset === 0) throw new Error("Offset cannot be zero.");
204
+ const index = this._body.getElementIndex(this);
205
+ return this._body.getElement(index + offset);
206
+ }
207
+ };
208
+
209
+ //#endregion
210
+ //#region src/facade/utils.ts
211
+ function cloneParagraphStyle(paragraphStyle) {
212
+ return paragraphStyle == null ? paragraphStyle : JSON.parse(JSON.stringify(paragraphStyle));
213
+ }
214
+ function normalizePlainTextDataStream(dataStream) {
215
+ return dataStream.replace(/\r\n/g, "\r").replace(/\n/g, "\r");
216
+ }
217
+ function getRemovedLeadingParagraphBreakLength(dataStream, removeLeadingParagraphBreak) {
218
+ const normalized = normalizePlainTextDataStream(dataStream);
219
+ if (removeLeadingParagraphBreak && normalized.length > 1 && normalized.startsWith("\r")) return 1;
220
+ return 0;
221
+ }
222
+ function buildPlainTextInsertBody(dataStream, options = {}) {
223
+ const normalizedDataStream = normalizePlainTextDataStream(dataStream).slice(getRemovedLeadingParagraphBreakLength(dataStream, options.removeLeadingParagraphBreak));
224
+ const body = {
225
+ dataStream: normalizedDataStream,
226
+ customDecorations: [],
227
+ customRanges: [],
228
+ textRuns: []
229
+ };
230
+ const paragraphs = [];
231
+ const existingParagraphIds = /* @__PURE__ */ new Set();
232
+ for (let index = 0; index < normalizedDataStream.length; index++) if (normalizedDataStream[index] === "\r") paragraphs.push({
233
+ startIndex: index,
234
+ paragraphId: (0, _univerjs_core.createParagraphId)(existingParagraphIds),
235
+ ...options.paragraphStyle == null ? {} : { paragraphStyle: cloneParagraphStyle(options.paragraphStyle) }
236
+ });
237
+ if (paragraphs.length > 0) body.paragraphs = paragraphs;
238
+ return body;
239
+ }
240
+
241
+ //#endregion
242
+ //#region src/facade/f-document-paragraph.ts
243
+ /**
244
+ * A paragraph facade wrapper.
245
+ *
246
+ * Paragraph identity is backed by the persisted `paragraphId`. The id is
247
+ * re-resolved before each method call, so insertions before this paragraph do
248
+ * not break the wrapper.
249
+ *
250
+ * @hideconstructor
251
+ */
252
+ var FDocumentParagraph = class extends FDocumentElement {
253
+ constructor(body, bodyEdit, info, injector) {
254
+ super(body, bodyEdit, info, injector);
255
+ this.body = body;
256
+ this.bodyEdit = bodyEdit;
257
+ this.info = info;
258
+ this.injector = injector;
259
+ if (this.getType() !== _univerjs_core.DocumentBlockType.PARAGRAPH) throw new Error(`Element type is not a paragraph: ${this.getType()}`);
260
+ }
261
+ /**
262
+ * Get the persisted paragraph id.
263
+ * @returns {string} The paragraph id.
264
+ * @example
265
+ * ```ts
266
+ * const fDocument = univerAPI.getActiveDocument();
267
+ * const fDocumentBody = fDocument.getBody();
268
+ * const element = fDocumentBody.getElement(0);
269
+ *
270
+ * if (element?.isParagraph()) {
271
+ * const paragraph = element.asParagraph();
272
+ * console.log(paragraph.getParagraphId());
273
+ * }
274
+ * ```
275
+ */
276
+ getParagraphId() {
277
+ return this.getKey();
278
+ }
279
+ /**
280
+ * Get the resolved paragraph info for this wrapper.
281
+ * @returns {IFDocumentResolvedParagraph} The resolved paragraph info, including the paragraph object, its index, and its text range.
282
+ * @example
283
+ * ```ts
284
+ * const fDocument = univerAPI.getActiveDocument();
285
+ * const fDocumentBody = fDocument.getBody();
286
+ * const element = fDocumentBody.getElement(0);
287
+ *
288
+ * if (element?.isParagraph()) {
289
+ * const paragraph = element.asParagraph();
290
+ * console.log(paragraph.getResolvedParagraphInfo());
291
+ * }
292
+ * ```
293
+ */
294
+ getResolvedParagraphInfo() {
295
+ const { paragraphs = [] } = this._body.getBody();
296
+ const matches = paragraphs.map((paragraph, paragraphIndex) => ({
297
+ paragraph,
298
+ paragraphIndex
299
+ })).filter(({ paragraph }) => paragraph.paragraphId === this.getKey());
300
+ if (matches.length === 0) throw new Error(`Document paragraph with id ${this.getKey()} not found`);
301
+ if (matches.length > 1) throw new Error(`Multiple document paragraphs with id ${this.getKey()} found`);
302
+ const { paragraph, paragraphIndex } = matches[0];
303
+ return {
304
+ paragraph,
305
+ paragraphIndex,
306
+ startOffset: paragraphIndex > 0 ? paragraphs[paragraphIndex - 1].startIndex + 1 : 0,
307
+ endOffset: paragraph.startIndex
308
+ };
309
+ }
310
+ /**
311
+ * Get the current text range occupied by this paragraph.
312
+ * @returns {IFDocumentTextRange} The paragraph text range, excluding the trailing paragraph break.
313
+ * @example
314
+ * ```ts
315
+ * const fDocument = univerAPI.getActiveDocument();
316
+ * const fDocumentBody = fDocument.getBody();
317
+ * const element = fDocumentBody.getElement(0);
318
+ *
319
+ * if (element?.isParagraph()) {
320
+ * const paragraph = element.asParagraph();
321
+ * const range = paragraph.getRange();
322
+ * fDocumentBody.setTextStyle(range, { bl: 1 });
323
+ * }
324
+ * ```
325
+ */
326
+ getRange() {
327
+ const { startOffset, endOffset } = this.getResolvedParagraphInfo();
328
+ return {
329
+ startOffset,
330
+ endOffset,
331
+ segmentId: this._body.getSegmentId()
332
+ };
333
+ }
334
+ /**
335
+ * Get this paragraph's plain text.
336
+ * @returns {string} The paragraph text without the trailing paragraph break.
337
+ * @example
338
+ * ```ts
339
+ * const fDocument = univerAPI.getActiveDocument();
340
+ * const fDocumentBody = fDocument.getBody();
341
+ * const element = fDocumentBody.getElement(0);
342
+ *
343
+ * if (element?.isParagraph()) {
344
+ * const paragraph = element.asParagraph();
345
+ * console.log(paragraph.getText());
346
+ * }
347
+ * ```
348
+ */
349
+ getText() {
350
+ const { dataStream } = this._body.getBody();
351
+ const { startOffset, endOffset } = this.getResolvedParagraphInfo();
352
+ return dataStream.slice(startOffset, endOffset);
353
+ }
354
+ /**
355
+ * Replace this paragraph's plain text.
356
+ * @param {string} text The replacement text. Do not include the paragraph break.
357
+ * @returns {boolean} `true` if the paragraph text was replaced.
358
+ * @example
359
+ * ```ts
360
+ * const fDocument = univerAPI.getActiveDocument();
361
+ * const fDocumentBody = fDocument.getBody();
362
+ * const element = fDocumentBody.getElement(0);
363
+ *
364
+ * if (element?.isParagraph()) {
365
+ * const paragraph = element.asParagraph();
366
+ * const success = paragraph.setText('Updated title');
367
+ * console.log(success ? 'Text updated' : 'Failed to update text');
368
+ * }
369
+ * ```
370
+ */
371
+ setText(text) {
372
+ const { startOffset, endOffset } = this.getResolvedParagraphInfo();
373
+ return this._bodyEdit.replaceRange({
374
+ startOffset,
375
+ endOffset
376
+ }, buildPlainTextInsertBody(text));
377
+ }
378
+ /**
379
+ * Append plain text before this paragraph's trailing paragraph break.
380
+ * @param {string} text The plain text to append.
381
+ * @returns {boolean} `true` if the text was appended.
382
+ * @example
383
+ * ```ts
384
+ * const fDocument = univerAPI.getActiveDocument();
385
+ * const fDocumentBody = fDocument.getBody();
386
+ * const element = fDocumentBody.getElement(0);
387
+ *
388
+ * if (element?.isParagraph()) {
389
+ * const paragraph = element.asParagraph();
390
+ * const success = paragraph.appendText(' Appended text');
391
+ * console.log(success ? 'Text appended' : 'Failed to append text');
392
+ * }
393
+ * ```
394
+ */
395
+ appendText(text) {
396
+ const { endOffset } = this.getResolvedParagraphInfo();
397
+ return this._body.insertText(endOffset, text);
398
+ }
399
+ /**
400
+ * Apply paragraph style to a paragraph handle or text range.
401
+ * @param {IParagraphStyle} style The Univer paragraph style patch.
402
+ * @returns {boolean} `true` if the style was applied.
403
+ * @example
404
+ * ```ts
405
+ * const fDocument = univerAPI.getActiveDocument();
406
+ * const fDocumentBody = fDocument.getBody();
407
+ * const element = fDocumentBody.getElement(0);
408
+ *
409
+ * if (element?.isParagraph()) {
410
+ * const paragraph = element.asParagraph();
411
+ * paragraph.setStyle({ horizontalAlign: 2 });
412
+ * }
413
+ * ```
414
+ */
415
+ setStyle(style) {
416
+ const { paragraph, endOffset } = this.getResolvedParagraphInfo();
417
+ const updateBody = {
418
+ dataStream: "",
419
+ paragraphs: [{
420
+ ...paragraph,
421
+ startIndex: 0,
422
+ paragraphStyle: {
423
+ ...paragraph.paragraphStyle,
424
+ ...style
425
+ }
426
+ }]
427
+ };
428
+ this._preserveExplicitParagraphIds(updateBody);
429
+ return this._bodyEdit.retainRange({
430
+ startOffset: endOffset,
431
+ endOffset: endOffset + 1
432
+ }, updateBody, _univerjs_core.UpdateDocsAttributeType.REPLACE);
433
+ }
434
+ /**
435
+ * Check whether this paragraph is a bullet, ordered, or checklist item.
436
+ * @returns {boolean} `true` if the paragraph has list metadata.
437
+ * @example
438
+ * ```ts
439
+ * const fDocument = univerAPI.getActiveDocument();
440
+ * const fDocumentBody = fDocument.getBody();
441
+ * const element = fDocumentBody.getElement(0);
442
+ *
443
+ * if (element?.isParagraph()) {
444
+ * const paragraph = element.asParagraph();
445
+ * console.log(paragraph.isListItem() ? 'This is a list item' : 'This is not a list item');
446
+ * }
447
+ * ```
448
+ */
449
+ isListItem() {
450
+ const { paragraph } = this.getResolvedParagraphInfo();
451
+ return Boolean(paragraph.bullet);
452
+ }
453
+ /**
454
+ * Check whether this paragraph is a task/checklist item.
455
+ * @returns {boolean} `true` if this paragraph is an unchecked or checked task item.
456
+ * @example
457
+ * ```ts
458
+ * const fDocument = univerAPI.getActiveDocument();
459
+ * const fDocumentBody = fDocument.getBody();
460
+ * const element = fDocumentBody.getElement(0);
461
+ *
462
+ * if (element?.isParagraph()) {
463
+ * const paragraph = element.asParagraph();
464
+ * console.log(paragraph.isTask() ? 'This is a task item' : 'This is not a task item');
465
+ * }
466
+ * ```
467
+ */
468
+ isTask() {
469
+ var _paragraph$bullet;
470
+ const { paragraph } = this.getResolvedParagraphInfo();
471
+ const listType = (_paragraph$bullet = paragraph.bullet) === null || _paragraph$bullet === void 0 ? void 0 : _paragraph$bullet.listType;
472
+ return listType === _univerjs_core.PresetListType.CHECK_LIST || listType === _univerjs_core.PresetListType.CHECK_LIST_CHECKED;
473
+ }
474
+ /**
475
+ * Set the checked state of this task/checklist paragraph.
476
+ * @param {boolean} checked Whether the task item should be checked.
477
+ * @returns {boolean} `true` if the task state was updated, or `false` if this paragraph is not a task item.
478
+ * @example
479
+ * ```ts
480
+ * const fDocument = univerAPI.getActiveDocument();
481
+ * const fDocumentBody = fDocument.getBody();
482
+ * const element = fDocumentBody.getElement(0);
483
+ *
484
+ * if (element?.isParagraph()) {
485
+ * const paragraph = element.asParagraph();
486
+ *
487
+ * if (paragraph.isTask()) {
488
+ * const success = paragraph.setTaskChecked(true);
489
+ * console.log(success ? 'Task checked' : 'Failed to check task');
490
+ * }
491
+ * }
492
+ * ```
493
+ */
494
+ setTaskChecked(checked) {
495
+ if (!this.isTask()) return false;
496
+ const { paragraph, endOffset } = this.getResolvedParagraphInfo();
497
+ const bullet = paragraph.bullet;
498
+ const updateBody = {
499
+ dataStream: "",
500
+ paragraphs: [{
501
+ ...paragraph,
502
+ startIndex: 0,
503
+ bullet: {
504
+ ...bullet,
505
+ listType: checked ? _univerjs_core.PresetListType.CHECK_LIST_CHECKED : _univerjs_core.PresetListType.CHECK_LIST
506
+ }
507
+ }]
508
+ };
509
+ this._preserveExplicitParagraphIds(updateBody);
510
+ return this._bodyEdit.retainRange({
511
+ startOffset: endOffset,
512
+ endOffset: endOffset + 1
513
+ }, updateBody, _univerjs_core.UpdateDocsAttributeType.REPLACE);
514
+ }
515
+ _preserveExplicitParagraphIds(body) {
516
+ body[_univerjs_core.RESTORE_INSERTED_PARAGRAPH_IDS] = true;
517
+ }
518
+ };
519
+ var FDocumentParagraphMixin = class extends FDocumentElement {
520
+ asParagraph() {
521
+ if (this.getType() !== _univerjs_core.DocumentBlockType.PARAGRAPH) throw new Error(`Element type is not a paragraph: ${this.getType()}`);
522
+ return this._injector.createInstance(FDocumentParagraph, this._body, this._bodyEdit, this.getResolvedInfo(), this._injector);
523
+ }
524
+ };
525
+ FDocumentElement.extend(FDocumentParagraphMixin);
526
+
527
+ //#endregion
528
+ //#region \0@oxc-project+runtime@0.137.0/helpers/esm/typeof.js
529
+ function _typeof(o) {
530
+ "@babel/helpers - typeof";
531
+ return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
532
+ return typeof o;
533
+ } : function(o) {
534
+ return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
535
+ }, _typeof(o);
536
+ }
537
+
538
+ //#endregion
539
+ //#region \0@oxc-project+runtime@0.137.0/helpers/esm/toPrimitive.js
540
+ function toPrimitive(t, r) {
541
+ if ("object" != _typeof(t) || !t) return t;
542
+ var e = t[Symbol.toPrimitive];
543
+ if (void 0 !== e) {
544
+ var i = e.call(t, r || "default");
545
+ if ("object" != _typeof(i)) return i;
546
+ throw new TypeError("@@toPrimitive must return a primitive value.");
547
+ }
548
+ return ("string" === r ? String : Number)(t);
549
+ }
550
+
551
+ //#endregion
552
+ //#region \0@oxc-project+runtime@0.137.0/helpers/esm/toPropertyKey.js
553
+ function toPropertyKey(t) {
554
+ var i = toPrimitive(t, "string");
555
+ return "symbol" == _typeof(i) ? i : i + "";
556
+ }
557
+
558
+ //#endregion
559
+ //#region \0@oxc-project+runtime@0.137.0/helpers/esm/defineProperty.js
560
+ function _defineProperty(e, r, t) {
561
+ return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
562
+ value: t,
563
+ enumerable: !0,
564
+ configurable: !0,
565
+ writable: !0
566
+ }) : e[r] = t, e;
567
+ }
568
+
569
+ //#endregion
570
+ //#region src/facade/f-document-body.ts
571
+ /**
572
+ * A Facade API object bounded to a document body or header/footer segment.
573
+ * It provides Google Docs-like element access and range editing methods.
574
+ *
575
+ * Paragraph elements use their persisted `paragraphId`. Tables, block ranges, and
576
+ * custom blocks use their persisted ids.
577
+ *
578
+ * @hideconstructor
579
+ */
580
+ var FDocumentBody = class {
581
+ constructor(_documentDataModel, _injector, _segmentId = "") {
582
+ this._documentDataModel = _documentDataModel;
583
+ this._injector = _injector;
584
+ this._segmentId = _segmentId;
585
+ _defineProperty(this, "_bodyEdit", void 0);
586
+ this._bodyEdit = {
587
+ replaceRange: this._replaceBodyRange.bind(this),
588
+ retainRange: this._retainBodyRange.bind(this)
589
+ };
590
+ }
591
+ /**
592
+ * Get the segment id of this document body facade.
593
+ * The main body has an empty string segment id.
594
+ * The header and footer FDocumentBody instances have their respective segment ids.
595
+ * @returns {string} The segment id of this document body facade.
596
+ * @example
597
+ * ```ts
598
+ * const fDocument = univerAPI.getActiveDocument();
599
+ * const fDocumentBody = fDocument.getBody();
600
+ * console.log(fDocumentBody.getSegmentId());
601
+ * ```
602
+ */
603
+ getSegmentId() {
604
+ return this._segmentId;
605
+ }
606
+ /**
607
+ * Get the underlying document body snapshot.
608
+ * @returns {IDocumentBody} The document body snapshot.
609
+ * @example
610
+ * ```ts
611
+ * const fDocument = univerAPI.getActiveDocument();
612
+ * const fDocumentBody = fDocument.getBody();
613
+ * console.log(fDocumentBody.getBody());
614
+ * ```
615
+ */
616
+ getBody() {
617
+ const body = this._documentDataModel.getSelfOrHeaderFooterModel(this._segmentId).getBody();
618
+ if (!body) throw new Error("The document body is empty");
619
+ return body;
620
+ }
621
+ /**
622
+ * Get a list of top-level child elements in the body.
623
+ * @returns {FDocumentElement[]} The list of top-level document elements.
624
+ * @example
625
+ * ```ts
626
+ * const fDocument = univerAPI.getActiveDocument();
627
+ * const fDocumentBody = fDocument.getBody();
628
+ * const elements = fDocumentBody.getElements();
629
+ * console.log(elements);
630
+ * ```
631
+ */
632
+ getElements() {
633
+ return this._getChildren().map((child) => {
634
+ return this._injector.createInstance(FDocumentElement, this, this._bodyEdit, child, this._injector);
635
+ });
636
+ }
637
+ /**
638
+ * Get a top-level child element by child index.
639
+ * @param {number} index The zero-based child index.
640
+ * @returns {FDocumentElement} The top-level child element wrapper.
641
+ * @example
642
+ * ```ts
643
+ * const fDocument = univerAPI.getActiveDocument();
644
+ * const fDocumentBody = fDocument.getBody();
645
+ * const element = fDocumentBody.getElement(0);
646
+ * console.log(element);
647
+ * ```
648
+ */
649
+ getElement(index) {
650
+ var _this$getElements$ind;
651
+ return (_this$getElements$ind = this.getElements()[index]) !== null && _this$getElements$ind !== void 0 ? _this$getElements$ind : null;
652
+ }
653
+ /**
654
+ * Get the current child index of an element handle.
655
+ * The index is resolved from the element key, so a paragraph handle keeps pointing
656
+ * to the same paragraph after facade edits insert content before it.
657
+ * @param {FDocumentElement} element The element handle to locate.
658
+ * @returns {number} The current zero-based child index.
659
+ * @example
660
+ * ```ts
661
+ * const fDocument = univerAPI.getActiveDocument();
662
+ * const fDocumentBody = fDocument.getBody();
663
+ * const element = fDocumentBody.getElement(0);
664
+ * console.log(fDocumentBody.getElementIndex(element));
665
+ * ```
666
+ */
667
+ getElementIndex(element) {
668
+ const { type, key } = element.getResolvedInfo();
669
+ const index = this._getChildren().findIndex((child) => child.type === type && child.key === key);
670
+ if (index < 0) throw new Error("Doc element is stale");
671
+ return index;
672
+ }
673
+ /**
674
+ * Insert plain text at a document body offset.
675
+ * @param {number} index The zero-based insertion offset.
676
+ * @param {string} text The plain text to insert.
677
+ * @returns {boolean} `true` if the edit was applied.
678
+ * @example
679
+ * ```ts
680
+ * const fDocument = univerAPI.getActiveDocument();
681
+ * const fDocumentBody = fDocument.getBody();
682
+ * fDocumentBody.insertText(0, 'Hello ');
683
+ * ```
684
+ */
685
+ insertText(index, text) {
686
+ return this._replaceBodyRange({
687
+ startOffset: index,
688
+ endOffset: index
689
+ }, buildPlainTextInsertBody(text));
690
+ }
691
+ /**
692
+ * Apply text style to a body range.
693
+ * @param {IFDocumentTextRange} range The range to style.
694
+ * @param {ITextStyle} style The Univer text style patch.
695
+ * @returns {boolean} `true` if the style was applied.
696
+ * @example
697
+ * ```ts
698
+ * const fDocument = univerAPI.getActiveDocument();
699
+ * const fDocumentBody = fDocument.getBody();
700
+ * fDocumentBody.setTextStyle({ startOffset: 0, endOffset: 5 }, { bl: 1 });
701
+ * ```
702
+ */
703
+ setTextStyle(range, style) {
704
+ const updateBody = {
705
+ dataStream: "",
706
+ textRuns: [{
707
+ st: 0,
708
+ ed: range.endOffset - range.startOffset,
709
+ ts: style
710
+ }]
711
+ };
712
+ return this._retainBodyRange(range, updateBody, _univerjs_core.UpdateDocsAttributeType.COVER);
713
+ }
714
+ /**
715
+ * Insert a plain-text paragraph before the paragraph at the given paragraph index.
716
+ * @param {number} index The zero-based paragraph insertion index.
717
+ * @param {string} text The paragraph text. Defaults to an empty paragraph.
718
+ * @returns {FDocumentParagraph} The inserted paragraph wrapper.
719
+ * @example
720
+ * ```ts
721
+ * const fDocument = univerAPI.getActiveDocument();
722
+ * const fDocumentBody = fDocument.getBody();
723
+ * const paragraph = fDocumentBody.insertParagraph(0, 'Document title');
724
+ * paragraph.appendText(' suffix');
725
+ * ```
726
+ */
727
+ insertParagraph(index, text = "") {
728
+ var _paragraphs;
729
+ const offset = this._getParagraphInsertOffset(index);
730
+ if (!this._replaceBodyRange({
731
+ startOffset: offset,
732
+ endOffset: offset
733
+ }, buildPlainTextInsertBody(`${text}\r`))) throw new Error("Failed to insert paragraph.");
734
+ const { paragraphs = [] } = this.getBody();
735
+ const paragraph = paragraphs[index];
736
+ if (!paragraph) throw new Error("Failed to insert paragraph.");
737
+ const info = this._resolveParagraphInfo(paragraph, index, (_paragraphs = paragraphs[index - 1]) === null || _paragraphs === void 0 ? void 0 : _paragraphs.startIndex);
738
+ return this._injector.createInstance(FDocumentParagraph, this, this._bodyEdit, info, this._injector);
739
+ }
740
+ /**
741
+ * Append a plain-text paragraph at the end of the body.
742
+ * @param {string} text The paragraph text. Defaults to an empty paragraph.
743
+ * @returns {FDocumentParagraph} The appended paragraph wrapper.
744
+ * @example
745
+ * ```ts
746
+ * const fDocument = univerAPI.getActiveDocument();
747
+ * const fDocumentBody = fDocument.getBody();
748
+ * const paragraph = fDocumentBody.appendParagraph('Summary');
749
+ * console.log(paragraph.getText());
750
+ * ```
751
+ */
752
+ appendParagraph(text = "") {
753
+ const { paragraphs = [] } = this.getBody();
754
+ return this.insertParagraph(paragraphs.length, text);
755
+ }
756
+ /**
757
+ * Delete a range from the body.
758
+ * @param {IFDocumentTextRange} range The text range to delete.
759
+ * @returns {boolean} `true` if the range was deleted.
760
+ * @example
761
+ * ```ts
762
+ * const fDocument = univerAPI.getActiveDocument();
763
+ * const fDocumentBody = fDocument.getBody();
764
+ * fDocumentBody.deleteRange({ startOffset: 0, endOffset: 5 });
765
+ * ```
766
+ */
767
+ deleteRange(range) {
768
+ return this._replaceBodyRange(range, { dataStream: "" });
769
+ }
770
+ /**
771
+ * Remove a paragraph by paragraph id.
772
+ * @param {FDocumentParagraph} paragraph The paragraph handle to remove.
773
+ * @returns {boolean} `true` if the paragraph was removed.
774
+ * @example
775
+ * ```ts
776
+ * const fDocument = univerAPI.getActiveDocument();
777
+ * const fDocumentBody = fDocument.getBody();
778
+ * const element = fDocumentBody.getElement(0);
779
+ *
780
+ * if (element?.isParagraph()) {
781
+ * const paragraph = element.asParagraph();
782
+ * const removed = fDocumentBody.removeParagraph(paragraph);
783
+ * console.log(removed ? 'Paragraph removed' : 'Failed to remove paragraph');
784
+ * }
785
+ * ```
786
+ */
787
+ removeParagraph(paragraph) {
788
+ const { startOffset, endOffset } = paragraph.getResolvedParagraphInfo();
789
+ return this.deleteRange({
790
+ startOffset,
791
+ endOffset: endOffset + 1
792
+ });
793
+ }
794
+ /**
795
+ * Remove a callout, quote, or code block range and its content.
796
+ * @param {FDocumentBlockRange} blockRange The block range handle to remove.
797
+ * @returns {boolean} `true` if the block range content was removed.
798
+ * @example
799
+ * ```ts
800
+ * const fDocument = univerAPI.getActiveDocument();
801
+ * const fDocumentBody = fDocument.getBody();
802
+ * const element = fDocumentBody.getElement(0);
803
+ *
804
+ * if (element?.isBlockRange()) {
805
+ * const blockRange = element.asBlockRange();
806
+ * const removed = fDocumentBody.removeBlockRange(blockRange);
807
+ * console.log(removed ? 'Block range removed' : 'Failed to remove block range');
808
+ * }
809
+ * ```
810
+ */
811
+ removeBlockRange(blockRange) {
812
+ const { startIndex, endIndex } = blockRange.getBlockRange();
813
+ return this.deleteRange({
814
+ startOffset: startIndex,
815
+ endOffset: endIndex + 1
816
+ });
817
+ }
818
+ /**
819
+ * Remove a table marker and its content range.
820
+ * @returns {boolean} `true` if the table range was removed.
821
+ * @example
822
+ * ```ts
823
+ * const fDocument = univerAPI.getActiveDocument();
824
+ * const fDocumentBody = fDocument.getBody();
825
+ * const element = fDocumentBody.getElement(0);
826
+ *
827
+ * if (element?.isTable()) {
828
+ * const table = element.asTable();
829
+ * const removed = fDocumentBody.removeTable(table);
830
+ * console.log(removed ? 'Table removed' : 'Failed to remove table');
831
+ * }
832
+ * ```
833
+ */
834
+ removeTable(table) {
835
+ const { startIndex, endIndex } = table.getTable();
836
+ return this.deleteRange({
837
+ startOffset: startIndex,
838
+ endOffset: endIndex + 1
839
+ });
840
+ }
841
+ /**
842
+ * Remove a custom block marker and its placeholder character.
843
+ * @param {FDocumentCustomBlock} customBlock The custom block handle to remove.
844
+ * @returns {boolean} `true` if the custom block placeholder was removed.
845
+ * @example
846
+ * ```ts
847
+ * const fDocument = univerAPI.getActiveDocument();
848
+ * const fDocumentBody = fDocument.getBody();
849
+ * const element = fDocumentBody.getElement(0);
850
+ *
851
+ * if (element?.isCustomBlock()) {
852
+ * const customBlock = element.asCustomBlock();
853
+ * const removed = fDocumentBody.removeCustomBlock(customBlock);
854
+ * console.log(removed ? 'Custom block removed' : 'Failed to remove custom block');
855
+ * }
856
+ * ```
857
+ */
858
+ removeCustomBlock(customBlock) {
859
+ const { startIndex } = customBlock.getCustomBlock();
860
+ return this.deleteRange({
861
+ startOffset: startIndex,
862
+ endOffset: startIndex + 1
863
+ });
864
+ }
865
+ /**
866
+ * Resolve an element key to its current child metadata.
867
+ * @param {FDocumentElement} element The element handle to resolve.
868
+ * @returns {IFDocumentElementInfo} The current child metadata used by the facade.
869
+ * @example
870
+ * ```ts
871
+ * const fDocument = univerAPI.getActiveDocument();
872
+ * const fDocumentBody = fDocument.getBody();
873
+ * const element = fDocumentBody.getElement(0);
874
+ * const resolved = fDocumentBody.resolveElement(element);
875
+ * console.log(resolved);
876
+ * ```
877
+ */
878
+ resolveElement(element) {
879
+ const { type, key } = element.getResolvedInfo();
880
+ const child = this._getChildren().find((item) => item.type === type && item.key === key);
881
+ if (!child) throw new Error("Doc element is stale");
882
+ return child;
883
+ }
884
+ _getChildren() {
885
+ const { paragraphs, blockRanges, tables, customBlocks } = this.getBody();
886
+ const children = [];
887
+ if (paragraphs) for (let i = 0; i < paragraphs.length; i++) {
888
+ var _paragraphs2;
889
+ const paragraph = paragraphs[i];
890
+ const info = this._resolveParagraphInfo(paragraph, i, (_paragraphs2 = paragraphs[i - 1]) === null || _paragraphs2 === void 0 ? void 0 : _paragraphs2.startIndex);
891
+ children.push(info);
892
+ }
893
+ if (blockRanges) for (let i = 0; i < blockRanges.length; i++) {
894
+ const blockRange = blockRanges[i];
895
+ children.push({
896
+ type: _univerjs_core.DocumentBlockType.BLOCK_RANGE,
897
+ key: blockRange.blockId,
898
+ position: blockRange.startIndex,
899
+ priority: 0
900
+ });
901
+ }
902
+ if (tables) for (let i = 0; i < tables.length; i++) {
903
+ const table = tables[i];
904
+ children.push({
905
+ type: _univerjs_core.DocumentBlockType.TABLE,
906
+ key: table.tableId,
907
+ position: table.startIndex,
908
+ priority: 1
909
+ });
910
+ }
911
+ if (customBlocks) for (let i = 0; i < customBlocks.length; i++) {
912
+ const customBlock = customBlocks[i];
913
+ children.push({
914
+ type: _univerjs_core.DocumentBlockType.CUSTOM_BLOCK,
915
+ key: customBlock.blockId,
916
+ position: customBlock.startIndex,
917
+ priority: 2
918
+ });
919
+ }
920
+ return children.sort((a, b) => a.position - b.position || a.priority - b.priority);
921
+ }
922
+ _resolveParagraphInfo(paragraph, paragraphIndex, previousParagraphStartIndex) {
923
+ return {
924
+ type: _univerjs_core.DocumentBlockType.PARAGRAPH,
925
+ key: this._getParagraphId(paragraph, paragraphIndex),
926
+ position: paragraphIndex > 0 ? previousParagraphStartIndex + 1 : 0,
927
+ priority: 3
928
+ };
929
+ }
930
+ _getParagraphId(paragraph, paragraphIndex) {
931
+ if (!paragraph) throw new Error(`Paragraph index ${paragraphIndex} is out of range.`);
932
+ if (!paragraph.paragraphId) throw new Error(`Paragraph at index ${paragraphIndex} is missing paragraphId.`);
933
+ return paragraph.paragraphId;
934
+ }
935
+ _getParagraphInsertOffset(index) {
936
+ if (index <= 0) return 0;
937
+ const { dataStream, paragraphs = [] } = this.getBody();
938
+ if (paragraphs.length === 0) return Math.max(0, dataStream.length - 1);
939
+ if (index >= paragraphs.length) return paragraphs[paragraphs.length - 1].startIndex + 1;
940
+ return paragraphs[index - 1].startIndex + 1;
941
+ }
942
+ _replaceBodyRange(range, insertBody) {
943
+ const { startOffset, endOffset } = range;
944
+ const textX = new _univerjs_core.TextX();
945
+ if (startOffset > 0) textX.push({
946
+ t: _univerjs_core.TextXActionType.RETAIN,
947
+ len: startOffset
948
+ });
949
+ if (endOffset > startOffset) textX.push({
950
+ t: _univerjs_core.TextXActionType.DELETE,
951
+ len: endOffset - startOffset
952
+ });
953
+ if (insertBody.dataStream.length > 0) textX.push({
954
+ t: _univerjs_core.TextXActionType.INSERT,
955
+ body: insertBody,
956
+ len: insertBody.dataStream.length
957
+ });
958
+ return this._executeTextX(textX);
959
+ }
960
+ _retainBodyRange(range, body, coverType) {
961
+ var _body$textRuns;
962
+ if (((_body$textRuns = body.textRuns) === null || _body$textRuns === void 0 ? void 0 : _body$textRuns.length) && this.getBody().textRuns == null) this._ensureTextRuns();
963
+ const textX = new _univerjs_core.TextX();
964
+ if (range.startOffset > 0) textX.push({
965
+ t: _univerjs_core.TextXActionType.RETAIN,
966
+ len: range.startOffset
967
+ });
968
+ textX.push({
969
+ t: _univerjs_core.TextXActionType.RETAIN,
970
+ body,
971
+ coverType,
972
+ len: range.endOffset - range.startOffset
973
+ });
974
+ return this._executeTextX(textX);
975
+ }
976
+ _ensureTextRuns() {
977
+ const actions = _univerjs_core.JSONX.getInstance().replaceOp([...(0, _univerjs_core.getRichTextEditPath)(this._documentDataModel, this._segmentId), "textRuns"], void 0, []);
978
+ this._injector.get(_univerjs_core.ICommandService).syncExecuteCommand(_univerjs_docs.RichTextEditingMutation.id, {
979
+ unitId: this._documentDataModel.getUnitId(),
980
+ segmentId: this._segmentId,
981
+ actions,
982
+ textRanges: [],
983
+ isEditing: false
984
+ });
985
+ }
986
+ _executeTextX(textX) {
987
+ const actions = _univerjs_core.JSONX.getInstance().editOp(textX.serialize(), (0, _univerjs_core.getRichTextEditPath)(this._documentDataModel, this._segmentId));
988
+ return this._injector.get(_univerjs_core.ICommandService).syncExecuteCommand(_univerjs_docs.RichTextEditingMutation.id, {
989
+ unitId: this._documentDataModel.getUnitId(),
990
+ segmentId: this._segmentId,
991
+ actions,
992
+ textRanges: [],
993
+ isEditing: false
994
+ }) !== false;
995
+ }
996
+ };
997
+
998
+ //#endregion
999
+ //#region \0@oxc-project+runtime@0.137.0/helpers/esm/decorateParam.js
1000
+ function __decorateParam(paramIndex, decorator) {
1001
+ return function(target, key) {
1002
+ decorator(target, key, paramIndex);
1003
+ };
1004
+ }
1005
+
1006
+ //#endregion
1007
+ //#region \0@oxc-project+runtime@0.137.0/helpers/esm/decorate.js
1008
+ function __decorate(decorators, target, key, desc) {
1009
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1010
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
1011
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1012
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
1013
+ }
1014
+
1015
+ //#endregion
1016
+ //#region src/facade/f-document.ts
1017
+ let FDocument = class FDocument extends _univerjs_core_facade.FBaseInitialable {
1018
+ constructor(_documentDataModel, _injector, _univerInstanceService, _resourceLoaderService, _commandService) {
1019
+ super(_injector);
1020
+ this._documentDataModel = _documentDataModel;
1021
+ this._injector = _injector;
1022
+ this._univerInstanceService = _univerInstanceService;
1023
+ this._resourceLoaderService = _resourceLoaderService;
1024
+ this._commandService = _commandService;
1025
+ _defineProperty(this, "id", void 0);
1026
+ this.id = this._documentDataModel.getUnitId();
1027
+ }
1028
+ /**
1029
+ * Get the document data model of the document.
1030
+ * @returns {DocumentDataModel} The document data model.
1031
+ * @example
1032
+ * ```typescript
1033
+ * const fDocument = univerAPI.getActiveDocument();
1034
+ * const documentDataModel = fDocument.getDocumentDataModel();
1035
+ * console.log(documentDataModel);
1036
+ * ```
1037
+ */
1038
+ getDocumentDataModel() {
1039
+ return this._documentDataModel;
1040
+ }
1041
+ /**
1042
+ * Get the document body facade.
1043
+ *
1044
+ * The returned body facade provides synchronous Google Docs-like element APIs
1045
+ * for reading and editing top-level document body elements. Paragraph elements
1046
+ * use their persisted `paragraphId` values. Persisted elements, such as tables
1047
+ * and custom blocks, use their existing ids.
1048
+ *
1049
+ * @returns {FDocumentBody} The document body API instance.
1050
+ * @example
1051
+ * ```typescript
1052
+ * const fDocument = univerAPI.getActiveDocument();
1053
+ * const fDocumentBody = fDocument.getBody();
1054
+ * console.log(fDocumentBody.getBody());
1055
+ *
1056
+ * const element = fDocumentBody.getElement(0);
1057
+ * if (element.isParagraph()) {
1058
+ * const paragraph = element.asParagraph();
1059
+ * paragraph.appendText(' updated');
1060
+ * console.log(paragraph.getText());
1061
+ * }
1062
+ * ```
1063
+ */
1064
+ getBody() {
1065
+ return this._injector.createInstance(FDocumentBody, this._documentDataModel, this._injector);
1066
+ }
1067
+ dispose() {
1068
+ super.dispose();
1069
+ }
1070
+ /**
1071
+ * Get the document id.
1072
+ * @returns {string} The document id.
1073
+ * @example
1074
+ * ```typescript
1075
+ * const fDocument = univerAPI.getActiveDocument();
1076
+ * const unitId = fDocument.getId();
1077
+ * console.log(unitId);
1078
+ * ```
1079
+ */
1080
+ getId() {
1081
+ return this.id;
1082
+ }
1083
+ /**
1084
+ * Get the document name.
1085
+ * @returns {string} The document name.
1086
+ * @example
1087
+ * ```typescript
1088
+ * const fDocument = univerAPI.getActiveDocument();
1089
+ * const name = fDocument.getName();
1090
+ * console.log(name);
1091
+ * ```
1092
+ */
1093
+ getName() {
1094
+ return this._documentDataModel.getTitle() || "";
1095
+ }
1096
+ /**
1097
+ * Save the document snapshot data, including the document content and resource data, etc.
1098
+ * @returns {IDocumentData} The document snapshot data.
1099
+ * @example
1100
+ * ```typescript
1101
+ * const fDocument = univerAPI.getActiveDocument();
1102
+ * const snapshot = fDocument.save();
1103
+ * console.log(snapshot);
1104
+ * ```
1105
+ */
1106
+ save() {
1107
+ return this._resourceLoaderService.saveUnit(this._documentDataModel.getUnitId());
1108
+ }
1109
+ /**
1110
+ * Undo the last operation in the document.
1111
+ * @returns {boolean} `true` if the undo operation was successful, or `false` if it failed.
1112
+ * @example
1113
+ * ```typescript
1114
+ * const fDocument = univerAPI.getActiveDocument();
1115
+ * const success = fDocument.undo();
1116
+ * console.log(success);
1117
+ * ```
1118
+ */
1119
+ undo() {
1120
+ this._univerInstanceService.focusUnit(this.id);
1121
+ return this._commandService.syncExecuteCommand(_univerjs_core.UndoCommand.id);
1122
+ }
1123
+ /**
1124
+ * Redo the last undone operation in the document.
1125
+ * @returns {boolean} `true` if the redo operation was successful, or `false` if it failed.
1126
+ * @example
1127
+ * ```typescript
1128
+ * const fDocument = univerAPI.getActiveDocument();
1129
+ * const success = fDocument.redo();
1130
+ * console.log(success);
1131
+ * ```
1132
+ */
1133
+ redo() {
1134
+ this._univerInstanceService.focusUnit(this.id);
1135
+ return this._commandService.syncExecuteCommand(_univerjs_core.RedoCommand.id);
1136
+ }
1137
+ };
1138
+ FDocument = __decorate([
1139
+ __decorateParam(1, (0, _univerjs_core.Inject)(_univerjs_core.Injector)),
1140
+ __decorateParam(2, _univerjs_core.IUniverInstanceService),
1141
+ __decorateParam(3, (0, _univerjs_core.Inject)(_univerjs_core.IResourceLoaderService)),
1142
+ __decorateParam(4, _univerjs_core.ICommandService)
1143
+ ], FDocument);
1144
+
1145
+ //#endregion
1146
+ //#region src/facade/f-univer.ts
1147
+ var FUniverDocsMixin = class extends _univerjs_core_facade.FUniver {
1148
+ createDocument(data) {
1149
+ const document = this._injector.get(_univerjs_core.IUniverInstanceService).createUnit(_univerjs_core.UniverInstanceType.UNIVER_DOC, data);
1150
+ return this._injector.createInstance(FDocument, document);
1151
+ }
1152
+ getActiveDocument() {
1153
+ const document = this._univerInstanceService.getCurrentUnitOfType(_univerjs_core.UniverInstanceType.UNIVER_DOC);
1154
+ if (!document) return null;
1155
+ return this._injector.createInstance(FDocument, document);
1156
+ }
1157
+ getDocument(id) {
1158
+ const document = this._univerInstanceService.getUnit(id, _univerjs_core.UniverInstanceType.UNIVER_DOC);
1159
+ if (!document) return null;
1160
+ return this._injector.createInstance(FDocument, document);
1161
+ }
1162
+ };
1163
+ _univerjs_core_facade.FUniver.extend(FUniverDocsMixin);
1164
+
1165
+ //#endregion
1166
+ //#region src/facade/f-document-block-range.ts
1167
+ /**
1168
+ * A facade wrapper for document block ranges, such as callout, quote, and code blocks.
1169
+ *
1170
+ * Block range identity is backed by the persisted `IDocumentBlockRange.blockId`,
1171
+ * so wrappers can be re-resolved after text is inserted before the block range.
1172
+ *
1173
+ * @hideconstructor
1174
+ */
1175
+ var FDocumentBlockRange = class extends FDocumentElement {
1176
+ constructor(body, bodyEdit, info, injector) {
1177
+ super(body, bodyEdit, info, injector);
1178
+ this.body = body;
1179
+ this.bodyEdit = bodyEdit;
1180
+ this.info = info;
1181
+ this.injector = injector;
1182
+ if (this.getType() !== _univerjs_core.DocumentBlockType.BLOCK_RANGE) throw new Error(`Element type is not a block range: ${this.getType()}`);
1183
+ }
1184
+ /**
1185
+ * Get the top-level block range data model.
1186
+ * @returns {IDocumentBlockRange} The block range data model.
1187
+ * @example
1188
+ * ```ts
1189
+ * const fDocument = univerAPI.getActiveDocument();
1190
+ * const fDocumentBody = fDocument.getBody();
1191
+ * const element = fDocumentBody.getElement(0);
1192
+ *
1193
+ * if (element?.isBlockRange()) {
1194
+ * const blockRange = element.asBlockRange();
1195
+ * console.log(blockRange.getBlockRange());
1196
+ * }
1197
+ * ```
1198
+ */
1199
+ getBlockRange() {
1200
+ const { blockRanges = [] } = this._body.getBody();
1201
+ const blockRange = blockRanges.find((blockRange) => blockRange.blockId === this.getKey());
1202
+ if (!blockRange) throw new Error(`Block range not found: ${this.getKey()}`);
1203
+ return blockRange;
1204
+ }
1205
+ /**
1206
+ * Get the block range type.
1207
+ * @returns {DocumentBlockRangeType} The block type, such as callout, quote, or code.
1208
+ * @example
1209
+ * ```ts
1210
+ * const fDocument = univerAPI.getActiveDocument();
1211
+ * const fDocumentBody = fDocument.getBody();
1212
+ * const element = fDocumentBody.getElement(0);
1213
+ *
1214
+ * if (element?.isBlockRange()) {
1215
+ * const blockRange = element.asBlockRange();
1216
+ * console.log(blockRange.getBlockType());
1217
+ * }
1218
+ * ```
1219
+ */
1220
+ getBlockType() {
1221
+ return this.getBlockRange().blockType;
1222
+ }
1223
+ /**
1224
+ * Get the plain text inside this block range.
1225
+ * @returns {string} The block range text.
1226
+ * @example
1227
+ * ```ts
1228
+ * const fDocument = univerAPI.getActiveDocument();
1229
+ * const fDocumentBody = fDocument.getBody();
1230
+ * const element = fDocumentBody.getElement(0);
1231
+ *
1232
+ * if (element?.isBlockRange()) {
1233
+ * const blockRange = element.asBlockRange();
1234
+ * console.log(blockRange.getText());
1235
+ * }
1236
+ * ```
1237
+ */
1238
+ getText() {
1239
+ const { dataStream } = this._body.getBody();
1240
+ const { startIndex, endIndex } = this.getBlockRange();
1241
+ return dataStream.slice(startIndex, endIndex);
1242
+ }
1243
+ /**
1244
+ * Replace the plain text inside this block range.
1245
+ * @param {string} text The replacement text.
1246
+ * @returns {boolean} `true` if the block range text was replaced.
1247
+ * @example
1248
+ * ```ts
1249
+ * const fDocument = univerAPI.getActiveDocument();
1250
+ * const fDocumentBody = fDocument.getBody();
1251
+ * const element = fDocumentBody.getElement(0);
1252
+ *
1253
+ * if (element?.isBlockRange()) {
1254
+ * const blockRange = element.asBlockRange();
1255
+ * blockRange.setText('Updated block text');
1256
+ * console.log(blockRange.getText());
1257
+ * }
1258
+ * ```
1259
+ */
1260
+ setText(text) {
1261
+ const blockRange = this.getBlockRange();
1262
+ const { startIndex, endIndex } = blockRange;
1263
+ const updateBody = buildPlainTextInsertBody(`${text}\r`);
1264
+ updateBody.blockRanges = [{
1265
+ ...blockRange,
1266
+ startIndex: 0,
1267
+ endIndex: text.length
1268
+ }];
1269
+ return this._bodyEdit.replaceRange({
1270
+ startOffset: startIndex,
1271
+ endOffset: endIndex + 1
1272
+ }, updateBody);
1273
+ }
1274
+ /**
1275
+ * Remove this block range wrapper from the body.
1276
+ *
1277
+ * This currently removes the block range and its content, matching
1278
+ * `remove()`.
1279
+ *
1280
+ * @returns {boolean} `true` if the block range content was removed.
1281
+ * @example
1282
+ * ```ts
1283
+ * const fDocument = univerAPI.getActiveDocument();
1284
+ * const fDocumentBody = fDocument.getBody();
1285
+ * const element = fDocumentBody.getElement(0);
1286
+ *
1287
+ * if (element?.isBlockRange()) {
1288
+ * const blockRange = element.asBlockRange();
1289
+ * const removed = blockRange.unwrap();
1290
+ * console.log(removed ? 'Block range removed' : 'Failed to remove block range');
1291
+ * }
1292
+ * ```
1293
+ */
1294
+ unwrap() {
1295
+ return this.remove();
1296
+ }
1297
+ };
1298
+ var FDocumentBlockRangeMixin = class extends FDocumentElement {
1299
+ asBlockRange() {
1300
+ if (this.getType() !== _univerjs_core.DocumentBlockType.BLOCK_RANGE) throw new Error(`Element type is not a block range: ${this.getType()}`);
1301
+ return this._injector.createInstance(FDocumentBlockRange, this._body, this._bodyEdit, this.getResolvedInfo(), this._injector);
1302
+ }
1303
+ };
1304
+ FDocumentElement.extend(FDocumentBlockRangeMixin);
1305
+
1306
+ //#endregion
1307
+ //#region src/facade/f-document-custom-block.ts
1308
+ /**
1309
+ * A facade wrapper for document top-level custom blocks.
1310
+ * @hideconstructor
1311
+ */
1312
+ var FDocumentCustomBlock = class extends FDocumentElement {
1313
+ constructor(body, bodyEdit, info, injector) {
1314
+ super(body, bodyEdit, info, injector);
1315
+ this.body = body;
1316
+ this.bodyEdit = bodyEdit;
1317
+ this.info = info;
1318
+ this.injector = injector;
1319
+ if (this.getType() !== _univerjs_core.DocumentBlockType.CUSTOM_BLOCK) throw new Error(`Element type is not a custom block: ${this.getType()}`);
1320
+ }
1321
+ /**
1322
+ * Get the custom block marker.
1323
+ * @returns {ICustomBlock} The custom block marker.
1324
+ * @example
1325
+ * ```ts
1326
+ * const fDocument = univerAPI.getActiveDocument();
1327
+ * const fDocumentBody = fDocument.getBody();
1328
+ * const element = fDocumentBody.getElement(0);
1329
+ *
1330
+ * if (element?.isCustomBlock()) {
1331
+ * const customBlock = element.asCustomBlock();
1332
+ * console.log(customBlock.getCustomBlock());
1333
+ * }
1334
+ * ```
1335
+ */
1336
+ getCustomBlock() {
1337
+ const { customBlocks = [] } = this._body.getBody();
1338
+ const block = customBlocks.find((item) => item.blockId === this.getKey());
1339
+ if (!block) throw new Error("Doc custom block is stale");
1340
+ return block;
1341
+ }
1342
+ };
1343
+ var FDocumentCustomBlockMixin = class extends FDocumentElement {
1344
+ asCustomBlock() {
1345
+ if (this.getType() !== _univerjs_core.DocumentBlockType.CUSTOM_BLOCK) throw new Error(`Element type is not a custom block: ${this.getType()}`);
1346
+ return this._injector.createInstance(FDocumentCustomBlock, this._body, this._bodyEdit, this.getResolvedInfo(), this._injector);
1347
+ }
1348
+ };
1349
+ FDocumentElement.extend(FDocumentCustomBlockMixin);
1350
+
1351
+ //#endregion
1352
+ //#region src/facade/f-document-table.ts
1353
+ /**
1354
+ * A facade wrapper for document top-level tables.
1355
+ * @hideconstructor
1356
+ */
1357
+ var FDocumentTable = class extends FDocumentElement {
1358
+ constructor(body, bodyEdit, info, injector) {
1359
+ super(body, bodyEdit, info, injector);
1360
+ this.body = body;
1361
+ this.bodyEdit = bodyEdit;
1362
+ this.info = info;
1363
+ this.injector = injector;
1364
+ if (this.getType() !== _univerjs_core.DocumentBlockType.TABLE) throw new Error(`Element type is not a table: ${this.getType()}`);
1365
+ }
1366
+ /**
1367
+ * Get the table marker.
1368
+ * @returns {ICustomTable} The table marker.
1369
+ * @example
1370
+ * ```ts
1371
+ * const fDocument = univerAPI.getActiveDocument();
1372
+ * const fDocumentBody = fDocument.getBody();
1373
+ * const element = fDocumentBody.getElement(0);
1374
+ *
1375
+ * if (element?.isTable()) {
1376
+ * const table = element.asTable();
1377
+ * console.log(table.getTable());
1378
+ * }
1379
+ * ```
1380
+ */
1381
+ getTable() {
1382
+ const { tables = [] } = this._body.getBody();
1383
+ const table = tables.find((item) => item.tableId === this.getKey());
1384
+ if (!table) throw new Error("Doc table is stale");
1385
+ return table;
1386
+ }
1387
+ };
1388
+ var FDocumentTableMixin = class extends FDocumentElement {
1389
+ asTable() {
1390
+ if (this.getType() !== _univerjs_core.DocumentBlockType.TABLE) throw new Error(`Element type is not a table: ${this.getType()}`);
1391
+ return this._injector.createInstance(FDocumentTable, this._body, this._bodyEdit, this.getResolvedInfo(), this._injector);
1392
+ }
1393
+ };
1394
+ FDocumentElement.extend(FDocumentTableMixin);
1395
+
1396
+ //#endregion
1397
+ Object.defineProperty(exports, 'FDocument', {
1398
+ enumerable: true,
1399
+ get: function () {
1400
+ return FDocument;
1401
+ }
1402
+ });
1403
+ exports.FDocumentBlockRange = FDocumentBlockRange;
1404
+ exports.FDocumentBody = FDocumentBody;
1405
+ exports.FDocumentCustomBlock = FDocumentCustomBlock;
1406
+ exports.FDocumentElement = FDocumentElement;
1407
+ exports.FDocumentParagraph = FDocumentParagraph;
1408
+ exports.FDocumentTable = FDocumentTable;