2024-04-15 12:04:17 -04:00
|
|
|
import ReactJson from 'react-json-view'
|
|
|
|
import { useMemo } from 'react'
|
|
|
|
import { ProgramMemory, Path, ExtrudeSurface } from 'lang/wasm'
|
|
|
|
import { useKclContext } from 'lang/KclProvider'
|
|
|
|
import { useResolvedTheme } from 'hooks/useResolvedTheme'
|
2024-05-17 16:40:20 -04:00
|
|
|
import { ActionButton } from 'components/ActionButton'
|
|
|
|
import toast from 'react-hot-toast'
|
|
|
|
import Tooltip from 'components/Tooltip'
|
|
|
|
|
|
|
|
export const MemoryPaneMenu = () => {
|
|
|
|
const { programMemory } = useKclContext()
|
|
|
|
|
|
|
|
function copyProgramMemoryToClipboard() {
|
|
|
|
if (globalThis && 'navigator' in globalThis) {
|
|
|
|
try {
|
|
|
|
navigator.clipboard.writeText(JSON.stringify(programMemory))
|
|
|
|
toast.success('Program memory copied to clipboard')
|
|
|
|
} catch (e) {
|
|
|
|
toast.error('Failed to copy program memory to clipboard')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<ActionButton
|
|
|
|
Element="button"
|
|
|
|
iconStart={{
|
|
|
|
icon: 'clipboardPlus',
|
|
|
|
iconClassName: '!text-current',
|
|
|
|
bgClassName: 'bg-transparent',
|
|
|
|
}}
|
|
|
|
className="!p-0 !bg-transparent hover:text-primary border-transparent hover:border-primary !outline-none"
|
|
|
|
onClick={copyProgramMemoryToClipboard}
|
|
|
|
>
|
|
|
|
<Tooltip position="bottom-right" delay={750}>
|
|
|
|
Copy to clipboard
|
|
|
|
</Tooltip>
|
|
|
|
</ActionButton>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
2024-04-15 12:04:17 -04:00
|
|
|
|
|
|
|
export const MemoryPane = () => {
|
|
|
|
const theme = useResolvedTheme()
|
|
|
|
const { programMemory } = useKclContext()
|
|
|
|
const ProcessedMemory = useMemo(
|
|
|
|
() => processMemory(programMemory),
|
|
|
|
[programMemory]
|
|
|
|
)
|
|
|
|
return (
|
|
|
|
<div className="h-full relative">
|
|
|
|
<div className="absolute inset-0 p-2 flex flex-col items-start">
|
|
|
|
<div className="overflow-auto h-full w-full pb-12">
|
|
|
|
<ReactJson
|
|
|
|
src={ProcessedMemory}
|
|
|
|
collapsed={1}
|
|
|
|
collapseStringsAfterLength={60}
|
|
|
|
enableClipboard={false}
|
|
|
|
displayDataTypes={false}
|
|
|
|
displayObjectSize={true}
|
|
|
|
indentWidth={2}
|
|
|
|
quotesOnKeys={false}
|
2024-05-20 17:28:51 -05:00
|
|
|
sortKeys={true}
|
2024-04-15 12:04:17 -04:00
|
|
|
name={false}
|
|
|
|
theme={theme === 'light' ? 'rjv-default' : 'monokai'}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export const processMemory = (programMemory: ProgramMemory) => {
|
|
|
|
const processedMemory: any = {}
|
|
|
|
Object.keys(programMemory?.root || {}).forEach((key) => {
|
|
|
|
const val = programMemory.root[key]
|
|
|
|
if (typeof val.value !== 'function') {
|
|
|
|
if (val.type === 'SketchGroup') {
|
|
|
|
processedMemory[key] = val.value.map(({ __geoMeta, ...rest }: Path) => {
|
|
|
|
return rest
|
|
|
|
})
|
|
|
|
} else if (val.type === 'ExtrudeGroup') {
|
|
|
|
processedMemory[key] = val.value.map(({ ...rest }: ExtrudeSurface) => {
|
|
|
|
return rest
|
|
|
|
})
|
2024-05-19 20:56:19 +10:00
|
|
|
} else if ((val.type as any) === 'Function') {
|
|
|
|
processedMemory[key] = `__function(${(val as any)?.expression?.params
|
|
|
|
?.map?.(({ identifier }: any) => identifier?.name || '')
|
|
|
|
.join(', ')})__`
|
2024-04-15 12:04:17 -04:00
|
|
|
} else {
|
|
|
|
processedMemory[key] = val.value
|
|
|
|
}
|
|
|
|
} else if (key !== 'log') {
|
|
|
|
processedMemory[key] = '__function__'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return processedMemory
|
|
|
|
}
|