Fix erasing bug (#490)

* Update EraseSession.ts

* Update EraseSession.ts
This commit is contained in:
Steve Ruiz 2022-01-06 20:22:49 +00:00 committed by GitHub
parent a793fadf74
commit 97c2b8c4c6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -20,14 +20,14 @@ export class EraseSession extends BaseSession {
erasedShapes = new Set<TDShape>() erasedShapes = new Set<TDShape>()
erasedBindings = new Set<TDBinding>() erasedBindings = new Set<TDBinding>()
initialSelectedShapes: TDShape[] initialSelectedShapes: TDShape[]
erasableShapes: TDShape[] erasableShapes: Set<TDShape>
prevPoint: number[] prevPoint: number[]
constructor(app: TldrawApp) { constructor(app: TldrawApp) {
super(app) super(app)
this.prevPoint = [...app.originPoint] this.prevPoint = [...app.originPoint]
this.initialSelectedShapes = this.app.selectedIds.map((id) => this.app.getShape(id)) this.initialSelectedShapes = this.app.selectedIds.map((id) => this.app.getShape(id))
this.erasableShapes = this.app.shapes.filter((shape) => !shape.isLocked) this.erasableShapes = new Set(this.app.shapes.filter((shape) => !shape.isLocked))
} }
start = (): TldrawPatch | undefined => void null start = (): TldrawPatch | undefined => void null
@ -62,9 +62,8 @@ export class EraseSession extends BaseSession {
const deletedShapeIds = new Set<string>([]) const deletedShapeIds = new Set<string>([])
for (const shape of this.erasableShapes) { this.erasableShapes.forEach((shape) => {
if (this.erasedShapes.has(shape)) continue if (this.erasedShapes.has(shape)) return
if (this.app.getShapeUtil(shape).hitTestLineSegment(shape, this.prevPoint, newPoint)) { if (this.app.getShapeUtil(shape).hitTestLineSegment(shape, this.prevPoint, newPoint)) {
this.erasedShapes.add(shape) this.erasedShapes.add(shape)
deletedShapeIds.add(shape.id) deletedShapeIds.add(shape.id)
@ -76,7 +75,7 @@ export class EraseSession extends BaseSession {
} }
} }
} }
} })
// Erase bindings that reference deleted shapes // Erase bindings that reference deleted shapes
@ -88,6 +87,15 @@ export class EraseSession extends BaseSession {
} }
}) })
this.erasedShapes.forEach((shape) => {
// Has the shape been deleted? If so, pull it from the list.
if (!this.app.getShape(shape.id)) {
this.erasedShapes.delete(shape)
this.erasableShapes.delete(shape)
deletedShapeIds.delete(shape.id)
}
})
const erasedShapes = Array.from(this.erasedShapes.values()) const erasedShapes = Array.from(this.erasedShapes.values())
this.prevPoint = newPoint this.prevPoint = newPoint
@ -106,6 +114,13 @@ export class EraseSession extends BaseSession {
cancel = (): TldrawPatch | undefined => { cancel = (): TldrawPatch | undefined => {
const { page } = this.app const { page } = this.app
this.erasedShapes.forEach((shape) => {
if (!this.app.getShape(shape.id)) {
this.erasedShapes.delete(shape)
this.erasableShapes.delete(shape)
}
})
const erasedShapes = Array.from(this.erasedShapes.values()) const erasedShapes = Array.from(this.erasedShapes.values())
return { return {
@ -127,6 +142,19 @@ export class EraseSession extends BaseSession {
complete = (): TldrawPatch | TldrawCommand | undefined => { complete = (): TldrawPatch | TldrawCommand | undefined => {
const { page } = this.app const { page } = this.app
this.erasedShapes.forEach((shape) => {
if (!this.app.getShape(shape.id)) {
this.erasedShapes.delete(shape)
this.erasableShapes.delete(shape)
}
})
this.erasedBindings.forEach((binding) => {
if (!this.app.getBinding(binding.id)) {
this.erasedBindings.delete(binding)
}
})
const erasedShapes = Array.from(this.erasedShapes.values()) const erasedShapes = Array.from(this.erasedShapes.values())
const erasedBindings = Array.from(this.erasedBindings.values()) const erasedBindings = Array.from(this.erasedBindings.values())
const erasedShapeIds = erasedShapes.map((shape) => shape.id) const erasedShapeIds = erasedShapes.map((shape) => shape.id)
@ -183,7 +211,9 @@ export class EraseSession extends BaseSession {
}, },
pageStates: { pageStates: {
[page.id]: { [page.id]: {
selectedIds: this.initialSelectedShapes.map((shape) => shape.id), selectedIds: this.initialSelectedShapes
.filter((shape) => !!this.app.getShape(shape.id))
.map((shape) => shape.id),
}, },
}, },
}, },
@ -196,6 +226,7 @@ export class EraseSession extends BaseSession {
pageStates: { pageStates: {
[page.id]: { [page.id]: {
selectedIds: this.initialSelectedShapes selectedIds: this.initialSelectedShapes
.filter((shape) => !!this.app.getShape(shape.id))
.filter((shape) => !erasedShapeIds.includes(shape.id)) .filter((shape) => !erasedShapeIds.includes(shape.id))
.map((shape) => shape.id), .map((shape) => shape.id),
}, },