mirror of
https://github.com/excalidraw/excalidraw.git
synced 2026-11-25 07:04:47 +03:00
fix(editor): Hit cache soundness (#11630)
--------- Signed-off-by: Mark Tolmacs <mark@lazycat.hu>
This commit is contained in:
1 parent
d331bb49fc
commit
082b2eeb1e
3 files changed
+21
-5
No files matched your search
@@ -115,6 +115,19 @@ let cachedElement: WeakRef<ExcalidrawElement> | null = null;
|
||||
let cachedThreshold: number = Infinity;
|
||||
let cachedHit: boolean = false;
|
||||
let cachedOverrideShouldTestInside = false;
|
||||
let cachedFrameNameBound: FrameNameBounds | null = null;
|
||||
|
||||
const frameNameBoundsEqual = (
|
||||
a: FrameNameBounds | null,
|
||||
b: FrameNameBounds | null,
|
||||
) =>
|
||||
a === b ||
|
||||
(!!a &&
|
||||
!!b &&
|
||||
a.x === b.x &&
|
||||
a.y === b.y &&
|
||||
a.width === b.width &&
|
||||
a.height === b.height);
|
||||
|
||||
export const hitElementItself = ({
|
||||
point,
|
||||
@@ -124,12 +137,16 @@ export const hitElementItself = ({
|
||||
frameNameBound = null,
|
||||
overrideShouldTestInside = false,
|
||||
}: HitTestArgs) => {
|
||||
// Return cached result if the same point and element version is tested again
|
||||
// Return cached result if the same point and element version is tested again.
|
||||
// A cached hit stays valid for any larger threshold, while a cached miss
|
||||
// stays valid only for a threshold no larger than the cached one (a larger
|
||||
// threshold could turn a miss into a hit).
|
||||
if (
|
||||
cachedPoint &&
|
||||
pointsEqual(point, cachedPoint) &&
|
||||
cachedThreshold <= threshold &&
|
||||
overrideShouldTestInside === cachedOverrideShouldTestInside
|
||||
(cachedHit ? cachedThreshold <= threshold : cachedThreshold >= threshold) &&
|
||||
overrideShouldTestInside === cachedOverrideShouldTestInside &&
|
||||
frameNameBoundsEqual(frameNameBound, cachedFrameNameBound)
|
||||
) {
|
||||
const derefElement = cachedElement?.deref();
|
||||
if (
|
||||
@@ -186,6 +203,7 @@ export const hitElementItself = ({
|
||||
cachedElement = new WeakRef(element);
|
||||
cachedThreshold = threshold;
|
||||
cachedOverrideShouldTestInside = overrideShouldTestInside;
|
||||
cachedFrameNameBound = frameNameBound;
|
||||
cachedHit = result;
|
||||
|
||||
return result;
|
||||
|
||||
@@ -1934,7 +1934,6 @@ class App extends React.Component<AppProps, AppState> {
|
||||
y: boxSceneTopLeft.y,
|
||||
width: boxSceneBottomRight.x - boxSceneTopLeft.x,
|
||||
height: boxSceneBottomRight.y - boxSceneTopLeft.y,
|
||||
angle: 0,
|
||||
zoom: this.state.zoom.value,
|
||||
versionNonce: frameElement.versionNonce,
|
||||
};
|
||||
|
||||
@@ -1089,7 +1089,6 @@ export type FrameNameBounds = {
|
||||
y: number;
|
||||
width: number;
|
||||
height: number;
|
||||
angle: number;
|
||||
};
|
||||
|
||||
export type FrameNameBoundsCache = {
|
||||
|
||||
Reference in new issue
Block a user