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.

duplicate.ts 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import Command from './command'
  2. import history from '../history'
  3. import { Data } from 'types'
  4. import {
  5. deepClone,
  6. getCurrentCamera,
  7. getPage,
  8. getSelectedShapes,
  9. setSelectedIds,
  10. } from 'utils'
  11. import { uniqueId } from 'utils'
  12. import vec from 'utils/vec'
  13. export default function duplicateCommand(data: Data): void {
  14. const selectedShapes = getSelectedShapes(data).map(deepClone)
  15. const duplicates = selectedShapes.map((shape) => ({
  16. ...shape,
  17. id: uniqueId(),
  18. point: vec.add(shape.point, vec.div([16, 16], getCurrentCamera(data).zoom)),
  19. isGenerated: false,
  20. }))
  21. history.execute(
  22. data,
  23. new Command({
  24. name: 'duplicate_shapes',
  25. category: 'canvas',
  26. manualSelection: true,
  27. do(data) {
  28. const { shapes } = getPage(data)
  29. for (const duplicate of duplicates) {
  30. shapes[duplicate.id] = duplicate
  31. }
  32. setSelectedIds(
  33. data,
  34. duplicates.map((d) => d.id)
  35. )
  36. },
  37. undo(data) {
  38. const { shapes } = getPage(data)
  39. for (const duplicate of duplicates) {
  40. delete shapes[duplicate.id]
  41. }
  42. setSelectedIds(
  43. data,
  44. selectedShapes.map((d) => d.id)
  45. )
  46. },
  47. })
  48. )
  49. }