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.

transform.ts 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import Command from './command'
  2. import history from '../history'
  3. import { Data } from 'types'
  4. import { TransformSnapshot } from 'state/sessions/transform-session'
  5. import { getShapeUtils } from 'lib/shape-utils'
  6. import { getPage, updateParents } from 'utils/utils'
  7. export default function transformCommand(
  8. data: Data,
  9. before: TransformSnapshot,
  10. after: TransformSnapshot,
  11. scaleX: number,
  12. scaleY: number
  13. ) {
  14. history.execute(
  15. data,
  16. new Command({
  17. name: 'translate_shapes',
  18. category: 'canvas',
  19. do(data, isInitial) {
  20. if (isInitial) return
  21. const { type, shapeBounds } = after
  22. const { shapes } = getPage(data)
  23. for (let id in shapeBounds) {
  24. const { initialShape, initialShapeBounds, transformOrigin } =
  25. shapeBounds[id]
  26. const shape = shapes[id]
  27. getShapeUtils(shape).transform(shape, initialShapeBounds, {
  28. type,
  29. initialShape,
  30. transformOrigin,
  31. scaleX,
  32. scaleY,
  33. })
  34. }
  35. updateParents(data, Object.keys(shapeBounds))
  36. },
  37. undo(data) {
  38. const { type, shapeBounds } = before
  39. const { shapes } = getPage(data)
  40. for (let id in shapeBounds) {
  41. const { initialShape, initialShapeBounds, transformOrigin } =
  42. shapeBounds[id]
  43. const shape = shapes[id]
  44. getShapeUtils(shape).transform(shape, initialShapeBounds, {
  45. type,
  46. initialShape,
  47. transformOrigin,
  48. scaleX: scaleX < 0 ? scaleX * -1 : scaleX,
  49. scaleY: scaleX < 0 ? scaleX * -1 : scaleX,
  50. })
  51. }
  52. updateParents(data, Object.keys(shapeBounds))
  53. },
  54. })
  55. )
  56. }