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

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