Skip to content

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.

refactor: add textX to build and serialize actions (#1256)

Ran Luo46deaf20ddae
diff --git a/packages/core/src/docs/data-model/document-data-model.ts b/packages/core/src/docs/data-model/document-data-model.ts
index 966ccf5165..752785640a 100644
--- a/packages/core/src/docs/data-model/document-data-model.ts
+++ b/packages/core/src/docs/data-model/document-data-model.ts
@@ -26,11 +26,11 @@ import type {
IParagraph,
ITextRun,
} from '../../types/interfaces/i-document-data';
-import { type IPaddingData } from '../../types/interfaces/i-style-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 DocMutationParams } from './mutation-types';
+import type { TextXAction } from './mutation-types';
export const DEFAULT_DOC = {
id: 'default_doc',
@@ -138,8 +138,8 @@ class DocumentDataModelSimple {
const { documentStyle } = this.snapshot;
if (!documentStyle.pageSize) {
- width = width ?? Infinity;
- height = height ?? Infinity;
+ width = width ?? Number.POSITIVE_INFINITY;
+ height = height ?? Number.POSITIVE_INFINITY;
documentStyle.pageSize = {
width,
@@ -254,8 +254,8 @@ export class DocumentDataModel extends DocumentDataModelSimple {
return this._unitId;
}
- apply(mutations: DocMutationParams[]) {
- const undoMutations: DocMutationParams[] = [];
+ apply(mutations: TextXAction[]) {
+ const undoMutations: TextXAction[] = [];
const memoryCursor = new MemoryCursor();
diff --git a/packages/core/src/docs/data-model/mutation-types.ts b/packages/core/src/docs/data-model/mutation-types.ts
index 4c6aa3f15d..af4a3309de 100644
--- a/packages/core/src/docs/data-model/mutation-types.ts
+++ b/packages/core/src/docs/data-model/mutation-types.ts
@@ -20,7 +20,7 @@ import type { IDocumentBody } from '../../types/interfaces/i-document-data';
/**
* Retain mutation is used to move the cursor or to update properties of the text in the given range.
*/
-export interface IRetainMutationParams {
+export interface IRetainAction {
t: 'r';
len: number;
body?: IDocumentBody;
@@ -31,7 +31,7 @@ export interface IRetainMutationParams {
/**
* Insert mutation is used to insert text (maybe with rich text properties) at the given position.
*/
-export interface IInsertMutationParams {
+export interface IInsertAction {
t: 'i';
body: IDocumentBody;
len: number;
@@ -42,11 +42,11 @@ export interface IInsertMutationParams {
/**
* Delete mutation is used to delete text at the given position.
*/
-export interface IDeleteMutationParams {
+export interface IDeleteAction {
t: 'd';
- line: number;
len: number;
+ line: number;
segmentId?: string;
}
-export type DocMutationParams = IRetainMutationParams | IInsertMutationParams | IDeleteMutationParams;
+export type TextXAction = IRetainAction | IInsertAction | IDeleteAction;
diff --git a/packages/core/src/docs/data-model/text-x/README.md b/packages/core/src/docs/data-model/text-x/README.md
new file mode 100644
index 0000000000..b6405b13c4
--- /dev/null
+++ b/packages/core/src/docs/data-model/text-x/README.md
@@ -0,0 +1,5 @@
+# TextX
+
+TextX is used to build and serialize `RichTextEditingMutation` actions in doc. There are three core actions in doc, insert, retain and delete.
+
+The TextX class also includes action operation methods, such as `insert`, `retain`, `delete` and `push` method, and associated methods, such as `apply`, `invert`, `compose` etc
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.ts
new file mode 100644
index 0000000000..ceddc8215d
--- /dev/null
+++ b/packages/core/src/docs/data-model/text-x/__tests__/text-x.spec.ts
@@ -0,0 +1,241 @@
+/**
+ * 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 { 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';
+
+describe('test TextX methods and branches', () => {
+ describe('test TextX methods', () => {
+ it('test TextX insert method', () => {
+ const textX = new TextX();
+ const body: IDocumentBody = {
+ dataStream: 'hello',
+ textRuns: [
+ {
+ st: 0,
+ ed: 5,
+ ts: {
+ bl: BooleanNumber.TRUE,
+ },
+ },
+ ],
+ };
+
+ textX.insert(5, body, '');
+
+ const actions = textX.serialize();
+
+ expect(actions).toEqual([
+ {
+ t: 'i',
+ body,
+ len: 5,
+ line: 0,
+ segmentId: '',
+ },
+ ]);
+ });
+
+ it('test TextX delete method', () => {
+ const textX = new TextX();
+
+ textX.delete(5, '');
+
+ const actions = textX.serialize();
+
+ expect(actions).toEqual([
+ {
+ t: 'd',
+ len: 5,
+ line: 0,
+ segmentId: '',
+ },
+ ]);
+ });
+
+ it('test TextX retain method', () => {
+ const textX = new TextX();
+ const body: IDocumentBody = {
+ dataStream: '',
+ textRuns: [
+ {
+ st: 0,
+ ed: 5,
+ ts: {
+ bl: BooleanNumber.TRUE,
+ },
+ },
+ ],
+ };
+
+ textX.retain(5, '', body, UpdateDocsAttributeType.COVER);
+
+ const actions = textX.serialize();
+
+ expect(actions).toEqual([
+ {
+ t: 'r',
+ body,
+ len: 5,
+ coverType: UpdateDocsAttributeType.COVER,
+ segmentId: '',
+ },
+ ]);
+ });
+
+ it('test TextX push method and merge two delete actions', () => {
+ const textX = new TextX();
+
+ textX.delete(5, '');
+ textX.delete(5, '');
+
+ const actions = textX.serialize();
+
+ expect(actions).toEqual([
+ {
+ t: 'd',
+ len: 10, // 5 + 5
+ line: 0,
+ segmentId: '',
+ },
+ ]);
+ });
+
+ it('test TextX push method and put insert action before delete action', () => {
+ const textX = new TextX();
+
+ const body: IDocumentBody = {
+ dataStream: 'hello',
+ textRuns: [
+ {
+ st: 0,
+ ed: 5,
+ ts: {
+ bl: BooleanNumber.TRUE,
+ },
+ },
+ ],
+ };
+
+ textX.delete(5, '');
+ textX.insert(5, body, '');
+
+ const actions = textX.serialize();
+
+ expect(actions).toEqual([
+ {
+ t: 'i',
+ body,
+ len: 5,
+ line: 0,
+ segmentId: '',
+ },
+ {
+ t: 'd',
+ len: 5,
+ line: 0,
+ segmentId: '',
+ },
+ ]);
+ });
+
+ it('test TextX push method and merge two simple retain action', () => {
+ const textX = new TextX();
+
+ textX.retain(4, '');
+ textX.retain(5, '');
+
+ const actions = textX.serialize();
+
+ expect(actions).toEqual([
+ {
+ t: 'r',
+ len: 9,
+ segmentId: '',
+ },
+ ]);
+ });
+
+ it('test TextX push method and with more than one params', () => {
+ const textX = new TextX();
+
+ textX.push({
+ t: 'r',
+ len: 4,
+ segmentId: '',
+ }, {
+ t: 'r',
+ len: 5,
+ segmentId: '',
+ });
+
+ const actions = textX.serialize();
+
+ expect(actions).toEqual([
+ {
+ t: 'r',
+ len: 9,
+ segmentId: '',
+ },
+ ]);
+ });
+
+ it('test TextX push method and with more than one params and merge the last two actions', () => {
+ const textX = new TextX();
+ const body: IDocumentBody = {
+ dataStream: 'hello',
+ textRuns: [
+ {
+ st: 0,
+ ed: 5,
+ ts: {
+ bl: BooleanNumber.TRUE,
+ },
+ },
+ ],
+ };
+
+ textX.push({
+ t: 'r',
+ len: 4,
+ segmentId: '',
+ }, {
+ t: 'd',
+ len: 5,
+ line: 0,
+ segmentId: '',
+ });
+
+ textX.push({
+ t: 'i',
+ len: 5,
+ body,
+ line: 0,
+ segmentId: '',
+ });
+
+ 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');
+ });
+ });
+});
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.ts
new file mode 100644
index 0000000000..0938cfafb6
--- /dev/null
+++ b/packages/core/src/docs/data-model/text-x/text-x.ts
@@ -0,0 +1,133 @@
+/**
+ * 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 { UpdateDocsAttributeType } from '../../../shared/command-enum';
+import type { IDocumentBody } from '../../../types/interfaces/i-document-data';
+import type { IDeleteAction, IInsertAction, IRetainAction, TextXAction } from '../mutation-types';
+
+export class TextX {
+ // eslint-disable-next-line unused-imports/no-unused-vars
+ static compose(thisActions: TextXAction[], otherActions: TextXAction[]): TextXAction[] {
+ // TODO: @jocs Implement this.
+ return [];
+ }
+
+ private _actions: TextXAction[] = [];
+
+ insert(len: number, body: IDocumentBody, segmentId: string): this {
+ const insertAction: IInsertAction = {
+ t: 'i',
+ body,
+ len,
+ line: 0, // hardcode
+ segmentId,
+ };
+
+ this.push(insertAction);
+
+ return this;
+ }
+
+ retain(len: number, segmentId: string, body?: IDocumentBody, coverType?: UpdateDocsAttributeType): this {
+ const retainAction: IRetainAction = {
+ t: 'r',
+ len,
+ segmentId,
+ };
+
+ if (body != null) {
+ retainAction.body = body;
+ }
+
+ if (coverType != null) {
+ retainAction.coverType = coverType;
+ }
+
+ this.push(retainAction);
+
+ return this;
+ }
+
+ delete(len: number, segmentId: string): this {
+ const deleteAction: IDeleteAction = {
+ t: 'd',
+ len,
+ line: 0, // hardcode
+ segmentId,
+ };
+
+ this.push(deleteAction);
+
+ return this;
+ }
+
+ serialize(): TextXAction[] {
+ return this._actions;
+ }
+
+ push(...args: TextXAction[]): this {
+ if (args.length > 1) {
+ for (const ac of args) {
+ this.push(ac);
+ }
+
+ return this;
+ }
+
+ let index = this._actions.length;
+ let lastAction = this._actions[index - 1];
+
+ const newAction = Tools.deepClone(args[0]);
+
+ 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') {
+ lastAction.len += newAction.len;
+
+ return this;
+ }
+
+ // 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') {
+ index -= 1;
+ lastAction = this._actions[index - 1];
+
+ if (lastAction == null) {
+ this._actions.unshift(newAction);
+
+ return this;
+ }
+ }
+
+ // 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) {
+ lastAction.len += newAction.len;
+
+ return this;
+ }
+ }
+
+ if (index === this._actions.length) {
+ this._actions.push(newAction);
+ } else {
+ this._actions.splice(index, 0, newAction);
+ }
+
+ return this;
+ }
+}
diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts
index 854011c777..d035c872f8 100644
--- a/packages/core/src/index.ts
+++ b/packages/core/src/index.ts
@@ -29,11 +29,12 @@ export { requestImmediateMacroTask } from './common/request-immediate-macro-task
export { type ISequenceExecuteResult, sequence, sequenceAsync } from './common/sequence';
export * from './docs/data-model';
export {
- type DocMutationParams,
- type IDeleteMutationParams,
- type IInsertMutationParams,
- type IRetainMutationParams,
+ type TextXAction,
+ type IDeleteAction,
+ type IInsertAction,
+ type IRetainAction,
} from './docs/data-model/mutation-types';
+export { TextX } from './docs/data-model/text-x/text-x';
export * from './observer';
export { Plugin, PluginType } from './plugin/plugin';
export {
diff --git a/packages/docs/src/basics/retain-delete-params.ts b/packages/docs/src/basics/retain-delete-params.ts
index d791011653..4fe1f0675d 100644
--- a/packages/docs/src/basics/retain-delete-params.ts
+++ b/packages/docs/src/basics/retain-delete-params.ts
@@ -14,15 +14,15 @@
* limitations under the License.
*/
-import type { IDeleteMutationParams, IRetainMutationParams, ITextRange } from '@univerjs/core';
+import type { IDeleteAction, IRetainAction, ITextRange } from '@univerjs/core';
export function getRetainAndDeleteFromReplace(
range: ITextRange,
segmentId: string = '',
memoryCursor: number = 0
-): Array<IRetainMutationParams | IDeleteMutationParams> {
+): Array<IRetainAction | IDeleteAction> {
const { startOffset, endOffset } = range;
- const dos: Array<IRetainMutationParams | IDeleteMutationParams> = [];
+ const dos: Array<IRetainAction | IDeleteAction> = [];
const textStart = startOffset - memoryCursor;
const textEnd = endOffset - memoryCursor;
diff --git a/packages/docs/src/commands/commands/clipboard.inner.command.ts b/packages/docs/src/commands/commands/clipboard.inner.command.ts
index 64e567d1c8..e128ccb7e5 100644
--- a/packages/docs/src/commands/commands/clipboard.inner.command.ts
+++ b/packages/docs/src/commands/commands/clipboard.inner.command.ts
@@ -16,10 +16,10 @@
import type {
ICommand,
- IDeleteMutationParams,
+ IDeleteAction,
IDocumentBody,
IMutationInfo,
- IRetainMutationParams,
+ IRetainAction,
ITextRange,
} from '@univerjs/core';
import {
@@ -29,6 +29,7 @@ import {
IUndoRedoService,
IUniverInstanceService,
MemoryCursor,
+ TextX,
} from '@univerjs/core';
import type { ITextRangeWithStyle } from '@univerjs/engine-render';
@@ -77,24 +78,24 @@ export const InnerPasteCommand: ICommand<IInnerPasteCommandParams> = {
memoryCursor.reset();
+ const textX = new TextX();
+
for (const selection of selections) {
const { startOffset, endOffset, collapsed } = selection;
const len = startOffset - memoryCursor.cursor;
if (collapsed) {
- doMutation.params.mutations.push({
+ textX.push({
t: 'r',
len,
segmentId,
});
} else {
- doMutation.params.mutations.push(
- ...getRetainAndDeleteFromReplace(selection, segmentId, memoryCursor.cursor)
- );
+ textX.push(...getRetainAndDeleteFromReplace(selection, segmentId, memoryCursor.cursor));
}
- doMutation.params.mutations.push({
+ textX.push({
t: 'i',
body,
len: body.dataStream.length,
@@ -106,6 +107,8 @@ export const InnerPasteCommand: ICommand<IInnerPasteCommandParams> = {
memoryCursor.moveCursor(endOffset);
}
+ doMutation.params.mutations = textX.serialize();
+
const result = commandService.syncExecuteCommand<
IRichTextEditingMutationParams,
IRichTextEditingMutationParams
@@ -186,27 +189,29 @@ export const CutContentCommand: ICommand<IInnerCutCommandParams> = {
memoryCursor.reset();
+ const textX = new TextX();
+
for (const selection of selections) {
const { startOffset, endOffset, collapsed } = selection;
const len = startOffset - memoryCursor.cursor;
if (collapsed) {
- doMutation.params.mutations.push({
+ textX.push({
t: 'r',
len,
segmentId,
});
} else {
- doMutation.params.mutations.push(
- ...getRetainAndDeleteAndExcludeLineBreak(selection, originBody, segmentId, memoryCursor.cursor)
- );
+ textX.push(...getRetainAndDeleteAndExcludeLineBreak(selection, originBody, segmentId, memoryCursor.cursor));
}
memoryCursor.reset();
memoryCursor.moveCursor(endOffset);
}
+ doMutation.params.mutations = textX.serialize();
+
const result = commandService.syncExecuteCommand<
IRichTextEditingMutationParams,
IRichTextEditingMutationParams
@@ -249,9 +254,9 @@ function getRetainAndDeleteAndExcludeLineBreak(
body: IDocumentBody,
segmentId: string = '',
memoryCursor: number = 0
-): Array<IRetainMutationParams | IDeleteMutationParams> {
+): Array<IRetainAction | IDeleteAction> {
const { startOffset, endOffset } = range;
- const dos: Array<IRetainMutationParams | IDeleteMutationParams> = [];
+ const dos: Array<IRetainAction | IDeleteAction> = [];
const { paragraphs = [] } = body;
diff --git a/packages/docs/src/commands/commands/core-editing.command.ts b/packages/docs/src/commands/commands/core-editing.command.ts
index c0700d1cdf..b59d034a1e 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 } from '@univerjs/core';
+import { CommandType, ICommandService, IUndoRedoService, TextX } from '@univerjs/core';
import type { ITextRangeWithStyle } from '@univerjs/engine-render';
import { getRetainAndDeleteFromReplace } from '../../basics/retain-delete-params';
@@ -62,19 +62,21 @@ export const InsertCommand: ICommand<IInsertCommandParams> = {
},
};
+ const textX = new TextX();
+
if (collapsed) {
if (startOffset > 0) {
- doMutation.params.mutations.push({
+ textX.push({
t: 'r',
len: startOffset,
segmentId,
});
}
} else {
- doMutation.params.mutations.push(...getRetainAndDeleteFromReplace(range, segmentId));
+ textX.push(...getRetainAndDeleteFromReplace(range, segmentId));
}
- doMutation.params.mutations.push({
+ textX.push({
t: 'i',
body,
len: body.dataStream.length,
@@ -82,6 +84,8 @@ export const InsertCommand: ICommand<IInsertCommandParams> = {
segmentId,
});
+ doMutation.params.mutations = textX.serialize();
+
const result = commandService.syncExecuteCommand<
IRichTextEditingMutationParams,
IRichTextEditingMutationParams
@@ -156,21 +160,25 @@ export const DeleteCommand: ICommand<IDeleteCommandParams> = {
},
};
+ const textX = new TextX();
+
if (startOffset > 0) {
- doMutation.params.mutations.push({
+ textX.push({
t: 'r',
len: direction === DeleteDirection.LEFT ? startOffset - len : startOffset,
segmentId,
});
}
- doMutation.params.mutations.push({
+ textX.push({
t: 'd',
len,
line: 0,
segmentId,
});
+ doMutation.params.mutations = textX.serialize();
+
const result = commandService.syncExecuteCommand<
IRichTextEditingMutationParams,
IRichTextEditingMutationParams
@@ -236,15 +244,17 @@ export const UpdateCommand: ICommand<IUpdateCommandParams> = {
},
};
+ const textX = new TextX();
+
const { startOffset, endOffset } = range;
- doMutation.params.mutations.push({
+ textX.push({
t: 'r',
len: startOffset,
segmentId,
});
- doMutation.params.mutations.push({
+ textX.push({
t: 'r',
body: updateBody,
len: endOffset - startOffset,
@@ -252,6 +262,8 @@ export const UpdateCommand: ICommand<IUpdateCommandParams> = {
coverType,
});
+ doMutation.params.mutations = textX.serialize();
+
const result = commandService.syncExecuteCommand<
IRichTextEditingMutationParams,
IRichTextEditingMutationParams
diff --git a/packages/docs/src/commands/commands/delete.command.ts b/packages/docs/src/commands/commands/delete.command.ts
index e5b20c3476..45a93d8a49 100644
--- a/packages/docs/src/commands/commands/delete.command.ts
+++ b/packages/docs/src/commands/commands/delete.command.ts
@@ -20,6 +20,7 @@ import {
ICommandService,
IUndoRedoService,
IUniverInstanceService,
+ TextX,
UpdateDocsAttributeType,
} from '@univerjs/core';
import type { IActiveTextRange, TextRange } from '@univerjs/engine-render';
@@ -305,14 +306,16 @@ export const MergeTwoParagraphCommand: ICommand<IMergeTwoParagraphParams> = {
},
};
- doMutation.params.mutations.push({
+ const textX = new TextX();
+
+ textX.push({
t: 'r',
len: direction === DeleteDirection.LEFT ? startOffset - 1 : startOffset,
segmentId,
});
if (body.dataStream.length) {
- doMutation.params.mutations.push({
+ textX.push({
t: 'i',
body,
len: body.dataStream.length,
@@ -321,19 +324,21 @@ export const MergeTwoParagraphCommand: ICommand<IMergeTwoParagraphParams> = {
});
}
- doMutation.params.mutations.push({
+ textX.push({
t: 'r',
len: 1,
segmentId,
});
- doMutation.params.mutations.push({
+ textX.push({
t: 'd',
len: endIndex + 1 - startIndex,
line: 0,
segmentId,
});
+ doMutation.params.mutations = textX.serialize();
+
const result = commandService.syncExecuteCommand<
IRichTextEditingMutationParams,
IRichTextEditingMutationParams
diff --git a/packages/docs/src/commands/commands/ime-input.command.ts b/packages/docs/src/commands/commands/ime-input.command.ts
index e67bb0791e..92afc33731 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 } from '@univerjs/core';
+import { CommandType, ICommandService, IUndoRedoService, TextX } from '@univerjs/core';
import type { ITextRangeWithStyle } from '@univerjs/engine-render';
import { getRetainAndDeleteFromReplace } from '../../basics/retain-delete-params';
@@ -54,18 +54,20 @@ export const IMEInputCommand: ICommand<IIMEInputCommandParams> = {
},
};
+ const textX = new TextX();
+
if (range.collapsed) {
- doMutation.params!.mutations.push({
+ textX.push({
t: 'r',
len: range.startOffset,
segmentId,
});
} else {
- doMutation.params!.mutations.push(...getRetainAndDeleteFromReplace(range, segmentId));
+ textX.push(...getRetainAndDeleteFromReplace(range, segmentId));
}
if (oldTextLen > 0) {
- doMutation.params!.mutations.push({
+ textX.push({
t: 'd',
len: oldTextLen,
line: 0,
@@ -73,7 +75,7 @@ export const IMEInputCommand: ICommand<IIMEInputCommandParams> = {
});
}
- doMutation.params!.mutations.push({
+ textX.push({
t: 'i',
body: {
dataStream: newText,
@@ -83,6 +85,8 @@ export const IMEInputCommand: ICommand<IIMEInputCommandParams> = {
segmentId,
});
+ doMutation.params!.mutations = textX.serialize();
+
const result = commandService.syncExecuteCommand<
IRichTextEditingMutationParams,
IRichTextEditingMutationParams
diff --git a/packages/docs/src/commands/commands/inline-format.command.ts b/packages/docs/src/commands/commands/inline-format.command.ts
index 1fd1006361..78fc6b03ef 100644
--- a/packages/docs/src/commands/commands/inline-format.command.ts
+++ b/packages/docs/src/commands/commands/inline-format.command.ts
@@ -25,6 +25,7 @@ import {
IUndoRedoService,
IUniverInstanceService,
MemoryCursor,
+ TextX,
} from '@univerjs/core';
import type { TextRange } from '@univerjs/engine-render';
@@ -174,6 +175,8 @@ export const SetInlineFormatCommand: ICommand<ISetInlineFormatCommandParams> = {
},
};
+ const textX = new TextX();
+
const memoryCursor = new MemoryCursor();
memoryCursor.reset();
@@ -197,14 +200,14 @@ export const SetInlineFormatCommand: ICommand<ISetInlineFormatCommandParams> = {
const len = startOffset - memoryCursor.cursor;
if (len !== 0) {
- doMutation.params!.mutations.push({
+ textX.push({
t: 'r',
len,
segmentId,
});
}
- doMutation.params!.mutations.push({
+ textX.push({
t: 'r',
body,
len: endOffset - startOffset,
@@ -215,6 +218,8 @@ export const SetInlineFormatCommand: ICommand<ISetInlineFormatCommandParams> = {
memoryCursor.moveCursor(endOffset);
}
+ doMutation.params.mutations = textX.serialize();
+
const result = commandService.syncExecuteCommand<
IRichTextEditingMutationParams,
IRichTextEditingMutationParams
diff --git a/packages/docs/src/commands/commands/list.command.ts b/packages/docs/src/commands/commands/list.command.ts
index bb4fa11dd8..6087cdbf88 100644
--- a/packages/docs/src/commands/commands/list.command.ts
+++ b/packages/docs/src/commands/commands/list.command.ts
@@ -22,6 +22,7 @@ import {
IUniverInstanceService,
MemoryCursor,
PresetListType,
+ TextX,
Tools,
UpdateDocsAttributeType,
} from '@univerjs/core';
@@ -125,10 +126,12 @@ export const ListOperationCommand: ICommand<IListOperationCommandParams> = {
memoryCursor.reset();
+ const textX = new TextX();
+
for (const paragraph of currentParagraphs) {
const { startIndex } = paragraph;
- doMutation.params.mutations.push({
+ textX.push({
t: 'r',
len: startIndex - memoryCursor.cursor,
segmentId,
@@ -141,7 +144,7 @@ export const ListOperationCommand: ICommand<IListOperationCommandParams> = {
indentStart: undefined,
};
- doMutation.params.mutations.push({
+ textX.push({
t: 'r',
len: 1,
body: {
@@ -175,6 +178,8 @@ export const ListOperationCommand: ICommand<IListOperationCommandParams> = {
memoryCursor.moveCursorTo(startIndex + 1);
}
+ doMutation.params.mutations = textX.serialize();
+
const result = commandService.syncExecuteCommand<
IRichTextEditingMutationParams,
IRichTextEditingMutationParams
diff --git a/packages/docs/src/commands/commands/replace-content.command.ts b/packages/docs/src/commands/commands/replace-content.command.ts
index 9c735b54dc..0d18362544 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 } from '@univerjs/core';
+import { CommandType, ICommandService, IUndoRedoService, IUniverInstanceService, TextX } from '@univerjs/core';
import type { ITextRangeWithStyle } from '@univerjs/engine-render';
import { TextSelectionManagerService } from '../../services/text-selection-manager.service';
@@ -136,9 +136,11 @@ function getMutationParams(unitId: string, segmentId: string, prevBody: IDocumen
},
};
+ const textX = new TextX();
+
const deleteLen = prevBody?.dataStream.length - 2;
if (deleteLen > 0) {
- doMutation.params.mutations.push({
+ textX.push({
t: 'd',
len: deleteLen,
line: 0,
@@ -147,7 +149,7 @@ function getMutationParams(unitId: string, segmentId: string, prevBody: IDocumen
}
if (body.dataStream.length > 0) {
- doMutation.params.mutations.push({
+ textX.push({
t: 'i',
body,
len: body.dataStream.length,
@@ -156,5 +158,7 @@ function getMutationParams(unitId: string, segmentId: string, prevBody: IDocumen
});
}
+ doMutation.params.mutations = textX.serialize();
+
return doMutation;
}
diff --git a/packages/docs/src/commands/mutations/core-editing.mutation.ts b/packages/docs/src/commands/mutations/core-editing.mutation.ts
index eb40a568a3..1ac30edfb9 100644
--- a/packages/docs/src/commands/mutations/core-editing.mutation.ts
+++ b/packages/docs/src/commands/mutations/core-editing.mutation.ts
@@ -14,13 +14,13 @@
* limitations under the License.
*/
-import { CommandType, type DocMutationParams, type IMutation, IUniverInstanceService } from '@univerjs/core';
+import { CommandType, type IMutation, IUniverInstanceService, type TextXAction } from '@univerjs/core';
import { DocViewModelManagerService } from '../../services/doc-view-model-manager.service';
export interface IRichTextEditingMutationParams {
unitId: string;
- mutations: DocMutationParams[];
+ mutations: TextXAction[];
}
/**
@@ -45,7 +45,7 @@ export const RichTextEditingMutation: IMutation<IRichTextEditingMutationParams,
}
if (mutations.length === 0) {
- throw new Error(`Mutation's length should great than 0 when call RichTextEditingMutation`);
+ throw new Error('Mutation\'s length should great than 0 when call RichTextEditingMutation');
}
// Step 1: Update Doc Data Model.