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.

move-to-page.ts 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 * as vec from 'utils/vec'
  16. import storage from 'state/storage'
  17. export default function nudgeCommand(data: Data, toPageId: string) {
  18. const { currentPageId: fromPageId } = data
  19. const selectedIds = setToArray(getSelectedIds(data))
  20. const selectedParents = uniqueArray(
  21. ...selectedIds.map((id) => getTopParentId(data, id))
  22. )
  23. history.execute(
  24. data,
  25. new Command({
  26. name: 'set_direction',
  27. category: 'canvas',
  28. manualSelection: true,
  29. do(data) {
  30. // The page we're moving the shapes from
  31. const fromPage = getPage(data, fromPageId)
  32. // Get all of the selected shapes and their descendents
  33. const shapesToMove = selectedParents.flatMap((id) =>
  34. getDocumentBranch(data, id).map((id) => fromPage.shapes[id])
  35. )
  36. // Delete the shapes from the "from" page
  37. shapesToMove.forEach((shape) => delete fromPage.shapes[shape.id])
  38. // Clear the current page state's selected ids
  39. getPageState(data, fromPageId).selectedIds.clear()
  40. // Save the "from" page
  41. storage.savePage(data, fromPageId)
  42. // Load the "to" page
  43. storage.loadPage(data, toPageId)
  44. // The page we're moving the shapes to
  45. const toPage = getPage(data, toPageId)
  46. // Add all of the selected shapes to the "from" page. Any shapes that
  47. // were children of the "from" page should become children of the "to"
  48. // page. Grouped shapes should keep their same parent.
  49. // What about shapes that were children of a group that we haven't moved?
  50. shapesToMove.forEach((shape) => {
  51. toPage.shapes[shape.id] = shape
  52. if (shape.parentId === fromPageId) {
  53. getShapeUtils(shape).setProperty(shape, 'parentId', toPageId)
  54. }
  55. })
  56. console.log('from', getPage(data, fromPageId))
  57. console.log('to', getPage(data, toPageId))
  58. // Select the selected ids on the new page
  59. getPageState(data, toPageId).selectedIds = new Set(selectedIds)
  60. // Move to the new page
  61. data.currentPageId = toPageId
  62. },
  63. undo(data) {
  64. const toPage = getPage(data, fromPageId)
  65. const shapesToMove = selectedParents.flatMap((id) =>
  66. getDocumentBranch(data, id).map((id) => toPage.shapes[id])
  67. )
  68. shapesToMove.forEach((shape) => delete toPage.shapes[shape.id])
  69. getPageState(data, toPageId).selectedIds.clear()
  70. storage.savePage(data, toPageId)
  71. storage.loadPage(data, fromPageId)
  72. const fromPage = getPage(data, toPageId)
  73. shapesToMove.forEach((shape) => {
  74. fromPage.shapes[shape.id] = shape
  75. if (shape.parentId === toPageId) {
  76. getShapeUtils(shape).setProperty(shape, 'parentId', fromPageId)
  77. }
  78. })
  79. getPageState(data, fromPageId).selectedIds = new Set(selectedIds)
  80. data.currentPageId = fromPageId
  81. },
  82. })
  83. )
  84. }