Fix: Only assets that are actually deleted call onAssetDelete (#510)

This commit is contained in:
Faraz Shaikh 2022-01-16 14:00:46 +04:00 committed by GitHub
parent 7c82fbbb65
commit 89797787e8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2588,8 +2588,25 @@ export class TldrawApp extends StateManager<TDSnapshot> {
*/ */
delete = (ids = this.selectedIds): this => { delete = (ids = this.selectedIds): this => {
if (ids.length === 0) return this if (ids.length === 0) return this
ids.forEach((id) => this.getShape(id).assetId && this.callbacks.onAssetDelete?.(id)) const drawCommand = Commands.deleteShapes(this, ids)
return this.setState(Commands.deleteShapes(this, ids))
if (
this.callbacks.onAssetDelete &&
drawCommand.before.document?.assets &&
drawCommand.after.document?.assets
) {
const beforeAssetIds = Object.keys(drawCommand.before.document.assets).filter(
(k) => !!drawCommand.before.document!.assets![k]
)
const afterAssetIds = Object.keys(drawCommand.after.document.assets).filter(
(k) => !!drawCommand.after.document!.assets![k]
)
const intersection = beforeAssetIds.filter((x) => !afterAssetIds.includes(x))
intersection.forEach((id) => this.callbacks.onAssetDelete!(id))
}
return this.setState(drawCommand)
} }
/** /**