import { useRef, useState, useEffect } from 'react' import { Canvas } from '@react-three/fiber' import { Allotment } from 'allotment' import { OrbitControls, OrthographicCamera } from '@react-three/drei' import { asyncLexer } from './lang/tokeniser' import { abstractSyntaxTree } from './lang/abstractSyntaxTree' import { executor, ExtrudeGroup, SketchGroup } from './lang/executor' import CodeMirror from '@uiw/react-codemirror' import { javascript } from '@codemirror/lang-javascript' import { ViewUpdate } from '@codemirror/view' import { lineHighlightField, addLineHighlight, } from './editor/highlightextension' import { useStore } from './useStore' import { Toolbar } from './Toolbar' import { BasePlanes } from './components/BasePlanes' import { SketchPlane } from './components/SketchPlane' import { Logs } from './components/Logs' import { AxisIndicator } from './components/AxisIndicator' import { RenderViewerArtifacts } from './components/RenderViewerArtifacts' import { PanelHeader } from './components/PanelHeader' import { MemoryPanel } from './components/MemoryPanel' import { useHotKeyListener } from './hooks/useHotKeyListener' import { Stream } from './components/Stream' const OrrthographicCamera = OrthographicCamera as any function App() { const cam = useRef() useHotKeyListener() const { editorView, setEditorView, setSelectionRanges, selectionRanges: selectionRange, guiMode, lastGuiMode, addLog, code, setCode, setAst, setError, errorState, setProgramMemory, resetLogs, } = useStore((s) => ({ editorView: s.editorView, setEditorView: s.setEditorView, setSelectionRanges: s.setSelectionRanges, selectionRanges: s.selectionRanges, guiMode: s.guiMode, setGuiMode: s.setGuiMode, addLog: s.addLog, code: s.code, setCode: s.setCode, setAst: s.setAst, lastGuiMode: s.lastGuiMode, setError: s.setError, errorState: s.errorState, setProgramMemory: s.setProgramMemory, resetLogs: s.resetLogs, })) // const onChange = React.useCallback((value: string, viewUpdate: ViewUpdate) => { const onChange = (value: string, viewUpdate: ViewUpdate) => { setCode(value) if (editorView) { editorView?.dispatch({ effects: addLineHighlight.of([0, 0]) }) } } //, []); const onUpdate = (viewUpdate: ViewUpdate) => { if (!editorView) { setEditorView(viewUpdate.view) } const ranges = viewUpdate.state.selection.ranges const isChange = ranges.length !== selectionRange.length || ranges.some(({ from, to }, i) => { return from !== selectionRange[i][0] || to !== selectionRange[i][1] }) if (!isChange) return setSelectionRanges(ranges.map(({ from, to }) => [from, to])) } const [geoArray, setGeoArray] = useState<(ExtrudeGroup | SketchGroup)[]>([]) useEffect(() => { const asyncWrap = async () => { try { if (!code) { setGeoArray([]) setAst(null) return } const tokens = await asyncLexer(code) const _ast = abstractSyntaxTree(tokens) setAst(_ast) resetLogs() const programMemory = executor(_ast, { root: { log: { type: 'userVal', value: (a: any) => { addLog(a) }, __meta: [ { pathToNode: [], sourceRange: [0, 0], }, ], }, }, _sketch: [], }) setProgramMemory(programMemory) const geos = programMemory?.return ?.map(({ name }: { name: string }) => { const artifact = programMemory?.root?.[name] if ( artifact.type === 'extrudeGroup' || artifact.type === 'sketchGroup' ) { return artifact } return null }) .filter((a) => a) as (ExtrudeGroup | SketchGroup)[] setGeoArray(geos) console.log(programMemory) setError() } catch (e: any) { setError('problem') console.log(e) addLog(e) } } asyncWrap() }, [code]) return (
{'last first: \n\n' + JSON.stringify(lastGuiMode, null, 2) + '\n\n' + JSON.stringify(guiMode)}