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.5KB

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