Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import Command from './command'
  2. import history from '../history'
  3. import { Data } from 'types'
  4. import {
  5. getDocumentBranch,
  6. getPage,
  7. getPageState,
  8. getSelectedIds,
  9. getSelectedShapes,
  10. getTopParentId,
  11. setToArray,
  12. uniqueArray,
  13. } from 'utils/utils'
  14. import { getShapeUtils } from 'lib/shape-utils'
  15. import vec from 'utils/vec'
  16. import storage from 'state/storage'
  17. export default function nudgeCommand(data: Data, newPageId: string) {
  18. const { currentPageId: oldPageId } = data
  19. const oldPage = getPage(data)
  20. const selectedIds = setToArray(getSelectedIds(data))
  21. const idsToMove = uniqueArray(
  22. ...selectedIds.flatMap((id) => getDocumentBranch(data, id))
  23. )
  24. const oldParentIds = Object.fromEntries(
  25. idsToMove.map((id) => [id, oldPage.shapes[id].parentId])
  26. )
  27. // const selectedParents = uniqueArray(
  28. // ...selectedIds.map((id) => getTopParentId(data, id))
  29. // )
  30. history.execute(
  31. data,
  32. new Command({
  33. name: 'set_direction',
  34. category: 'canvas',
  35. manualSelection: true,
  36. do(data) {
  37. const fromPageId = oldPageId
  38. const toPageId = newPageId
  39. const fromPage = getPage(data, fromPageId)
  40. // Get all of the selected shapes and their descendents
  41. const shapesToMove = idsToMove.map((id) => fromPage.shapes[id])
  42. shapesToMove.forEach((shape) => {
  43. // If the shape is a parent of a group that isn't selected,
  44. // remove the shape's id from its parent's children.
  45. if (
  46. shape.parentId !== fromPageId &&
  47. !idsToMove.includes(shape.parentId)
  48. ) {
  49. const parent = fromPage.shapes[shape.parentId]
  50. getShapeUtils(parent).setProperty(
  51. parent,
  52. 'children',
  53. parent.children.filter((id) => id !== shape.id)
  54. )
  55. }
  56. // Delete the shapes from the "from" page
  57. delete fromPage.shapes[shape.id]
  58. })
  59. // Clear the current page state's selected ids
  60. getPageState(data, fromPageId).selectedIds.clear()
  61. // Save the "from" page
  62. storage.savePage(data, data.document.id, fromPageId)
  63. // Load the "to" page
  64. storage.loadPage(data, toPageId)
  65. // The page we're moving the shapes to
  66. const toPage = getPage(data, toPageId)
  67. // Add all of the selected shapes to the "from" page.
  68. shapesToMove.forEach((shape) => {
  69. toPage.shapes[shape.id] = shape
  70. })
  71. // If a shape's parent isn't in the document, re-parent to the page.
  72. shapesToMove.forEach((shape) => {
  73. if (!toPage.shapes[shape.parentId]) {
  74. getShapeUtils(shape).setProperty(shape, 'parentId', toPageId)
  75. }
  76. })
  77. // Select the selected ids on the new page
  78. getPageState(data, toPageId).selectedIds = new Set(selectedIds)
  79. // Move to the new page
  80. data.currentPageId = toPageId
  81. },
  82. undo(data) {
  83. const fromPageId = newPageId
  84. const toPageId = oldPageId
  85. const fromPage = getPage(data, fromPageId)
  86. const shapesToMove = idsToMove.map((id) => fromPage.shapes[id])
  87. shapesToMove.forEach((shape) => {
  88. if (
  89. shape.parentId !== fromPageId &&
  90. !idsToMove.includes(shape.parentId)
  91. ) {
  92. const parent = fromPage.shapes[shape.parentId]
  93. getShapeUtils(parent).setProperty(
  94. parent,
  95. 'children',
  96. parent.children.filter((id) => id !== shape.id)
  97. )
  98. }
  99. delete fromPage.shapes[shape.id]
  100. })
  101. getPageState(data, fromPageId).selectedIds.clear()
  102. storage.savePage(data, data.document.id, fromPageId)
  103. storage.loadPage(data, toPageId)
  104. const toPage = getPage(data, toPageId)
  105. shapesToMove.forEach((shape) => {
  106. toPage.shapes[shape.id] = shape
  107. // Move shapes back to their old parent
  108. const parentId = oldParentIds[shape.id]
  109. getShapeUtils(shape).setProperty(shape, 'parentId', parentId)
  110. // And add the shape back to the parent's children
  111. if (parentId !== toPageId) {
  112. const parent = toPage.shapes[parentId]
  113. getShapeUtils(parent).setProperty(parent, 'children', [
  114. ...parent.children,
  115. shape.id,
  116. ])
  117. }
  118. })
  119. getPageState(data, toPageId).selectedIds = new Set(selectedIds)
  120. data.currentPageId = toPageId
  121. },
  122. })
  123. )
  124. }