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: fix the issue where formula helpfunction does not display (#5481)

wpxp123456ef5409b84377
diff --git a/packages/sheets-formula-ui/src/views/formula-editor/help-function/HelpFunction.tsx b/packages/sheets-formula-ui/src/views/formula-editor/help-function/HelpFunction.tsx
index 2bd3d3451f..6012aca371 100644
--- a/packages/sheets-formula-ui/src/views/formula-editor/help-function/HelpFunction.tsx
+++ b/packages/sheets-formula-ui/src/views/formula-editor/help-function/HelpFunction.tsx
@@ -21,7 +21,7 @@ import { borderClassName, borderTopClassName, clsx, scrollbarClassName } from '@
import { CloseIcon, MoreIcon } from '@univerjs/icons';
import { IEditorBridgeService } from '@univerjs/sheets-ui';
import { RectPopup, useDependency, useEvent, useObservable } from '@univerjs/ui';
-import { useMemo, useState } from 'react';
+import { useState } from 'react';
import { generateParam } from '../../../services/utils';
import { useEditorPosition } from '../hooks/use-editor-position';
import { useFormulaDescribe } from '../hooks/use-formula-describe';
@@ -95,7 +95,6 @@ const noop = () => { };
export function HelpFunction(props: IHelpFunctionProps) {
const { onParamsSwitch = noop, onClose: propColose = noop, isFocus, editor, formulaText } = props;
const { functionInfo, paramIndex, reset } = useFormulaDescribe(isFocus, formulaText, editor);
- const visible = useMemo(() => !!functionInfo && paramIndex >= 0, [functionInfo, paramIndex]);
const editorBridgeService = useDependency(IEditorBridgeService);
const hidden = !useObservable(editorBridgeService.helpFunctionVisible$);
const [contentVisible, setContentVisible] = useState(true);
@@ -103,7 +102,7 @@ export function HelpFunction(props: IHelpFunctionProps) {
const required = localeService.t('formula.prompt.required');
const optional = localeService.t('formula.prompt.optional');
const editorId = editor.getEditorId();
- const [position$] = useEditorPosition(editorId, visible, [functionInfo, paramIndex]);
+ const [position$] = useEditorPosition(editorId, !!functionInfo, [functionInfo, paramIndex]);
function handleSwitchActive(paramIndex: number) {
onParamsSwitch && onParamsSwitch(paramIndex);
}
@@ -117,7 +116,7 @@ export function HelpFunction(props: IHelpFunctionProps) {
propColose();
};
- return visible && functionInfo
+ return functionInfo
? hidden
? (
<RectPopup key="hidden" portal anchorRect$={position$} direction="left-center">
diff --git a/packages/sheets-formula-ui/src/views/formula-editor/hooks/use-formula-describe.ts b/packages/sheets-formula-ui/src/views/formula-editor/hooks/use-formula-describe.ts
index 7c6dfb4102..19235dfcec 100644
--- a/packages/sheets-formula-ui/src/views/formula-editor/hooks/use-formula-describe.ts
+++ b/packages/sheets-formula-ui/src/views/formula-editor/hooks/use-formula-describe.ts
@@ -21,9 +21,12 @@ import { IDescriptionService } from '@univerjs/sheets-formula';
import { useDependency } from '@univerjs/ui';
import { useEffect, useRef, useState } from 'react';
import { debounceTime, distinctUntilChanged, filter, map } from 'rxjs';
+import { IFormulaPromptService } from '../../../services/prompt.service';
import { useStateRef } from './use-state-ref';
+// eslint-disable-next-line max-lines-per-function
export const useFormulaDescribe = (isNeed: boolean, formulaText: string, editor?: Editor) => {
+ const formulaPromptService = useDependency(IFormulaPromptService);
const descriptionService = useDependency(IDescriptionService);
const lexerTreeBuilder = useDependency(LexerTreeBuilder);
@@ -40,20 +43,37 @@ export const useFormulaDescribe = (isNeed: boolean, formulaText: string, editor?
setIsShow(false);
};
+ useEffect(() => {
+ const nodes = lexerTreeBuilder.sequenceNodesBuilder(formulaText.slice(1));
+ formulaPromptService.setSequenceNodes(nodes ?? []);
+ }, [formulaText]);
+
useEffect(() => {
if (editor && isNeed) {
const d = editor.selectionChange$.pipe(debounceTime(50)).subscribe((e) => {
if (e.textRanges.length === 1) {
const [range] = e.textRanges;
if (range.collapsed && isShowRef.current) {
- // 为什么减1,因为nodes是不包含初始 ‘=’ 字符的,但是 selection 会包含 '='
- const res = lexerTreeBuilder.getFunctionAndParameter(`${formulaTextRef.current}A`, range.startOffset - 1);
- if (res) {
- const { functionName, paramIndex } = res;
- const info = descriptionService.getFunctionInfo(functionName);
- setFunctionInfo(info);
- setParamIndex(paramIndex);
- return;
+ const { startOffset } = range;
+ const currentSequenceNode = formulaPromptService.getCurrentSequenceNode(startOffset - 2);
+
+ if (currentSequenceNode) {
+ if (typeof currentSequenceNode !== 'string' && currentSequenceNode.nodeType === 3 && !descriptionService.hasDefinedNameDescription(currentSequenceNode.token.trim())) {
+ const info = descriptionService.getFunctionInfo(currentSequenceNode.token);
+ setFunctionInfo(info);
+ setParamIndex(-1);
+ return;
+ } else {
+ // 为什么减1,因为nodes是不包含初始 ‘=’ 字符的,但是 selection 会包含 '='
+ const res = lexerTreeBuilder.getFunctionAndParameter(`${formulaTextRef.current}A`, startOffset - 1);
+ if (res) {
+ const { functionName, paramIndex } = res;
+ const info = descriptionService.getFunctionInfo(functionName);
+ setFunctionInfo(info);
+ setParamIndex(paramIndex);
+ return;
+ }
+ }
}
}
}
@@ -61,7 +81,6 @@ export const useFormulaDescribe = (isNeed: boolean, formulaText: string, editor?
setParamIndex(-1);
});
const d2 = editor.selectionChange$.pipe(
- filter((e) => e.isEditing),
filter((e) => e.textRanges.length === 1),
map((e) => e.textRanges[0].startOffset),
distinctUntilChanged()
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 d6d370f045..c3d9fd6784 100644
--- a/packages/sheets-formula-ui/src/views/formula-editor/index.tsx
+++ b/packages/sheets-formula-ui/src/views/formula-editor/index.tsx
@@ -340,7 +340,7 @@ export const FormulaEditor = forwardRef((props: IFormulaEditorProps, ref: Ref<IF
</div>
)
: null}
- {editor
+ {editor && formulaWithoutEqualSymbol !== ''
? (
<HelpFunction
editor={editor}