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-single.ts 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import Command from './command'
  2. import history from '../history'
  3. import { Data, Corner, Edge } from 'types'
  4. import { getShapeUtils } from 'lib/shape-utils'
  5. import { current } from 'immer'
  6. import { TransformSingleSnapshot } from 'state/sessions/transform-single-session'
  7. import { getPage } from 'utils/utils'
  8. export default function transformSingleCommand(
  9. data: Data,
  10. before: TransformSingleSnapshot,
  11. after: TransformSingleSnapshot,
  12. scaleX: number,
  13. scaleY: number,
  14. isCreating: boolean
  15. ) {
  16. const shape = current(getPage(data, after.currentPageId).shapes[after.id])
  17. history.execute(
  18. data,
  19. new Command({
  20. name: 'transform_single_shape',
  21. category: 'canvas',
  22. manualSelection: true,
  23. do(data) {
  24. const { id, type, initialShape, initialShapeBounds } = after
  25. const { shapes } = getPage(data, after.currentPageId)
  26. data.selectedIds.clear()
  27. data.selectedIds.add(id)
  28. if (isCreating) {
  29. shapes[id] = shape
  30. } else {
  31. getShapeUtils(shape).transformSingle(shape, initialShapeBounds, {
  32. type,
  33. initialShape,
  34. scaleX,
  35. scaleY,
  36. transformOrigin: [0.5, 0.5],
  37. })
  38. }
  39. },
  40. undo(data) {
  41. const { id, type, initialShapeBounds } = before
  42. const { shapes } = getPage(data, before.currentPageId)
  43. data.selectedIds.clear()
  44. if (isCreating) {
  45. delete shapes[id]
  46. } else {
  47. const shape = shapes[id]
  48. data.selectedIds.add(id)
  49. getShapeUtils(shape).transform(shape, initialShapeBounds, {
  50. type,
  51. initialShape: after.initialShape,
  52. scaleX: 1,
  53. scaleY: 1,
  54. transformOrigin: [0.5, 0.5],
  55. })
  56. }
  57. },
  58. })
  59. )
  60. }