@univerjs/docs 1.0.0-alpha.8 → 1.0.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/es/facade.js CHANGED
@@ -156,14 +156,11 @@ var FDocumentTextRange = class extends FBaseInitialable {
156
156
  */
157
157
  getExplicitTextStyleRuns() {
158
158
  const { textRuns = [] } = this._document.getBody(this._segmentId);
159
- return textRuns.filter((run) => run.st < this._endOffset && run.ed > this._startOffset).map((run) => {
160
- var _run$ts;
161
- return {
162
- startOffset: Math.max(run.st, this._startOffset),
163
- endOffset: Math.min(run.ed, this._endOffset),
164
- textStyle: Tools.deepClone((_run$ts = run.ts) !== null && _run$ts !== void 0 ? _run$ts : {})
165
- };
166
- });
159
+ return textRuns.filter((run) => run.st < this._endOffset && run.ed > this._startOffset).map((run) => ({
160
+ startOffset: Math.max(run.st, this._startOffset),
161
+ endOffset: Math.min(run.ed, this._endOffset),
162
+ textStyle: Tools.deepClone(run.ts ?? {})
163
+ }));
167
164
  }
168
165
  /**
169
166
  * Returns top-level style properties that have the same explicit value
@@ -407,10 +404,9 @@ let FDocumentParagraph = class FDocumentParagraph extends FBaseInitialable {
407
404
  * ```
408
405
  */
409
406
  findText(text, options = {}) {
410
- var _options$occurrence, _this$findAllText$occ;
411
- const occurrence = (_options$occurrence = options.occurrence) !== null && _options$occurrence !== void 0 ? _options$occurrence : 0;
407
+ const occurrence = options.occurrence ?? 0;
412
408
  if (!Number.isInteger(occurrence) || occurrence < 0) throw new RangeError("Text occurrence must be a non-negative integer.");
413
- return (_this$findAllText$occ = this.findAllText(text, options)[occurrence]) !== null && _this$findAllText$occ !== void 0 ? _this$findAllText$occ : null;
409
+ return this.findAllText(text, options)[occurrence] ?? null;
414
410
  }
415
411
  /**
416
412
  * Finds every non-overlapping literal text occurrence inside this paragraph.
@@ -436,9 +432,8 @@ let FDocumentParagraph = class FDocumentParagraph extends FBaseInitialable {
436
432
  * ```
437
433
  */
438
434
  findAllText(text, options = {}) {
439
- var _options$matchCase;
440
435
  if (text.length === 0) throw new TypeError("Text to find must not be empty.");
441
- const matchCase = (_options$matchCase = options.matchCase) !== null && _options$matchCase !== void 0 ? _options$matchCase : true;
436
+ const matchCase = options.matchCase ?? true;
442
437
  const paragraphText = this.getText();
443
438
  const matcher = regexp.createLiteralRegExp(text, matchCase ? "gu" : "giu");
444
439
  const { startOffset } = this.getInfo();
@@ -738,8 +733,7 @@ let FDocumentSection = class FDocumentSection {
738
733
  * ```
739
734
  */
740
735
  getColumns() {
741
- var _this$_getConfigSnaps;
742
- return Tools.deepClone((_this$_getConfigSnaps = this._getConfigSnapshot().columnProperties) !== null && _this$_getConfigSnaps !== void 0 ? _this$_getConfigSnaps : []);
736
+ return Tools.deepClone(this._getConfigSnapshot().columnProperties ?? []);
743
737
  }
744
738
  /**
745
739
  * Returns a compact serializable section summary.
@@ -750,10 +744,9 @@ let FDocumentSection = class FDocumentSection {
750
744
  * ```
751
745
  */
752
746
  describe() {
753
- var _config$columnPropert, _config$columnSeparat, _config$sectionType;
754
747
  const { index } = this._resolve();
755
748
  const config = this._getConfigSnapshot();
756
- const columns = (_config$columnPropert = config.columnProperties) !== null && _config$columnPropert !== void 0 ? _config$columnPropert : [];
749
+ const columns = config.columnProperties ?? [];
757
750
  const headerFooter = {
758
751
  defaultHeader: this._describeHeaderFooterReference("header", "default"),
759
752
  defaultFooter: this._describeHeaderFooterReference("footer", "default"),
@@ -768,8 +761,8 @@ let FDocumentSection = class FDocumentSection {
768
761
  range: this._getRange(index),
769
762
  columnCount: columns.length || 1,
770
763
  columns: Tools.deepClone(columns),
771
- columnSeparatorType: (_config$columnSeparat = config.columnSeparatorType) !== null && _config$columnSeparat !== void 0 ? _config$columnSeparat : ColumnSeparatorType.NONE,
772
- sectionType: (_config$sectionType = config.sectionType) !== null && _config$sectionType !== void 0 ? _config$sectionType : SectionType.SECTION_TYPE_UNSPECIFIED,
764
+ columnSeparatorType: config.columnSeparatorType ?? ColumnSeparatorType.NONE,
765
+ sectionType: config.sectionType ?? SectionType.SECTION_TYPE_UNSPECIFIED,
773
766
  headerFooter,
774
767
  config
775
768
  };
@@ -787,15 +780,14 @@ let FDocumentSection = class FDocumentSection {
787
780
  * ```
788
781
  */
789
782
  setColumns(columnCount, options = {}) {
790
- var _options$gap, _options$separator;
791
783
  this._assertTraditionalDocument();
792
784
  if (!Number.isInteger(columnCount) || columnCount < 1) throw new RangeError("Section column count must be a positive integer.");
793
785
  if (options.widths && options.widths.length !== columnCount) throw new RangeError("Section column widths must match the column count.");
794
786
  if (options.gap != null && (!Number.isFinite(options.gap) || options.gap < 0)) throw new RangeError("Section column gap must be finite and non-negative.");
795
- const gap = Math.max(0, (_options$gap = options.gap) !== null && _options$gap !== void 0 ? _options$gap : 18);
787
+ const gap = Math.max(0, options.gap ?? 18);
796
788
  const config = this._getConfigSnapshot();
797
789
  const columns = createSectionColumnProperties(this._document.getDocumentDataModel().getSnapshot().documentStyle, config, columnCount, gap, options.widths);
798
- const separator = typeof options.separator === "boolean" ? options.separator ? ColumnSeparatorType.BETWEEN_EACH_COLUMN : ColumnSeparatorType.NONE : (_options$separator = options.separator) !== null && _options$separator !== void 0 ? _options$separator : ColumnSeparatorType.NONE;
790
+ const separator = typeof options.separator === "boolean" ? options.separator ? ColumnSeparatorType.BETWEEN_EACH_COLUMN : ColumnSeparatorType.NONE : options.separator ?? ColumnSeparatorType.NONE;
799
791
  if (!Object.values(ColumnSeparatorType).includes(separator)) throw new RangeError("Invalid section column separator type.");
800
792
  return this._update({
801
793
  columnProperties: columns,
@@ -1009,8 +1001,7 @@ let FDocumentSection = class FDocumentSection {
1009
1001
  * ```
1010
1002
  */
1011
1003
  getHeaderId(variant = "default") {
1012
- var _this$_getHeaderFoote;
1013
- return (_this$_getHeaderFoote = this._getHeaderFooterReference("header", variant).segmentId) !== null && _this$_getHeaderFoote !== void 0 ? _this$_getHeaderFoote : null;
1004
+ return this._getHeaderFooterReference("header", variant).segmentId ?? null;
1014
1005
  }
1015
1006
  /**
1016
1007
  * Returns the effective footer id after resolving links to previous sections.
@@ -1021,8 +1012,7 @@ let FDocumentSection = class FDocumentSection {
1021
1012
  * ```
1022
1013
  */
1023
1014
  getFooterId(variant = "default") {
1024
- var _this$_getHeaderFoote2;
1025
- return (_this$_getHeaderFoote2 = this._getHeaderFooterReference("footer", variant).segmentId) !== null && _this$_getHeaderFoote2 !== void 0 ? _this$_getHeaderFoote2 : null;
1015
+ return this._getHeaderFooterReference("footer", variant).segmentId ?? null;
1026
1016
  }
1027
1017
  /**
1028
1018
  * Whether this header variant inherits the previous section's reference.
@@ -1157,10 +1147,9 @@ let FDocumentSection = class FDocumentSection {
1157
1147
  return resolveSectionHeaderFooterReference(this._document.getDocumentDataModel().getSnapshot().documentStyle, getTopLevelSectionBreaks(this._document.getBody()), index, getSectionHeaderFooterReferenceKey(kind, variant));
1158
1148
  }
1159
1149
  _describeHeaderFooterReference(kind, variant) {
1160
- var _reference$segmentId;
1161
1150
  const reference = this._getHeaderFooterReference(kind, variant);
1162
1151
  return {
1163
- segmentId: (_reference$segmentId = reference.segmentId) !== null && _reference$segmentId !== void 0 ? _reference$segmentId : null,
1152
+ segmentId: reference.segmentId ?? null,
1164
1153
  linkedToPrevious: reference.linkedToPrevious
1165
1154
  };
1166
1155
  }
@@ -1276,6 +1265,28 @@ let FDocument = class FDocument extends FBaseInitialable {
1276
1265
  return documentDataModel;
1277
1266
  }
1278
1267
  /**
1268
+ * Returns the document's custom blocks in stable model order.
1269
+ *
1270
+ * This method is available in Node/headless environments and does not
1271
+ * perform font measurement, line wrapping, pagination, or rendering.
1272
+ *
1273
+ * @returns {IDocumentCustomBlockLayout} Custom block identifiers and model positions.
1274
+ * @example
1275
+ * ```ts
1276
+ * const document = univerAPI.getActiveDocument();
1277
+ * const layout = document?.getCustomBlockLayout();
1278
+ * console.log(layout?.blocks);
1279
+ * ```
1280
+ */
1281
+ getCustomBlockLayout() {
1282
+ var _this$_documentDataMo;
1283
+ return { blocks: (((_this$_documentDataMo = this._documentDataModel.getBody()) === null || _this$_documentDataMo === void 0 ? void 0 : _this$_documentDataMo.customBlocks) ?? []).map(({ blockId, startIndex }, index) => ({
1284
+ blockId,
1285
+ startIndex,
1286
+ index
1287
+ })) };
1288
+ }
1289
+ /**
1279
1290
  * Get the document body or header/footer body by the segment id.
1280
1291
  * The main body has an empty segment id.
1281
1292
  * The header and footer body have their respective segment ids.
@@ -1291,8 +1302,8 @@ let FDocument = class FDocument extends FBaseInitialable {
1291
1302
  * ```
1292
1303
  */
1293
1304
  getBody(segmentId = "") {
1294
- var _this$_documentDataMo;
1295
- const body = (_this$_documentDataMo = this._documentDataModel.getSelfOrHeaderFooterModel(segmentId)) === null || _this$_documentDataMo === void 0 ? void 0 : _this$_documentDataMo.getBody();
1305
+ var _this$_documentDataMo2;
1306
+ const body = (_this$_documentDataMo2 = this._documentDataModel.getSelfOrHeaderFooterModel(segmentId)) === null || _this$_documentDataMo2 === void 0 ? void 0 : _this$_documentDataMo2.getBody();
1296
1307
  if (!body) throw new Error(segmentId === "" ? "Body is not found in the document." : `Body is not found in the segment: ${segmentId}`);
1297
1308
  return body;
1298
1309
  }
@@ -1389,8 +1400,7 @@ let FDocument = class FDocument extends FBaseInitialable {
1389
1400
  return this._resolveDocumentFlavor() === DocumentFlavor.MODERN;
1390
1401
  }
1391
1402
  _resolveDocumentFlavor() {
1392
- var _this$_documentDataMo2;
1393
- return (_this$_documentDataMo2 = this._documentDataModel.getSnapshot().documentStyle.documentFlavor) !== null && _this$_documentDataMo2 !== void 0 ? _this$_documentDataMo2 : DocumentFlavor.UNSPECIFIED;
1403
+ return this._documentDataModel.getSnapshot().documentStyle.documentFlavor ?? DocumentFlavor.UNSPECIFIED;
1394
1404
  }
1395
1405
  /**
1396
1406
  * Save the document snapshot data, including the document content and resource data, etc.
@@ -1565,8 +1575,7 @@ let FDocument = class FDocument extends FBaseInitialable {
1565
1575
  * ```
1566
1576
  */
1567
1577
  getSection(index) {
1568
- var _this$getSections$ind;
1569
- return (_this$getSections$ind = this.getSections()[index]) !== null && _this$getSections$ind !== void 0 ? _this$getSections$ind : null;
1578
+ return this.getSections()[index] ?? null;
1570
1579
  }
1571
1580
  /**
1572
1581
  * Returns the traditional section containing a data-stream offset, or `null` in modern documents.
@@ -1580,11 +1589,10 @@ let FDocument = class FDocument extends FBaseInitialable {
1580
1589
  * ```
1581
1590
  */
1582
1591
  getSectionAt(offset) {
1583
- var _this$getSections$fin;
1584
- return (_this$getSections$fin = this.getSections().find((section) => {
1592
+ return this.getSections().find((section) => {
1585
1593
  const range = section.getRange();
1586
1594
  return offset >= range.startOffset && offset < range.endOffset;
1587
- })) !== null && _this$getSections$fin !== void 0 ? _this$getSections$fin : null;
1595
+ }) ?? null;
1588
1596
  }
1589
1597
  /**
1590
1598
  * Inserts a traditional document section break and returns its stable facade.
@@ -1639,10 +1647,9 @@ let FDocument = class FDocument extends FBaseInitialable {
1639
1647
  * ```
1640
1648
  */
1641
1649
  insertSectionBreak(offset, options = {}) {
1642
- var _this$getBody$section;
1643
1650
  if (this._documentDataModel.getSnapshot().documentStyle.documentFlavor !== DocumentFlavor.TRADITIONAL) throw new DocsSectionUnsupportedDocumentFlavorError();
1644
1651
  const { nextSectionType, ...config } = options;
1645
- const sectionId = createSectionId(new Set(((_this$getBody$section = this.getBody().sectionBreaks) !== null && _this$getBody$section !== void 0 ? _this$getBody$section : []).map((section) => section.sectionId)));
1652
+ const sectionId = createSectionId(new Set((this.getBody().sectionBreaks ?? []).map((section) => section.sectionId)));
1646
1653
  return this._commandService.syncExecuteCommand(InsertDocumentSectionBreakCommand.id, {
1647
1654
  unitId: this.getId(),
1648
1655
  offset,
@@ -1896,36 +1903,29 @@ let FDocument = class FDocument extends FBaseInitialable {
1896
1903
  return segmentId;
1897
1904
  }
1898
1905
  _getHeaderFooterCreateInfo(kind, pageIndex) {
1899
- var _documentStyle$defaul, _documentStyle$defaul2;
1900
1906
  const { documentStyle } = this._documentDataModel.getSnapshot();
1901
1907
  const isFirstPage = pageIndex === 0;
1902
1908
  const isEvenPage = (pageIndex + 1) % 2 === 0;
1903
- if (isFirstPage && documentStyle.useFirstPageHeaderFooter === BooleanNumber.TRUE) {
1904
- var _documentStyle$firstP, _documentStyle$firstP2;
1905
- return kind === "header" ? {
1906
- createType: HeaderFooterType.FIRST_PAGE_HEADER,
1907
- segmentId: (_documentStyle$firstP = documentStyle.firstPageHeaderId) !== null && _documentStyle$firstP !== void 0 ? _documentStyle$firstP : ""
1908
- } : {
1909
- createType: HeaderFooterType.FIRST_PAGE_FOOTER,
1910
- segmentId: (_documentStyle$firstP2 = documentStyle.firstPageFooterId) !== null && _documentStyle$firstP2 !== void 0 ? _documentStyle$firstP2 : ""
1911
- };
1912
- }
1913
- if (isEvenPage && documentStyle.evenAndOddHeaders === BooleanNumber.TRUE) {
1914
- var _documentStyle$evenPa, _documentStyle$evenPa2;
1915
- return kind === "header" ? {
1916
- createType: HeaderFooterType.EVEN_PAGE_HEADER,
1917
- segmentId: (_documentStyle$evenPa = documentStyle.evenPageHeaderId) !== null && _documentStyle$evenPa !== void 0 ? _documentStyle$evenPa : ""
1918
- } : {
1919
- createType: HeaderFooterType.EVEN_PAGE_FOOTER,
1920
- segmentId: (_documentStyle$evenPa2 = documentStyle.evenPageFooterId) !== null && _documentStyle$evenPa2 !== void 0 ? _documentStyle$evenPa2 : ""
1921
- };
1922
- }
1909
+ if (isFirstPage && documentStyle.useFirstPageHeaderFooter === BooleanNumber.TRUE) return kind === "header" ? {
1910
+ createType: HeaderFooterType.FIRST_PAGE_HEADER,
1911
+ segmentId: documentStyle.firstPageHeaderId ?? ""
1912
+ } : {
1913
+ createType: HeaderFooterType.FIRST_PAGE_FOOTER,
1914
+ segmentId: documentStyle.firstPageFooterId ?? ""
1915
+ };
1916
+ if (isEvenPage && documentStyle.evenAndOddHeaders === BooleanNumber.TRUE) return kind === "header" ? {
1917
+ createType: HeaderFooterType.EVEN_PAGE_HEADER,
1918
+ segmentId: documentStyle.evenPageHeaderId ?? ""
1919
+ } : {
1920
+ createType: HeaderFooterType.EVEN_PAGE_FOOTER,
1921
+ segmentId: documentStyle.evenPageFooterId ?? ""
1922
+ };
1923
1923
  return kind === "header" ? {
1924
1924
  createType: HeaderFooterType.DEFAULT_HEADER,
1925
- segmentId: (_documentStyle$defaul = documentStyle.defaultHeaderId) !== null && _documentStyle$defaul !== void 0 ? _documentStyle$defaul : ""
1925
+ segmentId: documentStyle.defaultHeaderId ?? ""
1926
1926
  } : {
1927
1927
  createType: HeaderFooterType.DEFAULT_FOOTER,
1928
- segmentId: (_documentStyle$defaul2 = documentStyle.defaultFooterId) !== null && _documentStyle$defaul2 !== void 0 ? _documentStyle$defaul2 : ""
1928
+ segmentId: documentStyle.defaultFooterId ?? ""
1929
1929
  };
1930
1930
  }
1931
1931
  };