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
feat(ui): add disable auto focus config (#1682)
Wenzhao Hu6256c151caa8
diff --git a/packages/sheets-ui/src/controllers/editor/start-edit.controller.ts b/packages/sheets-ui/src/controllers/editor/start-edit.controller.tsindex be05fb50a3..7b3b58e001 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.tsindex cbfcd173fc..f17616504c 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, IDisprivate _rect: Nullable<ICellEditorBoundingClientRect> = null;- private _focus: boolean = false;-private readonly _state$ = new BehaviorSubject<Nullable<ICellEditorManagerParam>>(null);readonly state$ = this._state$.asObservable();@@ -58,8 +56,8 @@ export class CellEditorManagerService implements ICellEditorManagerService, IDisreadonly rect$ = this._rect$.asObservable();+ private _focus: boolean = false;private readonly _focus$ = new BehaviorSubject<boolean>(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.tsxindex 174a8240e2..6e7547f2c3 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<ICellIEditorProps> = () => {...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<ICellIEditorProps> = () => {}, []); // Empty dependency array means this effect runs once on mount and clean up on unmountuseEffect(() => {- cellEditorManagerService.setFocus(true);- }, [state]);+ if (!disableAutoFocus) {+ cellEditorManagerService.setFocus(true);+ }+ // eslint-disable-next-line react-hooks/exhaustive-deps+ }, [disableAutoFocus, state]);return (<divdiff --git a/packages/ui/src/index.ts b/packages/ui/src/index.tsindex 4a5534a5ee..81ab913ff4 100644--- a/packages/ui/src/index.ts+++ b/packages/ui/src/index.ts@@ -73,7 +73,7 @@ export { DesktopShortcutService, type IShortcutItem, IShortcutService } from './export { DesktopSidebarService } from './services/sidebar/desktop-sidebar.service';export { ISidebarService } from './services/sidebar/sidebar.service';export { IZenZoneService } from './services/zen-zone/zen-zone.service';-export { UniverUIPlugin } from './ui-plugin';+export { UniverUIPlugin, DISABLE_AUTO_FOCUS_KEY } from './ui-plugin';export * from './utils';export { type IConfirmPartMethodOptions } from './views/components/confirm-part/interface';export { IEditorService, EditorService } from './services/editor/editor.service';diff --git a/packages/ui/src/ui-plugin.ts b/packages/ui/src/ui-plugin.tsindex fb5f65aec8..c301b3e082 100644--- a/packages/ui/src/ui-plugin.ts+++ b/packages/ui/src/ui-plugin.ts@@ -14,7 +14,8 @@* limitations under the License.*/-import { ILocalStorageService, LocaleService, Plugin, PluginType } from '@univerjs/core';+import type { IContextService } from '@univerjs/core';+import { IConfigService, ILocalStorageService, LocaleService, Plugin, PluginType } from '@univerjs/core';import type { Dependency } from '@wendellhu/redi';import { Inject, Injector } from '@wendellhu/redi';@@ -56,9 +57,12 @@ import { IRangeSelectorService, RangeSelectorService } from './services/range-seconst PLUGIN_NAME = 'ui';-export interface IUniverUIConfig extends IWorkbenchOptions { }+export interface IUniverUIConfig extends IWorkbenchOptions {+ /** Disable auto focus when Univer bootstraps. */+ disableAutoFocus?: true;+}-const DEFAULT_SLIDE_PLUGIN_DATA = {};+export const DISABLE_AUTO_FOCUS_KEY = 'DISABLE_AUTO_FOCUS';/*** UI plugin provides basic interaction with users. Including workbench (menus, UI parts, notifications etc.), copy paste, shortcut.@@ -66,20 +70,21 @@ const DEFAULT_SLIDE_PLUGIN_DATA = {};export class UniverUIPlugin extends Plugin {static override type = PluginType.Univer;- private _config: IUniverUIConfig;-constructor(- config: Partial<IUniverUIConfig> = {},+ private _config: Partial<IUniverUIConfig> = {},+ @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 {