2024-06-24 11:45:40 -04:00
|
|
|
import toast from 'react-hot-toast'
|
2024-04-15 12:04:17 -04:00
|
|
|
import ReactJson from 'react-json-view'
|
|
|
|
import { useMemo } from 'react'
|
Remove KclValue::SketchGroup variant (#3446)
We can store Rust types like `SketchGroup` as their own variant of `KclValue`, or as `KclValue::UserVal`. Sometimes we store in one and try to read from the other, which fails. This causes bugs, like #3338.
Instead, we should use either ::SketchGroup or ::UserVal, and stop using the other. If we stopped using ::UserVal, we'd need a new variant for every Rust type we wanted to build, including user-defined types. So I don't think that's practical.
Instead, we should store every KCL value by de/serializing it into UserVal. This is a first step along that path, removing just the SketchGroup variants. If it goes well, we can remove the other specialized variants too.
My only concern is there might be performance implications from how frequently we convert between serde_json::Value and Rust types via Serde. But I'm not too worried -- there's no parsing JSON strings, just traversing serde_json::Value trees. This isn't great for performance but I think it'll probably be miniscule in comparison to doing all the API calls.
2024-08-21 11:06:48 -05:00
|
|
|
import {
|
|
|
|
ProgramMemory,
|
|
|
|
Path,
|
|
|
|
ExtrudeSurface,
|
|
|
|
sketchGroupFromKclValue,
|
|
|
|
} from 'lang/wasm'
|
2024-04-15 12:04:17 -04:00
|
|
|
import { useKclContext } from 'lang/KclProvider'
|
|
|
|
import { useResolvedTheme } from 'hooks/useResolvedTheme'
|
2024-05-17 16:40:20 -04:00
|
|
|
import { ActionButton } from 'components/ActionButton'
|
Remove KclValue::SketchGroup variant (#3446)
We can store Rust types like `SketchGroup` as their own variant of `KclValue`, or as `KclValue::UserVal`. Sometimes we store in one and try to read from the other, which fails. This causes bugs, like #3338.
Instead, we should use either ::SketchGroup or ::UserVal, and stop using the other. If we stopped using ::UserVal, we'd need a new variant for every Rust type we wanted to build, including user-defined types. So I don't think that's practical.
Instead, we should store every KCL value by de/serializing it into UserVal. This is a first step along that path, removing just the SketchGroup variants. If it goes well, we can remove the other specialized variants too.
My only concern is there might be performance implications from how frequently we convert between serde_json::Value and Rust types via Serde. But I'm not too worried -- there's no parsing JSON strings, just traversing serde_json::Value trees. This isn't great for performance but I think it'll probably be miniscule in comparison to doing all the API calls.
2024-08-21 11:06:48 -05:00
|
|
|
import { err, trap } from 'lib/trap'
|
2024-05-17 16:40:20 -04:00
|
|
|
import Tooltip from 'components/Tooltip'
|
2024-05-24 20:54:42 +10:00
|
|
|
import { useModelingContext } from 'hooks/useModelingContext'
|
2024-05-17 16:40:20 -04:00
|
|
|
|
|
|
|
export const MemoryPaneMenu = () => {
|
|
|
|
const { programMemory } = useKclContext()
|
|
|
|
|
|
|
|
function copyProgramMemoryToClipboard() {
|
|
|
|
if (globalThis && 'navigator' in globalThis) {
|
2024-06-24 11:45:40 -04:00
|
|
|
navigator.clipboard
|
|
|
|
.writeText(JSON.stringify(programMemory))
|
|
|
|
.then(() => toast.success('Program memory copied to clipboard'))
|
|
|
|
.catch((e) =>
|
|
|
|
trap(new Error('Failed to copy program memory to clipboard'))
|
|
|
|
)
|
2024-05-17 16:40:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
2024-05-24 20:54:42 +10:00
|
|
|
const { state } = useModelingContext()
|
2024-04-15 12:04:17 -04:00
|
|
|
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>
|
2024-05-24 20:54:42 +10:00
|
|
|
{state.matches('Sketch') && (
|
|
|
|
<div
|
|
|
|
className="absolute inset-0 dark:bg-chalkboard-90/80 bg-chalkboard-10/80 cursor-not-allowed"
|
|
|
|
title="Variables won't update in sketch mode"
|
|
|
|
></div>
|
|
|
|
)}
|
2024-04-15 12:04:17 -04:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export const processMemory = (programMemory: ProgramMemory) => {
|
|
|
|
const processedMemory: any = {}
|
2024-07-22 19:43:40 -04:00
|
|
|
for (const [key, val] of programMemory?.visibleEntries()) {
|
2024-04-15 12:04:17 -04:00
|
|
|
if (typeof val.value !== 'function') {
|
Remove KclValue::SketchGroup variant (#3446)
We can store Rust types like `SketchGroup` as their own variant of `KclValue`, or as `KclValue::UserVal`. Sometimes we store in one and try to read from the other, which fails. This causes bugs, like #3338.
Instead, we should use either ::SketchGroup or ::UserVal, and stop using the other. If we stopped using ::UserVal, we'd need a new variant for every Rust type we wanted to build, including user-defined types. So I don't think that's practical.
Instead, we should store every KCL value by de/serializing it into UserVal. This is a first step along that path, removing just the SketchGroup variants. If it goes well, we can remove the other specialized variants too.
My only concern is there might be performance implications from how frequently we convert between serde_json::Value and Rust types via Serde. But I'm not too worried -- there's no parsing JSON strings, just traversing serde_json::Value trees. This isn't great for performance but I think it'll probably be miniscule in comparison to doing all the API calls.
2024-08-21 11:06:48 -05:00
|
|
|
const sg = sketchGroupFromKclValue(val, null)
|
|
|
|
if (!err(sg)) {
|
|
|
|
processedMemory[key] = sg.value.map(({ __geoMeta, ...rest }: Path) => {
|
2024-04-15 12:04:17 -04:00
|
|
|
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__'
|
|
|
|
}
|
2024-07-22 19:43:40 -04:00
|
|
|
}
|
2024-04-15 12:04:17 -04:00
|
|
|
return processedMemory
|
|
|
|
}
|