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: modify the judgment logic of rich text (#2957)

ybzky041f889509f2
diff --git a/packages/sheets-ui/src/controllers/editor/__tests__/end-edit.controller.spec.ts b/packages/sheets-ui/src/controllers/editor/__tests__/end-edit.controller.spec.ts
index b31b33b09d..409ec85b27 100644
--- a/packages/sheets-ui/src/controllers/editor/__tests__/end-edit.controller.spec.ts
+++ b/packages/sheets-ui/src/controllers/editor/__tests__/end-edit.controller.spec.ts
@@ -15,11 +15,11 @@
*/
import type { ICellData, IDocumentData, Injector, Univer, Workbook } from '@univerjs/core';
-import { CellValueType, IContextService, IResourceLoaderService, LocaleService, LocaleType } from '@univerjs/core';
+import { CellValueType, IContextService, IResourceLoaderService, LocaleService, LocaleType, Tools } from '@univerjs/core';
import { LexerTreeBuilder } from '@univerjs/engine-formula';
import { SpreadsheetSkeleton } from '@univerjs/engine-render';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
-import { getCellDataByInput } from '../editing.render-controller';
+import { getCellDataByInput, isRichText } from '../editing.render-controller';
import { normalizeString } from '../../utils/char-tools';
import { createTestBed } from './create-test-bed';
import { IMockFunctionService, MockFunctionService } from './mock-function.service';
@@ -269,5 +269,38 @@ describe('Test EndEditController', () => {
// '=if(1,“A”,“false ”)'
// '=A1+B2-C3*(D4>=E5)/(F6<G7)'
});
+
+ it('test isRichText util', () => {
+ const cellBody = {
+ dataStream: '11111111\r\n',
+ textRuns: [
+ {
+ ts: {
+ fs: 18,
+ },
+ st: 0,
+ ed: 8,
+ },
+ ],
+ paragraphs: [
+ {
+ startIndex: 8,
+ paragraphStyle: {
+ horizontalAlign: 0,
+ },
+ },
+ ],
+ customRanges: [],
+ customDecorations: [],
+ };
+ const isRichTextRes = isRichText(cellBody);
+ // textRuns acts on the entire string
+ expect(isRichTextRes).toBe(false);
+ const anotherCellBody = Tools.deepClone(cellBody);
+ anotherCellBody.dataStream = `111${anotherCellBody.dataStream}`;
+ // textRuns works on parts of strings
+ const isRichTextRes2 = isRichText(anotherCellBody);
+ expect(isRichTextRes2).toBe(true);
+ });
});
});
diff --git a/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts b/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts
index 42afff5373..a4a4f2c6ae 100644
--- a/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts
+++ b/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts
@@ -1094,11 +1094,13 @@ export function getCellDataByInput(
return cellData;
}
-function isRichText(body: IDocumentBody) {
+export function isRichText(body: IDocumentBody) {
const { textRuns = [], paragraphs = [], customRanges, customBlocks = [] } = body;
+ const bodyNoLineBreak = body.dataStream.replace('\r\n', '');
+
return (
- textRuns.some((textRun) => textRun.ts && !Tools.isEmptyObject(textRun.ts)) ||
+ textRuns.some((textRun) => textRun.ts && (textRun.st !== 0 || textRun.ed !== bodyNoLineBreak.length)) ||
paragraphs.some((paragraph) => paragraph.bullet) ||
paragraphs.length >= 2 ||
Boolean(customRanges?.length) ||