@deepnoid/canvas 0.1.46 → 0.1.48

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -20,7 +20,8 @@ export type AnnotationCanvasProps = {
20
20
  onImageLoadSuccess?: () => void;
21
21
  onImageLoadError?: (error: Error) => void;
22
22
  };
23
+ enableHotkeys?: boolean;
23
24
  editable?: boolean;
24
25
  };
25
- declare const AnnotationCanvas: ({ image, coordinates, setCoordinates, options, drawing, events, editable, }: AnnotationCanvasProps) => import("react/jsx-runtime").JSX.Element;
26
+ declare const AnnotationCanvas: ({ image, coordinates, setCoordinates, options, drawing, events, enableHotkeys, editable, }: AnnotationCanvasProps) => import("react/jsx-runtime").JSX.Element;
26
27
  export default AnnotationCanvas;
@@ -8,7 +8,7 @@ import { useDebounce } from '../../hooks/useDebounce';
8
8
  import { DrawMode } from '../../enum/common';
9
9
  import { createHistory } from './_utils/createHistory';
10
10
  import { clearCanvasRect, drawCanvas, resolutionCanvas } from '../../utils/canvas';
11
- const AnnotationCanvas = ({ image, coordinates, setCoordinates, options, drawing, events, editable = false, }) => {
11
+ const AnnotationCanvas = ({ image, coordinates, setCoordinates, options, drawing, events, enableHotkeys = false, editable = false, }) => {
12
12
  const { panZoomEnabled, zoom, ZoomButton, resetOnImageChange, timeout = 10000 } = options;
13
13
  const { onImageLoadSuccess, onImageLoadError } = events;
14
14
  const canvasRef = useRef(null);
@@ -86,6 +86,6 @@ const AnnotationCanvas = ({ image, coordinates, setCoordinates, options, drawing
86
86
  if (canvas)
87
87
  redraw(canvas);
88
88
  }, [canvasState]);
89
- return (_jsx("div", { style: { width: '100%', height: '100%', display: 'flex', flex: 1 }, children: _jsxs("div", { onWheel: handleWheel, onMouseMove: handleMouseMove, onMouseDown: handleMouseDown, onMouseUp: handleMouseUp, onMouseLeave: handleMouseLeave, onContextMenu: (e) => e.preventDefault(), style: { flex: 1, position: 'relative', cursor: !panZoomEnabled || editable ? 'default' : 'grab' }, children: [_jsx("canvas", { ref: canvasRef, style: { position: 'absolute', width: '100%', height: '100%', left: 0, top: 0 } }), _jsx(AnnotationLayer, { canvasState: canvasState, coordinates: displayCoordinates, setCoordinates: setCoordinates, historyRef: historyRef, drawing: drawing, editable: editable }), ZoomButton && canvasState.initZoom > 0 && (_jsx(ZoomButton, { onClick: initCanvas, children: `${Math.round((canvasState.zoom / canvasState.initZoom) * 100)}%` }))] }) }));
89
+ return (_jsx("div", { style: { width: '100%', height: '100%', display: 'flex', flex: 1 }, children: _jsxs("div", { onWheel: handleWheel, onMouseMove: handleMouseMove, onMouseDown: handleMouseDown, onMouseUp: handleMouseUp, onMouseLeave: handleMouseLeave, onContextMenu: (e) => e.preventDefault(), style: { flex: 1, position: 'relative', cursor: !panZoomEnabled || editable ? 'default' : 'grab' }, children: [_jsx("canvas", { ref: canvasRef, style: { position: 'absolute', width: '100%', height: '100%', left: 0, top: 0 } }), _jsx(AnnotationLayer, { canvasState: canvasState, coordinates: displayCoordinates, setCoordinates: setCoordinates, historyRef: historyRef, drawing: drawing, enableHotkeys: enableHotkeys, editable: editable }), ZoomButton && canvasState.initZoom > 0 && (_jsx(ZoomButton, { onClick: initCanvas, children: `${Math.round((canvasState.zoom / canvasState.initZoom) * 100)}%` }))] }) }));
90
90
  };
91
91
  export default AnnotationCanvas;
@@ -2,6 +2,7 @@ type Props = {
2
2
  onDelete: () => void;
3
3
  toggleSelectionOnly: () => void;
4
4
  onUndoRedo: (isRedo: boolean) => void;
5
+ enabled?: boolean;
5
6
  };
6
- export declare function useHotkeys({ onDelete, toggleSelectionOnly, onUndoRedo }: Props): void;
7
+ export declare function useHotkeys({ onDelete, toggleSelectionOnly, onUndoRedo, enabled }: Props): void;
7
8
  export {};
@@ -1,6 +1,8 @@
1
1
  import { useEffect } from 'react';
2
- export function useHotkeys({ onDelete, toggleSelectionOnly, onUndoRedo }) {
2
+ export function useHotkeys({ onDelete, toggleSelectionOnly, onUndoRedo, enabled = false }) {
3
3
  useEffect(() => {
4
+ if (!enabled)
5
+ return;
4
6
  const handleKeyDown = (event) => {
5
7
  if (event.key === 'Delete')
6
8
  onDelete();
@@ -11,5 +13,5 @@ export function useHotkeys({ onDelete, toggleSelectionOnly, onUndoRedo }) {
11
13
  };
12
14
  window.addEventListener('keydown', handleKeyDown);
13
15
  return () => window.removeEventListener('keydown', handleKeyDown);
14
- }, [onDelete, toggleSelectionOnly, onUndoRedo]);
16
+ }, [onDelete, toggleSelectionOnly, onUndoRedo, enabled]);
15
17
  }
@@ -7,7 +7,8 @@ type Props = {
7
7
  setCoordinates?: Dispatch<SetStateAction<Coordinate[] | undefined>>;
8
8
  historyRef: RefObject<CoordinateHistory>;
9
9
  drawing: AnnotationCanvasDrawing;
10
+ enableHotkeys?: boolean;
10
11
  editable: boolean;
11
12
  };
12
- declare const AnnotationLayer: ({ canvasState, coordinates, setCoordinates: propSetCoordinates, historyRef, drawing, editable, }: Props) => import("react/jsx-runtime").JSX.Element;
13
+ declare const AnnotationLayer: ({ canvasState, coordinates, setCoordinates: propSetCoordinates, historyRef, drawing, enableHotkeys, editable, }: Props) => import("react/jsx-runtime").JSX.Element;
13
14
  export default AnnotationLayer;
@@ -10,7 +10,7 @@ import { INIT_POINT, useMouse } from './_hooks/useMouse';
10
10
  import { useCanvasDraw } from './_hooks/useCanvasDraw';
11
11
  import { drawCanvas, resolutionCanvas } from '../../utils/canvas';
12
12
  const initCanvasState = { moveX: 0, moveY: 0, zoomX: 0, zoomY: 0, zoom: 0, dx: 0, dy: 0, dw: 0, dh: 0 };
13
- const AnnotationLayer = ({ canvasState, coordinates = [], setCoordinates: propSetCoordinates, historyRef, drawing, editable, }) => {
13
+ const AnnotationLayer = ({ canvasState, coordinates = [], setCoordinates: propSetCoordinates, historyRef, drawing, enableHotkeys, editable, }) => {
14
14
  const { lineSize: drawLineSize, applyStyle, label: drawLabel, mode: selectedDrawMode, color: drawColor } = drawing;
15
15
  const canvasRef = useRef(null);
16
16
  const canvasStateRef = useRef(initCanvasState);
@@ -75,6 +75,7 @@ const AnnotationLayer = ({ canvasState, coordinates = [], setCoordinates: propSe
75
75
  const coordinate = coordinatesRef.current.find((coordinate) => coordinate.selected);
76
76
  currentCoordinateRef.current = coordinate || null;
77
77
  },
78
+ enabled: enableHotkeys,
78
79
  });
79
80
  useEffect(() => {
80
81
  if (!isEqualWith(coordinates, coordinatesRef.current, ignoreImageKey)) {
@@ -40,26 +40,17 @@ export const isEqualWith = (a, b, areValuesEqual) => {
40
40
  };
41
41
  export const ignoreImageKey = (objValue, othValue) => {
42
42
  if (Array.isArray(objValue) && Array.isArray(othValue)) {
43
- if (objValue.length !== othValue.length) {
43
+ if (objValue.length !== othValue.length)
44
44
  return false;
45
- }
46
- for (let i = 0; i < objValue.length; i++) {
47
- if (!isEqualWith(objValue[i], othValue[i], ignoreImageKey)) {
48
- return false;
49
- }
50
- }
51
- return true;
45
+ return objValue.every((item, i) => isEqualWith(item, othValue[i], ignoreImageKey));
52
46
  }
53
- if (typeof objValue === 'object' && typeof othValue === 'object' && objValue !== null && othValue !== null) {
54
- const objKeys = Object.keys(objValue);
55
- const othKeys = Object.keys(othValue);
56
- if (objKeys.length !== othKeys.length) {
57
- return false;
58
- }
59
- for (let key of objKeys) {
60
- if (key === 'image') {
47
+ if (typeof objValue === 'object' && typeof othValue === 'object' && objValue && othValue) {
48
+ const objKeys = Object.keys(objValue).filter((key) => key !== 'image');
49
+ const othKeys = Object.keys(othValue).filter((key) => key !== 'image');
50
+ const allKeys = new Set([...objKeys, ...othKeys]);
51
+ for (const key of allKeys) {
52
+ if (key === 'image')
61
53
  continue;
62
- }
63
54
  if (!isEqualWith(objValue[key], othValue[key], ignoreImageKey)) {
64
55
  return false;
65
56
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deepnoid/canvas",
3
- "version": "0.1.46",
3
+ "version": "0.1.48",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "main": "./dist/index.cjs",