@deepnoid/canvas 0.1.51 → 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.
- package/dist/components/AnnotationCanvas/_utils/createHistory.d.ts +1 -1
- package/dist/components/AnnotationCanvas/_utils/createHistory.js +3 -7
- package/dist/components/AnnotationCanvas/index.js +5 -3
- package/dist/components/AnnotationLayer/_hooks/drawEvents/useDrawEvents.d.ts +0 -2
- package/dist/components/AnnotationLayer/_hooks/useMouse.d.ts +1 -1
- package/dist/components/AnnotationLayer/_hooks/useMouse.js +1 -1
- package/dist/components/AnnotationLayer/index.d.ts +1 -1
- package/dist/components/AnnotationLayer/index.js +2 -3
- package/package.json +1 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Coordinate } from '../../../types';
|
|
2
2
|
export type CoordinateHistory = {
|
|
3
|
-
getHistory: () => Coordinate[][]
|
|
3
|
+
getHistory: () => Coordinate[][];
|
|
4
4
|
getHistoryIndex: () => number;
|
|
5
5
|
setHistoryIndex: (index: number) => void;
|
|
6
6
|
init: (coordinates?: Coordinate[]) => void;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export const createHistory = () => {
|
|
2
|
-
let history =
|
|
2
|
+
let history = [];
|
|
3
3
|
let historyIndex = -1;
|
|
4
4
|
return {
|
|
5
5
|
getHistory: () => history,
|
|
@@ -12,22 +12,18 @@ export const createHistory = () => {
|
|
|
12
12
|
historyIndex = 0;
|
|
13
13
|
},
|
|
14
14
|
add: (coordinates) => {
|
|
15
|
-
if (history === undefined) {
|
|
16
|
-
history = [];
|
|
17
|
-
historyIndex = -1;
|
|
18
|
-
}
|
|
19
15
|
history.splice(historyIndex + 1);
|
|
20
16
|
history.push(coordinates);
|
|
21
17
|
historyIndex++;
|
|
22
18
|
},
|
|
23
19
|
undo: () => {
|
|
24
|
-
if (
|
|
20
|
+
if (historyIndex <= 0)
|
|
25
21
|
return undefined;
|
|
26
22
|
historyIndex--;
|
|
27
23
|
return history[historyIndex];
|
|
28
24
|
},
|
|
29
25
|
redo: () => {
|
|
30
|
-
if (
|
|
26
|
+
if (historyIndex > history.length)
|
|
31
27
|
return undefined;
|
|
32
28
|
historyIndex++;
|
|
33
29
|
return history[historyIndex];
|
|
@@ -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(
|
|
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,9 +73,10 @@ const AnnotationCanvas = ({ image, coordinates, setCoordinates, options, drawing
|
|
|
72
73
|
}, [image, resetOnImageChange]);
|
|
73
74
|
useEffect(() => {
|
|
74
75
|
if (imageRef.current.src) {
|
|
75
|
-
|
|
76
|
-
|
|
76
|
+
if (!historyRef.current) {
|
|
77
|
+
historyRef.current = createHistory();
|
|
77
78
|
historyRef.current.init(coordinates);
|
|
79
|
+
}
|
|
78
80
|
updateDisplayCoordinates(coordinates);
|
|
79
81
|
}
|
|
80
82
|
}, [coordinates, imageRef.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
|
|
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
|
|
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
|
|
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);
|