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: keep range selector documents structurally valid (#7365)

wpxp12345639323d26abe9
diff --git a/packages/docs-ui/src/views/rich-text-editor/hooks/__tests__/use-editor.spec.ts b/packages/docs-ui/src/views/rich-text-editor/hooks/__tests__/use-editor.spec.ts
new file mode 100644
index 0000000000..d3c212e70a
--- /dev/null
+++ b/packages/docs-ui/src/views/rich-text-editor/hooks/__tests__/use-editor.spec.ts
@@ -0,0 +1,57 @@
+/**
+ * Copyright 2023-present DreamNum Co., Ltd.
+ *
+ * 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 type { IDocumentData } from '@univerjs/core';
+import type { RefObject } from 'react';
+import { validateDocBodyStructure } from '@univerjs/core';
+import { describe, expect, it, vi } from 'vitest';
+import { useEditor } from '../use-editor';
+
+const { editor, getEditor, register, setEditor } = vi.hoisted(() => ({
+ editor: {},
+ getEditor: vi.fn(),
+ register: vi.fn(),
+ setEditor: vi.fn(),
+}));
+
+vi.mock('@univerjs/ui', async (importOriginal) => ({
+ ...await importOriginal<typeof import('@univerjs/ui')>(),
+ useDependency: () => ({ getEditor, register }),
+}));
+
+vi.mock('react', async (importOriginal) => ({
+ ...await importOriginal<typeof import('react')>(),
+ useLayoutEffect: (effect: () => void) => effect(),
+ useMemo: (factory: () => unknown) => factory(),
+ useState: () => [undefined, setEditor],
+}));
+
+describe('useEditor', () => {
+ it('registers a structurally valid document for a string initial value', () => {
+ getEditor.mockReturnValue(editor);
+
+ useEditor({
+ editorId: 'range-editor',
+ initialValue: 'A1',
+ container: { current: { clientWidth: 320 } } as RefObject<HTMLDivElement>,
+ isSingle: true,
+ });
+
+ const snapshot = register.mock.calls[0][0].initialSnapshot as IDocumentData;
+ expect(validateDocBodyStructure(snapshot.body!)).toEqual([]);
+ expect(snapshot.body?.paragraphs?.map((paragraph) => paragraph.startIndex)).toEqual([2]);
+ });
+});
diff --git a/packages/docs-ui/src/views/rich-text-editor/hooks/use-editor.ts b/packages/docs-ui/src/views/rich-text-editor/hooks/use-editor.ts
index cb41efaa01..a3b2a037c2 100644
--- a/packages/docs-ui/src/views/rich-text-editor/hooks/use-editor.ts
+++ b/packages/docs-ui/src/views/rich-text-editor/hooks/use-editor.ts
@@ -17,7 +17,7 @@
import type { IDocumentData, Nullable } from '@univerjs/core';
import type { RefObject } from 'react';
import type { Editor, IEditorCanvasStyle } from '../../../services/editor/editor';
-import { createParagraphId, Tools } from '@univerjs/core';
+import { createParagraphId, RichTextBuilder, Tools } from '@univerjs/core';
import { useDependency } from '@univerjs/ui';
import { useLayoutEffect, useMemo, useState } from 'react';
import { IEditorService } from '../../../services/editor/editor-manager.service';
@@ -39,7 +39,9 @@ export function useEditor(opts: IUseEditorProps) {
useLayoutEffect(() => {
if (container.current) {
- const initialDoc = typeof initialValue === 'string' ? undefined : Tools.deepClone(initialValue);
+ const initialDoc = typeof initialValue === 'string'
+ ? RichTextBuilder.create().insertText(initialValue).getData()
+ : Tools.deepClone(initialValue);
const snapshot: IDocumentData = {
body: {
dataStream: typeof initialValue === 'string' ? `${initialValue}\r\n` : '\r\n',
diff --git a/packages/sheets-formula-ui/src/views/range-selector/index.tsx b/packages/sheets-formula-ui/src/views/range-selector/index.tsx
index 187f3ee402..cefb1f6dc1 100644
--- a/packages/sheets-formula-ui/src/views/range-selector/index.tsx
+++ b/packages/sheets-formula-ui/src/views/range-selector/index.tsx
@@ -375,10 +375,9 @@ export function RangeSelector(props: IRangeSelectorProps) {
maxRangeCount={maxRangeCount}
onConfirm={(ranges) => {
const resultStr = stringifyRanges(ranges);
- const empty = RichTextBuilder.newEmptyData();
- empty.body!.dataStream = resultStr;
+ const documentData = RichTextBuilder.create().insertText(resultStr).getData();
editor?.replaceText(resultStr, false);
- onChange?.(empty, resultStr);
+ onChange?.(documentData, resultStr);
setPopupVisible(false);
setRangeSelectorRanges([]);
requestAnimationFrame(() => {