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(sheets-ui): improve focus handling EditorContainer (#6770)

白熱70720246cebc
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 d5911980e5..90f2ac2f04 100644
--- a/packages/sheets-formula-ui/src/views/formula-editor/index.tsx
+++ b/packages/sheets-formula-ui/src/views/formula-editor/index.tsx
@@ -244,9 +244,23 @@ export const FormulaEditor = forwardRef((props: IFormulaEditorProps, ref: Ref<IF
}, []);
useLayoutEffect(() => {
+ let focusRetryFrame = 0;
+ let finalFocusRetryFrame = 0;
+
+ const retryFocus = () => {
+ if (_isFocus && !docSelectionRenderService?.isFocusing) {
+ focus();
+ }
+ };
+
if (_isFocus) {
setIsFocus(_isFocus);
focus();
+ // In Shadow DOM hosts, canvas dblclick can steal focus back after the editor becomes visible.
+ focusRetryFrame = requestAnimationFrame(() => {
+ retryFocus();
+ finalFocusRetryFrame = requestAnimationFrame(retryFocus);
+ });
} else {
if (resetSelectionOnBlur) {
editor?.blur();
@@ -254,7 +268,12 @@ export const FormulaEditor = forwardRef((props: IFormulaEditorProps, ref: Ref<IF
}
setIsFocus(_isFocus);
}
- }, [_isFocus, editor, focus, resetSelection, resetSelectionOnBlur]);
+
+ return () => {
+ cancelAnimationFrame(focusRetryFrame);
+ cancelAnimationFrame(finalFocusRetryFrame);
+ };
+ }, [_isFocus, docSelectionRenderService, editor, focus, resetSelection, resetSelectionOnBlur]);
const { checkScrollBar } = useResize(editor, isSingle, autoScrollbar);
useRefactorEffect(isFocus, Boolean(isSelecting && docFocusing), unitId, editorId, disableContextMenu);
diff --git a/packages/sheets-ui/src/views/editor-container/EditorContainer.tsx b/packages/sheets-ui/src/views/editor-container/EditorContainer.tsx
index 316fcfb571..9fb09fb5aa 100644
--- a/packages/sheets-ui/src/views/editor-container/EditorContainer.tsx
+++ b/packages/sheets-ui/src/views/editor-container/EditorContainer.tsx
@@ -16,7 +16,7 @@
import type { KeyCode } from '@univerjs/ui';
import { DOCS_NORMAL_EDITOR_UNIT_ID_KEY, ICommandService, IContextService } from '@univerjs/core';
-import { IEditorService } from '@univerjs/docs-ui';
+import { DocSelectionRenderService, IEditorService } from '@univerjs/docs-ui';
import { DeviceInputEventType } from '@univerjs/engine-render';
import { ComponentManager, DISABLE_AUTO_FOCUS_KEY, MetaKeys, useDependency, useEvent, useObservable, useSidebarClick } from '@univerjs/ui';
import * as React from 'react';
@@ -111,6 +111,35 @@ export const EditorContainer: React.FC<ICellIEditorProps> = () => {
}
}, [disableAutoFocus, state]);
+ useEffect(() => {
+ if (!visible?.visible) {
+ return;
+ }
+
+ let focusRetryFrame = 0;
+ let finalFocusRetryFrame = 0;
+
+ const focusEditor = () => {
+ const editor = editorService.getEditor(DOCS_NORMAL_EDITOR_UNIT_ID_KEY);
+ const docSelectionRenderService = editor?.render.with(DocSelectionRenderService);
+
+ if (!docSelectionRenderService?.isFocusing) {
+ docSelectionRenderService?.focus();
+ }
+ };
+
+ focusEditor();
+ focusRetryFrame = requestAnimationFrame(() => {
+ focusEditor();
+ finalFocusRetryFrame = requestAnimationFrame(focusEditor);
+ });
+
+ return () => {
+ cancelAnimationFrame(focusRetryFrame);
+ cancelAnimationFrame(finalFocusRetryFrame);
+ };
+ }, [editorService, visible?.visible]);
+
const handleClickSideBar = useEvent(() => {
if (editorBridgeService.isVisible().visible) {
commandService.executeCommand(SetCellEditVisibleOperation.id, {