2025-04-01 23:54:26 -07:00
|
|
|
import { useMemo } from 'react'
|
2025-04-01 15:31:19 -07:00
|
|
|
import toast from 'react-hot-toast'
|
|
|
|
import ReactJson from 'react-json-view'
|
2025-04-01 23:54:26 -07:00
|
|
|
|
|
|
|
import type { ExtrudeSurface } from '@rust/kcl-lib/bindings/ExtrudeSurface'
|
|
|
|
import type { Path } from '@rust/kcl-lib/bindings/Path'
|
|
|
|
|
|
|
|
import { ActionButton } from '@src/components/ActionButton'
|
|
|
|
import Tooltip from '@src/components/Tooltip'
|
|
|
|
import { useModelingContext } from '@src/hooks/useModelingContext'
|
|
|
|
import { useResolvedTheme } from '@src/hooks/useResolvedTheme'
|
|
|
|
import { useKclContext } from '@src/lang/KclProvider'
|
|
|
|
import type { VariableMap } from '@src/lang/wasm'
|
|
|
|
import { sketchFromKclValueOptional } from '@src/lang/wasm'
|
|
|
|
import { Reason, trap } from '@src/lib/trap'
|
2024-05-17 16:40:20 -04:00
|
|
|
|
|
|
|
export const MemoryPaneMenu = () => {
|
2025-02-12 10:22:56 +13:00
|
|
|
const { variables } = useKclContext()
|
2024-05-17 16:40:20 -04:00
|
|
|
|
|
|
|
function copyProgramMemoryToClipboard() {
|
|
|
|
if (globalThis && 'navigator' in globalThis) {
|
2024-06-24 11:45:40 -04:00
|
|
|
navigator.clipboard
|
2025-02-12 10:22:56 +13:00
|
|
|
.writeText(JSON.stringify(variables))
|
2024-06-24 11:45:40 -04:00
|
|
|
.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}
|
|
|
|
>
|
2025-03-24 22:58:54 -04:00
|
|
|
<Tooltip position="bottom-right">Copy to clipboard</Tooltip>
|
2024-05-17 16:40:20 -04:00
|
|
|
</ActionButton>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
2024-04-15 12:04:17 -04:00
|
|
|
|
|
|
|
export const MemoryPane = () => {
|
|
|
|
const theme = useResolvedTheme()
|
2025-02-12 10:22:56 +13:00
|
|
|
const { variables } = useKclContext()
|
2024-05-24 20:54:42 +10:00
|
|
|
const { state } = useModelingContext()
|
2025-02-12 10:22:56 +13:00
|
|
|
const ProcessedMemory = useMemo(() => processMemory(variables), [variables])
|
2024-04-15 12:04:17 -04:00
|
|
|
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>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2025-02-12 10:22:56 +13:00
|
|
|
export const processMemory = (variables: VariableMap) => {
|
2024-04-15 12:04:17 -04:00
|
|
|
const processedMemory: any = {}
|
2025-02-12 10:22:56 +13:00
|
|
|
for (const [key, val] of Object.entries(variables)) {
|
|
|
|
if (val === undefined) continue
|
2024-10-28 20:52:51 -04:00
|
|
|
if (
|
2024-11-14 17:27:19 -06:00
|
|
|
val.type === 'Sketch' ||
|
2024-10-28 20:52:51 -04:00
|
|
|
// @ts-ignore
|
2024-11-14 17:27:19 -06:00
|
|
|
val.type !== 'Function'
|
2024-10-28 20:52:51 -04:00
|
|
|
) {
|
2024-11-19 17:34:54 -05:00
|
|
|
const sk = sketchFromKclValueOptional(val, key)
|
2024-09-27 15:44:44 -07:00
|
|
|
if (val.type === 'Solid') {
|
2025-01-22 09:42:09 +13:00
|
|
|
processedMemory[key] = val.value.value.map(
|
|
|
|
({ ...rest }: ExtrudeSurface) => {
|
|
|
|
return rest
|
|
|
|
}
|
|
|
|
)
|
2024-11-19 17:34:54 -05:00
|
|
|
} else if (!(sk instanceof Reason)) {
|
|
|
|
processedMemory[key] = sk.paths.map(({ __geoMeta, ...rest }: Path) => {
|
2024-04-15 12:04:17 -04:00
|
|
|
return rest
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
processedMemory[key] = val.value
|
|
|
|
}
|
2024-10-28 20:52:51 -04:00
|
|
|
//@ts-ignore
|
|
|
|
} else if (val.type === 'Function') {
|
2025-02-22 20:16:29 +13:00
|
|
|
processedMemory[key] = `__function__`
|
2024-04-15 12:04:17 -04:00
|
|
|
}
|
2024-07-22 19:43:40 -04:00
|
|
|
}
|
2024-04-15 12:04:17 -04:00
|
|
|
return processedMemory
|
|
|
|
}
|