@deepnoid/canvas 0.1.50 → 0.1.52

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.
@@ -5,7 +5,7 @@ export type CoordinateHistory = {
5
5
  setHistoryIndex: (index: number) => void;
6
6
  init: (coordinates?: Coordinate[]) => void;
7
7
  add: (coordinates: Coordinate[]) => void;
8
- undo: () => Coordinate[];
8
+ undo: () => Coordinate[] | undefined;
9
9
  redo: () => Coordinate[] | undefined;
10
10
  };
11
11
  export declare const createHistory: () => CoordinateHistory;
@@ -17,16 +17,16 @@ export const createHistory = () => {
17
17
  historyIndex++;
18
18
  },
19
19
  undo: () => {
20
- if (historyIndex > -1)
21
- historyIndex--;
22
- return history[historyIndex] || [];
20
+ if (historyIndex <= 0)
21
+ return undefined;
22
+ historyIndex--;
23
+ return history[historyIndex];
23
24
  },
24
25
  redo: () => {
25
- if (historyIndex < history.length - 1) {
26
- historyIndex++;
27
- return history[historyIndex];
28
- }
29
- return undefined;
26
+ if (historyIndex > history.length)
27
+ return undefined;
28
+ historyIndex++;
29
+ return history[historyIndex];
30
30
  },
31
31
  };
32
32
  };
@@ -13,7 +13,7 @@ const AnnotationCanvas = ({ image, coordinates, setCoordinates, options, drawing
13
13
  const { onImageLoadSuccess, onImageLoadError } = events;
14
14
  const canvasRef = useRef(null);
15
15
  const imageRef = useRef(new Image());
16
- const historyRef = useRef(createHistory());
16
+ const historyRef = useRef(null);
17
17
  const [displayCoordinates, setDisplayCoordinates] = useState();
18
18
  useResizeObserver({
19
19
  ref: canvasRef,
@@ -64,6 +64,7 @@ const AnnotationCanvas = ({ image, coordinates, setCoordinates, options, drawing
64
64
  };
65
65
  tempImage.onerror = () => !cancelled && resetCanvas(`Failed to load image: ${image}`);
66
66
  tempImage.src = image;
67
+ historyRef.current = null;
67
68
  return () => {
68
69
  cancelled = true;
69
70
  tempImage.onload = null;
@@ -72,6 +73,10 @@ const AnnotationCanvas = ({ image, coordinates, setCoordinates, options, drawing
72
73
  }, [image, resetOnImageChange]);
73
74
  useEffect(() => {
74
75
  if (imageRef.current.src) {
76
+ if (!historyRef.current) {
77
+ historyRef.current = createHistory();
78
+ historyRef.current.init(coordinates);
79
+ }
75
80
  updateDisplayCoordinates(coordinates);
76
81
  }
77
82
  }, [coordinates, imageRef.current]);
@@ -1,5 +1,5 @@
1
1
  import { DrawEventsParams } from './useDrawEvents';
2
2
  export declare const handleMouseDownRectangle: ({ canvasRef, canvasStateRef, mouseActionRef, mousePointRef, setMousePoint, startMousePointRef, currentCoordinateRef, coordinatesRef, rectangleAnchorRef, }: DrawEventsParams) => (event: globalThis.MouseEvent) => void;
3
3
  export declare const handleMouseMoveRectangle: ({ canvasRef, canvasStateRef, mouseActionRef, mousePointRef, setMousePoint, prevMousePointRef, currentCoordinateRef, coordinatesRef, rectangleAnchorRef, }: DrawEventsParams) => (event: globalThis.MouseEvent) => void;
4
- export declare const handleMouseUpRectangle: ({ canvasRef, canvasStateRef, mouseActionRef, mousePointRef, setMousePoint, startMousePointRef, prevMousePointRef, currentCoordinateRef, coordinatesRef, setCoordinates, rectangleAnchorRef, historyRef, drawLabelRef, }: DrawEventsParams) => (event: globalThis.MouseEvent) => void;
4
+ export declare const handleMouseUpRectangle: ({ canvasRef, canvasStateRef, mouseActionRef, mousePointRef, setMousePoint, startMousePointRef, prevMousePointRef, currentCoordinateRef, coordinatesRef, setCoordinates, rectangleAnchorRef, drawLabelRef, }: DrawEventsParams) => (event: globalThis.MouseEvent) => void;
5
5
  export declare const handleMouseLeaveRectangle: ({ canvasRef, canvasStateRef, setMousePoint, startMousePointRef, prevMousePointRef, mouseActionRef, rectangleAnchorRef, }: DrawEventsParams) => (event: globalThis.MouseEvent) => void;
@@ -36,7 +36,7 @@ export const handleMouseMoveRectangle = ({ canvasRef, canvasStateRef, mouseActio
36
36
  });
37
37
  event.preventDefault();
38
38
  };
39
- export const handleMouseUpRectangle = ({ canvasRef, canvasStateRef, mouseActionRef, mousePointRef, setMousePoint, startMousePointRef, prevMousePointRef, currentCoordinateRef, coordinatesRef, setCoordinates, rectangleAnchorRef, historyRef, drawLabelRef, }) => (event) => {
39
+ export const handleMouseUpRectangle = ({ canvasRef, canvasStateRef, mouseActionRef, mousePointRef, setMousePoint, startMousePointRef, prevMousePointRef, currentCoordinateRef, coordinatesRef, setCoordinates, rectangleAnchorRef, drawLabelRef, }) => (event) => {
40
40
  if (!canvasRef.current || !isMouseClickAction(event.button, MouseAction.LEFT))
41
41
  return;
42
42
  const mousePoint = getCanvasMousePoint(event, canvasRef.current, canvasStateRef.current);
@@ -1,6 +1,5 @@
1
1
  import { RefObject } from 'react';
2
2
  import { Point, Coordinate, CanvasState, Label } from '../../../../types';
3
- import { CoordinateHistory } from '../../../AnnotationCanvas/_utils/createHistory';
4
3
  import { DrawMode, RectangleAnchor } from '../../../../enum/common';
5
4
  import { MouseAction } from '../../../../utils/mouseActions';
6
5
  export type DrawEventsParams = {
@@ -15,7 +14,6 @@ export type DrawEventsParams = {
15
14
  coordinatesRef: RefObject<Coordinate[]>;
16
15
  setCoordinates: (coordinates: Coordinate[]) => void;
17
16
  rectangleAnchorRef: RefObject<RectangleAnchor | null>;
18
- historyRef: RefObject<CoordinateHistory>;
19
17
  maskImageRef: RefObject<HTMLImageElement | null>;
20
18
  drawLabelRef: RefObject<Label | undefined>;
21
19
  };
@@ -10,7 +10,7 @@ type UseMouseParams = {
10
10
  setMousePoint: (point: Point) => void;
11
11
  coordinatesRef?: RefObject<Coordinate[]>;
12
12
  setCoordinates?: (coordinates: Coordinate[]) => void;
13
- historyRef: RefObject<CoordinateHistory>;
13
+ historyRef: RefObject<CoordinateHistory | null>;
14
14
  selectedDrawMode?: DrawMode;
15
15
  };
16
16
  export declare function useMouse({ canvasRef, mouseActionRef, setMousePoint, coordinatesRef, setCoordinates, historyRef, selectedDrawMode, }: UseMouseParams): {
@@ -15,7 +15,7 @@ export function useMouse({ canvasRef, mouseActionRef, setMousePoint, coordinates
15
15
  if (canvasRef.current && isMouseClickAction(event.button, MouseAction.LEFT) && selectedDrawMode) {
16
16
  if (selectedDrawMode === DrawMode.RECTANGLE && coordinatesRef?.current && setCoordinates) {
17
17
  setCoordinates(coordinatesRef.current);
18
- historyRef.current.add(cloneDeep(coordinatesRef.current));
18
+ historyRef.current?.add(cloneDeep(coordinatesRef.current));
19
19
  }
20
20
  }
21
21
  mouseActionRef.current = null;
@@ -5,7 +5,7 @@ type Props = {
5
5
  canvasState: CanvasState;
6
6
  coordinates: Coordinate[] | undefined;
7
7
  setCoordinates?: Dispatch<SetStateAction<Coordinate[] | undefined>>;
8
- historyRef: RefObject<CoordinateHistory>;
8
+ historyRef: RefObject<CoordinateHistory | null>;
9
9
  drawing: AnnotationCanvasDrawing;
10
10
  enableHotkeys?: boolean;
11
11
  editable: boolean;
@@ -46,7 +46,6 @@ const AnnotationLayer = ({ canvasState, coordinates = [], setCoordinates: propSe
46
46
  coordinatesRef,
47
47
  setCoordinates,
48
48
  rectangleAnchorRef,
49
- historyRef,
50
49
  maskImageRef,
51
50
  drawLabelRef,
52
51
  selectedDrawMode,
@@ -65,11 +64,11 @@ const AnnotationLayer = ({ canvasState, coordinates = [], setCoordinates: propSe
65
64
  onDelete: () => {
66
65
  coordinatesRef.current = coordinatesRef.current.filter((coordinate) => !coordinate.selected);
67
66
  setCoordinates(coordinatesRef.current);
68
- historyRef.current.add(cloneDeep(coordinatesRef.current));
67
+ historyRef.current?.add(cloneDeep(coordinatesRef.current));
69
68
  },
70
69
  toggleSelectionOnly: () => setShowSelectedOnly((prev) => !prev),
71
70
  onUndoRedo: (isRedo) => {
72
- const result = isRedo ? historyRef.current.redo() : historyRef.current.undo();
71
+ const result = isRedo ? historyRef.current?.redo() : historyRef.current?.undo();
73
72
  if (result)
74
73
  setCoordinates(result);
75
74
  const coordinate = coordinatesRef.current.find((coordinate) => coordinate.selected);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deepnoid/canvas",
3
- "version": "0.1.50",
3
+ "version": "0.1.52",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "main": "./dist/index.cjs",