Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

generate.ts 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import Rectangle from "./rectangle"
  2. import Circle from "./circle"
  3. import Ellipse from "./ellipse"
  4. import Polyline from "./polyline"
  5. import Dot from "./dot"
  6. import Ray from "./ray"
  7. import Line from "./line"
  8. import Vector from "./vector"
  9. import Utils from "./utils"
  10. import { NumberControl, VectorControl, codeControls, controls } from "./control"
  11. import { codeShapes } from "./index"
  12. import { CodeControl } from "types"
  13. const baseScope = {
  14. Dot,
  15. Circle,
  16. Ellipse,
  17. Ray,
  18. Line,
  19. Polyline,
  20. Rectangle,
  21. Vector,
  22. Utils,
  23. VectorControl,
  24. NumberControl,
  25. }
  26. /**
  27. * Evaluate code, collecting generated shapes in the shape set. Return the
  28. * collected shapes as an array.
  29. * @param code
  30. */
  31. export function generateFromCode(code: string) {
  32. codeControls.clear()
  33. codeShapes.clear()
  34. ;(window as any).isUpdatingCode = false
  35. const scope = { ...baseScope, controls }
  36. new Function(...Object.keys(scope), `${code}`)(...Object.values(scope))
  37. const generatedShapes = Array.from(codeShapes.values()).map((instance) => {
  38. instance.shape.isGenerated = true
  39. return instance.shape
  40. })
  41. const generatedControls = Array.from(codeControls.values())
  42. return { shapes: generatedShapes, controls: generatedControls }
  43. }
  44. /**
  45. * Evaluate code, collecting generated shapes in the shape set. Return the
  46. * collected shapes as an array.
  47. * @param code
  48. */
  49. export function updateFromCode(
  50. code: string,
  51. controls: Record<string, CodeControl>
  52. ) {
  53. codeShapes.clear()
  54. ;(window as any).isUpdatingCode = true
  55. const scope = {
  56. ...baseScope,
  57. controls: Object.fromEntries(
  58. Object.entries(controls).map(([id, control]) => [
  59. control.label,
  60. control.value,
  61. ])
  62. ),
  63. }
  64. new Function(...Object.keys(scope), `${code}`)(...Object.values(scope))
  65. const generatedShapes = Array.from(codeShapes.values()).map((instance) => {
  66. instance.shape.isGenerated = true
  67. return instance.shape
  68. })
  69. return { shapes: generatedShapes }
  70. }