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.

nudge.ts 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import Command from './command'
  2. import history from '../history'
  3. import { Data } from 'types'
  4. import { getPage, getSelectedShapes } from 'utils/utils'
  5. import { getShapeUtils } from 'lib/shape-utils'
  6. import vec from 'utils/vec'
  7. export default function nudgeCommand(data: Data, delta: number[]) {
  8. const { currentPageId } = data
  9. const selectedShapes = getSelectedShapes(data)
  10. const shapeBounds = Object.fromEntries(
  11. selectedShapes.map(
  12. (shape) => [shape.id, getShapeUtils(shape).getBounds(shape)] as const
  13. )
  14. )
  15. history.execute(
  16. data,
  17. new Command({
  18. name: 'nudge_shapes',
  19. category: 'canvas',
  20. do(data) {
  21. const { shapes } = getPage(data, currentPageId)
  22. for (let id in shapeBounds) {
  23. const shape = shapes[id]
  24. getShapeUtils(shape).setProperty(
  25. shape,
  26. 'point',
  27. vec.add(shape.point, delta)
  28. )
  29. }
  30. },
  31. undo(data) {
  32. const { shapes } = getPage(data, currentPageId)
  33. for (let id in shapeBounds) {
  34. const shape = shapes[id]
  35. getShapeUtils(shape).setProperty(
  36. shape,
  37. 'point',
  38. vec.sub(shape.point, delta)
  39. )
  40. }
  41. },
  42. })
  43. )
  44. }