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