You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

generate.ts 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import Rectangle from './rectangle'
  2. import Ellipse from './ellipse'
  3. import Polyline from './polyline'
  4. import Dot from './dot'
  5. import Ray from './ray'
  6. import Line from './line'
  7. import Arrow from './arrow'
  8. import Draw from './draw'
  9. import Utils from './utils'
  10. import Vec from 'utils/vec'
  11. import { NumberControl, VectorControl, codeControls, controls } from './control'
  12. import { codeShapes } from './index'
  13. import {
  14. CodeControl,
  15. Data,
  16. Shape,
  17. DashStyle,
  18. ColorStyle,
  19. SizeStyle,
  20. } from 'types'
  21. import { getPage, getShapes } from 'utils'
  22. import { transform } from 'sucrase'
  23. const baseScope = {
  24. Dot,
  25. Ellipse,
  26. Ray,
  27. Line,
  28. Polyline,
  29. Rectangle,
  30. Vec,
  31. Utils,
  32. Arrow,
  33. Draw,
  34. VectorControl,
  35. NumberControl,
  36. DashStyle,
  37. ColorStyle,
  38. SizeStyle,
  39. }
  40. /**
  41. * Evaluate code, collecting generated shapes in the shape set. Return the
  42. * collected shapes as an array.
  43. * @param code
  44. */
  45. export function generateFromCode(
  46. data: Data,
  47. code: string
  48. ): {
  49. shapes: Shape[]
  50. controls: CodeControl[]
  51. } {
  52. codeControls.clear()
  53. codeShapes.clear()
  54. ;(window as any).isUpdatingCode = false
  55. ;(window as any).currentPageId = data.currentPageId
  56. const { currentPageId } = data
  57. const scope = { ...baseScope, controls, currentPageId }
  58. const transformed = transform(code, { transforms: ['typescript'] }).code
  59. new Function(...Object.keys(scope), `${transformed}`)(...Object.values(scope))
  60. const startingChildIndex =
  61. getShapes(data)
  62. .filter((shape) => shape.parentId === data.currentPageId)
  63. .sort((a, b) => a.childIndex - b.childIndex)[0]?.childIndex || 1
  64. const generatedShapes = Array.from(codeShapes.values())
  65. .sort((a, b) => a.shape.childIndex - b.shape.childIndex)
  66. .map((instance, i) => ({
  67. ...instance.shape,
  68. isGenerated: true,
  69. parentId: getPage(data).id,
  70. childIndex: startingChildIndex + i,
  71. }))
  72. const generatedControls = Array.from(codeControls.values())
  73. return { shapes: generatedShapes, controls: generatedControls }
  74. }
  75. /**
  76. * Evaluate code, collecting generated shapes in the shape set. Return the
  77. * collected shapes as an array.
  78. * @param code
  79. */
  80. export function updateFromCode(
  81. data: Data,
  82. code: string
  83. ): {
  84. shapes: Shape[]
  85. } {
  86. codeShapes.clear()
  87. ;(window as any).isUpdatingCode = true
  88. ;(window as any).currentPageId = data.currentPageId
  89. const { currentPageId } = data
  90. const scope = {
  91. ...baseScope,
  92. currentPageId,
  93. controls: Object.fromEntries(
  94. Object.entries(controls).map(([_, control]) => [
  95. control.label,
  96. control.value,
  97. ])
  98. ),
  99. }
  100. new Function(...Object.keys(scope), `${code}`)(...Object.values(scope))
  101. const generatedShapes = Array.from(codeShapes.values()).map(
  102. (instance) => instance.shape
  103. )
  104. return { shapes: generatedShapes }
  105. }