@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/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.1";
666
+ var version = "1.0.0-alpha.2";
536
667
 
537
668
  //#endregion
538
669
  //#region src/commands/mutations/docs-rename.mutation.ts
@@ -724,7 +855,7 @@ let DocStateChangeManagerService = class DocStateChangeManagerService extends Rx
724
855
  _initialize() {
725
856
  this.disposeWithMe(this._commandService.beforeCommandExecuted((command) => {
726
857
  if (command.id === UndoCommandId || command.id === RedoCommandId) {
727
- const univerDoc = this._univerInstanceService.getCurrentUniverDocInstance();
858
+ const univerDoc = this._univerInstanceService.getCurrentUnitOfType(UniverInstanceType.UNIVER_DOC);
728
859
  if (univerDoc == null) return;
729
860
  const unitId = univerDoc.getUnitId();
730
861
  this._pushHistory(unitId);
@@ -854,6 +985,7 @@ let UniverDocsPlugin = class UniverDocsPlugin extends Plugin {
854
985
  InsertTextCommand,
855
986
  DeleteTextCommand,
856
987
  UpdateTextCommand,
988
+ CreateHeaderFooterCommand,
857
989
  RichTextEditingMutation,
858
990
  DocsRenameMutation,
859
991
  SetTextSelectionsOperation
@@ -983,7 +1115,7 @@ function addCustomRangeFactory(accessor, param, body) {
983
1115
  return doMutation;
984
1116
  }
985
1117
  function addCustomRangeBySelectionFactory(accessor, param) {
986
- var _selections$;
1118
+ var _selections$, _documentDataModel$ge;
987
1119
  const { rangeId, rangeType, wholeEntity, properties, unitId, selections: propSelection } = param;
988
1120
  const docSelectionManagerService = accessor.get(DocSelectionManagerService);
989
1121
  const univerInstanceService = accessor.get(IUniverInstanceService);
@@ -995,7 +1127,7 @@ function addCustomRangeBySelectionFactory(accessor, param) {
995
1127
  if (!(selections === null || selections === void 0 ? void 0 : selections.length)) return false;
996
1128
  const documentDataModel = univerInstanceService.getUnit(unitId, UniverInstanceType.UNIVER_DOC);
997
1129
  if (!documentDataModel) return false;
998
- const body = documentDataModel.getSelfOrHeaderFooterModel(segmentId).getBody();
1130
+ const body = (_documentDataModel$ge = documentDataModel.getSelfOrHeaderFooterModel(segmentId)) === null || _documentDataModel$ge === void 0 ? void 0 : _documentDataModel$ge.getBody();
999
1131
  if (!body) return false;
1000
1132
  const textX = BuildTextUtils.customRange.add({
1001
1133
  ranges: selections,
@@ -1092,4 +1224,17 @@ function replaceSelectionFactory(accessor, params) {
1092
1224
  }
1093
1225
 
1094
1226
  //#endregion
1095
- export { DOC_INTERCEPTOR_POINT, DeleteTextCommand, DocBlockMoveValidatorService, 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 };