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.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
refactor(formula-bar): simplify focus logic and fix unitId references (#5636)
WEI ZHANG97d0ac8aa20e
diff --git a/packages/sheets-formula-ui/src/views/formula-editor/hooks/use-refactor-effect.ts b/packages/sheets-formula-ui/src/views/formula-editor/hooks/use-refactor-effect.tsindex aee586c752..c1dae28d1c 100644--- a/packages/sheets-formula-ui/src/views/formula-editor/hooks/use-refactor-effect.ts+++ b/packages/sheets-formula-ui/src/views/formula-editor/hooks/use-refactor-effect.ts@@ -23,7 +23,7 @@ import { IContextMenuService, useDependency, useObservable } from '@univerjs/ui'import { useEffect, useLayoutEffect, useMemo } from 'react';import { RefSelectionsRenderService } from '../../../services/render-services/ref-selections.render-service';-export const useRefactorEffect = (isNeed: boolean, selecting: boolean, unitId: string, disableContextMenu = true) => {+export const useRefactorEffect = (isNeed: boolean, selecting: boolean, unitId: string, editorId: string, disableContextMenu = true) => {const renderManagerService = useDependency(IRenderManagerService);const contextService = useDependency(IContextService);const contextMenuService = useDependency(IContextMenuService);@@ -36,13 +36,19 @@ export const useRefactorEffect = (isNeed: boolean, selecting: boolean, unitId: suseLayoutEffect(() => {if (isNeed) {contextService.setContextValue(EDITOR_ACTIVATED, true);+ disableContextMenu && contextMenuService.disable();return () => {- contextService.setContextValue(EDITOR_ACTIVATED, false);+ const currentDoc = univerInstanceService.getCurrentUnitOfType(UniverInstanceType.UNIVER_DOC)!;+ if (currentDoc.getUnitId() === editorId) {+ contextService.setContextValue(EDITOR_ACTIVATED, false);+ }++ disableContextMenu && contextMenuService.enable();refSelectionsService.clear();};}- }, [contextService, isNeed, refSelectionsService]);+ }, [contextService, isNeed, refSelectionsService, disableContextMenu, editorId]);useLayoutEffect(() => {if (isNeed && selecting) {@@ -56,18 +62,6 @@ export const useRefactorEffect = (isNeed: boolean, selecting: boolean, unitId: s}}, [contextService, isNeed, refSelectionsRenderService, selecting]);- // right context controller- useEffect(() => {- if (isNeed) {- contextService.setContextValue(EDITOR_ACTIVATED, true);- disableContextMenu && contextMenuService.disable();- return () => {- contextService.setContextValue(EDITOR_ACTIVATED, false);- disableContextMenu && contextMenuService.enable();- };- }- }, [contextMenuService, contextService, isNeed, disableContextMenu]);-// reset setSkipLastEnableduseEffect(() => {if (isNeed) {diff --git a/packages/sheets-formula-ui/src/views/formula-editor/index.tsx b/packages/sheets-formula-ui/src/views/formula-editor/index.tsxindex 23e8cef912..4ae1643c72 100644--- a/packages/sheets-formula-ui/src/views/formula-editor/index.tsx+++ b/packages/sheets-formula-ui/src/views/formula-editor/index.tsx@@ -251,7 +251,7 @@ export const FormulaEditor = forwardRef((props: IFormulaEditorProps, ref: Ref<IF}, [_isFocus, editor, focus, resetSelection, resetSelectionOnBlur]);const { checkScrollBar } = useResize(editor, isSingle, autoScrollbar);- useRefactorEffect(isFocus, Boolean(isSelecting && docFocusing), unitId, disableContextMenu);+ useRefactorEffect(isFocus, Boolean(isSelecting && docFocusing), unitId, editorId, disableContextMenu);useLeftAndRightArrow(Boolean(isFocus && isFocusing && moveCursor), selectingMode, editor, onMoveInEditor);const handleSelectionChange = useEvent((refString: string, offset: number, isEnd: boolean) => {diff --git a/packages/sheets-ui/src/controllers/editor/formula-editor.controller.ts b/packages/sheets-ui/src/controllers/editor/formula-editor.controller.tsindex b9f5ed74e6..7e6cc60da0 100644--- a/packages/sheets-ui/src/controllers/editor/formula-editor.controller.ts+++ b/packages/sheets-ui/src/controllers/editor/formula-editor.controller.ts@@ -22,6 +22,7 @@ import type { RenderComponentType } from '@univerjs/engine-render';import type { IEditorBridgeServiceVisibleParam } from '../../services/editor-bridge.service';import {DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY,+ DOCS_NORMAL_EDITOR_UNIT_ID_KEY,EDITOR_ACTIVATED,FOCUSING_EDITOR_BUT_HIDDEN,FOCUSING_FX_BAR_EDITOR,@@ -31,7 +32,6 @@ import {IUndoRedoService,IUniverInstanceService,RxDisposable,- UniverInstanceType,} from '@univerjs/core';import {DocSelectionManagerService,@@ -129,9 +129,6 @@ export class FormulaEditorController extends RxDisposable {if (isFocusButHidden) {this._univerInstanceService.setCurrentUnitForType(DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY);- this._contextService.setContextValue(FOCUSING_FX_BAR_EDITOR, true);-- const currentSheet = this._univerInstanceService.getCurrentUnitForType(UniverInstanceType.UNIVER_SHEET);const formulaEditorDataModel = this._univerInstanceService.getUniverDocInstance(DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY);@@ -143,7 +140,7 @@ export class FormulaEditorController extends RxDisposable {{visible: true,eventType: DeviceInputEventType.PointerDown,- unitId: currentSheet?.getUnitId() ?? '',+ unitId: DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY,} as IEditorBridgeServiceVisibleParam);}@@ -177,7 +174,15 @@ export class FormulaEditorController extends RxDisposable {this._commandService.syncExecuteCommand(CoverContentCommand.id, coverContentParams);requestAnimationFrame(() => {- this._textSelectionManagerService.replaceDocRanges(textRanges);+ this._textSelectionManagerService.replaceDocRanges([], {+ unitId: DOCS_NORMAL_EDITOR_UNIT_ID_KEY,+ subUnitId: DOCS_NORMAL_EDITOR_UNIT_ID_KEY,+ });+ this._textSelectionManagerService.replaceDocRanges(textRanges, {+ unitId: DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY,+ subUnitId: DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY,+ });+ this._contextService.setContextValue(FOCUSING_FX_BAR_EDITOR, true);});}});diff --git a/packages/sheets-ui/src/views/formula-bar/FormulaBar.tsx b/packages/sheets-ui/src/views/formula-bar/FormulaBar.tsxindex bacc001707..f487745154 100644--- a/packages/sheets-ui/src/views/formula-bar/FormulaBar.tsx+++ b/packages/sheets-ui/src/views/formula-bar/FormulaBar.tsx@@ -18,7 +18,6 @@ import type { Workbook } from '@univerjs/core';import type { IEditorBridgeServiceVisibleParam } from '../../services/editor-bridge.service';import {DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY,- EDITOR_ACTIVATED,FOCUSING_FX_BAR_EDITOR,ICommandService,IContextService,@@ -87,7 +86,7 @@ export function FormulaBar(props: IProps) {const formulaAuxUIParts = useComponentsOfPart(SheetsUIPart.FORMULA_AUX);const contextService = useDependency(IContextService);useObservable(useMemo(() => contextService.subscribeContextValue$(FOCUSING_FX_BAR_EDITOR), [contextService]));- const isFocusFxBar = contextService.getContextValue(FOCUSING_FX_BAR_EDITOR) || contextService.getContextValue(EDITOR_ACTIVATED);+ const isFocusFxBar = contextService.getContextValue(FOCUSING_FX_BAR_EDITOR);const ref = useRef<HTMLDivElement>(null);const editorService = useDependency(IEditorService);