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(range-selector): remove dataStream (#4191)

Gpound.liua90779efbf26
diff --git a/packages/sheets-formula-ui/src/views/formula-editor/index.tsx b/packages/sheets-formula-ui/src/views/formula-editor/index.tsx
index 803f5a9ba7..b36b7a2cd0 100644
--- a/packages/sheets-formula-ui/src/views/formula-editor/index.tsx
+++ b/packages/sheets-formula-ui/src/views/formula-editor/index.tsx
@@ -19,7 +19,7 @@ import type { Editor } from '@univerjs/docs-ui';
import type { ReactNode } from 'react';
import { createInternalEditorID, generateRandomId, useDependency } from '@univerjs/core';
import { DocBackScrollRenderController, IEditorService } from '@univerjs/docs-ui';
-import { operatorToken } from '@univerjs/engine-formula';
+import { deserializeRangeWithSheet, LexerTreeBuilder, operatorToken, sequenceNodeType } from '@univerjs/engine-formula';
import { EMBEDDING_FORMULA_EDITOR } from '@univerjs/sheets-ui';
import clsx from 'clsx';
import React, { useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react';
@@ -34,6 +34,9 @@ import { useRefocus } from '../range-selector/hooks/useRefocus';
import { useResetSelection } from '../range-selector/hooks/useResetSelection';
import { useResize } from '../range-selector/hooks/useResize';
import { useSwitchSheet } from '../range-selector/hooks/useSwitchSheet';
+import { rangePreProcess } from '../range-selector/utils/rangePreProcess';
+import { sequenceNodeToText } from '../range-selector/utils/sequenceNodeToText';
+import { unitRangesToText } from '../range-selector/utils/unitRangesToText';
import { HelpFunction } from './help-function/HelpFunction';
import { useFormulaDescribe } from './hooks/useFormulaDescribe';
import { useFormulaSearch } from './hooks/useFormulaSearch';
@@ -69,6 +72,7 @@ export function FormulaEditor(props: IFormulaEditorProps) {
} = props;
const editorService = useDependency(IEditorService);
+ const lexerTreeBuilder = useDependency(LexerTreeBuilder);
const sheetEmbeddingRef = useRef<HTMLDivElement>(null);
const [formulaText, formulaTextSet] = useState(() => {
@@ -103,7 +107,29 @@ export function FormulaEditor(props: IFormulaEditorProps) {
const sequenceNodes = useMemo(() => getFormulaToken(formulaWithoutEqualSymbol), [formulaWithoutEqualSymbol]);
const needEmit = useEmitChange(sequenceNodes, (text: string) => {
- onChange(`=${text}`);
+ const nodes = lexerTreeBuilder.sequenceNodesBuilder(text);
+ if (nodes) {
+ const preNodes = nodes.map((node) => {
+ if (typeof node === 'string') {
+ return node;
+ } else if (node.nodeType === sequenceNodeType.REFERENCE) {
+ // The 'sequenceNodesBuilder' will cache the results.
+ // You Can't modify the reference here. This will cause a cache error
+ const cloneNode = { ...node };
+ const unitRange = deserializeRangeWithSheet(node.token);
+ unitRange.range = rangePreProcess(unitRange.range);
+ if (!isSupportAcrossSheet) {
+ unitRange.sheetName = '';
+ unitRange.unitId = '';
+ }
+ cloneNode.token = unitRangesToText([unitRange], isSupportAcrossSheet)[0];
+ return cloneNode;
+ }
+ return node;
+ });
+ const result = sequenceNodeToText(preNodes);
+ onChange(`=${result}`);
+ }
}, editor);
const highlightDoc = useDocHight('=');
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 904f2a7bb5..5d987fb11d 100644
--- a/packages/sheets-formula-ui/src/views/range-selector/index.tsx
+++ b/packages/sheets-formula-ui/src/views/range-selector/index.tsx
@@ -312,6 +312,9 @@ export function RangeSelector(props: IRangeSelectorProps) {
const handleConfirm = (ranges: IUnitRangeName[]) => {
const text = unitRangesToText(ranges, isSupportAcrossSheet).join(matchToken.COMMA);
+ if (!text) {
+ editor?.setDocumentData({ ...editor.getDocumentData(), body: { dataStream: '\r\n', textRuns: [] } });
+ }
highligh(text);
needEmit();
rangeStringSet(text);