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: ime use compose method to build undo and redo params (#1289)
Ran Luo8fa67e07fd5e
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.tsindex 879fe3041f..0951953e96 100644--- 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.ts@@ -25,6 +25,42 @@ describe('compose test cases', () => {expect(TextX.compose([], [])).toEqual([]);});+ it('should return second actions when action A is empty', () => {+ const actions_a: TextXAction[] = [];+ const actions_b: TextXAction[] = [{+ t: TextXActionType.RETAIN,+ len: 5,+ }, {+ t: TextXActionType.INSERT,+ body: {+ dataStream: 'w',+ },+ len: 5,+ line: 0,+ }];+ const expect_actions = actions_b;++ expect(TextX.compose(actions_a, actions_b)).toEqual(expect_actions);+ });++ it('should return first actions when action B is empty', () => {+ const actions_a: TextXAction[] = [{+ t: TextXActionType.RETAIN,+ len: 5,+ }, {+ t: TextXActionType.INSERT,+ body: {+ dataStream: 'w',+ },+ len: 5,+ line: 0,+ }];+ const actions_b: TextXAction[] = [];+ const expect_actions = actions_a;++ expect(TextX.compose(actions_a, actions_b)).toEqual(expect_actions);+ });+it('test compose basic use', () => {const actions_a: TextXAction[] = [{t: TextXActionType.RETAIN,@@ -403,4 +439,78 @@ describe('compose test cases', () => {expect(TextX.compose(actions_a, actions_b)).toEqual(expect_actions);});++ it('test compose r-i with r-i-d', () => {+ const actions_a: TextXAction[] = [{+ t: TextXActionType.RETAIN,+ len: 1,+ }, {+ t: TextXActionType.INSERT,+ body: {+ dataStream: 'h',+ },+ len: 1,+ line: 0,+ }];++ const actions_b: TextXAction[] = [{+ t: TextXActionType.RETAIN,+ len: 1,+ }, {+ t: TextXActionType.INSERT,+ body: {+ dataStream: 'ab',+ },+ len: 2,+ line: 0,+ }, {+ t: TextXActionType.DELETE,+ len: 1,+ line: 0,+ }];++ const inner_actions: TextXAction[] = [{+ t: TextXActionType.RETAIN,+ len: 1,+ }, {+ t: TextXActionType.INSERT,+ body: {+ dataStream: 'ab',+ },+ len: 2,+ line: 0,+ }];++ expect(TextX.compose(actions_a, actions_b)).toEqual(inner_actions);++ const actions_c: TextXAction[] = [{+ t: TextXActionType.RETAIN,+ len: 1,+ }, {+ t: TextXActionType.INSERT,+ body: {+ dataStream: 'c',+ },+ len: 1,+ line: 0,+ }, {+ t: TextXActionType.DELETE,+ len: 2,+ line: 0,+ }];++ const expect_actions: TextXAction[] = [{+ t: TextXActionType.RETAIN,+ len: 1,+ }, {+ t: TextXActionType.INSERT,+ body: {+ dataStream: 'c',+ },+ len: 1,+ line: 0,+ }];++ expect(TextX.compose(inner_actions, actions_c)).toEqual(expect_actions);+ });});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 3231f1170c..beae6ba75c 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@@ -53,7 +53,7 @@ export class TextX {}} else if (thisAction.t === TextXActionType.RETAIN && otherAction.t === TextXActionType.RETAIN) {if (thisAction.body == null && otherAction.body == null) {- textX.push(thisAction); // or otherAction+ textX.push(thisAction.len !== Number.POSITIVE_INFINITY ? thisAction : otherAction); // or otherAction} else if (thisAction.body && otherAction.body) {textX.push({...thisAction,@@ -146,6 +146,11 @@ export class TextX {const newAction = Tools.deepClone(args[0]);+ // Nothing need to do, if the retain 0 and body is null.+ if (newAction.t === TextXActionType.RETAIN && newAction.len === 0 && newAction.body == null) {+ return this;+ }+if (typeof lastAction === 'object') {// if lastAction and newAction are both delete action, merge the two actions and return this.if (lastAction.t === TextXActionType.DELETE && newAction.t === TextXActionType.DELETE) {diff --git a/packages/docs/src/commands/commands/ime-input.command.ts b/packages/docs/src/commands/commands/ime-input.command.tsindex d90e1398da..40d90eecae 100644--- a/packages/docs/src/commands/commands/ime-input.command.ts+++ b/packages/docs/src/commands/commands/ime-input.command.ts@@ -98,7 +98,7 @@ export const IMEInputCommand: ICommand<IIMEInputCommandParams> = {if (isCompositionEnd) {if (result) {- const historyParams = imeInputManagerService.fetchComposedUndoRedoMutationParams(newText);+ const historyParams = imeInputManagerService.fetchComposedUndoRedoMutationParams();if (historyParams == null) {return false;diff --git a/packages/docs/src/services/ime-input-manager.service.ts b/packages/docs/src/services/ime-input-manager.service.tsindex 5269aea000..7f5cd5f9b6 100644--- a/packages/docs/src/services/ime-input-manager.service.ts+++ b/packages/docs/src/services/ime-input-manager.service.ts@@ -14,11 +14,11 @@* limitations under the License.*/-import { type Nullable, TextXActionType } from '@univerjs/core';+import { TextX } from '@univerjs/core';+import type { Nullable, TextXAction } from '@univerjs/core';import type { ITextRangeWithStyle } from '@univerjs/engine-render';import type { IDisposable } from '@wendellhu/redi';-import { getRetainAndDeleteFromReplace } from '../basics/retain-delete-params';import type { IRichTextEditingMutationParams } from '../commands/mutations/core-editing.mutation';// Used to record all intermediate states when typing with IME,@@ -42,68 +42,27 @@ export class IMEInputManagerService implements IDisposable {this._redoMutationParamsCache.push(redoParams);}- fetchComposedUndoRedoMutationParams(newText: string) {- if (this._undoMutationParamsCache.length === 0 || this._previousActiveRange == null) {+ fetchComposedUndoRedoMutationParams() {+ if (this._undoMutationParamsCache.length === 0 || this._previousActiveRange == null || this._redoMutationParamsCache.length === 0) {return null;}const { unitId } = this._undoMutationParamsCache[0];- const { segmentId, startOffset, collapsed } = this._previousActiveRange;const undoMutationParams: IRichTextEditingMutationParams = {unitId,- mutations: [],+ mutations: this._undoMutationParamsCache.reverse().reduce((acc, cur) => {+ return TextX.compose(acc, cur.mutations);+ }, [] as TextXAction[]),};const redoMutationParams: IRichTextEditingMutationParams = {unitId,- mutations: [],+ mutations: this._redoMutationParamsCache.reduce((acc, cur) => {+ return TextX.compose(acc, cur.mutations);+ }, [] as TextXAction[]),};- if (collapsed) {- undoMutationParams.mutations.push({- t: TextXActionType.RETAIN,- len: startOffset,- segmentId,- });- redoMutationParams.mutations.push({- t: TextXActionType.RETAIN,- len: startOffset,- segmentId,- });- } else {- if (startOffset > 0) {- undoMutationParams.mutations.push({- t: TextXActionType.RETAIN,- len: startOffset,- segmentId,- });- }-- 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: TextXActionType.DELETE,- len: newText.length,- line: 0,- segmentId,- });-- redoMutationParams.mutations.push({- t: TextXActionType.INSERT,- body: {- dataStream: newText,- },- len: newText.length,- line: 0,- segmentId,- });- }-return { redoMutationParams, undoMutationParams, previousActiveRange: this._previousActiveRange };}