Adds code editing and shape generation
This commit is contained in:
parent
afa8f53dff
commit
1a01c47835
34 changed files with 1298 additions and 237 deletions
29
lib/code/generate.ts
Normal file
29
lib/code/generate.ts
Normal file
|
@ -0,0 +1,29 @@
|
|||
import Rectangle from "./rectangle"
|
||||
import Circle from "./circle"
|
||||
import Ellipse from "./ellipse"
|
||||
import Polyline from "./polyline"
|
||||
import Dot from "./dot"
|
||||
import Line from "./line"
|
||||
import Vector from "./vector"
|
||||
import Utils from "./utils"
|
||||
import { codeShapes } from "./index"
|
||||
|
||||
const scope = { Dot, Circle, Ellipse, Line, Polyline, Rectangle, Vector, Utils }
|
||||
|
||||
/**
|
||||
* Evaluate code, collecting generated shapes in the shape set. Return the
|
||||
* collected shapes as an array.
|
||||
* @param code
|
||||
*/
|
||||
export function getShapesFromCode(code: string) {
|
||||
codeShapes.clear()
|
||||
|
||||
new Function(...Object.keys(scope), `${code}`)(...Object.values(scope))
|
||||
|
||||
const generatedShapes = Array.from(codeShapes.values()).map((instance) => {
|
||||
instance.shape.isGenerated = true
|
||||
return instance.shape
|
||||
})
|
||||
|
||||
return generatedShapes
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue