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

generate.ts 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import Command from './command'
  2. import history from '../history'
  3. import { CodeControl, Data, Shape } from 'types'
  4. import { current } from 'immer'
  5. import { getPage, getSelectedIds, setSelectedIds } from 'utils/utils'
  6. export default function generateCommand(
  7. data: Data,
  8. currentPageId: string,
  9. generatedShapes: Shape[]
  10. ) {
  11. const cData = current(data)
  12. const page = getPage(cData)
  13. const currentShapes = page.shapes
  14. const prevGeneratedShapes = Object.values(currentShapes).filter(
  15. (shape) => shape.isGenerated
  16. )
  17. // Remove previous generated shapes
  18. for (let id in currentShapes) {
  19. if (currentShapes[id].isGenerated) {
  20. delete currentShapes[id]
  21. }
  22. }
  23. // Add new ones
  24. for (let shape of generatedShapes) {
  25. currentShapes[shape.id] = shape
  26. }
  27. history.execute(
  28. data,
  29. new Command({
  30. name: 'generate_shapes',
  31. category: 'canvas',
  32. do(data) {
  33. const { shapes } = getPage(data)
  34. setSelectedIds(data, [])
  35. // Remove previous generated shapes
  36. for (let id in shapes) {
  37. if (shapes[id].isGenerated) {
  38. delete shapes[id]
  39. }
  40. }
  41. // Add new generated shapes
  42. for (let shape of generatedShapes) {
  43. shapes[shape.id] = shape
  44. }
  45. },
  46. undo(data) {
  47. const { shapes } = getPage(data)
  48. // Remove generated shapes
  49. for (let id in shapes) {
  50. if (shapes[id].isGenerated) {
  51. delete shapes[id]
  52. }
  53. }
  54. // Restore previous generated shapes
  55. for (let shape of prevGeneratedShapes) {
  56. shapes[shape.id] = shape
  57. }
  58. },
  59. })
  60. )
  61. }