選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

duplicate.ts 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. isGenerated: false,
  22. }))
  23. history.execute(
  24. data,
  25. new Command({
  26. name: 'duplicate_shapes',
  27. category: 'canvas',
  28. manualSelection: true,
  29. do(data) {
  30. const { shapes } = getPage(data, currentPageId)
  31. for (const duplicate of duplicates) {
  32. shapes[duplicate.id] = duplicate
  33. }
  34. setSelectedIds(
  35. data,
  36. duplicates.map((d) => d.id)
  37. )
  38. },
  39. undo(data) {
  40. const { shapes } = getPage(data, currentPageId)
  41. for (const duplicate of duplicates) {
  42. delete shapes[duplicate.id]
  43. }
  44. setSelectedIds(
  45. data,
  46. selectedShapes.map((d) => d.id)
  47. )
  48. },
  49. })
  50. )
  51. }