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.

fix: issues when insertTextRuns is empty (#435)

Ran Luo2496c7c958b9
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.ts
index 994be59b0c..e9fffe0db5 100644
--- a/packages/base-docs/src/commands/mutations/__tests__/apply-utils.spec.ts
+++ b/packages/base-docs/src/commands/mutations/__tests__/apply-utils.spec.ts
@@ -352,5 +352,21 @@ describe('example', () => {
expect(body?.textRuns[1].ts?.bl).toBe(BooleanNumber.FALSE);
expect(body?.textRuns[2].ts?.bl).toBe(BooleanNumber.FALSE);
});
+
+ it('it should pass the case when the insertTextRuns is empty', async () => {
+ insertTextRuns(
+ body as IDocumentBody,
+ {
+ textRuns: [],
+ } as unknown 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.ts
index 3d760de7ef..e851746e17 100644
--- a/packages/base-docs/src/commands/mutations/functions/common.ts
+++ b/packages/base-docs/src/commands/mutations/functions/common.ts
@@ -88,24 +88,28 @@ export function insertTextRuns(
const pendingTextRuns = [];
- const startSplitTextRun = {
- ...textRun,
- st,
- ed: insertTextRuns[0].st,
- };
-
- pendingTextRuns.push(startSplitTextRun);
- pendingTextRuns.push(...insertTextRuns);
-
- const lastInsertTextRuns = insertTextRuns[insertTextRuns.length - 1];
-
- const endSplitTextRun = {
- ...textRun,
- st: lastInsertTextRuns.ed,
- ed: ed + textLength,
- };
-
- pendingTextRuns.push(endSplitTextRun);
+ if (insertTextRuns.length) {
+ const startSplitTextRun = {
+ ...textRun,
+ st,
+ ed: insertTextRuns[0].st,
+ };
+
+ pendingTextRuns.push(startSplitTextRun);
+ pendingTextRuns.push(...insertTextRuns);
+
+ const lastInsertTextRuns = insertTextRuns[insertTextRuns.length - 1];
+
+ const endSplitTextRun = {
+ ...textRun,
+ st: lastInsertTextRuns.ed,
+ ed: ed + textLength,
+ };
+
+ pendingTextRuns.push(endSplitTextRun);
+ } else {
+ pendingTextRuns.push(textRun);
+ }
newTextRuns.push(...pendingTextRuns);
} else {