Skip to content

The VS Code of video editing, built for agents.

TypeScript

Enhance @inspect variable functionality and UI controls

konstantin-paulus914c2222aa9c
diff --git a/apps/desktop/src/source.ts b/apps/desktop/src/source.ts
index f7c1f4e4..faf1d3bc 100644
--- a/apps/desktop/src/source.ts
+++ b/apps/desktop/src/source.ts
@@ -134,7 +134,20 @@ const INSPECT_ANNOTATION = new RegExp(`(?:^|\\n)\\s*\\*?\\s*@${INSPECT_TAG}\\b([
const INSPECT_PAIR = /(\w+)=(?:"([^"]*)"|(\S+))/g;
-type ParsedInspect = Pick<InspectDeclaration, "type" | "path" | "min" | "max" | "step">;
+/**
+ * TS nodes that hold a value expression: `x as T`, `x satisfies T`, `x!`,
+ * `f<T>`. A reference under one of these is read at runtime; anything else
+ * TS-flavored above a reference is a type position, which mentions the name
+ * without reading the value.
+ */
+const TS_VALUE_NODES: ReadonlySet<string> = new Set([
+ "TSAsExpression", "TSSatisfiesExpression", "TSNonNullExpression", "TSInstantiationExpression",
+]);
+
+const inTypePosition = (reference: NodePath): boolean =>
+ reference.findParent((parent) => parent.node.type.startsWith("TS") && !TS_VALUE_NODES.has(parent.node.type)) !== null;
+
+type ParsedInspect = Pick<InspectDeclaration, "type" | "path" | "min" | "max" | "step" | "options">;
const isInspectType = (value: string): value is InspectType => (INSPECT_TYPES as readonly string[]).includes(value);
@@ -185,8 +198,19 @@ function parseInspectComments(comments: readonly t.Comment[] | null | undefined)
parsed.path = segments;
}
+ if (options.options !== undefined && type !== "select") {
+ throw new Error(`@${INSPECT_TAG}: "options" only applies to a select`);
+ }
+ if (type === "select") {
+ const choices = (options.options ?? "").split(",").map((choice) => choice.trim()).filter(Boolean);
+ if (choices.length < 2) {
+ throw new Error(`@${INSPECT_TAG}: a select needs options to choose between, as options="a,b,c"`);
+ }
+ parsed.options = choices;
+ }
+
for (const key of Object.keys(options)) {
- if (!["min", "max", "step", "path", "label"].includes(key)) {
+ if (!["min", "max", "step", "path", "label", "options"].includes(key)) {
throw new Error(`@${INSPECT_TAG}: unknown option "${key}"`);
}
}
@@ -202,9 +226,20 @@ function inspectInitializer(node: t.Expression | null | undefined, type: Inspect
if (node.type === "UnaryExpression" && (node.operator === "-" || node.operator === "+") && node.argument.type === "NumericLiteral") return node;
return undefined;
}
+ if (type === "boolean") return node.type === "BooleanLiteral" ? node : undefined;
return node.type === "StringLiteral" ? node : undefined;
}
+/** What the initializer of each type must literally be, for the error message. */
+const INSPECT_LITERALS: Record<InspectType, string> = {
+ number: "number",
+ boolean: "true/false",
+ color: "string",
+ text: "string",
+ font: "string",
+ select: "string",
+};
+
/**
* Turns `@inspect`-annotated top-level consts into signals: the declaration's
* initializer becomes an `__inspect(declaration, literal)` call — which the
@@ -264,7 +299,14 @@ export function inspectPlugin({ types }: BabelApi, { file }: { file: string }):
const initial = inspectInitializer(declarator.init, annotation.type);
if (!initial) {
throw path.buildCodeFrameError(
- `an @${INSPECT_TAG} ${annotation.type} must be initialized with a ${annotation.type === "number" ? "number" : "string"} literal — it is what the editor writes back to`,
+ `an @${INSPECT_TAG} ${annotation.type} must be initialized with a ${INSPECT_LITERALS[annotation.type]} literal — it is what the editor writes back to`,
+ );
+ }
+ // A select must start on one of its own choices, or the control
+ // would show a value it cannot offer.
+ if (annotation.type === "select" && initial.type === "StringLiteral" && !annotation.options!.includes(initial.value)) {
+ throw path.buildCodeFrameError(
+ `an @${INSPECT_TAG} select is initialized with "${initial.value}", which is not one of its options (${annotation.options!.join(", ")})`,
);
}
@@ -273,7 +315,7 @@ export function inspectPlugin({ types }: BabelApi, { file }: { file: string }):
const binding = program.scope.getBinding(name);
for (const reference of binding?.referencePaths ?? []) {
// A type position mentions the name without reading the value.
- if (reference.findParent((parent) => parent.node.type.startsWith("TS"))) continue;
+ if (inTypePosition(reference)) continue;
if (reference.parentPath?.isExportSpecifier()) {
throw reference.buildCodeFrameError(
`an @${INSPECT_TAG} variable cannot be exported: another module would import the value, not the control`,
diff --git a/apps/web/src/components/sidebar-right/inspector/variables.tsx b/apps/web/src/components/sidebar-right/inspector/variables.tsx
index c15fe627..a74cbf1c 100644
--- a/apps/web/src/components/sidebar-right/inspector/variables.tsx
+++ b/apps/web/src/components/sidebar-right/inspector/variables.tsx
@@ -29,6 +29,15 @@ import {
import { Icon } from "@/components/ui/icon";
import { Button } from "@/components/ui/button";
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";
+import { Switch as SwitchToggle, SwitchControl, SwitchInput, SwitchThumb } from "@/components/ui/switch";
+import {
+ Select,
+ SelectContent,
+ SelectItem,
+ SelectPortal,
+ SelectTrigger,
+ SelectValue,
+} from "@/components/ui/select";
import { For, Match, Switch, createMemo, createSignal } from "solid-js";
import { WebFonts, loadWebFont } from "@diffusionstudio/runtime";
import { useWorld } from "@diffusionstudio/koota-solid";
@@ -109,6 +118,41 @@ function VariableControl(props: { entry: InspectEntry }) {
/>
</ControlRow>
</Match>
+ <Match when={entry().type === "boolean"}>
+ <ControlRow label={entry().label} contentClass="flex justify-end">
+ <SwitchToggle
+ checked={Boolean(entry().get())}
+ onChange={(checked) => commit(checked)}
+ class="flex items-center"
+ >
+ <SwitchInput aria-label={entry().label} />
+ <SwitchControl variant="compact">
+ <SwitchThumb variant="compact" />
+ </SwitchControl>
+ </SwitchToggle>
+ </ControlRow>
+ </Match>
+ <Match when={entry().type === "select"}>
+ <ControlRow label={entry().label}>
+ <Select
+ value={String(entry().get())}
+ onChange={(value) => {
+ if (value !== null) commit(value);
+ }}
+ options={entry().options ?? []}
+ itemComponent={(itemProps) => (
+ <SelectItem item={itemProps.item}>{itemProps.item.rawValue}</SelectItem>
+ )}
+ >
+ <SelectTrigger>
+ <SelectValue>{String(entry().get())}</SelectValue>
+ </SelectTrigger>
+ <SelectPortal>
+ <SelectContent />
+ </SelectPortal>
+ </Select>
+ </ControlRow>
+ </Match>
</Switch>
);
}
diff --git a/examples/09-inspect-variables.tsx b/examples/09-inspect-variables.tsx
index 2259783e..f5379407 100644
--- a/examples/09-inspect-variables.tsx
+++ b/examples/09-inspect-variables.tsx
@@ -23,29 +23,37 @@ const fontSize = 120;
const textColor = "#ffffff";
/** @inspect color */
-const backdrop = "#161616";
+const backdrop = "#000000";
/** @inspect number min=0 max=200 */
const padding = 64;
+/** @inspect boolean path="Frame/Show" */
+const showFrame = true;
+
+/** @inspect select options="left,center,right" path="Typography/Align" */
+const align = "center";
+
export default function InspectVariables() {
return (
<stage>
<scene name="Inspect" width={1920} height={1080} fill={backdrop} active>
- <rect
- x={padding}
- y={padding}
- width={1920 - padding * 2}
- height={1080 - padding * 2}
- fill="transparent"
- cornerRadius={24}
- >
- <stroke color="#ffffff33" width={2} />
- </rect>
+ {showFrame && (
+ <rect
+ x={padding}
+ y={padding}
+ width={1920 - padding * 2}
+ height={1080 - padding * 2}
+ fill="transparent"
+ cornerRadius={24}
+ >
+ <stroke color="#ffffff33" width={2} />
+ </rect>
+ )}
<text
width={1920}
height={1080}
- textAlign="center"
+ textAlign={align as "left" | "center" | "right"}
textBaseline="middle"
color={textColor}
fontFamily={fontFamily}
diff --git a/packages/jsx/src/inspect.ts b/packages/jsx/src/inspect.ts
index 685f00b9..04841e08 100644
--- a/packages/jsx/src/inspect.ts
+++ b/packages/jsx/src/inspect.ts
@@ -20,8 +20,9 @@
* it, the label is the variable's own name, prettified, and the control is
* ungrouped. `label="Font"` names the control without grouping it.
*
- * The initializer must be a plain literal — a `number` a numeric one, the
- * string types a string — since it is what the editor overwrites. Because the
+ * The initializer must be a plain literal — a `number` a numeric one, a
+ * `boolean` `true`/`false`, the string types a string (a `select`'s one of its
+ * `options="a,b,c"`) — since it is what the editor overwrites. Because the
* rewrite is per file, an annotated variable cannot be exported: an importer
* would receive the accessor where it expects the value.
*/
@@ -32,12 +33,12 @@ import type { Accessor } from "solid-js";
export const INSPECT_TAG = "inspect";
/** The controls an annotation can ask for, keyed by how the value is edited. */
-export const INSPECT_TYPES = ["number", "color", "text", "font"] as const;
+export const INSPECT_TYPES = ["number", "color", "text", "font", "boolean", "select"] as const;
export type InspectType = (typeof INSPECT_TYPES)[number];
/** What an inspected variable can hold: what its literal initializer can be. */
-export type InspectValue = string | number;
+export type InspectValue = string | number | boolean;
/**
* An annotated declaration as the compile step spells it into the bundle —
@@ -55,6 +56,8 @@ export interface InspectDeclaration {
min?: number;
max?: number;
step?: number;
+ /** What a `select` chooses between, in the authored order. */
+ options?: string[];
}
function hostOnly(name: string): never {
diff --git a/packages/reconciler/src/inspect.ts b/packages/reconciler/src/inspect.ts
index a1c7c6fa..9e6bcaed 100644
--- a/packages/reconciler/src/inspect.ts
+++ b/packages/reconciler/src/inspect.ts
@@ -32,6 +32,8 @@ export interface InspectEntry {
min?: number;
max?: number;
step?: number;
+ /** What a `select` chooses between, in the authored order. */
+ options?: string[];
/** What the source declared, before anyone touched the signal. */
initial: InspectValue;
get: Accessor<InspectValue>;
@@ -80,6 +82,7 @@ export function __inspect(declaration: InspectDeclaration, initial: InspectValue
...(declaration.min === undefined ? {} : { min: declaration.min }),
...(declaration.max === undefined ? {} : { max: declaration.max }),
...(declaration.step === undefined ? {} : { step: declaration.step }),
+ ...(declaration.options === undefined ? {} : { options: declaration.options }),
initial,
get,
set,
diff --git a/reference/jsx/README.md b/reference/jsx/README.md
index b9e65029..3bb590fc 100644
--- a/reference/jsx/README.md
+++ b/reference/jsx/README.md
@@ -50,7 +50,7 @@ export default function Project() {
| [audio-sync.md](./audio-sync.md) | `syncTo` audio alignment |
| [captions.md](./captions.md) | `<captions>` and style presets |
| [generate.md](./generate.md) | Declarative AI asset generation (`generate.*`) |
-| [variables.md](./variables.md) | `@inspect` variables: annotated consts as live inspector controls |
+| [variables.md](./variables.md) | `@inspect` variables: annotated consts as live inspector controls (number, color, text, font, boolean, select) |
| [lifecycle.md](./lifecycle.md) | Mount lifecycle: always live, persisted + re-executed, `useTicker` |
| [errors.md](./errors.md) | Where each pipeline stage fails and with what effect |
diff --git a/reference/jsx/variables.md b/reference/jsx/variables.md
index 8a311155..03e4e059 100644
--- a/reference/jsx/variables.md
+++ b/reference/jsx/variables.md
@@ -29,7 +29,7 @@ export default function Project() {
## The annotation
```
-@inspect <type> [min=…] [max=…] [step=…] [path="Group/Label"] [label="Label"]
+@inspect <type> [min=…] [max=…] [step=…] [options="a,b,c"] [path="Group/Label"] [label="Label"]
```
| Type | Control | Initializer |
@@ -38,8 +38,18 @@ export default function Project() {
| `color` | Color picker | a string literal (`"#ff3366"`) |
| `text` | Text area | a string literal |
| `font` | Font picker (web + system fonts) | a string literal naming the family |
+| `boolean` | Toggle | `true` or `false` |
+| `select` | Dropdown over `options="a,b,c"` (required, at least two) | a string literal that is one of the options |
-`min`, `max` and `step` only apply to `number`.
+`min`, `max` and `step` only apply to `number`; `options` only to `select`.
+
+```tsx
+/** @inspect boolean */
+const showFrame = true;
+
+/** @inspect select options="left,center,right" */
+const align = "center";
+```
**Identity is the file and the variable name** — nothing else. Renaming the variable renames the control's identity; no id is minted, and nothing extra is written into your source.