The VS Code of video editing, built for agents.
TypeScript
Add source settings component to inspector and refactor fill-picker
konstantin-paulus2725d5a87f2e
diff --git a/apps/web/src/components/sidebar-right/inspector/fill-picker.tsx b/apps/web/src/components/sidebar-right/inspector/fill-picker.tsxindex fb61cd95..9dfdd5cd 100644--- a/apps/web/src/components/sidebar-right/inspector/fill-picker.tsx+++ b/apps/web/src/components/sidebar-right/inspector/fill-picker.tsx@@ -58,13 +58,13 @@ const DEFAULT_GRADIENT_STOPS = ["#E0E0E0", "#000000"];* The fit modes `objectFit` can say. `cover` is what an unset prop renders* as, and `ScaleModeType.NONE` has no spelling, so it is not offered.*/-const FIT_OPTIONS: ReadonlyArray<{ value: Fit; label: string }> = [+export const FIT_OPTIONS: ReadonlyArray<{ value: Fit; label: string }> = [{ value: "cover", label: "Cover" },{ value: "contain", label: "Contain" },{ value: "fill", label: "Fill" },];-const FIT_NAMES: Partial<Record<ScaleModeType, Fit>> = {+export const FIT_NAMES: Partial<Record<ScaleModeType, Fit>> = {[ScaleModeType.COVER]: "cover",[ScaleModeType.FIT]: "contain",[ScaleModeType.FILL]: "fill",@@ -242,8 +242,12 @@ type FitMenuProps = {fill: Entity;};-/** How an asset fill maps into the box: the paint's `objectFit`. */-function FitMenu(props: FitMenuProps) {+/**+ * How an asset fill maps into the box: the paint's `objectFit`. Also serves+ * the Source section's picker, where `fill` is the geometry itself — an+ * intrinsic paint's traits live on the node, and the prop is the same.+ */+export function FitMenu(props: FitMenuProps) {const editor = useEditor();const scaleMode = useTrait(() => props.fill, ScaleMode);diff --git a/apps/web/src/components/sidebar-right/inspector/inspector.tsx b/apps/web/src/components/sidebar-right/inspector/inspector.tsxindex b073be86..1fa53465 100644--- a/apps/web/src/components/sidebar-right/inspector/inspector.tsx+++ b/apps/web/src/components/sidebar-right/inspector/inspector.tsx@@ -31,6 +31,7 @@ import { TransformSettings } from "./transform";import { CaptionSettings } from "./caption-settings";import { TextPanel } from "./text";import { FillsSettings } from "./fills";+import { SourceSettings } from "./source";import { StrokesSettings } from "./strokes";import { ShadowsSettings } from "./shadows";import { EffectsSettings } from "./effects";@@ -150,6 +151,10 @@ export function Inspector() {<FillsSettings selection={nodes()} /></Show>+ <Show when={includesTarget("shape")}>+ <SourceSettings selection={nodes()} />+ </Show>+<Show when={includesTarget("shape", "text", "scene", "caption")}><StrokesSettings selection={nodes()} /></Show>diff --git a/apps/web/src/components/sidebar-right/inspector/source.tsx b/apps/web/src/components/sidebar-right/inspector/source.tsxnew file mode 100644index 00000000..788fd6f3--- /dev/null+++ b/apps/web/src/components/sidebar-right/inspector/source.tsx@@ -0,0 +1,282 @@+/* This Source Code Form is subject to the terms of the Mozilla Public+ * License, v. 2.0. If a copy of the MPL was not distributed with this+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */++import { Show, createEffect, createMemo, createSignal } from "solid-js";+import { Button } from "@/components/ui/button";+import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";+import {+ FloatingInspector,+ FloatingInspectorContent,+ FloatingInspectorHeader,+ FloatingInspectorTitle,+} from "@/components/ui/floating-inspector";+import { Icon } from "@/components/ui/icon";+import { PanelSection } from "@/components/ui/panel-section";+import { ControlRow } from "@/components/ui/control-group";+import { AssetThumbnail } from "@/components/ui/asset-thumbnail";+import { OpacitySwatch } from "@/components/ui/opacity-swatch";+import { assetName } from "@diffusionstudio/assets";+import { useHas, useTrait } from "@diffusionstudio/koota-solid";+import { AssetId, Color, Computed, PaintType, colorToHex, getIntrinsicPaint, parseColor } from "@diffusionstudio/runtime";+import { useDerived, useEditor } from "@/engine/hooks";+import { useLibrary } from "@/engine/library";+import { FitMenu } from "./fill-picker";+import { AssetFillPicker } from "./asset-picker";++import type { Asset } from "@diffusionstudio/assets";+import type { Entity } from "koota";++/** The row label, like a fill row's: which kind of media the node is. */+const MEDIA_LABELS: Partial<Record<PaintType, string>> = {+ [PaintType.VIDEO]: "Video",+ [PaintType.IMAGE]: "Image",+};++/** What a non-media intrinsic is, said instead of controls it cannot have. */+const INTRINSIC_LABELS: Partial<Record<PaintType, string>> = {+ [PaintType.HTML]: "HTML content",+ [PaintType.SURFACE]: "Canvas surface",+ [PaintType.WAVEFORM]: "Audio waveform",+};++type SourceSettingsProps = {+ selection: Entity[];+};++/**+ * What the node is intrinsically painted with, as its own section beneath+ * Fill, which mirrors the canvas: the intrinsics draw beneath every paint+ * child. Two rows can show — the intrinsic paint (a `<video>`'s footage, an+ * `<image>`'s picture, a `<surface>`'s canvas) and, beneath it as it draws,+ * the intrinsic solid (the `fill` prop). An intrinsic is not an element of+ * its own, so it offers exactly two things: another value (a `src` or `fill`+ * write, `objectFit` through the picker's fit menu — the node keeps its+ * identity) or removal. Removing the solid takes the prop off; removing the+ * media takes the element's nature off, rewriting it as the `<rect>` it+ * otherwise was (see `DocumentEditor.removeIntrinsicPaint`), and the section+ * goes with it. A surface or html intrinsic has neither; its row just says+ * what the node is made of.+ */+export function SourceSettings(props: SourceSettingsProps) {+ const editor = useEditor();+ const library = useLibrary();+ const entity = () => props.selection[0]!;++ let anchorRef!: HTMLDivElement;++ const [picking, setPicking] = createSignal(false);++ const intrinsic = useDerived(() => getIntrinsicPaint(entity()));+ const isMedia = createMemo(+ () => intrinsic() === PaintType.VIDEO || intrinsic() === PaintType.IMAGE,+ );++ // The node's intrinsic solid (the `fill` prop, its Color trait): the other+ // intrinsic, drawn beneath even the intrinsic paint.+ const hasFill = useHas(() => entity(), Color);++ const assetId = useTrait(() => entity(), AssetId);+ const asset = createMemo(() => library()?.get(assetId()?.value ?? ""));++ // Same element, another source: the paint follows what the src turns out+ // to name (an image path onto a <video> paints as a picture), so a swap is+ // only ever a `src` write and the node keeps its identity.+ const handleSelectAsset = (picked: Asset) => {+ editor.editProperty(entity(), "src", picked.path);+ };++ // Removal is the element ceasing to be a media element: the node is+ // rewritten as the <rect> it otherwise was, and this section goes with it.+ const handleRemoveMedia = () => {+ setPicking(false);+ editor.removeIntrinsicPaint(entity());+ };++ return (+ <Show when={intrinsic() !== undefined || hasFill()}>+ <PanelSection title="Source" ref={anchorRef}>+ <Show+ when={isMedia()}+ fallback={+ <Show when={intrinsic() !== undefined}>+ <div class="flex h-7 items-center gap-2 rounded-md bg-input px-2 text-xs text-muted-foreground select-none">+ <Icon name="html-small" />+ <span class="min-w-0 truncate">+ {INTRINSIC_LABELS[intrinsic()!] ?? "Intrinsic paint"}+ </span>+ </div>+ </Show>+ }+ >+ <ControlRow label={MEDIA_LABELS[intrinsic()!]}>+ <div class="flex h-7 w-full items-center overflow-hidden rounded-md border border-transparent bg-input text-foreground focus-within:border-primary">+ <button+ class="flex h-full min-w-0 flex-1 items-center gap-2 pl-1 text-left"+ onClick={() => setPicking(true)}+ >+ <span class="size-5 min-w-5 overflow-hidden rounded-sm">+ <Show+ when={asset()}+ fallback={+ <span class="flex size-full items-center justify-center bg-muted text-muted-foreground">+ <Icon name="media-image" class="size-4" />+ </span>+ }+ >+ <AssetThumbnail+ asset={asset()!}+ class="size-full"+ size={{ width: 50, height: 50 }}+ cache={library()?.cache}+ />+ </Show>+ </span>+ <span class="min-w-0 flex-1 truncate text-xxs">+ {asset() ? assetName(asset()!) : "No media"}+ </span>+ </button>+ <Tooltip>+ <TooltipTrigger+ as={Button}+ size="icon"+ variant="ghost"+ class="text-muted-foreground"+ onClick={handleRemoveMedia}+ >+ <Icon name="close-remove-small" />+ </TooltipTrigger>+ <TooltipContent>Remove media</TooltipContent>+ </Tooltip>+ </div>+ </ControlRow>+ </Show>++ <Show when={hasFill()}>+ <SolidFillRow node={entity()} />+ </Show>+ </PanelSection>++ <Show when={picking()}>+ <FloatingInspector open anchorRef={anchorRef}>+ <FloatingInspectorHeader>+ <FloatingInspectorTitle>Source</FloatingInspectorTitle>+ <div class="ml-auto flex items-center gap-1">+ <FitMenu fill={entity()} />+ <Tooltip>+ <TooltipTrigger+ as={Button}+ size="icon"+ variant="ghost"+ class="text-muted-foreground"+ onClick={() => setPicking(false)}+ >+ <Icon name="close-remove" class="size-6" />+ </TooltipTrigger>+ <TooltipContent>Close</TooltipContent>+ </Tooltip>+ </div>+ </FloatingInspectorHeader>+ <FloatingInspectorContent class="p-0">+ <AssetFillPicker+ node={entity()}+ fill={entity()}+ onSelectAsset={handleSelectAsset}+ />+ </FloatingInspectorContent>+ </FloatingInspector>+ </Show>+ </Show>+ );+}++type SolidFillRowProps = {+ node: Entity;+};++/**+ * The node's intrinsic solid: the `fill` prop, edited in place as a hex the+ * way a solid fill row edits its color. Read from `Computed.color` so it+ * animates; alpha is not offered because the prop ignores it. The X takes+ * the prop off the node.+ */+function SolidFillRow(props: SolidFillRowProps) {+ const editor = useEditor();++ const color = useDerived(() => props.node.get(Computed)?.color ?? 0xE0E0E0);+ const colorText = createMemo(() => colorToHex(color()).replace("#", ""));++ const [draft, setDraft] = createSignal(colorText());+ let cancelCommit = false;++ createEffect(() => {+ setDraft(colorText());+ });++ const commit = () => {+ const next = parseColor(draft());+ if (next === null || next === color()) {+ setDraft(colorText());+ return;+ }+ editor.editProperty(props.node, "fill", colorToHex(next));+ };++ const handleKeyDown = (event: KeyboardEvent & { currentTarget: HTMLInputElement }) => {+ if (event.key === "Enter") {+ event.preventDefault();+ event.currentTarget.blur();+ return;+ }++ if (event.key !== "Escape") return;+ event.preventDefault();+ cancelCommit = true;+ setDraft(colorText());+ event.currentTarget.value = colorText();+ event.currentTarget.blur();+ };++ const handleBlur = (event: FocusEvent & { currentTarget: HTMLInputElement }) => {+ if (cancelCommit) {+ cancelCommit = false;+ event.currentTarget.value = colorText();+ return;+ }++ commit();+ event.currentTarget.value = colorText();+ };++ return (+ <ControlRow label="Solid">+ <div class="flex h-7 w-full items-center overflow-hidden rounded-md border border-transparent bg-input text-foreground focus-within:border-primary">+ <div class="flex h-full min-w-0 flex-1 items-center gap-2 pl-1">+ <span class="size-5 min-w-5 overflow-hidden rounded-sm">+ <OpacitySwatch color={color()} opacity={1} />+ </span>+ <input+ type="text"+ value={draft()}+ class="w-[7ch] bg-transparent text-xxs outline-none"+ onInput={(event) => setDraft(event.currentTarget.value)}+ onKeyDown={handleKeyDown}+ onBlur={handleBlur}+ />+ </div>+ <Tooltip>+ <TooltipTrigger+ as={Button}+ size="icon"+ variant="ghost"+ class="text-muted-foreground"+ onClick={() => editor.editProperty(props.node, "fill", false)}+ >+ <Icon name="close-remove-small" />+ </TooltipTrigger>+ <TooltipContent>Remove fill</TooltipContent>+ </Tooltip>+ </div>+ </ControlRow>+ );+}diff --git a/apps/web/src/engine/editor.ts b/apps/web/src/engine/editor.tsindex ebebcfff..24c16e13 100644--- a/apps/web/src/engine/editor.ts+++ b/apps/web/src/engine/editor.ts@@ -10,7 +10,7 @@* where the commands live.*/-import { Active, Chars, getActiveEntity, getEntityChildren, getEntityTree, getParentEntity, isText, Loop, Selected, Sequential, setActive, Source, Stage } from '@diffusionstudio/runtime';+import { Active, Chars, Computed, End, FrameRate, framesToSeconds, getActiveEntity, getEntityChildren, getEntityTree, getIntrinsicPaint, getParentEntity, getTimelineOrigin, isText, Loop, PaintType, Selected, Sequential, setActive, Size, Source, Stage, Start } from '@diffusionstudio/runtime';import { isAssetRef, isPropValue, serializeAssetRef, SOURCE_ATTR } from '@diffusionstudio/jsx';import { createRoot } from 'solid-js';@@ -166,6 +166,16 @@ function wireProps(props: Record<string, unknown>): Record<string, EditValue> {return wired;}+/**+ * The props of a `<video>`/`<image>` that only mean something while the node+ * plays media, dropped when the intrinsic paint is removed (see+ * `removeIntrinsicPaint`): the source, what qualifies it (its fit, its+ * window, a frames directory's rate, the modifiers put on it), its error,+ * and the audio mix a rect has no track to apply to. `start`/`end` stay:+ * they place the clip, media or not.+ */+const MEDIA_PROPS = ['src', 'error', 'objectFit', 'frameRate', 'sourceIn', 'sourceOut', 'upscale', 'removeBackground', 'addAudio', 'volume', 'muted', 'syncTo'] as const;+/*** What `copy` took: the subtrees as authored, and the source of the parent* they were copied out of (by stamp, so it outlives a remount), which `paste`@@ -593,6 +603,72 @@ export class DocumentEditor {return wrapper;}+ /**+ * Takes a node's intrinsic media off by rewriting the element: a `<video>`+ * or `<image>` becomes the `<rect>` it otherwise was — same box, same+ * timing, same children (fills, strokes, animations) — with the props that+ * only meant something while it played media dropped (see `MEDIA_PROPS`).+ * What the media implied rather than the element authored is pinned so+ * only the picture goes away: the box (a media element defaults to+ * 1920x1080 where a rect is 100x100) and the span (a clip lasts its+ * footage where a rect falls back to the default; the same pin the runtime+ * makes when a geometry loses its paint, see `pinEndToCurrentBounds`).+ * Goes to the file the way the fill picker swaps a fill's kind: the+ * insert of the rect, then the removal of the old element. The selection+ * and the timeline's active pointer move to the rect. Returns the rect,+ * or null when the node has no intrinsic media to remove.+ */+ public removeIntrinsicPaint(node: Entity): Entity | null {+ const intrinsic = getIntrinsicPaint(node);+ if (intrinsic !== PaintType.VIDEO && intrinsic !== PaintType.IMAGE) return null;++ const parent = getParentEntity(node);+ if (!parent) return null;++ // Spelled as its own element, not every iteration's of a loop.+ this.settle(node);+ const tree = this.spell(node);+ if (!tree) return null;++ const props = { ...tree.props };+ for (const name of MEDIA_PROPS) {+ delete props[name];+ }++ const size = node.get(Size);+ if (size) {+ props.width ??= size.width;+ props.height ??= size.height;+ }++ const fps = this.world.get(FrameRate)?.value ?? 30;+ if (props.start === undefined) {+ // A start the source derived (a syncTo correlation) rather than+ // the element authored.+ const start = node.get(Start)?.value ?? 0;+ if (start > 0) props.start = framesToSeconds(start, fps);+ }+ if (props.end === undefined) {+ const end = node.get(End)?.value+ ?? (node.get(Computed)?.end ?? 0) - getTimelineOrigin(node);+ if (end > 0) props.end = framesToSeconds(end, fps);+ }++ const rect: AuthoredTree = { tag: 'rect', props, children: tree.children };++ const wasSelected = node.has(Selected);+ const wasActive = node.has(Active);++ const [next] = this.insertElement(parent, () => renderAuthored(rect), node);+ if (!next) return null;+ this.remove(node);++ if (wasSelected) this.select(next);+ if (wasActive) this.activate(next);++ return next;+ }+/*** Takes a copy of `entities` (the tops of their subtrees, as `duplicate`* would) for `paste`. Nothing is reported: the file only changes when the