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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import Command from './command'
  2. import history from '../history'
  3. import { Data, Shape } from 'types'
  4. import { getCommonBounds, setToArray } from 'utils'
  5. import tld from 'utils/tld'
  6. import { uniqueId } from 'utils/utils'
  7. import vec from 'utils/vec'
  8. import { getShapeUtils } from 'state/shape-utils'
  9. import state from 'state/state'
  10. export default function pasteCommand(data: Data, initialShapes: Shape[]): void {
  11. const center = tld.screenToWorld(
  12. [window.innerWidth / 2, window.innerHeight / 2],
  13. data
  14. )
  15. const bounds = getCommonBounds(
  16. ...initialShapes.map((shape) =>
  17. getShapeUtils(shape).getRotatedBounds(shape)
  18. )
  19. )
  20. const topLeft = vec.sub(center, [bounds.width / 2, bounds.height / 2])
  21. const newIdMap = Object.fromEntries(
  22. initialShapes.map((shape) => [shape.id, uniqueId()])
  23. )
  24. const oldSelectedIds = setToArray(tld.getSelectedIds(data))
  25. history.execute(
  26. data,
  27. new Command({
  28. name: 'paste_new_shapes',
  29. category: 'canvas',
  30. manualSelection: true,
  31. do(data) {
  32. const { shapes } = tld.getPage(data)
  33. let childIndex =
  34. (state.values.currentShapes[state.values.currentShapes.length - 1]
  35. ?.childIndex || 0) + 1
  36. for (const shape of initialShapes) {
  37. const topLeftOffset = vec.sub(shape.point, [bounds.minX, bounds.minY])
  38. const newId = newIdMap[shape.id]
  39. shapes[newId] = {
  40. ...shape,
  41. id: newId,
  42. parentId: oldSelectedIds[shape.parentId] || data.currentPageId,
  43. childIndex: childIndex++,
  44. point: vec.add(topLeft, topLeftOffset),
  45. isGenerated: false,
  46. }
  47. }
  48. tld.setSelectedIds(data, Object.values(newIdMap))
  49. },
  50. undo(data) {
  51. const { shapes } = tld.getPage(data)
  52. Object.values(newIdMap).forEach((id) => delete shapes[id])
  53. tld.setSelectedIds(data, oldSelectedIds)
  54. },
  55. })
  56. )
  57. }