You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

delete-selected.ts 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import Command from './command'
  2. import history from '../history'
  3. import { TranslateSnapshot } from 'state/sessions/translate-session'
  4. import { Data, ShapeType } from 'types'
  5. import { getDocumentBranch, getPage, updateParents } from 'utils/utils'
  6. import { current } from 'immer'
  7. import { getShapeUtils } from 'lib/shape-utils'
  8. export default function deleteSelected(data: Data) {
  9. const { currentPageId } = data
  10. const selectedIds = Array.from(data.selectedIds.values())
  11. const page = getPage(current(data))
  12. const childrenToDelete = selectedIds
  13. .flatMap((id) => getDocumentBranch(data, id))
  14. .map((id) => page.shapes[id])
  15. data.selectedIds.clear()
  16. history.execute(
  17. data,
  18. new Command({
  19. name: 'delete_shapes',
  20. category: 'canvas',
  21. manualSelection: true,
  22. do(data) {
  23. const page = getPage(data, currentPageId)
  24. for (let id of selectedIds) {
  25. const shape = page.shapes[id]
  26. if (shape.parentId !== data.currentPageId) {
  27. const parent = page.shapes[shape.parentId]
  28. getShapeUtils(parent)
  29. .setProperty(
  30. parent,
  31. 'children',
  32. parent.children.filter((childId) => childId !== shape.id)
  33. )
  34. .onChildrenChange(
  35. parent,
  36. parent.children.map((id) => page.shapes[id])
  37. )
  38. }
  39. for (let shape of childrenToDelete) {
  40. delete page.shapes[shape.id]
  41. }
  42. }
  43. data.selectedIds.clear()
  44. },
  45. undo(data) {
  46. const page = getPage(data, currentPageId)
  47. for (let shape of childrenToDelete) {
  48. page.shapes[shape.id] = shape
  49. }
  50. data.selectedIds = new Set(selectedIds)
  51. },
  52. })
  53. )
  54. }