@univerjs/docs 1.0.0-alpha.1 → 1.0.0-alpha.2

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/index.js CHANGED
@@ -387,14 +387,14 @@ const InsertTextCommand = {
387
387
  id: "doc.command.insert-text",
388
388
  type: _univerjs_core.CommandType.COMMAND,
389
389
  handler: (accessor, params) => {
390
- var _activeRange$segmentI;
390
+ var _docDataModel$getSelf, _activeRange$segmentI;
391
391
  const commandService = accessor.get(_univerjs_core.ICommandService);
392
392
  const { range, segmentId, body, unitId, cursorOffset } = params;
393
393
  const docSelectionManagerService = accessor.get(DocSelectionManagerService);
394
394
  const docDataModel = accessor.get(_univerjs_core.IUniverInstanceService).getUnit(unitId, _univerjs_core.UniverInstanceType.UNIVER_DOC);
395
395
  if (docDataModel == null) return false;
396
396
  const activeRange = docSelectionManagerService.getActiveTextRange();
397
- const originBody = docDataModel.getSelfOrHeaderFooterModel((_activeRange$segmentI = activeRange === null || activeRange === void 0 ? void 0 : activeRange.segmentId) !== null && _activeRange$segmentI !== void 0 ? _activeRange$segmentI : "").getBody();
397
+ const originBody = (_docDataModel$getSelf = docDataModel.getSelfOrHeaderFooterModel((_activeRange$segmentI = activeRange === null || activeRange === void 0 ? void 0 : activeRange.segmentId) !== null && _activeRange$segmentI !== void 0 ? _activeRange$segmentI : "")) === null || _docDataModel$getSelf === void 0 ? void 0 : _docDataModel$getSelf.getBody();
398
398
  if (originBody == null) return false;
399
399
  const { startOffset, collapsed } = range;
400
400
  const cursorMove = cursorOffset !== null && cursorOffset !== void 0 ? cursorOffset : body.dataStream.length;
@@ -447,12 +447,12 @@ const DeleteTextCommand = {
447
447
  id: "doc.command.delete-text",
448
448
  type: _univerjs_core.CommandType.COMMAND,
449
449
  handler: (accessor, params) => {
450
- var _body$customRanges;
450
+ var _docDataModel$getSelf2, _body$customRanges;
451
451
  const commandService = accessor.get(_univerjs_core.ICommandService);
452
452
  const univerInstanceService = accessor.get(_univerjs_core.IUniverInstanceService);
453
453
  const { range, segmentId, unitId, direction, len = 1 } = params;
454
454
  const docDataModel = univerInstanceService.getUnit(unitId, _univerjs_core.UniverInstanceType.UNIVER_DOC);
455
- const body = docDataModel === null || docDataModel === void 0 ? void 0 : docDataModel.getSelfOrHeaderFooterModel(segmentId).getBody();
455
+ const body = docDataModel === null || docDataModel === void 0 || (_docDataModel$getSelf2 = docDataModel.getSelfOrHeaderFooterModel(segmentId)) === null || _docDataModel$getSelf2 === void 0 ? void 0 : _docDataModel$getSelf2.getBody();
456
456
  if (docDataModel == null || body == null) return false;
457
457
  const { startOffset } = range;
458
458
  let start = direction === _univerjs_core.DeleteDirection.LEFT ? startOffset - len : startOffset;
@@ -500,7 +500,7 @@ const UpdateTextCommand = {
500
500
  handler: (accessor, params) => {
501
501
  const { range, segmentId, updateBody, coverType, unitId, textRanges } = params;
502
502
  const commandService = accessor.get(_univerjs_core.ICommandService);
503
- const docDataModel = accessor.get(_univerjs_core.IUniverInstanceService).getCurrentUniverDocInstance();
503
+ const docDataModel = accessor.get(_univerjs_core.IUniverInstanceService).getCurrentUnitOfType(_univerjs_core.UniverInstanceType.UNIVER_DOC);
504
504
  if (docDataModel == null) return false;
505
505
  const doMutation = {
506
506
  id: RichTextEditingMutation.id,
@@ -530,10 +530,141 @@ const UpdateTextCommand = {
530
530
  }
531
531
  };
532
532
 
533
+ //#endregion
534
+ //#region src/commands/commands/create-header-footer.command.ts
535
+ let HeaderFooterType = /* @__PURE__ */ function(HeaderFooterType) {
536
+ HeaderFooterType[HeaderFooterType["FIRST_PAGE_HEADER"] = 0] = "FIRST_PAGE_HEADER";
537
+ HeaderFooterType[HeaderFooterType["FIRST_PAGE_FOOTER"] = 1] = "FIRST_PAGE_FOOTER";
538
+ HeaderFooterType[HeaderFooterType["DEFAULT_HEADER"] = 2] = "DEFAULT_HEADER";
539
+ HeaderFooterType[HeaderFooterType["DEFAULT_FOOTER"] = 3] = "DEFAULT_FOOTER";
540
+ HeaderFooterType[HeaderFooterType["EVEN_PAGE_HEADER"] = 4] = "EVEN_PAGE_HEADER";
541
+ HeaderFooterType[HeaderFooterType["EVEN_PAGE_FOOTER"] = 5] = "EVEN_PAGE_FOOTER";
542
+ return HeaderFooterType;
543
+ }({});
544
+ function getEmptyHeaderFooterBody() {
545
+ return {
546
+ dataStream: "\r\n",
547
+ textRuns: [{
548
+ st: 0,
549
+ ed: 0,
550
+ ts: { fs: 9 }
551
+ }],
552
+ customBlocks: [],
553
+ paragraphs: [{
554
+ startIndex: 0,
555
+ paragraphId: (0, _univerjs_core.createParagraphId)(/* @__PURE__ */ new Set()),
556
+ paragraphStyle: {
557
+ spaceAbove: { v: 0 },
558
+ lineSpacing: 1.5,
559
+ spaceBelow: { v: 0 }
560
+ }
561
+ }],
562
+ sectionBreaks: [{ startIndex: 1 }]
563
+ };
564
+ }
565
+ function createHeaderFooterAction(segmentId, createType, documentStyle, actions, createMode = "single") {
566
+ const jsonX = _univerjs_core.JSONX.getInstance();
567
+ const ID_LEN = 6;
568
+ const firstSegmentId = segmentId !== null && segmentId !== void 0 ? segmentId : (0, _univerjs_core.generateRandomId)(ID_LEN);
569
+ const isHeader = createType === 2 || createType === 0 || createType === 4;
570
+ const insertAction = jsonX.insertOp([isHeader ? "headers" : "footers", firstSegmentId], {
571
+ [isHeader ? "headerId" : "footerId"]: firstSegmentId,
572
+ body: getEmptyHeaderFooterBody()
573
+ });
574
+ actions.push(insertAction);
575
+ let key = "defaultHeaderId";
576
+ let pairKey = "defaultFooterId";
577
+ switch (createType) {
578
+ case 2:
579
+ key = "defaultHeaderId";
580
+ pairKey = "defaultFooterId";
581
+ break;
582
+ case 3:
583
+ key = "defaultFooterId";
584
+ pairKey = "defaultHeaderId";
585
+ break;
586
+ case 0:
587
+ key = "firstPageHeaderId";
588
+ pairKey = "firstPageFooterId";
589
+ break;
590
+ case 1:
591
+ key = "firstPageFooterId";
592
+ pairKey = "firstPageHeaderId";
593
+ break;
594
+ case 4:
595
+ key = "evenPageHeaderId";
596
+ pairKey = "evenPageFooterId";
597
+ break;
598
+ case 5:
599
+ key = "evenPageFooterId";
600
+ pairKey = "evenPageHeaderId";
601
+ break;
602
+ default: throw new Error(`Unknown header footer type: ${createType}`);
603
+ }
604
+ const linkedSegmentIds = [[key, firstSegmentId]];
605
+ if (createMode === "pair" && pairKey != null) {
606
+ const secondSegmentId = (0, _univerjs_core.generateRandomId)(ID_LEN);
607
+ const insertPairAction = jsonX.insertOp([isHeader ? "footers" : "headers", secondSegmentId], {
608
+ [isHeader ? "footerId" : "headerId"]: secondSegmentId,
609
+ body: getEmptyHeaderFooterBody()
610
+ });
611
+ actions.push(insertPairAction);
612
+ linkedSegmentIds.push([pairKey, secondSegmentId]);
613
+ }
614
+ for (const [k, id] of linkedSegmentIds) if (documentStyle[k] != null) {
615
+ const replaceAction = jsonX.replaceOp(["documentStyle", k], documentStyle[k], id);
616
+ actions.push(replaceAction);
617
+ } else {
618
+ const insertAction = jsonX.insertOp(["documentStyle", k], id);
619
+ actions.push(insertAction);
620
+ }
621
+ return actions;
622
+ }
623
+ const CreateHeaderFooterCommand = {
624
+ id: "doc.command.create-header-footer",
625
+ type: _univerjs_core.CommandType.COMMAND,
626
+ handler: (accessor, params) => {
627
+ const commandService = accessor.get(_univerjs_core.ICommandService);
628
+ const univerInstanceService = accessor.get(_univerjs_core.IUniverInstanceService);
629
+ const { unitId, segmentId, createType, headerFooterProps, createMode = "single" } = params;
630
+ const docDataModel = univerInstanceService.getUnit(unitId, _univerjs_core.UniverInstanceType.UNIVER_DOC);
631
+ if (docDataModel == null) return false;
632
+ const { documentStyle } = docDataModel.getSnapshot();
633
+ if (documentStyle.documentFlavor === _univerjs_core.DocumentFlavor.MODERN) return false;
634
+ const rawActions = [];
635
+ const jsonX = _univerjs_core.JSONX.getInstance();
636
+ if (createType != null) createHeaderFooterAction(segmentId, createType, documentStyle, rawActions, createMode);
637
+ if (headerFooterProps != null) Object.keys(headerFooterProps).forEach((key) => {
638
+ const value = headerFooterProps[key];
639
+ const oldValue = documentStyle[key];
640
+ if (value === oldValue) return;
641
+ const action = oldValue === void 0 ? jsonX.insertOp(["documentStyle", key], value) : jsonX.replaceOp(["documentStyle", key], oldValue, value);
642
+ rawActions.push(action);
643
+ });
644
+ if (rawActions.length === 0) return false;
645
+ const doMutation = {
646
+ id: RichTextEditingMutation.id,
647
+ params: {
648
+ unitId,
649
+ actions: rawActions.reduce((acc, cur) => _univerjs_core.JSONX.compose(acc, cur), null),
650
+ textRanges: [{
651
+ startOffset: 0,
652
+ endOffset: 0,
653
+ collapsed: true
654
+ }],
655
+ debounce: true
656
+ }
657
+ };
658
+ if ((headerFooterProps === null || headerFooterProps === void 0 ? void 0 : headerFooterProps.marginFooter) != null || (headerFooterProps === null || headerFooterProps === void 0 ? void 0 : headerFooterProps.marginHeader) != null) doMutation.params.noNeedSetTextRange = true;
659
+ const result = commandService.syncExecuteCommand(doMutation.id, doMutation.params);
660
+ return Boolean(result);
661
+ }
662
+ };
663
+
533
664
  //#endregion
534
665
  //#region package.json
535
666
  var name = "@univerjs/docs";
536
- var version = "1.0.0-alpha.1";
667
+ var version = "1.0.0-alpha.2";
537
668
 
538
669
  //#endregion
539
670
  //#region src/commands/mutations/docs-rename.mutation.ts
@@ -725,7 +856,7 @@ let DocStateChangeManagerService = class DocStateChangeManagerService extends _u
725
856
  _initialize() {
726
857
  this.disposeWithMe(this._commandService.beforeCommandExecuted((command) => {
727
858
  if (command.id === _univerjs_core.UndoCommandId || command.id === _univerjs_core.RedoCommandId) {
728
- const univerDoc = this._univerInstanceService.getCurrentUniverDocInstance();
859
+ const univerDoc = this._univerInstanceService.getCurrentUnitOfType(_univerjs_core.UniverInstanceType.UNIVER_DOC);
729
860
  if (univerDoc == null) return;
730
861
  const unitId = univerDoc.getUnitId();
731
862
  this._pushHistory(unitId);
@@ -855,6 +986,7 @@ let UniverDocsPlugin = class UniverDocsPlugin extends _univerjs_core.Plugin {
855
986
  InsertTextCommand,
856
987
  DeleteTextCommand,
857
988
  UpdateTextCommand,
989
+ CreateHeaderFooterCommand,
858
990
  RichTextEditingMutation,
859
991
  DocsRenameMutation,
860
992
  SetTextSelectionsOperation
@@ -984,7 +1116,7 @@ function addCustomRangeFactory(accessor, param, body) {
984
1116
  return doMutation;
985
1117
  }
986
1118
  function addCustomRangeBySelectionFactory(accessor, param) {
987
- var _selections$;
1119
+ var _selections$, _documentDataModel$ge;
988
1120
  const { rangeId, rangeType, wholeEntity, properties, unitId, selections: propSelection } = param;
989
1121
  const docSelectionManagerService = accessor.get(DocSelectionManagerService);
990
1122
  const univerInstanceService = accessor.get(_univerjs_core.IUniverInstanceService);
@@ -996,7 +1128,7 @@ function addCustomRangeBySelectionFactory(accessor, param) {
996
1128
  if (!(selections === null || selections === void 0 ? void 0 : selections.length)) return false;
997
1129
  const documentDataModel = univerInstanceService.getUnit(unitId, _univerjs_core.UniverInstanceType.UNIVER_DOC);
998
1130
  if (!documentDataModel) return false;
999
- const body = documentDataModel.getSelfOrHeaderFooterModel(segmentId).getBody();
1131
+ const body = (_documentDataModel$ge = documentDataModel.getSelfOrHeaderFooterModel(segmentId)) === null || _documentDataModel$ge === void 0 ? void 0 : _documentDataModel$ge.getBody();
1000
1132
  if (!body) return false;
1001
1133
  const textX = _univerjs_core.BuildTextUtils.customRange.add({
1002
1134
  ranges: selections,
@@ -1093,6 +1225,20 @@ function replaceSelectionFactory(accessor, params) {
1093
1225
  }
1094
1226
 
1095
1227
  //#endregion
1228
+ //#region src/utils/util.ts
1229
+ function consumeContentInsertRange(accessor, unitId) {
1230
+ try {
1231
+ return accessor.get(DocContentInsertService).consumeInsertRange(unitId);
1232
+ } catch {
1233
+ return null;
1234
+ }
1235
+ }
1236
+ function isHeaderFooterSelection(range) {
1237
+ return Boolean(range === null || range === void 0 ? void 0 : range.segmentId);
1238
+ }
1239
+
1240
+ //#endregion
1241
+ exports.CreateHeaderFooterCommand = CreateHeaderFooterCommand;
1096
1242
  exports.DOC_INTERCEPTOR_POINT = DOC_INTERCEPTOR_POINT;
1097
1243
  exports.DeleteTextCommand = DeleteTextCommand;
1098
1244
  exports.DocBlockMoveValidatorService = DocBlockMoveValidatorService;
@@ -1122,6 +1268,7 @@ Object.defineProperty(exports, 'DocStateChangeManagerService', {
1122
1268
  }
1123
1269
  });
1124
1270
  exports.DocStateEmitService = DocStateEmitService;
1271
+ exports.HeaderFooterType = HeaderFooterType;
1125
1272
  exports.IDocStateChangeInterceptorService = IDocStateChangeInterceptorService;
1126
1273
  exports.InsertTextCommand = InsertTextCommand;
1127
1274
  exports.RichTextEditingMutation = RichTextEditingMutation;
@@ -1135,5 +1282,7 @@ Object.defineProperty(exports, 'UniverDocsPlugin', {
1135
1282
  exports.UpdateTextCommand = UpdateTextCommand;
1136
1283
  exports.addCustomRangeBySelectionFactory = addCustomRangeBySelectionFactory;
1137
1284
  exports.addCustomRangeFactory = addCustomRangeFactory;
1285
+ exports.consumeContentInsertRange = consumeContentInsertRange;
1138
1286
  exports.deleteCustomRangeFactory = deleteCustomRangeFactory;
1287
+ exports.isHeaderFooterSelection = isHeaderFooterSelection;
1139
1288
  exports.replaceSelectionFactory = replaceSelectionFactory;