Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
refactor: compose actions (#1274)
Ran Luob80ec0d5ad17
diff --git a/packages/core/src/docs/data-model/mutation-types.ts b/packages/core/src/docs/data-model/action-types.tssimilarity index 89%rename from packages/core/src/docs/data-model/mutation-types.tsrename to packages/core/src/docs/data-model/action-types.tsindex af4a3309de..55a7cd4d56 100644--- a/packages/core/src/docs/data-model/mutation-types.ts+++ b/packages/core/src/docs/data-model/action-types.ts@@ -17,11 +17,17 @@import type { UpdateDocsAttributeType } from '../../shared/command-enum';import type { IDocumentBody } from '../../types/interfaces/i-document-data';+export enum TextXActionType {+ RETAIN = 'r',+ INSERT = 'i',+ DELETE = 'd',+}+/*** Retain mutation is used to move the cursor or to update properties of the text in the given range.*/export interface IRetainAction {- t: 'r';+ t: TextXActionType.RETAIN;len: number;body?: IDocumentBody;coverType?: UpdateDocsAttributeType;@@ -32,7 +38,7 @@ export interface IRetainAction {* Insert mutation is used to insert text (maybe with rich text properties) at the given position.*/export interface IInsertAction {- t: 'i';+ t: TextXActionType.INSERT;body: IDocumentBody;len: number;line: number;@@ -43,7 +49,7 @@ export interface IInsertAction {* Delete mutation is used to delete text at the given position.*/export interface IDeleteAction {- t: 'd';+ t: TextXActionType.DELETE;len: number;line: number;segmentId?: string;diff --git a/packages/core/src/docs/data-model/apply-utils/common.ts b/packages/core/src/docs/data-model/apply-utils/common.tsindex 33cfb36c73..c8a19edbb8 100644--- a/packages/core/src/docs/data-model/apply-utils/common.ts+++ b/packages/core/src/docs/data-model/apply-utils/common.ts@@ -34,6 +34,10 @@ export function normalizeTextRuns(textRuns: ITextRun[]) {for (const textRun of textRuns) {const { st, ed } = textRun;+ if (textRun.sId === undefined) {+ delete textRun.sId;+ }+if (st === ed) {continue;}diff --git a/packages/core/src/docs/data-model/document-data-model.ts b/packages/core/src/docs/data-model/document-data-model.tsindex 752785640a..918b99f36f 100644--- a/packages/core/src/docs/data-model/document-data-model.ts+++ b/packages/core/src/docs/data-model/document-data-model.ts@@ -23,14 +23,13 @@ import type {IDocumentBody,IDocumentData,IDocumentRenderConfig,- IParagraph,- ITextRun,} from '../../types/interfaces/i-document-data';import type { IPaddingData } from '../../types/interfaces/i-style-data';import { updateAttributeByDelete } from './apply-utils/delete-apply';import { updateAttributeByInsert } from './apply-utils/insert-apply';import { updateAttribute } from './apply-utils/update-apply';-import type { TextXAction } from './mutation-types';+import { type TextXAction, TextXActionType } from './action-types';+import { getBodySlice } from './text-x/utils';export const DEFAULT_DOC = {id: 'default_doc',@@ -254,61 +253,61 @@ export class DocumentDataModel extends DocumentDataModelSimple {return this._unitId;}- apply(mutations: TextXAction[]) {+ apply(actions: TextXAction[]) {const undoMutations: TextXAction[] = [];const memoryCursor = new MemoryCursor();memoryCursor.reset();- mutations.forEach((mutation) => {- // FIXME: @JOCS Since updateApply modifies the mutation(used in undo/redo),+ actions.forEach((action) => {+ // FIXME: @JOCS Since updateApply modifies the action(used in undo/redo),// so make a deep copy here, does updateApply need to// be modified to have no side effects in the future?- mutation = Tools.deepClone(mutation);+ action = Tools.deepClone(action);- if (mutation.t === 'r') {- const { coverType, body, len, segmentId } = mutation;+ if (action.t === TextXActionType.RETAIN) {+ const { coverType, body, len, segmentId } = action;if (body != null) {const documentBody = this._updateApply(body, len, memoryCursor.cursor, coverType, segmentId);undoMutations.push({- ...mutation,- t: 'r',+ ...action,+ t: TextXActionType.RETAIN,coverType: UpdateDocsAttributeType.REPLACE,body: documentBody,});} else {undoMutations.push({- ...mutation,- t: 'r',+ ...action,+ t: TextXActionType.RETAIN,});}memoryCursor.moveCursor(len);- } else if (mutation.t === 'i') {- const { body, len, segmentId, line } = mutation;+ } else if (action.t === TextXActionType.INSERT) {+ const { body, len, segmentId, line } = action;this._insertApply(body!, len, memoryCursor.cursor, segmentId);memoryCursor.moveCursor(len);undoMutations.push({- t: 'd',+ t: TextXActionType.DELETE,len,line,segmentId,});- } else if (mutation.t === 'd') {- const { len, segmentId } = mutation;+ } else if (action.t === TextXActionType.DELETE) {+ const { len, segmentId } = action;const documentBody = this._deleteApply(len, memoryCursor.cursor, segmentId);undoMutations.push({- ...mutation,- t: 'i',+ ...action,+ t: TextXActionType.INSERT,body: documentBody,});} else {- throw new Error(`Unknown mutation type for mutation: ${mutation}.`);+ throw new Error(`Unknown action type for action: ${action}.`);}});@@ -322,64 +321,7 @@ export class DocumentDataModel extends DocumentDataModelSimple {return;}- const { dataStream, textRuns = [], paragraphs = [] } = body;-- const docBody: IDocumentBody = {- dataStream: dataStream.slice(startOffset, endOffset),- };-- const newTextRuns: ITextRun[] = [];-- for (const textRun of textRuns) {- const clonedTextRun = Tools.deepClone(textRun);- const { st, ed } = clonedTextRun;- if (Tools.hasIntersectionBetweenTwoRanges(st, ed, startOffset, endOffset)) {- if (startOffset >= st && startOffset <= ed) {- newTextRuns.push({- ...clonedTextRun,- st: startOffset,- ed: Math.min(endOffset, ed),- });- } else if (endOffset >= st && endOffset <= ed) {- newTextRuns.push({- ...clonedTextRun,- st: Math.max(startOffset, st),- ed: endOffset,- });- } else {- newTextRuns.push(clonedTextRun);- }- }- }-- if (newTextRuns.length) {- docBody.textRuns = newTextRuns.map((tr) => {- const { st, ed } = tr;- return {- ...tr,- st: st - startOffset,- ed: ed - startOffset,- };- });- }-- const newParagraphs: IParagraph[] = [];-- for (const paragraph of paragraphs) {- const { startIndex } = paragraph;- if (startIndex >= startOffset && startIndex <= endOffset) {- newParagraphs.push(Tools.deepClone(paragraph));- }- }-- if (newParagraphs.length) {- docBody.paragraphs = newParagraphs.map((p) => ({- ...p,- startIndex: p.startIndex - startOffset,- }));- }-- return docBody;+ return getBodySlice(body, startOffset, endOffset);}private _updateApply(diff --git a/packages/core/src/docs/data-model/text-x/__tests__/action-iterator.spec.ts b/packages/core/src/docs/data-model/text-x/__tests__/action-iterator.spec.tsnew file mode 100644index 0000000000..19798aa60c--- /dev/null+++ b/packages/core/src/docs/data-model/text-x/__tests__/action-iterator.spec.ts@@ -0,0 +1,163 @@+/**+ * Copyright 2023-present DreamNum Inc.+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import { describe, expect, it } from 'vitest';+import { ActionIterator } from '../action-iterator';+import { TextXActionType } from '../../action-types';+import { BooleanNumber } from '../../../../types/enum/text-style';++describe('Test action iterator', () => {+ it('test action iterator basic use', () => {+ const iterator = new ActionIterator([{+ t: TextXActionType.RETAIN,+ len: 5,+ }, {+ t: TextXActionType.INSERT,+ body: {+ dataStream: 'hello',+ textRuns: [{+ st: 0,+ ed: 5,+ ts: {+ bl: BooleanNumber.TRUE,+ },+ }],+ },+ len: 5,+ line: 0,+ }, {+ t: TextXActionType.DELETE,+ len: 5,+ line: 0,+ }]);++ expect(iterator.hasNext()).toBe(true);++ expect(iterator.peekType()).toBe(TextXActionType.RETAIN);++ expect(iterator.peekLength()).toBe(5);++ expect(iterator.peek()).toEqual({+ t: TextXActionType.RETAIN,+ len: 5,+ });++ let action = iterator.next();++ expect(action).toEqual({+ t: TextXActionType.RETAIN,+ len: 5,+ });++ expect(iterator.peekType()).toBe(TextXActionType.INSERT);++ action = iterator.next(2);++ expect(action).toEqual({+ t: TextXActionType.INSERT,+ body: {+ dataStream: 'he',+ textRuns: [{+ st: 0,+ ed: 2,+ ts: {+ bl: BooleanNumber.TRUE,+ },+ }],+ },+ len: 2,+ line: 0,+ });++ expect(iterator.peekType()).toBe(TextXActionType.INSERT);++ action = iterator.next(4);++ expect(action).toEqual({+ t: TextXActionType.INSERT,+ body: {+ dataStream: 'llo',+ textRuns: [{+ st: 0,+ ed: 3,+ ts: {+ bl: BooleanNumber.TRUE,+ },+ }],+ },+ len: 3,+ line: 0,+ });++ expect(iterator.peekType()).toBe(TextXActionType.DELETE);++ action = iterator.next();++ expect(action).toEqual({+ t: TextXActionType.DELETE,+ len: 5,+ line: 0,+ });++ expect(iterator.hasNext()).toBe(false);++ expect(iterator.peekType()).toBe(TextXActionType.RETAIN);++ expect(iterator.peekLength()).toBe(Number.POSITIVE_INFINITY);++ expect(iterator.peek()).toBe(undefined);++ expect(iterator.rest()).toEqual([]);++ action = iterator.next();++ expect(action).toEqual({+ t: TextXActionType.RETAIN,+ len: Number.POSITIVE_INFINITY,+ });+ });++ it('test action iterator rest', () => {+ const iterator = new ActionIterator([{+ t: TextXActionType.RETAIN,+ len: 5,+ }, {+ t: TextXActionType.DELETE,+ len: 5,+ line: 0,+ }]);++ expect(iterator.rest()).toEqual([{+ t: TextXActionType.RETAIN,+ len: 5,+ }, {+ t: TextXActionType.DELETE,+ len: 5,+ line: 0,+ }]);++ iterator.next(3);++ expect(iterator.rest()).toEqual([{+ t: TextXActionType.RETAIN,+ len: 2,+ }, {+ t: TextXActionType.DELETE,+ len: 5,+ line: 0,+ }]);+ });+});diff --git a/packages/core/src/docs/data-model/text-x/__tests__/compose.spec.ts b/packages/core/src/docs/data-model/text-x/__tests__/compose.spec.tsnew file mode 100644index 0000000000..879fe3041f--- /dev/null+++ b/packages/core/src/docs/data-model/text-x/__tests__/compose.spec.ts@@ -0,0 +1,406 @@+/**+ * Copyright 2023-present DreamNum Inc.+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import { describe, expect, it } from 'vitest';+import type { TextXAction } from '../../action-types';+import { TextXActionType } from '../../action-types';+import { BooleanNumber } from '../../../../types/enum/text-style';+import { TextX } from '../text-x';++describe('compose test cases', () => {+ it('should return empty array when action A and action B are both empty', () => {+ expect(TextX.compose([], [])).toEqual([]);+ });++ it('test compose basic use', () => {+ const actions_a: TextXAction[] = [{+ t: TextXActionType.RETAIN,+ len: 5,+ }, {+ t: TextXActionType.DELETE,+ len: 2,+ line: 0,+ }];++ const actions_b: TextXAction[] = [{+ t: TextXActionType.DELETE,+ len: 8,+ line: 0,+ }];++ const expect_actions: TextXAction[] = [{+ t: TextXActionType.DELETE,+ len: 10,+ line: 0,+ }];++ expect(TextX.compose(actions_a, actions_b)).toEqual(expect_actions);+ });++ it('test compose delete + delete', () => {+ const actions_a: TextXAction[] = [{+ t: TextXActionType.DELETE,+ len: 2,+ line: 0,+ }];++ const actions_b: TextXAction[] = [{+ t: TextXActionType.DELETE,+ len: 8,+ line: 0,+ }];++ const expect_actions: TextXAction[] = [{+ t: TextXActionType.DELETE,+ len: 10,+ line: 0,+ }];++ expect(TextX.compose(actions_a, actions_b)).toEqual(expect_actions);+ });++ it('test compose delete + retain', () => {+ const actions_a: TextXAction[] = [{+ t: TextXActionType.DELETE,+ len: 2,+ line: 0,+ }];++ const actions_b: TextXAction[] = [{+ t: TextXActionType.RETAIN,+ len: 8,+ }];++ const expect_actions: TextXAction[] = [{+ t: TextXActionType.DELETE,+ len: 2,+ line: 0,+ }];++ expect(TextX.compose(actions_a, actions_b)).toEqual(expect_actions);+ });++ it('test compose delete + insert', () => {+ const actions_a: TextXAction[] = [{+ t: TextXActionType.DELETE,+ len: 2,+ line: 0,+ }];++ const actions_b: TextXAction[] = [{+ t: TextXActionType.INSERT,+ body: {+ dataStream: 'h',+ },+ len: 1,+ line: 0,+ }];++ // Always put insert before delete.+ const expect_actions: TextXAction[] = [{+ t: TextXActionType.INSERT,+ body: {+ dataStream: 'h',+ },+ len: 1,+ line: 0,+ }, {+ t: TextXActionType.DELETE,+ len: 2,+ line: 0,+ }];++ expect(TextX.compose(actions_a, actions_b)).toEqual(expect_actions);+ });++ it('test compose retain + retain', () => {+ const actions_a: TextXAction[] = [{+ t: TextXActionType.RETAIN,+ len: 2,+ }];++ const actions_b: TextXAction[] = [{+ t: TextXActionType.RETAIN,+ len: 8,+ }];++ const expect_actions: TextXAction[] = [];++ expect(TextX.compose(actions_a, actions_b)).toEqual(expect_actions);+ });++ it('test compose retain + insert', () => {+ const actions_a: TextXAction[] = [{+ t: TextXActionType.RETAIN,+ len: 2,+ }];++ const actions_b: TextXAction[] = [{+ t: TextXActionType.INSERT,+ len: 1,+ body: {+ dataStream: 'h',+ },+ line: 0,+ }];++ const expect_actions: TextXAction[] = [{+ t: TextXActionType.INSERT,+ len: 1,+ body: {+ dataStream: 'h',+ },+ line: 0,+ }];++ expect(TextX.compose(actions_a, actions_b)).toEqual(expect_actions);+ });++ it('test compose retain + delete', () => {+ const actions_a: TextXAction[] = [{+ t: TextXActionType.RETAIN,+ len: 2,+ }];++ const actions_b: TextXAction[] = [{+ t: TextXActionType.DELETE,+ len: 1,+ line: 0,+ }];++ const expect_actions: TextXAction[] = [{+ t: TextXActionType.DELETE,+ len: 1,+ line: 0,+ }];++ expect(TextX.compose(actions_a, actions_b)).toEqual(expect_actions);+ });++ it('test compose insert + retain', () => {+ const actions_a: TextXAction[] = [{+ t: TextXActionType.INSERT,+ body: {+ dataStream: 'h',+ },+ len: 1,+ line: 0,+ }];++ const actions_b: TextXAction[] = [{+ t: TextXActionType.RETAIN,+ len: 1,+ }];++ const expect_actions: TextXAction[] = [{+ t: TextXActionType.INSERT,+ body: {+ dataStream: 'h',+ },+ len: 1,+ line: 0,+ }];++ expect(TextX.compose(actions_a, actions_b)).toEqual(expect_actions);+ });++ it('test compose insert + insert', () => {+ const actions_a: TextXAction[] = [{+ t: TextXActionType.INSERT,+ body: {+ dataStream: 'h',+ },+ len: 1,+ line: 0,+ }];++ const actions_b: TextXAction[] = [{+ t: TextXActionType.INSERT,+ body: {+ dataStream: 'b',+ },+ len: 1,+ line: 0,+ }];++ const expect_actions: TextXAction[] = [{+ t: TextXActionType.INSERT,+ body: {+ dataStream: 'bh',+ },+ len: 2,+ line: 0,+ }];++ const composed = TextX.compose(actions_a, actions_b);++ expect(composed).toEqual(expect_actions);+ });++ it('test compose insert + delete', () => {+ const actions_a: TextXAction[] = [{+ t: TextXActionType.INSERT,+ body: {+ dataStream: 'h',+ },+ len: 1,+ line: 0,+ }];++ const actions_b: TextXAction[] = [{+ t: TextXActionType.DELETE,+ len: 1,+ line: 0,+ }];++ const expect_actions: TextXAction[] = [];++ expect(TextX.compose(actions_a, actions_b)).toEqual(expect_actions);+ });++ it('test compose retain + retain with style', () => {+ const actions_a: TextXAction[] = [{+ t: TextXActionType.RETAIN,+ body: {+ dataStream: '',+ textRuns: [{+ st: 0,+ ed: 1,+ ts: {+ bl: BooleanNumber.TRUE,+ },+ }],+ },+ len: 1,+ }];++ const actions_b: TextXAction[] = [{+ t: TextXActionType.RETAIN,+ body: {+ dataStream: '',+ textRuns: [{+ st: 0,+ ed: 1,+ ts: {+ it: BooleanNumber.TRUE,+ },+ }],+ },+ len: 1,+ }];++ const expect_actions: TextXAction[] = [{+ t: TextXActionType.RETAIN,+ body: {+ dataStream: '',+ textRuns: [{+ st: 0,+ ed: 1,+ ts: {+ bl: BooleanNumber.TRUE,+ it: BooleanNumber.TRUE,+ },+ }],+ },+ len: 1,+ }];++ expect(TextX.compose(actions_a, actions_b)).toEqual(expect_actions);+ });++ it('test compose insert + retain with style', () => {+ const actions_a: TextXAction[] = [{+ t: TextXActionType.INSERT,+ body: {+ dataStream: 'h',+ },+ len: 1,+ line: 0,+ }];++ const actions_b: TextXAction[] = [{+ t: TextXActionType.RETAIN,+ body: {+ dataStream: '',+ textRuns: [{+ st: 0,+ ed: 1,+ ts: {+ it: BooleanNumber.TRUE,+ },+ }],+ },+ len: 1,+ }];++ const expect_actions: TextXAction[] = [{+ t: TextXActionType.INSERT,+ body: {+ dataStream: 'h',+ textRuns: [{+ st: 0,+ ed: 1,+ ts: {+ it: BooleanNumber.TRUE,+ },+ }],+ },+ len: 1,+ line: 0,+ }];++ expect(TextX.compose(actions_a, actions_b)).toEqual(expect_actions);+ });++ it('test compose retain with no body + retain with style', () => {+ const actions_a: TextXAction[] = [{+ t: TextXActionType.RETAIN,+ len: 1,+ }];++ const actions_b: TextXAction[] = [{+ t: TextXActionType.RETAIN,+ body: {+ dataStream: '',+ textRuns: [{+ st: 0,+ ed: 1,+ ts: {+ it: BooleanNumber.TRUE,+ },+ }],+ },+ len: 1,+ }];++ const expect_actions: TextXAction[] = [{+ t: TextXActionType.RETAIN,+ body: {+ dataStream: '',+ textRuns: [{+ st: 0,+ ed: 1,+ ts: {+ it: BooleanNumber.TRUE,+ },+ }],+ },+ len: 1,+ }];++ expect(TextX.compose(actions_a, actions_b)).toEqual(expect_actions);+ });+});diff --git a/packages/core/src/docs/data-model/text-x/__tests__/text-x.spec.ts b/packages/core/src/docs/data-model/text-x/__tests__/text-x.spec.tsindex ceddc8215d..cff1894a44 100644--- a/packages/core/src/docs/data-model/text-x/__tests__/text-x.spec.ts+++ b/packages/core/src/docs/data-model/text-x/__tests__/text-x.spec.ts@@ -19,6 +19,7 @@ import { TextX } from '../text-x';import type { IDocumentBody } from '../../../../types/interfaces/i-document-data';import { BooleanNumber } from '../../../../types/enum/text-style';import { UpdateDocsAttributeType } from '../../../../shared/command-enum';+import { TextXActionType } from '../../action-types';describe('test TextX methods and branches', () => {describe('test TextX methods', () => {@@ -43,7 +44,7 @@ describe('test TextX methods and branches', () => {expect(actions).toEqual([{- t: 'i',+ t: TextXActionType.INSERT,body,len: 5,line: 0,@@ -61,7 +62,7 @@ describe('test TextX methods and branches', () => {expect(actions).toEqual([{- t: 'd',+ t: TextXActionType.DELETE,len: 5,line: 0,segmentId: '',@@ -90,7 +91,7 @@ describe('test TextX methods and branches', () => {expect(actions).toEqual([{- t: 'r',+ t: TextXActionType.RETAIN,body,len: 5,coverType: UpdateDocsAttributeType.COVER,@@ -109,7 +110,7 @@ describe('test TextX methods and branches', () => {expect(actions).toEqual([{- t: 'd',+ t: TextXActionType.DELETE,len: 10, // 5 + 5line: 0,segmentId: '',@@ -140,14 +141,14 @@ describe('test TextX methods and branches', () => {expect(actions).toEqual([{- t: 'i',+ t: TextXActionType.INSERT,body,len: 5,line: 0,segmentId: '',},{- t: 'd',+ t: TextXActionType.DELETE,len: 5,line: 0,segmentId: '',@@ -165,7 +166,7 @@ describe('test TextX methods and branches', () => {expect(actions).toEqual([{- t: 'r',+ t: TextXActionType.RETAIN,len: 9,segmentId: '',},@@ -176,11 +177,11 @@ describe('test TextX methods and branches', () => {const textX = new TextX();textX.push({- t: 'r',+ t: TextXActionType.RETAIN,len: 4,segmentId: '',}, {- t: 'r',+ t: TextXActionType.RETAIN,len: 5,segmentId: '',});@@ -189,7 +190,7 @@ describe('test TextX methods and branches', () => {expect(actions).toEqual([{- t: 'r',+ t: TextXActionType.RETAIN,len: 9,segmentId: '',},@@ -212,18 +213,18 @@ describe('test TextX methods and branches', () => {};textX.push({- t: 'r',+ t: TextXActionType.RETAIN,len: 4,segmentId: '',}, {- t: 'd',+ t: TextXActionType.DELETE,len: 5,line: 0,segmentId: '',});textX.push({- t: 'i',+ t: TextXActionType.INSERT,len: 5,body,line: 0,@@ -233,9 +234,9 @@ describe('test TextX methods and branches', () => {const actions = textX.serialize();expect(actions.length).toBe(3);- expect(actions[0].t).toBe('r');- expect(actions[1].t).toBe('i');- expect(actions[2].t).toBe('d');+ expect(actions[0].t).toBe(TextXActionType.RETAIN);+ expect(actions[1].t).toBe(TextXActionType.INSERT);+ expect(actions[2].t).toBe(TextXActionType.DELETE);});});});diff --git a/packages/core/src/docs/data-model/text-x/__tests__/utils.spec.ts b/packages/core/src/docs/data-model/text-x/__tests__/utils.spec.tsnew file mode 100644index 0000000000..504bf20801--- /dev/null+++ b/packages/core/src/docs/data-model/text-x/__tests__/utils.spec.ts@@ -0,0 +1,342 @@+/**+ * Copyright 2023-present DreamNum Inc.+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import { describe, expect, it } from 'vitest';+import type { IDocumentBody } from '../../../../types/interfaces/i-document-data';+import { BooleanNumber } from '../../../../types/enum/text-style';+import { composeBody, getBodySlice } from '../utils';++describe('test text-x utils', () => {+ it('test getBodySlice fn', () => {+ const body: IDocumentBody = {+ dataStream: 'hello\nworld',+ textRuns: [+ {+ st: 0,+ ed: 5,+ ts: {+ bl: BooleanNumber.TRUE,+ },+ },+ {+ st: 6,+ ed: 10,+ ts: {+ bl: BooleanNumber.FALSE,+ it: BooleanNumber.TRUE,+ },+ },+ ],+ paragraphs: [{+ startIndex: 5,+ }],+ };++ const sliceBody = getBodySlice(body, 3, 8);+ expect(sliceBody).toEqual({+ dataStream: 'lo\nwo',+ textRuns: [+ {+ st: 0,+ ed: 2,+ ts: {+ bl: BooleanNumber.TRUE,+ },+ },+ {+ st: 3,+ ed: 5,+ ts: {+ bl: BooleanNumber.FALSE,+ it: BooleanNumber.TRUE,+ },+ },+ ],+ paragraphs: [{+ startIndex: 2,+ }],+ });+ });++ it('test getBodySlice fn with a larger range', () => {+ const body: IDocumentBody = {+ dataStream: 'hello\nworld',+ textRuns: [+ {+ st: 0,+ ed: 3,+ ts: {+ bl: BooleanNumber.TRUE,+ },+ },+ {+ st: 3,+ ed: 5,+ ts: {+ bl: BooleanNumber.FALSE,+ },+ },+ {+ st: 6,+ ed: 10,+ ts: {+ bl: BooleanNumber.FALSE,+ it: BooleanNumber.TRUE,+ },+ },+ ],+ paragraphs: [{+ startIndex: 5,+ }],+ };++ const sliceBody = getBodySlice(body, 2, 8);+ expect(sliceBody).toEqual({+ dataStream: 'llo\nwo',+ textRuns: [+ {+ st: 0,+ ed: 1,+ ts: {+ bl: BooleanNumber.TRUE,+ },+ },+ {+ st: 1,+ ed: 3,+ ts: {+ bl: BooleanNumber.FALSE,+ },+ },+ {+ st: 4,+ ed: 6,+ ts: {+ bl: BooleanNumber.FALSE,+ it: BooleanNumber.TRUE,+ },+ },+ ],+ paragraphs: [{+ startIndex: 3,+ }],+ });+ });++ it('test composeBody fn with textRuns', () => {+ const thisBody: IDocumentBody = {+ dataStream: 'hello\nworld',+ textRuns: [+ {+ st: 0,+ ed: 5,+ ts: {+ bl: BooleanNumber.TRUE,+ },+ },+ {+ st: 6,+ ed: 10,+ ts: {+ bl: BooleanNumber.FALSE,+ it: BooleanNumber.TRUE,+ },+ },+ ],+ paragraphs: [{+ startIndex: 5,+ }],+ };++ const otherBody: IDocumentBody = {+ dataStream: '',+ textRuns: [+ {+ st: 0,+ ed: 3,+ ts: {+ bl: BooleanNumber.FALSE,+ },+ },+ {+ st: 3,+ ed: 5,+ ts: {+ it: BooleanNumber.TRUE,+ },+ },+ ],+ };++ const composedBody = composeBody(thisBody, otherBody);++ expect(composedBody).toEqual({+ dataStream: 'hello\nworld',+ textRuns: [+ {+ st: 0,+ ed: 3,+ ts: {+ bl: BooleanNumber.FALSE,+ },+ },+ {+ st: 3,+ ed: 5,+ ts: {+ bl: BooleanNumber.TRUE,+ it: BooleanNumber.TRUE,+ },+ },+ {+ st: 6,+ ed: 10,+ ts: {+ bl: BooleanNumber.FALSE,+ it: BooleanNumber.TRUE,+ },+ },+ ],+ paragraphs: [{+ startIndex: 5,+ }],+ });+ });++ it('test composeBody and throw error', () => {+ const thisBody: IDocumentBody = {+ dataStream: 'hello\nworld',+ textRuns: [],+ };+ const otherBody: IDocumentBody = {+ dataStream: 'error',+ textRuns: [],+ };++ expect(() => {+ composeBody(thisBody, otherBody);+ }).toThrowError();+ });++ it('test composeBody fn both width paragraphs', () => {+ const thisBody: IDocumentBody = {+ dataStream: 'hello\nworld',+ paragraphs: [{+ startIndex: 5,+ }],+ };++ const otherBody: IDocumentBody = {+ dataStream: '',+ paragraphs: [{+ startIndex: 5,+ paragraphStyle: {+ spaceAbove: 10,+ lineSpacing: 2,+ spaceBelow: 0,+ },+ }],+ };++ const composedBody = composeBody(thisBody, otherBody);++ expect(composedBody).toEqual({+ dataStream: 'hello\nworld',+ paragraphs: [{+ startIndex: 5,+ paragraphStyle: {+ spaceAbove: 10,+ lineSpacing: 2,+ spaceBelow: 0,+ },+ }],+ });+ });++ it('test composeBody fn both width paragraphs and from retain', () => {+ const thisBody: IDocumentBody = {+ dataStream: '',+ paragraphs: [{+ startIndex: 2,+ }],+ };++ const otherBody: IDocumentBody = {+ dataStream: '',+ paragraphs: [{+ startIndex: 5,+ paragraphStyle: {+ spaceAbove: 10,+ lineSpacing: 2,+ spaceBelow: 0,+ },+ }],+ };++ const composedBody = composeBody(thisBody, otherBody);++ expect(composedBody).toEqual({+ dataStream: '',+ paragraphs: [{+ startIndex: 2,+ }, {+ startIndex: 5,+ paragraphStyle: {+ spaceAbove: 10,+ lineSpacing: 2,+ spaceBelow: 0,+ },+ }],+ });+ });++ it('test composeBody fn both width paragraphs and from retain 2', () => {+ const thisBody: IDocumentBody = {+ dataStream: '',+ paragraphs: [{+ startIndex: 8,+ }],+ };++ const otherBody: IDocumentBody = {+ dataStream: '',+ paragraphs: [{+ startIndex: 5,+ paragraphStyle: {+ spaceAbove: 10,+ lineSpacing: 2,+ spaceBelow: 0,+ },+ }],+ };++ const composedBody = composeBody(thisBody, otherBody);++ expect(composedBody).toEqual({+ dataStream: '',+ paragraphs: [{+ startIndex: 5,+ paragraphStyle: {+ spaceAbove: 10,+ lineSpacing: 2,+ spaceBelow: 0,+ },+ }, {+ startIndex: 8,+ }],+ });+ });+});diff --git a/packages/core/src/docs/data-model/text-x/action-iterator.ts b/packages/core/src/docs/data-model/text-x/action-iterator.tsnew file mode 100644index 0000000000..9ed28f276d--- /dev/null+++ b/packages/core/src/docs/data-model/text-x/action-iterator.ts@@ -0,0 +1,120 @@+/**+ * Copyright 2023-present DreamNum Inc.+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import { Tools } from '../../../shared/tools';+import type { TextXAction } from '../action-types';+import { TextXActionType } from '../action-types';+import { getBodySlice } from './utils';++export class ActionIterator {+ private _index = 0;+ private _offset = 0;++ constructor(private _actions: TextXAction[]) {}++ hasNext() {+ return this.peekLength() < Number.POSITIVE_INFINITY;+ }++ next(length?: number): TextXAction {+ if (!length) {+ length = Number.POSITIVE_INFINITY;+ }++ const nextAction = this._actions[this._index];+ if (nextAction) {+ const offset = this._offset;+ const actionLength = nextAction.len;+ if (length >= actionLength - offset) {+ // Return the action if the length is great than action length, and reset offset to 0.+ length = actionLength - offset;+ this._index += 1;+ this._offset = 0;+ } else {+ this._offset += length;+ }++ /**+ * How to deal with it?+ * 1. If it is of the delete type or retain(the body attribute is undefined)+ * 1) Then change the len property and return the deepClone before action+ * 2. If it is a retain, insert type+ * 1) First of all, you need to change the len attribute to `length`+ * 2) Slice the body and slice the range [offset, length]+ * 3) Reassemble the returned action+ */++ if (nextAction.t === TextXActionType.DELETE || (nextAction.t === TextXActionType.RETAIN && nextAction.body == null)) {+ return Tools.deepClone({+ ...nextAction,+ len: length,+ });+ } else {+ // handle condition: (nextAction.t === TextXActionType.INSERT || nextAction.t === TextXActionType.RETAIN && nextAction.body)+ return Tools.deepClone({+ ...nextAction,+ len: length,+ body: getBodySlice(nextAction.body!, offset, offset + length),+ });+ }+ } else {+ // It indicate that the iterator is exhausted when return TextXActionType.RETAIN and len is positive infinity.+ return {+ t: TextXActionType.RETAIN,+ len: Number.POSITIVE_INFINITY,+ };+ }+ }++ peek(): TextXAction {+ return this._actions[this._index];+ }++ peekLength() {+ if (this._actions[this._index]) {+ // Should never return 0 if our index is being managed correctly+ return this._actions[this._index].len - this._offset;+ } else {+ return Number.POSITIVE_INFINITY;+ }+ }++ peekType(): TextXActionType {+ const action = this._actions[this._index];+ if (action) {+ return action.t;+ }++ return TextXActionType.RETAIN;+ }++ rest(): TextXAction[] {+ if (!this.hasNext()) {+ return [];+ } else if (this._offset === 0) {+ return this._actions.slice(this._index);+ } else {+ const offset = this._offset;+ const index = this._index;+ const next = this.next();+ const restActions = this._actions.slice(this._index);+ this._offset = offset;+ this._index = index;++ return [next].concat(restActions);+ }+ }+}diff --git a/packages/core/src/docs/data-model/text-x/text-x.ts b/packages/core/src/docs/data-model/text-x/text-x.tsindex 0938cfafb6..3231f1170c 100644--- a/packages/core/src/docs/data-model/text-x/text-x.ts+++ b/packages/core/src/docs/data-model/text-x/text-x.ts@@ -17,20 +17,73 @@import { Tools } from '../../../shared/tools';import type { UpdateDocsAttributeType } from '../../../shared/command-enum';import type { IDocumentBody } from '../../../types/interfaces/i-document-data';-import type { IDeleteAction, IInsertAction, IRetainAction, TextXAction } from '../mutation-types';+import { type IDeleteAction, type IInsertAction, type IRetainAction, type TextXAction, TextXActionType } from '../action-types';+import { ActionIterator } from './action-iterator';+import { composeBody } from './utils';++function onlyHasDataStream(body: IDocumentBody) {+ return Object.keys(body).length === 1;+}export class TextX {- // eslint-disable-next-line unused-imports/no-unused-varsstatic compose(thisActions: TextXAction[], otherActions: TextXAction[]): TextXAction[] {- // TODO: @jocs Implement this.- return [];+ const thisIter = new ActionIterator(thisActions);+ const otherIter = new ActionIterator(otherActions);++ const textX = new TextX();++ while (thisIter.hasNext() || otherIter.hasNext()) {+ if (otherIter.peekType() === TextXActionType.INSERT) {+ textX.push(otherIter.next());+ } else if (thisIter.peekType() === TextXActionType.DELETE) {+ textX.push(thisIter.next());+ } else {+ const length = Math.min(thisIter.peekLength(), otherIter.peekLength());+ const thisAction = thisIter.next(length);+ const otherAction = otherIter.next(length);++ if (thisAction.t === TextXActionType.INSERT && otherAction.t === TextXActionType.RETAIN) {+ if (otherAction.body == null) {+ textX.push(thisAction);+ } else {+ textX.push({+ ...thisAction,+ body: composeBody(thisAction.body, otherAction.body, otherAction.coverType),+ });+ }+ } else if (thisAction.t === TextXActionType.RETAIN && otherAction.t === TextXActionType.RETAIN) {+ if (thisAction.body == null && otherAction.body == null) {+ textX.push(thisAction); // or otherAction+ } else if (thisAction.body && otherAction.body) {+ textX.push({+ ...thisAction,+ body: composeBody(thisAction.body, otherAction.body, otherAction.coverType),+ });+ } else {+ textX.push(thisAction.body ? thisAction : otherAction);+ }+ } else if (thisAction.t === TextXActionType.RETAIN && otherAction.t === TextXActionType.DELETE) {+ textX.push(otherAction);+ } else if (thisAction.t === TextXActionType.INSERT && otherAction.t === TextXActionType.DELETE) {+ // Nothing need to do, they are just cancel off.+ }+ // else {+ // // I think exec will never go here.+ // throw new Error('unknown compose case');+ // }+ }+ }++ textX.trimEndUselessRetainAction();++ return textX.serialize();}private _actions: TextXAction[] = [];insert(len: number, body: IDocumentBody, segmentId: string): this {const insertAction: IInsertAction = {- t: 'i',+ t: TextXActionType.INSERT,body,len,line: 0, // hardcode@@ -44,7 +97,7 @@ export class TextX {retain(len: number, segmentId: string, body?: IDocumentBody, coverType?: UpdateDocsAttributeType): this {const retainAction: IRetainAction = {- t: 'r',+ t: TextXActionType.RETAIN,len,segmentId,};@@ -64,7 +117,7 @@ export class TextX {delete(len: number, segmentId: string): this {const deleteAction: IDeleteAction = {- t: 'd',+ t: TextXActionType.DELETE,len,line: 0, // hardcodesegmentId,@@ -95,7 +148,7 @@ export class TextX {if (typeof lastAction === 'object') {// if lastAction and newAction are both delete action, merge the two actions and return this.- if (lastAction.t === 'd' && newAction.t === 'd') {+ if (lastAction.t === TextXActionType.DELETE && newAction.t === TextXActionType.DELETE) {lastAction.len += newAction.len;return this;@@ -103,7 +156,7 @@ export class TextX {// Since it does not matter if we insert before or after deleting at the same index,// always prefer to insert first- if (lastAction.t === 'd' && newAction.t === 'i') {+ if (lastAction.t === TextXActionType.DELETE && newAction.t === TextXActionType.INSERT) {index -= 1;lastAction = this._actions[index - 1];@@ -115,11 +168,19 @@ export class TextX {}// if lastAction and newAction are both retain action and has no body, merge the two actions and return this.- if (lastAction.t === 'r' && newAction.t === 'r' && lastAction.body == null && newAction.body == null) {+ if (lastAction.t === TextXActionType.RETAIN && newAction.t === TextXActionType.RETAIN && lastAction.body == null && newAction.body == null) {lastAction.len += newAction.len;return this;}++ // Both are insert action, and has no styles, merge it.+ if (lastAction.t === TextXActionType.INSERT && onlyHasDataStream(lastAction.body) && newAction.t === TextXActionType.INSERT && onlyHasDataStream(newAction.body)) {+ lastAction.len += newAction.len;+ lastAction.body.dataStream += newAction.body.dataStream;++ return this;+ }}if (index === this._actions.length) {@@ -130,4 +191,16 @@ export class TextX {return this;}++ trimEndUselessRetainAction(): this {+ let lastAction = this._actions[this._actions.length - 1];++ while (lastAction && lastAction.t === TextXActionType.RETAIN && lastAction.body == null) {+ this._actions.pop();++ lastAction = this._actions[this._actions.length - 1];+ }++ return this;+ }}diff --git a/packages/core/src/docs/data-model/text-x/utils.ts b/packages/core/src/docs/data-model/text-x/utils.tsnew file mode 100644index 0000000000..efef339e1c--- /dev/null+++ b/packages/core/src/docs/data-model/text-x/utils.ts@@ -0,0 +1,143 @@+/**+ * Copyright 2023-present DreamNum Inc.+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import { UpdateDocsAttributeType } from '../../../shared/command-enum';+import { Tools } from '../../../shared/tools';+import type { IDocumentBody, IParagraph, ITextRun } from '../../../types/interfaces/i-document-data';+import { coverTextRuns } from '../apply-utils/update-apply';++export function getBodySlice(body: IDocumentBody, startOffset: number, endOffset: number): IDocumentBody {+ const { dataStream, textRuns = [], paragraphs = [] } = body;++ const docBody: IDocumentBody = {+ dataStream: dataStream.slice(startOffset, endOffset),+ };++ const newTextRuns: ITextRun[] = [];++ for (const textRun of textRuns) {+ const clonedTextRun = Tools.deepClone(textRun);+ const { st, ed } = clonedTextRun;++ if (Tools.hasIntersectionBetweenTwoRanges(st, ed, startOffset, endOffset)) {+ if (startOffset >= st && startOffset <= ed) {+ newTextRuns.push({+ ...clonedTextRun,+ st: startOffset,+ ed: Math.min(endOffset, ed),+ });+ } else if (endOffset >= st && endOffset <= ed) {+ newTextRuns.push({+ ...clonedTextRun,+ st: Math.max(startOffset, st),+ ed: endOffset,+ });+ } else {+ newTextRuns.push(clonedTextRun);+ }+ }+ }++ if (newTextRuns.length) {+ docBody.textRuns = newTextRuns.map((tr) => {+ const { st, ed } = tr;+ return {+ ...tr,+ st: st - startOffset,+ ed: ed - startOffset,+ };+ });+ }++ const newParagraphs: IParagraph[] = [];++ for (const paragraph of paragraphs) {+ const { startIndex } = paragraph;+ if (startIndex >= startOffset && startIndex <= endOffset) {+ newParagraphs.push(Tools.deepClone(paragraph));+ }+ }++ if (newParagraphs.length) {+ docBody.paragraphs = newParagraphs.map((p) => ({+ ...p,+ startIndex: p.startIndex - startOffset,+ }));+ }++ return docBody;+}++export function composeBody(+ thisBody: IDocumentBody,+ otherBody: IDocumentBody,+ coverType: UpdateDocsAttributeType = UpdateDocsAttributeType.COVER+): IDocumentBody {+ if (otherBody.dataStream !== '') {+ throw new Error('Cannot compose other body with non-empty dataStream');+ }++ const retBody: IDocumentBody = {+ dataStream: thisBody.dataStream,+ };++ const { textRuns: thisTextRuns = [], paragraphs: thisParagraphs = [] } = thisBody;+ const { textRuns: otherTextRuns = [], paragraphs: otherParagraphs = [] } = otherBody;++ const textRuns = coverTextRuns(otherTextRuns, thisTextRuns, coverType);+ if (textRuns.length) {+ retBody.textRuns = textRuns;+ }++ const paragraphs: IParagraph[] = [];++ let thisIndex = 0;+ let otherIndex = 0;++ while (thisIndex < thisParagraphs.length && otherIndex < otherParagraphs.length) {+ const thisParagraph = thisParagraphs[thisIndex];+ const otherParagraph = otherParagraphs[otherIndex];++ const { startIndex: thisStart } = thisParagraph;+ const { startIndex: otherStart } = otherParagraph;++ if (thisStart === otherStart) {+ paragraphs.push(Tools.deepMerge(thisParagraph, otherParagraph));+ thisIndex++;+ otherIndex++;+ } else if (thisStart < otherStart) {+ paragraphs.push(Tools.deepClone(thisParagraph));+ thisIndex++;+ } else {+ paragraphs.push(Tools.deepClone(otherParagraph));+ otherIndex++;+ }+ }++ if (thisIndex < thisParagraphs.length) {+ paragraphs.push(...thisParagraphs.slice(thisIndex));+ }++ if (otherIndex < otherParagraphs.length) {+ paragraphs.push(...otherParagraphs.slice(otherIndex));+ }++ if (paragraphs.length) {+ retBody.paragraphs = paragraphs;+ }++ return retBody;+}diff --git a/packages/core/src/index.ts b/packages/core/src/index.tsindex d035c872f8..9f68999a36 100644--- a/packages/core/src/index.ts+++ b/packages/core/src/index.ts@@ -29,11 +29,12 @@ export { requestImmediateMacroTask } from './common/request-immediate-macro-taskexport { type ISequenceExecuteResult, sequence, sequenceAsync } from './common/sequence';export * from './docs/data-model';export {+ TextXActionType,type TextXAction,type IDeleteAction,type IInsertAction,type IRetainAction,-} from './docs/data-model/mutation-types';+} from './docs/data-model/action-types';export { TextX } from './docs/data-model/text-x/text-x';export * from './observer';export { Plugin, PluginType } from './plugin/plugin';diff --git a/packages/docs/src/basics/retain-delete-params.ts b/packages/docs/src/basics/retain-delete-params.tsindex 4fe1f0675d..8e057b29ea 100644--- a/packages/docs/src/basics/retain-delete-params.ts+++ b/packages/docs/src/basics/retain-delete-params.ts@@ -14,7 +14,7 @@* limitations under the License.*/-import type { IDeleteAction, IRetainAction, ITextRange } from '@univerjs/core';+import { type IDeleteAction, type IRetainAction, type ITextRange, TextXActionType } from '@univerjs/core';export function getRetainAndDeleteFromReplace(range: ITextRange,@@ -29,14 +29,14 @@ export function getRetainAndDeleteFromReplace(if (textStart > 0) {dos.push({- t: 'r',+ t: TextXActionType.RETAIN,len: textStart,segmentId,});}dos.push({- t: 'd',+ t: TextXActionType.DELETE,len: textEnd - textStart,line: 0,segmentId,diff --git a/packages/docs/src/commands/commands/clipboard.inner.command.ts b/packages/docs/src/commands/commands/clipboard.inner.command.tsindex e128ccb7e5..d38009fb0e 100644--- a/packages/docs/src/commands/commands/clipboard.inner.command.ts+++ b/packages/docs/src/commands/commands/clipboard.inner.command.ts@@ -30,6 +30,7 @@ import {IUniverInstanceService,MemoryCursor,TextX,+ TextXActionType,} from '@univerjs/core';import type { ITextRangeWithStyle } from '@univerjs/engine-render';@@ -87,7 +88,7 @@ export const InnerPasteCommand: ICommand<IInnerPasteCommandParams> = {if (collapsed) {textX.push({- t: 'r',+ t: TextXActionType.RETAIN,len,segmentId,});@@ -96,7 +97,7 @@ export const InnerPasteCommand: ICommand<IInnerPasteCommandParams> = {}textX.push({- t: 'i',+ t: TextXActionType.INSERT,body,len: body.dataStream.length,line: 0,@@ -198,7 +199,7 @@ export const CutContentCommand: ICommand<IInnerCutCommandParams> = {if (collapsed) {textX.push({- t: 'r',+ t: TextXActionType.RETAIN,len,segmentId,});@@ -269,7 +270,7 @@ function getRetainAndDeleteAndExcludeLineBreak(if (textStart > 0) {dos.push({- t: 'r',+ t: TextXActionType.RETAIN,len: textStart,segmentId,});@@ -279,21 +280,21 @@ function getRetainAndDeleteAndExcludeLineBreak(const paragraphIndex = paragraphInRange.startIndex - memoryCursor;dos.push({- t: 'd',+ t: TextXActionType.DELETE,len: paragraphIndex - textStart,line: 0,segmentId,});dos.push({- t: 'r',+ t: TextXActionType.RETAIN,len: 1,segmentId,});if (textEnd > paragraphIndex + 1) {dos.push({- t: 'd',+ t: TextXActionType.DELETE,len: textEnd - paragraphIndex - 1,line: 0,segmentId,@@ -301,7 +302,7 @@ function getRetainAndDeleteAndExcludeLineBreak(}} else {dos.push({- t: 'd',+ t: TextXActionType.DELETE,len: textEnd - textStart,line: 0,segmentId,diff --git a/packages/docs/src/commands/commands/core-editing.command.ts b/packages/docs/src/commands/commands/core-editing.command.tsindex b59d034a1e..c8ef8a9846 100644--- a/packages/docs/src/commands/commands/core-editing.command.ts+++ b/packages/docs/src/commands/commands/core-editing.command.ts@@ -22,7 +22,7 @@ import type {ITextRange,UpdateDocsAttributeType,} from '@univerjs/core';-import { CommandType, ICommandService, IUndoRedoService, TextX } from '@univerjs/core';+import { CommandType, ICommandService, IUndoRedoService, TextX, TextXActionType } from '@univerjs/core';import type { ITextRangeWithStyle } from '@univerjs/engine-render';import { getRetainAndDeleteFromReplace } from '../../basics/retain-delete-params';@@ -67,7 +67,7 @@ export const InsertCommand: ICommand<IInsertCommandParams> = {if (collapsed) {if (startOffset > 0) {textX.push({- t: 'r',+ t: TextXActionType.RETAIN,len: startOffset,segmentId,});@@ -77,7 +77,7 @@ export const InsertCommand: ICommand<IInsertCommandParams> = {}textX.push({- t: 'i',+ t: TextXActionType.INSERT,body,len: body.dataStream.length,line: 0,@@ -164,14 +164,14 @@ export const DeleteCommand: ICommand<IDeleteCommandParams> = {if (startOffset > 0) {textX.push({- t: 'r',+ t: TextXActionType.RETAIN,len: direction === DeleteDirection.LEFT ? startOffset - len : startOffset,segmentId,});}textX.push({- t: 'd',+ t: TextXActionType.DELETE,len,line: 0,segmentId,@@ -249,13 +249,13 @@ export const UpdateCommand: ICommand<IUpdateCommandParams> = {const { startOffset, endOffset } = range;textX.push({- t: 'r',+ t: TextXActionType.RETAIN,len: startOffset,segmentId,});textX.push({- t: 'r',+ t: TextXActionType.RETAIN,body: updateBody,len: endOffset - startOffset,segmentId,diff --git a/packages/docs/src/commands/commands/delete.command.ts b/packages/docs/src/commands/commands/delete.command.tsindex 45a93d8a49..2fb723c573 100644--- a/packages/docs/src/commands/commands/delete.command.ts+++ b/packages/docs/src/commands/commands/delete.command.ts@@ -21,6 +21,7 @@ import {IUndoRedoService,IUniverInstanceService,TextX,+ TextXActionType,UpdateDocsAttributeType,} from '@univerjs/core';import type { IActiveTextRange, TextRange } from '@univerjs/engine-render';@@ -309,14 +310,14 @@ export const MergeTwoParagraphCommand: ICommand<IMergeTwoParagraphParams> = {const textX = new TextX();textX.push({- t: 'r',+ t: TextXActionType.RETAIN,len: direction === DeleteDirection.LEFT ? startOffset - 1 : startOffset,segmentId,});if (body.dataStream.length) {textX.push({- t: 'i',+ t: TextXActionType.INSERT,body,len: body.dataStream.length,line: 0,@@ -325,13 +326,13 @@ export const MergeTwoParagraphCommand: ICommand<IMergeTwoParagraphParams> = {}textX.push({- t: 'r',+ t: TextXActionType.RETAIN,len: 1,segmentId,});textX.push({- t: 'd',+ t: TextXActionType.DELETE,len: endIndex + 1 - startIndex,line: 0,segmentId,diff --git a/packages/docs/src/commands/commands/ime-input.command.ts b/packages/docs/src/commands/commands/ime-input.command.tsindex 92afc33731..d90e1398da 100644--- a/packages/docs/src/commands/commands/ime-input.command.ts+++ b/packages/docs/src/commands/commands/ime-input.command.ts@@ -15,7 +15,7 @@*/import type { ICommand, ICommandInfo, ITextRange } from '@univerjs/core';-import { CommandType, ICommandService, IUndoRedoService, TextX } from '@univerjs/core';+import { CommandType, ICommandService, IUndoRedoService, TextX, TextXActionType } from '@univerjs/core';import type { ITextRangeWithStyle } from '@univerjs/engine-render';import { getRetainAndDeleteFromReplace } from '../../basics/retain-delete-params';@@ -58,7 +58,7 @@ export const IMEInputCommand: ICommand<IIMEInputCommandParams> = {if (range.collapsed) {textX.push({- t: 'r',+ t: TextXActionType.RETAIN,len: range.startOffset,segmentId,});@@ -68,7 +68,7 @@ export const IMEInputCommand: ICommand<IIMEInputCommandParams> = {if (oldTextLen > 0) {textX.push({- t: 'd',+ t: TextXActionType.DELETE,len: oldTextLen,line: 0,segmentId,@@ -76,7 +76,7 @@ export const IMEInputCommand: ICommand<IIMEInputCommandParams> = {}textX.push({- t: 'i',+ t: TextXActionType.INSERT,body: {dataStream: newText,},diff --git a/packages/docs/src/commands/commands/inline-format.command.ts b/packages/docs/src/commands/commands/inline-format.command.tsindex 78fc6b03ef..f0ceb35727 100644--- a/packages/docs/src/commands/commands/inline-format.command.ts+++ b/packages/docs/src/commands/commands/inline-format.command.ts@@ -26,6 +26,7 @@ import {IUniverInstanceService,MemoryCursor,TextX,+ TextXActionType,} from '@univerjs/core';import type { TextRange } from '@univerjs/engine-render';@@ -201,14 +202,14 @@ export const SetInlineFormatCommand: ICommand<ISetInlineFormatCommandParams> = {if (len !== 0) {textX.push({- t: 'r',+ t: TextXActionType.RETAIN,len,segmentId,});}textX.push({- t: 'r',+ t: TextXActionType.RETAIN,body,len: endOffset - startOffset,segmentId,diff --git a/packages/docs/src/commands/commands/list.command.ts b/packages/docs/src/commands/commands/list.command.tsindex 6087cdbf88..0764f12d8d 100644--- a/packages/docs/src/commands/commands/list.command.ts+++ b/packages/docs/src/commands/commands/list.command.ts@@ -23,6 +23,7 @@ import {MemoryCursor,PresetListType,TextX,+ TextXActionType,Tools,UpdateDocsAttributeType,} from '@univerjs/core';@@ -132,7 +133,7 @@ export const ListOperationCommand: ICommand<IListOperationCommandParams> = {const { startIndex } = paragraph;textX.push({- t: 'r',+ t: TextXActionType.RETAIN,len: startIndex - memoryCursor.cursor,segmentId,});@@ -145,7 +146,7 @@ export const ListOperationCommand: ICommand<IListOperationCommandParams> = {};textX.push({- t: 'r',+ t: TextXActionType.RETAIN,len: 1,body: {dataStream: '',diff --git a/packages/docs/src/commands/commands/replace-content.command.ts b/packages/docs/src/commands/commands/replace-content.command.tsindex 0d18362544..8130fbce70 100644--- a/packages/docs/src/commands/commands/replace-content.command.ts+++ b/packages/docs/src/commands/commands/replace-content.command.ts@@ -15,7 +15,7 @@*/import type { ICommand, IDocumentBody, IMutationInfo } from '@univerjs/core';-import { CommandType, ICommandService, IUndoRedoService, IUniverInstanceService, TextX } from '@univerjs/core';+import { CommandType, ICommandService, IUndoRedoService, IUniverInstanceService, TextX, TextXActionType } from '@univerjs/core';import type { ITextRangeWithStyle } from '@univerjs/engine-render';import { TextSelectionManagerService } from '../../services/text-selection-manager.service';@@ -141,7 +141,7 @@ function getMutationParams(unitId: string, segmentId: string, prevBody: IDocumenconst deleteLen = prevBody?.dataStream.length - 2;if (deleteLen > 0) {textX.push({- t: 'd',+ t: TextXActionType.DELETE,len: deleteLen,line: 0,segmentId,@@ -150,7 +150,7 @@ function getMutationParams(unitId: string, segmentId: string, prevBody: IDocumenif (body.dataStream.length > 0) {textX.push({- t: 'i',+ t: TextXActionType.INSERT,body,len: body.dataStream.length,line: 0,diff --git a/packages/docs/src/services/ime-input-manager.service.ts b/packages/docs/src/services/ime-input-manager.service.tsindex 3b902f5cac..5269aea000 100644--- a/packages/docs/src/services/ime-input-manager.service.ts+++ b/packages/docs/src/services/ime-input-manager.service.ts@@ -14,7 +14,7 @@* limitations under the License.*/-import type { Nullable } from '@univerjs/core';+import { type Nullable, TextXActionType } from '@univerjs/core';import type { ITextRangeWithStyle } from '@univerjs/engine-render';import type { IDisposable } from '@wendellhu/redi';@@ -62,39 +62,39 @@ export class IMEInputManagerService implements IDisposable {if (collapsed) {undoMutationParams.mutations.push({- t: 'r',+ t: TextXActionType.RETAIN,len: startOffset,segmentId,});redoMutationParams.mutations.push({- t: 'r',+ t: TextXActionType.RETAIN,len: startOffset,segmentId,});} else {if (startOffset > 0) {undoMutationParams.mutations.push({- t: 'r',+ t: TextXActionType.RETAIN,len: startOffset,segmentId,});}- undoMutationParams.mutations.push(this._undoMutationParamsCache[0].mutations.find((m) => m.t === 'i')!);+ undoMutationParams.mutations.push(this._undoMutationParamsCache[0].mutations.find((m) => m.t === TextXActionType.INSERT)!);redoMutationParams.mutations.push(...getRetainAndDeleteFromReplace(this._previousActiveRange, segmentId));}if (newText.length) {undoMutationParams.mutations.push({- t: 'd',+ t: TextXActionType.DELETE,len: newText.length,line: 0,segmentId,});redoMutationParams.mutations.push({- t: 'i',+ t: TextXActionType.INSERT,body: {dataStream: newText,},