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: optimization of TextX codes (#1297)
Ran Luo12070d481d13
diff --git a/packages/core/src/docs/data-model/__tests__/common.spec.ts b/packages/core/src/docs/data-model/__tests__/common.spec.tsnew file mode 100644index 0000000000..f9f4810431--- /dev/null+++ b/packages/core/src/docs/data-model/__tests__/common.spec.ts@@ -0,0 +1,72 @@+/**+ * 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 { normalizeTextRuns } from '../apply-utils/common';+import { BooleanNumber } from '../../../types/enum/text-style';+import type { ITextRun } from '../../../types/interfaces/i-document-data';++describe('common utils test cases', () => {+ describe('normalizeTextRuns', () => {+ it('should normalize text runs', () => {+ const textRuns: ITextRun[] = [+ {+ st: 0,+ ed: 0,+ ts: {+ bl: BooleanNumber.FALSE,+ },+ },+ {+ st: 0,+ ed: 15,+ sId: undefined,+ ts: {},+ },+ {+ st: 15,+ ed: 30,+ ts: {+ bl: BooleanNumber.FALSE,+ it: BooleanNumber.TRUE,+ },+ },+ {+ st: 30,+ ed: 35,+ ts: {+ bl: BooleanNumber.FALSE,+ it: BooleanNumber.TRUE,+ },+ },+ ];++ const expectedTextRuns: ITextRun[] = [+ {+ st: 15,+ ed: 35,+ ts: {+ bl: BooleanNumber.FALSE,+ it: BooleanNumber.TRUE,+ },+ },+ ];++ expect(normalizeTextRuns(textRuns)).toEqual(expectedTextRuns);+ });+ });+});diff --git a/packages/core/src/docs/data-model/apply-utils/common.ts b/packages/core/src/docs/data-model/apply-utils/common.tsindex c8a19edbb8..c8f40e14b8 100644--- a/packages/core/src/docs/data-model/apply-utils/common.ts+++ b/packages/core/src/docs/data-model/apply-utils/common.ts@@ -32,7 +32,7 @@ export function normalizeTextRuns(textRuns: ITextRun[]) {const results: ITextRun[] = [];for (const textRun of textRuns) {- const { st, ed } = textRun;+ const { st, ed, ts } = textRun;if (textRun.sId === undefined) {delete textRun.sId;@@ -42,6 +42,11 @@ export function normalizeTextRuns(textRuns: ITextRun[]) {continue;}+ // Delete textRun if it has no style(ts is empty or has no sId)+ if (Tools.isEmptyObject(ts) && textRun.sId == null) {+ continue;+ }+if (results.length === 0) {results.push(textRun);continue;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.tsindex 504bf20801..97f1eb4aba 100644--- 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.ts@@ -17,7 +17,9 @@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';+import { composeBody, getBodySlice, isUselessRetainAction } from '../utils';+import type { IRetainAction } from '../../action-types';+import { TextXActionType } from '../../action-types';describe('test text-x utils', () => {it('test getBodySlice fn', () => {@@ -339,4 +341,39 @@ describe('test text-x utils', () => {}],});});++ it('test isUselessRetainAction()', () => {+ const action1: IRetainAction = {+ t: TextXActionType.RETAIN,+ len: 10,+ };++ const action2: IRetainAction = {+ t: TextXActionType.RETAIN,+ len: 10,+ body: {+ dataStream: '',+ textRuns: [],+ },+ };++ const action3: IRetainAction = {+ t: TextXActionType.RETAIN,+ len: 10,+ body: {+ dataStream: '',+ textRuns: [{+ st: 0,+ ed: 10,+ ts: {+ bl: BooleanNumber.FALSE,+ },+ }],+ },+ };++ expect(isUselessRetainAction(action1)).toBe(true);+ expect(isUselessRetainAction(action2)).toBe(true);+ expect(isUselessRetainAction(action3)).toBe(false);+ });});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 beae6ba75c..e2e9da5bb6 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@@ -19,7 +19,7 @@ import type { UpdateDocsAttributeType } from '../../../shared/command-enum';import type { IDocumentBody } from '../../../types/interfaces/i-document-data';import { type IDeleteAction, type IInsertAction, type IRetainAction, type TextXAction, TextXActionType } from '../action-types';import { ActionIterator } from './action-iterator';-import { composeBody } from './utils';+import { composeBody, isUselessRetainAction } from './utils';function onlyHasDataStream(body: IDocumentBody) {return Object.keys(body).length === 1;@@ -200,7 +200,7 @@ export class TextX {trimEndUselessRetainAction(): this {let lastAction = this._actions[this._actions.length - 1];- while (lastAction && lastAction.t === TextXActionType.RETAIN && lastAction.body == null) {+ while (lastAction && lastAction.t === TextXActionType.RETAIN && isUselessRetainAction(lastAction)) {this._actions.pop();lastAction = this._actions[this._actions.length - 1];diff --git a/packages/core/src/docs/data-model/text-x/utils.ts b/packages/core/src/docs/data-model/text-x/utils.tsindex efef339e1c..da2eb7ba1f 100644--- a/packages/core/src/docs/data-model/text-x/utils.ts+++ b/packages/core/src/docs/data-model/text-x/utils.ts@@ -17,6 +17,7 @@import { UpdateDocsAttributeType } from '../../../shared/command-enum';import { Tools } from '../../../shared/tools';import type { IDocumentBody, IParagraph, ITextRun } from '../../../types/interfaces/i-document-data';+import type { IRetainAction } from '../action-types';import { coverTextRuns } from '../apply-utils/update-apply';export function getBodySlice(body: IDocumentBody, startOffset: number, endOffset: number): IDocumentBody {@@ -141,3 +142,19 @@ export function composeBody(return retBody;}++export function isUselessRetainAction(action: IRetainAction): boolean {+ const { body } = action;++ if (body == null) {+ return true;+ }++ const { textRuns = [], paragraphs = [] } = body;++ if (textRuns.length === 0 && paragraphs.length === 0) {+ return true;+ }++ return false;+}