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 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 async function generateFromCode(
  46. data: Data,
  47. code: string
  48. ): Promise<{
  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, {
  59. transforms: ['typescript'],
  60. }).code
  61. new Function(...Object.keys(scope), `${transformed}`)(...Object.values(scope))
  62. const startingChildIndex =
  63. getShapes(data)
  64. .filter((shape) => shape.parentId === data.currentPageId)
  65. .sort((a, b) => a.childIndex - b.childIndex)[0]?.childIndex || 1
  66. const generatedShapes = Array.from(codeShapes.values())
  67. .sort((a, b) => a.shape.childIndex - b.shape.childIndex)
  68. .map((instance, i) => ({
  69. ...instance.shape,
  70. isGenerated: true,
  71. parentId: getPage(data).id,
  72. childIndex: startingChildIndex + i,
  73. }))
  74. const generatedControls = Array.from(codeControls.values())
  75. return { shapes: generatedShapes, controls: generatedControls }
  76. }
  77. /**
  78. * Evaluate code, collecting generated shapes in the shape set. Return the
  79. * collected shapes as an array.
  80. * @param code
  81. */
  82. export async function updateFromCode(
  83. data: Data,
  84. code: string
  85. ): Promise<{
  86. shapes: Shape[]
  87. }> {
  88. codeShapes.clear()
  89. ;(window as any).isUpdatingCode = true
  90. ;(window as any).currentPageId = data.currentPageId
  91. const { currentPageId } = data
  92. const newControls = Object.fromEntries(
  93. Object.entries(data.codeControls).map(([_, control]) => [
  94. control.label,
  95. control.value,
  96. ])
  97. )
  98. const scope = {
  99. ...baseScope,
  100. currentPageId,
  101. controls: newControls,
  102. }
  103. const startingChildIndex =
  104. getShapes(data)
  105. .filter((shape) => shape.parentId === data.currentPageId)
  106. .sort((a, b) => a.childIndex - b.childIndex)[0]?.childIndex || 1
  107. const transformed = transform(code, {
  108. transforms: ['typescript'],
  109. }).code
  110. new Function(...Object.keys(scope), `${transformed}`)(...Object.values(scope))
  111. const generatedShapes = Array.from(codeShapes.values())
  112. .sort((a, b) => a.shape.childIndex - b.shape.childIndex)
  113. .map((instance, i) => ({
  114. ...instance.shape,
  115. isGenerated: true,
  116. parentId: getPage(data).id,
  117. childIndex: startingChildIndex + i,
  118. }))
  119. return { shapes: generatedShapes }
  120. }