From dbbdb764ab128de878f03c671d196334d2cf6706 Mon Sep 17 00:00:00 2001 From: Yao Wang <7734046+Soyn@users.noreply.github.com> Date: Sat, 27 Nov 2021 22:51:17 +0800 Subject: [PATCH] fix(core advance example): fix the error, when erase the shape (#398) --- .../state/actions/erase/eraseShapesAtPoint.ts | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/examples/core-example-advanced/src/state/actions/erase/eraseShapesAtPoint.ts b/examples/core-example-advanced/src/state/actions/erase/eraseShapesAtPoint.ts index 23c536cb1..91f9f6e58 100644 --- a/examples/core-example-advanced/src/state/actions/erase/eraseShapesAtPoint.ts +++ b/examples/core-example-advanced/src/state/actions/erase/eraseShapesAtPoint.ts @@ -1,10 +1,8 @@ import type { Action } from 'state/constants' -import type { TLPointerInfo } from '@tldraw/core' -import { getPagePoint } from 'state/helpers' import { getShapeUtils } from 'shapes' import { mutables } from 'state/mutables' -export const eraseShapesAtPoint: Action = (data, payload: TLPointerInfo) => { +export const eraseShapesAtPoint: Action = (data) => { const { currentPoint } = mutables Object.values(data.page.shapes).forEach((shape) => { @@ -12,4 +10,17 @@ export const eraseShapesAtPoint: Action = (data, payload: TLPointerInfo) => { delete data.page.shapes[shape.id] } }) + + const { shapes } = data.page + const { selectedIds, hoveredId } = data.pageState + + // Filter out any deleted shapes + data.pageState.selectedIds = selectedIds.filter((id) => { + return shapes[id] !== undefined + }) + + // Remove hovered id if it's been deleted + if (hoveredId && !shapes[hoveredId]) { + data.pageState.hoveredId = undefined + } }