@univerjs/docs 1.0.0-beta.1 → 1.0.0-beta.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/facade.js CHANGED
@@ -33,31 +33,24 @@ function buildPlainTextInsertBody(dataStream, options = {}) {
33
33
  if (paragraphs.length > 0) body.paragraphs = paragraphs;
34
34
  return body;
35
35
  }
36
- function replaceBodyRange(range, insertBody, docDataModel, injector) {
36
+ function replaceBodyRange(range, insertBody, docDataModel, commandService) {
37
37
  const { startOffset, endOffset, segmentId } = range;
38
- const textX = new _univerjs_core.TextX();
39
- if (startOffset > 0) textX.push({
40
- t: _univerjs_core.TextXActionType.RETAIN,
41
- len: startOffset
42
- });
43
- if (endOffset > startOffset) textX.push({
44
- t: _univerjs_core.TextXActionType.DELETE,
45
- len: endOffset - startOffset
46
- });
47
- if (insertBody.dataStream.length > 0) textX.push({
48
- t: _univerjs_core.TextXActionType.INSERT,
49
- body: insertBody,
50
- len: insertBody.dataStream.length
51
- });
52
- const actions = _univerjs_core.JSONX.getInstance().editOp(textX.serialize(), (0, _univerjs_core.getRichTextEditPath)(docDataModel, segmentId));
53
- const result = injector.get(_univerjs_core.ICommandService).syncExecuteCommand(_univerjs_docs.RichTextEditingMutation.id, {
38
+ const result = commandService.syncExecuteCommand(_univerjs_docs.InsertTextCommand.id, {
54
39
  unitId: docDataModel.getUnitId(),
40
+ body: insertBody,
41
+ range: {
42
+ startOffset,
43
+ endOffset,
44
+ collapsed: startOffset === endOffset,
45
+ segmentId
46
+ },
55
47
  segmentId,
56
- actions,
48
+ debounce: false,
57
49
  textRanges: [],
50
+ noNeedSetTextRange: true,
58
51
  isEditing: false
59
52
  });
60
- return Boolean((result === null || result === void 0 ? void 0 : result.actions) && result.actions.length > 0);
53
+ return Boolean(result);
61
54
  }
62
55
  function retainBodyRange(range, updateBody, coverType, docDataModel, injector) {
63
56
  var _updateBody$textRuns, _docDataModel$getSelf;
@@ -98,23 +91,34 @@ function stripBlockTokens(text) {
98
91
  return Array.from(text).map((char) => char === _univerjs_core.DataStreamTreeTokenType.PARAGRAPH ? "\n" : char).filter((char) => char !== _univerjs_core.DataStreamTreeTokenType.BLOCK_START && char !== _univerjs_core.DataStreamTreeTokenType.BLOCK_END && char !== _univerjs_core.DataStreamTreeTokenType.SECTION_BREAK).join("").replace(/\n$/, "");
99
92
  }
100
93
 
94
+ //#endregion
95
+ //#region \0@oxc-project+runtime@0.140.0/helpers/esm/decorateParam.js
96
+ function __decorateParam(paramIndex, decorator) {
97
+ return function(target, key) {
98
+ decorator(target, key, paramIndex);
99
+ };
100
+ }
101
+
102
+ //#endregion
103
+ //#region \0@oxc-project+runtime@0.140.0/helpers/esm/decorate.js
104
+ function __decorate(decorators, target, key, desc) {
105
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
106
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
107
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
108
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
109
+ }
110
+
101
111
  //#endregion
102
112
  //#region src/facade/f-document-text-range.ts
103
- /**
104
- * Facade wrapper for reading and styling a fixed document text range.
105
- *
106
- * Offsets are fixed when the wrapper is created. Create a new range after edits
107
- * that insert or remove content before it.
108
- * @hideconstructor
109
- */
110
- var FDocumentTextRange = class extends _univerjs_core_facade.FBaseInitialable {
111
- constructor(_document, _startOffset, _endOffset, _segmentId, _injector) {
113
+ let FDocumentTextRange = class FDocumentTextRange extends _univerjs_core_facade.FBaseInitialable {
114
+ constructor(_document, _startOffset, _endOffset, _segmentId, _injector, _commandService) {
112
115
  super(_injector);
113
116
  this._document = _document;
114
117
  this._startOffset = _startOffset;
115
118
  this._endOffset = _endOffset;
116
119
  this._segmentId = _segmentId;
117
120
  this._injector = _injector;
121
+ this._commandService = _commandService;
118
122
  this._validateRange();
119
123
  }
120
124
  /**
@@ -233,7 +237,7 @@ var FDocumentTextRange = class extends _univerjs_core_facade.FBaseInitialable {
233
237
  * ```
234
238
  */
235
239
  setText(text) {
236
- return replaceBodyRange(this.getRange(), buildPlainTextInsertBody(text), this._document.getDocumentDataModel(), this._injector);
240
+ return replaceBodyRange(this.getRange(), buildPlainTextInsertBody(text), this._document.getDocumentDataModel(), this._commandService);
237
241
  }
238
242
  _validateRange() {
239
243
  const bodyLength = this._document.getBody(this._segmentId).dataStream.length;
@@ -260,27 +264,11 @@ var FDocumentTextRange = class extends _univerjs_core_facade.FBaseInitialable {
260
264
  return segments;
261
265
  }
262
266
  };
267
+ FDocumentTextRange = __decorate([__decorateParam(5, _univerjs_core.ICommandService)], FDocumentTextRange);
263
268
  function isDeepEqual(left, right) {
264
269
  return JSON.stringify(left) === JSON.stringify(right);
265
270
  }
266
271
 
267
- //#endregion
268
- //#region \0@oxc-project+runtime@0.140.0/helpers/esm/decorateParam.js
269
- function __decorateParam(paramIndex, decorator) {
270
- return function(target, key) {
271
- decorator(target, key, paramIndex);
272
- };
273
- }
274
-
275
- //#endregion
276
- //#region \0@oxc-project+runtime@0.140.0/helpers/esm/decorate.js
277
- function __decorate(decorators, target, key, desc) {
278
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
279
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
280
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
281
- return c > 3 && r && Object.defineProperty(target, key, r), r;
282
- }
283
-
284
272
  //#endregion
285
273
  //#region src/facade/f-document-paragraph.ts
286
274
  let FDocumentParagraph = class FDocumentParagraph extends _univerjs_core_facade.FBaseInitialable {
@@ -478,7 +466,7 @@ let FDocumentParagraph = class FDocumentParagraph extends _univerjs_core_facade.
478
466
  startOffset,
479
467
  endOffset,
480
468
  segmentId: this._segmentId
481
- }, buildPlainTextInsertBody(text), this._document.getDocumentDataModel(), this._injector);
469
+ }, buildPlainTextInsertBody(text), this._document.getDocumentDataModel(), this._commandService);
482
470
  }
483
471
  /**
484
472
  * Append plain text before this paragraph's trailing paragraph break.
@@ -1510,7 +1498,7 @@ let FDocument = class FDocument extends _univerjs_core_facade.FBaseInitialable {
1510
1498
  startOffset: index,
1511
1499
  endOffset: index,
1512
1500
  segmentId
1513
- }, buildPlainTextInsertBody(text), this._documentDataModel, this._injector);
1501
+ }, buildPlainTextInsertBody(text), this._documentDataModel, this._commandService);
1514
1502
  }
1515
1503
  /**
1516
1504
  * Returns document-level header/footer switches and margins. Margin values use 96-DPI layout pixels.
@@ -1731,7 +1719,7 @@ let FDocument = class FDocument extends _univerjs_core_facade.FBaseInitialable {
1731
1719
  }, {
1732
1720
  dataStream: _univerjs_core.DataStreamTreeTokenType.PARAGRAPH,
1733
1721
  paragraphs
1734
- }, this._documentDataModel, this._injector) ? this.getParagraph(paragraphId, segmentId) : null;
1722
+ }, this._documentDataModel, this._commandService) ? this.getParagraph(paragraphId, segmentId) : null;
1735
1723
  }
1736
1724
  /**
1737
1725
  * Get all paragraphs in the document body or header/footer body by the segment id.
@@ -1844,7 +1832,7 @@ let FDocument = class FDocument extends _univerjs_core_facade.FBaseInitialable {
1844
1832
  startOffset: offset,
1845
1833
  endOffset: offset,
1846
1834
  segmentId
1847
- }, buildPlainTextInsertBody(`${text}\r`), this._documentDataModel, this._injector)) throw new Error("Failed to insert paragraph.");
1835
+ }, buildPlainTextInsertBody(`${text}\r`), this._documentDataModel, this._commandService)) throw new Error("Failed to insert paragraph.");
1848
1836
  const { paragraphs = [] } = this.getBody(segmentId);
1849
1837
  const paragraph = paragraphs[index];
1850
1838
  if (!paragraph) throw new Error("Failed to insert paragraph.");
@@ -1886,7 +1874,7 @@ let FDocument = class FDocument extends _univerjs_core_facade.FBaseInitialable {
1886
1874
  deleteRange(range) {
1887
1875
  const normalizedRange = this._normalizeDeleteRange(range);
1888
1876
  if (normalizedRange.startOffset >= normalizedRange.endOffset) return false;
1889
- return replaceBodyRange(normalizedRange, { dataStream: "" }, this._documentDataModel, this._injector);
1877
+ return replaceBodyRange(normalizedRange, { dataStream: "" }, this._documentDataModel, this._commandService);
1890
1878
  }
1891
1879
  _createFDocumentParagraph(paragraphId, segmentId = "") {
1892
1880
  return this._injector.createInstance(FDocumentParagraph, this, paragraphId, segmentId, this._injector);
@@ -2026,6 +2014,11 @@ Object.defineProperty(exports, 'FDocumentSection', {
2026
2014
  return FDocumentSection;
2027
2015
  }
2028
2016
  });
2029
- exports.FDocumentTextRange = FDocumentTextRange;
2017
+ Object.defineProperty(exports, 'FDocumentTextRange', {
2018
+ enumerable: true,
2019
+ get: function () {
2020
+ return FDocumentTextRange;
2021
+ }
2022
+ });
2030
2023
  exports.isParagraphFacade = isParagraphFacade;
2031
2024
  exports.stripBlockTokens = stripBlockTokens;
package/lib/cjs/index.js CHANGED
@@ -414,59 +414,63 @@ const InsertTextCommand = {
414
414
  handler: (accessor, params) => {
415
415
  var _docDataModel$getSelf;
416
416
  const commandService = accessor.get(_univerjs_core.ICommandService);
417
- const { range, segmentId, body, unitId, cursorOffset } = params;
417
+ const { range, segmentId, body, unitId, cursorOffset, debounce = true, textRanges, noNeedSetTextRange, isEditing } = params;
418
418
  const docSelectionManagerService = accessor.get(DocSelectionManagerService);
419
419
  const docDataModel = accessor.get(_univerjs_core.IUniverInstanceService).getUnit(unitId, _univerjs_core.UniverInstanceType.UNIVER_DOC);
420
420
  if (docDataModel == null) return false;
421
421
  const activeRange = docSelectionManagerService.getActiveTextRange();
422
- const rangeSegmentId = "segmentId" in range ? range.segmentId : void 0;
422
+ const rangeSegmentId = "segmentId" in range && typeof range.segmentId === "string" ? range.segmentId : void 0;
423
423
  const targetSegmentId = segmentId ?? rangeSegmentId ?? (activeRange === null || activeRange === void 0 ? void 0 : activeRange.segmentId) ?? "";
424
424
  const originBody = (_docDataModel$getSelf = docDataModel.getSelfOrHeaderFooterModel(targetSegmentId)) === null || _docDataModel$getSelf === void 0 ? void 0 : _docDataModel$getSelf.getBody();
425
425
  if (originBody == null) return false;
426
426
  const { startOffset, collapsed } = range;
427
427
  const cursorMove = cursorOffset ?? body.dataStream.length;
428
- const textRanges = [{
428
+ const mutationTextRanges = textRanges ?? [{
429
429
  startOffset: startOffset + cursorMove,
430
430
  endOffset: startOffset + cursorMove,
431
- style: activeRange === null || activeRange === void 0 ? void 0 : activeRange.style,
432
431
  collapsed
433
432
  }];
434
433
  const doMutation = {
435
434
  id: RichTextEditingMutation.id,
436
435
  params: {
437
436
  unitId,
437
+ segmentId: targetSegmentId,
438
438
  actions: [],
439
- textRanges,
440
- debounce: true
439
+ textRanges: mutationTextRanges,
440
+ debounce,
441
+ noNeedSetTextRange,
442
+ isEditing
441
443
  }
442
444
  };
443
- const textX = new _univerjs_core.TextX();
444
- const jsonX = _univerjs_core.JSONX.getInstance();
445
- if (collapsed) {
446
- if (startOffset > 0) textX.push({
447
- t: _univerjs_core.TextXActionType.RETAIN,
448
- len: startOffset
449
- });
450
- textX.push({
451
- t: _univerjs_core.TextXActionType.INSERT,
452
- body,
453
- len: body.dataStream.length
454
- });
455
- } else {
456
- const dos = _univerjs_core.BuildTextUtils.selection.delete([range], originBody, 0, body);
457
- textX.push(...dos);
458
- }
459
- doMutation.params.textRanges = [{
460
- startOffset: startOffset + cursorMove,
461
- endOffset: startOffset + cursorMove,
462
- collapsed
463
- }];
464
- const path = (0, _univerjs_core.getRichTextEditPath)(docDataModel, segmentId);
465
- doMutation.params.actions = jsonX.editOp(textX.serialize(), path);
445
+ doMutation.params.actions = buildInsertTextActions(docDataModel, originBody, range, body, targetSegmentId);
466
446
  const result = commandService.syncExecuteCommand(doMutation.id, doMutation.params);
467
447
  return Boolean(result);
468
448
  }
469
449
  };
450
+ function buildInsertTextActions(documentDataModel, originBody, range, insertBody, segmentId) {
451
+ const { startOffset, endOffset, collapsed } = range;
452
+ const textX = new _univerjs_core.TextX();
453
+ if (collapsed) {
454
+ if (startOffset > 0) textX.push({
455
+ t: _univerjs_core.TextXActionType.RETAIN,
456
+ len: startOffset
457
+ });
458
+ textX.push({
459
+ t: _univerjs_core.TextXActionType.INSERT,
460
+ body: insertBody,
461
+ len: insertBody.dataStream.length
462
+ });
463
+ } else textX.push(..._univerjs_core.BuildTextUtils.selection.delete([range], originBody, 0, insertBody));
464
+ const textActions = _univerjs_core.JSONX.getInstance().editOp(textX.serialize(), (0, _univerjs_core.getRichTextEditPath)(documentDataModel, segmentId));
465
+ return collapsed ? textActions : appendRemovedDrawingActions(textActions, documentDataModel, originBody, startOffset, endOffset);
466
+ }
467
+ function appendRemovedDrawingActions(textActions, documentDataModel, body, startOffset, endOffset) {
468
+ return [textActions, ..._univerjs_core.BuildTextUtils.drawing.remove(documentDataModel.getSnapshot(), [{
469
+ startOffset,
470
+ endOffset,
471
+ collapsed: false
472
+ }], body)].reduce((accumulator, action) => _univerjs_core.JSONX.compose(accumulator, action));
473
+ }
470
474
  /**
471
475
  * The command to delete text, mainly used in BACKSPACE and DELETE when collapsed is true. ONLY handle collapsed range!!!
472
476
  */
@@ -493,6 +497,7 @@ const DeleteTextCommand = {
493
497
  id: RichTextEditingMutation.id,
494
498
  params: {
495
499
  unitId,
500
+ segmentId,
496
501
  actions: [],
497
502
  textRanges: [{
498
503
  startOffset: start,
@@ -511,7 +516,7 @@ const DeleteTextCommand = {
511
516
  collapsed: false
512
517
  }], body));
513
518
  const path = (0, _univerjs_core.getRichTextEditPath)(docDataModel, segmentId);
514
- doMutation.params.actions = jsonX.editOp(textX.serialize(), path);
519
+ doMutation.params.actions = appendRemovedDrawingActions(jsonX.editOp(textX.serialize(), path), docDataModel, body, start, end + 1);
515
520
  const result = commandService.syncExecuteCommand(doMutation.id, doMutation.params);
516
521
  return Boolean(result);
517
522
  }
@@ -1439,7 +1444,7 @@ function composeActions(actions) {
1439
1444
  //#endregion
1440
1445
  //#region package.json
1441
1446
  var name = "@univerjs/docs";
1442
- var version = "1.0.0-beta.1";
1447
+ var version = "1.0.0-beta.2";
1443
1448
 
1444
1449
  //#endregion
1445
1450
  //#region src/config/config.ts
@@ -1610,6 +1615,9 @@ let DocStateChangeManagerService = class DocStateChangeManagerService extends _u
1610
1615
  this._historyStateCache.set(unitId, cache.history);
1611
1616
  this._changeStateCache.set(unitId, cache.collaboration);
1612
1617
  }
1618
+ clearHistory(unitId) {
1619
+ this._historyStateCache.delete(unitId);
1620
+ }
1613
1621
  _setChangeState(changeState) {
1614
1622
  this._cacheChangeState(changeState, "history");
1615
1623
  this._cacheChangeState(changeState, "collaboration");
package/lib/es/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, SetDocumentNameCommand, SetSectionHeaderFooterLinkCommand, UpdateDocumentParagraphStyleCommand, UpdateDocumentSectionCommand, createSectionColumnProperties, generateParagraphs, getEffectiveSectionPageSetup, getSectionContentWidth, getTopLevelSectionBreaks } from "@univerjs/docs";
3
+ import { CreateHeaderFooterCommand, DeleteDocumentSectionBreakCommand, HeaderFooterType, InsertDocumentColumnBreakCommand, InsertDocumentSectionBreakCommand, InsertTextCommand, 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) {
@@ -32,31 +32,24 @@ function buildPlainTextInsertBody(dataStream, options = {}) {
32
32
  if (paragraphs.length > 0) body.paragraphs = paragraphs;
33
33
  return body;
34
34
  }
35
- function replaceBodyRange(range, insertBody, docDataModel, injector) {
35
+ function replaceBodyRange(range, insertBody, docDataModel, commandService) {
36
36
  const { startOffset, endOffset, segmentId } = range;
37
- const textX = new TextX();
38
- if (startOffset > 0) textX.push({
39
- t: TextXActionType.RETAIN,
40
- len: startOffset
41
- });
42
- if (endOffset > startOffset) textX.push({
43
- t: TextXActionType.DELETE,
44
- len: endOffset - startOffset
45
- });
46
- if (insertBody.dataStream.length > 0) textX.push({
47
- t: TextXActionType.INSERT,
48
- body: insertBody,
49
- len: insertBody.dataStream.length
50
- });
51
- const actions = JSONX.getInstance().editOp(textX.serialize(), getRichTextEditPath(docDataModel, segmentId));
52
- const result = injector.get(ICommandService).syncExecuteCommand(RichTextEditingMutation.id, {
37
+ const result = commandService.syncExecuteCommand(InsertTextCommand.id, {
53
38
  unitId: docDataModel.getUnitId(),
39
+ body: insertBody,
40
+ range: {
41
+ startOffset,
42
+ endOffset,
43
+ collapsed: startOffset === endOffset,
44
+ segmentId
45
+ },
54
46
  segmentId,
55
- actions,
47
+ debounce: false,
56
48
  textRanges: [],
49
+ noNeedSetTextRange: true,
57
50
  isEditing: false
58
51
  });
59
- return Boolean((result === null || result === void 0 ? void 0 : result.actions) && result.actions.length > 0);
52
+ return Boolean(result);
60
53
  }
61
54
  function retainBodyRange(range, updateBody, coverType, docDataModel, injector) {
62
55
  var _updateBody$textRuns, _docDataModel$getSelf;
@@ -97,23 +90,34 @@ function stripBlockTokens(text) {
97
90
  return Array.from(text).map((char) => char === DataStreamTreeTokenType.PARAGRAPH ? "\n" : char).filter((char) => char !== DataStreamTreeTokenType.BLOCK_START && char !== DataStreamTreeTokenType.BLOCK_END && char !== DataStreamTreeTokenType.SECTION_BREAK).join("").replace(/\n$/, "");
98
91
  }
99
92
 
93
+ //#endregion
94
+ //#region \0@oxc-project+runtime@0.140.0/helpers/esm/decorateParam.js
95
+ function __decorateParam(paramIndex, decorator) {
96
+ return function(target, key) {
97
+ decorator(target, key, paramIndex);
98
+ };
99
+ }
100
+
101
+ //#endregion
102
+ //#region \0@oxc-project+runtime@0.140.0/helpers/esm/decorate.js
103
+ function __decorate(decorators, target, key, desc) {
104
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
105
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
106
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
107
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
108
+ }
109
+
100
110
  //#endregion
101
111
  //#region src/facade/f-document-text-range.ts
102
- /**
103
- * Facade wrapper for reading and styling a fixed document text range.
104
- *
105
- * Offsets are fixed when the wrapper is created. Create a new range after edits
106
- * that insert or remove content before it.
107
- * @hideconstructor
108
- */
109
- var FDocumentTextRange = class extends FBaseInitialable {
110
- constructor(_document, _startOffset, _endOffset, _segmentId, _injector) {
112
+ let FDocumentTextRange = class FDocumentTextRange extends FBaseInitialable {
113
+ constructor(_document, _startOffset, _endOffset, _segmentId, _injector, _commandService) {
111
114
  super(_injector);
112
115
  this._document = _document;
113
116
  this._startOffset = _startOffset;
114
117
  this._endOffset = _endOffset;
115
118
  this._segmentId = _segmentId;
116
119
  this._injector = _injector;
120
+ this._commandService = _commandService;
117
121
  this._validateRange();
118
122
  }
119
123
  /**
@@ -232,7 +236,7 @@ var FDocumentTextRange = class extends FBaseInitialable {
232
236
  * ```
233
237
  */
234
238
  setText(text) {
235
- return replaceBodyRange(this.getRange(), buildPlainTextInsertBody(text), this._document.getDocumentDataModel(), this._injector);
239
+ return replaceBodyRange(this.getRange(), buildPlainTextInsertBody(text), this._document.getDocumentDataModel(), this._commandService);
236
240
  }
237
241
  _validateRange() {
238
242
  const bodyLength = this._document.getBody(this._segmentId).dataStream.length;
@@ -259,27 +263,11 @@ var FDocumentTextRange = class extends FBaseInitialable {
259
263
  return segments;
260
264
  }
261
265
  };
266
+ FDocumentTextRange = __decorate([__decorateParam(5, ICommandService)], FDocumentTextRange);
262
267
  function isDeepEqual(left, right) {
263
268
  return JSON.stringify(left) === JSON.stringify(right);
264
269
  }
265
270
 
266
- //#endregion
267
- //#region \0@oxc-project+runtime@0.140.0/helpers/esm/decorateParam.js
268
- function __decorateParam(paramIndex, decorator) {
269
- return function(target, key) {
270
- decorator(target, key, paramIndex);
271
- };
272
- }
273
-
274
- //#endregion
275
- //#region \0@oxc-project+runtime@0.140.0/helpers/esm/decorate.js
276
- function __decorate(decorators, target, key, desc) {
277
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
278
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
279
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
280
- return c > 3 && r && Object.defineProperty(target, key, r), r;
281
- }
282
-
283
271
  //#endregion
284
272
  //#region src/facade/f-document-paragraph.ts
285
273
  let FDocumentParagraph = class FDocumentParagraph extends FBaseInitialable {
@@ -477,7 +465,7 @@ let FDocumentParagraph = class FDocumentParagraph extends FBaseInitialable {
477
465
  startOffset,
478
466
  endOffset,
479
467
  segmentId: this._segmentId
480
- }, buildPlainTextInsertBody(text), this._document.getDocumentDataModel(), this._injector);
468
+ }, buildPlainTextInsertBody(text), this._document.getDocumentDataModel(), this._commandService);
481
469
  }
482
470
  /**
483
471
  * Append plain text before this paragraph's trailing paragraph break.
@@ -1509,7 +1497,7 @@ let FDocument = class FDocument extends FBaseInitialable {
1509
1497
  startOffset: index,
1510
1498
  endOffset: index,
1511
1499
  segmentId
1512
- }, buildPlainTextInsertBody(text), this._documentDataModel, this._injector);
1500
+ }, buildPlainTextInsertBody(text), this._documentDataModel, this._commandService);
1513
1501
  }
1514
1502
  /**
1515
1503
  * Returns document-level header/footer switches and margins. Margin values use 96-DPI layout pixels.
@@ -1730,7 +1718,7 @@ let FDocument = class FDocument extends FBaseInitialable {
1730
1718
  }, {
1731
1719
  dataStream: DataStreamTreeTokenType.PARAGRAPH,
1732
1720
  paragraphs
1733
- }, this._documentDataModel, this._injector) ? this.getParagraph(paragraphId, segmentId) : null;
1721
+ }, this._documentDataModel, this._commandService) ? this.getParagraph(paragraphId, segmentId) : null;
1734
1722
  }
1735
1723
  /**
1736
1724
  * Get all paragraphs in the document body or header/footer body by the segment id.
@@ -1843,7 +1831,7 @@ let FDocument = class FDocument extends FBaseInitialable {
1843
1831
  startOffset: offset,
1844
1832
  endOffset: offset,
1845
1833
  segmentId
1846
- }, buildPlainTextInsertBody(`${text}\r`), this._documentDataModel, this._injector)) throw new Error("Failed to insert paragraph.");
1834
+ }, buildPlainTextInsertBody(`${text}\r`), this._documentDataModel, this._commandService)) throw new Error("Failed to insert paragraph.");
1847
1835
  const { paragraphs = [] } = this.getBody(segmentId);
1848
1836
  const paragraph = paragraphs[index];
1849
1837
  if (!paragraph) throw new Error("Failed to insert paragraph.");
@@ -1885,7 +1873,7 @@ let FDocument = class FDocument extends FBaseInitialable {
1885
1873
  deleteRange(range) {
1886
1874
  const normalizedRange = this._normalizeDeleteRange(range);
1887
1875
  if (normalizedRange.startOffset >= normalizedRange.endOffset) return false;
1888
- return replaceBodyRange(normalizedRange, { dataStream: "" }, this._documentDataModel, this._injector);
1876
+ return replaceBodyRange(normalizedRange, { dataStream: "" }, this._documentDataModel, this._commandService);
1889
1877
  }
1890
1878
  _createFDocumentParagraph(paragraphId, segmentId = "") {
1891
1879
  return this._injector.createInstance(FDocumentParagraph, this, paragraphId, segmentId, this._injector);
package/lib/es/index.js CHANGED
@@ -413,59 +413,63 @@ const InsertTextCommand = {
413
413
  handler: (accessor, params) => {
414
414
  var _docDataModel$getSelf;
415
415
  const commandService = accessor.get(ICommandService);
416
- const { range, segmentId, body, unitId, cursorOffset } = params;
416
+ const { range, segmentId, body, unitId, cursorOffset, debounce = true, textRanges, noNeedSetTextRange, isEditing } = params;
417
417
  const docSelectionManagerService = accessor.get(DocSelectionManagerService);
418
418
  const docDataModel = accessor.get(IUniverInstanceService).getUnit(unitId, UniverInstanceType.UNIVER_DOC);
419
419
  if (docDataModel == null) return false;
420
420
  const activeRange = docSelectionManagerService.getActiveTextRange();
421
- const rangeSegmentId = "segmentId" in range ? range.segmentId : void 0;
421
+ const rangeSegmentId = "segmentId" in range && typeof range.segmentId === "string" ? range.segmentId : void 0;
422
422
  const targetSegmentId = segmentId ?? rangeSegmentId ?? (activeRange === null || activeRange === void 0 ? void 0 : activeRange.segmentId) ?? "";
423
423
  const originBody = (_docDataModel$getSelf = docDataModel.getSelfOrHeaderFooterModel(targetSegmentId)) === null || _docDataModel$getSelf === void 0 ? void 0 : _docDataModel$getSelf.getBody();
424
424
  if (originBody == null) return false;
425
425
  const { startOffset, collapsed } = range;
426
426
  const cursorMove = cursorOffset ?? body.dataStream.length;
427
- const textRanges = [{
427
+ const mutationTextRanges = textRanges ?? [{
428
428
  startOffset: startOffset + cursorMove,
429
429
  endOffset: startOffset + cursorMove,
430
- style: activeRange === null || activeRange === void 0 ? void 0 : activeRange.style,
431
430
  collapsed
432
431
  }];
433
432
  const doMutation = {
434
433
  id: RichTextEditingMutation.id,
435
434
  params: {
436
435
  unitId,
436
+ segmentId: targetSegmentId,
437
437
  actions: [],
438
- textRanges,
439
- debounce: true
438
+ textRanges: mutationTextRanges,
439
+ debounce,
440
+ noNeedSetTextRange,
441
+ isEditing
440
442
  }
441
443
  };
442
- const textX = new TextX();
443
- const jsonX = JSONX.getInstance();
444
- if (collapsed) {
445
- if (startOffset > 0) textX.push({
446
- t: TextXActionType.RETAIN,
447
- len: startOffset
448
- });
449
- textX.push({
450
- t: TextXActionType.INSERT,
451
- body,
452
- len: body.dataStream.length
453
- });
454
- } else {
455
- const dos = BuildTextUtils.selection.delete([range], originBody, 0, body);
456
- textX.push(...dos);
457
- }
458
- doMutation.params.textRanges = [{
459
- startOffset: startOffset + cursorMove,
460
- endOffset: startOffset + cursorMove,
461
- collapsed
462
- }];
463
- const path = getRichTextEditPath(docDataModel, segmentId);
464
- doMutation.params.actions = jsonX.editOp(textX.serialize(), path);
444
+ doMutation.params.actions = buildInsertTextActions(docDataModel, originBody, range, body, targetSegmentId);
465
445
  const result = commandService.syncExecuteCommand(doMutation.id, doMutation.params);
466
446
  return Boolean(result);
467
447
  }
468
448
  };
449
+ function buildInsertTextActions(documentDataModel, originBody, range, insertBody, segmentId) {
450
+ const { startOffset, endOffset, collapsed } = range;
451
+ const textX = new TextX();
452
+ if (collapsed) {
453
+ if (startOffset > 0) textX.push({
454
+ t: TextXActionType.RETAIN,
455
+ len: startOffset
456
+ });
457
+ textX.push({
458
+ t: TextXActionType.INSERT,
459
+ body: insertBody,
460
+ len: insertBody.dataStream.length
461
+ });
462
+ } else textX.push(...BuildTextUtils.selection.delete([range], originBody, 0, insertBody));
463
+ const textActions = JSONX.getInstance().editOp(textX.serialize(), getRichTextEditPath(documentDataModel, segmentId));
464
+ return collapsed ? textActions : appendRemovedDrawingActions(textActions, documentDataModel, originBody, startOffset, endOffset);
465
+ }
466
+ function appendRemovedDrawingActions(textActions, documentDataModel, body, startOffset, endOffset) {
467
+ return [textActions, ...BuildTextUtils.drawing.remove(documentDataModel.getSnapshot(), [{
468
+ startOffset,
469
+ endOffset,
470
+ collapsed: false
471
+ }], body)].reduce((accumulator, action) => JSONX.compose(accumulator, action));
472
+ }
469
473
  /**
470
474
  * The command to delete text, mainly used in BACKSPACE and DELETE when collapsed is true. ONLY handle collapsed range!!!
471
475
  */
@@ -492,6 +496,7 @@ const DeleteTextCommand = {
492
496
  id: RichTextEditingMutation.id,
493
497
  params: {
494
498
  unitId,
499
+ segmentId,
495
500
  actions: [],
496
501
  textRanges: [{
497
502
  startOffset: start,
@@ -510,7 +515,7 @@ const DeleteTextCommand = {
510
515
  collapsed: false
511
516
  }], body));
512
517
  const path = getRichTextEditPath(docDataModel, segmentId);
513
- doMutation.params.actions = jsonX.editOp(textX.serialize(), path);
518
+ doMutation.params.actions = appendRemovedDrawingActions(jsonX.editOp(textX.serialize(), path), docDataModel, body, start, end + 1);
514
519
  const result = commandService.syncExecuteCommand(doMutation.id, doMutation.params);
515
520
  return Boolean(result);
516
521
  }
@@ -1438,7 +1443,7 @@ function composeActions(actions) {
1438
1443
  //#endregion
1439
1444
  //#region package.json
1440
1445
  var name = "@univerjs/docs";
1441
- var version = "1.0.0-beta.1";
1446
+ var version = "1.0.0-beta.2";
1442
1447
 
1443
1448
  //#endregion
1444
1449
  //#region src/config/config.ts
@@ -1609,6 +1614,9 @@ let DocStateChangeManagerService = class DocStateChangeManagerService extends Rx
1609
1614
  this._historyStateCache.set(unitId, cache.history);
1610
1615
  this._changeStateCache.set(unitId, cache.collaboration);
1611
1616
  }
1617
+ clearHistory(unitId) {
1618
+ this._historyStateCache.delete(unitId);
1619
+ }
1612
1620
  _setChangeState(changeState) {
1613
1621
  this._cacheChangeState(changeState, "history");
1614
1622
  this._cacheChangeState(changeState, "collaboration");