tldraw/state/commands/delete-selected.ts

112 lines
2.7 KiB
TypeScript
Raw Normal View History

2021-06-04 16:08:43 +00:00
import Command from './command'
import history from '../history'
2021-06-26 11:52:36 +00:00
import { Data, Shape } from 'types'
2021-06-29 12:00:59 +00:00
import { deepClone } from 'utils'
import tld from 'utils/tld'
2021-06-21 21:35:28 +00:00
import { getShapeUtils } from 'state/shape-utils'
2021-05-23 17:09:23 +00:00
2021-06-21 21:35:28 +00:00
export default function deleteSelected(data: Data): void {
2021-06-29 12:00:59 +00:00
const selectedShapes = tld.getSelectedShapes(data)
2021-06-18 15:31:46 +00:00
const selectedIdsArr = selectedShapes
.filter((shape) => !shape.isLocked)
.map((shape) => shape.id)
2021-05-23 17:09:23 +00:00
2021-06-26 11:52:36 +00:00
const shapeIdsToDelete = selectedIdsArr.flatMap((id) =>
2021-06-29 12:00:59 +00:00
tld.getDocumentBranch(data, id)
2021-06-26 11:52:36 +00:00
)
2021-05-23 17:09:23 +00:00
2021-06-18 15:31:46 +00:00
const remainingIds = selectedShapes
.filter((shape) => shape.isLocked)
.map((shape) => shape.id)
2021-05-23 17:09:23 +00:00
2021-06-26 11:52:36 +00:00
let deletedShapes: Shape[] = []
2021-05-23 17:09:23 +00:00
history.execute(
data,
new Command({
2021-06-19 17:22:46 +00:00
name: 'delete_selection',
2021-06-04 16:08:43 +00:00
category: 'canvas',
2021-05-23 17:09:23 +00:00
manualSelection: true,
do(data) {
2021-06-26 11:52:36 +00:00
// Update selected ids
2021-06-29 12:00:59 +00:00
tld.setSelectedIds(data, remainingIds)
2021-06-26 11:52:36 +00:00
// Recursively delete shapes (and maybe their parents too)
deletedShapes = deleteShapes(data, shapeIdsToDelete)
2021-05-23 17:09:23 +00:00
},
undo(data) {
2021-06-29 12:00:59 +00:00
const page = tld.getPage(data)
2021-06-04 17:56:46 +00:00
2021-06-26 11:52:36 +00:00
// Update selected ids
2021-06-29 12:00:59 +00:00
tld.setSelectedIds(data, selectedIdsArr)
2021-06-26 11:52:36 +00:00
// Restore deleted shapes
deletedShapes.forEach((shape) => (page.shapes[shape.id] = shape))
// Update parents
deletedShapes.forEach((shape) => {
if (shape.parentId === data.currentPageId) return
const parent = page.shapes[shape.parentId]
getShapeUtils(parent)
.setProperty(parent, 'children', [...parent.children, shape.id])
.onChildrenChange(
parent,
parent.children.map((id) => page.shapes[id])
)
})
2021-05-23 17:09:23 +00:00
},
})
)
}
2021-06-26 11:52:36 +00:00
/** Recursively delete shapes and their parents */
function deleteShapes(
data: Data,
shapeIds: string[],
shapesDeleted: Shape[] = []
): Shape[] {
const parentsToDelete: string[] = []
2021-06-29 12:00:59 +00:00
const page = tld.getPage(data)
2021-06-26 11:52:36 +00:00
const parentIds = new Set(shapeIds.map((id) => page.shapes[id].parentId))
// Delete shapes
shapeIds.forEach((id) => {
shapesDeleted.push(deepClone(page.shapes[id]))
delete page.shapes[id]
})
// Update parents
parentIds.forEach((id) => {
const parent = page.shapes[id]
if (!parent || id === page.id) return
getShapeUtils(parent)
.setProperty(
parent,
'children',
parent.children.filter((childId) => !shapeIds.includes(childId))
)
.onChildrenChange(
parent,
parent.children.map((id) => page.shapes[id])
)
if (getShapeUtils(parent).shouldDelete(parent)) {
parentsToDelete.push(parent.id)
}
})
if (parentsToDelete.length > 0) {
return deleteShapes(data, parentsToDelete, shapesDeleted)
}
return shapesDeleted
}