Skip to content

Commit

Permalink
feat(ui): add disable auto focus config (#1682)
Browse files Browse the repository at this point in the history
* feat(ui): add disable auto focus config

* fix: lint
  • Loading branch information
wzhudev committed Apr 10, 2024
1 parent c0b3dce commit 6256c15
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 20 deletions.
Expand Up @@ -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';
Expand Down Expand Up @@ -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();
})
);
Expand Down
Expand Up @@ -48,8 +48,6 @@ export class CellEditorManagerService implements ICellEditorManagerService, IDis

private _rect: Nullable<ICellEditorBoundingClientRect> = null;

private _focus: boolean = false;

private readonly _state$ = new BehaviorSubject<Nullable<ICellEditorManagerParam>>(null);

readonly state$ = this._state$.asObservable();
Expand All @@ -58,8 +56,8 @@ export class CellEditorManagerService implements ICellEditorManagerService, IDis

readonly rect$ = this._rect$.asObservable();

private _focus: boolean = false;
private readonly _focus$ = new BehaviorSubject<boolean>(this._focus);

readonly focus$ = this._focus$.asObservable();

dispose(): void {
Expand Down
24 changes: 17 additions & 7 deletions packages/sheets-ui/src/views/editor-container/EditorContainer.tsx
Expand Up @@ -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;

Expand All @@ -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,
Expand Down Expand Up @@ -102,8 +109,11 @@ export const EditorContainer: React.FC<ICellIEditorProps> = () => {
}, []); // 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 (
<div
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/index.ts
Expand Up @@ -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';
Expand Down
21 changes: 13 additions & 8 deletions packages/ui/src/ui-plugin.ts
Expand Up @@ -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';

Expand Down Expand Up @@ -56,30 +57,34 @@ import { IRangeSelectorService, RangeSelectorService } from './services/range-se

const 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.
*/
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 {
Expand Down

0 comments on commit 6256c15

Please sign in to comment.