Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

duplicate.ts 1.3KB

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