@deepnoid/canvas 0.1.54 → 0.1.55
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.
|
@@ -2,6 +2,7 @@ import { useCallback } from 'react';
|
|
|
2
2
|
import { isMouseClickAction, mapButtonToMouseAction, MouseAction } from '../../../utils/mouseActions';
|
|
3
3
|
import { DrawMode } from '../../../enum/common';
|
|
4
4
|
import { cloneDeep } from '../../../utils/common/cloneDeep';
|
|
5
|
+
import { deepEqual } from '../../../utils/common/deepEqual';
|
|
5
6
|
export const INIT_POINT = { x: 0, y: 0, selected: false };
|
|
6
7
|
export function useMouse({ canvasRef, mouseActionRef, setMousePoint, coordinatesRef, setCoordinates, historyRef, selectedDrawMode, }) {
|
|
7
8
|
const handleMouseDown = useCallback((event) => {
|
|
@@ -15,7 +16,11 @@ export function useMouse({ canvasRef, mouseActionRef, setMousePoint, coordinates
|
|
|
15
16
|
if (canvasRef.current && isMouseClickAction(event.button, MouseAction.LEFT) && selectedDrawMode) {
|
|
16
17
|
if (selectedDrawMode === DrawMode.RECTANGLE && coordinatesRef?.current && setCoordinates) {
|
|
17
18
|
setCoordinates(coordinatesRef.current);
|
|
18
|
-
historyRef.current?.
|
|
19
|
+
const history = historyRef.current?.getHistory();
|
|
20
|
+
const lastHistoryItem = history && history.length > 0 ? history[history.length - 1] : null;
|
|
21
|
+
const cloneCoordinates = cloneDeep(coordinatesRef.current);
|
|
22
|
+
if (!deepEqual(lastHistoryItem, cloneCoordinates))
|
|
23
|
+
historyRef.current?.add(cloneCoordinates);
|
|
19
24
|
}
|
|
20
25
|
}
|
|
21
26
|
mouseActionRef.current = null;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const deepEqual: (obj1: any, obj2: any) => boolean;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export const deepEqual = (obj1, obj2) => {
|
|
2
|
+
if (obj1 === obj2)
|
|
3
|
+
return true;
|
|
4
|
+
if (obj1 == null || obj2 == null)
|
|
5
|
+
return false;
|
|
6
|
+
if (typeof obj1 !== typeof obj2)
|
|
7
|
+
return false;
|
|
8
|
+
if (typeof obj1 !== 'object')
|
|
9
|
+
return obj1 === obj2;
|
|
10
|
+
const isArray1 = Array.isArray(obj1);
|
|
11
|
+
const isArray2 = Array.isArray(obj2);
|
|
12
|
+
if (isArray1 !== isArray2)
|
|
13
|
+
return false;
|
|
14
|
+
if (isArray1 && isArray2) {
|
|
15
|
+
if (obj1.length !== obj2.length)
|
|
16
|
+
return false;
|
|
17
|
+
for (let i = 0; i < obj1.length; i++) {
|
|
18
|
+
if (!deepEqual(obj1[i], obj2[i]))
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
return true;
|
|
22
|
+
}
|
|
23
|
+
const keys1 = Object.keys(obj1);
|
|
24
|
+
const keys2 = Object.keys(obj2);
|
|
25
|
+
if (keys1.length !== keys2.length)
|
|
26
|
+
return false;
|
|
27
|
+
for (const key of keys1) {
|
|
28
|
+
if (!keys2.includes(key))
|
|
29
|
+
return false;
|
|
30
|
+
if (!deepEqual(obj1[key], obj2[key]))
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
return true;
|
|
34
|
+
};
|