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(sheet): inline format error when textRuns is empty (#1480)

Ran Luo8f5caae899bc
diff --git a/examples/src/docs/main.ts b/examples/src/docs/main.ts
index e073375c61..e462b40e6e 100644
--- a/examples/src/docs/main.ts
+++ b/examples/src/docs/main.ts
@@ -68,6 +68,7 @@ univer.createUniverDoc(DEFAULT_DOCUMENT_DATA_CN);
// use for console test
declare global {
+ // eslint-disable-next-line ts/naming-convention
interface Window {
univer?: Univer;
}
diff --git a/packages/sheets/src/commands/mutations/set-range-values.mutation.ts b/packages/sheets/src/commands/mutations/set-range-values.mutation.ts
index 9490eb85d7..abd8febe38 100644
--- a/packages/sheets/src/commands/mutations/set-range-values.mutation.ts
+++ b/packages/sheets/src/commands/mutations/set-range-values.mutation.ts
@@ -26,6 +26,7 @@ import type {
IObjectMatrixPrimitiveType,
IRange,
IStyleData,
+ ITextRun,
ITextStyle,
Nullable,
} from '@univerjs/core';
@@ -377,24 +378,75 @@ export function mergeStyle(
* @param newStyle
*/
export function mergeRichTextStyle(p: IDocumentData, newStyle: Nullable<IStyleData>) {
- p.body?.textRuns?.forEach((textRun) => {
- if (!textRun.ts) {
- textRun.ts = {};
+ if (p.body == null) {
+ return;
+ }
+
+ if (!Array.isArray(p.body.textRuns)) {
+ p.body.textRuns = [];
+ }
+
+ let index = 0;
+ const newTextRuns = [];
+
+ for (const textRun of p.body.textRuns) {
+ const { st, ed, ts = {} } = textRun;
+
+ if (index !== st) {
+ const tr: ITextRun = {
+ st: index,
+ ed: st,
+ };
+
+ const merge = mergeStyle({}, newStyle, true);
+
+ // then remove null
+ merge && Tools.removeNull(merge);
+
+ if (!Tools.isEmptyObject(merge)) {
+ tr.ts = merge!;
+ }
+
+ newTextRuns.push(tr);
+ } else {
+ const merge = mergeStyle(ts, newStyle, true);
+
+ // then remove null
+ merge && Tools.removeNull(merge);
+
+ if (Tools.isEmptyObject(merge)) {
+ delete textRun.ts;
+ } else {
+ textRun.ts = merge as ITextStyle;
+ }
+
+ newTextRuns.push(textRun);
}
- const oldStyle = textRun.ts || {};
+ index = ed;
+ }
+
+ const endIndex = p.body.dataStream.endsWith('\r\n') ? p.body.dataStream.length - 2 : p.body.dataStream.length;
- const merge = mergeStyle(oldStyle as Nullable<IStyleData>, newStyle, true);
+ if (index !== endIndex) {
+ const tr: ITextRun = {
+ st: index,
+ ed: endIndex,
+ };
+
+ const merge = mergeStyle({}, newStyle, true);
// then remove null
merge && Tools.removeNull(merge);
- if (Tools.isEmptyObject(merge)) {
- delete textRun.ts;
- } else {
- textRun.ts = merge as ITextStyle;
+ if (!Tools.isEmptyObject(merge)) {
+ tr.ts = merge!;
}
- });
+
+ newTextRuns.push(tr);
+ }
+
+ p.body.textRuns = newTextRuns;
}
function isNumeric(str: string) {