2021-05-15 13:02:13 +00:00
|
|
|
import Rectangle from "./rectangle"
|
|
|
|
import Circle from "./circle"
|
|
|
|
import Ellipse from "./ellipse"
|
|
|
|
import Polyline from "./polyline"
|
|
|
|
import Dot from "./dot"
|
2021-05-16 08:33:08 +00:00
|
|
|
import Ray from "./ray"
|
2021-05-15 13:02:13 +00:00
|
|
|
import Line from "./line"
|
|
|
|
import Vector from "./vector"
|
|
|
|
import Utils from "./utils"
|
2021-05-17 10:01:11 +00:00
|
|
|
import { NumberControl, VectorControl, codeControls, controls } from "./control"
|
2021-05-15 13:02:13 +00:00
|
|
|
import { codeShapes } from "./index"
|
2021-05-17 10:01:11 +00:00
|
|
|
import { CodeControl } 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-05-17 10:01:11 +00:00
|
|
|
export function generateFromCode(code: string) {
|
|
|
|
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
|
|
|
|
|
|
|
|
const scope = { ...baseScope, controls }
|
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
export function updateFromCode(
|
|
|
|
code: string,
|
|
|
|
controls: Record<string, CodeControl>
|
|
|
|
) {
|
|
|
|
codeShapes.clear()
|
|
|
|
;(window as any).isUpdatingCode = true
|
|
|
|
|
|
|
|
const scope = {
|
|
|
|
...baseScope,
|
|
|
|
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
|
|
|
}
|