Adds more tests, simplifies draw tool
This commit is contained in:
parent
75e60d5eb2
commit
ff58073d12
17 changed files with 521 additions and 20437 deletions
|
@ -1,6 +1,6 @@
|
|||
import Command from './command'
|
||||
import history from '../history'
|
||||
import { Data } from 'types'
|
||||
import { Data, Shape } from 'types'
|
||||
import {
|
||||
deepClone,
|
||||
getDocumentBranch,
|
||||
|
@ -17,16 +17,16 @@ export default function deleteSelected(data: Data): void {
|
|||
.filter((shape) => !shape.isLocked)
|
||||
.map((shape) => shape.id)
|
||||
|
||||
const page = getPage(data)
|
||||
|
||||
const childrenToDelete = selectedIdsArr
|
||||
.flatMap((id) => getDocumentBranch(data, id))
|
||||
.map((id) => deepClone(page.shapes[id]))
|
||||
const shapeIdsToDelete = selectedIdsArr.flatMap((id) =>
|
||||
getDocumentBranch(data, id)
|
||||
)
|
||||
|
||||
const remainingIds = selectedShapes
|
||||
.filter((shape) => shape.isLocked)
|
||||
.map((shape) => shape.id)
|
||||
|
||||
let deletedShapes: Shape[] = []
|
||||
|
||||
history.execute(
|
||||
data,
|
||||
new Command({
|
||||
|
@ -34,57 +34,83 @@ export default function deleteSelected(data: Data): void {
|
|||
category: 'canvas',
|
||||
manualSelection: true,
|
||||
do(data) {
|
||||
const page = getPage(data)
|
||||
|
||||
for (const id of selectedIdsArr) {
|
||||
const shape = page.shapes[id]
|
||||
if (!shape) {
|
||||
console.error('no shape ' + id)
|
||||
continue
|
||||
}
|
||||
|
||||
if (shape.parentId !== data.currentPageId) {
|
||||
const parent = page.shapes[shape.parentId]
|
||||
getShapeUtils(parent)
|
||||
.setProperty(
|
||||
parent,
|
||||
'children',
|
||||
parent.children.filter((childId) => childId !== shape.id)
|
||||
)
|
||||
.onChildrenChange(
|
||||
parent,
|
||||
parent.children.map((id) => page.shapes[id])
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
for (const shape of childrenToDelete) {
|
||||
delete page.shapes[shape.id]
|
||||
}
|
||||
|
||||
// Update selected ids
|
||||
setSelectedIds(data, remainingIds)
|
||||
|
||||
// Recursively delete shapes (and maybe their parents too)
|
||||
deletedShapes = deleteShapes(data, shapeIdsToDelete)
|
||||
},
|
||||
undo(data) {
|
||||
const page = getPage(data)
|
||||
|
||||
for (const shape of childrenToDelete) {
|
||||
page.shapes[shape.id] = shape
|
||||
}
|
||||
|
||||
for (const shape of childrenToDelete) {
|
||||
if (shape.parentId !== data.currentPageId) {
|
||||
const parent = page.shapes[shape.parentId]
|
||||
getShapeUtils(parent)
|
||||
.setProperty(parent, 'children', [...parent.children, shape.id])
|
||||
.onChildrenChange(
|
||||
parent,
|
||||
parent.children.map((id) => page.shapes[id])
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// Update selected ids
|
||||
setSelectedIds(data, selectedIdsArr)
|
||||
|
||||
// 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])
|
||||
)
|
||||
})
|
||||
},
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
/** Recursively delete shapes and their parents */
|
||||
|
||||
function deleteShapes(
|
||||
data: Data,
|
||||
shapeIds: string[],
|
||||
shapesDeleted: Shape[] = []
|
||||
): Shape[] {
|
||||
const parentsToDelete: string[] = []
|
||||
|
||||
const page = getPage(data)
|
||||
|
||||
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
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue