2023-08-01 13:23:17 -04:00
|
|
|
import { create } from 'zustand'
|
2023-02-21 14:50:22 +11:00
|
|
|
import { persist } from 'zustand/middleware'
|
2022-11-26 08:34:23 +11:00
|
|
|
import { addLineHighlight, EditorView } from './editor/highlightextension'
|
2024-02-11 18:26:09 -08:00
|
|
|
import {
|
|
|
|
Program,
|
|
|
|
_executor,
|
|
|
|
ProgramMemory,
|
|
|
|
programMemoryInit,
|
|
|
|
} from './lang/wasm'
|
2023-10-17 16:34:35 +11:00
|
|
|
import { Selection } from 'lib/selections'
|
2023-09-29 11:11:01 -07:00
|
|
|
import { enginelessExecutor } from './lib/testHelpers'
|
2023-09-15 04:35:48 -07:00
|
|
|
import { EngineCommandManager } from './lang/std/engineConnection'
|
2023-08-18 17:14:35 -05:00
|
|
|
import { KCLError } from './lang/errors'
|
2023-10-11 13:36:54 +11:00
|
|
|
|
2023-09-15 11:48:23 -04:00
|
|
|
export type ToolTip =
|
2023-02-12 10:56:45 +11:00
|
|
|
| 'lineTo'
|
|
|
|
| 'line'
|
|
|
|
| 'angledLine'
|
|
|
|
| 'angledLineOfXLength'
|
|
|
|
| 'angledLineOfYLength'
|
|
|
|
| 'angledLineToX'
|
|
|
|
| 'angledLineToY'
|
|
|
|
| 'xLine'
|
|
|
|
| 'yLine'
|
|
|
|
| 'xLineTo'
|
|
|
|
| 'yLineTo'
|
2023-03-19 18:46:39 +11:00
|
|
|
| 'angledLineThatIntersects'
|
2024-02-11 12:59:00 +11:00
|
|
|
| 'tangentialArcTo'
|
2022-11-26 05:13:07 +11:00
|
|
|
|
2023-09-13 08:36:47 +10:00
|
|
|
export const toolTips = [
|
|
|
|
'sketch_line',
|
|
|
|
'move',
|
|
|
|
// original tooltips
|
2023-02-12 10:56:45 +11:00
|
|
|
'line',
|
2023-09-13 08:36:47 +10:00
|
|
|
'lineTo',
|
2023-02-12 10:56:45 +11:00
|
|
|
'angledLine',
|
|
|
|
'angledLineOfXLength',
|
|
|
|
'angledLineOfYLength',
|
|
|
|
'angledLineToX',
|
|
|
|
'angledLineToY',
|
|
|
|
'xLine',
|
|
|
|
'yLine',
|
|
|
|
'xLineTo',
|
|
|
|
'yLineTo',
|
2023-03-19 18:46:39 +11:00
|
|
|
'angledLineThatIntersects',
|
2024-02-11 12:59:00 +11:00
|
|
|
'tangentialArcTo',
|
2023-09-15 11:48:23 -04:00
|
|
|
] as any as ToolTip[]
|
2023-02-12 10:56:45 +11:00
|
|
|
|
2023-09-05 16:02:27 -07:00
|
|
|
export type PaneType =
|
|
|
|
| 'code'
|
|
|
|
| 'variables'
|
|
|
|
| 'debug'
|
|
|
|
| 'kclErrors'
|
|
|
|
| 'logs'
|
|
|
|
| 'lspMessages'
|
2023-08-06 21:29:26 -04:00
|
|
|
|
2023-04-14 07:49:36 +10:00
|
|
|
export interface StoreState {
|
2022-11-26 08:34:23 +11:00
|
|
|
editorView: EditorView | null
|
|
|
|
setEditorView: (editorView: EditorView) => void
|
|
|
|
highlightRange: [number, number]
|
2023-04-03 16:05:25 +10:00
|
|
|
setHighlightRange: (range: Selection['range']) => void
|
2023-02-21 10:28:34 +11:00
|
|
|
isShiftDown: boolean
|
|
|
|
setIsShiftDown: (isShiftDown: boolean) => void
|
2023-06-22 16:43:33 +10:00
|
|
|
mediaStream?: MediaStream
|
|
|
|
setMediaStream: (mediaStream: MediaStream) => void
|
2023-06-23 14:19:15 +10:00
|
|
|
isStreamReady: boolean
|
|
|
|
setIsStreamReady: (isStreamReady: boolean) => void
|
2024-02-15 13:56:31 -08:00
|
|
|
isKclLspServerReady: boolean
|
|
|
|
isCopilotLspServerReady: boolean
|
|
|
|
setIsKclLspServerReady: (isKclLspServerReady: boolean) => void
|
|
|
|
setIsCopilotLspServerReady: (isCopilotLspServerReady: boolean) => void
|
2023-09-11 16:21:23 -04:00
|
|
|
buttonDownInStream: number | undefined
|
|
|
|
setButtonDownInStream: (buttonDownInStream: number | undefined) => void
|
2023-08-09 20:49:10 +10:00
|
|
|
didDragInStream: boolean
|
|
|
|
setDidDragInStream: (didDragInStream: boolean) => void
|
2023-08-06 21:29:26 -04:00
|
|
|
fileId: string
|
|
|
|
setFileId: (fileId: string) => void
|
2023-08-09 20:49:10 +10:00
|
|
|
streamDimensions: { streamWidth: number; streamHeight: number }
|
|
|
|
setStreamDimensions: (dimensions: {
|
|
|
|
streamWidth: number
|
|
|
|
streamHeight: number
|
|
|
|
}) => void
|
2024-04-11 13:15:49 -07:00
|
|
|
setHtmlRef: (ref: React.RefObject<HTMLDivElement>) => void
|
|
|
|
htmlRef: React.RefObject<HTMLDivElement> | null
|
2023-06-19 10:16:45 +10:00
|
|
|
|
|
|
|
showHomeMenu: boolean
|
|
|
|
setHomeShowMenu: (showMenu: boolean) => void
|
2023-08-06 21:29:26 -04:00
|
|
|
openPanes: PaneType[]
|
|
|
|
setOpenPanes: (panes: PaneType[]) => void
|
2023-06-19 10:16:45 +10:00
|
|
|
homeMenuItems: {
|
|
|
|
name: string
|
|
|
|
path: string
|
|
|
|
}[]
|
|
|
|
setHomeMenuItems: (items: { name: string; path: string }[]) => void
|
2024-03-22 10:23:04 +11:00
|
|
|
lastCodeMirrorSelectionUpdatedFromScene: number
|
|
|
|
setLastCodeMirrorSelectionUpdatedFromScene: (time: number) => void
|
2022-11-26 05:13:07 +11:00
|
|
|
}
|
|
|
|
|
2023-02-21 14:50:22 +11:00
|
|
|
export const useStore = create<StoreState>()(
|
|
|
|
persist(
|
2023-09-08 17:50:37 +10:00
|
|
|
(set, get) => {
|
|
|
|
return {
|
|
|
|
editorView: null,
|
|
|
|
setEditorView: (editorView) => {
|
|
|
|
set({ editorView })
|
|
|
|
},
|
|
|
|
highlightRange: [0, 0],
|
|
|
|
setHighlightRange: (selection) => {
|
|
|
|
set({ highlightRange: selection })
|
|
|
|
const editorView = get().editorView
|
2024-02-11 12:59:00 +11:00
|
|
|
const safeEnd = Math.min(
|
|
|
|
selection[1],
|
|
|
|
editorView?.state.doc.length || selection[1]
|
|
|
|
)
|
2023-09-08 17:50:37 +10:00
|
|
|
if (editorView) {
|
2024-02-11 12:59:00 +11:00
|
|
|
editorView.dispatch({
|
|
|
|
effects: addLineHighlight.of([selection[0], safeEnd]),
|
|
|
|
})
|
2023-06-22 16:43:33 +10:00
|
|
|
}
|
2023-09-08 17:50:37 +10:00
|
|
|
},
|
|
|
|
isShiftDown: false,
|
|
|
|
setIsShiftDown: (isShiftDown) => set({ isShiftDown }),
|
|
|
|
setMediaStream: (mediaStream) => set({ mediaStream }),
|
|
|
|
isStreamReady: false,
|
|
|
|
setIsStreamReady: (isStreamReady) => set({ isStreamReady }),
|
2024-02-15 13:56:31 -08:00
|
|
|
isKclLspServerReady: false,
|
|
|
|
isCopilotLspServerReady: false,
|
|
|
|
setIsKclLspServerReady: (isKclLspServerReady) =>
|
|
|
|
set({ isKclLspServerReady }),
|
|
|
|
setIsCopilotLspServerReady: (isCopilotLspServerReady) =>
|
|
|
|
set({ isCopilotLspServerReady }),
|
2023-09-11 16:21:23 -04:00
|
|
|
buttonDownInStream: undefined,
|
2023-09-08 10:13:35 -04:00
|
|
|
setButtonDownInStream: (buttonDownInStream) => {
|
|
|
|
set({ buttonDownInStream })
|
2023-09-08 17:50:37 +10:00
|
|
|
},
|
2024-04-11 13:15:49 -07:00
|
|
|
setHtmlRef: (htmlRef) => {
|
|
|
|
set({ htmlRef })
|
|
|
|
},
|
|
|
|
htmlRef: null,
|
2023-09-08 17:50:37 +10:00
|
|
|
didDragInStream: false,
|
|
|
|
setDidDragInStream: (didDragInStream) => {
|
|
|
|
set({ didDragInStream })
|
|
|
|
},
|
|
|
|
// For stream event handling
|
|
|
|
fileId: '',
|
|
|
|
setFileId: (fileId) => set({ fileId }),
|
|
|
|
streamDimensions: { streamWidth: 1280, streamHeight: 720 },
|
2023-09-25 19:49:53 -07:00
|
|
|
setStreamDimensions: (streamDimensions) => {
|
|
|
|
set({ streamDimensions })
|
|
|
|
},
|
2023-07-26 11:47:18 -05:00
|
|
|
|
2023-09-08 17:50:37 +10:00
|
|
|
// tauri specific app settings
|
|
|
|
defaultDir: {
|
|
|
|
dir: '',
|
|
|
|
},
|
|
|
|
openPanes: ['code'],
|
|
|
|
setOpenPanes: (openPanes) => set({ openPanes }),
|
|
|
|
showHomeMenu: true,
|
|
|
|
setHomeShowMenu: (showHomeMenu) => set({ showHomeMenu }),
|
|
|
|
homeMenuItems: [],
|
|
|
|
setHomeMenuItems: (homeMenuItems) => set({ homeMenuItems }),
|
2024-03-22 10:23:04 +11:00
|
|
|
lastCodeMirrorSelectionUpdatedFromScene: Date.now(),
|
|
|
|
setLastCodeMirrorSelectionUpdatedFromScene: (time) =>
|
|
|
|
set({ lastCodeMirrorSelectionUpdatedFromScene: time }),
|
2023-09-08 17:50:37 +10:00
|
|
|
}
|
|
|
|
},
|
2023-02-21 14:50:22 +11:00
|
|
|
{
|
|
|
|
name: 'store',
|
|
|
|
partialize: (state) =>
|
|
|
|
Object.fromEntries(
|
2023-10-11 13:36:54 +11:00
|
|
|
Object.entries(state).filter(([key]) => ['openPanes'].includes(key))
|
2023-02-21 14:50:22 +11:00
|
|
|
),
|
2023-01-13 17:58:37 +11:00
|
|
|
}
|
2023-02-21 14:50:22 +11:00
|
|
|
)
|
|
|
|
)
|
2023-09-15 04:35:48 -07:00
|
|
|
|
2023-10-11 13:36:54 +11:00
|
|
|
export async function executeAst({
|
2023-09-15 04:35:48 -07:00
|
|
|
ast,
|
|
|
|
engineCommandManager,
|
|
|
|
useFakeExecutor = false,
|
2023-11-06 11:49:13 +11:00
|
|
|
programMemoryOverride,
|
2023-09-15 04:35:48 -07:00
|
|
|
}: {
|
|
|
|
ast: Program
|
|
|
|
engineCommandManager: EngineCommandManager
|
|
|
|
useFakeExecutor?: boolean
|
2023-11-06 11:49:13 +11:00
|
|
|
programMemoryOverride?: ProgramMemory
|
2023-09-15 04:35:48 -07:00
|
|
|
}): Promise<{
|
|
|
|
logs: string[]
|
|
|
|
errors: KCLError[]
|
|
|
|
programMemory: ProgramMemory
|
|
|
|
}> {
|
|
|
|
try {
|
|
|
|
if (!useFakeExecutor) {
|
|
|
|
engineCommandManager.endSession()
|
|
|
|
engineCommandManager.startNewSession()
|
|
|
|
}
|
|
|
|
const programMemory = await (useFakeExecutor
|
2024-02-11 18:26:09 -08:00
|
|
|
? enginelessExecutor(ast, programMemoryOverride || programMemoryInit())
|
2024-03-26 19:32:31 -07:00
|
|
|
: _executor(ast, programMemoryInit(), engineCommandManager, false))
|
2023-09-15 04:35:48 -07:00
|
|
|
|
2023-10-14 03:47:46 +11:00
|
|
|
await engineCommandManager.waitForAllCommands()
|
2023-09-15 04:35:48 -07:00
|
|
|
return {
|
|
|
|
logs: [],
|
|
|
|
errors: [],
|
|
|
|
programMemory,
|
|
|
|
}
|
|
|
|
} catch (e: any) {
|
|
|
|
if (e instanceof KCLError) {
|
|
|
|
return {
|
|
|
|
errors: [e],
|
|
|
|
logs: [],
|
|
|
|
programMemory: {
|
|
|
|
root: {},
|
|
|
|
return: null,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
console.log(e)
|
|
|
|
return {
|
|
|
|
logs: [e],
|
|
|
|
errors: [],
|
|
|
|
programMemory: {
|
|
|
|
root: {},
|
|
|
|
return: null,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|