@univerjs/docs 1.0.0-alpha.8 → 1.0.0-beta.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.
package/lib/facade.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { BooleanNumber, ColumnSeparatorType, DashStyleType, DataStreamTreeTokenType, DocumentFlavor, ICommandService, IResourceLoaderService, IUniverInstanceService, Inject, Injector, JSONX, PageOrientType, PresetListType, RESTORE_INSERTED_PARAGRAPH_IDS, RedoCommand, SectionType, TextX, TextXActionType, Tools, UndoCommand, UniverInstanceType, UpdateDocsAttributeType, createParagraphId, createSectionId, generateRandomId, getParagraphContentStartOffset, getRichTextEditPath, getSectionHeaderFooterReferenceKey, regexp, resolveSectionHeaderFooterReference } from "@univerjs/core";
2
2
  import { FBaseInitialable, FEnum, FUniver } from "@univerjs/core/facade";
3
- import { CreateHeaderFooterCommand, DeleteDocumentSectionBreakCommand, HeaderFooterType, InsertDocumentColumnBreakCommand, InsertDocumentSectionBreakCommand, RichTextEditingMutation, SetSectionHeaderFooterLinkCommand, UpdateDocumentParagraphStyleCommand, UpdateDocumentSectionCommand, createSectionColumnProperties, generateParagraphs, getEffectiveSectionPageSetup, getSectionContentWidth, getTopLevelSectionBreaks } from "@univerjs/docs";
3
+ import { CreateHeaderFooterCommand, DeleteDocumentSectionBreakCommand, HeaderFooterType, InsertDocumentColumnBreakCommand, InsertDocumentSectionBreakCommand, RichTextEditingMutation, SetDocumentNameCommand, SetSectionHeaderFooterLinkCommand, UpdateDocumentParagraphStyleCommand, UpdateDocumentSectionCommand, createSectionColumnProperties, generateParagraphs, getEffectiveSectionPageSetup, getSectionContentWidth, getTopLevelSectionBreaks } from "@univerjs/docs";
4
4
 
5
5
  //#region src/facade/utils.ts
6
6
  function cloneParagraphStyle(paragraphStyle) {
@@ -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
  }
@@ -1324,6 +1335,24 @@ let FDocument = class FDocument extends FBaseInitialable {
1324
1335
  return this._documentDataModel.getTitle() || "";
1325
1336
  }
1326
1337
  /**
1338
+ * Set the document name.
1339
+ * @param {string} name The new document name.
1340
+ * @returns {FDocument} The current document for chaining.
1341
+ *
1342
+ * @example
1343
+ * ```ts
1344
+ * const document = univerAPI.getActiveDocument();
1345
+ * document?.setName('Quarterly Report');
1346
+ * ```
1347
+ */
1348
+ setName(name) {
1349
+ this._commandService.syncExecuteCommand(SetDocumentNameCommand.id, {
1350
+ unitId: this.id,
1351
+ name
1352
+ });
1353
+ return this;
1354
+ }
1355
+ /**
1327
1356
  * Returns the document's explicit layout flavor.
1328
1357
  *
1329
1358
  * Use this method when all three states matter. Do not infer a Traditional
@@ -1389,8 +1418,7 @@ let FDocument = class FDocument extends FBaseInitialable {
1389
1418
  return this._resolveDocumentFlavor() === DocumentFlavor.MODERN;
1390
1419
  }
1391
1420
  _resolveDocumentFlavor() {
1392
- var _this$_documentDataMo2;
1393
- return (_this$_documentDataMo2 = this._documentDataModel.getSnapshot().documentStyle.documentFlavor) !== null && _this$_documentDataMo2 !== void 0 ? _this$_documentDataMo2 : DocumentFlavor.UNSPECIFIED;
1421
+ return this._documentDataModel.getSnapshot().documentStyle.documentFlavor ?? DocumentFlavor.UNSPECIFIED;
1394
1422
  }
1395
1423
  /**
1396
1424
  * Save the document snapshot data, including the document content and resource data, etc.
@@ -1565,8 +1593,7 @@ let FDocument = class FDocument extends FBaseInitialable {
1565
1593
  * ```
1566
1594
  */
1567
1595
  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;
1596
+ return this.getSections()[index] ?? null;
1570
1597
  }
1571
1598
  /**
1572
1599
  * Returns the traditional section containing a data-stream offset, or `null` in modern documents.
@@ -1580,11 +1607,10 @@ let FDocument = class FDocument extends FBaseInitialable {
1580
1607
  * ```
1581
1608
  */
1582
1609
  getSectionAt(offset) {
1583
- var _this$getSections$fin;
1584
- return (_this$getSections$fin = this.getSections().find((section) => {
1610
+ return this.getSections().find((section) => {
1585
1611
  const range = section.getRange();
1586
1612
  return offset >= range.startOffset && offset < range.endOffset;
1587
- })) !== null && _this$getSections$fin !== void 0 ? _this$getSections$fin : null;
1613
+ }) ?? null;
1588
1614
  }
1589
1615
  /**
1590
1616
  * Inserts a traditional document section break and returns its stable facade.
@@ -1639,10 +1665,9 @@ let FDocument = class FDocument extends FBaseInitialable {
1639
1665
  * ```
1640
1666
  */
1641
1667
  insertSectionBreak(offset, options = {}) {
1642
- var _this$getBody$section;
1643
1668
  if (this._documentDataModel.getSnapshot().documentStyle.documentFlavor !== DocumentFlavor.TRADITIONAL) throw new DocsSectionUnsupportedDocumentFlavorError();
1644
1669
  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)));
1670
+ const sectionId = createSectionId(new Set((this.getBody().sectionBreaks ?? []).map((section) => section.sectionId)));
1646
1671
  return this._commandService.syncExecuteCommand(InsertDocumentSectionBreakCommand.id, {
1647
1672
  unitId: this.getId(),
1648
1673
  offset,
@@ -1896,36 +1921,29 @@ let FDocument = class FDocument extends FBaseInitialable {
1896
1921
  return segmentId;
1897
1922
  }
1898
1923
  _getHeaderFooterCreateInfo(kind, pageIndex) {
1899
- var _documentStyle$defaul, _documentStyle$defaul2;
1900
1924
  const { documentStyle } = this._documentDataModel.getSnapshot();
1901
1925
  const isFirstPage = pageIndex === 0;
1902
1926
  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
- }
1927
+ if (isFirstPage && documentStyle.useFirstPageHeaderFooter === BooleanNumber.TRUE) return kind === "header" ? {
1928
+ createType: HeaderFooterType.FIRST_PAGE_HEADER,
1929
+ segmentId: documentStyle.firstPageHeaderId ?? ""
1930
+ } : {
1931
+ createType: HeaderFooterType.FIRST_PAGE_FOOTER,
1932
+ segmentId: documentStyle.firstPageFooterId ?? ""
1933
+ };
1934
+ if (isEvenPage && documentStyle.evenAndOddHeaders === BooleanNumber.TRUE) return kind === "header" ? {
1935
+ createType: HeaderFooterType.EVEN_PAGE_HEADER,
1936
+ segmentId: documentStyle.evenPageHeaderId ?? ""
1937
+ } : {
1938
+ createType: HeaderFooterType.EVEN_PAGE_FOOTER,
1939
+ segmentId: documentStyle.evenPageFooterId ?? ""
1940
+ };
1923
1941
  return kind === "header" ? {
1924
1942
  createType: HeaderFooterType.DEFAULT_HEADER,
1925
- segmentId: (_documentStyle$defaul = documentStyle.defaultHeaderId) !== null && _documentStyle$defaul !== void 0 ? _documentStyle$defaul : ""
1943
+ segmentId: documentStyle.defaultHeaderId ?? ""
1926
1944
  } : {
1927
1945
  createType: HeaderFooterType.DEFAULT_FOOTER,
1928
- segmentId: (_documentStyle$defaul2 = documentStyle.defaultFooterId) !== null && _documentStyle$defaul2 !== void 0 ? _documentStyle$defaul2 : ""
1946
+ segmentId: documentStyle.defaultFooterId ?? ""
1929
1947
  };
1930
1948
  }
1931
1949
  };