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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import Command from './command'
  2. import history from '../history'
  3. import { TranslateSnapshot } from 'state/sessions/translate-session'
  4. import { Data } from 'types'
  5. import {
  6. getDocumentBranch,
  7. getPage,
  8. setSelectedIds,
  9. updateParents,
  10. } from 'utils/utils'
  11. import { getShapeUtils } from 'state/shape-utils'
  12. export default function translateCommand(
  13. data: Data,
  14. before: TranslateSnapshot,
  15. after: TranslateSnapshot,
  16. isCloning: boolean
  17. ): void {
  18. history.execute(
  19. data,
  20. new Command({
  21. name: isCloning ? 'clone_shapes' : 'translate_shapes',
  22. category: 'canvas',
  23. manualSelection: true,
  24. do(data, initial) {
  25. if (initial) return
  26. const { initialShapes, currentPageId } = after
  27. const { shapes } = getPage(data, currentPageId)
  28. // Restore clones to document
  29. if (isCloning) {
  30. for (const clone of before.clones) {
  31. shapes[clone.id] = clone
  32. if (clone.parentId !== data.currentPageId) {
  33. const parent = shapes[clone.parentId]
  34. getShapeUtils(parent).setProperty(parent, 'children', [
  35. ...parent.children,
  36. clone.id,
  37. ])
  38. // if (clone.type === ShapeType.Group) {
  39. // }
  40. }
  41. }
  42. }
  43. // Move shapes (these initialShapes will include clones if any)
  44. for (const { id, point } of initialShapes) {
  45. getDocumentBranch(data, id).forEach((id) => {
  46. const shape = shapes[id]
  47. getShapeUtils(shape).translateTo(shape, point)
  48. })
  49. }
  50. // Set selected shapes
  51. setSelectedIds(
  52. data,
  53. initialShapes.map((s) => s.id)
  54. )
  55. // Update parents
  56. updateParents(
  57. data,
  58. initialShapes.map((s) => s.id)
  59. )
  60. },
  61. undo(data) {
  62. const { initialShapes, clones, currentPageId, initialParents } = before
  63. const { shapes } = getPage(data, currentPageId)
  64. // Move shapes back to where they started
  65. for (const { id, point } of initialShapes) {
  66. getDocumentBranch(data, id).forEach((id) => {
  67. const shape = shapes[id]
  68. getShapeUtils(shape).translateTo(shape, point)
  69. })
  70. }
  71. // Delete clones
  72. if (isCloning) for (const { id } of clones) delete shapes[id]
  73. // Set selected shapes
  74. setSelectedIds(
  75. data,
  76. initialShapes.map((s) => s.id)
  77. )
  78. // Restore children on parents
  79. initialParents.forEach(({ id, children }) => {
  80. const parent = shapes[id]
  81. getShapeUtils(parent).setProperty(parent, 'children', children)
  82. })
  83. // Update parents
  84. updateParents(
  85. data,
  86. initialShapes.map((s) => s.id)
  87. )
  88. },
  89. })
  90. )
  91. }