Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent delete drawing when input focus #3629

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion packages/design/src/components/input-number/InputNumber.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
* limitations under the License.
*/

import type { InputNumberProps } from 'rc-input-number';
import RcInputNumber from 'rc-input-number';
import React, { forwardRef } from 'react';
import type { InputNumberProps } from 'rc-input-number';

import styles from './index.module.less';

Expand Down Expand Up @@ -87,6 +87,16 @@ export interface IInputNumberProps {
* @param e
*/
onPressEnter?: InputNumberProps['onPressEnter'];

/**
* Callback when input is blurred
* @param e
*/
onFocus?: InputNumberProps['onFocus'];
/**
* Callback when input is blurred
*/
onBlur?: InputNumberProps['onBlur'];
}

export const InputNumber = forwardRef<HTMLInputElement, IInputNumberProps>((props, ref) => {
Expand All @@ -103,6 +113,8 @@ export const InputNumber = forwardRef<HTMLInputElement, IInputNumberProps>((prop
onKeyDown,
onChange,
onPressEnter,
onFocus,
onBlur,
} = props;

function handleChange(value: number | null) {
Expand All @@ -127,6 +139,8 @@ export const InputNumber = forwardRef<HTMLInputElement, IInputNumberProps>((prop
onKeyDown={onKeyDown}
onChange={handleChange}
onPressEnter={onPressEnter}
onFocus={onFocus}
onBlur={onBlur}
/>
);
});
43 changes: 37 additions & 6 deletions packages/drawing-ui/src/controllers/drawing-ui.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,22 @@
* limitations under the License.
*/

import { Disposable, ICommandService, Inject, LifecycleStages, OnLifecycle } from '@univerjs/core';
import { Disposable, EDITOR_ACTIVATED, ICommandService, IContextService, Inject, LifecycleStages, OnLifecycle } from '@univerjs/core';
import { ComponentManager } from '@univerjs/ui';
import { SetDrawingAlignOperation } from '../commands/operations/drawing-align.operation';
import { AutoImageCropOperation, CloseImageCropOperation, OpenImageCropOperation } from '../commands/operations/image-crop.operation';
import { ImageResetSizeOperation } from '../commands/operations/image-reset-size.operation';
import { COMPONENT_IMAGE_POPUP_MENU } from '../views/image-popup-menu/component-name';
import { ImagePopupMenu } from '../views/image-popup-menu/ImagePopupMenu';
import { AutoImageCropOperation, CloseImageCropOperation, OpenImageCropOperation } from '../commands/operations/image-crop.operation';
import { ImageViewer } from '../views/image-viewer/ImageViewer';
import { COMPONENT_IMAGE_VIEWER } from '../views/image-viewer/component-name';
import { ImageResetSizeOperation } from '../commands/operations/image-reset-size.operation';
import { SetDrawingAlignOperation } from '../commands/operations/drawing-align.operation';
import { ImageViewer } from '../views/image-viewer/ImageViewer';

@OnLifecycle(LifecycleStages.Rendered, DrawingUIController)
export class DrawingUIController extends Disposable {
constructor(
@Inject(ComponentManager) private readonly _componentManager: ComponentManager,
@ICommandService private readonly _commandService: ICommandService
@ICommandService private readonly _commandService: ICommandService,
@IContextService private readonly _contextService: IContextService
) {
super();

Expand All @@ -51,7 +52,37 @@ export class DrawingUIController extends Disposable {
].forEach((command) => this.disposeWithMe(this._commandService.registerCommand(command)));
}

private _initInputFocus() {
const isEditableElement = (element: EventTarget | null) => {
if (element instanceof HTMLElement) {
return element.isContentEditable
|| element instanceof HTMLInputElement
|| element instanceof HTMLTextAreaElement;
}

return false;
};
const handleFocus = (event: FocusEvent) => {
if (isEditableElement(event.target)) {
this._contextService.setContextValue(EDITOR_ACTIVATED, true);
}
};
const handleBlur = (event: FocusEvent) => {
if (isEditableElement(event.target)) {
this._contextService.setContextValue(EDITOR_ACTIVATED, false);
}
};

document.addEventListener('focus', handleFocus, { capture: true });
document.addEventListener('blur', handleBlur, { capture: true });
this.disposeWithMe(() => {
document.removeEventListener('focus', handleFocus, { capture: true });
document.removeEventListener('blur', handleBlur, { capture: true });
});
}

private _init(): void {
this._initInputFocus();
this._initCommands();
this._initCustomComponents();
}
Expand Down
12 changes: 6 additions & 6 deletions packages/drawing-ui/src/views/panel/DrawingTransform.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@
*/

import type { Nullable } from '@univerjs/core';
import type { IDrawingParam } from '@univerjs/drawing';
import type { IChangeObserverConfig, Scene } from '@univerjs/engine-render';
import { debounce, LocaleService, useDependency } from '@univerjs/core';
import React, { useEffect, useState } from 'react';
import { Checkbox, InputNumber } from '@univerjs/design';
import clsx from 'clsx';
import type { IChangeObserverConfig, Scene } from '@univerjs/engine-render';
import { IRenderManagerService } from '@univerjs/engine-render';
import type { IDrawingParam } from '@univerjs/drawing';
import { IDrawingManagerService } from '@univerjs/drawing';
import { getUpdateParams } from '../../utils/get-update-params';
import { IRenderManagerService } from '@univerjs/engine-render';
import clsx from 'clsx';
import React, { useEffect, useState } from 'react';
import { MIN_DRAWING_HEIGHT_LIMIT, MIN_DRAWING_WIDTH_LIMIT, RANGE_DRAWING_ROTATION_LIMIT } from '../../utils/config';
import { getUpdateParams } from '../../utils/get-update-params';
import styles from './index.module.less';

export interface IDrawingTransformProps {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,28 @@
*/

import { Disposable, ICommandService, Inject, Injector, LifecycleStages, OnLifecycle } from '@univerjs/core';
import { ComponentManager, IMenuManagerService, IShortcutService } from '@univerjs/ui';
import { IDrawingManagerService } from '@univerjs/drawing';

import { AddImageSingle } from '@univerjs/icons';
import { IMAGE_UPLOAD_ICON } from '../views/menu/image.menu';
import { ComponentManager, IMenuManagerService, IShortcutService } from '@univerjs/ui';
import { DeleteDrawingsCommand } from '../commands/commands/delete-drawings.command';
import { GroupSheetDrawingCommand } from '../commands/commands/group-sheet-drawing.command';
import { InsertFloatImageCommand } from '../commands/commands/insert-image.command';
import { InsertSheetDrawingCommand } from '../commands/commands/insert-sheet-drawing.command';
import { MoveDrawingsCommand } from '../commands/commands/move-drawings.command';
import { RemoveSheetDrawingCommand } from '../commands/commands/remove-sheet-drawing.command';
import { SetSheetDrawingCommand } from '../commands/commands/set-sheet-drawing.command';
import { COMPONENT_SHEET_DRAWING_PANEL } from '../views/sheet-image-panel/component-name';
import { SheetDrawingPanel } from '../views/sheet-image-panel/SheetDrawingPanel';

import { SetDrawingArrangeCommand } from '../commands/commands/set-drawing-arrange.command';
import { SetSheetDrawingCommand } from '../commands/commands/set-sheet-drawing.command';
import { UngroupSheetDrawingCommand } from '../commands/commands/ungroup-sheet-drawing.command';
import { ClearSheetDrawingTransformerOperation } from '../commands/operations/clear-drawing-transformer.operation';
import { EditSheetDrawingOperation } from '../commands/operations/edit-sheet-drawing.operation';
import { GroupSheetDrawingCommand } from '../commands/commands/group-sheet-drawing.command';
import { UngroupSheetDrawingCommand } from '../commands/commands/ungroup-sheet-drawing.command';
import { SidebarSheetDrawingOperation } from '../commands/operations/open-drawing-panel.operation';
import { MoveDrawingsCommand } from '../commands/commands/move-drawings.command';
import { DeleteDrawingsCommand } from '../commands/commands/delete-drawings.command';
import { SetDrawingArrangeCommand } from '../commands/commands/set-drawing-arrange.command';
import { DeleteDrawingsShortcutItem, MoveDrawingDownShortcutItem, MoveDrawingLeftShortcutItem, MoveDrawingRightShortcutItem, MoveDrawingUpShortcutItem } from './shortcuts/drawing.shortcut';
import { IMAGE_UPLOAD_ICON } from '../views/menu/image.menu';
import { COMPONENT_SHEET_DRAWING_PANEL } from '../views/sheet-image-panel/component-name';
import { SheetDrawingPanel } from '../views/sheet-image-panel/SheetDrawingPanel';
import { menuSchema } from './menu.schema';
import { DeleteDrawingsShortcutItem, MoveDrawingDownShortcutItem, MoveDrawingLeftShortcutItem, MoveDrawingRightShortcutItem, MoveDrawingUpShortcutItem } from './shortcuts/drawing.shortcut';

@OnLifecycle(LifecycleStages.Rendered, SheetDrawingUIController)
export class SheetDrawingUIController extends Disposable {
Expand All @@ -44,7 +45,8 @@ export class SheetDrawingUIController extends Disposable {
@Inject(ComponentManager) private readonly _componentManager: ComponentManager,
@IMenuManagerService private readonly _menuManagerService: IMenuManagerService,
@ICommandService private readonly _commandService: ICommandService,
@IShortcutService private readonly _shortcutService: IShortcutService
@IShortcutService private readonly _shortcutService: IShortcutService,
@IDrawingManagerService private readonly _drawingManagerService: IDrawingManagerService
) {
super();

Expand All @@ -55,6 +57,22 @@ export class SheetDrawingUIController extends Disposable {
const componentManager = this._componentManager;
this.disposeWithMe(componentManager.register(IMAGE_UPLOAD_ICON, AddImageSingle));
this.disposeWithMe(componentManager.register(COMPONENT_SHEET_DRAWING_PANEL, SheetDrawingPanel));

this.disposeWithMe(this._drawingManagerService.remove$.subscribe((drawings) => {
// close the drawing panel when focus drawings are removed
const focusDrawings = this._drawingManagerService.getFocusDrawings();
const removedDrawingIds = drawings.reduce((collection, drawing) => {
collection.add(drawing.drawingId);
return collection;
}, new Set<string>());

const hasRemove = focusDrawings.every((drawing) => removedDrawingIds.has(drawing.drawingId));
if (hasRemove) {
this._commandService.executeCommand(SidebarSheetDrawingOperation.id, {
value: 'close',
});
}
}));
}

private _initMenus(): void {
Expand Down
Loading