@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/es/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { BuildTextUtils, CommandType, DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY, DOCS_NORMAL_EDITOR_UNIT_ID_KEY, DeleteDirection, Disposable, DisposableCollection, ICommandService, IConfigService, IUndoRedoService, IUniverInstanceService, Inject, Injector, JSONX, LocaleService, Optional, Plugin, RedoCommandId, RxDisposable, TextX, TextXActionType, UndoCommandId, UniverInstanceType, composeInterceptors, createIdentifier, createInterceptorKey, getRichTextEditPath, isInternalEditorID, merge, remove, toDisposable } from "@univerjs/core";
1
+ import { BuildTextUtils, CommandType, DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY, DOCS_NORMAL_EDITOR_UNIT_ID_KEY, DeleteDirection, Disposable, DisposableCollection, DocumentFlavor, ICommandService, IConfigService, IUndoRedoService, IUniverInstanceService, Inject, Injector, JSONX, LocaleService, Optional, Plugin, RedoCommandId, RxDisposable, TextX, TextXActionType, UndoCommandId, UniverInstanceType, composeInterceptors, createIdentifier, createInterceptorKey, createParagraphId, generateRandomId, getRichTextEditPath, isInternalEditorID, merge, remove, toDisposable } from "@univerjs/core";
2
2
  import { DocumentSkeleton, DocumentViewModel, IRenderManagerService, NORMAL_TEXT_SELECTION_PLUGIN_STYLE } from "@univerjs/engine-render";
3
3
  import { BehaviorSubject, Subject, takeUntil } from "rxjs";
4
4
 
@@ -386,14 +386,14 @@ const InsertTextCommand = {
386
386
  id: "doc.command.insert-text",
387
387
  type: CommandType.COMMAND,
388
388
  handler: (accessor, params) => {
389
- var _activeRange$segmentI;
389
+ var _docDataModel$getSelf, _activeRange$segmentI;
390
390
  const commandService = accessor.get(ICommandService);
391
391
  const { range, segmentId, body, unitId, cursorOffset } = params;
392
392
  const docSelectionManagerService = accessor.get(DocSelectionManagerService);
393
393
  const docDataModel = accessor.get(IUniverInstanceService).getUnit(unitId, UniverInstanceType.UNIVER_DOC);
394
394
  if (docDataModel == null) return false;
395
395
  const activeRange = docSelectionManagerService.getActiveTextRange();
396
- const originBody = docDataModel.getSelfOrHeaderFooterModel((_activeRange$segmentI = activeRange === null || activeRange === void 0 ? void 0 : activeRange.segmentId) !== null && _activeRange$segmentI !== void 0 ? _activeRange$segmentI : "").getBody();
396
+ 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();
397
397
  if (originBody == null) return false;
398
398
  const { startOffset, collapsed } = range;
399
399
  const cursorMove = cursorOffset !== null && cursorOffset !== void 0 ? cursorOffset : body.dataStream.length;
@@ -446,12 +446,12 @@ const DeleteTextCommand = {
446
446
  id: "doc.command.delete-text",
447
447
  type: CommandType.COMMAND,
448
448
  handler: (accessor, params) => {
449
- var _body$customRanges;
449
+ var _docDataModel$getSelf2, _body$customRanges;
450
450
  const commandService = accessor.get(ICommandService);
451
451
  const univerInstanceService = accessor.get(IUniverInstanceService);
452
452
  const { range, segmentId, unitId, direction, len = 1 } = params;
453
453
  const docDataModel = univerInstanceService.getUnit(unitId, UniverInstanceType.UNIVER_DOC);
454
- const body = docDataModel === null || docDataModel === void 0 ? void 0 : docDataModel.getSelfOrHeaderFooterModel(segmentId).getBody();
454
+ const body = docDataModel === null || docDataModel === void 0 || (_docDataModel$getSelf2 = docDataModel.getSelfOrHeaderFooterModel(segmentId)) === null || _docDataModel$getSelf2 === void 0 ? void 0 : _docDataModel$getSelf2.getBody();
455
455
  if (docDataModel == null || body == null) return false;
456
456
  const { startOffset } = range;
457
457
  let start = direction === DeleteDirection.LEFT ? startOffset - len : startOffset;
@@ -499,7 +499,7 @@ const UpdateTextCommand = {
499
499
  handler: (accessor, params) => {
500
500
  const { range, segmentId, updateBody, coverType, unitId, textRanges } = params;
501
501
  const commandService = accessor.get(ICommandService);
502
- const docDataModel = accessor.get(IUniverInstanceService).getCurrentUniverDocInstance();
502
+ const docDataModel = accessor.get(IUniverInstanceService).getCurrentUnitOfType(UniverInstanceType.UNIVER_DOC);
503
503
  if (docDataModel == null) return false;
504
504
  const doMutation = {
505
505
  id: RichTextEditingMutation.id,
@@ -529,10 +529,141 @@ const UpdateTextCommand = {
529
529
  }
530
530
  };
531
531
 
532
+ //#endregion
533
+ //#region src/commands/commands/create-header-footer.command.ts
534
+ let HeaderFooterType = /* @__PURE__ */ function(HeaderFooterType) {
535
+ HeaderFooterType[HeaderFooterType["FIRST_PAGE_HEADER"] = 0] = "FIRST_PAGE_HEADER";
536
+ HeaderFooterType[HeaderFooterType["FIRST_PAGE_FOOTER"] = 1] = "FIRST_PAGE_FOOTER";
537
+ HeaderFooterType[HeaderFooterType["DEFAULT_HEADER"] = 2] = "DEFAULT_HEADER";
538
+ HeaderFooterType[HeaderFooterType["DEFAULT_FOOTER"] = 3] = "DEFAULT_FOOTER";
539
+ HeaderFooterType[HeaderFooterType["EVEN_PAGE_HEADER"] = 4] = "EVEN_PAGE_HEADER";
540
+ HeaderFooterType[HeaderFooterType["EVEN_PAGE_FOOTER"] = 5] = "EVEN_PAGE_FOOTER";
541
+ return HeaderFooterType;
542
+ }({});
543
+ function getEmptyHeaderFooterBody() {
544
+ return {
545
+ dataStream: "\r\n",
546
+ textRuns: [{
547
+ st: 0,
548
+ ed: 0,
549
+ ts: { fs: 9 }
550
+ }],
551
+ customBlocks: [],
552
+ paragraphs: [{
553
+ startIndex: 0,
554
+ paragraphId: createParagraphId(/* @__PURE__ */ new Set()),
555
+ paragraphStyle: {
556
+ spaceAbove: { v: 0 },
557
+ lineSpacing: 1.5,
558
+ spaceBelow: { v: 0 }
559
+ }
560
+ }],
561
+ sectionBreaks: [{ startIndex: 1 }]
562
+ };
563
+ }
564
+ function createHeaderFooterAction(segmentId, createType, documentStyle, actions, createMode = "single") {
565
+ const jsonX = JSONX.getInstance();
566
+ const ID_LEN = 6;
567
+ const firstSegmentId = segmentId !== null && segmentId !== void 0 ? segmentId : generateRandomId(ID_LEN);
568
+ const isHeader = createType === 2 || createType === 0 || createType === 4;
569
+ const insertAction = jsonX.insertOp([isHeader ? "headers" : "footers", firstSegmentId], {
570
+ [isHeader ? "headerId" : "footerId"]: firstSegmentId,
571
+ body: getEmptyHeaderFooterBody()
572
+ });
573
+ actions.push(insertAction);
574
+ let key = "defaultHeaderId";
575
+ let pairKey = "defaultFooterId";
576
+ switch (createType) {
577
+ case 2:
578
+ key = "defaultHeaderId";
579
+ pairKey = "defaultFooterId";
580
+ break;
581
+ case 3:
582
+ key = "defaultFooterId";
583
+ pairKey = "defaultHeaderId";
584
+ break;
585
+ case 0:
586
+ key = "firstPageHeaderId";
587
+ pairKey = "firstPageFooterId";
588
+ break;
589
+ case 1:
590
+ key = "firstPageFooterId";
591
+ pairKey = "firstPageHeaderId";
592
+ break;
593
+ case 4:
594
+ key = "evenPageHeaderId";
595
+ pairKey = "evenPageFooterId";
596
+ break;
597
+ case 5:
598
+ key = "evenPageFooterId";
599
+ pairKey = "evenPageHeaderId";
600
+ break;
601
+ default: throw new Error(`Unknown header footer type: ${createType}`);
602
+ }
603
+ const linkedSegmentIds = [[key, firstSegmentId]];
604
+ if (createMode === "pair" && pairKey != null) {
605
+ const secondSegmentId = generateRandomId(ID_LEN);
606
+ const insertPairAction = jsonX.insertOp([isHeader ? "footers" : "headers", secondSegmentId], {
607
+ [isHeader ? "footerId" : "headerId"]: secondSegmentId,
608
+ body: getEmptyHeaderFooterBody()
609
+ });
610
+ actions.push(insertPairAction);
611
+ linkedSegmentIds.push([pairKey, secondSegmentId]);
612
+ }
613
+ for (const [k, id] of linkedSegmentIds) if (documentStyle[k] != null) {
614
+ const replaceAction = jsonX.replaceOp(["documentStyle", k], documentStyle[k], id);
615
+ actions.push(replaceAction);
616
+ } else {
617
+ const insertAction = jsonX.insertOp(["documentStyle", k], id);
618
+ actions.push(insertAction);
619
+ }
620
+ return actions;
621
+ }
622
+ const CreateHeaderFooterCommand = {
623
+ id: "doc.command.create-header-footer",
624
+ type: CommandType.COMMAND,
625
+ handler: (accessor, params) => {
626
+ const commandService = accessor.get(ICommandService);
627
+ const univerInstanceService = accessor.get(IUniverInstanceService);
628
+ const { unitId, segmentId, createType, headerFooterProps, createMode = "single" } = params;
629
+ const docDataModel = univerInstanceService.getUnit(unitId, UniverInstanceType.UNIVER_DOC);
630
+ if (docDataModel == null) return false;
631
+ const { documentStyle } = docDataModel.getSnapshot();
632
+ if (documentStyle.documentFlavor === DocumentFlavor.MODERN) return false;
633
+ const rawActions = [];
634
+ const jsonX = JSONX.getInstance();
635
+ if (createType != null) createHeaderFooterAction(segmentId, createType, documentStyle, rawActions, createMode);
636
+ if (headerFooterProps != null) Object.keys(headerFooterProps).forEach((key) => {
637
+ const value = headerFooterProps[key];
638
+ const oldValue = documentStyle[key];
639
+ if (value === oldValue) return;
640
+ const action = oldValue === void 0 ? jsonX.insertOp(["documentStyle", key], value) : jsonX.replaceOp(["documentStyle", key], oldValue, value);
641
+ rawActions.push(action);
642
+ });
643
+ if (rawActions.length === 0) return false;
644
+ const doMutation = {
645
+ id: RichTextEditingMutation.id,
646
+ params: {
647
+ unitId,
648
+ actions: rawActions.reduce((acc, cur) => JSONX.compose(acc, cur), null),
649
+ textRanges: [{
650
+ startOffset: 0,
651
+ endOffset: 0,
652
+ collapsed: true
653
+ }],
654
+ debounce: true
655
+ }
656
+ };
657
+ 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;
658
+ const result = commandService.syncExecuteCommand(doMutation.id, doMutation.params);
659
+ return Boolean(result);
660
+ }
661
+ };
662
+
532
663
  //#endregion
533
664
  //#region package.json
534
665
  var name = "@univerjs/docs";
535
- var version = "1.0.0-alpha.0";
666
+ var version = "1.0.0-alpha.2";
536
667
 
537
668
  //#endregion
538
669
  //#region src/commands/mutations/docs-rename.mutation.ts
@@ -621,6 +752,33 @@ DocCustomRangeController = __decorate([
621
752
  __decorateParam(2, IUniverInstanceService)
622
753
  ], DocCustomRangeController);
623
754
 
755
+ //#endregion
756
+ //#region src/services/doc-block-move-validator.service.ts
757
+ var DocBlockMoveValidatorService = class extends Disposable {
758
+ constructor(..._args) {
759
+ super(..._args);
760
+ _defineProperty(this, "_validators", []);
761
+ _defineProperty(this, "_transformers", []);
762
+ }
763
+ registerValidator(validator) {
764
+ this._validators.push(validator);
765
+ return this.disposeWithMe(toDisposable(() => remove(this._validators, validator)));
766
+ }
767
+ registerTransformer(transformer) {
768
+ this._transformers.push(transformer);
769
+ return this.disposeWithMe(toDisposable(() => remove(this._transformers, transformer)));
770
+ }
771
+ canMoveBlock(context) {
772
+ return this._validators.every((validator) => validator(context));
773
+ }
774
+ transformMoveResult(context) {
775
+ return this._transformers.reduce((result, transformer) => transformer({
776
+ ...context,
777
+ result
778
+ }), context.result);
779
+ }
780
+ };
781
+
624
782
  //#endregion
625
783
  //#region src/services/doc-content-insert.service.ts
626
784
  /**
@@ -697,7 +855,7 @@ let DocStateChangeManagerService = class DocStateChangeManagerService extends Rx
697
855
  _initialize() {
698
856
  this.disposeWithMe(this._commandService.beforeCommandExecuted((command) => {
699
857
  if (command.id === UndoCommandId || command.id === RedoCommandId) {
700
- const univerDoc = this._univerInstanceService.getCurrentUniverDocInstance();
858
+ const univerDoc = this._univerInstanceService.getCurrentUnitOfType(UniverInstanceType.UNIVER_DOC);
701
859
  if (univerDoc == null) return;
702
860
  const unitId = univerDoc.getUnitId();
703
861
  this._pushHistory(unitId);
@@ -827,6 +985,7 @@ let UniverDocsPlugin = class UniverDocsPlugin extends Plugin {
827
985
  InsertTextCommand,
828
986
  DeleteTextCommand,
829
987
  UpdateTextCommand,
988
+ CreateHeaderFooterCommand,
830
989
  RichTextEditingMutation,
831
990
  DocsRenameMutation,
832
991
  SetTextSelectionsOperation
@@ -839,6 +998,7 @@ let UniverDocsPlugin = class UniverDocsPlugin extends Plugin {
839
998
  [DocSelectionManagerService],
840
999
  [DocStateEmitService],
841
1000
  [DocStateChangeManagerService],
1001
+ [DocBlockMoveValidatorService],
842
1002
  [DocContentInsertService],
843
1003
  [DocCustomRangeController]
844
1004
  ].forEach((d) => this._injector.add(d));
@@ -955,7 +1115,7 @@ function addCustomRangeFactory(accessor, param, body) {
955
1115
  return doMutation;
956
1116
  }
957
1117
  function addCustomRangeBySelectionFactory(accessor, param) {
958
- var _selections$;
1118
+ var _selections$, _documentDataModel$ge;
959
1119
  const { rangeId, rangeType, wholeEntity, properties, unitId, selections: propSelection } = param;
960
1120
  const docSelectionManagerService = accessor.get(DocSelectionManagerService);
961
1121
  const univerInstanceService = accessor.get(IUniverInstanceService);
@@ -967,7 +1127,7 @@ function addCustomRangeBySelectionFactory(accessor, param) {
967
1127
  if (!(selections === null || selections === void 0 ? void 0 : selections.length)) return false;
968
1128
  const documentDataModel = univerInstanceService.getUnit(unitId, UniverInstanceType.UNIVER_DOC);
969
1129
  if (!documentDataModel) return false;
970
- const body = documentDataModel.getSelfOrHeaderFooterModel(segmentId).getBody();
1130
+ const body = (_documentDataModel$ge = documentDataModel.getSelfOrHeaderFooterModel(segmentId)) === null || _documentDataModel$ge === void 0 ? void 0 : _documentDataModel$ge.getBody();
971
1131
  if (!body) return false;
972
1132
  const textX = BuildTextUtils.customRange.add({
973
1133
  ranges: selections,
@@ -1064,4 +1224,17 @@ function replaceSelectionFactory(accessor, params) {
1064
1224
  }
1065
1225
 
1066
1226
  //#endregion
1067
- export { DOC_INTERCEPTOR_POINT, DeleteTextCommand, DocContentInsertService, DocInterceptorService, DocSelectionManagerService, DocSkeletonManagerService, DocStateChangeManagerService, DocStateEmitService, IDocStateChangeInterceptorService, InsertTextCommand, RichTextEditingMutation, SetTextSelectionsOperation, UniverDocsPlugin, UpdateTextCommand, addCustomRangeBySelectionFactory, addCustomRangeFactory, deleteCustomRangeFactory, replaceSelectionFactory };
1227
+ //#region src/utils/util.ts
1228
+ function consumeContentInsertRange(accessor, unitId) {
1229
+ try {
1230
+ return accessor.get(DocContentInsertService).consumeInsertRange(unitId);
1231
+ } catch {
1232
+ return null;
1233
+ }
1234
+ }
1235
+ function isHeaderFooterSelection(range) {
1236
+ return Boolean(range === null || range === void 0 ? void 0 : range.segmentId);
1237
+ }
1238
+
1239
+ //#endregion
1240
+ export { CreateHeaderFooterCommand, DOC_INTERCEPTOR_POINT, DeleteTextCommand, DocBlockMoveValidatorService, DocContentInsertService, DocInterceptorService, DocSelectionManagerService, DocSkeletonManagerService, DocStateChangeManagerService, DocStateEmitService, HeaderFooterType, IDocStateChangeInterceptorService, InsertTextCommand, RichTextEditingMutation, SetTextSelectionsOperation, UniverDocsPlugin, UpdateTextCommand, addCustomRangeBySelectionFactory, addCustomRangeFactory, consumeContentInsertRange, deleteCustomRangeFactory, isHeaderFooterSelection, replaceSelectionFactory };