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
fix: insert textRuns (#413)
Ran Luo003e3712850c
diff --git a/packages/base-docs/src/commands/mutations/__tests__/apply-utils.spec.ts b/packages/base-docs/src/commands/mutations/__tests__/apply-utils.spec.tsnew file mode 100644index 0000000000..994be59b0c--- /dev/null+++ b/packages/base-docs/src/commands/mutations/__tests__/apply-utils.spec.ts@@ -0,0 +1,356 @@+/* eslint-disable no-magic-numbers */+import { BooleanNumber, IDocumentBody, ITextRun, Nullable, UpdateDocsAttributeType } from '@univerjs/core';+import { afterEach, beforeEach, describe, expect, it } from 'vitest';++import { deleteTextRuns, insertTextRuns } from '../functions/common';+import { coverTextRuns } from '../functions/update-apply';++interface IMockBody {+ textRuns: ITextRun[];+}++describe('example', () => {+ let body: Nullable<IMockBody> = null;+ let updateTextRuns: ITextRun[] = [];++ beforeEach(() => {+ body = {+ textRuns: [+ {+ st: 0,+ ed: 15,+ ts: {+ bl: BooleanNumber.FALSE,+ },+ },+ {+ st: 15,+ ed: 20,+ ts: {+ bl: BooleanNumber.FALSE,+ it: BooleanNumber.TRUE,+ },+ },+ {+ st: 30,+ ed: 40,+ ts: {+ bl: BooleanNumber.FALSE,+ },+ },+ ],+ };++ updateTextRuns = [+ {+ st: 0,+ ed: 10,+ ts: {+ bl: BooleanNumber.TRUE,+ fs: 28,+ },+ },+ ];+ });++ afterEach(() => {+ body = null;+ updateTextRuns = [];+ });++ describe('test cases in function deleteTextRuns', () => {+ it('the inserted textRun is between two testRuns', async () => {+ const removedTextRuns = deleteTextRuns(body as IDocumentBody, 10, 20);++ expect(removedTextRuns.length).toBe(0);+ expect(body?.textRuns[2].st).toBe(20);+ expect(body?.textRuns[2].ed).toBe(30);+ });++ it('the inserted textRun is bigger than one testRuns', async () => {+ const removeTextRuns = deleteTextRuns(body as IDocumentBody, 20, 5);++ expect(removeTextRuns.length).toBe(2);+ expect(body?.textRuns[0].ed).toBe(5);+ expect(body?.textRuns[1].st).toBe(10);+ expect(body?.textRuns[1].ed).toBe(20);+ });++ it('the inserted textRun is smaller than one testRuns', async () => {+ const removeTextRuns = deleteTextRuns(body as IDocumentBody, 2, 16);++ expect(removeTextRuns.length).toBe(1);+ expect(body?.textRuns[1].ed).toBe(18);+ expect(body?.textRuns[2].st).toBe(28);+ expect(body?.textRuns[2].ed).toBe(38);+ });++ it('the inserted textRun is across two testRuns', async () => {+ const removeTextRuns = deleteTextRuns(body as IDocumentBody, 13, 5);++ expect(removeTextRuns.length).toBe(2);+ expect(body?.textRuns[0].ed).toBe(5);+ expect(body?.textRuns[1].st).toBe(5);+ expect(body?.textRuns[1].ed).toBe(7);+ expect(body?.textRuns[2].st).toBe(17);+ expect(body?.textRuns[2].ed).toBe(27);+ });++ it('the inserted textRun is across two testRuns that scattered', async () => {+ const removeTextRuns = deleteTextRuns(body as IDocumentBody, 20, 18);++ expect(removeTextRuns.length).toBe(2);+ expect(body?.textRuns[1].ed).toBe(18);+ expect(body?.textRuns[2].st).toBe(18);+ expect(body?.textRuns[2].ed).toBe(20);+ });+ });++ describe('test cases in function coverTextRuns', () => {+ it('it should return the updateTextRuns when the removeTextRuns is empty', async () => {+ const needUpdateTextRuns = coverTextRuns(updateTextRuns, [], UpdateDocsAttributeType.COVER);+ expect(needUpdateTextRuns.length).toBe(1);+ expect(needUpdateTextRuns[0].st).toBe(0);+ expect(needUpdateTextRuns[0].ed).toBe(10);+ expect(needUpdateTextRuns[0].ts?.bl).toBe(BooleanNumber.TRUE);+ });++ it('it should be passed when the removeTextRuns are continuous', async () => {+ const removeTextRuns = [+ {+ st: 0,+ ed: 5,+ ts: {+ it: BooleanNumber.TRUE,+ bl: BooleanNumber.FALSE,+ },+ },+ {+ st: 5,+ ed: 10,+ ts: {+ it: BooleanNumber.FALSE,+ bl: BooleanNumber.FALSE,+ },+ },+ ];++ const needUpdateTextRuns = coverTextRuns(updateTextRuns, removeTextRuns, UpdateDocsAttributeType.COVER);++ expect(needUpdateTextRuns.length).toBe(2);+ expect(needUpdateTextRuns[0]?.ts?.bl).toBe(BooleanNumber.TRUE);+ expect(needUpdateTextRuns[1]?.ts?.bl).toBe(BooleanNumber.TRUE);+ });++ it('it should be passed when the removeTextRuns are scattered', async () => {+ const removeTextRuns = [+ {+ st: 3,+ ed: 5,+ ts: {+ it: BooleanNumber.TRUE,+ bl: BooleanNumber.FALSE,+ },+ },+ {+ st: 7,+ ed: 9,+ ts: {+ it: BooleanNumber.FALSE,+ bl: BooleanNumber.FALSE,+ },+ },+ ];++ const needUpdateTextRuns = coverTextRuns(updateTextRuns, removeTextRuns, UpdateDocsAttributeType.COVER);++ expect(needUpdateTextRuns.length).toBe(5);+ expect(needUpdateTextRuns[0]?.ts?.bl).toBe(BooleanNumber.TRUE);+ expect(needUpdateTextRuns[1]?.ts?.bl).toBe(BooleanNumber.TRUE);+ expect(needUpdateTextRuns[2]?.ts?.bl).toBe(BooleanNumber.TRUE);+ expect(needUpdateTextRuns[3]?.ts?.bl).toBe(BooleanNumber.TRUE);+ expect(needUpdateTextRuns[4]?.ts?.bl).toBe(BooleanNumber.TRUE);+ });++ it('it should be pass the test when the removeTextRuns and updateTextRuns are both scattered', async () => {+ const updateTextRuns = [+ {+ st: 1,+ ed: 3,+ ts: {+ fs: 28,+ bl: BooleanNumber.TRUE,+ },+ },+ {+ st: 5,+ ed: 10,+ ts: {+ fs: 23,+ bl: BooleanNumber.FALSE,+ },+ },+ ];++ const removeTextRuns = [+ {+ st: 2,+ ed: 4,+ ts: {+ it: BooleanNumber.TRUE,+ bl: BooleanNumber.FALSE,+ },+ },+ {+ st: 6,+ ed: 8,+ ts: {+ it: BooleanNumber.FALSE,+ bl: BooleanNumber.TRUE,+ },+ },+ ];++ const needUpdateTextRuns = coverTextRuns(updateTextRuns, removeTextRuns, UpdateDocsAttributeType.COVER);++ expect(needUpdateTextRuns.length).toBe(6);+ expect(needUpdateTextRuns[0]?.ts?.bl).toBe(BooleanNumber.TRUE);+ expect(needUpdateTextRuns[1]?.ts?.bl).toBe(BooleanNumber.TRUE);+ expect(needUpdateTextRuns[2]?.ts?.bl).toBe(BooleanNumber.FALSE);+ expect(needUpdateTextRuns[3]?.ts?.bl).toBe(BooleanNumber.FALSE);+ expect(needUpdateTextRuns[4]?.ts?.bl).toBe(BooleanNumber.FALSE);+ expect(needUpdateTextRuns[5]?.ts?.bl).toBe(BooleanNumber.FALSE);+ });++ it('it should be pass the test when the removeTextRuns and updateTextRuns are both scattered, and has no intersection', async () => {+ const updateTextRuns = [+ {+ st: 1,+ ed: 2,+ ts: {+ fs: 28,+ bl: BooleanNumber.TRUE,+ },+ },+ {+ st: 5,+ ed: 6,+ ts: {+ fs: 23,+ bl: BooleanNumber.FALSE,+ },+ },+ ];++ const removeTextRuns = [+ {+ st: 3,+ ed: 4,+ ts: {+ it: BooleanNumber.TRUE,+ bl: BooleanNumber.FALSE,+ },+ },+ {+ st: 7,+ ed: 8,+ ts: {+ it: BooleanNumber.FALSE,+ bl: BooleanNumber.TRUE,+ },+ },+ ];++ const needUpdateTextRuns = coverTextRuns(updateTextRuns, removeTextRuns, UpdateDocsAttributeType.COVER);++ expect(needUpdateTextRuns.length).toBe(4);+ expect(needUpdateTextRuns[0]?.ts?.bl).toBe(BooleanNumber.TRUE);+ expect(needUpdateTextRuns[1]?.ts?.bl).toBe(BooleanNumber.FALSE);+ expect(needUpdateTextRuns[2]?.ts?.bl).toBe(BooleanNumber.FALSE);+ expect(needUpdateTextRuns[3]?.ts?.bl).toBe(BooleanNumber.TRUE);+ });+ });++ describe('test cases in function insertTextRuns', () => {+ it('it should pass the case when the insertTextRuns is at the beginning of one testRun', async () => {+ insertTextRuns(+ body as IDocumentBody,+ {+ textRuns: updateTextRuns,+ } as IDocumentBody,+ 10,+ 0+ );++ expect(body?.textRuns.length).toBe(4);+ });++ it('it should pass the case when the insertTextRuns is between original testRuns', async () => {+ insertTextRuns(+ body as IDocumentBody,+ {+ textRuns: updateTextRuns,+ } as IDocumentBody,+ 10,+ 25+ );++ expect(body?.textRuns.length).toBe(4);+ expect(body?.textRuns[0].ts?.bl).toBe(BooleanNumber.FALSE);+ expect(body?.textRuns[1].ts?.bl).toBe(BooleanNumber.FALSE);+ expect(body?.textRuns[2].ts?.bl).toBe(BooleanNumber.TRUE);+ expect(body?.textRuns[3].ts?.bl).toBe(BooleanNumber.FALSE);+ });++ it('it should pass the case when the insertTextRuns is at the end of one testRun', async () => {+ insertTextRuns(+ body as IDocumentBody,+ {+ textRuns: updateTextRuns,+ } as IDocumentBody,+ 10,+ 15+ );++ expect(body?.textRuns.length).toBe(4);+ });++ it('it should pass the case when the insertTextRuns is at the in the middle of one testRun', async () => {+ insertTextRuns(+ body as IDocumentBody,+ {+ textRuns: updateTextRuns,+ } as IDocumentBody,+ 10,+ 10+ );++ expect(body?.textRuns.length).toBe(5);+ });++ it('it should pass the case when the insertTextRuns has the same style with the origin textRun, and should be merged', async () => {+ const updateTextRuns = [+ {+ st: 0,+ ed: 10,+ ts: {+ bl: BooleanNumber.FALSE,+ },+ },+ ];+ insertTextRuns(+ body as IDocumentBody,+ {+ textRuns: updateTextRuns,+ } as IDocumentBody,+ 10,+ 10+ );++ expect(body?.textRuns.length).toBe(3);+ expect(body?.textRuns[0].ts?.bl).toBe(BooleanNumber.FALSE);+ expect(body?.textRuns[1].ts?.bl).toBe(BooleanNumber.FALSE);+ expect(body?.textRuns[2].ts?.bl).toBe(BooleanNumber.FALSE);+ });+ });+});diff --git a/packages/base-docs/src/commands/mutations/functions/common.ts b/packages/base-docs/src/commands/mutations/functions/common.tsindex 09078850d1..3d760de7ef 100644--- a/packages/base-docs/src/commands/mutations/functions/common.ts+++ b/packages/base-docs/src/commands/mutations/functions/common.ts@@ -13,6 +13,36 @@ import {sortRulesFactory,} from '@univerjs/core';+export function normalizeTextRuns(textRuns: ITextRun[]) {+ const results: ITextRun[] = [];++ for (const textRun of textRuns) {+ const { st, ed } = textRun;++ if (st === ed) {+ continue;+ }++ if (results.length === 0) {+ results.push(textRun);+ continue;+ }++ const peak = results.pop()!;+ if (isSameStyleTextRun(textRun, peak)) {+ results.push({+ ...textRun,+ st: peak.st,+ ed,+ });+ } else {+ results.push(peak, textRun);+ }+ }++ return results;+}+/*** Inserting styled text content into the current document model.* @param body The current content object of the document model.@@ -32,92 +62,78 @@ export function insertTextRuns(return;}- let insertIndex = Infinity; // The current index of the textRun where the insertion needs to be made.- for (let i = 0, len = textRuns.length; i < len; i++) {- const textRun = textRuns[i];- const { st, ed } = textRun;-- if (st >= currentIndex) {- // Handles the insertion of text at the beginning of a paragraph- if (st === currentIndex && insertBody.textRuns) {- textRun.st += textLength;- }-- textRun.ed += textLength;-- if (insertIndex === Infinity) {- insertIndex = -Infinity;- }- } else if (ed >= currentIndex) {- /**- * Inserting text at the end of a rich text content,- * with the newly inserted text inheriting the style of that content.- * So, it is necessary to set ed >= currentIndex - 1 to ensure that the new text is inserted while maintaining the style of the existing content.- */- textRun.ed += textLength;+ const newTextRuns: ITextRun[] = [];+ const len = textRuns.length;+ let hasInserted = false;- if (!Number.isFinite(insertIndex)) {- insertIndex = i;- }- }- }-- const insertTextRuns = insertBody.textRuns;-- if (insertTextRuns) {+ const insertTextRuns = insertBody.textRuns ?? [];+ if (insertTextRuns.length) {for (let i = 0, len = insertTextRuns.length; i < len; i++) {const insertTextRun = insertTextRuns[i];insertTextRun.st += currentIndex;insertTextRun.ed += currentIndex;}+ }- if (insertIndex === Infinity) {- textRuns.push(...insertTextRuns);- } else if (insertIndex === -Infinity) {- textRuns.unshift(...insertTextRuns);- } else {- const splitTextRun = textRuns[insertIndex];- const { st, ed } = splitTextRun;-- const startSplitTextRun = {- ...splitTextRun,- st,- ed: insertTextRuns[0].st,- };-- if (isSameStyleTextRun(startSplitTextRun, insertTextRuns[0])) {- startSplitTextRun.ed = insertTextRuns[0].ed;- insertTextRuns.shift();- }+ for (let i = 0; i < len; i++) {+ const textRun = textRuns[i];+ const { st, ed } = textRun;- const lastInsertTextRuns = insertTextRuns[insertTextRuns.length - 1];+ if (ed < currentIndex) {+ newTextRuns.push(textRun);+ } else if (currentIndex >= st && currentIndex <= ed) {+ if (!hasInserted) {+ hasInserted = true;+ textRun.ed += textLength;- if (!lastInsertTextRuns) {- textRuns.splice(insertIndex, 1, startSplitTextRun);+ const pendingTextRuns = [];- return;- }+ const startSplitTextRun = {+ ...textRun,+ st,+ ed: insertTextRuns[0].st,+ };- if (lastInsertTextRuns.ed === ed) {- textRuns.splice(insertIndex, 1, startSplitTextRun, ...insertTextRuns);+ pendingTextRuns.push(startSplitTextRun);+ pendingTextRuns.push(...insertTextRuns);- return;- }+ const lastInsertTextRuns = insertTextRuns[insertTextRuns.length - 1];- const endSplitTextRun = {- ...splitTextRun,- st: lastInsertTextRuns.ed,- ed,- };+ const endSplitTextRun = {+ ...textRun,+ st: lastInsertTextRuns.ed,+ ed: ed + textLength,+ };++ pendingTextRuns.push(endSplitTextRun);- if (isSameStyleTextRun(endSplitTextRun, lastInsertTextRuns)) {- endSplitTextRun.st = lastInsertTextRuns.st;- insertTextRuns.pop();+ newTextRuns.push(...pendingTextRuns);+ } else {+ textRun.st += textLength;+ textRun.ed += textLength;++ newTextRuns.push(textRun);}+ } else {+ // currentIndex < st+ textRun.st += textLength;+ textRun.ed += textLength;- textRuns.splice(insertIndex, 1, startSplitTextRun, ...insertTextRuns, endSplitTextRun);+ if (!hasInserted) {+ hasInserted = true;+ newTextRuns.push(...insertTextRuns);+ }++ newTextRuns.push(textRun);}}++ if (!hasInserted) {+ hasInserted = true;+ newTextRuns.push(...insertTextRuns);+ }++ body.textRuns = normalizeTextRuns(newTextRuns);}/**@@ -334,7 +350,6 @@ export function insertCustomRanges(export function deleteTextRuns(body: IDocumentBody, textLength: number, currentIndex: number) {const { textRuns } = body;const startIndex = currentIndex;-const endIndex = currentIndex + textLength;const removeTextRuns: ITextRun[] = [];@@ -365,10 +380,8 @@ export function deleteTextRuns(body: IDocumentBody, textLength: number, currentIst: startIndex - startIndex,ed: endIndex - startIndex,});- const segments = horizontalLineSegmentsSubtraction(st, ed, startIndex, endIndex - 1);- textRun.st = segments[0];- textRun.ed = segments[1];+ textRun.ed -= textLength;} else if (startIndex >= st && startIndex < ed) {/*** If the cursor start position is within the textRun,@@ -380,6 +393,7 @@ export function deleteTextRuns(body: IDocumentBody, textLength: number, currentIst: startIndex - startIndex,ed: ed - startIndex,});+textRun.ed = startIndex;} else if (endIndex > st && endIndex <= ed) {/**@@ -392,6 +406,7 @@ export function deleteTextRuns(body: IDocumentBody, textLength: number, currentIst: st - startIndex,ed: endIndex - startIndex,});+textRun.st = endIndex - textLength;textRun.ed -= textLength;} else if (st >= endIndex) {diff --git a/packages/base-docs/src/commands/mutations/functions/update-apply.ts b/packages/base-docs/src/commands/mutations/functions/update-apply.tsindex ed753cf0e0..8dbaa2b0cd 100644--- a/packages/base-docs/src/commands/mutations/functions/update-apply.ts+++ b/packages/base-docs/src/commands/mutations/functions/update-apply.ts@@ -9,6 +9,7 @@ import {ITable,ITextRun,Nullable,+ Tools,UpdateDocsAttributeType,} from '@univerjs/core';@@ -23,6 +24,7 @@ import {insertParagraphs,insertTables,insertTextRuns,+ normalizeTextRuns,} from './common';export function UpdateAttributeApply(@@ -96,7 +98,7 @@ function updateTextRuns(const removeTextRuns = deleteTextRuns(body, textLength, currentIndex);if (coverType !== UpdateDocsAttributeType.REPLACE) {- const newUpdateTextRun = coverTextRun(updateDataTextRuns, removeTextRuns, coverType);+ const newUpdateTextRun = coverTextRuns(updateDataTextRuns, removeTextRuns, coverType);updateBody.textRuns = newUpdateTextRun;}@@ -105,12 +107,36 @@ function updateTextRuns(return removeTextRuns;}-function coverTextRun(updateDataTextRuns: ITextRun[], removeTextRuns: ITextRun[], coverType: UpdateDocsAttributeType) {+export function coverTextRuns(+ updateDataTextRuns: ITextRun[],+ removeTextRuns: ITextRun[],+ coverType: UpdateDocsAttributeType+) {+ if (removeTextRuns.length === 0) {+ return updateDataTextRuns;+ }++ updateDataTextRuns = Tools.deepClone(updateDataTextRuns);+ removeTextRuns = Tools.deepClone(removeTextRuns);+const newUpdateTextRun: ITextRun[] = [];+ const updateLength = updateDataTextRuns.length;+ const removeLength = removeTextRuns.length;let updateIndex = 0;let removeIndex = 0;+ let pending: Nullable<ITextRun> = null;- while (updateIndex !== updateDataTextRuns.length && removeIndex !== removeTextRuns.length) {+ function pushPendingAndReturnStatus() {+ if (pending) {+ newUpdateTextRun.push(pending);+ pending = null;+ return true;+ }++ return false;+ }++ while (updateIndex < updateLength && removeIndex < removeLength) {const { st: updateSt, ed: updateEd, ts: updateStyle } = updateDataTextRuns[updateIndex];const { st: removeSt, ed: removeEd, ts: removeStyle, sId } = removeTextRuns[removeIndex];let newTs;@@ -121,24 +147,66 @@ function coverTextRun(updateDataTextRuns: ITextRun[], removeTextRuns: ITextRun[]newTs = { ...updateStyle, ...removeStyle };}- newUpdateTextRun.push({- st: updateSt >= removeSt ? updateSt : removeSt,- ed: updateEd >= removeEd ? removeEd : updateEd,- ts: newTs,- sId,- });+ if (updateEd < removeSt) {+ if (!pushPendingAndReturnStatus()) {+ newUpdateTextRun.push(updateDataTextRuns[updateIndex]);+ }- if (updateEd > removeEd) {- removeIndex++;- } else if (updateEd === removeEd) {updateIndex++;+ } else if (removeEd < updateSt) {+ if (!pushPendingAndReturnStatus()) {+ newUpdateTextRun.push(removeTextRuns[removeIndex]);+ }+removeIndex++;} else {- updateIndex++;+ newUpdateTextRun.push({+ st: Math.min(updateSt, removeSt),+ ed: Math.max(updateSt, removeSt),+ ts: updateSt < removeSt ? { ...updateStyle } : { ...removeStyle },+ sId: updateSt < removeSt ? undefined : sId,+ });++ newUpdateTextRun.push({+ st: Math.max(updateSt, removeSt),+ ed: Math.min(updateEd, removeEd),+ ts: newTs,+ sId,+ });++ if (updateEd < removeEd) {+ updateIndex++;+ removeTextRuns[removeIndex].st = updateEd;+ } else {+ removeIndex++;+ updateDataTextRuns[updateIndex].st = removeEd;+ }++ pending = {+ st: Math.min(updateEd, removeEd),+ ed: Math.max(updateEd, removeEd),+ ts: updateEd < removeEd ? { ...removeStyle } : { ...updateStyle },+ sId: updateEd < removeEd ? sId : undefined,+ };+ }+ }++ pushPendingAndReturnStatus();++ // If the last textRun is also disjoint, then the last textRun needs to be pushed in `newUpdateTextRun`+ const tempTopTextRun = newUpdateTextRun[newUpdateTextRun.length - 1];+ const updateLastTextRun = updateDataTextRuns[updateLength - 1];+ const removeLastTextRun = removeTextRuns[removeLength - 1];++ if (tempTopTextRun.ed !== Math.max(updateLastTextRun.ed, removeLastTextRun.ed)) {+ if (updateLastTextRun.ed > removeLastTextRun.ed) {+ newUpdateTextRun.push(updateLastTextRun);+ } else {+ newUpdateTextRun.push(removeLastTextRun);}}- return newUpdateTextRun;+ return normalizeTextRuns(newUpdateTextRun);}function updateParagraphs(