您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

generate.ts 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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, Data } 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(data: Data, code: string) {
  32. codeControls.clear()
  33. codeShapes.clear()
  34. ;(window as any).isUpdatingCode = false
  35. ;(window as any).currentPageId = data.currentPageId
  36. const { currentPageId } = data
  37. const scope = { ...baseScope, controls, currentPageId }
  38. new Function(...Object.keys(scope), `${code}`)(...Object.values(scope))
  39. const generatedShapes = Array.from(codeShapes.values()).map(
  40. (instance) => instance.shape
  41. )
  42. const generatedControls = Array.from(codeControls.values())
  43. return { shapes: generatedShapes, controls: generatedControls }
  44. }
  45. /**
  46. * Evaluate code, collecting generated shapes in the shape set. Return the
  47. * collected shapes as an array.
  48. * @param code
  49. */
  50. export function updateFromCode(data: Data, code: string) {
  51. codeShapes.clear()
  52. ;(window as any).isUpdatingCode = true
  53. ;(window as any).currentPageId = data.currentPageId
  54. const { currentPageId } = data
  55. const scope = {
  56. ...baseScope,
  57. currentPageId,
  58. controls: Object.fromEntries(
  59. Object.entries(controls).map(([id, control]) => [
  60. control.label,
  61. control.value,
  62. ])
  63. ),
  64. }
  65. new Function(...Object.keys(scope), `${code}`)(...Object.values(scope))
  66. const generatedShapes = Array.from(codeShapes.values()).map(
  67. (instance) => instance.shape
  68. )
  69. return { shapes: generatedShapes }
  70. }