2021-06-04 16:08:43 +00:00
|
|
|
import Command from './command'
|
|
|
|
import history from '../history'
|
|
|
|
import { TranslateSnapshot } from 'state/sessions/translate-session'
|
2021-06-04 17:56:46 +00:00
|
|
|
import { Data, ShapeType } from 'types'
|
|
|
|
import { getDocumentBranch, getPage, updateParents } from 'utils/utils'
|
2021-06-04 16:08:43 +00:00
|
|
|
import { current } from 'immer'
|
|
|
|
import { getShapeUtils } from 'lib/shape-utils'
|
2021-05-23 17:09:23 +00:00
|
|
|
|
|
|
|
export default function deleteSelected(data: Data) {
|
|
|
|
const { currentPageId } = data
|
|
|
|
|
|
|
|
const selectedIds = Array.from(data.selectedIds.values())
|
|
|
|
|
|
|
|
const page = getPage(current(data))
|
|
|
|
|
2021-06-04 17:56:46 +00:00
|
|
|
const childrenToDelete = selectedIds
|
|
|
|
.flatMap((id) => getDocumentBranch(data, id))
|
|
|
|
.map((id) => page.shapes[id])
|
2021-05-23 17:09:23 +00:00
|
|
|
|
|
|
|
data.selectedIds.clear()
|
|
|
|
|
|
|
|
history.execute(
|
|
|
|
data,
|
|
|
|
new Command({
|
2021-06-04 16:08:43 +00:00
|
|
|
name: 'delete_shapes',
|
|
|
|
category: 'canvas',
|
2021-05-23 17:09:23 +00:00
|
|
|
manualSelection: true,
|
|
|
|
do(data) {
|
|
|
|
const page = getPage(data, currentPageId)
|
|
|
|
|
|
|
|
for (let id of selectedIds) {
|
2021-06-04 16:08:43 +00:00
|
|
|
const shape = page.shapes[id]
|
2021-06-04 21:21:03 +00:00
|
|
|
if (!shape) {
|
|
|
|
console.error('no shape ' + id)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-06-04 16:08:43 +00:00
|
|
|
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])
|
|
|
|
)
|
|
|
|
}
|
2021-06-04 21:21:03 +00:00
|
|
|
}
|
2021-06-04 17:56:46 +00:00
|
|
|
|
2021-06-04 21:21:03 +00:00
|
|
|
for (let shape of childrenToDelete) {
|
|
|
|
delete page.shapes[shape.id]
|
2021-05-23 17:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
data.selectedIds.clear()
|
|
|
|
},
|
|
|
|
undo(data) {
|
|
|
|
const page = getPage(data, currentPageId)
|
2021-06-04 17:56:46 +00:00
|
|
|
|
|
|
|
for (let shape of childrenToDelete) {
|
2021-05-23 17:09:23 +00:00
|
|
|
page.shapes[shape.id] = shape
|
|
|
|
}
|
2021-06-04 17:56:46 +00:00
|
|
|
|
2021-06-04 18:49:27 +00:00
|
|
|
for (let 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])
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-04 17:56:46 +00:00
|
|
|
data.selectedIds = new Set(selectedIds)
|
2021-05-23 17:09:23 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|