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 1 commit
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}
/>
);
});
28 changes: 16 additions & 12 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 { 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 type { IChangeObserverConfig, Scene } from '@univerjs/engine-render';
import { debounce, EDITOR_ACTIVATED, IContextService, LocaleService, useDependency } from '@univerjs/core';
import { Checkbox, InputNumber } from '@univerjs/design';
import { IDrawingManagerService } from '@univerjs/drawing';
import { getUpdateParams } from '../../utils/get-update-params';
import { IRenderManagerService } from '@univerjs/engine-render';
import clsx from 'clsx';
import React, { useCallback, 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 All @@ -38,6 +38,10 @@ export const DrawingTransform = (props: IDrawingTransformProps) => {
const localeService = useDependency(LocaleService);
const drawingManagerService = useDependency(IDrawingManagerService);
const renderManagerService = useDependency(IRenderManagerService);
const contextService = useDependency(IContextService);

const handleFocus = useCallback(() => contextService.setContextValue(EDITOR_ACTIVATED, true), [contextService]);
const handleBlur = useCallback(() => contextService.setContextValue(EDITOR_ACTIVATED, false), [contextService]);

const { drawings, transformShow } = props;

Expand Down Expand Up @@ -385,7 +389,7 @@ export const DrawingTransform = (props: IDrawingTransformProps) => {
</div>
<div className={styles.imageCommonPanelRow}>
<div className={styles.imageCommonPanelColumn}>
<InputNumber precision={1} value={width} onChange={(val) => { handleWidthChange(val); }} className={styles.imageCommonPanelInput} />
<InputNumber onFocus={handleFocus} onBlur={handleBlur} precision={1} value={width} onChange={(val) => { handleWidthChange(val); }} className={styles.imageCommonPanelInput} />
</div>
</div>
</label>
Expand All @@ -399,7 +403,7 @@ export const DrawingTransform = (props: IDrawingTransformProps) => {
</div>
<div className={styles.imageCommonPanelRow}>
<div className={styles.imageCommonPanelColumn}>
<InputNumber precision={1} value={height} onChange={(val) => { handleHeightChange(val); }} className={styles.imageCommonPanelInput} />
<InputNumber onFocus={handleFocus} onBlur={handleBlur} precision={1} value={height} onChange={(val) => { handleHeightChange(val); }} className={styles.imageCommonPanelInput} />
</div>
</div>
</label>
Expand Down Expand Up @@ -429,7 +433,7 @@ export const DrawingTransform = (props: IDrawingTransformProps) => {
</div>
<div className={styles.imageCommonPanelRow}>
<div className={styles.imageCommonPanelColumn}>
<InputNumber precision={1} value={xPosition} onChange={(val) => { handleXChange(val); }} className={styles.imageCommonPanelInput} />
<InputNumber onFocus={handleFocus} onBlur={handleBlur} precision={1} value={xPosition} onChange={(val) => { handleXChange(val); }} className={styles.imageCommonPanelInput} />
</div>
</div>
</label>
Expand All @@ -443,7 +447,7 @@ export const DrawingTransform = (props: IDrawingTransformProps) => {
</div>
<div className={styles.imageCommonPanelRow}>
<div className={styles.imageCommonPanelColumn}>
<InputNumber precision={1} value={yPosition} onChange={(val) => { handleYChange(val); }} className={styles.imageCommonPanelInput} />
<InputNumber onFocus={handleFocus} onBlur={handleBlur} precision={1} value={yPosition} onChange={(val) => { handleYChange(val); }} className={styles.imageCommonPanelInput} />
</div>
</div>
</label>
Expand All @@ -457,7 +461,7 @@ export const DrawingTransform = (props: IDrawingTransformProps) => {
</div>
<div className={styles.imageCommonPanelRow}>
<div className={styles.imageCommonPanelColumn}>
<InputNumber precision={1} value={rotation} onChange={handleRotationChange} className={styles.imageCommonPanelInput} />
<InputNumber onFocus={handleFocus} onBlur={handleBlur} precision={1} value={rotation} onChange={handleRotationChange} className={styles.imageCommonPanelInput} />
</div>
</div>
</label>
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