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 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import Command from './command'
  2. import history from '../history'
  3. import { Data } from 'types'
  4. import {
  5. getDocumentBranch,
  6. getPage,
  7. getSelectedShapes,
  8. setSelectedIds,
  9. } from 'utils/utils'
  10. import { current } from 'immer'
  11. import { getShapeUtils } from 'state/shape-utils'
  12. export default function deleteSelected(data: Data): void {
  13. const { currentPageId } = data
  14. const selectedShapes = getSelectedShapes(data)
  15. const selectedIdsArr = selectedShapes
  16. .filter((shape) => !shape.isLocked)
  17. .map((shape) => shape.id)
  18. const page = getPage(current(data))
  19. const childrenToDelete = selectedIdsArr
  20. .flatMap((id) => getDocumentBranch(data, id))
  21. .map((id) => page.shapes[id])
  22. const remainingIds = selectedShapes
  23. .filter((shape) => shape.isLocked)
  24. .map((shape) => shape.id)
  25. history.execute(
  26. data,
  27. new Command({
  28. name: 'delete_selection',
  29. category: 'canvas',
  30. manualSelection: true,
  31. do(data) {
  32. const page = getPage(data, currentPageId)
  33. for (const id of selectedIdsArr) {
  34. const shape = page.shapes[id]
  35. if (!shape) {
  36. console.error('no shape ' + id)
  37. continue
  38. }
  39. if (shape.parentId !== data.currentPageId) {
  40. const parent = page.shapes[shape.parentId]
  41. getShapeUtils(parent)
  42. .setProperty(
  43. parent,
  44. 'children',
  45. parent.children.filter((childId) => childId !== shape.id)
  46. )
  47. .onChildrenChange(
  48. parent,
  49. parent.children.map((id) => page.shapes[id])
  50. )
  51. }
  52. }
  53. for (const shape of childrenToDelete) {
  54. delete page.shapes[shape.id]
  55. }
  56. setSelectedIds(data, remainingIds)
  57. },
  58. undo(data) {
  59. const page = getPage(data, currentPageId)
  60. for (const shape of childrenToDelete) {
  61. page.shapes[shape.id] = shape
  62. }
  63. for (const shape of childrenToDelete) {
  64. if (shape.parentId !== data.currentPageId) {
  65. const parent = page.shapes[shape.parentId]
  66. getShapeUtils(parent)
  67. .setProperty(parent, 'children', [...parent.children, shape.id])
  68. .onChildrenChange(
  69. parent,
  70. parent.children.map((id) => page.shapes[id])
  71. )
  72. }
  73. }
  74. setSelectedIds(data, selectedIdsArr)
  75. },
  76. })
  77. )
  78. }