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.

translate.ts 2.6KB

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