Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

paste.ts 1.9KB

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