tldraw/state/code/generate.ts

103 lines
2.3 KiB
TypeScript
Raw Normal View History

2021-06-08 10:32:20 +00:00
import Rectangle from './rectangle'
import Ellipse from './ellipse'
import Polyline from './polyline'
import Dot from './dot'
import Ray from './ray'
import Line from './line'
2021-06-23 22:32:21 +00:00
import Arrow from './arrow'
import Draw from './draw'
2021-06-08 10:32:20 +00:00
import Utils from './utils'
2021-06-23 22:32:21 +00:00
import Vec from 'utils/vec'
2021-06-08 10:32:20 +00:00
import { NumberControl, VectorControl, codeControls, controls } from './control'
import { codeShapes } from './index'
2021-06-21 21:35:28 +00:00
import { CodeControl, Data, Shape } from 'types'
2021-06-24 08:18:14 +00:00
import { getPage } from 'utils'
2021-06-24 14:59:56 +00:00
import { transform } from 'sucrase'
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,
Ellipse,
Ray,
Line,
Polyline,
Rectangle,
2021-06-23 22:32:21 +00:00
Vec,
2021-05-16 08:33:08 +00:00
Utils,
2021-06-23 22:32:21 +00:00
Arrow,
Draw,
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-21 21:35:28 +00:00
export function generateFromCode(
data: Data,
code: string
): {
shapes: Shape[]
controls: CodeControl[]
} {
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
2021-06-24 14:59:56 +00:00
const transformed = transform(code, { transforms: ['typescript'] }).code
new Function(...Object.keys(scope), `${transformed}`)(...Object.values(scope))
2021-05-17 10:01:11 +00:00
2021-06-23 22:32:21 +00:00
const generatedShapes = Array.from(codeShapes.values()).map((instance) => ({
...instance.shape,
isGenerated: true,
parentId: getPage(data).id,
}))
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-21 21:35:28 +00:00
export function updateFromCode(
data: Data,
code: string
): {
shapes: Shape[]
} {
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(
2021-06-21 21:35:28 +00:00
Object.entries(controls).map(([_, control]) => [
2021-05-17 10:01:11 +00:00
control.label,
control.value,
])
),
}
2021-05-15 13:02:13 +00:00
new Function(...Object.keys(scope), `${code}`)(...Object.values(scope))
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
}