Build and revise procedural 3D assets with your coding agent. Local MCP server, CLI and TypeScript engine with rendered review, editable source and GLB export.
JavaScript 3d3d-assets3d-modelingagent-skillsagent-toolsai-agentsasset-pipelinecligame-assetsglbgltfmcpmcp-servermodel-context-protocolprocedural-generationprocedural-modelingtext-to-3dthreejstypescript
---
import { createHash } from "node:crypto";
import { Code } from "astro:components";
import { workbenchContrast, workbenchTheme } from "../lib/shiki-contrast";
import { codePreview, collapseLabel, EXPAND_SCRIPT, expandLabel } from "../lib/code-preview";
import type { ComponentProps } from "astro/types";
interface Props {
code: string;
lang?: ComponentProps<typeof Code>["lang"];
label?: string;
/** Lines shown before the fade when the block is long enough to collapse (default 14). */
previewLines?: number;
}
const { code, lang = "javascript", label = "Source", previewLines } = Astro.props;
// The block always holds the whole file. A long one starts collapsed to its first lines once the page's script
// has run; without the script the whole file is simply shown.
const preview = codePreview(code, previewLines);
const regionId = `code-${createHash("sha256").update(label).update("\0").update(code).digest("hex").slice(0, 10)}`;
---
<div
class="code-block min-w-0 border border-rule bg-sheet"
data-code-block
data-language={lang}
data-preview-lines={preview.collapsible ? preview.previewLines : undefined}
data-expand-label={preview.collapsible ? expandLabel(preview.lines) : undefined}
data-collapse-label={preview.collapsible ? collapseLabel : undefined}
style={preview.collapsible ? `--preview-lines:${preview.previewLines}` : undefined}
>
{/* Copy is revealed by the page's script; the header is as tall as it will be, so revealing it moves nothing. */}
<div class="flex min-h-[calc(3.25rem+1px)] items-center justify-between gap-4 border-b border-rule px-4 py-1">
<span class="font-mono text-code text-dim">{label}</span>
<button
type="button"
class="min-h-11 px-2 font-mono text-code text-ink"
data-copy-code
hidden
aria-label={`Copy ${label}`}
>
Copy
</button>
<span data-copy-status role="status" class="sr-only"></span>
</div>
<div class="code-wrap">
<section
id={regionId}
class="code-scroll overflow-x-auto p-4"
tabindex="0"
aria-label={`${label}, horizontally scrollable`}
>
<Code code={code} lang={lang} theme={workbenchTheme} transformers={[workbenchContrast]} />
</section>
{preview.collapsible && <div class="code-fade" aria-hidden="true"></div>}
</div>
{
preview.collapsible && (
<button
type="button"
class="flex min-h-11 w-full items-center gap-2 border-t border-rule px-4 py-1 text-left font-mono text-code text-ink"
data-code-expand
aria-expanded="false"
aria-controls={regionId}
hidden
>
<span data-code-expand-text>{expandLabel(preview.lines)}</span>
<span class="sr-only">{` of ${label}`}</span>
<span aria-hidden="true" data-code-expand-icon>↓</span>
</button>
)
}
<p data-scroll-hint hidden class="border-t border-rule px-4 py-1 font-mono text-code text-dim">
Scroll code horizontally <span aria-hidden="true">↔</span>
</p>
{/* Last in the block, so it runs while the page is still being parsed: collapsed before anything below exists. */}
{preview.collapsible && <script is:inline set:html={EXPAND_SCRIPT} />}
</div>
<style is:global>
.code-wrap {
position: relative;
}
.code-scroll pre { overflow: visible !important; width: max-content; min-width: 100%; max-width: none; margin: 0; }
.code-block:is([data-language="sh"],[data-language="bash"],[data-language="shell"]) .code-scroll pre { width: auto; white-space: pre-wrap; overflow-wrap: anywhere; }
.code-fade {
display: none;
}
/* Collapsed: the first lines of the file and half of the next, so the cut is visibly mid-file. */
.code-block[data-collapsed] .code-scroll {
font-size: var(--text-code);
max-height: calc(2rem + (var(--preview-lines) + 0.5) * 1lh);
overflow-y: hidden;
}
.code-block[data-collapsed] .code-fade {
display: block;
position: absolute;
inset-inline: 0;
bottom: 0;
height: 5.5rem;
pointer-events: none;
background: linear-gradient(to bottom, transparent, var(--color-sheet) 88%);
}
/* Expanded: the control that undoes it stays at the bottom of the viewport while the block is in view, so the focus
that just activated it does not end up hundreds of lines below the screen. */
.code-block:not([data-collapsed]) [data-code-expand] {
position: sticky;
bottom: 0;
background: var(--color-sheet);
}
</style>
<script>
const SELECTOR = "[data-code-block]";
async function copy(block: HTMLElement, button: HTMLButtonElement) {
// The block holds the whole file whether or not it is collapsed, so this is always the whole file.
const codeElement = block.querySelector("code");
const status = block.querySelector("[data-copy-status]");
if (status) status.textContent = "";
try {
await navigator.clipboard.writeText(codeElement?.textContent ?? "");
button.textContent = "Copied";
if (status) status.textContent = "Code copied to clipboard.";
} catch {
if (codeElement) {
const range = document.createRange();
range.selectNodeContents(codeElement);
const selection = window.getSelection();
selection?.removeAllRanges();
selection?.addRange(range);
}
button.textContent = "Code selected";
if (status)
status.textContent =
"Clipboard unavailable. Code selected; use your browser’s copy command.";
}
setTimeout(() => {
button.textContent = "Copy";
}, 2000);
}
function enhance(block: HTMLElement) {
const scroll = block.querySelector<HTMLElement>(".code-scroll");
const hint = block.querySelector<HTMLElement>("[data-scroll-hint]");
if (scroll && hint) {
const sync = () => { hint.hidden = scroll.scrollWidth <= scroll.clientWidth + 1; };
new ResizeObserver(sync).observe(scroll);
sync();
}
const copyButton = block.querySelector<HTMLButtonElement>("[data-copy-code]");
if (copyButton) {
copyButton.hidden = false;
copyButton.addEventListener("click", () => copy(block, copyButton));
}
}
document.querySelectorAll<HTMLElement>(SELECTOR).forEach(enhance);
</script>