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.

generate.ts 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import Command from './command'
  2. import history from '../history'
  3. import { Data, Shape } from 'types'
  4. import { current } from 'immer'
  5. import { getPage, setSelectedIds } from 'utils'
  6. export default function generateCommand(
  7. data: Data,
  8. currentPageId: string,
  9. generatedShapes: Shape[]
  10. ): void {
  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 (const id in currentShapes) {
  19. if (currentShapes[id].isGenerated) {
  20. delete currentShapes[id]
  21. }
  22. }
  23. // Add new ones
  24. for (const 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 (const id in shapes) {
  37. if (shapes[id].isGenerated) {
  38. delete shapes[id]
  39. }
  40. }
  41. // Add new generated shapes
  42. for (const shape of generatedShapes) {
  43. shapes[shape.id] = shape
  44. }
  45. },
  46. undo(data) {
  47. const { shapes } = getPage(data)
  48. // Remove generated shapes
  49. for (const id in shapes) {
  50. if (shapes[id].isGenerated) {
  51. delete shapes[id]
  52. }
  53. }
  54. // Restore previous generated shapes
  55. for (const shape of prevGeneratedShapes) {
  56. shapes[shape.id] = shape
  57. }
  58. },
  59. })
  60. )
  61. }