@univerjs/docs 1.0.0-alpha.0 → 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.0";
667
+ var version = "1.0.0-alpha.2";
537
668
 
538
669
  //#endregion
539
670
  //#region src/commands/mutations/docs-rename.mutation.ts
@@ -622,6 +753,33 @@ DocCustomRangeController = __decorate([
622
753
  __decorateParam(2, _univerjs_core.IUniverInstanceService)
623
754
  ], DocCustomRangeController);
624
755
 
756
+ //#endregion
757
+ //#region src/services/doc-block-move-validator.service.ts
758
+ var DocBlockMoveValidatorService = class extends _univerjs_core.Disposable {
759
+ constructor(..._args) {
760
+ super(..._args);
761
+ _defineProperty(this, "_validators", []);
762
+ _defineProperty(this, "_transformers", []);
763
+ }
764
+ registerValidator(validator) {
765
+ this._validators.push(validator);
766
+ return this.disposeWithMe((0, _univerjs_core.toDisposable)(() => (0, _univerjs_core.remove)(this._validators, validator)));
767
+ }
768
+ registerTransformer(transformer) {
769
+ this._transformers.push(transformer);
770
+ return this.disposeWithMe((0, _univerjs_core.toDisposable)(() => (0, _univerjs_core.remove)(this._transformers, transformer)));
771
+ }
772
+ canMoveBlock(context) {
773
+ return this._validators.every((validator) => validator(context));
774
+ }
775
+ transformMoveResult(context) {
776
+ return this._transformers.reduce((result, transformer) => transformer({
777
+ ...context,
778
+ result
779
+ }), context.result);
780
+ }
781
+ };
782
+
625
783
  //#endregion
626
784
  //#region src/services/doc-content-insert.service.ts
627
785
  /**
@@ -698,7 +856,7 @@ let DocStateChangeManagerService = class DocStateChangeManagerService extends _u
698
856
  _initialize() {
699
857
  this.disposeWithMe(this._commandService.beforeCommandExecuted((command) => {
700
858
  if (command.id === _univerjs_core.UndoCommandId || command.id === _univerjs_core.RedoCommandId) {
701
- const univerDoc = this._univerInstanceService.getCurrentUniverDocInstance();
859
+ const univerDoc = this._univerInstanceService.getCurrentUnitOfType(_univerjs_core.UniverInstanceType.UNIVER_DOC);
702
860
  if (univerDoc == null) return;
703
861
  const unitId = univerDoc.getUnitId();
704
862
  this._pushHistory(unitId);
@@ -828,6 +986,7 @@ let UniverDocsPlugin = class UniverDocsPlugin extends _univerjs_core.Plugin {
828
986
  InsertTextCommand,
829
987
  DeleteTextCommand,
830
988
  UpdateTextCommand,
989
+ CreateHeaderFooterCommand,
831
990
  RichTextEditingMutation,
832
991
  DocsRenameMutation,
833
992
  SetTextSelectionsOperation
@@ -840,6 +999,7 @@ let UniverDocsPlugin = class UniverDocsPlugin extends _univerjs_core.Plugin {
840
999
  [DocSelectionManagerService],
841
1000
  [DocStateEmitService],
842
1001
  [DocStateChangeManagerService],
1002
+ [DocBlockMoveValidatorService],
843
1003
  [DocContentInsertService],
844
1004
  [DocCustomRangeController]
845
1005
  ].forEach((d) => this._injector.add(d));
@@ -956,7 +1116,7 @@ function addCustomRangeFactory(accessor, param, body) {
956
1116
  return doMutation;
957
1117
  }
958
1118
  function addCustomRangeBySelectionFactory(accessor, param) {
959
- var _selections$;
1119
+ var _selections$, _documentDataModel$ge;
960
1120
  const { rangeId, rangeType, wholeEntity, properties, unitId, selections: propSelection } = param;
961
1121
  const docSelectionManagerService = accessor.get(DocSelectionManagerService);
962
1122
  const univerInstanceService = accessor.get(_univerjs_core.IUniverInstanceService);
@@ -968,7 +1128,7 @@ function addCustomRangeBySelectionFactory(accessor, param) {
968
1128
  if (!(selections === null || selections === void 0 ? void 0 : selections.length)) return false;
969
1129
  const documentDataModel = univerInstanceService.getUnit(unitId, _univerjs_core.UniverInstanceType.UNIVER_DOC);
970
1130
  if (!documentDataModel) return false;
971
- const body = documentDataModel.getSelfOrHeaderFooterModel(segmentId).getBody();
1131
+ const body = (_documentDataModel$ge = documentDataModel.getSelfOrHeaderFooterModel(segmentId)) === null || _documentDataModel$ge === void 0 ? void 0 : _documentDataModel$ge.getBody();
972
1132
  if (!body) return false;
973
1133
  const textX = _univerjs_core.BuildTextUtils.customRange.add({
974
1134
  ranges: selections,
@@ -1065,8 +1225,23 @@ function replaceSelectionFactory(accessor, params) {
1065
1225
  }
1066
1226
 
1067
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;
1068
1242
  exports.DOC_INTERCEPTOR_POINT = DOC_INTERCEPTOR_POINT;
1069
1243
  exports.DeleteTextCommand = DeleteTextCommand;
1244
+ exports.DocBlockMoveValidatorService = DocBlockMoveValidatorService;
1070
1245
  exports.DocContentInsertService = DocContentInsertService;
1071
1246
  Object.defineProperty(exports, 'DocInterceptorService', {
1072
1247
  enumerable: true,
@@ -1093,6 +1268,7 @@ Object.defineProperty(exports, 'DocStateChangeManagerService', {
1093
1268
  }
1094
1269
  });
1095
1270
  exports.DocStateEmitService = DocStateEmitService;
1271
+ exports.HeaderFooterType = HeaderFooterType;
1096
1272
  exports.IDocStateChangeInterceptorService = IDocStateChangeInterceptorService;
1097
1273
  exports.InsertTextCommand = InsertTextCommand;
1098
1274
  exports.RichTextEditingMutation = RichTextEditingMutation;
@@ -1106,5 +1282,7 @@ Object.defineProperty(exports, 'UniverDocsPlugin', {
1106
1282
  exports.UpdateTextCommand = UpdateTextCommand;
1107
1283
  exports.addCustomRangeBySelectionFactory = addCustomRangeBySelectionFactory;
1108
1284
  exports.addCustomRangeFactory = addCustomRangeFactory;
1285
+ exports.consumeContentInsertRange = consumeContentInsertRange;
1109
1286
  exports.deleteCustomRangeFactory = deleteCustomRangeFactory;
1287
+ exports.isHeaderFooterSelection = isHeaderFooterSelection;
1110
1288
  exports.replaceSelectionFactory = replaceSelectionFactory;