diff --git a/packages/sheets-ui/src/controllers/editor/start-edit.controller.ts b/packages/sheets-ui/src/controllers/editor/start-edit.controller.ts index be05fb50a3e..7b3b58e001a 100644 --- a/packages/sheets-ui/src/controllers/editor/start-edit.controller.ts +++ b/packages/sheets-ui/src/controllers/editor/start-edit.controller.ts @@ -57,6 +57,7 @@ import { import { IEditorService, KeyCode, SetEditorResizeOperation } from '@univerjs/ui'; import { Inject } from '@wendellhu/redi'; +import { filter } from 'rxjs'; import { getEditorObject } from '../../basics/editor/get-editor-object'; import { SetCellEditVisibleOperation } from '../../commands/operations/cell-edit.operation'; import { ICellEditorManagerService } from '../../services/editor/cell-editor-manager.service'; @@ -147,7 +148,7 @@ export class StartEditController extends Disposable { private _initialCursorSync() { this.disposeWithMe( - this._cellEditorManagerService.focus$.subscribe(() => { + this._cellEditorManagerService.focus$.pipe(filter((f) => !!f)).subscribe(() => { this._textSelectionRenderManager.sync(); }) ); diff --git a/packages/sheets-ui/src/services/editor/cell-editor-manager.service.ts b/packages/sheets-ui/src/services/editor/cell-editor-manager.service.ts index cbfcd173fc1..f17616504c8 100644 --- a/packages/sheets-ui/src/services/editor/cell-editor-manager.service.ts +++ b/packages/sheets-ui/src/services/editor/cell-editor-manager.service.ts @@ -48,8 +48,6 @@ export class CellEditorManagerService implements ICellEditorManagerService, IDis private _rect: Nullable = null; - private _focus: boolean = false; - private readonly _state$ = new BehaviorSubject>(null); readonly state$ = this._state$.asObservable(); @@ -58,8 +56,8 @@ export class CellEditorManagerService implements ICellEditorManagerService, IDis readonly rect$ = this._rect$.asObservable(); + private _focus: boolean = false; private readonly _focus$ = new BehaviorSubject(this._focus); - readonly focus$ = this._focus$.asObservable(); dispose(): void { diff --git a/packages/sheets-ui/src/views/editor-container/EditorContainer.tsx b/packages/sheets-ui/src/views/editor-container/EditorContainer.tsx index 174a8240e24..6e7547f2c31 100644 --- a/packages/sheets-ui/src/views/editor-container/EditorContainer.tsx +++ b/packages/sheets-ui/src/views/editor-container/EditorContainer.tsx @@ -15,16 +15,16 @@ */ import type { IDocumentData } from '@univerjs/core'; -import { DEFAULT_EMPTY_DOCUMENT_VALUE, DOCS_NORMAL_EDITOR_UNIT_ID_KEY } from '@univerjs/core'; +import { DEFAULT_EMPTY_DOCUMENT_VALUE, DOCS_NORMAL_EDITOR_UNIT_ID_KEY, IContextService } from '@univerjs/core'; import { useDependency } from '@wendellhu/redi/react-bindings'; import React, { useEffect, useState } from 'react'; -import { IEditorService, TextEditor } from '@univerjs/ui'; import { FIX_ONE_PIXEL_BLUR_OFFSET } from '@univerjs/engine-render'; +import { DISABLE_AUTO_FOCUS_KEY, IEditorService, TextEditor, useObservable } from '@univerjs/ui'; import { ICellEditorManagerService } from '../../services/editor/cell-editor-manager.service'; import styles from './index.module.less'; -interface ICellIEditorProps {} +interface ICellIEditorProps { } const HIDDEN_EDITOR_POSITION = -1000; @@ -44,9 +44,16 @@ export const EditorContainer: React.FC = () => { ...EDITOR_DEFAULT_POSITION, }); - const cellEditorManagerService: ICellEditorManagerService = useDependency(ICellEditorManagerService); + const cellEditorManagerService = useDependency(ICellEditorManagerService); + const editorService = useDependency(IEditorService); + const contextService = useDependency(IContextService); - const editorService: IEditorService = useDependency(IEditorService); + const disableAutoFocus = useObservable( + () => contextService.subscribeContextValue$(DISABLE_AUTO_FOCUS_KEY), + false, + undefined, + [contextService, DISABLE_AUTO_FOCUS_KEY] + ); const snapshot: IDocumentData = { id: DOCS_NORMAL_EDITOR_UNIT_ID_KEY, @@ -102,8 +109,11 @@ export const EditorContainer: React.FC = () => { }, []); // Empty dependency array means this effect runs once on mount and clean up on unmount useEffect(() => { - cellEditorManagerService.setFocus(true); - }, [state]); + if (!disableAutoFocus) { + cellEditorManagerService.setFocus(true); + } + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [disableAutoFocus, state]); return (
= {}, + private _config: Partial = {}, + @IConfigService private readonly _contextService: IContextService, @Inject(Injector) protected readonly _injector: Injector, @Inject(LocaleService) private readonly _localeService: LocaleService ) { super(PLUGIN_NAME); - this._config = { ...DEFAULT_SLIDE_PLUGIN_DATA, ...config }; - this._localeService.load({ zhCN, }); + + if (this._config.disableAutoFocus) { + this._contextService.setContextValue(DISABLE_AUTO_FOCUS_KEY, true); + } } override onStarting(_injector: Injector): void {