2021-06-08 10:32:20 +00:00
|
|
|
import Rectangle from './rectangle'
|
|
|
|
import Circle from './circle'
|
|
|
|
import Ellipse from './ellipse'
|
|
|
|
import Polyline from './polyline'
|
|
|
|
import Dot from './dot'
|
|
|
|
import Ray from './ray'
|
|
|
|
import Line from './line'
|
|
|
|
import Vector from './vector'
|
|
|
|
import Utils from './utils'
|
|
|
|
import { NumberControl, VectorControl, codeControls, controls } from './control'
|
|
|
|
import { codeShapes } from './index'
|
|
|
|
import { CodeControl, Data } from 'types'
|
2021-05-15 13:02:13 +00:00
|
|
|
|
2021-05-17 10:01:11 +00:00
|
|
|
const baseScope = {
|
2021-05-16 08:33:08 +00:00
|
|
|
Dot,
|
|
|
|
Circle,
|
|
|
|
Ellipse,
|
|
|
|
Ray,
|
|
|
|
Line,
|
|
|
|
Polyline,
|
|
|
|
Rectangle,
|
|
|
|
Vector,
|
|
|
|
Utils,
|
2021-05-17 10:01:11 +00:00
|
|
|
VectorControl,
|
|
|
|
NumberControl,
|
2021-05-16 08:33:08 +00:00
|
|
|
}
|
2021-05-15 13:02:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Evaluate code, collecting generated shapes in the shape set. Return the
|
|
|
|
* collected shapes as an array.
|
|
|
|
* @param code
|
|
|
|
*/
|
2021-06-08 10:32:20 +00:00
|
|
|
export function generateFromCode(data: Data, code: string) {
|
2021-05-17 10:01:11 +00:00
|
|
|
codeControls.clear()
|
2021-05-15 13:02:13 +00:00
|
|
|
codeShapes.clear()
|
2021-05-17 10:01:11 +00:00
|
|
|
;(window as any).isUpdatingCode = false
|
2021-06-08 10:32:20 +00:00
|
|
|
;(window as any).currentPageId = data.currentPageId
|
2021-05-17 10:01:11 +00:00
|
|
|
|
2021-06-08 10:32:20 +00:00
|
|
|
const { currentPageId } = data
|
|
|
|
const scope = { ...baseScope, controls, currentPageId }
|
2021-05-17 10:01:11 +00:00
|
|
|
|
|
|
|
new Function(...Object.keys(scope), `${code}`)(...Object.values(scope))
|
|
|
|
|
2021-05-25 09:00:59 +00:00
|
|
|
const generatedShapes = Array.from(codeShapes.values()).map(
|
|
|
|
(instance) => instance.shape
|
|
|
|
)
|
2021-05-17 10:01:11 +00:00
|
|
|
|
|
|
|
const generatedControls = Array.from(codeControls.values())
|
|
|
|
|
|
|
|
return { shapes: generatedShapes, controls: generatedControls }
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Evaluate code, collecting generated shapes in the shape set. Return the
|
|
|
|
* collected shapes as an array.
|
|
|
|
* @param code
|
|
|
|
*/
|
2021-06-08 10:32:20 +00:00
|
|
|
export function updateFromCode(data: Data, code: string) {
|
2021-05-17 10:01:11 +00:00
|
|
|
codeShapes.clear()
|
|
|
|
;(window as any).isUpdatingCode = true
|
2021-06-08 10:32:20 +00:00
|
|
|
;(window as any).currentPageId = data.currentPageId
|
|
|
|
|
|
|
|
const { currentPageId } = data
|
2021-05-17 10:01:11 +00:00
|
|
|
|
|
|
|
const scope = {
|
|
|
|
...baseScope,
|
2021-06-08 10:32:20 +00:00
|
|
|
currentPageId,
|
2021-05-17 10:01:11 +00:00
|
|
|
controls: Object.fromEntries(
|
|
|
|
Object.entries(controls).map(([id, control]) => [
|
|
|
|
control.label,
|
|
|
|
control.value,
|
|
|
|
])
|
|
|
|
),
|
|
|
|
}
|
2021-05-15 13:02:13 +00:00
|
|
|
|
|
|
|
new Function(...Object.keys(scope), `${code}`)(...Object.values(scope))
|
|
|
|
|
2021-05-25 09:00:59 +00:00
|
|
|
const generatedShapes = Array.from(codeShapes.values()).map(
|
|
|
|
(instance) => instance.shape
|
|
|
|
)
|
2021-05-15 13:02:13 +00:00
|
|
|
|
2021-05-17 10:01:11 +00:00
|
|
|
return { shapes: generatedShapes }
|
2021-05-15 13:02:13 +00:00
|
|
|
}
|