From 0cf36d6b30fc84085aa903460a04d0d568518f30 Mon Sep 17 00:00:00 2001 From: David Luzar <5153846+dwelle@users.noreply.github.com> Date: Wed, 16 Apr 2025 10:28:56 +0200 Subject: [PATCH] fix: erasing locked elements (#9400) * fix: erasing locked elements * signature tweaks --- packages/excalidraw/eraser/index.ts | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/packages/excalidraw/eraser/index.ts b/packages/excalidraw/eraser/index.ts index a9f9103f55..2ea668aef2 100644 --- a/packages/excalidraw/eraser/index.ts +++ b/packages/excalidraw/eraser/index.ts @@ -23,7 +23,7 @@ import type { GlobalPoint, LineSegment, } from "@excalidraw/math/types"; -import type { ExcalidrawElement } from "@excalidraw/element/types"; +import type { ElementsMap, ExcalidrawElement } from "@excalidraw/element/types"; import { AnimatedTrail } from "../animated-trail"; @@ -91,7 +91,11 @@ export class EraserTrail extends AnimatedTrail { // take only POINTS_ON_TRAIL points to form some number of segments eraserPath = eraserPath?.slice(eraserPath.length - POINTS_ON_TRAIL); - const visibleElementsMap = arrayToMap(this.app.visibleElements); + const candidateElements = this.app.visibleElements.filter( + (el) => !el.locked, + ); + + const candidateElementsMap = arrayToMap(candidateElements); const pathSegments = eraserPath.reduce((acc, point, index) => { if (index === 0) { @@ -105,7 +109,7 @@ export class EraserTrail extends AnimatedTrail { return []; } - for (const element of this.app.visibleElements) { + for (const element of candidateElements) { // restore only if already added to the to-be-erased set if (restoreToErase && this.elementsToErase.has(element.id)) { const intersects = eraserTest( @@ -113,7 +117,7 @@ export class EraserTrail extends AnimatedTrail { element, this.segmentsCache, this.geometricShapesCache, - visibleElementsMap, + candidateElementsMap, this.app, ); @@ -151,7 +155,7 @@ export class EraserTrail extends AnimatedTrail { element, this.segmentsCache, this.geometricShapesCache, - visibleElementsMap, + candidateElementsMap, this.app, ); @@ -203,14 +207,14 @@ const eraserTest = ( pathSegments: LineSegment[], element: ExcalidrawElement, elementsSegments: ElementsSegmentsMap, - shapesCache = new Map>(), - visibleElementsMap = new Map(), + shapesCache: Map>, + elementsMap: ElementsMap, app: App, ): boolean => { let shape = shapesCache.get(element.id); if (!shape) { - shape = getElementShape(element, visibleElementsMap); + shape = getElementShape(element, elementsMap); shapesCache.set(element.id, shape); } @@ -222,7 +226,7 @@ const eraserTest = ( let elementSegments = elementsSegments.get(element.id); if (!elementSegments) { - elementSegments = getElementLineSegments(element, visibleElementsMap); + elementSegments = getElementLineSegments(element, elementsMap); elementsSegments.set(element.id, elementSegments); }