2021-06-04 16:08:43 +00:00
|
|
|
import Command from './command'
|
|
|
|
import history from '../history'
|
2021-06-21 21:35:28 +00:00
|
|
|
import { Data } from 'types'
|
2021-06-07 11:18:50 +00:00
|
|
|
import {
|
2021-06-24 12:34:43 +00:00
|
|
|
deepClone,
|
2021-06-07 11:18:50 +00:00
|
|
|
getDocumentBranch,
|
|
|
|
getPage,
|
2021-06-18 15:31:46 +00:00
|
|
|
getSelectedShapes,
|
2021-06-07 11:18:50 +00:00
|
|
|
setSelectedIds,
|
2021-06-24 08:18:14 +00:00
|
|
|
} from 'utils'
|
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-18 15:31:46 +00:00
|
|
|
const selectedShapes = getSelectedShapes(data)
|
|
|
|
|
|
|
|
const selectedIdsArr = selectedShapes
|
|
|
|
.filter((shape) => !shape.isLocked)
|
|
|
|
.map((shape) => shape.id)
|
2021-05-23 17:09:23 +00:00
|
|
|
|
2021-06-24 12:34:43 +00:00
|
|
|
const page = getPage(data)
|
2021-05-23 17:09:23 +00:00
|
|
|
|
2021-06-07 11:18:50 +00:00
|
|
|
const childrenToDelete = selectedIdsArr
|
2021-06-04 17:56:46 +00:00
|
|
|
.flatMap((id) => getDocumentBranch(data, id))
|
2021-06-24 12:34:43 +00:00
|
|
|
.map((id) => deepClone(page.shapes[id]))
|
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
|
|
|
|
|
|
|
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-24 12:34:43 +00:00
|
|
|
const page = getPage(data)
|
2021-05-23 17:09:23 +00:00
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
for (const id of selectedIdsArr) {
|
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-21 21:35:28 +00:00
|
|
|
for (const shape of childrenToDelete) {
|
2021-06-04 21:21:03 +00:00
|
|
|
delete page.shapes[shape.id]
|
2021-05-23 17:09:23 +00:00
|
|
|
}
|
|
|
|
|
2021-06-18 15:31:46 +00:00
|
|
|
setSelectedIds(data, remainingIds)
|
2021-05-23 17:09:23 +00:00
|
|
|
},
|
|
|
|
undo(data) {
|
2021-06-24 12:34:43 +00:00
|
|
|
const page = getPage(data)
|
2021-06-04 17:56:46 +00:00
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
for (const 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-21 21:35:28 +00:00
|
|
|
for (const shape of childrenToDelete) {
|
2021-06-04 18:49:27 +00:00
|
|
|
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-07 11:18:50 +00:00
|
|
|
setSelectedIds(data, selectedIdsArr)
|
2021-05-23 17:09:23 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|