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.1KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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 * as 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: 'set_direction',
  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).translateTo(shape, vec.add(shape.point, delta))
  25. }
  26. },
  27. undo(data) {
  28. const { shapes } = getPage(data, currentPageId)
  29. for (let id in shapeBounds) {
  30. const shape = shapes[id]
  31. getShapeUtils(shape).translateTo(shape, vec.sub(shape.point, delta))
  32. }
  33. },
  34. })
  35. )
  36. }