Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(range-selector): remove dataStream #4191

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions packages/sheets-formula-ui/src/views/formula-editor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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';
Expand Down Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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('=');
Expand Down
3 changes: 3 additions & 0 deletions packages/sheets-formula-ui/src/views/range-selector/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down