The open source AI video agent: turns any coding agent into a video agent, with an editor to change anything it makes.
Your own key's reasoning efforts and services; Claude Code on its own sign-in; Studio title and timeline work (#46)
riverisbuildinge4e67fe86004
diff --git a/.changeset/all-like-it.md b/.changeset/all-like-it.mdnew file mode 100644index 0000000..72d68a6--- /dev/null+++ b/.changeset/all-like-it.md@@ -0,0 +1,6 @@+---+"openfilm": patch+"@openfilm/desktop": patch+---++Studio: a title picked on the picture can be restyled together with every title like it. The inspector's "All like it" sends its color, size, font and the rest to each text of the same tag and classes in every page of the film (its words and place stay its own), in one change and one undo.diff --git a/.changeset/byok-efforts.md b/.changeset/byok-efforts.mdnew file mode 100644index 0000000..cec7b06--- /dev/null+++ b/.changeset/byok-efforts.md@@ -0,0 +1,6 @@+---+"@openfilm/desktop": patch+"openfilm": patch+---++Your own key: each model has the reasoning efforts you give it. In Settings → Agents → Your own key, models are a list: add one by name, tick the efforts it takes (Auto, Off, Minimal, Low, Medium, High, Extra high, Max) and choose the one it starts on. The chat's model menu then offers those efforts for that model, as it does for Claude Code and Codex. Auto sends no reasoning setting and lets the service decide; a new model offers Auto alone, so nothing changes until you choose.diff --git a/.changeset/chat-pills-caret.md b/.changeset/chat-pills-caret.mdnew file mode 100644index 0000000..0fa845d--- /dev/null+++ b/.changeset/chat-pills-caret.md@@ -0,0 +1,6 @@+---+"@openfilm/desktop": patch+"openfilm": patch+---++Desktop: the caret moves freely around references in the message box. The arrow keys step over a reference in one press instead of stopping on it, where typing did nothing. A click between two references puts the caret there, and a click on one goes where it points with the caret left where it was, no longer thrown to the start of the message. References have a little more room around them.diff --git a/.changeset/claude-models.md b/.changeset/claude-models.mdnew file mode 100644index 0000000..a847965--- /dev/null+++ b/.changeset/claude-models.md@@ -0,0 +1,6 @@+---+"@openfilm/desktop": patch+"openfilm": patch+---++Desktop: Claude Code's model menu shows its four current models, as Claude Code's own picker does (the one in use stays too, if it is an older one). With Claude Code both installed on its own and in the Claude app, the later of the two is used, so an install that was never updated no longer brings old model names.diff --git a/.changeset/claude-own-sign-in.md b/.changeset/claude-own-sign-in.mdnew file mode 100644index 0000000..7fb923b--- /dev/null+++ b/.changeset/claude-own-sign-in.md@@ -0,0 +1,6 @@+---+"@openfilm/desktop": patch+"openfilm": patch+---++Desktop: Claude Code runs on your Claude subscription only. An `ANTHROPIC_API_KEY`, `ANTHROPIC_AUTH_TOKEN` or `ANTHROPIC_BASE_URL` in your shell is no longer given to it, so a stale key or gateway can no longer make it look connected and fail every message (a gateway of your own goes in Settings → Agents → Own key). Signing in uses the way Claude Code's adapter offers it (ACP terminal sign-in). Claude Code is connected when you connect it; Codex still is from the start. On Windows, signing in opens a PowerShell window that runs a script, so an install path with a space in it no longer breaks it.diff --git a/.changeset/click-in-typed-line.md b/.changeset/click-in-typed-line.mdnew file mode 100644index 0000000..1f8be02--- /dev/null+++ b/.changeset/click-in-typed-line.md@@ -0,0 +1,6 @@+---+"openfilm": patch+"@openfilm/desktop": patch+---++Studio: while a title is typed in, a click on its words puts the caret there, and a drag selects some of them. Something the page draws over the words unseen (a number still to come in, a grain) or a clip on top took that click, and the edit ended instead. A click anywhere else still keeps the edit.diff --git a/.changeset/double-click-aim.md b/.changeset/double-click-aim.mdnew file mode 100644index 0000000..050a251--- /dev/null+++ b/.changeset/double-click-aim.md@@ -0,0 +1,6 @@+---+"openfilm": patch+"@openfilm/desktop": patch+---++Studio: a double-click on a title types in it even right after a click somewhere else. That click no longer counted as the double-click's first one, so the double-click was aimed at where it had been and nothing was typed in.diff --git a/.changeset/empty-timeline-band.md b/.changeset/empty-timeline-band.mdnew file mode 100644index 0000000..877b601--- /dev/null+++ b/.changeset/empty-timeline-band.md@@ -0,0 +1,6 @@+---+"openfilm": patch+"@openfilm/desktop": patch+---++Studio: an empty timeline shows one wide drop band across the tracks, with the hint at its start.diff --git a/.changeset/inspector-no-flash.md b/.changeset/inspector-no-flash.mdnew file mode 100644index 0000000..e4732f8--- /dev/null+++ b/.changeset/inspector-no-flash.md@@ -0,0 +1,6 @@+---+"openfilm": patch+"@openfilm/desktop": patch+---++Studio: clicking from one layer to another in the picture goes straight from one to the next in the inspector, without showing the film's settings in between. A click on nothing still shows them.diff --git a/.changeset/layers-scale-turn.md b/.changeset/layers-scale-turn.mdnew file mode 100644index 0000000..1685dba--- /dev/null+++ b/.changeset/layers-scale-turn.md@@ -0,0 +1,6 @@+---+"openfilm": patch+"@openfilm/desktop": patch+---++Studio: several layers selected on the picture have handles on their box: the corners scale them together, the sides stretch them, and past a corner they turn about the box's middle (Shift: in steps of 15°). Each layer keeps its place in the group as it scales or turns, and the change is one step to undo.diff --git a/.changeset/main-track-shown.md b/.changeset/main-track-shown.mdnew file mode 100644index 0000000..06d50e3--- /dev/null+++ b/.changeset/main-track-shown.md@@ -0,0 +1,6 @@+---+"openfilm": patch+"@openfilm/desktop": patch+---++Studio: the timeline shows which track is the main track by its look, not by a name. The lowest picture track is drawn a little taller, on a band of its own shade, with the magnet by its badge (hover the head for "Main track"), so it is clear where clips close up when the magnet is on and nothing reads like a track name to rename or delete.diff --git a/.changeset/markers-on-ruler.md b/.changeset/markers-on-ruler.mdnew file mode 100644index 0000000..b84cd6c--- /dev/null+++ b/.changeset/markers-on-ruler.md@@ -0,0 +1,6 @@+---+"openfilm": patch+"@openfilm/desktop": patch+---++Studio: markers are simpler. Add one with the marker button left of split, with M, or by right-clicking the ruler. Markers sit on their own row under the ruler (under the subtitle row when it shows), each a small tab pointing down at its moment with its name beside it. Hover one for its time and a dashed line through the whole timeline; drag it to move it, snapping to clip edges and the playhead with that line shown; click it to go there and open its card (name, color, delete). A click or drag on the row between markers moves the playhead, as in the tracks' empty space. M on a marker that is already there opens its card ready to name it. The old markers list in the toolbar is gone, and a name typed in the card is kept when the card closes.diff --git a/.changeset/menus-preview.md b/.changeset/menus-preview.mdnew file mode 100644index 0000000..ead3f76--- /dev/null+++ b/.changeset/menus-preview.md@@ -0,0 +1,6 @@+---+"openfilm": patch+"@openfilm/desktop": patch+---++Studio: the inspector's menus show a choice on the picture before it is picked, as the font menu does. The font size presets, the weight, the blend mode and the stroke position are shown while hovered or reached with the arrow keys, the selection box following; a click keeps one, leaving the menu takes it back. The arrow keys in a menu (or any inspector field) no longer nudge the layer in hand as well.diff --git a/.changeset/move-selected-clips.md b/.changeset/move-selected-clips.mdnew file mode 100644index 0000000..b24432a--- /dev/null+++ b/.changeset/move-selected-clips.md@@ -0,0 +1,6 @@+---+"openfilm": patch+"@openfilm/desktop": patch+---++Studio: clips selected together and dragged along their track go where they are let go. Dragged further than the gap to the next selected clip, the one in hand counted that clip as one it landed on, went into insert, and the clips jumped back after the clip before them. With the main track magnet on, clips moved or ⌥-copied together by one that is not the first now go in at the insert point by the first of them, instead of cutting the clip before it.diff --git a/.changeset/own-services.md b/.changeset/own-services.mdnew file mode 100644index 0000000..90913c0--- /dev/null+++ b/.changeset/own-services.md@@ -0,0 +1,6 @@+---+"openfilm": patch+"@openfilm/desktop": patch+---++Your own services: in Settings → Providers, add any service you run or use (your own voice, a transcription server on this computer, an API that isn't listed) with its address, its key if it takes one, and what it does. Your agent reaches it with `openfilm get call`: Studio adds the key (it never reaches the agent or the chat), and whatever the service answers comes back as it is, text and JSON for the agent to read, files saved under `assets/<service>/`.diff --git a/.changeset/pill-click-hint.md b/.changeset/pill-click-hint.mdnew file mode 100644index 0000000..9a09d66--- /dev/null+++ b/.changeset/pill-click-hint.md@@ -0,0 +1,6 @@+---+"@openfilm/desktop": patch+"openfilm": patch+---++Desktop: a reference no longer shows a locate icon on hover, since clicking it is what goes there. Its card now says so: "Click to jump to this reference".diff --git a/.changeset/restyle-holds-others.md b/.changeset/restyle-holds-others.mdnew file mode 100644index 0000000..2dbdef7--- /dev/null+++ b/.changeset/restyle-holds-others.md@@ -0,0 +1,6 @@+---+"openfilm": patch+"@openfilm/desktop": patch+---++Studio: restyling a layer no longer moves the others. A page often lines words up in a row (a number and the words beside it); a wider or larger font for the number pushed the words along. Now the layer takes its new size (its selection box grows with a larger font) while margins give the page back the room it gained, written with its style, so the words beside it, under it or lined up with its baseline stay where they are, while the menu previews a font too and when its line height changes. A width or height you set yourself still moves what is around it.diff --git a/.changeset/ruler-refers-time.md b/.changeset/ruler-refers-time.mdnew file mode 100644index 0000000..cf0349b--- /dev/null+++ b/.changeset/ruler-refers-time.md@@ -0,0 +1,6 @@+---+"openfilm": patch+"@openfilm/desktop": patch+---++Desktop: the ruler points at time for the chat. A click on it puts that moment in the message, a drag that span: the moment under the pointer is drawn as a line with a tag of its time and the chat's mark, a drag as its span; the playhead stays where it is (its knob drags it, a click in the tracks' empty space moves it). A clip or a subtitle line referred to the chat no longer shows its time on its pill (it is on its card): a pill shows time only when it is time.diff --git a/.changeset/shared-files-keep-unknown.md b/.changeset/shared-files-keep-unknown.mdnew file mode 100644index 0000000..0806493--- /dev/null+++ b/.changeset/shared-files-keep-unknown.md@@ -0,0 +1,6 @@+---+"openfilm": patch+"@openfilm/desktop": patch+---++The OpenFilm on the web (`npx openfilm`) and the app share their projects list, keys and preferences on this computer. When their versions differ, one no longer drops what only the other knows (a newer version's settings) as it saves.diff --git a/.changeset/text-box-hugs.md b/.changeset/text-box-hugs.mdnew file mode 100644index 0000000..0ee5cc6--- /dev/null+++ b/.changeset/text-box-hugs.md@@ -0,0 +1,6 @@+---+"openfilm": patch+"@openfilm/desktop": patch+---++Studio: a title restyled keeps a box that hugs its words. A heading as wide as the card it sits in kept that width, so a smaller size left its selection box far wider than its words; and the room kept for the words beside it after one change stayed for the next and stretched it further. Now each change works out that room again, and a preview taken back shows exactly what is kept.diff --git a/.changeset/thumbs-show-changes.md b/.changeset/thumbs-show-changes.mdnew file mode 100644index 0000000..58a5af6--- /dev/null+++ b/.changeset/thumbs-show-changes.md@@ -0,0 +1,6 @@+---+"openfilm": patch+"@openfilm/desktop": patch+---++Studio: a page clip's pictures on the timeline show the changes made inside it. A layer moved, restyled or retyped on the picture was not in them, since they were drawn from the page's file alone; now they are drawn with the clip's changes, and drawn again after each one.diff --git a/.changeset/title-own-words.md b/.changeset/title-own-words.mdnew file mode 100644index 0000000..72f95ba--- /dev/null+++ b/.changeset/title-own-words.md@@ -0,0 +1,6 @@+---+"openfilm": patch+"@openfilm/desktop": patch+---++Studio: a title written with its subtitle inside it (`<div>Title<div>subtitle</div></div>`) can be picked in the picture, and only its own words are picked. Its box hugs its words and the inspector shows them; a double-click types them in place while the subtitle stays shown. Moved, scaled, turned or restyled, the title leaves its subtitle where it was and as it looked: the subtitle's own override that keeps it there is written with the title's edit, one undo. The subtitle is still a layer of its own.diff --git a/.changeset/undo-whole-track.md b/.changeset/undo-whole-track.mdnew file mode 100644index 0000000..3ab830a--- /dev/null+++ b/.changeset/undo-whole-track.md@@ -0,0 +1,6 @@+---+"openfilm": patch+"@openfilm/desktop": patch+---++Studio: undoing a delete that emptied a whole track puts that track back as it was. Before, every clip of it came back on a new track of its own, so the timeline looked scrambled; tracks emptied together come back in their order too.diff --git a/.changeset/voice-again-drops-old-subtitles.md b/.changeset/voice-again-drops-old-subtitles.mdnew file mode 100644index 0000000..6fab37e--- /dev/null+++ b/.changeset/voice-again-drops-old-subtitles.md@@ -0,0 +1,6 @@+---+"openfilm": patch+"@openfilm/desktop": patch+---++A voice-over made again under the same name (by a service that doesn't time its words, or one of your own services) no longer keeps the last take's subtitles: they go to the trash, so subtitles are never timed by another take, and your agent is told the new take has none yet.diff --git a/.changeset/windows-no-console-windows.md b/.changeset/windows-no-console-windows.mdnew file mode 100644index 0000000..2fa84ef--- /dev/null+++ b/.changeset/windows-no-console-windows.md@@ -0,0 +1,6 @@+---+"@openfilm/desktop": patch+"openfilm": patch+---++Desktop on Windows: no more command windows flashing up. Checking the agents, reading Codex's plan, talking to Claude Code or Codex and checking ffmpeg ran console programs that each opened a window, and Settings → Agents, which looks again whenever the window gets focus, kept opening them one after another. They now run hidden; only the sign-in window shows.diff --git a/.changeset/words-hold-others.md b/.changeset/words-hold-others.mdnew file mode 100644index 0000000..7b41f7f--- /dev/null+++ b/.changeset/words-hold-others.md@@ -0,0 +1,6 @@+---+"openfilm": patch+"@openfilm/desktop": patch+---++Studio: new words for a title leave the others where they are, as a restyle does. Typed in place or in the inspector, longer or shorter words pushed what sits beside or under them; now the title's box takes the words (hugging them) and keeps its place in the page, as each key is typed too.diff --git a/.changeset/words-under-lines.md b/.changeset/words-under-lines.mdnew file mode 100644index 0000000..0788e02--- /dev/null+++ b/.changeset/words-under-lines.md@@ -0,0 +1,6 @@+---+"openfilm": patch+"@openfilm/desktop": patch+---++Studio: a click on a title takes the title, though a line of a drawing behind it runs nearby. A line was found by the box around it, so a diagonal line in the background took clicks meant for the words beside it, and a double-click there typed nothing.diff --git a/apps/desktop/chat/src/components/ModelPopover.tsx b/apps/desktop/chat/src/components/ModelPopover.tsxindex f6801fe..50163cf 100644--- a/apps/desktop/chat/src/components/ModelPopover.tsx+++ b/apps/desktop/chat/src/components/ModelPopover.tsx@@ -19,7 +19,7 @@ import { Tooltip } from '@/components/Tooltip';import { AgentIcon } from '@/components/AgentIcons';import { POPOVER_CHROME } from '@/components/ComposerCard';import { desktopBridge, type LocalAgentId, type RosterAgent } from '@/lib/desktop-bridge';-import { agentModelSummary, agentOptions, currentAgent, effectivePrefs, offeredOption, optionLabel, optionValue, shownAgents, useLocalAgents } from '@/state/local-agent-store';+import { agentModelSummary, agentOptions, currentAgent, effectivePrefs, offeredChoices, offeredOption, optionLabel, optionValue, shownAgents, useLocalAgents } from '@/state/local-agent-store';import { chatLayer } from '@/lib/chat-layer';/** the card, wider when it lists the agents; the side menu's least width; the gap between them; the window's margin */@@ -155,8 +155,8 @@ export function ModelPopover({ placement = 'bottom', align = 'start' }: { placemconst sideMenu = open && choosing && typeof document !== 'undefined' ? createPortal(<div ref={sideRef} onMouseEnter={() => showSide(choosing.id)} onMouseLeave={hideSideSoon}style={{ ...POPOVER_CHROME, padding: 4, overflowY: 'auto', ...(sideStyle ?? { position: 'fixed', left: -9999, top: 0 }) }}>- {/* the names only: no descriptions, and no "Default" row (a default is one of the others) */}- {(choosing.options ?? []).filter((choice) => choice.value !== 'default').map((choice) => {+ {/* the names only: no descriptions, and the choices the menu offers (offeredChoices) */}+ {offeredChoices(agent, choosing, chosenValue(choosing)).map((choice) => {const selected = chosenValue(choosing) === choice.value;return (<button key={String(choice.value)} type="button" onMouseDown={keepFocus}diff --git a/apps/desktop/chat/src/components/prompt-editor/PromptPill.tsx b/apps/desktop/chat/src/components/prompt-editor/PromptPill.tsxindex dea5523..6adce6c 100644--- a/apps/desktop/chat/src/components/prompt-editor/PromptPill.tsx+++ b/apps/desktop/chat/src/components/prompt-editor/PromptPill.tsx@@ -12,7 +12,7 @@import React from 'react';import {- Blocks, Brackets, Captions, Clock3, Crop, Film, LocateFixed, Mic, MousePointer2, Music, Paperclip,+ Blocks, Brackets, Captions, Clock3, Crop, Film, Mic, MousePointer2, Music, Paperclip,Quote, Rows3, Shapes, SquareDashed, Video, Waves,} from 'lucide-react';import {@@ -233,6 +233,7 @@ const noSubscribe = () => () => {};export function PromptPill({reference,onClick,+ onMouseDown,title,trailing,card,@@ -241,6 +242,8 @@ export function PromptPill({reference: StoredPromptReference;/** Only when clicking leads somewhere; without it the pill is read-only. */onClick?: (event: React.MouseEvent) => void;+ /** A press on it: in the editor, where the caret goes. */+ onMouseDown?: (event: React.MouseEvent) => void;title?: string;/** The trailing slot: the remove button in the editor, nothing in history. */trailing?: React.ReactNode;@@ -265,6 +268,7 @@ export function PromptPill({<spancontentEditable={false}onClick={onClick}+ onMouseDown={onMouseDown}onMouseEnter={hoverCtx ? () => { hovering.current = true; hoverCtx.hover(reference); } : undefined}onMouseLeave={hoverCtx ? () => { hovering.current = false; hoverCtx.hover(null); } : undefined}/* No native title with a card: the system one would appear on top of it a second later */@@ -273,7 +277,7 @@ export function PromptPill({`vertical-align: middle` centers the box on baseline + half the x-height, so thepart below the baseline must fit in the line box too. 1.45em (the pill's own fontsize) is 18.7px at 14px/22px, with room on both sides. */- className={`group/pill mx-[1px] inline-flex h-[1.45em] max-w-[240px] select-none items-center gap-[5px] rounded-[6px] pl-[3px] align-middle text-[0.9em] font-medium leading-none text-[var(--text)] ring-1 ring-inset transition-[background-color,box-shadow] ${+ className={`mx-[1px] inline-flex h-[1.45em] max-w-[240px] select-none items-center gap-[5px] rounded-[6px] pl-[3px] align-middle text-[0.9em] font-medium leading-none text-[var(--text)] ring-1 ring-inset transition-[background-color,box-shadow] ${trailing ? 'pr-[3px]' : 'pr-1.5'} ${onClick ? 'cursor-pointer' : ''}`}style={{@@ -281,7 +285,6 @@ export function PromptPill({? `color-mix(in srgb, ${tone} ${onClick ? 11 : 9}%, var(--surface))`: 'var(--surface-2)',['--tw-ring-color' as string]: tone ? `color-mix(in srgb, ${tone} 26%, transparent)` : 'var(--border)',- ...(tone ? { ['--pill-tone' as string]: tone } : {}),/* Lit from elsewhere: a solid border (over the faint ring) and a deeper fill */...(lit ? {background: tone ? `color-mix(in srgb, ${tone} 24%, var(--surface))` : 'var(--bg-hover)',@@ -312,14 +315,6 @@ export function PromptPill({{reference.detail ? (<span className="shrink-0 font-normal tabular-nums text-[var(--text-faint)]">{reference.detail}</span>) : null}- {onClick ? (- /* Clickable pills show a locate icon on hover: clicking takes you there. */- <LocateFixed- size={10}- aria-hidden- className="-ml-0.5 hidden shrink-0 text-[var(--pill-tone,var(--text-muted))] group-hover/pill:block"- />- ) : null}{trailing}</span>);diff --git a/apps/desktop/chat/src/components/prompt-editor/PromptReferenceNode.tsx b/apps/desktop/chat/src/components/prompt-editor/PromptReferenceNode.tsxindex 8fc3327..c55887a 100644--- a/apps/desktop/chat/src/components/prompt-editor/PromptReferenceNode.tsx+++ b/apps/desktop/chat/src/components/prompt-editor/PromptReferenceNode.tsx@@ -20,8 +20,9 @@ type SerializedPromptReferenceNode = Spread<>;/**- * A reference node: the caret cannot enter it, and one Backspace deletes it whole. It is- * one indivisible thing in the document, not a few characters.+ * A reference node: the caret cannot enter it, an arrow key steps over it, and one+ * Backspace deletes it whole. It is one indivisible thing in the document, not a few+ * characters.*/export class PromptReferenceNode extends DecoratorNode<React.JSX.Element> {__reference: PromptReference;@@ -51,6 +52,9 @@ export class PromptReferenceNode extends DecoratorNode<React.JSX.Element> {const dom = document.createElement('span');dom.style.display = 'inline-flex';dom.style.verticalAlign = 'middle';+ /* Room on each side, outside this box: a click there is in the text (the caret goes+ beside the pill), where a click on the box itself puts the caret at the start. */+ dom.style.margin = '0 2px';return dom;}@@ -62,8 +66,10 @@ export class PromptReferenceNode extends DecoratorNode<React.JSX.Element> {return true;}- isKeyboardSelectable(): true {- return true;+ /* Not selectable by the arrow keys: selected, a pill shows nothing and swallows what is+ typed next. The caret goes past it instead, to the text on its other side. */+ isKeyboardSelectable(): false {+ return false;}/* As plain text it is a marker, so the document's getTextContent() is the string the@@ -122,6 +128,11 @@ function PromptReferencePill({<PromptPillreference={reference}card={handlers?.describe?.(reference) ?? null}+ /* A click on the pill goes where it points; the caret stays where it was. Left to the+ browser, a press on something not editable puts the caret at the start of the text. */+ onMouseDown={(event: React.MouseEvent) => {+ if (event.button === 0) event.preventDefault();+ }}{...(reveal && handlers?.revealLabel ? { hint: handlers.revealLabel } : {})}{...(reveal ? {onClick: (event: React.MouseEvent) => {diff --git a/apps/desktop/chat/src/i18n/locales/en-US.ts b/apps/desktop/chat/src/i18n/locales/en-US.tsindex d3f1d79..b1798d2 100644--- a/apps/desktop/chat/src/i18n/locales/en-US.ts+++ b/apps/desktop/chat/src/i18n/locales/en-US.ts@@ -146,7 +146,7 @@ export const enUS = {},library: {removeReference: 'Remove reference',- revealReference: 'Jump to this reference',+ revealReference: 'Click to jump to this reference',},mention: {currentRange: 'Current range',diff --git a/apps/desktop/chat/src/i18n/locales/es.ts b/apps/desktop/chat/src/i18n/locales/es.tsindex f8e0526..56539ea 100644--- a/apps/desktop/chat/src/i18n/locales/es.ts+++ b/apps/desktop/chat/src/i18n/locales/es.ts@@ -147,7 +147,7 @@ export const es: Words = {},library: {removeReference: 'Quitar referencia',- revealReference: 'Ir a esta referencia',+ revealReference: 'Haz clic para ir a esta referencia',},mention: {currentRange: 'Rango actual',diff --git a/apps/desktop/chat/src/i18n/locales/ja.ts b/apps/desktop/chat/src/i18n/locales/ja.tsindex bb234bd..ad3a046 100644--- a/apps/desktop/chat/src/i18n/locales/ja.ts+++ b/apps/desktop/chat/src/i18n/locales/ja.ts@@ -147,7 +147,7 @@ export const ja: Words = {},library: {removeReference: '参照を削除',- revealReference: 'この参照へ移動',+ revealReference: 'クリックでこの参照へ移動',},mention: {currentRange: '現在の範囲',diff --git a/apps/desktop/chat/src/i18n/locales/ko.ts b/apps/desktop/chat/src/i18n/locales/ko.tsindex 681dd68..e44f5dd 100644--- a/apps/desktop/chat/src/i18n/locales/ko.ts+++ b/apps/desktop/chat/src/i18n/locales/ko.ts@@ -147,7 +147,7 @@ export const ko: Words = {},library: {removeReference: '참조 제거',- revealReference: '이 참조로 이동',+ revealReference: '클릭하면 이 참조로 이동',},mention: {currentRange: '현재 구간',diff --git a/apps/desktop/chat/src/i18n/locales/zh-CN.ts b/apps/desktop/chat/src/i18n/locales/zh-CN.tsindex a4f4105..6e59fce 100644--- a/apps/desktop/chat/src/i18n/locales/zh-CN.ts+++ b/apps/desktop/chat/src/i18n/locales/zh-CN.ts@@ -147,7 +147,7 @@ export const zhCN: Words = {},library: {removeReference: '移除引用',- revealReference: '跳转到此引用',+ revealReference: '点击跳转到此引用',},mention: {currentRange: '当前区间',diff --git a/apps/desktop/chat/src/lib/desktop-bridge.ts b/apps/desktop/chat/src/lib/desktop-bridge.tsindex b3d3d18..446c136 100644--- a/apps/desktop/chat/src/lib/desktop-bridge.ts+++ b/apps/desktop/chat/src/lib/desktop-bridge.ts@@ -24,7 +24,11 @@ export interface RosterAgent {install?: string | null;setupRequired?: string | null;error?: string;- byok?: { format: 'responses' | 'chat' | 'anthropic'; baseUrl: string; model: string; models?: string[]; last4: string };+ byok?: {+ format: 'responses' | 'chat' | 'anthropic'; baseUrl: string; model: string; models?: string[]; last4: string;+ /** per model, the reasoning efforts it offers and the one it starts on (apps/desktop src/pi-config.mjs EFFORTS) */+ efforts?: Record<string, { levels: string[]; effort: string }>;+ };}export interface LocalAgentState {diff --git a/apps/desktop/chat/src/state/local-agent-store.ts b/apps/desktop/chat/src/state/local-agent-store.tsindex 59eace9..69ee1b6 100644--- a/apps/desktop/chat/src/state/local-agent-store.ts+++ b/apps/desktop/chat/src/state/local-agent-store.ts@@ -84,24 +84,46 @@ function byokModels(roster: RosterAgent[] | null): string[] {return byok.models?.length ? byok.models : [byok.model].filter(Boolean);}+const EFFORT_NAMES: Record<string, string> = { auto: 'Auto', off: 'Off', minimal: 'Minimal', low: 'Low', medium: 'Medium', high: 'High', xhigh: 'Extra high', max: 'Max' };+/**- * An agent's option list, for the menu. The own key's is its models, from Settings: no session has to start to know- * them (the app's own agent reports the same `model` option, apps/desktop/src/pi-config.mjs modelOption). The person's- * own agents' is the one a session last reported.+ * An agent's option list, for the menu. The own key's is its models, and the efforts of the one picked when it offers+ * more than one, from Settings: no session has to start to know them (the app's own agent reports the same `model` and+ * `effort` options, apps/desktop/src/pi-config.mjs). The person's own agents' is the one a session last reported.*/-export function agentOptions(state: Pick<LocalAgentStore, 'options' | 'roster'>, agent: LocalAgentId): ConfigOptions | undefined {+export function agentOptions(state: Pick<LocalAgentStore, 'options' | 'roster'> & Partial<Pick<LocalAgentStore, 'prefs'>>, agent: LocalAgentId): ConfigOptions | undefined {if (agent !== 'byok') return state.options[agent];const models = byokModels(state.roster);if (!models.length) return undefined;- return [{ id: 'model', name: 'Model', category: 'model', type: 'select', currentValue: models[0], options: models.map((m) => ({ value: m, name: m })) }];+ const picked = String(state.prefs?.byok?.model ?? '');+ const model = models.includes(picked) ? picked : models[0]!;+ const options: ConfigOptions = [{ id: 'model', name: 'Model', category: 'model', type: 'select', currentValue: model, options: models.map((m) => ({ value: m, name: m })) }];+ const effort = state.roster?.find((a) => a.id === 'byok')?.byok?.efforts?.[model];+ if (effort && effort.levels.length > 1) {+ options.push({ id: 'effort', name: 'Effort', category: 'thought_level', type: 'select', currentValue: effort.effort,+ options: effort.levels.map((l) => ({ value: l, name: EFFORT_NAMES[l] ?? l })) });+ }+ return options;}-/** Which of an agent's options the menu offers. The app's own agent offers its models, and no OpenAI fast mode. */-export function offeredOption(agent: LocalAgentId, roster: RosterAgent[] | null, option: { id: string; category?: string; name: string }): boolean {+/** Which of an agent's options the menu offers. The app's own agent offers its models and their efforts, and no OpenAI fast mode. */+export function offeredOption(agent: LocalAgentId, _roster: RosterAgent[] | null, option: { id: string; category?: string; name: string }): boolean {if (agent !== 'byok') return true;- if (/^(fast|fast-mode|speed)$/i.test(option.id) || option.category === 'speed') return false;- if (option.category === 'thought_level') return roster?.find((a) => a.id === 'byok')?.byok?.format !== 'chat';- return true;+ return !(/^(fast|fast-mode|speed)$/i.test(option.id) || option.category === 'speed');+}++/* Claude Code's own model picker shows four above its line, the newest of each family; the ones it lists after them+ are older models */+const CLAUDE_MODELS_OFFERED = 4;++/**+ * The choices a menu row offers, in the agent's order, without a "Default" row (a default is one of the others). Of+ * Claude Code's models, the first four it lists, as its own picker shows them, and the one in use if it is further down.+ */+export function offeredChoices<T extends { value: unknown }>(agent: LocalAgentId, option: { id: string; category?: string; options?: T[] }, chosen: unknown): T[] {+ const choices = (option.options ?? []).filter((choice) => choice.value !== 'default');+ if (agent !== 'claude' || (option.category !== 'model' && option.id !== 'model')) return choices;+ return choices.filter((choice, i) => i < CLAUDE_MODELS_OFFERED || choice.value === chosen);}/** The agents the menu lists: the ones switched on in Settings, in the roster's order. */diff --git a/apps/desktop/chat/src/studio-refs.ts b/apps/desktop/chat/src/studio-refs.tsindex 6f00381..58b5818 100644--- a/apps/desktop/chat/src/studio-refs.ts+++ b/apps/desktop/chat/src/studio-refs.ts@@ -55,8 +55,9 @@ export function refPill(id: string | undefined, ref: StudioRef): PromptReferenceconst picture = ref.image?.src ? { image: { ...ref.image, src: ref.image.src } } : {};const base = { id: id ?? idOf(ref), target: targetOf(ref), ...picture };switch (ref.kind) {- case 'clip': return { ...base, kind: CLIP_KIND[ref.clipKind] ?? 'clipMg', label: ref.id ?? ref.label, detail: `${clock(ref.start)}–${clock(ref.end)}` };- /* a thing in the picture is named by what it is; its moment is on the card */+ /* a thing on the timeline or in the picture is named by what it is; its span or moment is on the card. A pill shows+ time only when it is time (a moment or a span picked on the ruler) */+ case 'clip': return { ...base, kind: CLIP_KIND[ref.clipKind] ?? 'clipMg', label: ref.id ?? ref.label };case 'layer': return { ...base, kind: 'element', label: ref.text ? words(ref.text, 40) : ref.label };case 'region': return { ...base, kind: 'region', label: ref.texts[0] ? words(ref.texts[0], 40) : ref.clipIds.join(', ') || `${Math.round(ref.box.w)}×${Math.round(ref.box.h)}` };case 'time': return { ...base, kind: 'time', label: clock(ref.time) };@@ -68,7 +69,7 @@ export function refPill(id: string | undefined, ref: StudioRef): PromptReferencelabel: ref.path.split('/').pop() || ref.path,...(ref.w && ref.h ? { detail: `${ref.w}×${ref.h}` } : ref.duration ? { detail: clock(ref.duration) } : {}),};- case 'subtitle': return { ...base, kind: 'subtitle', label: clipped(ref.text, 40) || span(ref.start, ref.end), detail: span(ref.start, ref.end) };+ case 'subtitle': return { ...base, kind: 'subtitle', label: clipped(ref.text, 40) || span(ref.start, ref.end) };}}diff --git a/apps/desktop/src/agent-installation.mjs b/apps/desktop/src/agent-installation.mjsindex 21e07b2..fc97187 100644--- a/apps/desktop/src/agent-installation.mjs+++ b/apps/desktop/src/agent-installation.mjs@@ -5,7 +5,7 @@* cmd.exe (agents.mjs). A program the person chose themselves (Settings → Agents → Locate) comes before any of these.* Reads no Electron module, so `node --test` can use it.*/-import { accessSync, closeSync, constants, lstatSync, openSync, readFileSync, readSync, readdirSync, statSync } from 'node:fs';+import { accessSync, closeSync, constants, lstatSync, openSync, readFileSync, readSync, readdirSync, realpathSync, statSync } from 'node:fs';import { homedir } from 'node:os';import { delimiter, dirname, isAbsolute, join } from 'node:path';@@ -84,6 +84,33 @@ export function claudeAppCli(home = homedir()) {} catch { return null; /* the Claude app has not downloaded Claude Code */ }}+/**+ * Which Claude Code a program is, told from where it is installed without starting it: the Claude app's and Homebrew's+ * folders and the official installer's file are named for the version (`claude-code/2.1.295/…`,+ * `Caskroom/claude-code/2.1.295/…`, `claude/versions/2.1.295`); npm's has its package.json. Null when it can't be told.+ */+export function claudeVersion(file) {+ let real;+ try { real = realpathSync(file); } catch { return null; }+ /* npm's first: its folders can be named for Node's version (Homebrew's Cellar/node/26.0.0/lib/node_modules/…) */+ for (let dir = dirname(real), up = 0; up < 3; dir = dirname(dir), up += 1) {+ try {+ const pkg = JSON.parse(readFileSync(join(dir, 'package.json'), 'utf8'));+ if (pkg.name === '@anthropic-ai/claude-code' && typeof pkg.version === 'string') return pkg.version;+ } catch { /* not here */ }+ }+ return real.split(/[\\/]/).reverse().find((part) => /^\d+\.\d+\.\d+$/.test(part)) ?? null;+}++/** Whether version `a` is later than `b` (dotted numbers); false when either is unknown. */+export function laterVersion(a, b) {+ if (!a || !b) return false;+ const x = a.split('.').map(Number);+ const y = b.split('.').map(Number);+ for (let i = 0; i < Math.max(x.length, y.length); i += 1) if ((x[i] ?? 0) !== (y[i] ?? 0)) return (x[i] ?? 0) > (y[i] ?? 0);+ return false;+}+const windows = process.platform === 'win32';const valueOf = (env, name) => Object.entries(env ?? {}).find(([k]) => k.toLowerCase() === name.toLowerCase())?.[1] || undefined;@@ -237,7 +264,13 @@ export function detectAgentInstallation(id, { home = homedir(), env = process.enconst dirs = [...pathDirs, ...(program.dirs?.(home) ?? [])];let cli = null;for (const name of program.names) if (!cli) cli = findExecutable(name, dirs, env, { win });- if (cli) return { installed: true, cli, source: 'path', install };+ if (cli) {+ /* Claude Code both on the PATH (an npm install that is never updated, say) and in the Claude app, which keeps its+ own up to date: the later one, whose models are the current ones. One whose version can't be told stays. */+ const inApp = id === 'claude' ? claudeAppCli(home) : null;+ if (inApp && laterVersion(claudeVersion(inApp), claudeVersion(cli))) return { installed: true, cli: inApp, source: 'app', install };+ return { installed: true, cli, source: 'path', install };+ }if (id === 'codex') {cli = applicationDirs.flatMap((dir) => ['Codex.app', 'ChatGPT.app'].flatMap((app) => CODEX_IN_APP.map((p) => join(dir, app, p)))).find(executableFile) ?? null;}diff --git a/apps/desktop/src/agent-installation.test.mjs b/apps/desktop/src/agent-installation.test.mjsindex 0f49d9b..84e7859 100644--- a/apps/desktop/src/agent-installation.test.mjs+++ b/apps/desktop/src/agent-installation.test.mjs@@ -1,9 +1,9 @@import { test } from 'node:test';import assert from 'node:assert/strict';-import { mkdirSync, mkdtempSync, writeFileSync } from 'node:fs';+import { mkdirSync, mkdtempSync, symlinkSync, writeFileSync } from 'node:fs';import { tmpdir } from 'node:os';import { join } from 'node:path';-import { chosenProgram, claudeAppCli, cmdTarget, consoleProgram, detectAgentInstallation, findExecutable, searchReport, windowsBinDirs } from './agent-installation.mjs';+import { chosenProgram, claudeAppCli, claudeVersion, cmdTarget, consoleProgram, detectAgentInstallation, findExecutable, laterVersion, searchReport, windowsBinDirs } from './agent-installation.mjs';test("an npm .cmd shim (Windows) runs its package's script: that is what gets started", () => {const npm = mkdtempSync(join(tmpdir(), 'of-npm-'));@@ -191,3 +191,37 @@ test('the report: every folder searched, each file with the name, what a shim ru{ file: join(npm, 'gemini.ps1'), runs: null, usable: false },]);});++test('Claude Code on the PATH and in the Claude app: the later one is used, told from where each is installed', () => {+ if (process.platform === 'win32') return;+ const home = mkdtempSync(join(tmpdir(), 'of-home-'));+ const exe = (file) => { mkdirSync(join(file, '..'), { recursive: true }); writeFileSync(file, '', { mode: 0o755 }); return file; };+ const inApp = exe(join(home, 'Library/Application Support/Claude/claude-code/2.1.295/abc/claude.app/Contents/MacOS/claude'));+ assert.equal(claudeVersion(inApp), '2.1.295');++ /* an npm install never updated, under a Node whose folder is named for its own version */+ const npm = join(home, 'Cellar/node/26.0.0/lib/node_modules/@anthropic-ai/claude-code');+ const cliJs = exe(join(npm, 'cli.js'));+ writeFileSync(join(npm, 'package.json'), JSON.stringify({ name: '@anthropic-ai/claude-code', version: '2.1.280' }));+ const bin = join(home, 'bin');+ mkdirSync(bin);+ symlinkSync(cliJs, join(bin, 'claude'));+ assert.equal(claudeVersion(join(bin, 'claude')), '2.1.280', "npm's own version, not Node's");+ const found = detectAgentInstallation('claude', { home, env: { PATH: '' }, pathDirs: [bin] });+ assert.deepEqual([found.cli, found.source], [inApp, 'app']);++ /* the official installer's, later than the app's: the one on the PATH */+ const native = exe(join(home, '.local/share/claude/versions/2.1.300'));+ const local = join(home, 'local-bin');+ mkdirSync(local);+ symlinkSync(native, join(local, 'claude'));+ assert.equal(detectAgentInstallation('claude', { home, env: { PATH: '' }, pathDirs: [local] }).cli, join(local, 'claude'));++ /* one whose version can't be told stays as it is found */+ const plain = join(home, 'plain-bin');+ exe(join(plain, 'claude'));+ assert.equal(claudeVersion(join(plain, 'claude')), null);+ assert.equal(detectAgentInstallation('claude', { home, env: { PATH: '' }, pathDirs: [plain] }).source, 'path');+ assert.equal(laterVersion('2.1.10', '2.1.9'), true);+ assert.equal(laterVersion('2.1.9', '2.1.9'), false);+});diff --git a/apps/desktop/src/agent-roster.mjs b/apps/desktop/src/agent-roster.mjsindex 77ecab2..04e3209 100644--- a/apps/desktop/src/agent-roster.mjs+++ b/apps/desktop/src/agent-roster.mjs@@ -6,11 +6,13 @@* account signed in on the computer as it is: connecting opens Terminal for its own sign-in command only when it* is signed out, disconnecting only stops OpenFilm using it, and connecting again uses whichever account is signed* in then (one switched in the agent or its maker's app). `account` says whose it is. Codex and Claude Code are- * shown in the chat's menu from the start; the others once the person connects one or switches it on.+ * shown in the chat's menu from the start; the others once the person connects one or switches it on. Codex is+ * connected from the start, Claude Code once the person connects it.* · byok — the app's own agent (Pi, pi-agent.mjs) on the person's own key, on a standard API in one of three formats: the OpenAI* Responses API (…/v1/responses), the OpenAI Chat Completions API (…/v1/chat/completions) or the Anthropic Messages* API (…/v1/messages); Pi speaks each. It may name several models (the chat's menu switches between them, the first- * one to begin with). Each has to read pictures (the agent looks at its own frames): setting it up sends one small+ * one to begin with), each with the reasoning efforts the person says it takes and the one it starts on (`efforts`;+ * `auto` alone, sending no reasoning setting, when they said nothing). Each has to read pictures (the agent looks at its own frames): setting it up sends one small* request with a picture to each, and a model that cannot take it is refused.** One of the person's own agents not found where it is looked for can be pointed at (`locate`): the program they choose@@ -27,7 +29,7 @@*/import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';import { join } from 'node:path';-import { PI_APIS } from './pi-config.mjs';+import { PI_APIS, modelEfforts } from './pi-config.mjs';/** The person's own agents, as agents.mjs AGENTS has them. */export const OWN_AGENT_IDS = Object.freeze(['codex', 'claude', 'gemini', 'copilot', 'cursor', 'opencode', 'codebuddy', 'qwen', 'kimi']);@@ -47,6 +49,9 @@ export const BYOK_FORMATS = Object.freeze({/* the person's other agents: not shown until connected or switched on */const DEFAULT_SHOWN = { codex: true, claude: true, byok: false };+/* Codex is used as it is signed in on the computer from the start; Claude Code only once the person connects it, which+ signs in to their Claude subscription the first time */+const DEFAULT_DISCONNECTED = Object.freeze(['claude']);/** How long a sign-in in Terminal is waited for, asked about every few seconds. */const SIGN_IN_WAIT_MS = 5 * 60_000;const SIGN_IN_POLL_MS = 3000;@@ -71,7 +76,7 @@ export function readChoices(dataDir) {for (const id of OWN_AGENT_IDS) if (typeof raw.paths?.[id] === 'string' && raw.paths[id]) paths[id] = raw.paths[id];return {shown,- disconnected: Array.isArray(raw.disconnected) ? raw.disconnected.filter((id) => AGENT_IDS.includes(id)) : [],+ disconnected: Array.isArray(raw.disconnected) ? raw.disconnected.filter((id) => AGENT_IDS.includes(id)) : [...DEFAULT_DISCONNECTED],byok,paths,};@@ -80,7 +85,7 @@ export function readChoices(dataDir) {/** The own key's choice as kept: its models (one kept before there could be several is the one), the first the default. */function byokChoice(raw) {const models = modelList(Array.isArray(raw.models) && raw.models.length ? raw.models : [raw.model]);- return { format: raw.format, baseUrl: String(raw.baseUrl ?? ''), model: models[0] ?? '', models, last4: String(raw.last4 ?? '') };+ return { format: raw.format, baseUrl: String(raw.baseUrl ?? ''), model: models[0] ?? '', models, efforts: modelEfforts(raw.efforts, models), last4: String(raw.last4 ?? '') };}/** Model names, from a list or text (separated by commas or new lines): trimmed, each once, in order. */@@ -196,7 +201,7 @@ export function createRoster(deps) {shown: choices.shown[id] ?? false,...stateOf(id),...(live.errors[id] ? { error: live.errors[id] } : {}),- ...(id === 'byok' && choices.byok ? { byok: { ...choices.byok, models: [...choices.byok.models] } } : {}),+ ...(id === 'byok' && choices.byok ? { byok: { ...choices.byok, models: [...choices.byok.models], efforts: modelEfforts(choices.byok.efforts, choices.byok.models) } } : {}),}));}@@ -308,8 +313,9 @@ export function createRoster(deps) {/*** The person's own key: checked (key accepted, each model reads pictures) before it is kept. A key left empty keeps* the one kept already (the models or the address changed). `models`: a list, or text with the names separated by- * commas or new lines (`model`, one name, as before).- * @param {{ format: string, baseUrl?: string, models?: string[] | string, model?: string, key?: string }} config+ * commas or new lines (`model`, one name, as before). `efforts`: per model, the efforts it takes and the one it+ * starts on (pi-config.mjs EFFORTS; `auto` alone for a model not named there).+ * @param {{ format: string, baseUrl?: string, models?: string[] | string, model?: string, efforts?: Record<string, { levels?: string[], effort?: string }>, key?: string }} config*/async configureByok(config) {const format = BYOK_FORMATS[config?.format];@@ -330,7 +336,7 @@ export function createRoster(deps) {...choices,shown: { ...choices.shown, byok: true },disconnected: choices.disconnected.filter((a) => a !== 'byok'),- byok: { format: config.format, baseUrl, model: models[0], models, last4: key.slice(-4) },+ byok: { format: config.format, baseUrl, model: models[0], models, efforts: modelEfforts(config.efforts, models), last4: key.slice(-4) },});deps.closeSessions('byok');changed();@@ -346,9 +352,9 @@ export function createRoster(deps) {if (id !== 'byok') return null;const key = byokKey();if (!choices.byok || !key) return null;- const { format, baseUrl, model, models } = choices.byok;+ const { format, baseUrl, model, models, efforts } = choices.byok;const provider = { id: 'byok', api: PI_APIS[format], baseUrl, apiKeyEnv: 'OPENFILM_BYOK_KEY' };- return { home: join(dataDir, 'agents', 'byok', 'pi'), provider, model, models: [...models], env: { OPENFILM_BYOK_KEY: key } };+ return { home: join(dataDir, 'agents', 'byok', 'pi'), provider, model, models: [...models], efforts: modelEfforts(efforts, models), env: { OPENFILM_BYOK_KEY: key } };},};}diff --git a/apps/desktop/src/agent-roster.test.mjs b/apps/desktop/src/agent-roster.test.mjsindex 03b53b6..fcd5e76 100644--- a/apps/desktop/src/agent-roster.test.mjs+++ b/apps/desktop/src/agent-roster.test.mjs@@ -43,7 +43,7 @@ test('a key is kept only once the service took it with a picture; a model that cassert.match((await r.configureByok({ format: 'chat', baseUrl: 'https://api.deepseek.com/v1', model: 'm', key: 'bad' })).error, /did not accept/);answer = { status: 200, body: { choices: [] } };assert.deepEqual(await r.configureByok({ format: 'chat', baseUrl: 'https://api.deepseek.com/v1/', model: 'vision', key: 'sk-good-9876' }), { ok: true });- assert.deepEqual(readChoices(dir).byok, { format: 'chat', baseUrl: 'https://api.deepseek.com/v1', model: 'vision', models: ['vision'], last4: '9876' });+ assert.deepEqual(readChoices(dir).byok, { format: 'chat', baseUrl: 'https://api.deepseek.com/v1', model: 'vision', models: ['vision'], efforts: { vision: { levels: ['auto'], effort: 'auto' } }, last4: '9876' });assert.equal(readChoices(dir).shown.byok, true, 'set up: shown in the chat');assert.equal(readFileSync(join(dir, 'byok.key'), 'utf8'), 'sealed:sk-good-9876', 'the key sealed');assert.equal(JSON.stringify(readChoices(dir)).includes('sk-good'), false, 'never in the choices file');@@ -98,10 +98,21 @@ test('a key may name several models: each is asked with a picture, the first isassert.deepEqual(r.list().find((a) => a.id === 'byok').byok.models, ['vision', 'big']);});+test('each model keeps the efforts the person gave it and the one it starts on; one given none offers Auto alone', async () => {+ const dir = mkdtempSync(join(tmpdir(), 'of-roster-'));+ const r = roster(dir, async () => new Response('{}', { status: 200 }));+ assert.deepEqual(await r.configureByok({ format: 'responses', models: ['gpt-a', 'mine'], key: 'sk-1234',+ efforts: { 'gpt-a': { levels: ['high', 'auto', 'medium', 'turbo'], effort: 'medium' }, mine: { levels: [], effort: 'high' }, gone: { levels: ['max'] } } }), { ok: true });+ const efforts = { 'gpt-a': { levels: ['auto', 'medium', 'high'], effort: 'medium' }, mine: { levels: ['auto'], effort: 'auto' } };+ assert.deepEqual(readChoices(dir).byok.efforts, efforts, 'known efforts in order; nothing for a model not named');+ assert.deepEqual(r.harness('byok').efforts, efforts);+ assert.deepEqual(r.list().find((a) => a.id === 'byok').byok.efforts, efforts);+});+test('a key kept with one model, before there could be several, reads as that one', () => {const dir = mkdtempSync(join(tmpdir(), 'of-roster-'));writeFileSync(join(dir, 'agents.json'), JSON.stringify({ byok: { format: 'responses', baseUrl: 'https://api.openai.com/v1', model: 'gpt-x', last4: 'abcd' } }));- assert.deepEqual(readChoices(dir).byok, { format: 'responses', baseUrl: 'https://api.openai.com/v1', model: 'gpt-x', models: ['gpt-x'], last4: 'abcd' });+ assert.deepEqual(readChoices(dir).byok, { format: 'responses', baseUrl: 'https://api.openai.com/v1', model: 'gpt-x', models: ['gpt-x'], efforts: { 'gpt-x': { levels: ['auto'], effort: 'auto' } }, last4: 'abcd' });});test("Codex and Claude Code are shown in the chat from the start, the person's other agents once connected or switched on", async () => {@@ -167,6 +178,8 @@ test('disconnecting only stops OpenFilm using an agent; connecting again uses thcloseSessions: (id) => closed.push(id),});await r.refresh();+ assert.equal(r.list().find((a) => a.id === 'claude').status, 'off', 'Claude Code waits for the person to connect it');+ assert.deepEqual(await r.connect('claude'), { ok: true });assert.deepEqual(r.list().find((a) => a.id === 'claude').account, { method: 'claude.ai', email: '[email protected]' });r.disconnect('claude');assert.equal(r.list().find((a) => a.id === 'claude').status, 'off');@@ -176,5 +189,12 @@ test('disconnecting only stops OpenFilm using an agent; connecting again uses thconst claude = r.list().find((a) => a.id === 'claude');assert.equal(claude.status, 'ready');assert.equal(claude.account.email, '[email protected]');- assert.deepEqual(closed, ['claude', 'claude'], 'its sessions closed both times: none goes on with the old account');+ assert.deepEqual(closed, ['claude', 'claude', 'claude'], 'its sessions closed each time: none goes on with the old account');+});++test('Codex is connected from the start, Claude Code once the person connects it; a choice kept stays as it is', () => {+ const dir = mkdtempSync(join(tmpdir(), 'of-roster-'));+ assert.deepEqual(readChoices(dir).disconnected, ['claude']);+ writeFileSync(join(dir, 'agents.json'), JSON.stringify({ disconnected: [] }));+ assert.deepEqual(readChoices(dir).disconnected, [], 'connected before: still connected');});diff --git a/apps/desktop/src/agent-usage.mjs b/apps/desktop/src/agent-usage.mjsindex 9ea2d47..7409e60 100644--- a/apps/desktop/src/agent-usage.mjs+++ b/apps/desktop/src/agent-usage.mjs@@ -61,7 +61,7 @@ const within = (p, ms) => Promise.race([p, new Promise((done) => setTimeout(() =/** Codex's usage, on OpenFilm's sign-in: its app server asked twice, then stopped. @param {string} cli */async function codexUsage(cli) {const shell = await loginShellVariables();- const child = spawn(cli, ['app-server'], { env: childEnvironment({ shell }), stdio: ['pipe', 'pipe', 'ignore'] });+ const child = spawn(cli, ['app-server'], { env: childEnvironment({ shell }), stdio: ['pipe', 'pipe', 'ignore'], windowsHide: true });child.once('error', () => {});/** @type {Map<number, (message: any) => void>} */const waiting = new Map();diff --git a/apps/desktop/src/agents.mjs b/apps/desktop/src/agents.mjsindex a4a2b11..89b980f 100644--- a/apps/desktop/src/agents.mjs+++ b/apps/desktop/src/agents.mjs@@ -24,14 +24,14 @@* Reads no Electron module, so `node --test` can use it.*/import { execFile, spawn } from 'node:child_process';-import { existsSync, mkdtempSync, readFileSync } from 'node:fs';+import { existsSync, mkdirSync, mkdtempSync, readFileSync, writeFileSync } from 'node:fs';import { arch, homedir, release, tmpdir } from 'node:os';import { createRequire } from 'node:module';import { dirname, isAbsolute, join } from 'node:path';import { Readable, Writable } from 'node:stream';import { fileURLToPath } from 'node:url';import * as acp from '@agentclientprotocol/sdk';-import { PATH_SEP, childEnvironment, lastPathSource, loginShellEnvironment, loginShellVariables } from './environment.mjs';+import { PATH_SEP, childEnvironment, lastPathSource, loginShellEnvironment, loginShellVariables, shellSubset } from './environment.mjs';import { AGENT_PROGRAMS, HOME_BIN_CANDIDATES, chosenProgram, consoleProgram, detectAgentInstallation, searchReport } from './agent-installation.mjs';import { nodeBinary } from './node-runtime.mjs';import { outOfBalance } from './pi-config.mjs';@@ -54,6 +54,11 @@ export const AGENTS = Object.freeze({env: (path) => ({ CLAUDE_CODE_EXECUTABLE: path }),auth: ['auth', 'status'],login: ['auth', 'login'],+ /* of the shell only its PATH, proxies, config folder and locale, as 0.1.5 had it and ChatCut has it: a key or+ gateway set there (ANTHROPIC_*) would take the place of the person's Claude subscription */+ fromShell: shellSubset,+ /* signed in to the person's Claude subscription, the way its adapter offers it (ACP terminal auth), in Terminal */+ acpSignIn: 'claude-ai-login',systemPrompt: true,},codex: {@@ -313,7 +318,7 @@ export async function agentStatus(id, cliPath) {if (!spec || !cliPath) return { signedIn: null, account: null };if (!spec.auth) return { signedIn: await acpSignedIn(id), account: null };const how = starting(cliPath, spec.auth);- const r = await run(how.cmd, how.args, 10_000, { env: childEnvironment({ shell: await loginShellVariables(), extra: how.env }), options: how.options });+ const r = await run(how.cmd, how.args, 10_000, { env: childEnvironment({ shell: agentShell(id, await loginShellVariables()), extra: how.env }), options: how.options });return readAgentStatus(id, r.stdout, r.out);}@@ -385,24 +390,91 @@ export function signInScript(cliPath, args) {return `tell application "Terminal"\nactivate\ndo script "${command.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}"\nend tell`;}-/** Sign in: Terminal runs the program's own sign-in, which opens the maker's own page. The app touches nothing. */+/**+ * A PowerShell script that names its window and runs one program (`& 'program' 'arg'…`, each in single quotes, as+ * PowerShell quotes): Windows sign-in. With a BOM, so Windows PowerShell reads a path with Chinese in it as UTF-8.+ */+export function powerShellScript(title, cmd, args) {+ const q = (s) => `'${String(s).replace(/'/g, "''")}'`;+ return `\ufeff$Host.UI.RawUI.WindowTitle = ${q(title)}\r\n& ${[cmd, ...args].map(q).join(' ')}\r\n`;+}++/** The person's shell variables as this agent is given them (`fromShell`; all of them, for most). */+export function agentShell(id, shell) {+ return AGENTS[id]?.fromShell?.(shell) ?? shell;+}++/**+ * The ways an agent's adapter offers to sign in in a terminal (ACP terminal auth; Claude Code's: its subscription and+ * Anthropic Console). `[{ id, name, description?, args, env }]`, asked of the adapter as it shakes hands; none for an+ * agent signed in its own way (no `acpSignIn`) or one that cannot be started.+ */+export async function agentSignInMethods(id) {+ const spec = AGENTS[id];+ if (!spec?.acpSignIn) return [];+ const shell = agentShell(id, await loginShellVariables());+ const cli = resolveAgentCli(id, { pathDirs: shell.PATH.split(PATH_SEP) });+ if (!cli) return [];+ const launch = adapterLaunch(id, cli.path);+ const child = spawn(launch.cmd, launch.args, { env: childEnvironment({ shell, extra: launch.env }), stdio: ['pipe', 'pipe', 'ignore'], windowsHide: true });+ child.once('error', () => {});+ try {+ const conn = acp.client({ name: 'openfilm-desktop' }).connect(acp.ndJsonStream(Writable.toWeb(child.stdin), Readable.toWeb(child.stdout)));+ const init = await Promise.race([+ conn.agent.request(acp.methods.agent.initialize, { protocolVersion: acp.PROTOCOL_VERSION, clientCapabilities: CLIENT_CAPABILITIES, clientInfo: CLIENT_INFO }),+ new Promise((_, fail) => setTimeout(() => fail(new Error('timed out')), 30_000)),+ ]);+ return (init.authMethods ?? []).filter((m) => m?.type === 'terminal' && m.id && m.name).map((m) => ({+ id: String(m.id), name: String(m.name), ...(m.description ? { description: String(m.description) } : {}),+ args: Array.isArray(m.args) ? m.args.map(String) : [], env: m.env && typeof m.env === 'object' ? m.env : {},+ }));+ } catch {+ return [];+ } finally {+ child.kill();+ }+}++/**+ * Sign in: Terminal runs the program's own sign-in, which opens the maker's own page. The app touches nothing. One+ * signed in the way its adapter offers (`acpSignIn`, that way's id) runs the adapter with that way's arguments, as ACP+ * has a client do it.+ */export async function openAgentSignIn(id) {const spec = AGENTS[id];const pathDirs = (await loginShellEnvironment()).PATH.split(PATH_SEP);const cli = spec ? resolveAgentCli(id, { pathDirs }) : null;if (!spec || !cli) return { ok: false, error: `${spec?.label ?? id} is not installed on this computer.` };asked.delete(id);+ let how = null;+ if (spec.acpSignIn) {+ const way = (await agentSignInMethods(id)).find((w) => w.id === spec.acpSignIn);+ if (!way) return { ok: false, error: `${spec.label} did not offer a way to sign in.` };+ const launch = adapterLaunch(id, cli.path);+ how = { cmd: launch.cmd, args: [...launch.args, ...way.args], env: { ...launch.env, ...way.env } };+ }if (process.platform === 'win32') {- /* a command window of its own, left open so its sign-in can be read and answered (it runs a shim itself) */- const how = /\.(?:cmd|bat)$/i.test(cli.path) ? { cmd: cli.path, args: spec.login, env: {} } : starting(cli.path, spec.login);+ /* a PowerShell window of its own running a script, as ChatCut does: no command line for cmd.exe to unquote (a path+ with a space in it), and left open so its sign-in can be read and answered (it runs a shim itself) */+ how ??= /\.(?:cmd|bat)$/i.test(cli.path) ? { cmd: cli.path, args: spec.login, env: {} } : starting(cli.path, spec.login);try {- spawn('cmd.exe', ['/d', '/c', 'start', `${spec.label} sign-in`, 'cmd.exe', '/k', how.cmd, ...how.args], {- detached: true, stdio: 'ignore', windowsHide: false, env: childEnvironment({ shell: await loginShellVariables(), extra: how.env }),- }).unref();+ const dir = join(tmpdir(), 'openfilm-sign-in');+ mkdirSync(dir, { recursive: true });+ const file = join(dir, `${id}.ps1`);+ writeFileSync(file, powerShellScript(`${spec.label} sign-in`, how.cmd, how.args), 'utf8');+ const powershell = join(process.env.SystemRoot ?? 'C:\\Windows', 'System32', 'WindowsPowerShell', 'v1.0', 'powershell.exe');+ const child = spawn(powershell, ['-NoLogo', '-NoExit', '-ExecutionPolicy', 'Bypass', '-File', file], {+ detached: true, stdio: 'ignore', windowsHide: false, env: childEnvironment({ shell: agentShell(id, await loginShellVariables()), extra: how.env }),+ });+ await new Promise((done, fail) => { child.once('spawn', done); child.once('error', fail); });+ child.unref();return { ok: true };} catch (e) { return { ok: false, error: e instanceof Error ? e.message : String(e) }; }}- const r = await run('/usr/bin/osascript', ['-e', signInScript(cli.path, spec.login)], 15_000);+ const script = how+ ? signInScript('/usr/bin/env', [...Object.entries(how.env).map(([k, v]) => `${k}=${v}`), how.cmd, ...how.args])+ : signInScript(cli.path, spec.login);+ const r = await run('/usr/bin/osascript', ['-e', script], 15_000);return r.ok ? { ok: true } : { ok: false, error: r.out.trim() || 'Terminal did not open.' };}@@ -442,6 +514,17 @@ function adapterScript(pkg) {return join(dirname(manifest), bin ?? json.main ?? 'index.js');}+/** How an agent's adapter starts: the app's Node running its script (on Electron, through as-node.mjs), told where the person's program is. */+function adapterLaunch(id, cliPath) {+ const spec = AGENTS[id];+ const node = nodeRuntime();+ return {+ cmd: node.command,+ args: [...(node.env.ELECTRON_RUN_AS_NODE ? [AS_NODE] : []), adapterScript(spec.adapter)],+ env: { ...node.env, ...spec.env(cliPath) },+ };+}+function nodeRuntime() {return process.versions.electron? { command: nodeBinary(), env: { ELECTRON_RUN_AS_NODE: '1' } }@@ -514,6 +597,8 @@ export function formContent(questions, answers) {/** What the app tells each agent, as it shakes hands, that the chat can do. */export const CLIENT_CAPABILITIES = {fs: { readTextFile: false, writeTextFile: false }, terminal: false, elicitation: { form: {} },+ /* the person signs in in Terminal, the way the agent offers (agentSignInMethods) */+ auth: { terminal: true },/* the agent's own warnings (Codex: "Falling back from WebSockets…") come as notices, not as words in its reply */session: { notices: {} },/* Claude Code's "recommended value" extension: its model and effort lists come without a "Default" row, the@@ -521,6 +606,8 @@ export const CLIENT_CAPABILITIES = {_meta: { jetbrains: { air: { version: 1, capabilities: ['recommendedValue'] } } },};+const CLIENT_INFO = { name: 'openfilm-desktop', title: 'OpenFilm', version: '0.1.0' };+/** ACP's auth_required: the agent wants its own sign-in first. */const AUTH_REQUIRED = -32000;@@ -581,11 +668,11 @@ export class AgentSession {/*** Start the adapter, shake hands, open (or resume) the session. The agent gets all the person's login shell sets- * (their keys, gateways, tokens), as in their terminal, and the app's `openfilm` first on its PATH; the app's ffmpeg- * (`fallbackDir`) last, for a computer that has none of its own.+ * (their keys, gateways, tokens; Claude Code only its PATH and the like, agentShell), as in their terminal, and the app's+ * `openfilm` first on its PATH; the app's ffmpeg (`fallbackDir`) last, for a computer that has none of its own.*/async start() {- const shell = await loginShellVariables({ staleOk: true });+ const shell = agentShell(this.opts.agent, await loginShellVariables({ staleOk: true }));const harness = this.opts.harness ?? null;const cli = harness ? null : resolveAgentCli(this.opts.agent, { pathDirs: shell.PATH.split(PATH_SEP) });if (!harness && !cli) throw Object.assign(new Error(`${this.spec.label} is not installed on this computer.`), { code: 'not-installed' });@@ -597,7 +684,7 @@ export class AgentSession {const own = !harness ? {} : {...harness.env,OPENFILM_GET_WEB: '1',- OPENFILM_PI: JSON.stringify({ agentDir: harness.home, provider: harness.provider, model: harness.model, models: harness.models ?? [harness.model], systemPrompt: ownAgentPrompt() }),+ OPENFILM_PI: JSON.stringify({ agentDir: harness.home, provider: harness.provider, model: harness.model, models: harness.models ?? [harness.model], efforts: harness.efforts ?? {}, systemPrompt: ownAgentPrompt() }),};/* an agent that speaks ACP itself: its own program, started as the registry starts it */const native = cli && !this.spec.adapter ? starting(cli.path, this.spec.args) : null;@@ -612,8 +699,8 @@ export class AgentSession {const script = harness ? fileURLToPath(new URL('./pi-agent.mjs', import.meta.url)) : native ? null : adapterScript(this.spec.adapter);this.child = native- ? spawn(native.cmd, native.args, { ...native.options, cwd: this.opts.cwd, env, stdio: ['pipe', 'pipe', 'pipe'] })- : spawn(node.command, [...(node.env.ELECTRON_RUN_AS_NODE ? [AS_NODE] : []), script], { cwd: this.opts.cwd, env, stdio: ['pipe', 'pipe', 'pipe'] });+ ? spawn(native.cmd, native.args, { ...native.options, cwd: this.opts.cwd, env, stdio: ['pipe', 'pipe', 'pipe'], windowsHide: true })+ : spawn(node.command, [...(node.env.ELECTRON_RUN_AS_NODE ? [AS_NODE] : []), script], { cwd: this.opts.cwd, env, stdio: ['pipe', 'pipe', 'pipe'], windowsHide: true });this.child.stderr.on('data', (d) => { this.stderr = (this.stderr + String(d)).slice(-16_000); });this.child.once('exit', (code) => this.opts.onExit?.({ code, stderr: this.stderr }));this.child.once('error', () => {});@@ -639,7 +726,7 @@ export class AgentSession {const init = await this.conn.agent.request(acp.methods.agent.initialize, {protocolVersion: acp.PROTOCOL_VERSION,clientCapabilities: CLIENT_CAPABILITIES,- clientInfo: { name: 'openfilm-desktop', title: 'OpenFilm', version: '0.1.0' },+ clientInfo: CLIENT_INFO,});const params = {diff --git a/apps/desktop/src/agents.test.mjs b/apps/desktop/src/agents.test.mjsindex 9e7ef97..2cde62c 100644--- a/apps/desktop/src/agents.test.mjs+++ b/apps/desktop/src/agents.test.mjs@@ -1,6 +1,6 @@import { test } from 'node:test';import assert from 'node:assert/strict';-import { CLIENT_CAPABILITIES, allowOption, codexDeveloperInstructions, legacyOptions, readAgentStatus } from './agents.mjs';+import { CLIENT_CAPABILITIES, agentShell, allowOption, codexDeveloperInstructions, legacyOptions, powerShellScript, readAgentStatus } from './agents.mjs';test('an agent that asks: yes for good where it offers that, else yes this once; nothing to say yes with is null', () => {const options = [@@ -56,3 +56,30 @@ test('the chat says it shows notices, so an agent sends its warnings as noticesassert.ok(typeof notices === 'object' && notices !== null && !Array.isArray(notices));assert.deepEqual(parsed.elicitation, { form: {} });});++test('Claude Code is given of the shell only its PATH and the like: a key or gateway there never stands in for the subscription', () => {+ const shell = { PATH: '/bin', HTTPS_PROXY: 'http://proxy', CLAUDE_CONFIG_DIR: '/c', LANG: 'zh_CN.UTF-8',+ ANTHROPIC_API_KEY: 'k', ANTHROPIC_AUTH_TOKEN: 't', ANTHROPIC_BASE_URL: 'http://relay', OPENAI_API_KEY: 'o' };+ const given = agentShell('claude', shell);+ for (const key of ['ANTHROPIC_API_KEY', 'ANTHROPIC_AUTH_TOKEN', 'ANTHROPIC_BASE_URL', 'OPENAI_API_KEY']) assert.equal(given[key], undefined, key);+ assert.equal(given.HTTPS_PROXY, 'http://proxy');+ assert.equal(given.CLAUDE_CONFIG_DIR, '/c', 'where its own sign-in is kept');+ assert.equal(given.LANG, 'zh_CN.UTF-8');+ assert.equal(agentShell('codex', shell), shell, 'the others have the shell as it is');+});++test('the chat says it signs in in Terminal, so Claude Code offers its subscription sign-in (ACP terminal auth)', async () => {+ const { zClientCapabilities } = await import(new URL('../node_modules/@agentclientprotocol/sdk/dist/schema/zod.gen.js', import.meta.url).href);+ /* the check Claude Code's adapter makes before it offers its terminal sign-ins */+ assert.equal(zClientCapabilities.parse(CLIENT_CAPABILITIES).auth?.terminal, true);+});++test('a Windows sign-in is a PowerShell script: each path whole however it is spelled, read as UTF-8', () => {+ const script = powerShellScript('Claude Code sign-in', 'C:\\Program Files\\OpenFilm\\OpenFilm.exe',+ ['C:\\Program Files\\OpenFilm\\app.asar\\src\\as-node.mjs', 'D:\\用户\\it\'s\\index.js', '--cli', 'auth', 'login', '--claudeai']);+ assert.ok(script.startsWith('\ufeff'), 'a BOM: Windows PowerShell reads it as UTF-8');+ const lines = script.slice(1).split('\r\n');+ assert.equal(lines[0], "$Host.UI.RawUI.WindowTitle = 'Claude Code sign-in'");+ assert.equal(lines[1], "& 'C:\\Program Files\\OpenFilm\\OpenFilm.exe' 'C:\\Program Files\\OpenFilm\\app.asar\\src\\as-node.mjs' "+ + "'D:\\用户\\it''s\\index.js' '--cli' 'auth' 'login' '--claudeai'");+});diff --git a/apps/desktop/src/as-node.mjs b/apps/desktop/src/as-node.mjsindex 4241eee..293817d 100644--- a/apps/desktop/src/as-node.mjs+++ b/apps/desktop/src/as-node.mjs@@ -4,11 +4,16 @@* the program starts, so what the program starts (the person's agent, and every command that agent runs) does not* have it. An Electron app the agent runs, or its tests, would start as Node too.*+ * On Windows, what the program starts opens no console window (hidden-windows.mjs).+ ** electron as-node.mjs <program> [args…]*/+import childProcess from 'node:child_process';import { pathToFileURL } from 'node:url';+import { hideChildWindows } from './hidden-windows.mjs';delete process.env.ELECTRON_RUN_AS_NODE;+if (process.platform === 'win32') hideChildWindows(childProcess);const program = process.argv[2];/* the program sees itself as the script it is: argv[1] is its path, its own arguments follow */process.argv.splice(1, 1);diff --git a/apps/desktop/src/ffmpeg-runtime.mjs b/apps/desktop/src/ffmpeg-runtime.mjsindex db52efe..fdadfc1 100644--- a/apps/desktop/src/ffmpeg-runtime.mjs+++ b/apps/desktop/src/ffmpeg-runtime.mjs@@ -53,7 +53,7 @@ export const ffmpegIn = (dir, platform = process.platform) => ['ffmpeg', 'ffprob/** Whether both programs run from this PATH. */export function ffmpegOnPath(path) {return ['ffmpeg', 'ffprobe'].every((bin) => {- const r = spawnSync(bin, ['-version'], { stdio: 'ignore', env: { ...process.env, PATH: path } });+ const r = spawnSync(bin, ['-version'], { stdio: 'ignore', env: { ...process.env, PATH: path }, windowsHide: true });return !r.error && r.status === 0;});}diff --git a/apps/desktop/src/hidden-windows.mjs b/apps/desktop/src/hidden-windows.mjsnew file mode 100644index 0000000..5e32c8f--- /dev/null+++ b/apps/desktop/src/hidden-windows.mjs@@ -0,0 +1,39 @@+/**+ * Windows: the programs a Node program of the app starts get no window of their own. The app's Node (Electron as Node)+ * has no console, so a console program it starts (`claude auth status`, `codex app-server` through cmd.exe) opens a+ * console window unless asked not to, and the agents' adapters do not ask. as-node.mjs makes every child_process+ * function take `windowsHide: true` unless it is given otherwise, before the adapter is loaded. A window someone has+ * to see (a sign-in) is opened by the app itself, not from there. Reads no Electron module, so `node --test` can use it.+ */+import { syncBuiltinESMExports } from 'node:module';+import { promisify } from 'node:util';++const NAMES = ['spawn', 'spawnSync', 'execFile', 'execFileSync', 'exec', 'execSync', 'fork'];++/**+ * A child_process call's arguments with `windowsHide: true` in its options: the options given (a plain object after the+ * program) get it unless they say otherwise; with none, options are put in before the callback, or at the end.+ */+export function withWindowsHide(args) {+ const at = args.findIndex((a, i) => i > 0 && a !== null && typeof a === 'object' && !Array.isArray(a));+ if (at >= 0) return args.map((a, i) => (i === at ? { windowsHide: true, ...a } : a));+ const callback = args.findIndex((a, i) => i > 0 && typeof a === 'function');+ const out = [...args];+ out.splice(callback >= 0 ? callback : out.length, 0, { windowsHide: true });+ return out;+}++/** Every child_process function of `childProcess` (node:child_process) given `windowsHide: true` unless told otherwise. */+export function hideChildWindows(childProcess) {+ for (const name of NAMES) {+ const original = childProcess[name];+ if (typeof original !== 'function' || original.hidesWindows) continue;+ const hidden = Object.assign(function (...args) { return original.apply(this, withWindowsHide(args)); }, { hidesWindows: true });+ /* `promisify(execFile)` resolves { stdout, stderr } through its own custom form: kept, with the same options */+ const custom = original[promisify.custom];+ if (typeof custom === 'function') hidden[promisify.custom] = (...args) => custom(...withWindowsHide(args));+ childProcess[name] = hidden;+ }+ /* `import { spawn } from 'node:child_process'` sees the new ones too */+ syncBuiltinESMExports();+}diff --git a/apps/desktop/src/hidden-windows.test.mjs b/apps/desktop/src/hidden-windows.test.mjsnew file mode 100644index 0000000..db5cbae--- /dev/null+++ b/apps/desktop/src/hidden-windows.test.mjs@@ -0,0 +1,27 @@+import { test } from 'node:test';+import assert from 'node:assert/strict';+import childProcess from 'node:child_process';+import { promisify } from 'node:util';+import { hideChildWindows, withWindowsHide } from './hidden-windows.mjs';++test('every way to call child_process gets windowsHide, unless it says otherwise', () => {+ const cb = () => {};+ assert.deepEqual(withWindowsHide(['claude']), ['claude', { windowsHide: true }]);+ assert.deepEqual(withWindowsHide(['claude', ['auth', 'status']]), ['claude', ['auth', 'status'], { windowsHide: true }]);+ assert.deepEqual(withWindowsHide(['claude', ['auth'], { timeout: 5 }]), ['claude', ['auth'], { windowsHide: true, timeout: 5 }]);+ assert.deepEqual(withWindowsHide(['claude', ['auth'], cb]), ['claude', ['auth'], { windowsHide: true }, cb]);+ assert.deepEqual(withWindowsHide(['codex app-server', { shell: true }]), ['codex app-server', { windowsHide: true, shell: true }]);+ assert.deepEqual(withWindowsHide(['dir', cb]), ['dir', { windowsHide: true }, cb]);+ assert.deepEqual(withWindowsHide(['x', { windowsHide: false }]), ['x', { windowsHide: false }], 'a window asked for is kept');+});++test('once hidden, the functions still work as before: promisify(execFile) still gives stdout and stderr', async () => {+ hideChildWindows(childProcess);+ hideChildWindows(childProcess);+ assert.equal(childProcess.execFile.hidesWindows, true);+ const { execFile, spawnSync } = await import('node:child_process');+ assert.equal(execFile, childProcess.execFile, 'an ES import sees the hidden one');+ const { stdout, stderr } = await promisify(execFile)(process.execPath, ['-e', 'process.stdout.write("out"); process.stderr.write("err")']);+ assert.deepEqual([stdout, stderr], ['out', 'err']);+ assert.equal(spawnSync(process.execPath, ['-e', 'process.exit(3)']).status, 3);+});diff --git a/apps/desktop/src/pi-agent.mjs b/apps/desktop/src/pi-agent.mjsindex 420dea6..65c6bb2 100644--- a/apps/desktop/src/pi-agent.mjs+++ b/apps/desktop/src/pi-agent.mjs@@ -5,7 +5,8 @@** What it runs with comes in OPENFILM_PI (JSON; pi-config.mjs): its own folder (`agentDir`: Pi's models and settings,* never the person's ~/.pi), the service (`provider`), its models (`model` the one a session starts on; the chat- * switches between them with the session's `model` option), and its whole system prompt (agents.mjs+ * switches between them with the session's `model` option), each model's reasoning efforts (`efforts`: the session's+ * `effort` option, for a model offering more than one; `auto` sends none), and its whole system prompt (agents.mjs* ownAgentPrompt), in place of Pi's. Of the project's instructions only its own AGENTS.md (or CLAUDE.md) is read, not* the folders above it; no skills, no SYSTEM.md. Besides Pi's read, bash, edit and write it has `ask`: questions for* the person, with options, shown in the chat as Claude Code's and Codex's are (an ACP form).@@ -24,7 +25,7 @@ import { Readable, Writable } from 'node:stream';import * as acp from '@agentclientprotocol/sdk';import { DefaultResourceLoader, ModelRuntime, SessionManager, SettingsManager, createAgentSession } from '@earendil-works/pi-coding-agent';import { agentSessionsDir, openLog } from './chat-store.mjs';-import { contextUsed, modelOption, modelsJson, stopReason, toolCall, toolContent } from './pi-config.mjs';+import { THINKING, contextUsed, effortOption, modelEfforts, modelOption, modelsJson, stopReason, toolCall, toolContent } from './pi-config.mjs';/* stdout is the protocol's: anything else Pi or a library prints goes to stderr */console.log = console.info = console.warn = console.debug = (...args) => process.stderr.write(`${args.map(String).join(' ')}\n`);@@ -32,15 +33,35 @@ console.log = console.info = console.warn = console.debug = (...args) => processconst config = JSON.parse(process.env.OPENFILM_PI ?? '{}');const VERSION = '1.1.0';const models = Array.isArray(config.models) && config.models.length ? config.models : [config.model];+const efforts = modelEfforts(config.efforts, models);mkdirSync(config.agentDir, { recursive: true });const modelsPath = join(config.agentDir, 'models.json');-writeFileSync(modelsPath, JSON.stringify(modelsJson(config.provider, models), null, 2));+writeFileSync(modelsPath, JSON.stringify(modelsJson(config.provider, models, efforts), null, 2));const modelRuntime = await ModelRuntime.create({ authPath: join(config.agentDir, 'auth.json'), modelsPath });-/** Each open session: Pi's, and the turn being logged. */+/** Each open session: Pi's, the turn being logged, and its model and effort as the chat names them. */const sessions = new Map();+/** Pi's model for a model and an effort: the service as itself for `auto` (no reasoning setting), else the thinking one. */+function piModel(/** @type {string} */ id, /** @type {string} */ effort) {+ const model = modelRuntime.getModel(effort === 'auto' ? config.provider.id : THINKING(config.provider.id), id);+ if (!model) throw new Error(`No model ${id} at ${config.provider.id}.`);+ return model;+}++/** The session's options: the model, and its effort when it offers more than one. */+const optionsOf = (/** @type {{ model: string, effort: string }} */ entry) =>+ [modelOption(models, entry.model), effortOption(efforts[entry.model]?.levels ?? ['auto'], entry.effort)].filter(Boolean);++/** Put a session on a model and an effort. */+async function use(/** @type {any} */ entry, /** @type {string} */ model, /** @type {string} */ effort) {+ await entry.session.setModel(piModel(model, effort));+ if (effort !== 'auto') entry.session.setThinkingLevel(effort);+ entry.model = model;+ entry.effort = effort;+}+/** The model log of a turn: each request's context and its answer, only what is new since the last request. */function modelLog(cwd, turnId) {const file = openLog(cwd, turnId, (e) => process.stderr.write(`model log: ${e.message}\n`), '.model');@@ -90,13 +111,18 @@ async function open(client, cwd, resumeId) {],});await resourceLoader.reload();- const model = modelRuntime.getModel(config.provider.id, config.model);- if (!model) throw new Error(`No model ${config.model} at ${config.provider.id}.`);+ const startEffort = efforts[config.model]?.effort ?? 'auto';const { session } = await createAgentSession({- cwd, agentDir: config.agentDir, modelRuntime, model,+ cwd, agentDir: config.agentDir, modelRuntime, model: piModel(config.model, startEffort),+ ...(startEffort !== 'auto' ? { thinkingLevel: startEffort } : {}),resourceLoader, sessionManager, settingsManager, tools: ['read', 'bash', 'edit', 'write', 'ask'],});- const entry = { session, log: null };+ /* what it runs on now, as the chat names it (a conversation taken up again may be on another model and effort) */+ const onModel = models.includes(session.model?.id) ? session.model.id : config.model;+ const levels = efforts[onModel]?.levels ?? ['auto'];+ const thinking = session.model?.provider === THINKING(config.provider.id);+ const entry = { session, log: null, model: onModel,+ effort: thinking && levels.includes(session.thinkingLevel) ? session.thinkingLevel : !thinking && levels.includes('auto') ? 'auto' : efforts[onModel]?.effort ?? 'auto' };sessionId = session.sessionId;const send = (update) => { void client.sessionUpdate({ sessionId, update }).catch(() => {}); };@@ -136,7 +162,7 @@ async function open(client, cwd, resumeId) {}});sessions.set(sessionId, entry);- return { sessionId, configOptions: [modelOption(models, model.id)], modes: null };+ return { sessionId, configOptions: optionsOf(entry), modes: null };}/**@@ -241,11 +267,18 @@ const connection = new acp.AgentSideConnection((client) => ({async setSessionConfigOption(params) {const entry = sessions.get(params.sessionId);if (!entry) throw new Error('This conversation has ended.');- if (params.configId !== 'model') throw new Error(`No option ${params.configId}.`);- const model = models.includes(params.value) ? modelRuntime.getModel(config.provider.id, params.value) : null;- if (!model) throw new Error(`No model ${params.value} at ${config.provider.id}.`);- await entry.session.setModel(model);- return { configOptions: [modelOption(models, model.id)] };+ if (params.configId === 'model') {+ const model = String(params.value);+ if (!models.includes(model)) throw new Error(`No model ${model} at ${config.provider.id}.`);+ /* the effort it was on, when this model takes it; else the one this model starts on */+ const levels = efforts[model]?.levels ?? ['auto'];+ await use(entry, model, levels.includes(entry.effort) ? entry.effort : efforts[model]?.effort ?? 'auto');+ } else if (params.configId === 'effort') {+ const effort = String(params.value);+ if (!(efforts[entry.model]?.levels ?? []).includes(effort)) throw new Error(`${entry.model} does not take the effort ${effort}.`);+ await use(entry, entry.model, effort);+ } else throw new Error(`No option ${params.configId}.`);+ return { configOptions: optionsOf(entry) };},async cancel(params) { await sessions.get(params.sessionId)?.session.abort(); },async closeSession(params) {diff --git a/apps/desktop/src/pi-agent.test.mjs b/apps/desktop/src/pi-agent.test.mjsindex d3f6492..d99cae1 100644--- a/apps/desktop/src/pi-agent.test.mjs+++ b/apps/desktop/src/pi-agent.test.mjs@@ -6,15 +6,17 @@ import { tmpdir } from 'node:os';import { join } from 'node:path';import { AgentSession } from './agents.mjs';-/** A service on the Chat Completions API that answers "ok" and notes the model each request named. */+/** A service on the Chat Completions API that answers "ok" and notes the model each request named (and each request). */async function service() {const asked = [];+ const bodies = [];const server = createServer((req, res) => {let body = '';req.on('data', (d) => { body += d; });req.on('end', () => {const { model } = JSON.parse(body || '{}');asked.push(model);+ bodies.push(JSON.parse(body || '{}'));res.writeHead(200, { 'content-type': 'text/event-stream' });const chunk = (choice, extra = {}) => `data: ${JSON.stringify({ id: 'c', object: 'chat.completion.chunk', created: 0, model, choices: [choice], ...extra })}\n\n`;res.end(chunk({ index: 0, delta: { role: 'assistant', content: 'ok' }, finish_reason: null })@@ -23,7 +25,7 @@ async function service() {});});await new Promise((done) => server.listen(0, '127.0.0.1', done));- return { asked, url: `http://127.0.0.1:${server.address().port}/v1`, close: () => server.close() };+ return { asked, bodies, url: `http://127.0.0.1:${server.address().port}/v1`, close: () => server.close() };}test('the own key\'s agent offers its models as the session\'s model option, and a turn after a switch goes to that model', { timeout: 60_000 }, async () => {@@ -55,3 +57,47 @@ test('the own key\'s agent offers its models as the session\'s model option, andsvc.close();}});++test('a model\'s efforts are the session\'s effort option: Auto sends no reasoning setting, a level sends that level', { timeout: 60_000 }, async () => {+ const svc = await service();+ const session = new AgentSession({+ agent: 'byok',+ cwd: mkdtempSync(join(tmpdir(), 'of-pi-')),+ harness: {+ home: mkdtempSync(join(tmpdir(), 'of-pi-home-')),+ provider: { id: 'byok', api: 'openai-completions', baseUrl: svc.url, apiKeyEnv: 'OPENFILM_BYOK_KEY' },+ model: 'thinker', models: ['thinker', 'plain'],+ efforts: { thinker: { levels: ['auto', 'off', 'low', 'high'], effort: 'low' } },+ env: { OPENFILM_BYOK_KEY: 'sk-test' },+ },+ });+ const effortOf = (options) => options.find((o) => o.id === 'effort');+ try {+ const info = await session.start();+ const effort = effortOf(info.configOptions);+ assert.equal(effort.category, 'thought_level');+ assert.deepEqual(effort.options.map((o) => o.value), ['auto', 'off', 'low', 'high'], 'only the efforts it was given');+ assert.equal(effort.currentValue, 'low', 'the one it starts on');+ await session.prompt([{ type: 'text', text: 'one' }]);+ assert.equal(svc.bodies.at(-1).reasoning_effort, 'low');++ await session.setConfigOption('effort', 'high');+ await session.prompt([{ type: 'text', text: 'two' }]);+ assert.equal(svc.bodies.at(-1).reasoning_effort, 'high');++ await session.setConfigOption('effort', 'auto');+ await session.prompt([{ type: 'text', text: 'three' }]);+ assert.equal(svc.bodies.at(-1).reasoning_effort, undefined, 'Auto: nothing about reasoning');+ await assert.rejects(session.setConfigOption('effort', 'max'), 'an effort it was not given is refused');++ /* a model given no efforts offers none, and is asked with none */+ const plain = await session.setConfigOption('model', 'plain');+ assert.equal(effortOf(plain.configOptions), undefined);+ await session.prompt([{ type: 'text', text: 'four' }]);+ assert.equal(svc.bodies.at(-1).model, 'plain');+ assert.equal(svc.bodies.at(-1).reasoning_effort, undefined);+ } finally {+ session.close();+ svc.close();+ }+});diff --git a/apps/desktop/src/pi-config.mjs b/apps/desktop/src/pi-config.mjsindex 653437c..b1efc40 100644--- a/apps/desktop/src/pi-config.mjs+++ b/apps/desktop/src/pi-config.mjs@@ -10,30 +10,70 @@/** The person's own key's API, as Pi names it. */export const PI_APIS = Object.freeze({ chat: 'openai-completions', responses: 'openai-responses', anthropic: 'anthropic-messages' });+/**+ * The reasoning efforts a model of the person's own can offer, in order: `auto` sends no reasoning setting (the+ * service decides, as it always did), the others are Pi's thinking levels. Which of them a model offers is the+ * person's to say in Settings (OpenFilm cannot know what a service's model takes); a model they said nothing of offers+ * `auto` alone.+ */+export const EFFORTS = Object.freeze(['auto', 'off', 'minimal', 'low', 'medium', 'high', 'xhigh', 'max']);++/** The efforts a model offers, as kept: known ones, each once, in EFFORTS' order; `auto` alone when none. */+export function effortLevels(value) {+ const asked = new Set(Array.isArray(value) ? value.map(String) : []);+ const levels = EFFORTS.filter((e) => asked.has(e));+ return levels.length ? levels : ['auto'];+}++/**+ * Each model's efforts, for the models named: the ones it offers (`levels`) and the one a conversation starts on+ * (`effort`, one of them; the first when the one asked is not).+ * @param {unknown} raw @param {string[]} models+ * @returns {Record<string, { levels: string[], effort: string }>}+ */+export function modelEfforts(raw, models) {+ const all = raw && typeof raw === 'object' ? /** @type {Record<string, any>} */ (raw) : {};+ return Object.fromEntries(models.map((model) => {+ const levels = effortLevels(all[model]?.levels);+ const effort = levels.includes(String(all[model]?.effort)) ? String(all[model].effort) : levels[0];+ return [model, { levels, effort }];+ }));+}++/** The provider that thinks: the same service, its models told which levels they take. */+export const THINKING = (/** @type {string} */ id) => `${id}-thinking`;++/* what a level is called on the wire: Pi's own names, but "none" for off on OpenAI's APIs */+const wire = (/** @type {string} */ api, /** @type {string} */ level) => (level === 'off' && api !== 'anthropic-messages' ? 'none' : level);+/** A context length for a model the service does not describe. */const CONTEXT = 128_000;/**- * Pi's `models.json` for the service and its models.+ * Pi's `models.json` for the service and its models. The service twice: as itself, where no model is told to reason+ * (`auto`: no reasoning setting is sent), and as THINKING(id) with the models that offer levels, each told exactly+ * which (Pi sends the one picked; the others are not there).* @param {{ id: string, api: string, baseUrl: string, apiKeyEnv: string }} provider @param {string[] | string} models+ * @param {Record<string, { levels: string[] }>} [efforts]*/-export function modelsJson(provider, models) {+export function modelsJson(provider, models, efforts = {}) {+ const names = [models].flat();+ const entry = (/** @type {string} */ model, /** @type {string[] | null} */ levels) => ({+ id: model,+ name: model,+ input: ['text', 'image'],+ contextWindow: CONTEXT,+ maxTokens: Math.floor(CONTEXT / 4),+ reasoning: Boolean(levels),+ ...(levels ? { thinkingLevelMap: Object.fromEntries(EFFORTS.slice(1).map((l) => [l, levels.includes(l) ? wire(provider.api, l) : null])) } : {}),+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },+ });+ const service = { baseUrl: provider.baseUrl, api: provider.api, apiKey: `$${provider.apiKeyEnv}` };+ const thinking = names.map((m) => [m, (efforts[m]?.levels ?? []).filter((l) => l !== 'auto')]).filter(([, levels]) => levels.length);return {providers: {- [provider.id]: {- baseUrl: provider.baseUrl,- api: provider.api,- apiKey: `$${provider.apiKeyEnv}`,- models: [models].flat().map((model) => ({- id: model,- name: model,- input: ['text', 'image'],- contextWindow: CONTEXT,- maxTokens: Math.floor(CONTEXT / 4),- reasoning: false,- cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },- })),- },+ [provider.id]: { ...service, models: names.map((m) => entry(m, null)) },+ ...(thinking.length ? { [THINKING(provider.id)]: { ...service, models: thinking.map(([m, levels]) => entry(/** @type {string} */ (m), /** @type {string[]} */ (levels))) } } : {}),},};}@@ -43,6 +83,15 @@ export function modelOption(models, current) {return { id: 'model', name: 'Model', category: 'model', type: 'select', currentValue: current, options: models.map((m) => ({ value: m, name: m })) };}+const EFFORT_NAMES = { auto: 'Auto', off: 'Off', minimal: 'Minimal', low: 'Low', medium: 'Medium', high: 'High', xhigh: 'Extra high', max: 'Max' };++/** The reasoning effort as a config option (ACP), for a model offering more than one; null otherwise. */+export function effortOption(/** @type {string[]} */ levels, /** @type {string} */ current) {+ if (levels.length < 2) return null;+ return { id: 'effort', name: 'Effort', category: 'thought_level', type: 'select', currentValue: current,+ options: levels.map((l) => ({ value: l, name: EFFORT_NAMES[/** @type {keyof typeof EFFORT_NAMES} */ (l)] ?? l })) };+}+/** A tool call as the chat reads it (ACP): its kind, its title, and what it was run with. */export function toolCall(name, args) {const a = args && typeof args === 'object' ? args : {};diff --git a/apps/desktop/src/pi-config.test.mjs b/apps/desktop/src/pi-config.test.mjsindex 3935109..61eec18 100644--- a/apps/desktop/src/pi-config.test.mjs+++ b/apps/desktop/src/pi-config.test.mjs@@ -1,6 +1,6 @@import { test } from 'node:test';import assert from 'node:assert/strict';-import { contextUsed, modelOption, modelsJson, outOfBalance, stopReason, toolCall, toolContent } from './pi-config.mjs';+import { THINKING, contextUsed, effortOption, modelEfforts, modelOption, modelsJson, outOfBalance, stopReason, toolCall, toolContent } from './pi-config.mjs';test('the person\'s own key is one provider in Pi\'s models.json, with the one model they named', () => {const p = modelsJson({ id: 'byok', api: 'openai-completions', baseUrl: 'https://api.deepseek.com/v1', apiKeyEnv: 'K' }, 'vision').providers.byok;@@ -55,3 +55,24 @@ test('no balance left, as each service says it, is one outcome with the service\assert.equal(outOfBalance(own('https://api.deepseek.com/v1'), new Error('402 Payment Required')).message,"DeepSeek doesn't have enough balance for this. Top it up there, or switch to another service, then try again.");});++test('the service twice: as itself (no reasoning setting) and thinking, each model told only the levels it was given', () => {+ const efforts = modelEfforts({ a: { levels: ['auto', 'off', 'low', 'max'], effort: 'low' } }, ['a', 'b']);+ assert.deepEqual(efforts.b, { levels: ['auto'], effort: 'auto' });+ const json = modelsJson({ id: 'byok', api: 'openai-responses', baseUrl: 'https://x/v1', apiKeyEnv: 'K' }, ['a', 'b'], efforts);+ assert.deepEqual(json.providers.byok.models.map((m) => [m.id, m.reasoning]), [['a', false], ['b', false]]);+ const thinking = json.providers[THINKING('byok')].models;+ assert.deepEqual(thinking.map((m) => m.id), ['a'], 'only a model given levels thinks');+ assert.deepEqual(thinking[0].thinkingLevelMap, { off: 'none', minimal: null, low: 'low', medium: null, high: null, xhigh: null, max: 'max' });+ /* Anthropic's API is told off as off */+ const claude = modelsJson({ id: 'byok', api: 'anthropic-messages', baseUrl: 'https://x', apiKeyEnv: 'K' }, ['a'], efforts);+ assert.equal(claude.providers[THINKING('byok')].models[0].thinkingLevelMap.off, 'off');+ /* nobody thinks: no thinking service */+ assert.equal(modelsJson({ id: 'byok', api: 'openai-responses', baseUrl: 'https://x/v1', apiKeyEnv: 'K' }, ['b'], efforts).providers[THINKING('byok')], undefined);+});++test('the effort option: for a model offering more than one, its levels named', () => {+ assert.equal(effortOption(['auto'], 'auto'), null);+ assert.deepEqual(effortOption(['auto', 'xhigh'], 'xhigh'), { id: 'effort', name: 'Effort', category: 'thought_level', type: 'select', currentValue: 'xhigh',+ options: [{ value: 'auto', name: 'Auto' }, { value: 'xhigh', name: 'Extra high' }] });+});diff --git a/apps/desktop/src/studio-process.mjs b/apps/desktop/src/studio-process.mjsindex f6a2ffa..72b73d4 100644--- a/apps/desktop/src/studio-process.mjs+++ b/apps/desktop/src/studio-process.mjs@@ -54,7 +54,7 @@ try {process.exit(1);}-send({ type: 'ffmpeg', found: ['ffmpeg', 'ffprobe'].every((bin) => !spawnSync(bin, ['-version'], { stdio: 'ignore' }).error) });+send({ type: 'ffmpeg', found: ['ffmpeg', 'ffprobe'].every((bin) => !spawnSync(bin, ['-version'], { stdio: 'ignore', windowsHide: true }).error) });/* Studio draws pages' pictures in Playwright's headless browser: download it once, the first time (about 100 MB) */try {@@ -66,7 +66,7 @@ try {send({ type: 'browser', state: 'downloading' });const openfilm = dirname(createRequire(import.meta.url).resolve('openfilm/package.json'));const cli = join(dirname(createRequire(join(openfilm, 'package.json')).resolve('playwright-core/package.json')), 'cli.js');- const child = spawn(process.execPath, [cli, 'install', 'chromium-headless-shell'], { env: { ...process.env, ELECTRON_RUN_AS_NODE: '1' }, stdio: 'ignore' });+ const child = spawn(process.execPath, [cli, 'install', 'chromium-headless-shell'], { env: { ...process.env, ELECTRON_RUN_AS_NODE: '1' }, stdio: 'ignore', windowsHide: true });child.once('exit', (code) => send(code === 0 ? { type: 'browser', state: 'ready' } : { type: 'browser', state: 'failed', message: `the download stopped (${code})` }));child.once('error', (error) => send({ type: 'browser', state: 'failed', message: error.message }));}diff --git a/apps/desktop/src/studio-refs.test.mjs b/apps/desktop/src/studio-refs.test.mjsindex da2549e..18f168a 100644--- a/apps/desktop/src/studio-refs.test.mjs+++ b/apps/desktop/src/studio-refs.test.mjs@@ -45,6 +45,7 @@ test('a pill keeps its picture and what it points at, and the pill alone takes tassert.equal('image' in pill.target, false);assert.deepEqual(refFromPill(JSON.parse(JSON.stringify(pill))), { kind: 'region', time: 15.4, box: region.box, clipIds: ['s3'], texts: ['OpenFilm'] });assert.equal(refPill(undefined, range).label, '12.6–17.2 s');+ assert.equal(refPill(undefined, clip).detail, undefined, 'a clip on the timeline: no time on its pill');assert.equal(refPill(undefined, track).kind, 'track');assert.equal(refPill(undefined, file).detail, '1200×400');assert.equal(refPill('kept', moment).id, 'kept');@@ -91,7 +92,7 @@ test('a subtitle line, or words picked in it, is its span, its words, its languaconst pill = refPill(undefined, { ...line, text: 'a line far longer than any pill has room for, said slowly' });assert.equal(pill.kind, 'subtitle');assert.equal(pill.label, 'a line far longer than any pill has roo…');- assert.equal(pill.detail, '12.4–15.1 s');+ assert.equal(pill.detail, undefined, 'its span is on the card, not the pill');assert.notEqual(refPill(undefined, words).id, refPill(undefined, line).id);const card = pillCard(refPill(undefined, words), (path) => path);assert.equal(card.title, 'refCard.title.subtitle');diff --git a/packages/openfilm/src/host.mjs b/packages/openfilm/src/host.mjsindex 326dc6c..e358b45 100644--- a/packages/openfilm/src/host.mjs+++ b/packages/openfilm/src/host.mjs@@ -27,6 +27,8 @@ export const HOST_FLAG = `window.filmHost={version:'0.1',name:'film'};${SEEK_INSconst TIMELINE = readFileSync(fileURLToPath(new URL('./timeline.mjs', import.meta.url)), 'utf8');/* what film.html says, as Studio reads it too: the timeline imports it from here */const FILM_DOC = readFileSync(fileURLToPath(new URL('./film-doc.mjs', import.meta.url)), 'utf8');+/* a page clip's overrides, applied: the timeline imports it from here, and so do Studio's thumbnails of a page clip */+const OVERRIDES = readFileSync(fileURLToPath(new URL('./overrides.mjs', import.meta.url)), 'utf8');/*** The start of a film, before the timeline takes its clips over: nothing shows, a page clip has the stage's size to@@ -75,6 +77,10 @@ export function folderFiles(root, { inject = `<script>${HOST_FLAG}</script>`, tires.writeHead(200, { ...extraHeaders, 'content-type': 'text/javascript; charset=utf-8', 'cache-control': 'no-store' }).end(FILM_DOC);return true;}+ if (path.endsWith('/__film/overrides.mjs')) {+ res.writeHead(200, { ...extraHeaders, 'content-type': 'text/javascript; charset=utf-8', 'cache-control': 'no-store' }).end(OVERRIDES);+ return true;+ }if (path.endsWith('/__film/frame-duration')) {/* how long a video's frames last, for the timeline's exact frames (`?of=` the video's address) */const file = fileInRoot(root, query?.get('of') ?? '');diff --git a/packages/openfilm/src/overrides.mjs b/packages/openfilm/src/overrides.mjsnew file mode 100644index 0000000..0068f9b--- /dev/null+++ b/packages/openfilm/src/overrides.mjs@@ -0,0 +1,71 @@+// Overrides (SPEC §1): a person's tweaks of elements inside a page clip, applied after the page draws a frame. The+// film's timeline applies them to each page clip it plays (timeline.mjs); Studio's thumbnails of a page clip apply them+// too (studio/server/pageframes.mjs), so the timeline's pictures of a clip show what was changed in it. `c` is the+// clip as the timeline keeps it: its page's window (`win`), what its tweaks changed (`touched`), which ones found an+// element (`found`).++/**+ * Overrides: a person's tweak of one element inside a page, applied after the page draws each frame. `found` keeps,+ * per tweak, whether its selector ever found an element (a page rewritten since may have lost it). What a tweak+ * changes is remembered as it was before (`touched`), so a tweak taken away, or changed, leaves nothing behind.+ */+export function override(c, list = []) {+ const win = c.win;+ if (!win) return;+ const touch = (el, prop) => {+ let saved = c.touched.get(el);+ if (!saved) { saved = { props: new Map(), texts: null }; c.touched.set(el, saved); }+ if (!saved.props.has(prop)) saved.props.set(prop, [el.style.getPropertyValue(prop), el.style.getPropertyPriority(prop)]);+ };+ const set = (el, prop, value, priority = '') => { touch(el, prop); el.style.setProperty(prop, value, priority); };+ for (const [i, o] of list.entries()) {+ if (o.at === undefined) continue; // the clip's own entry (its fades): not an element's+ let nodes = [];+ try { nodes = [...win.document.querySelectorAll(o.at)]; } catch { continue; }+ if (o.n) nodes = nodes.slice(o.n - 1, o.n);+ if (nodes.length) c.found[i] = true;+ for (const el of nodes) {+ if (o.t) set(el, 'translate', `${o.t[0]}px ${o.t[1]}px`);+ if (o.s != null) set(el, 'scale', Array.isArray(o.s) ? `${o.s[0]} ${o.s[1]}` : String(o.s));+ if (o.r != null) set(el, 'rotate', `${o.r}deg`);+ for (const [k, v] of Object.entries(o.style ?? {})) {+ const prop = k.replace(/[A-Z]/g, (ch) => '-' + ch.toLowerCase());+ set(el, prop, typeof v === 'number' && !/opacity|z-index|font-weight|line-height|flex/.test(prop) ? v + 'px' : String(v), 'important');+ }+ if (o.text != null) {+ const saved = (touch(el, 'translate'), c.touched.get(el));+ if ([...el.children].every((ch) => ch.localName === 'br')) {+ /* words alone, or lines between <br>s: all of them change, a <br> at each line break (SPEC §1) */+ saved.nodes ??= [...el.childNodes];+ if (linesOf(el) !== o.text) {+ const doc = el.ownerDocument;+ el.replaceChildren(...o.text.split('\n').flatMap((line, k) => (k ? [doc.createElement('br'), doc.createTextNode(line)] : [doc.createTextNode(line)])));+ }+ } else {+ const parts = [...el.childNodes].filter((n) => n.nodeType === 3 && n.data.trim());+ saved.texts ??= parts.map((n) => [n, n.data]);+ /* a heading with other inline parts (`Quiet<br><em>you</em>`): its own words change, the parts stay */+ parts.forEach((n, k) => { n.data = k ? '' : o.text; });+ }+ }+ }+ }+}++/** An element's words with a line break for each <br>, as a `text` override writes them. */+export function linesOf(el) {+ return [...el.childNodes].map((n) => (n.nodeType === 3 ? n.data : n.localName === 'br' ? '\n' : '')).join('');+}++/** Put back what a clip's tweaks changed (before a changed list is applied, or when it is taken away). */+export function restore(c) {+ for (const [el, saved] of c.touched) {+ for (const [prop, [value, priority]] of saved.props) {+ if (value) el.style.setProperty(prop, value, priority); else el.style.removeProperty(prop);+ }+ if (saved.nodes) el.replaceChildren(...saved.nodes);+ else if (saved.texts) for (const [node, data] of saved.texts) node.data = data;+ }+ c.touched = new Map();+ c.found = [];+}diff --git a/packages/openfilm/src/timeline.mjs b/packages/openfilm/src/timeline.mjsindex 5902e8a..1fc5598 100644--- a/packages/openfilm/src/timeline.mjs+++ b/packages/openfilm/src/timeline.mjs@@ -3,6 +3,7 @@// <audio> a sound for the mix. Where each plays and sits is what film-doc.mjs reads in the file, the same reading// Studio plays and exports by; how each looks is the file's own CSS. Track 0, the first <section>, is on top.import { clipFade, clipSpan, clipTag, declarations, fadeGain, filmHead, fittedFade, kindOf, parseFilm, pictureRect, readFilmFile, sourceAt } from './film-doc.mjs';+import { override, restore } from './overrides.mjs';/* the file as written (without what the host puts in it), read as every reader reads it */const own = new URL(location.href);@@ -136,72 +137,6 @@ function unfade(c) {c.faded = false;}-/**- * Overrides: a person's tweak of one element inside a page, applied after the page draws each frame. `found` keeps,- * per tweak, whether its selector ever found an element (a page rewritten since may have lost it). What a tweak- * changes is remembered as it was before (`touched`), so a tweak taken away, or changed, leaves nothing behind.- */-function override(c, list = []) {- const win = c.win;- if (!win) return;- const touch = (el, prop) => {- let saved = c.touched.get(el);- if (!saved) { saved = { props: new Map(), texts: null }; c.touched.set(el, saved); }- if (!saved.props.has(prop)) saved.props.set(prop, [el.style.getPropertyValue(prop), el.style.getPropertyPriority(prop)]);- };- const set = (el, prop, value, priority = '') => { touch(el, prop); el.style.setProperty(prop, value, priority); };- for (const [i, o] of list.entries()) {- if (o.at === undefined) continue; // the clip's own entry (its fades): not an element's- let nodes = [];- try { nodes = [...win.document.querySelectorAll(o.at)]; } catch { continue; }- if (o.n) nodes = nodes.slice(o.n - 1, o.n);- if (nodes.length) c.found[i] = true;- for (const el of nodes) {- if (o.t) set(el, 'translate', `${o.t[0]}px ${o.t[1]}px`);- if (o.s != null) set(el, 'scale', Array.isArray(o.s) ? `${o.s[0]} ${o.s[1]}` : String(o.s));- if (o.r != null) set(el, 'rotate', `${o.r}deg`);- for (const [k, v] of Object.entries(o.style ?? {})) {- const prop = k.replace(/[A-Z]/g, (ch) => '-' + ch.toLowerCase());- set(el, prop, typeof v === 'number' && !/opacity|z-index|font-weight|line-height|flex/.test(prop) ? v + 'px' : String(v), 'important');- }- if (o.text != null) {- const saved = (touch(el, 'translate'), c.touched.get(el));- if ([...el.children].every((ch) => ch.localName === 'br')) {- /* words alone, or lines between <br>s: all of them change, a <br> at each line break (SPEC §1) */- saved.nodes ??= [...el.childNodes];- if (linesOf(el) !== o.text) {- const doc = el.ownerDocument;- el.replaceChildren(...o.text.split('\n').flatMap((line, k) => (k ? [doc.createElement('br'), doc.createTextNode(line)] : [doc.createTextNode(line)])));- }- } else {- const parts = [...el.childNodes].filter((n) => n.nodeType === 3 && n.data.trim());- saved.texts ??= parts.map((n) => [n, n.data]);- /* a heading with other inline parts (`Quiet<br><em>you</em>`): its own words change, the parts stay */- parts.forEach((n, k) => { n.data = k ? '' : o.text; });- }- }- }- }-}--/** An element's words with a line break for each <br>, as a `text` override writes them. */-function linesOf(el) {- return [...el.childNodes].map((n) => (n.nodeType === 3 ? n.data : n.localName === 'br' ? '\n' : '')).join('');-}--/** Put back what a clip's tweaks changed (before a changed list is applied, or when it is taken away). */-function restore(c) {- for (const [el, saved] of c.touched) {- for (const [prop, [value, priority]] of saved.props) {- if (value) el.style.setProperty(prop, value, priority); else el.style.removeProperty(prop);- }- if (saved.nodes) el.replaceChildren(...saved.nodes);- else if (saved.texts) for (const [node, data] of saved.texts) node.data = data;- }- c.touched = new Map();- c.found = [];-}-/** `n` frames of the page's rendering: what was changed before is committed to the compositor. */const frames = (n) => new Promise((done) => { const next = () => (--n > 0 ? requestAnimationFrame(next) : done()); requestAnimationFrame(next); });diff --git a/packages/openfilm/studio/server/custom-services.mjs b/packages/openfilm/studio/server/custom-services.mjsnew file mode 100644index 0000000..d74e6de--- /dev/null+++ b/packages/openfilm/studio/server/custom-services.mjs@@ -0,0 +1,266 @@+// @ts-check+/**+ * The person's own services: any HTTP service, on this computer or elsewhere (a transcription server they run, their+ * own voice, a cloud API OpenFilm has no provider for), added in Settings → Providers with its address, a key when it+ * takes one, where the key goes, and a few words about what it does.+ *+ * `openfilm get call` sends a request to one through Studio, which adds the key (the agent never sees it) and lands+ * what comes back: a file under assets/<service>/, or text and JSON as they are, for the agent to read. OpenFilm knows+ * nothing about what a service answers; the agent does, from the person's words and the service's own docs.+ *+ * A request goes only to the address the person gave (a path under it, never another host), and carries only the+ * project's own files.+ */+import { createWriteStream } from 'node:fs';+import { mkdir, readFile, rename, rm, stat } from 'node:fs/promises';+import { basename, dirname, extname, join } from 'node:path';+import { Readable } from 'node:stream';+import { pipeline } from 'node:stream/promises';+import { clipKind } from '../../src/film-doc.mjs';+import { inside } from './files.mjs';+import { dropTranscripts } from './transcripts.mjs';++/**+ * `auth`: the header the key goes in, `{key}` standing for it (`Authorization: Bearer {key}`, `x-api-key: {key}`).+ * @typedef {{ id: string, name: string, url: string, key?: string, auth: string, note: string }} CustomService+ */++export const DEFAULT_AUTH = 'Authorization: Bearer {key}';+/** The most a file sent to a service may be. */+const MAX_SEND_BYTES = 200 << 20;+/** Text longer than this is saved as a file, its start shown. */+const MAX_SHOWN_CHARS = 20_000;+/** How long a service has to answer. */+export const CALL_TIMEOUT_SEC = 600;++const bad = (/** @type {string} */ message) => Object.assign(new Error(message), { status: 400 });+const plainText = (/** @type {unknown} */ v) => (typeof v === 'string' ? v.trim() : '');++/** A service as kept, or null when it is not one (a file edited by hand). @param {unknown} raw @returns {CustomService | null} */+function kept(raw) {+ if (!raw || typeof raw !== 'object') return null;+ const s = /** @type {Record<string, unknown>} */ (raw);+ if (typeof s.id !== 'string' || typeof s.name !== 'string' || typeof s.url !== 'string') return null;+ return {+ /* what a newer OpenFilm keeps on a service stays with it */+ ...unknownOf(s),+ id: s.id, name: s.name, url: s.url,+ ...(typeof s.key === 'string' && s.key ? { key: s.key } : {}),+ auth: typeof s.auth === 'string' && s.auth.includes('{key}') ? s.auth : DEFAULT_AUTH,+ note: typeof s.note === 'string' ? s.note : '',+ };+}++/** A service's fields this OpenFilm does not know (a newer one's). @param {Record<string, unknown>} s */+const unknownOf = (s) => Object.fromEntries(Object.entries(s).filter(([k]) => !['id', 'name', 'url', 'key', 'auth', 'note'].includes(k)));++/** The services kept in the settings file. @param {unknown} raw @returns {CustomService[]} */+export const keptServices = (raw) => (Array.isArray(raw) ? raw.map(kept).filter((s) => s !== null) : []);++/** An address the person gave: http(s), no password in it, no query or fragment; kept without a trailing slash. */+export function cleanAddress(/** @type {unknown} */ value) {+ let url;+ try { url = new URL(plainText(value)); } catch { throw bad('The address is a web address, e.g. http://localhost:8000 or https://api.example.com/v1'); }+ if (url.protocol !== 'http:' && url.protocol !== 'https:') throw bad('The address starts with http:// or https://');+ if (url.username || url.password) throw bad('Leave the key out of the address: it has its own field');+ if (url.search || url.hash) throw bad('The address is where the service is, without ? or #: the agent adds the rest');+ return `${url.origin}${url.pathname.replace(/\/+$/, '')}`;+}++/** Where the key goes: `Header-Name: … {key} …`. */+export function cleanAuth(/** @type {unknown} */ value) {+ const auth = plainText(value) || DEFAULT_AUTH;+ if (!/^[A-Za-z0-9-]+:\s*[^\r\n]*\{key\}[^\r\n]*$/.test(auth)) throw bad('Where the key goes is a header with {key} in it, e.g. Authorization: Bearer {key}');+ return auth;+}++/** A key as kept: one line, of a plausible length. */+function cleanServiceKey(/** @type {string} */ key) {+ if (key.length < 4 || key.length > 4096 || /[\s\u0000-\u001f\u007f]/.test(key)) throw bad('That does not look like a key: paste the whole key, on its own');+ return key;+}++/** The id a name is called by on the command line: `My Whisper` → `my-whisper`. */+const idOf = (/** @type {string} */ name) => name.normalize('NFKD').toLowerCase().replace(/[^\p{L}\p{N}]+/gu, '-').replace(/^-+|-+$/g, '').slice(0, 40) || 'service';++/**+ * A service added (`id` null) or changed, from what Settings sends: `name`, `url`, `key` (absent: kept as it is; '':+ * none), `auth`, `note`. Throws a sentence the page shows (status 400).+ * @param {CustomService[]} services @param {string | null} id @param {Record<string, unknown>} input+ * @returns {CustomService[]}+ */+export function withService(services, id, input) {+ const was = id === null ? null : services.find((s) => s.id === id);+ if (id !== null && !was) throw Object.assign(new Error('There is no such service'), { status: 404 });+ const name = plainText(input.name);+ if (!name || name.length > 40 || /[\u0000-\u001f]/.test(name)) throw bad('Give the service a short name (40 characters at most)');+ const others = services.filter((s) => s !== was);+ if (others.some((s) => s.name.toLowerCase() === name.toLowerCase())) throw bad(`There is a service named ${name} already`);+ let newId = was?.id ?? idOf(name);+ for (let n = 2; others.some((s) => s.id === newId); n++) newId = `${idOf(name)}-${n}`;+ const note = plainText(input.note);+ if (note.length > 600) throw bad('Say what it does in a few lines (600 characters at most)');+ const key = input.key === undefined ? was?.key : plainText(input.key) ? cleanServiceKey(plainText(input.key)) : undefined;+ /** @type {CustomService} */+ const service = { ...(was ? unknownOf(was) : {}), id: newId, name, url: cleanAddress(input.url), ...(key ? { key } : {}), auth: cleanAuth(input.auth), note };+ return was ? services.map((s) => (s === was ? service : s)) : [...services, service];+}++/** A service as Settings shows it: never its key, only its last four characters. @param {CustomService} s */+export const shownService = (s) => ({ id: s.id, name: s.name, url: s.url, auth: s.auth, note: s.note, last4: s.key ? s.key.slice(-4) : null });++/** The services as `openfilm get` lists them for the agent. @param {CustomService[]} services */+export const servicesHelp = (services) => services.map((s) => `--service ${s.id}: ${s.name} at ${s.url}${s.note ? `. ${s.note.replace(/\s+/g, ' ')}` : ''}`);++/* ── a request ───────────────────────────────────────────────────────────── */++/**+ * The address a path asks for, under the service's own: `/v1/audio/transcriptions?x=1` → its URL. Never another host,+ * never out of the service's path. @param {string} base @param {string} path+ */+export function targetOf(base, path) {+ if (!path.startsWith('/') || path.startsWith('//') || /[\\\u0000-\u001f#]/.test(path)) throw bad('--path starts with / and stays under the service, e.g. --path /v1/audio/transcriptions');+ const at = new URL(base);+ const prefix = at.pathname.replace(/\/+$/, '');+ const url = new URL(`${at.origin}${prefix}${path}`);+ if (url.origin !== at.origin || !(url.pathname === prefix || url.pathname.startsWith(`${prefix}/`)) || url.pathname.split('/').includes('..')) {+ throw bad('--path stays under the service\'s address');+ }+ return url;+}++/** A project file to send. @param {string} root @param {string} rel @param {string} flag */+async function projectBytes(root, rel, flag) {+ let abs;+ try { abs = inside(root, rel); } catch { throw bad(`${flag} ${rel} is not a path inside the project`); }+ const info = await stat(abs).catch(() => null);+ if (!info?.isFile()) throw bad(`${flag} ${rel} is not in the project`);+ if (info.size > MAX_SEND_BYTES) throw bad(`${rel} is too large to send (${Math.round(info.size / 1e6)} MB)`);+ return { bytes: await readFile(abs), name: basename(rel), type: typeOf(rel) };+}++/** `name=value` of --form and --file. @param {string} pair @param {string} flag */+function split(pair, flag) {+ const at = pair.indexOf('=');+ if (at < 1) throw bad(`${flag} is written name=value, e.g. ${flag === '--file' ? '--file file=assets/voice/1.wav' : '--form model=whisper-1'}`);+ return [pair.slice(0, at), pair.slice(at + 1)];+}++const TYPES = /** @type {Record<string, string>} */ ({+ '.wav': 'audio/wav', '.mp3': 'audio/mpeg', '.m4a': 'audio/mp4', '.aac': 'audio/aac', '.ogg': 'audio/ogg', '.opus': 'audio/opus', '.flac': 'audio/flac',+ '.mp4': 'video/mp4', '.mov': 'video/quicktime', '.webm': 'video/webm', '.png': 'image/png', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg',+ '.webp': 'image/webp', '.gif': 'image/gif', '.svg': 'image/svg+xml', '.json': 'application/json', '.txt': 'text/plain', '.vtt': 'text/vtt',+ '.srt': 'text/plain', '.html': 'text/html', '.csv': 'text/csv', '.pdf': 'application/pdf',+});+const typeOf = (/** @type {string} */ path) => TYPES[extname(path).toLowerCase()] ?? 'application/octet-stream';+/** The extension for what a service answered with. */+const extFor = (/** @type {string} */ type) => Object.entries(TYPES).find(([, t]) => t === type)?.[0]+ ?? ({ 'audio/x-wav': '.wav', 'audio/wave': '.wav', 'audio/mp3': '.mp3', 'audio/mpeg3': '.mp3', 'audio/x-flac': '.flac', 'audio/webm': '.webm' })[type] ?? '.bin';+const isText = (/** @type {string} */ type) => /^text\/|[/+]json$|\/xml$|[/+]xml$|javascript|x-www-form-urlencoded|x-ndjson/.test(type);++/**+ * Send `args` to `service` and land what it answers, for the project at `root`. Resolves with what the agent reads;+ * throws an Error whose `status` 400 is a mistake in the command, anything else what went wrong on the way.+ * @param {string} root @param {CustomService} service+ * @param {{ path?: string, method?: string, json?: string, form?: string[], file?: string[], body?: string, header?: string[], out?: string }} args+ * @param {AbortSignal} signal+ */+export async function callService(root, service, args, signal) {+ const url = targetOf(service.url, args.path ?? '/');+ const forms = args.form ?? [], files = args.file ?? [];+ const kinds = [args.json !== undefined, forms.length + files.length > 0, args.body !== undefined].filter(Boolean).length;+ if (kinds > 1) throw bad('Send one kind of body: --json, or --form and --file, or --body');+ /** @type {Record<string, string>} */+ const headers = {};+ /** @type {BodyInit | undefined} */+ let body;+ if (args.json !== undefined) {+ try { JSON.parse(args.json); } catch { throw bad('--json is JSON, e.g. --json \'{"text":"Hello"}\''); }+ body = args.json;+ headers['content-type'] = 'application/json';+ } else if (forms.length || files.length) {+ const form = new FormData();+ for (const pair of forms) { const [k, v] = split(pair, '--form'); form.append(k, v); }+ for (const pair of files) {+ const [k, rel] = split(pair, '--file');+ const f = await projectBytes(root, rel, '--file');+ form.append(k, new Blob([f.bytes], { type: f.type }), f.name);+ }+ body = form;+ } else if (args.body !== undefined) {+ const f = await projectBytes(root, args.body, '--body');+ body = new Blob([f.bytes], { type: f.type });+ headers['content-type'] = f.type;+ }+ for (const line of args.header ?? []) {+ const at = line.indexOf(':');+ const name = at > 0 ? line.slice(0, at).trim() : '';+ if (!/^[A-Za-z0-9-]+$/.test(name) || /[\r\n]/.test(line)) throw bad('--header is written "Name: value"');+ if (/^(host|content-length|connection|transfer-encoding)$/i.test(name)) throw bad(`--header ${name} is set by Studio`);+ headers[name.toLowerCase()] = line.slice(at + 1).trim();+ }+ if (service.key) {+ const at = service.auth.indexOf(':');+ headers[service.auth.slice(0, at).trim().toLowerCase()] = service.auth.slice(at + 1).trim().replaceAll('{key}', service.key);+ }+ const method = (args.method ?? (body === undefined ? 'GET' : 'POST')).toUpperCase();+ if (body !== undefined && (method === 'GET' || method === 'HEAD')) throw bad(`A ${method} request sends no body: use --method POST`);+ const hide = (/** @type {string} */ text) => (service.key ? text.replaceAll(service.key, '••••') : text);++ let res;+ try {+ res = await fetch(url, { method, headers, ...(body === undefined ? {} : { body }), signal: AbortSignal.any([signal, AbortSignal.timeout(CALL_TIMEOUT_SEC * 1000)]) });+ } catch (e) {+ if (signal.aborted) throw e;+ const why = /** @type {{ cause?: { code?: string } }} */ (e)?.cause?.code;+ throw new Error(why === 'ECONNREFUSED' ? `${service.name} is not answering at ${service.url}: is it running?`+ : /** @type {Error} */ (e)?.name === 'TimeoutError' ? `${service.name} did not answer within ${CALL_TIMEOUT_SEC} s.`+ : `${service.name} could not be reached at ${service.url} (${hide(String(/** @type {Error} */ (e)?.message ?? e))}).`);+ }+ const type = (res.headers.get('content-type') ?? '').split(';')[0].trim().toLowerCase();+ const head = `${service.name} answered ${res.status}${type ? ` (${type})` : ''}`;+ if (!res.ok) {+ const said = hide((await res.text().catch(() => '')).slice(0, 2000)).trim();+ throw new Error(`${head}${said ? `:\n${said}` : '.'}`);+ }+ const name = args.out ?? `${service.id}-${new Date().toISOString().replace(/[-:]/g, '').replace(/\..*/, '').replace('T', '-')}`;+ const dir = `assets/${service.id}`;+ if (!res.body) return `${head}, with nothing in it.`;+ /* no type said: text when its bytes are text */+ /** @type {Readable | null} */+ let stream = null;+ let text = isText(type) ? await res.text() : null;+ if (!type) {+ const bytes = Buffer.from(await res.arrayBuffer());+ try { text = bytes.includes(0) ? null : new TextDecoder('utf-8', { fatal: true }).decode(bytes); } catch { text = null; }+ stream = Readable.from([bytes]);+ }+ if (text !== null) {+ text = hide(text);+ if (text.length <= MAX_SHOWN_CHARS) return `${head}:\n${text}`;+ const rel = `${dir}/${name}${type.includes('json') ? '.json' : '.txt'}`;+ await land(root, rel, Readable.from([text]));+ return `${head}, ${Math.round(text.length / 1000)} k characters, saved as ${rel}. Its start:\n${text.slice(0, 4000)}`;+ }+ const rel = `${dir}/${name}${extFor(type)}`;+ const size = await land(root, rel, stream ?? Readable.fromWeb(/** @type {import('node:stream/web').ReadableStream} */ (res.body)), signal);+ /* a sound or video saved over the last one: the old take's subtitles are not this one's */+ const kind = clipKind(rel);+ if (kind === 'sound' || kind === 'video') await dropTranscripts(root, rel);+ return `${head}: saved as ${rel} (${size < 1e6 ? `${Math.max(1, Math.round(size / 1000))} kB` : `${(size / 1e6).toFixed(1)} MB`}).`;+}++/** Write a stream under the project's assets/ (a hidden name until it is whole); resolves with its size. */+async function land(/** @type {string} */ root, /** @type {string} */ rel, /** @type {Readable} */ from, /** @type {AbortSignal | undefined} */ signal = undefined) {+ const abs = inside(root, rel);+ await mkdir(dirname(abs), { recursive: true });+ const part = join(dirname(abs), `.${basename(abs)}.${process.pid}.part`);+ try {+ await pipeline(from, createWriteStream(part), ...(signal ? [{ signal }] : []));+ await rename(part, abs);+ } catch (e) {+ await rm(part, { force: true });+ throw e;+ }+ return (await stat(abs)).size;+}diff --git a/packages/openfilm/studio/server/custom-services.test.mjs b/packages/openfilm/studio/server/custom-services.test.mjsnew file mode 100644index 0000000..3249a70--- /dev/null+++ b/packages/openfilm/studio/server/custom-services.test.mjs@@ -0,0 +1,112 @@+import { test, before, after } from 'node:test';+import assert from 'node:assert/strict';+import { createServer } from 'node:http';+import { existsSync, mkdirSync, mkdtempSync, readFileSync, writeFileSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import { callService, shownService, targetOf, withService } from './custom-services.mjs';++/* a service of the person's own, on this computer: it says what it was sent */+let base = '';+const seen = [];+const server = createServer((req, res) => {+ const chunks = [];+ req.on('data', (c) => chunks.push(c));+ req.on('end', () => {+ const body = Buffer.concat(chunks);+ seen.push({ method: req.method, url: req.url, headers: req.headers, body });+ if (req.url === '/v1/speak') { res.writeHead(200, { 'content-type': 'audio/wav' }); res.end(Buffer.from('RIFF....WAVEfmt ')); return; }+ if (req.url === '/v1/broken') { res.writeHead(401, { 'content-type': 'application/json' }); res.end(`{"error":"bad key ${req.headers.authorization}"}`); return; }+ if (req.url === '/v1/plain') { res.writeHead(200); res.end('{"words":[]}'); return; }+ res.writeHead(200, { 'content-type': 'application/json' });+ res.end(JSON.stringify({ path: req.url, words: [{ word: 'hi', start: 0.1, end: 0.4 }] }));+ });+});+before(() => new Promise((done) => server.listen(0, '127.0.0.1', () => { base = `http://127.0.0.1:${server.address().port}`; done(); })));+after(() => server.close());++const project = () => {+ const root = mkdtempSync(join(tmpdir(), 'of-call-'));+ writeFileSync(join(root, 'film.html'), '<!doctype html>');+ mkdirSync(join(root, 'assets/voice'), { recursive: true });+ writeFileSync(join(root, 'assets/voice/1.wav'), 'RIFF-voice');+ return root;+};+const signal = new AbortController().signal;++test('a service is added with a name the command line calls it by; its key is never shown', () => {+ const [s] = withService([], null, { name: 'My Whisper', url: `${base}/v1/`, key: 'sk-local-1234', note: 'Transcribes, OpenAI style' });+ assert.equal(s.id, 'my-whisper');+ assert.equal(s.url, `${base}/v1`);+ assert.equal(s.auth, 'Authorization: Bearer {key}');+ assert.deepEqual(shownService(s), { id: 'my-whisper', name: 'My Whisper', url: `${base}/v1`, auth: 'Authorization: Bearer {key}', note: 'Transcribes, OpenAI style', last4: '1234' });+ /* changed without a key: the key stays; with '' it goes */+ assert.equal(withService([s], 'my-whisper', { name: 'My Whisper', url: base })[0].key, 'sk-local-1234');+ assert.equal(withService([s], 'my-whisper', { name: 'My Whisper', url: base, key: '' })[0].key, undefined);+ /* a second one by another name gets its own id; the same name is refused */+ assert.equal(withService([s], null, { name: 'my whisper!', url: base }).length, 2);+ assert.throws(() => withService([s], null, { name: 'MY WHISPER', url: base }), /already/);+ for (const url of ['ftp://x', 'localhost:8000', 'http://user:pw@host', 'http://host/?a=1']) assert.throws(() => withService([], null, { name: 'x', url }), url);+ assert.throws(() => withService([], null, { name: 'x', url: base, auth: 'Authorization: Bearer' }), /\{key\}/);+});++test('a request stays under the service\'s address', () => {+ assert.equal(targetOf('http://h:1/v1', '/audio/x?y=1').href, 'http://h:1/v1/audio/x?y=1');+ for (const path of ['//evil.com/x', 'http://evil.com/', '/../admin', '/a/../../b', 'audio', '/a\\b']) assert.throws(() => targetOf('http://h:1/v1', path), path);+});++test('JSON comes back as it is; the key goes in its header and never in what the agent reads', async () => {+ const root = project();+ const [s] = withService([], null, { name: 'whisper', url: `${base}/v1`, key: 'sk-local-1234' });+ const said = await callService(root, s, { path: '/audio/transcriptions', form: ['model=whisper-1', 'timestamp_granularities[]=word'], file: ['file=assets/voice/1.wav'] }, signal);+ assert.match(said, /^whisper answered 200 \(application\/json\):\n\{"path":"\/v1\/audio\/transcriptions"/);+ const got = seen.at(-1);+ assert.equal(got.method, 'POST');+ assert.equal(got.headers.authorization, 'Bearer sk-local-1234');+ assert.match(got.body.toString(), /name="file"; filename="1.wav"[\s\S]*RIFF-voice/);+ assert.match(got.body.toString(), /name="timestamp_granularities\[\]"\r\n\r\nword/);+ /* a refusal says why, with the key blanked out */+ await assert.rejects(callService(root, s, { path: '/broken' }, signal), (e) => /answered 401/.test(e.message) && !e.message.includes('sk-local-1234') && e.message.includes('••••'));+});++test('a file comes back into assets/<service>/; text without a type is read as text', async () => {+ const root = project();+ const [s] = withService([], null, { name: 'Della', url: base, auth: 'x-api-key: {key}', key: 'della-key' });+ const said = await callService(root, s, { path: '/v1/speak', json: '{"text":"Hello"}', out: 'line-1' }, signal);+ assert.match(said, /saved as assets\/della\/line-1\.wav/);+ assert.equal(readFileSync(join(root, 'assets/della/line-1.wav'), 'utf8'), 'RIFF....WAVEfmt ');+ assert.equal(seen.at(-1).headers['x-api-key'], 'della-key');+ assert.equal(seen.at(-1).headers['content-type'], 'application/json');+ assert.match(await callService(root, s, { path: '/v1/plain' }, signal), /answered 200:\n\{"words":\[\]\}/);+});++test('a voice saved over the last take takes the last take\'s subtitles to the trash', async () => {+ process.env.OPENFILM_TRASH_DIR ??= mkdtempSync(join(tmpdir(), 'of-trash-'));+ const root = project();+ const [s] = withService([], null, { name: 'Della', url: base });+ mkdirSync(join(root, 'assets/della'), { recursive: true });+ writeFileSync(join(root, 'assets/della/line-1.vtt'), 'WEBVTT\n\n00:00.000 --> 00:05.000\nThe last take\n');+ writeFileSync(join(root, 'assets/della/line-1.fr.vtt'), 'WEBVTT\n\n00:00.000 --> 00:05.000\nLa dernière\n');+ writeFileSync(join(root, 'assets/della/line-10.vtt'), 'WEBVTT\n');+ await callService(root, s, { path: '/v1/speak', json: '{}', out: 'line-1' }, signal);+ assert.equal(existsSync(join(root, 'assets/della/line-1.vtt')), false);+ assert.equal(existsSync(join(root, 'assets/della/line-1.fr.vtt')), false);+ assert.equal(existsSync(join(root, 'assets/della/line-10.vtt')), true, 'another file\'s stays');+});++test('only the project\'s files are sent, and one body at a time', async () => {+ const root = project();+ const [s] = withService([], null, { name: 'x', url: base });+ await assert.rejects(callService(root, s, { path: '/', file: ['file=../etc/passwd'] }, signal), (e) => e.status === 400);+ await assert.rejects(callService(root, s, { path: '/', file: ['file=assets/none.wav'] }, signal), /not in the project/);+ await assert.rejects(callService(root, s, { path: '/', json: '{}', form: ['a=b'] }, signal), /one kind of body/);+ await assert.rejects(callService(root, s, { path: '/', method: 'GET', json: '{}' }, signal), /POST/);+ await assert.rejects(callService(root, s, { path: '/', header: ['Host: evil.com'] }, signal), /set by Studio/);+});++test('a service that is not running says so', async () => {+ /* a port that was free a moment ago */+ const port = await new Promise((done) => { const s = createServer().listen(0, '127.0.0.1', () => { const p = s.address().port; s.close(() => done(p)); }); });+ const [s] = withService([], null, { name: 'Off', url: `http://127.0.0.1:${port}` });+ await assert.rejects(callService(project(), s, { path: '/' }, signal), new RegExp(`not answering at http://127\\.0\\.0\\.1:${port}: is it running\\?`));+});diff --git a/packages/openfilm/studio/server/film.mjs b/packages/openfilm/studio/server/film.mjsindex 1c141fe..f303fff 100644--- a/packages/openfilm/studio/server/film.mjs+++ b/packages/openfilm/studio/server/film.mjs@@ -43,6 +43,19 @@ export async function readProjectFilm(root) {return { text, rev, value: doc ? value : null, doc, problems };}+/**+ * A clip's overrides (SPEC §1), by its id, as film.html has them now: what its thumbnails are drawn with. Null when it+ * has none, or no clip has that id.+ * @param {string} root @param {string} id+ */+export async function clipOverrides(root, id) {+ const { value } = await readProjectFilm(root);+ for (const track of value?.tracks ?? []) {+ for (const clip of track.clips ?? []) if (clip.id === id) return clip.overrides?.length ? clip.overrides : null;+ }+ return null;+}+/*** film.html as Studio writes it: over `before` (its text as read), so what did not change stays as it was, through a* temporary file so no reader sees half.diff --git a/packages/openfilm/studio/server/get.mjs b/packages/openfilm/studio/server/get.mjsindex 759e8c9..8ab7cfb 100644--- a/packages/openfilm/studio/server/get.mjs+++ b/packages/openfilm/studio/server/get.mjs@@ -31,11 +31,12 @@ import { tmpdir } from 'node:os';import { basename, dirname, extname, join, resolve, sep } from 'node:path';import { FILM_FILE, clipKind } from '../../src/film-doc.mjs';import { spokenLines } from './captions.mjs';-import { readTranscript, transcriptIsCurrent, transcriptPath, writeTranscript } from './transcripts.mjs';+import { dropTranscripts, readTranscript, transcriptIsCurrent, transcriptPath, writeTranscript } from './transcripts.mjs';import { inside } from './files.mjs';import { HttpError, json, readJson } from './http.mjs';import { kindOf, modelFor, providerKey } from './keys.mjs';import { chooseProvider, listProviders, providerSettings } from './providers/index.mjs';+import { CALL_TIMEOUT_SEC, callService, servicesHelp } from './custom-services.mjs';/*** @typedef {import('./http.mjs').Req} Req @typedef {import('./http.mjs').Res} Res@@ -114,6 +115,14 @@ export const VERBS = /** @type {Record<string, VerbSpec>} */ ({dir: 'assets/fetch', timeoutSec: 120,options: { url: { value: 'url', required: true, repeat: true } },},+ call: {+ help: 'A request to one of the person\'s own services (below), with its key added by Studio. Its answer comes back as it is: text or JSON here, a file saved under assets/<service>/. What it means is yours to read.',+ timeoutSec: CALL_TIMEOUT_SEC,+ options: { service: { value: 'id', required: true }, path: { value: '/path', default: '/' },+ method: { value: 'method', choices: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'] }, json: { value: 'json' },+ form: { value: 'name=value', repeat: true }, file: { value: 'name=path', repeat: true }, body: { value: 'path' },+ header: { value: 'Name: value', repeat: true }, out: { value: 'name' } },+ },});/** Listed only for an agent without web tools of its own (see the top). */@@ -137,6 +146,10 @@ const nothing = () => 'Nothing is connected for media here: each kind works only+ 'Until then, make sound and pictures in code, or use a service the person has a key for (with its API docs), and save them under assets/. '+ `\`openfilm get <what> --help\` shows what one kind takes (${Object.keys(VERBS).filter((v) => !WEB_VERBS.includes(v) && offered(v)).join(', ')}).`;+/** What `call` says when the person has added no service of their own. */+const NO_OWN_SERVICES = 'The person has added no services of their own. In Studio, the OpenFilm logo at the top left opens Settings: '+ + 'they add one under Providers (its address, its key if it takes one, and what it does).';+/** Each kind of media, in words (finding voices is part of voice-over). */const WORDS = /** @type {Record<string, string>} */ ({tts: 'voice-over', music: 'music', sfx: 'sound effects', asr: 'transcripts', image: 'images', 'image-search': 'image search',@@ -256,6 +269,14 @@ async function projectFile(root, rel, name, want) {*/export async function runGet(root, verb, given, { via, model, yes = false, ask, tell, signal = new AbortController().signal } = {}) {const args = await checkArgs(root, verb, given);+ if (verb === 'call') {+ const { custom } = await providerSettings();+ const service = custom.find((s) => s.id === args.service);+ if (!service) throw usage(custom.length ? `There is no service ${args.service}: there is ${custom.map((s) => s.id).join(', ')}` : NO_OWN_SERVICES);+ const { service: _, ...rest } = args;+ try { return await callService(root, service, /** @type {Parameters<typeof callService>[2]} */ (rest), signal); }+ catch (e) { throw /** @type {{ status?: number }} */ (e)?.status === 400 ? usage(/** @type {Error} */ (e).message) : e; }+ }if (verb === 'asr' && args.force !== true && await transcriptIsCurrent(root, String(args.src))) {return show(await speechReceipt(root, 'asr', String(args.src), { skip: true, note: 'It has a transcript already, made since the file last changed (--force makes it again).' }));}@@ -427,6 +448,9 @@ async function land(root, verb, args, result) {if ((await lstat(abs).catch(() => null))?.isSymbolicLink()) await rm(abs);await mkdir(dirname(abs), { recursive: true });await writeFile(abs, file.bytes);+ /* a sound or video made again under its name: the old take's subtitles go (its own are written below) */+ const kind = clipKind(file.path);+ if (kind === 'sound' || kind === 'video') await dropTranscripts(root, file.path);}if (verb !== 'tts' && verb !== 'asr') return;for (const row of result.index ?? []) {@@ -487,16 +511,22 @@ function show(receipt) {export async function catalog(only, { web = false } = {}) {if (only !== undefined && !VERBS[only]) throw new HttpError(400, `there is no ${only}; there is ${Object.keys(VERBS).join(', ')}`);const settings = await providerSettings();- const listed = Object.entries(VERBS).filter(([verb]) => (only === undefined ? (web || !WEB_VERBS.includes(verb)) && offered(verb) : verb === only));+ /* the person's own services: `call`, listed with each of them, whenever there is one */+ const own = settings.custom;+ const listed = Object.entries(VERBS).filter(([verb]) => (only === undefined+ ? (verb === 'call' ? own.length > 0 : (web || !WEB_VERBS.includes(verb)) && offered(verb))+ : verb === only));const all = await Promise.all(listed.map(async ([verb, spec]) => ({- verb, help: spec.help, ...(spec.dir ? { dir: spec.dir } : {}), options: spec.options,- usable: Boolean(await chooseProvider(/** @type {Verb} */ (verb), { settings })),+ verb, help: verb === 'call' && own.length ? `${spec.help} The person's services: ${servicesHelp(own).join('; ')}` : spec.help,+ ...(spec.dir ? { dir: spec.dir } : {}), options: spec.options,+ usable: verb === 'call' ? own.length > 0 : Boolean(await chooseProvider(/** @type {Verb} */ (verb), { settings })),})));- const missing = all.filter((v) => !v.usable).map((v) => v.verb);- if (only === undefined && missing.length === all.length) return { verbs: [], note: nothing(), services: services() };+ const missing = all.filter((v) => !v.usable && v.verb !== 'call').map((v) => v.verb);+ if (only === undefined && !all.some((v) => v.usable)) return { verbs: [], note: nothing(), services: services() };+ const notes = [...(missing.length ? [notConnected(missing)] : []), ...(only === 'call' && !own.length ? [NO_OWN_SERVICES] : [])];return {verbs: all.filter((v) => only !== undefined || v.usable).map(({ usable: _, ...v }) => v),- note: missing.length ? notConnected(missing) : null,+ note: notes.length ? notes.join(' ') : null,services: services(),};}diff --git a/packages/openfilm/studio/server/get.test.mjs b/packages/openfilm/studio/server/get.test.mjsindex a3d9564..76828ef 100644--- a/packages/openfilm/studio/server/get.test.mjs+++ b/packages/openfilm/studio/server/get.test.mjs@@ -425,3 +425,38 @@ test('nothing connected: no verbs, and a note naming only what the services make/* one kind: which services make it, and where the person connects one */assert.equal((await catalog('video')).note, `Not connected here: ${unconnected('video')}. ${CONNECT_ONE}`);});++test('call: the person\'s own services are listed with what they do, and reached with their key', async () => {+ assert.equal((await catalog()).verbs.some((v) => v.verb === 'call'), false, 'listed only once there is one');+ assert.match((await catalog('call')).note, /added no services of their own/);+ await changeSettings((s) => { s.custom = [{ id: 'whisper', name: 'Whisper', url: `${base}/local`, key: 'local-key-1234', auth: 'Authorization: Bearer {key}', note: 'Transcribes, OpenAI style, word timings' }]; });+ const call = (await catalog()).verbs.find((v) => v.verb === 'call');+ assert.match(call.help, /--service whisper: Whisper at http:\/\/127\.0\.0\.1:\d+\/local\. Transcribes, OpenAI style, word timings/);+ assert.equal(call.help.includes('local-key-1234'), false);+ const root = project();+ mkdirSync(join(root, 'assets'), { recursive: true });+ writeFileSync(join(root, 'assets/vo.wav'), 'RIFF');+ refuse = () => [200, { text: 'hi', words: [{ word: 'hi', start: 0, end: 0.2 }] }];+ const said = await runGet(root, 'call', { service: 'whisper', path: '/v1/audio/transcriptions', file: 'file=assets/vo.wav', form: ['model=whisper-1'] });+ assert.match(said, /^Whisper answered 200 \(application\/json\):/);+ assert.equal(calls.at(-1).path, '/local/v1/audio/transcriptions');+ assert.equal(calls.at(-1).headers.authorization, 'Bearer local-key-1234');+ await assert.rejects(runGet(root, 'call', { service: 'nope' }), (e) => e.code === 2 && /There is no service nope: there is whisper/.test(e.message));+ await assert.rejects(runGet(root, 'call', { service: 'whisper', path: '//evil.com/' }), (e) => e.code === 2);+});++test('a voice-over spoken again without word timings: the last take\'s transcript goes, and the receipt says there is none', async () => {+ process.env.OPENFILM_TRASH_DIR ??= mkdtempSync(join(tmpdir(), 'of-trash-'));+ const root = project();+ mkdirSync(join(root, 'assets/audio/vo'), { recursive: true });+ writeFileSync(join(root, 'assets/audio/vo/intro.wav'), 'old take');+ writeFileSync(join(root, 'assets/audio/vo/intro.vtt'), 'WEBVTT\n\n00:00.000 --> 00:09.000\nThe old take, spoken slower\n');+ /* a WAV of a second of silence, as OpenAI answers */+ const wav = Buffer.alloc(44 + 32000);+ wav.write('RIFF', 0); wav.writeUInt32LE(36 + 32000, 4); wav.write('WAVEfmt ', 8); wav.writeUInt32LE(16, 16); wav.writeUInt16LE(1, 20); wav.writeUInt16LE(1, 22);+ wav.writeUInt32LE(16000, 24); wav.writeUInt32LE(32000, 28); wav.writeUInt16LE(2, 32); wav.writeUInt16LE(16, 34); wav.write('data', 36); wav.writeUInt32LE(32000, 40);+ refuse = (call) => (call.path === '/v1/audio/speech' ? [200, wav] : [404, {}]);+ const said = await runGet(root, 'tts', { text: 'Hello', voice: 'alloy', out: 'intro' }, { via: 'openai' });+ assert.equal(existsSync(join(root, 'assets/audio/vo/intro.vtt')), false);+ assert.match(said, /"transcript": "none: the service does not time words/);+});diff --git a/packages/openfilm/studio/server/keys.mjs b/packages/openfilm/studio/server/keys.mjsindex e6d5dc0..8eb0a0c 100644--- a/packages/openfilm/studio/server/keys.mjs+++ b/packages/openfilm/studio/server/keys.mjs@@ -4,10 +4,14 @@* which model (Settings → Models).** Kept in ~/.openfilm/providers.json, readable by this user only (0600, in a 0700 folder), written whole and atomically:- * { "keys": { "<provider id>": "<key>" }, "use": { "<verb>": "<provider id>" }, "model": { "<verb>": "<model id>" } }+ * { "keys": { "<provider id>": "<key>" }, "use": { "<verb>": "<provider id>" }, "model": { "<verb>": "<model id>" },+ * "custom": [ the person's own services (custom-services.mjs), each with its key ] }* A kind of media with no model (or one its provider no longer has) is made with the provider's first. Listing voices* goes with voice-over: same provider, same model.*+ * The file is shared with every OpenFilm on this computer (`npx openfilm`, the app), which may be older or newer than+ * this one: what this one does not know of it (a newer one's field) is written back as it was, never dropped.+ ** A key in the environment (the provider's variable, e.g. ELEVENLABS_API_KEY) wins over the file: it is how people* already keep keys, and Studio cannot remove it. Keys leave this process only towards their own service; the editor* page sees where a key comes from and its last four characters, never the key.@@ -17,11 +21,14 @@ import { chmod, mkdir, readFile, stat, writeFile } from 'node:fs/promises';import { dirname, join } from 'node:path';import { studioHome } from './projects.mjs';import { replaceFile } from './atomic.mjs';+import { keptServices } from './custom-services.mjs';/** @typedef {import('./providers/contract.mjs').Provider} Provider *//**- * `off`: the providers disconnected while the environment has a key for them (that key is not used).- * @typedef {{ keys: Record<string, string>, use: Record<string, string>, model: Record<string, string>, off: string[] }} ProviderSettings+ * `off`: the providers disconnected while the environment has a key for them (that key is not used). `custom`: the+ * person's own services.+ * @typedef {{ keys: Record<string, string>, use: Record<string, string>, model: Record<string, string>, off: string[],+ * custom: import('./custom-services.mjs').CustomService[] }} ProviderSettings*//** Where the keys and choices are kept: ~/.openfilm/providers.json. */@@ -43,8 +50,8 @@ export async function readSettings(file = providersFile()) {/* 'auto' was a choice once: it means none now */const use = Object.fromEntries(Object.entries(strings(raw.use)).filter(([, id]) => id !== 'auto'));const off = Array.isArray(raw.off) ? raw.off.filter((id) => typeof id === 'string') : [];- return /** @type {ProviderSettings} */ ({ keys: strings(raw.keys), use, model: strings(raw.model), off });- } catch { return /** @type {ProviderSettings} */ ({ keys: {}, use: {}, model: {}, off: [] }); }+ return /** @type {ProviderSettings} */ ({ keys: strings(raw.keys), use, model: strings(raw.model), off, custom: keptServices(raw.custom) });+ } catch { return /** @type {ProviderSettings} */ ({ keys: {}, use: {}, model: {}, off: [], custom: [] }); }}/**@@ -72,9 +79,11 @@ export function changeSettings(/** @type {(s: ProviderSettings) => void} */ editconst next = changing.catch(() => {}).then(async () => {const settings = await readSettings(file);edit(settings);+ /* what another OpenFilm keeps here, unknown to this one, stays */+ const kept = plain(await readFile(file, 'utf8').then(JSON.parse, () => null));await mkdir(dirname(file), { recursive: true, mode: 0o700 });const tmp = `${file}.${randomBytes(6).toString('hex')}.tmp`;- await writeFile(tmp, `${JSON.stringify(settings, null, 2)}\n`, { mode: 0o600, flag: 'wx' });+ await writeFile(tmp, `${JSON.stringify({ ...kept, ...settings }, null, 2)}\n`, { mode: 0o600, flag: 'wx' });await replaceFile(tmp, file);return settings;});diff --git a/packages/openfilm/studio/server/keys.test.mjs b/packages/openfilm/studio/server/keys.test.mjsindex 417ed36..e0098bb 100644--- a/packages/openfilm/studio/server/keys.test.mjs+++ b/packages/openfilm/studio/server/keys.test.mjs@@ -1,6 +1,6 @@import { test, before, after } from 'node:test';import assert from 'node:assert/strict';-import { chmodSync, mkdtempSync, readFileSync, statSync } from 'node:fs';+import { chmodSync, mkdtempSync, readFileSync, statSync, writeFileSync } from 'node:fs';import { tmpdir } from 'node:os';import { join } from 'node:path';import { changeSettings, cleanKey, modelFor, providerKey, providersFile, readSettings } from './keys.mjs';@@ -154,3 +154,17 @@ test('an own key comes with its model: the provider\'s first unless chosen, kept/* a model the provider no longer has is its first */assert.equal(modelFor({ keys: {}, use: {}, model: { tts: 'gone' } }, provider, 'tts'), first.id);});++test('what a newer OpenFilm keeps in providers.json stays when this one writes it', async () => {+ const file = join(mkdtempSync(join(tmpdir(), 'of-keys-newer-')), 'providers.json');+ writeFileSync(file, JSON.stringify({+ keys: { elevenlabs: 'xi-kept-0123456789' }, use: {}, model: {}, off: [], spending: { ask: 'video' },+ custom: [{ id: 'whisper', name: 'Whisper', url: 'http://localhost:8000', auth: 'Authorization: Bearer {key}', note: '', askFirst: true }],+ }));+ await changeSettings((s) => { s.keys.openai = 'sk-new-0123456789'; s.custom[0].note = 'Transcribes'; }, file);+ const written = JSON.parse(readFileSync(file, 'utf8'));+ assert.deepEqual(written.spending, { ask: 'video' });+ assert.equal(written.custom[0].askFirst, true);+ assert.equal(written.custom[0].note, 'Transcribes');+ assert.equal(written.keys.openai, 'sk-new-0123456789');+});diff --git a/packages/openfilm/studio/server/pageframes.mjs b/packages/openfilm/studio/server/pageframes.mjsindex c7e09a9..8bc3dd8 100644--- a/packages/openfilm/studio/server/pageframes.mjs+++ b/packages/openfilm/studio/server/pageframes.mjs@@ -54,17 +54,20 @@ export const FILM_PAGE = FILM_FILE;/*** The page `rel` of the folder served at `base` (its film-origin URL), drawn at `ms`, `width` px wide, as JPEG.- * `ms` past the page's end draws its last frame; `ms` < 0 draws the middle (its poster).+ * `ms` past the page's end draws its last frame; `ms` < 0 draws the middle (its poster). `overrides`: a page clip's+ * (SPEC §1), drawn with it, so its pictures on the timeline show what was changed in it.* @param {string} root @param {string} base @param {string} rel @param {number} ms @param {number} width+ * @param {object[] | null} [overrides]*/-export async function pageFrame(root, base, rel, ms, width) {+export async function pageFrame(root, base, rel, ms, width, overrides = null) {const w = Math.max(32, Math.min(960, Math.round(width / 2) * 2 || 160));/* `rel` is the request's: a page of the project only, never one reached by `../` or a link out of the folder */const page = inProject(root, rel);const info = page ? await stat(page).catch(() => null) : null;if (!info?.isFile()) throw new PageFrameError(`${rel} is not there`);/* `black`: how frames are drawn (frames drawn on white before are not reused) */- const key = createHash('sha256').update(`black\0${rel}\0${Math.round(ms)}\0${w}`).digest('hex').slice(0, 24);+ const tweaks = overrides?.some((o) => o && typeof o === 'object' && 'at' in o) ? JSON.stringify(overrides) : '';+ const key = createHash('sha256').update(`black\0${rel}\0${Math.round(ms)}\0${w}${tweaks ? `\0${tweaks}` : ''}`).digest('hex').slice(0, 24);const dir = join(root, TOOL_DIR, 'cache', 'pageframes');const file = join(dir, `${key}.jpg`);/* a frame is good while every file the page loaded to draw it is as it was then: the page, and its styles, code,@@ -85,7 +88,9 @@ export async function pageFrame(root, base, rel, ms, width) {const pending = drawing.get(id);if (pending) return pending;const made = (async () => {- const { jpeg, duration, loaded } = await draw(pageUrl(base, rel), ms, w, version, base).catch((e) => {+ /* a page drawn with tweaks is one of its own: the page kept open for another clip of the same file has none */+ const drawn = tweaks ? `${version}:${createHash('sha256').update(tweaks).digest('hex').slice(0, 12)}` : version;+ const { jpeg, duration, loaded } = await draw(pageUrl(base, rel), ms, w, drawn, base, tweaks ? overrides : null).catch((e) => {failures.set(id, { error: e, at: Date.now() });if (failures.size > 500) failures.delete(failures.keys().next().value ?? '');throw e;@@ -187,7 +192,7 @@ async function closeBrowser() {* Draw `url` at `ms`, `w` px wide. `loaded`: the project's files the page (and every page inside it) loaded, project* relative (`base`: where the project is served), so the frame is known stale when one of them changes.*/-function draw(/** @type {string} */ url, /** @type {number} */ ms, /** @type {number} */ w, /** @type {string} */ version, /** @type {string} */ base) {+function draw(/** @type {string} */ url, /** @type {number} */ ms, /** @type {number} */ w, /** @type {string} */ version, /** @type {string} */ base, /** @type {object[] | null} */ overrides = null) {return withPage(url, w, version, async (page) => {const duration = lengthOf(page);const end = duration != null ? Math.max(0, duration - 1e-3) : Infinity;@@ -197,6 +202,13 @@ function draw(/** @type {string} */ url, /** @type {number} */ ms, /** @type {nulet jpeg = null;for (const t of moments) {await page.seek(Math.max(0, t), { timeoutMs: FRAME_MS });+ /* the clip's tweaks, as the film's timeline applies them after each frame (src/overrides.mjs) */+ if (overrides) {+ await page.page.evaluate(async (list) => {+ const { override } = await import(new URL('__film/overrides.mjs', location.href).href);+ override({ win: window, touched: new Map(), found: [] }, list);+ }, overrides);+ }const shot = await page.capture({ format: 'jpeg', quality: 70 });if (!jpeg || shot.length > jpeg.length) jpeg = shot;}diff --git a/packages/openfilm/studio/server/pageframes.test.mjs b/packages/openfilm/studio/server/pageframes.test.mjsindex f574338..ccb26e6 100644--- a/packages/openfilm/studio/server/pageframes.test.mjs+++ b/packages/openfilm/studio/server/pageframes.test.mjs@@ -81,6 +81,22 @@ test('a page\'s thumbnail follows the files the page loads: a stylesheet it sharassert.ok(!blue.body.equals(red.body), 'the page drawn again with its new style');});+test('a page clip\'s thumbnail shows the changes made in it (its overrides): another change, another picture', async () => {+ const { project } = await (await api('/api/projects', { method: 'POST', body: { path: join(scratch, 'Tweaked') } })).json();+ writeFileSync(join(project.path, 'p.html'), '<!doctype html><body style="margin:0;background:#fff"><div id="box" style="width:960px;height:1080px;background:#f00"></div><script>window.film = { duration: 2, frame() {} };</script></body>');+ const film = (overrides) => writeFileSync(join(project.path, 'film.html'), filmHtml({ tracks: [{ clips: [{ id: 'p', src: 'p.html', ...(overrides ? { overrides } : {}) }] }] }));+ const frame = async (clip) => Buffer.from(await (await api(`/api/projects/${project.id}/media?what=frame&path=p.html&ms=500&w=160${clip ? '&clip=p' : ''}`)).arrayBuffer());+ film(null);+ const plain = await frame(true);+ assert.ok(plain.equals(await frame(false)), 'no changes: the page as it is');+ film([{ at: '#box', t: [960, 0] }]);+ const moved = await frame(true);+ assert.ok(!moved.equals(plain), 'the box moved across');+ assert.ok((await frame(false)).equals(plain), 'the page itself, asked without its clip, is as it was');+ film([{ at: '#box', style: { backgroundColor: '#00f' } }]);+ assert.ok(!(await frame(true)).equals(moved), 'changed again: drawn again');+});+test('a page that never gets ready (a loop that never ends) fails in its own time, and the pages after it are drawn', async () => {const { project } = await (await api('/api/projects', { method: 'POST', body: { path: join(scratch, 'Stuck') } })).json();writeFileSync(join(project.path, 'loop.html'), '<!doctype html><body><script>window.film = { duration: 2, ready: new Promise(() => setTimeout(() => { for (;;) {} }, 100)), frame() {} };</script></body>');diff --git a/packages/openfilm/studio/server/prefs.mjs b/packages/openfilm/studio/server/prefs.mjsindex 8c2411b..c2f7280 100644--- a/packages/openfilm/studio/server/prefs.mjs+++ b/packages/openfilm/studio/server/prefs.mjs@@ -37,14 +37,20 @@ export function readPrefs() {try { return prefsOf(JSON.parse(readFileSync(file(), 'utf8'))); } catch { return prefsOf(null); }}-/** Change some of them (a field at a time); returns them all. @param {unknown} patch @returns {Prefs} */+/**+ * Change some of them (a field at a time); returns them all. The file is shared with every OpenFilm on this computer+ * (`npx openfilm`, the app), older or newer: what this one does not know of it is written back as it was.+ * @param {unknown} patch @returns {Prefs}+ */export function writePrefs(patch) {const now = readPrefs();const asked = /** @type {{ proxies?: object, seenVersion?: unknown }} */ (patch && typeof patch === 'object' ? patch : {});const next = prefsOf({ ...now, ...asked, proxies: { ...now.proxies, ...(asked.proxies ?? {}) } });const at = file();+ let kept = {};+ try { const raw = JSON.parse(readFileSync(at, 'utf8')); if (raw && typeof raw === 'object' && !Array.isArray(raw)) kept = raw; } catch { /* none yet */ }mkdirSync(studioHome(), { recursive: true });- writeFileSync(`${at}.${process.pid}.tmp`, `${JSON.stringify(next, null, 2)}\n`);+ writeFileSync(`${at}.${process.pid}.tmp`, `${JSON.stringify({ ...kept, ...next, proxies: { ...(/** @type {{ proxies?: object }} */ (kept).proxies ?? {}), ...next.proxies } }, null, 2)}\n`);renameSync(`${at}.${process.pid}.tmp`, at);return next;}diff --git a/packages/openfilm/studio/server/prefs.test.mjs b/packages/openfilm/studio/server/prefs.test.mjsindex a682768..032761b 100644--- a/packages/openfilm/studio/server/prefs.test.mjs+++ b/packages/openfilm/studio/server/prefs.test.mjs@@ -1,6 +1,9 @@import { test } from 'node:test';import assert from 'node:assert/strict';-import { prefsOf } from './prefs.mjs';+import { mkdtempSync, readFileSync, writeFileSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import { prefsOf, writePrefs } from './prefs.mjs';test('the version whose notes were last shown: a version, else none', () => {assert.equal(prefsOf(null).seenVersion, null, 'a new install has none');@@ -10,3 +13,19 @@ test('the version whose notes were last shown: a version, else none', () => {assert.equal(prefsOf({ seenVersion: 7 }).seenVersion, null);assert.deepEqual(prefsOf({ seenVersion: '0.1.6' }).proxies, { on: true, height: 720 }, 'the rest as before');});++test('what a newer OpenFilm keeps in prefs.json stays when this one writes it', () => {+ const home = mkdtempSync(join(tmpdir(), 'of-prefs-'));+ const was = process.env.OPENFILM_HOME;+ process.env.OPENFILM_HOME = home;+ try {+ writeFileSync(join(home, 'prefs.json'), JSON.stringify({ proxies: { on: true, height: 720, codec: 'h265' }, seenVersion: null, theme: 'dark' }));+ writePrefs({ proxies: { on: false } });+ const written = JSON.parse(readFileSync(join(home, 'prefs.json'), 'utf8'));+ assert.equal(written.theme, 'dark');+ assert.equal(written.proxies.codec, 'h265');+ assert.equal(written.proxies.on, false);+ } finally {+ if (was === undefined) delete process.env.OPENFILM_HOME; else process.env.OPENFILM_HOME = was;+ }+});diff --git a/packages/openfilm/studio/server/projects.mjs b/packages/openfilm/studio/server/projects.mjsindex ef4ce2f..0f35322 100644--- a/packages/openfilm/studio/server/projects.mjs+++ b/packages/openfilm/studio/server/projects.mjs@@ -78,10 +78,13 @@ export async function readState() {} catch { return /** @type {StudioState} */ ({ recent: [], folders: [] }); }}+/* the file is shared with every OpenFilm on this computer (`npx openfilm`, the app), older or newer: what this one does+ not know of it is written back as it was */async function writeState(/** @type {StudioState} */ state) {await mkdir(studioHome(), { recursive: true });const path = join(studioHome(), 'studio.json');- await writeAtomic(path, `${JSON.stringify(state, null, 2)}\n`);+ const kept = await readFile(path, 'utf8').then(JSON.parse, () => null);+ await writeAtomic(path, `${JSON.stringify({ ...(kept && typeof kept === 'object' && !Array.isArray(kept) ? kept : {}), ...state }, null, 2)}\n`);}/** Folders that are never a project: the disk's root, the home folder, and Studio's own. */diff --git a/packages/openfilm/studio/server/projects.test.mjs b/packages/openfilm/studio/server/projects.test.mjsindex 0d95593..7afee15 100644--- a/packages/openfilm/studio/server/projects.test.mjs+++ b/packages/openfilm/studio/server/projects.test.mjs@@ -136,3 +136,12 @@ test('a moved project is located: same id and place in the list, its history witassert.equal(readFileSync(join(scratch, 'q2', '.film', 'id'), 'utf8').trim(), q.id);assert.equal((await openProject(join(scratch, 'q2'))).id, q.id, 'opened again, it is the same project');});++test('what a newer OpenFilm keeps in studio.json stays when this one writes it', async () => {+ mkdirSync(process.env.OPENFILM_HOME, { recursive: true });+ writeFileSync(join(process.env.OPENFILM_HOME, 'studio.json'), JSON.stringify({ recent: [], folders: [], pinned: ['abc'] }));+ await openProject(join(scratch, 'Launch'));+ const written = JSON.parse(readFileSync(join(process.env.OPENFILM_HOME, 'studio.json'), 'utf8'));+ assert.deepEqual(written.pinned, ['abc']);+ assert.equal(written.recent.length, 1);+});diff --git a/packages/openfilm/studio/server/providers/index.mjs b/packages/openfilm/studio/server/providers/index.mjsindex 1e5eb39..7c1a287 100644--- a/packages/openfilm/studio/server/providers/index.mjs+++ b/packages/openfilm/studio/server/providers/index.mjs@@ -14,6 +14,7 @@*/import { HttpError, json, readJson } from '../http.mjs';import { changeSettings, cleanKey, envKey, kindOf, modelFor, providerKey, readSettings } from '../keys.mjs';+import { shownService, withService } from '../custom-services.mjs';import elevenlabs from './elevenlabs.mjs';import openai from './openai.mjs';import google from './google.mjs';@@ -118,6 +119,7 @@ async function state() {return { id: p.id, name: p.name, verbs: p.verbs, models: p.models ?? {}, keysUrl: p.keysUrl, env: key?.env ?? inEnv?.env ?? p.env, envKey: Boolean(inEnv), source: key?.source ?? null, last4: key ? key.key.slice(-4) : null };}),...shownChoices(settings),+ custom: settings.custom.map(shownService),};}@@ -134,11 +136,31 @@ async function state() {* PUT /api/providers/use { verb, provider, model? } who serves that kind of media (an id, or null: none),* and with which of its models (its first when none is given)* POST /api/providers/:id/billing → {}, the provider's own page for its balance opened in the person's browser+ * POST /api/providers/custom { name, url, key?, auth?, note? } one of the person's own services added+ * (custom-services.mjs); the state has `custom: [{ id, name, url, auth, note,+ * last4 }]`+ * PUT /api/providers/custom/:id the same fields, changed (no `key`: kept as it is; '': none)+ * DELETE /api/providers/custom/:id taken away* @param {Req} req @param {Res} res @param {string[]} parts the path after /api/providers* @param {{ openBrowser: (url: string) => void }} options*/export async function providersRoute(req, res, parts, { openBrowser }) {if (!parts.length && req.method === 'GET') return json(res, 200, await state());+ if (parts[0] === 'custom' && parts.length <= 2) {+ const id = parts[1] ?? null;+ if (id === null ? req.method !== 'POST' : req.method !== 'PUT' && req.method !== 'DELETE') throw new HttpError(405, 'POST to add, PUT or DELETE one');+ const input = req.method === 'DELETE' ? {} : /** @type {Record<string, unknown>} */ (await readJson(req));+ try {+ await changeSettings((s) => {+ s.custom = req.method === 'DELETE' ? s.custom.filter((c) => c.id !== id) : withService(s.custom, id, input);+ });+ } catch (e) {+ const status = /** @type {{ status?: number }} */ (e)?.status;+ if (status) throw new HttpError(status, /** @type {Error} */ (e).message);+ throw e;+ }+ return json(res, 200, await state());+ }if (parts.length === 2 && parts[1] === 'billing' && req.method === 'POST') {const provider = OWN.find((p) => p.id === parts[0]);if (!provider) throw new HttpError(404, 'no such provider');diff --git a/packages/openfilm/studio/server/server.mjs b/packages/openfilm/studio/server/server.mjsindex ad3a1d4..d84d710 100644--- a/packages/openfilm/studio/server/server.mjs+++ b/packages/openfilm/studio/server/server.mjs@@ -20,7 +20,7 @@ import { createServer } from 'node:http';import { mkdir, readFile } from 'node:fs/promises';import { createHash } from 'node:crypto';import { spawn } from 'node:child_process';-import { EditError, editProjectFilm, readProjectFilm } from './film.mjs';+import { EditError, clipOverrides, editProjectFilm, readProjectFilm } from './film.mjs';import { FileError, bringIn, collect, extractAudio, freezeFrame, importFile, inside, listFiles, makeFolder, moveCheck, moveFile, readable, relink, trashFile } from './files.mjs';import { ExportError, createExports, draftApps, exportCapabilities, exportsRoot, projectSizes } from './exports.mjs';import { MediaError, frame, loudness, poster, probe, waveform } from './derived.mjs';@@ -557,9 +557,12 @@ export async function startStudio({ port = 4747, key, openBrowser = systemBrowseconst to = url.searchParams.get('to');return json(res, 200, await loudness(project.path, at, Number(url.searchParams.get('from') ?? 0), to == null ? undefined : Number(to)));}- /* a web page has no file to cut a frame from: it is drawn, as an export draws it (pageframes.mjs) */+ /* a web page has no file to cut a frame from: it is drawn, as an export draws it (pageframes.mjs); a page clip's+ frame (`clip`, its id) with the person's changes in it, as the film shows them */+ const clipId = what === 'frame' ? url.searchParams.get('clip') : null;+ const tweaks = clipId && isPage(at) ? await clipOverrides(project.path, clipId) : null;const page = isPage(at) && (what === 'poster' || what === 'frame')- ? () => pageFrame(project.path, pages.folderUrl(project), at, what === 'poster' ? -1 : Number(url.searchParams.get('ms')), what === 'poster' ? 480 : Number(url.searchParams.get('w'))).catch((e) => {+ ? () => pageFrame(project.path, pages.folderUrl(project), at, what === 'poster' ? -1 : Number(url.searchParams.get('ms')), what === 'poster' ? 480 : Number(url.searchParams.get('w')), tweaks).catch((e) => {throw new HttpError(e instanceof PageFrameError ? 404 : 422, String(e?.message ?? e));}): null;diff --git a/packages/openfilm/studio/server/stage.js b/packages/openfilm/studio/server/stage.jsindex a966f6a..2fff0ee 100644--- a/packages/openfilm/studio/server/stage.js+++ b/packages/openfilm/studio/server/stage.js@@ -151,10 +151,48 @@const ICON = new Set(['svg', 'img']);/** Words alone, or lines between <br>s: a `text` override changes all of them, a <br> at each line break. */const onlyBreaks = (el) => [...el.children].every((ch) => ch.localName === 'br');- const textOf = (el) => {- if (onlyBreaks(el)) return stageText(el);- if (![...el.children].every((c) => INLINE.has(c.tagName) || ICON.has(c.localName))) return '';- return ownWords(el);+ /* words of its own beside blocks of their own (a title with its subtitle written inside it) are its text too: the+ blocks are layers of their own, and a text override changes its own words only */+ const textOf = (el) => (onlyBreaks(el) ? stageText(el) : ownWords(el));+ /** The blocks written inside a line of words (a subtitle inside its title): layers of their own. */+ const blocksOf = (el) => [...el.children].filter((ch) => !INLINE.has(ch.tagName) && !ICON.has(ch.localName) && !SKIP.has(ch.localName.toLowerCase()));+ /** Those shown now: still shown while the line's own words are typed. */+ const blocksIn = (el) => blocksOf(el).filter((ch) => css(ch).visibility === 'visible');+ /**+ * A line of words with blocks written inside it: its own words are the layer (its box hugs them, the inspector shows+ * them), and the blocks in it stay where they are and as they look when it is moved or restyled (holdBlocks).+ */+ const mixedText = (el) => el.namespaceURI !== SVG_NS && !onlyBreaks(el) && blocksOf(el).length > 0 && Boolean(ownWords(el));+ /** The box of an element's own words on its page (the union of their lines' boxes). */+ const wordsRect = (el) => {+ const range = el.ownerDocument.createRange();+ let l = Infinity; let t = Infinity; let r = -Infinity; let b = -Infinity;+ for (const n of el.childNodes) {+ if (n.nodeType !== 3 || !n.data.trim()) continue;+ range.selectNodeContents(n);+ for (const x of range.getClientRects()) {+ if (x.width < 0.5 && x.height < 0.5) continue;+ l = Math.min(l, x.left); t = Math.min(t, x.top); r = Math.max(r, x.right); b = Math.max(b, x.bottom);+ }+ }+ if (l < r && t < b) return { left: l, top: t, width: r - l, height: b - t };+ const x = el.getBoundingClientRect();+ return { left: x.left, top: x.top, width: x.width, height: x.height };+ };+ /** The size of an element's own words as laid out, before any transform (each run measured in a span of its own). */+ const wordsSize = (el) => {+ const doc = el.ownerDocument;+ let l = Infinity; let t = Infinity; let r = -Infinity; let b = -Infinity;+ for (const n of [...el.childNodes]) {+ if (n.nodeType !== 3 || !n.data.trim()) continue;+ const span = doc.createElement('span');+ n.before(span);+ span.append(n);+ l = Math.min(l, span.offsetLeft); t = Math.min(t, span.offsetTop);+ r = Math.max(r, span.offsetLeft + span.offsetWidth); b = Math.max(b, span.offsetTop + span.offsetHeight);+ span.replaceWith(n);+ }+ return l < r && t < b ? { w: r - l, h: b - t } : { w: el.offsetWidth, h: el.offsetHeight };};const transparent = (color) => {@@ -228,6 +266,7 @@}if (l < r && t < b) return { left: l, top: t, width: r - l, height: b - t };}+ if (mixedText(el)) return wordsRect(el);const x = el.getBoundingClientRect();return { left: x.left, top: x.top, width: x.width, height: x.height };};@@ -237,11 +276,12 @@* aspect (times the override's scale) and its turn, so whatever scaled it is counted.*/const measureFrame = (el, s, r) => {- const box = el.getBoundingClientRect();+ const mixed = mixedText(el);+ const box = mixed ? wordsRect(el) : el.getBoundingClientRect();const cx = box.left + box.width / 2;const cy = box.top + box.height / 2;- let lw = el.offsetWidth;- let lh = el.offsetHeight;+ if (mixed && Math.abs(r % 360) < 0.01) return { cx, cy, w: box.width, h: box.height, r: 0 };+ let { w: lw, h: lh } = mixed ? wordsSize(el) : { w: el.offsetWidth, h: el.offsetHeight };if (!(lw > 0 && lh > 0)) {try { const bb = el.getBBox?.(); lw = bb?.width ?? 0; lh = bb?.height ?? 0; } catch { lw = 0; lh = 0; }}@@ -306,6 +346,11 @@return { at, n, loc: `${at}#${n}`, instance: n, instances: all.length };};const targetOf = (el) => { const { at, n } = locOf(el); return n ? { at, n } : { at }; };+ /** Layers like it, wherever they are: its tag and classes (`h2.title`), or null when it has no class to go by. */+ const similarOf = (el) => {+ const classes = [...el.classList].filter((c) => /^[A-Za-z_][\w-]*$/.test(c)).slice(0, 3);+ return classes.length ? el.localName + classes.map((c) => `.${CSS.escape(c)}`).join('') : null;+ };/** The clip's kept override that applies to this element (the last one that finds it). */const overrideOf = (c, el) => {@@ -512,11 +557,16 @@/** All layers under a point of a clip's page, the smallest first (equal areas: the one drawn later first). */const layersAt = (c, x, y, includeLocked) => {+ const doc = c.win.document;+ /* what is really at the point: a line's own stroke, not the box around it (a diagonal line's box covers the words+ beside it, and as the smaller box it was taken for them) */+ const under = seeingAll(doc, () => doc.elementsFromPoint(x, y));const hits = [];- layersOf(c.win.document).forEach((el, order) => {- const rect = el.getBoundingClientRect();+ layersOf(doc).forEach((el, order) => {+ const rect = mixedText(el) ? wordsRect(el) : el.getBoundingClientRect();if (rect.width < 1 || rect.height < 1) return;if (x < rect.left || x > rect.right || y < rect.top || y > rect.bottom) return;+ if (!under.some((u) => el === u || el.contains(u))) return;const frame = frameOf(el, rect);if (frame.r && !inFrame(frame, x, y)) return;const kind = kindOf(el);@@ -563,6 +613,7 @@...where,kind,tag: el.localName.toLowerCase(),+ ...(similarOf(el) ? { similar: similarOf(el) } : {}),label: labelOf(el, kind),...(words ? { text: { value: words, shape: 'value' } } : {}),style: styleOf(el, kind),@@ -610,6 +661,8 @@if (!d) return;const list = (c.overrides ?? []).map((o) => ({ ...o, ...(o.style ? { style: { ...o.style } } : {}) }));for (const { target, patch } of d.items.values()) {+ /* null: the override taken away */+ if (patch === null) { const i = list.findIndex((x) => sameTarget(x, target)); if (i >= 0) list.splice(i, 1); continue; }let o = list.find((x) => sameTarget(x, target));if (!o) { o = { at: target.at, ...(target.n ? { n: target.n } : {}) }; list.push(o); }if (patch.t) o.t = patch.t;@@ -624,13 +677,67 @@}editor()?.preview(c.id, list);};+ const draftKey = (target) => `${target.at}\u0000${target.n ?? ''}`;+ /** Show a change of a layer (`patch` null: its override taken away); the blocks written inside it stay put. */const draft = (c, target, patch) => {let d = drafts.get(c.id);if (!d) { d = { base: JSON.stringify(c.overrides ?? []), items: new Map() }; drafts.set(c.id, d); }- const key = `${target.at}\u0000${target.n ?? ''}`;+ const el = c.win ? matches(c.win.document, target)[0] : null;+ const blocks = el && mixedText(el) ? blocksOf(el).map(blockNow) : null;+ const g0 = blocks ? paintedOf(el) : null;+ const key = draftKey(target);const prev = d.items.get(key)?.patch ?? {};- d.items.set(key, { target, patch: { ...prev, ...patch, ...(prev.style || patch.style ? { style: { ...(prev.style ?? {}), ...(patch.style ?? {}) } } : {}) } });+ d.items.set(key, { target, patch: patch === null ? null : { ...prev, ...patch, ...(prev.style || patch.style ? { style: { ...(prev.style ?? {}), ...(patch.style ?? {}) } } : {}) } });show(c);+ if (blocks) holdBlocks(c, blocks, g0, paintedOf(el));+ };+ /* what a block keeps when the line it is written in changes: its look (the type it takes from the line) and its place */+ const INHERITED = ['color', 'fontFamily', 'fontSize', 'fontWeight', 'fontStyle', 'letterSpacing', 'lineHeight', 'wordSpacing', 'textTransform', 'textAlign', 'textShadow', 'fontVariant'];+ const blockNow = (b) => {+ const cs = css(b);+ const turn = totalTurn(b);+ const f = measureFrame(b, paintedOf(b).s, turn);+ const box = b.getBoundingClientRect();+ /* held by the side its words start from (a block's width follows the line's, its words do not) */+ const align = cs.textAlign;+ const ax = /center/.test(align) ? 0.5 : /right|end/.test(align) ? 1 : 0;+ return {+ b, turn, ax, look: Object.fromEntries(INHERITED.map((k) => [k, cs[k]])),+ box: { left: box.left, top: box.top, width: box.width, height: box.height },+ frame: { w: f.w, h: f.h, ow: b.offsetWidth, oh: b.offsetHeight },+ };+ };+ /**+ * The blocks inside a line put back as they were: the type they took from it written on them, then their move, scale+ * and turn undone around where they were (their own overrides, drafted: written with the line's edit, see riders).+ */+ const holdBlocks = (c, before, g0, g1) => {+ /* the line's own scale and turn, undone on each block (exact: from its overrides, not measured) */+ const ks = [g1.s[0] ? g0.s[0] / g1.s[0] : 1, g1.s[1] ? g0.s[1] / g1.s[1] : 1];+ const dr = g0.r - g1.r;+ for (const was of before) {+ const { b } = was;+ const own = overrideOf(c, b)?.style ?? {};+ const cs = css(b);+ const look = {};+ for (const k of INHERITED) if (!(k in own) && cs[k] !== was.look[k]) look[k] = was.look[k];+ if (Object.keys(look).length) draft(c, targetOf(b), { style: look });+ const now = blockNow(b);+ const g = paintedOf(b);+ /* its anchor (the start of its words, its top) where it was, with its box as it will be: its laid-out size at+ the scale and turn it had */+ const rad = (was.turn * Math.PI) / 180;+ const fw = was.frame.ow > 0 ? (was.frame.w * b.offsetWidth) / was.frame.ow : 0;+ const fh = was.frame.oh > 0 ? (was.frame.h * b.offsetHeight) / was.frame.oh : 0;+ const flat = Math.abs(was.turn % 360) < 0.01;+ const w = flat ? (was.box.width * b.offsetWidth) / (was.frame.ow || 1) : Math.abs(fw * Math.cos(rad)) + Math.abs(fh * Math.sin(rad));+ const h = flat ? (was.box.height * b.offsetHeight) / (was.frame.oh || 1) : Math.abs(fw * Math.sin(rad)) + Math.abs(fh * Math.cos(rad));+ const center = { x: was.box.left + was.ax * was.box.width + (0.5 - was.ax) * w, y: was.box.top + h / 2 };+ const moved = Math.hypot(now.box.left + now.box.width / 2 - center.x, now.box.top + now.box.height / 2 - center.y);+ if (Math.abs(ks[0] - 1) < 1e-4 && Math.abs(ks[1] - 1) < 1e-4 && Math.abs(dr) < 0.01 && moved < 0.05) continue;+ const round = (n) => Math.round(n * 10000) / 10000;+ paintAround(b, c, { t: g.t, s: [round(g.s[0] * ks[0]), round(g.s[1] * ks[1])], r: Math.round((g.r + dr) * 100) / 100 }, center);+ }};const release = (id) => {if (!drafts.delete(id)) return;@@ -651,15 +758,23 @@* scale or turn in place. The offset is outermost, a pure shift: paint, measure, correct, two or three times.*/const paintAround = (el, c, g, target) => {- const k = localScale(el) || 1;- let t = [g.t[0], g.t[1]];- for (let i = 0; i < 3; i++) {+ /* the center moves with the offset as an affine map (whatever turns and scales the parents): measured at the offset+ and one px along each axis, then solved for the offset that puts it at `target` */+ const centerAt = (t) => {paint(el, c, { ...g, t });- const box = el.getBoundingClientRect();- const dx = target.x - (box.left + box.width / 2);- const dy = target.y - (box.top + box.height / 2);- if (Math.abs(dx) < 0.05 && Math.abs(dy) < 0.05) break;- t = [t[0] + dx / k, t[1] + dy / k];+ const box = mixedText(el) ? wordsRect(el) : el.getBoundingClientRect();+ return [box.left + box.width / 2, box.top + box.height / 2];+ };+ const t0 = [g.t[0], g.t[1]];+ const p0 = centerAt(t0);+ let t = t0;+ if (Math.abs(target.x - p0[0]) >= 0.05 || Math.abs(target.y - p0[1]) >= 0.05) {+ const px = centerAt([t0[0] + 1, t0[1]]);+ const py = centerAt([t0[0], t0[1] + 1]);+ const a = px[0] - p0[0]; const b = py[0] - p0[0]; const cy = px[1] - p0[1]; const d = py[1] - p0[1];+ const det = a * d - b * cy;+ const ex = target.x - p0[0]; const ey = target.y - p0[1];+ if (Math.abs(det) > 1e-9) t = [t0[0] + (d * ex - b * ey) / det, t0[1] + (a * ey - cy * ex) / det];}t = [Math.round(t[0] * 10) / 10, Math.round(t[1] * 10) / 10];paint(el, c, { ...g, t });@@ -737,13 +852,30 @@put('contenteditable', edit.attrs.contentEditable);put('style', edit.attrs.style);put('spellcheck', edit.attrs.spellcheck);+ for (const [b, style] of edit.blocks) { if (style == null) b.removeAttribute('style'); else b.setAttribute('style', style); }el.replaceChildren(...edit.children);edit.data.forEach((text, n) => { n.data = text; });el.ownerDocument.getSelection()?.removeAllRanges();const kept = commit && value && value !== edit.before;- /* the new words stay on screen until the edit is kept (or refused: the editor releases the clip) */- if (kept) draft(c, edit.target, { text: value });- post({ type: 'stage-event', event: 'text', handle: edit.handle, commit: Boolean(kept), ...(kept ? { value, before: edit.before } : {}) });+ /* the new words stay on screen until the edit is kept (or refused: the editor releases the clip), with what held+ the others in place as they were typed (kept with them) */+ const hold = kept && edit.hold && Object.keys(edit.hold).length ? edit.hold : null;+ if (kept) draft(c, edit.target, { text: value, ...(hold ? { style: hold } : {}) });+ post({ type: 'stage-event', event: 'text', handle: edit.handle, commit: Boolean(kept), ...(kept ? { value, before: edit.before, ...(hold ? { hold } : {}) } : {}) });+ };+ /**+ * Only `el` takes the pointer in its document until the returned function is called. While a line is typed in, a+ * press on its words moves the caret or selects them, though the page draws something over it there (a number still+ * to come in, at opacity 0; a grain) or another clip lies on top; a press anywhere else blurs it, which keeps it.+ */+ const TYPING_ATTR = 'data-openfilm-typing';+ const onlyHit = (el) => {+ const doc = el.ownerDocument;+ const style = doc.createElement('style');+ style.textContent = `* { pointer-events: none !important; } [${TYPING_ATTR}], [${TYPING_ATTR}] * { pointer-events: auto !important; }`;+ el.setAttribute(TYPING_ATTR, '');+ (doc.head ?? doc.documentElement).append(style);+ return () => { style.remove(); el.removeAttribute(TYPING_ATTR); };};const startText = ({ handle }) => {const el = nodeOf(handle);@@ -756,8 +888,25 @@const attrs = { contentEditable: el.getAttribute('contenteditable'), style: el.getAttribute('style'), spellcheck: el.getAttribute('spellcheck') };/* typed in place, lines and all, where it can be: the line keeps its own width and wraps as it does */const overlay = isSvgText(el) || !onlyBreaks(el);+ /* typed in place, the words beside it and under it stay where they are as it grows (holdSearch), its box hugging+ them: worked out from the page as it is now, and shown on it as each word is typed */+ const search = !overlay && el instanceof c.win.HTMLElement ? holdSearch(c.win, el, keptHold(c, targetOf(el)).unheld) : null;+ const kebab = (k) => k.replace(/[A-Z]/g, (ch) => '-' + ch.toLowerCase());+ const inlineWas = new Map(HOLD_KEYS.map((k) => [k, [el.style.getPropertyValue(kebab(k)), el.style.getPropertyPriority(kebab(k))]]));+ const holdInline = (extra) => {+ for (const k of HOLD_KEYS) {+ const prop = kebab(k);+ if (!(k in extra)) { const [v, pr] = inlineWas.get(k); if (v) el.style.setProperty(prop, v, pr); else el.style.removeProperty(prop); }+ else if (extra[k] == null) el.style.removeProperty(prop);+ else el.style.setProperty(prop, extra[k], 'important');+ }+ };+ const onInput = () => { if (search && editing) editing.hold = search(holdInline); };const host = overlay ? openOverlay(el) : el;+ /* the line hidden under its overlay, the blocks in it still shown (they are not what is typed) */+ const blocks = overlay ? blocksIn(el).map((b) => [b, b.getAttribute('style')]) : [];if (overlay) el.style.setProperty('visibility', 'hidden', 'important');+ for (const [b] of blocks) b.style.setProperty('visibility', 'visible', 'important');try { host.contentEditable = 'plaintext-only'; } catch { host.contentEditable = 'true'; }if (host.contentEditable !== 'plaintext-only') host.contentEditable = 'true';host.spellcheck = false;@@ -774,12 +923,17 @@const onBlur = () => finishText(true);host.addEventListener('keydown', onKey);host.addEventListener('blur', onBlur);+ host.addEventListener('input', onInput);editing = {- el, host, c, handle, overlay, data, attrs, target: targetOf(el),+ el, host, c, handle, overlay, data, attrs, blocks, target: targetOf(el),children: [...el.childNodes],before: textOf(el),- cleanup: () => { host.removeEventListener('keydown', onKey); host.removeEventListener('blur', onBlur); },+ hold: null,+ cleanup: () => { host.removeEventListener('keydown', onKey); host.removeEventListener('blur', onBlur); host.removeEventListener('input', onInput); release(); },};+ /* the line in its page, and its page in the film */+ const hits = [onlyHit(host), onlyHit(c.el)];+ const release = () => { for (const off of hits) off(); };window.focus();c.win.focus();host.focus({ preventScroll: true });@@ -874,6 +1028,131 @@return pinned.some((e) => e.style.position) && holds(pinned) ? pinned : null;};+ /* ── a layer restyled: the others stay where they are, as in Figma, where one layer's look never moves another ── */+ /** The style a hold sets (holdFor): kept apart from what the person set, so a new hold replaces the one before. */+ const HOLD_KEYS = ['marginTop', 'marginRight', 'marginBottom', 'marginLeft', 'width'];+ const SIDES = ['Top', 'Right', 'Bottom', 'Left'];+ /**+ * The search for a hold, begun while the page shows `el` as it was: what the other layers' places are, the room `el`+ * takes in the page's flow, where its words sit. Returned: the search itself, given how to show `el` changed with a+ * hold's style on it (`show`, style keys to null take a kept one away). `unheld`: a hold kept before, taken away+ * first (it was for the change before).+ *+ * The layer takes its new size (its box grows with a larger font or more words, as a text box in Figma does); margins+ * give back to the page's flow the room it gained or lost, on the side the flow lays it out from, so what is beside+ * it, under it or lined up with its baseline stays put. Words in a block as wide as what holds it get a box that hugs+ * them, and stay where they were set in it (centered, at its end).+ */+ const holdSearch = (win, el, unheld) => {+ const others = layersOf(el.ownerDocument).filter((o) => o !== el && !o.contains(el) && !el.contains(o));+ const before = others.map((o) => o.getBoundingClientRect());+ const marginsNow = () => {+ const cs = win.getComputedStyle(el);+ return Object.fromEntries(SIDES.map((side) => [side, Number.parseFloat(cs[`margin${side}`]) || 0]));+ };+ const was = marginsNow();+ const roomW = el.offsetWidth + was.Left + was.Right;+ const roomH = el.offsetHeight + was.Top + was.Bottom;+ const cs = win.getComputedStyle(el);+ const fit = Boolean(textOf(el)) && !isGroup(el) && /^(block|flow-root|list-item)$/.test(cs.display) ? { width: 'fit-content' } : {};+ const align = cs.textAlign;+ const anchorOf = () => (/center/.test(align) ? el.offsetLeft + el.offsetWidth / 2 : /right|end/.test(align) ? el.offsetLeft + el.offsetWidth : el.offsetLeft);+ const anchor0 = anchorOf();+ /* how far the others went, the furthest, and which way up or down */+ const moved = () => {+ let far = 0;+ let dy = 0;+ others.forEach((o, i) => {+ const r = o.getBoundingClientRect();+ const d = Math.max(Math.abs(r.left - before[i].left), Math.abs(r.top - before[i].top));+ if (d > far) { far = d; dy = r.top - before[i].top; }+ });+ return { far, dy };+ };+ return (show) => {+ const base = { ...unheld, ...fit };+ show(base);+ /* changed, with the page's own margins: the room it would take, more or less than it takes now */+ const margin = marginsNow();+ const shift = fit.width ? anchor0 - anchorOf() : 0;+ const dw = el.offsetWidth + margin.Left + margin.Right - roomW;+ const dh = el.offsetHeight + margin.Top + margin.Bottom - roomH;+ const hold = (give) => {+ const side = { ...give, Left: (give.Left ?? 0) - shift, Right: (give.Right ?? 0) + shift };+ return { ...base, ...Object.fromEntries(SIDES.filter((k) => side[k]).map((k) => [`margin${k}`, `${Math.round((margin[k] - side[k]) * 100) / 100}px`])) };+ };+ show(hold({}));+ const alone = moved().far;+ if (alone < 0.5) return hold({});+ let best = { hold: hold({}), far: alone };+ const tryGive = (give) => {+ const h = hold(give);+ show(h);+ const m = moved();+ if (m.far < best.far) best = { hold: h, far: m.far };+ return m;+ };+ /* laid out from the start, the middle or the end of its row and of its column */+ const across = [{ Right: dw }, { Left: dw / 2, Right: dw / 2 }, { Left: dw }];+ const down = [{ Bottom: dh }, { Top: dh / 2, Bottom: dh / 2 }, { Top: dh }];+ for (const x of across) {+ for (const y of down) {+ const give = { ...x, ...y };+ let m = tryGive(give);+ /* lined up by its baseline: the words sit higher or lower in the new box, the room above them gives that back */+ for (let i = 0; i < 2 && m.far >= 0.5 && Math.abs(m.dy) >= 0.5; i++) {+ give.Top = (give.Top ?? 0) + m.dy;+ give.Bottom = (give.Bottom ?? 0) - m.dy;+ m = tryGive(give);+ }+ if (best.far < 0.5) return best.hold;+ }+ }+ show(best.hold);+ return best.hold;+ };+ };+ /** The style keys of a hold kept for this layer, to null: taken away before a hold is worked out again. */+ const keptHold = (c, target) => {+ const kept = (c.overrides ?? []).find((o) => o.at === target.at && (o.n ?? null) === (target.n ?? null));+ return { style: kept?.style ?? {}, text: kept?.text, unheld: Object.fromEntries(HOLD_KEYS.filter((k) => k in (kept?.style ?? {})).map((k) => [k, null])) };+ };+ /**+ * What the layer at `target` needs besides `style` (and new words, `text`) so that no other layer of its page moves+ * (holdSearch): style for the same override; {} when it needs none, or the change is what is kept already (a preview+ * taken back: the hold kept with it stands). Measured against the page as shown without this change (the kept+ * overrides, and other changes not kept yet).+ */+ const holdFor = (c, target, style, text) => {+ const doc = c.win?.document;+ /* a size or margin the person sets is meant to move what is around it */+ if (!doc || ['width', 'height', ...HOLD_KEYS, 'margin'].some((k) => k in style)) return {};+ const kept = keptHold(c, target);+ if (Object.entries(style).every(([k, v]) => k in kept.style && String(kept.style[k]) === String(v)) && (text == null || text === kept.text)) return {};+ const had = drafts.get(c.id);+ const items0 = had && new Map(had.items);+ const key = draftKey(target);+ const own = items0?.get(key)?.patch;+ const ownStyle = { ...(own?.style ?? {}) };+ for (const k of [...Object.keys(style), ...HOLD_KEYS]) delete ownStyle[k];+ const { text: _ownText, ...ownRest } = own ?? {};+ const showWith = (extra, changed) => {+ const items = new Map(items0 ?? []);+ const patch = { ...(changed || text == null ? own ?? {} : ownRest), style: { ...ownStyle, ...(changed ? style : {}), ...extra }, ...(changed && text != null ? { text } : {}) };+ items.set(key, { target, patch });+ drafts.set(c.id, { base: had?.base ?? JSON.stringify(c.overrides ?? []), items });+ show(c);+ };+ try {+ showWith({}, false);+ const el = matches(doc, target)[0];+ if (!el || !(el instanceof c.win.HTMLElement)) return {};+ return holdSearch(c.win, el, kept.unheld)((extra) => showWith(extra, true));+ } finally {+ if (had) { had.items = new Map(items0); drafts.set(c.id, had); show(c); } else release(c.id);+ }+ };+/* ── the Layers panel's tree ── */const ROWS = 800;/** Per element, whether the clip's kept overrides hide it (the panel's eye) or lock it: the last one to say counts. */@@ -1047,9 +1326,39 @@tweak({ clip, at, n, patch }) {const c = clipById(clip);if (!c || c.kind !== 'page') return false;- draft(c, n ? { at, n } : { at }, patch);+ const target = n ? { at, n } : { at };+ if (!patch.style && typeof patch.text !== 'string') { draft(c, target, patch); return true; }+ /* restyled or retyped, held in place (holdFor): the hold shown before gives way to this one */+ const style = patch.style ?? {};+ const hold = holdFor(c, target, style, typeof patch.text === 'string' ? patch.text : undefined);+ const shown = drafts.get(c.id)?.items.get(draftKey(target))?.patch.style;+ if (shown) for (const k of HOLD_KEYS) if (!(k in style)) delete shown[k];+ draft(c, target, { ...patch, style: { ...style, ...hold } });return true;},+ /**+ * What goes with an edit of a line that has blocks written inside it (`patch` null: its override taken away): the+ * edit shown, the overrides of the layers inside it that keep them where they were and as they looked, to be+ * written with it ([] for any other layer).+ */+ riders({ clip, at, n, patch }) {+ const c = clipById(clip);+ const target = n ? { at, n } : { at };+ const el = c?.win ? matches(c.win.document, target)[0] : null;+ if (!c || !el || !mixedText(el)) return [];+ draft(c, target, patch ?? null);+ const out = [];+ for (const item of drafts.get(c.id)?.items.values() ?? []) {+ const inner = matches(c.win.document, item.target)[0];+ if (inner && inner !== el && el.contains(inner) && item.patch) out.push({ ...item.target, patch: item.patch });+ }+ return out;+ },+ /** What keeps the other layers in place when this style (and these words) is kept for the layer at `at` (holdFor). */+ hold({ clip, at, n, style, text }) {+ const c = clipById(clip);+ return c?.kind === 'page' ? holdFor(c, n ? { at, n } : { at }, style ?? {}, typeof text === 'string' ? text : undefined) : {};+ },/*** The inspector's scale / turn of a layer about a pivot (a corner of its box, or its center): the offset that keeps* that point in place, shown.diff --git a/packages/openfilm/studio/server/stage.test.mjs b/packages/openfilm/studio/server/stage.test.mjsindex 49f3561..0eecd63 100644--- a/packages/openfilm/studio/server/stage.test.mjs+++ b/packages/openfilm/studio/server/stage.test.mjs@@ -9,6 +9,12 @@ import { filmHtml } from '../../src/film-doc.mjs';const STAGE = readFileSync(new URL('./stage.js', import.meta.url), 'utf8');+/**+ * The same place, to half a pixel: what the stage holds a layer to (stage.js holdSearch), since a page's own widths are+ * whole pixels while its words are laid out in fractions of one, as each system draws its fonts.+ */+const samePlace = (now, was, message) => assert.ok(now.every((v, i) => Math.abs(v - was[i]) < 0.5), `${message} (${JSON.stringify(now)}, was ${JSON.stringify(was)})`);+/** A film served with Studio's stage in it, the page itself standing in for the editor (its own origin). */async function stageOf(dir) {let files = null;@@ -43,7 +49,7 @@ async function stageOf(dir) {window.addEventListener('message', hear);window.postMessage({ source: 'openfilm-studio', type: 'stage', seq, op, ...args }, '*');}), { op, args, seq: ++seq });- return { ask, close };+ return { ask, page, close };}test('a click on a page\'s backdrop (a box filling the page) takes the clip, not the box; a smaller box is taken', { timeout: 60_000 }, async () => {@@ -70,3 +76,256 @@ test('a click on a page\'s backdrop (a box filling the page) takes the clip, notawait stage.close();}});++test('a line typed in place starts with all of its words selected; a click on them moves the caret, though something unseen lies over them', { timeout: 60_000 }, async () => {+ const dir = mkdtempSync(join(tmpdir(), 'of-stage-select-'));+ writeFileSync(join(dir, 'title.html'), `<!doctype html><body style="margin:0">+<h1 id="title" style="position:absolute;left:20px;top:20px;margin:0;font:700 24px monospace">Every parcel</h1>+<div id="later" style="position:absolute;left:0;top:0;width:320px;height:90px;opacity:0">3800</div>+<script>window.film={duration:2,frame(){}}</script></body>`);+ writeFileSync(join(dir, 'film.html'), filmHtml({ stage: { w: 320, h: 180 }, tracks: [{ clips: [{ id: 'title', src: 'title.html' }] }] }));+ const stage = await stageOf(dir);+ try {+ const title = await stage.ask('hit', { x: 40, y: 30, detail: true });+ assert.equal(await stage.ask('textStart', { handle: title.handle }), true);+ const page = stage.page.frames().find((f) => f.url().endsWith('title.html'));+ const state = () => page.evaluate(() => {+ const s = document.getSelection();+ return { typing: document.getElementById('title').isContentEditable, selected: s.toString(), collapsed: s.isCollapsed };+ });+ assert.deepEqual(await state(), { typing: true, selected: 'Every parcel', collapsed: false });+ /* on the words, under the unseen box: the caret moves there, the line is still typed in */+ await stage.page.mouse.click(80, 34);+ assert.deepEqual(await state(), { typing: true, selected: '', collapsed: true });+ /* elsewhere: let go */+ await stage.page.mouse.click(250, 150);+ assert.equal((await state()).typing, false);+ } finally {+ await stage.close();+ }+});++test('a click on words takes them, though a line drawn behind runs across its own box there', { timeout: 60_000 }, async () => {+ const dir = mkdtempSync(join(tmpdir(), 'of-stage-line-'));+ writeFileSync(join(dir, 'net.html'), `<!doctype html><body style="margin:0">+<svg style="position:absolute;left:0;top:0" width="320" height="180"><line x1="100" y1="40" x2="130" y2="70" stroke="#888" stroke-width="2"/></svg>+<div id="card" style="position:absolute;left:30px;top:40px;width:200px;height:60px;background:rgba(255,255,255,.07)">+ <h3 id="name" style="margin:0;font:600 30px sans-serif;width:200px">Cloud</h3>+</div>+<script>window.film={duration:2,frame(){}}</script></body>`);+ writeFileSync(join(dir, 'film.html'), filmHtml({ stage: { w: 320, h: 180 }, tracks: [{ clips: [{ id: 'net', src: 'net.html' }] }] }));+ const stage = await stageOf(dir);+ try {+ /* (125, 45): inside the line's box (100…130 × 40…70, smaller than the words'), off its stroke, on the words */+ assert.equal((await stage.ask('hit', { x: 125, y: 45, detail: true }))?.text?.value, 'Cloud');+ /* on the stroke itself: the line, the smaller of what is there */+ assert.equal((await stage.ask('hit', { x: 115, y: 55, detail: true }))?.kind, 'shape');+ } finally {+ await stage.close();+ }+});++test('a title with its subtitle written inside it: its own words are a layer, typed over with the subtitle still shown', { timeout: 60_000 }, async () => {+ const dir = mkdtempSync(join(tmpdir(), 'of-stage-own-words-'));+ writeFileSync(join(dir, 'title.html'), `<!doctype html><body style="margin:0">+<div id="title" style="position:absolute;left:20px;top:20px;font:700 40px sans-serif;line-height:1.15">Test video<div id="sub" style="font:400 14px sans-serif;margin-top:10px">picture · motion</div></div>+<script>window.film={duration:2,frame(){}}</script></body>`);+ writeFileSync(join(dir, 'film.html'), filmHtml({ stage: { w: 320, h: 180 }, tracks: [{ clips: [{ id: 'title', src: 'title.html' }] }] }));+ const stage = await stageOf(dir);+ try {+ const title = await stage.ask('hit', { x: 40, y: 40, detail: true });+ assert.equal(title?.kind, 'text');+ assert.equal(title?.text?.value, 'Test video');+ assert.equal((await stage.ask('hit', { x: 40, y: 82, detail: true }))?.text?.value, 'picture · motion');+ assert.equal(await stage.ask('textStart', { handle: title.handle }), true);+ const page = stage.page.frames().find((f) => f.url().endsWith('title.html'));+ const sub = () => page.evaluate(() => { const el = document.getElementById('sub'); return { shown: getComputedStyle(el).visibility, style: el.getAttribute('style') }; });+ assert.equal((await sub()).shown, 'visible', 'the subtitle stays while the title is typed');+ await stage.page.mouse.click(250, 150);+ assert.deepEqual(await sub(), { shown: 'visible', style: 'font:400 14px sans-serif;margin-top:10px' });+ } finally {+ await stage.close();+ }+});++test('a title with its subtitle inside it: its box hugs its own words, and moved or restyled the subtitle stays as it was', { timeout: 60_000 }, async () => {+ const dir = mkdtempSync(join(tmpdir(), 'of-stage-own-box-'));+ writeFileSync(join(dir, 'title.html'), `<!doctype html><body style="margin:0">+<div id="title" style="position:absolute;left:20px;top:20px;font:700 40px sans-serif;line-height:1.15;color:#fff">Test video<div id="sub" style="font-size:14px;margin-top:10px">picture · motion</div></div>+<script>window.film={duration:2,frame(){}}</script></body>`);+ writeFileSync(join(dir, 'film.html'), filmHtml({ stage: { w: 320, h: 180 }, tracks: [{ clips: [{ id: 'title', src: 'title.html' }] }] }));+ const stage = await stageOf(dir);+ try {+ const page = stage.page.frames().find((f) => f.url().endsWith('title.html'));+ const subNow = () => page.evaluate(() => {+ const el = document.getElementById('sub');+ const r = el.getBoundingClientRect();+ return { x: Math.round(r.left * 10) / 10, y: Math.round(r.top * 10) / 10, w: Math.round(r.width * 10) / 10, color: getComputedStyle(el).color };+ });+ const title = await stage.ask('hit', { x: 40, y: 40, detail: true });+ /* the box is the words', not the subtitle's too */+ assert.ok(title.rect.height < 50, `the box hugs the words (${title.rect.height})`);+ /* beside the words, on the title's box but on no word: not the title */+ assert.notEqual((await stage.ask('hit', { x: 250, y: 40, detail: true }))?.text?.value, 'Test video');+ const was = await subNow();+ assert.equal(await stage.ask('tweak', { clip: 'title', at: '#title', patch: { t: [30, 12] } }), true);+ assert.deepEqual(await subNow(), was, 'moved, the subtitle stays');+ assert.equal(await stage.ask('tweak', { clip: 'title', at: '#title', patch: { style: { color: '#ff0000', fontSize: '60px' } } }), true);+ const after = await subNow();+ assert.equal(after.color, was.color, 'recolored, the subtitle keeps its color');+ /* its words start where they did (its block is as wide as the title, which grew) */+ assert.ok(Math.abs(after.x - was.x) < 0.2 && Math.abs(after.y - was.y) < 0.2, `restyled, the subtitle stays (${JSON.stringify(after)})`);+ /* scaled and turned: the subtitle neither grows nor turns */+ const flat = await subNow();+ assert.equal(await stage.ask('tweak', { clip: 'title', at: '#title', patch: { s: 1.5, r: 20 } }), true);+ const turned = await subNow();+ for (const k of ['x', 'y', 'w']) assert.ok(Math.abs(turned[k] - flat[k]) < 0.5, `scaled and turned, the subtitle stays (${k}: ${flat[k]} → ${turned[k]})`);+ /* what is written with the title's edit: the subtitle's override */+ const riders = await stage.ask('riders', { clip: 'title', at: '#title', patch: { t: [30, 12] } });+ assert.deepEqual(riders.map((r) => r.at), ['#sub']);+ assert.ok(riders[0].patch.t && riders[0].patch.style?.color);+ } finally {+ await stage.close();+ }+});++test('a layer restyled leaves the others where they are, and takes its new size: the words beside a number stay put', { timeout: 60_000 }, async () => {+ const dir = mkdtempSync(join(tmpdir(), 'of-stage-hold-'));+ writeFileSync(join(dir, 'row.html'), `<!doctype html><body style="margin:0">+<div id="row" style="position:absolute;left:10px;top:40px;display:flex;align-items:baseline;gap:10px">+ <b id="num" style="font:700 60px monospace;line-height:1">18</b><span id="words" style="font:600 20px sans-serif">founders</span>+</div>+<p id="below" style="position:absolute;left:10px;top:120px;margin:0">a dream</p>+<script>window.film={duration:2,frame(){}}</script></body>`);+ writeFileSync(join(dir, 'film.html'), filmHtml({ stage: { w: 320, h: 180 }, tracks: [{ clips: [{ id: 'row', src: 'row.html' }] }] }));+ const stage = await stageOf(dir);+ try {+ const page = stage.page.frames().find((f) => f.url().endsWith('row.html'));+ const place = () => page.evaluate(() => {+ const w = document.getElementById('words').getBoundingClientRect();+ const n = document.getElementById('num');+ return { words: [Math.round(w.left * 10) / 10, Math.round(w.top * 10) / 10], num: [n.offsetWidth, n.offsetHeight] };+ });+ const was = await place();+ for (const style of [{ fontSize: '90px' }, { lineHeight: '2' }, { letterSpacing: '12px' }]) {+ assert.equal(await stage.ask('tweak', { clip: 'row', at: '#num', patch: { style } }), true);+ const now = await place();+ samePlace(now.words, was.words, `the words beside it did not move (${JSON.stringify(style)})`);+ assert.notDeepEqual(now.num, was.num, `the number took its new size (${JSON.stringify(style)})`);+ await stage.ask('release', { clip: 'row' });+ }+ /* a size the person sets is meant to move what is around it */+ assert.deepEqual(await stage.ask('hold', { clip: 'row', at: '#num', style: { width: '200px' } }), {});+ } finally {+ await stage.close();+ }+});++test('a hold kept before gives way: the number set back smaller takes its own size, and the words stay', { timeout: 60_000 }, async () => {+ const page = (overrides) => `<!doctype html><body style="margin:0">+<div id="row" style="position:absolute;left:10px;top:40px;display:flex;align-items:baseline;gap:10px">+ <b id="num" style="font:700 60px monospace;line-height:1">18</b><span id="words" style="font:600 20px sans-serif">founders</span>+</div>+<script>window.film={duration:2,frame(){}}</script></body>`;+ /* first, what holding the words in place takes at 90px */+ const dir = mkdtempSync(join(tmpdir(), 'of-stage-rehold-'));+ writeFileSync(join(dir, 'row.html'), page());+ writeFileSync(join(dir, 'film.html'), filmHtml({ stage: { w: 320, h: 180 }, tracks: [{ clips: [{ id: 'row', src: 'row.html' }] }] }));+ let stage = await stageOf(dir);+ const read = (st) => st.page.frames().find((f) => f.url().endsWith('row.html')).evaluate(() => {+ const w = document.getElementById('words').getBoundingClientRect();+ return { words: [Math.round(w.left * 10) / 10, Math.round(w.top * 10) / 10], num: document.getElementById('num').offsetWidth };+ });+ let held;+ let plain;+ try {+ plain = await read(stage);+ held = await stage.ask('hold', { clip: 'row', at: '#num', style: { fontSize: '90px' } });+ assert.ok(Object.keys(held).length);+ } finally {+ await stage.close();+ }+ /* kept so; then set back to 60px: the margins of 90px go, the number is as wide as its words again */+ writeFileSync(join(dir, 'film.html'), filmHtml({ stage: { w: 320, h: 180 }, tracks: [{ clips: [{ id: 'row', src: 'row.html', overrides: [{ at: '#num', style: { fontSize: '90px', ...held } }] }] }] }));+ stage = await stageOf(dir);+ try {+ const kept = await read(stage);+ samePlace(kept.words, plain.words, 'kept with its hold, the words are where they were');+ const back = await stage.ask('hold', { clip: 'row', at: '#num', style: { fontSize: '60px' } });+ for (const k of Object.keys(held).filter((key) => key.startsWith('margin'))) assert.equal(back[k], null, `${k} taken away`);+ assert.equal(await stage.ask('tweak', { clip: 'row', at: '#num', patch: { style: { fontSize: '60px', ...back } } }), true);+ const now = await read(stage);+ assert.deepEqual(now, plain, 'as it was before any change: the words in place, the number its own width');+ } finally {+ await stage.close();+ }+});++test('words in a block as wide as their box restyled: the box hugs them, they stay centered where they were', { timeout: 60_000 }, async () => {+ const dir = mkdtempSync(join(tmpdir(), 'of-stage-fit-'));+ writeFileSync(join(dir, 'card.html'), `<!doctype html><body style="margin:0">+<div id="card" style="position:absolute;left:20px;top:20px;width:260px">+ <h3 id="title" style="margin:0;font:600 20px sans-serif;text-align:center">Cloud</h3>+ <p id="line" style="margin:6px 0 0;font:14px sans-serif">built on its own</p>+</div>+<script>window.film={duration:2,frame(){}}</script></body>`);+ writeFileSync(join(dir, 'film.html'), filmHtml({ stage: { w: 320, h: 180 }, tracks: [{ clips: [{ id: 'card', src: 'card.html' }] }] }));+ const stage = await stageOf(dir);+ try {+ const page = stage.page.frames().find((f) => f.url().endsWith('card.html'));+ const read = () => page.evaluate(() => {+ const t = document.getElementById('title');+ const words = document.createRange();+ words.selectNodeContents(t);+ const w = words.getBoundingClientRect();+ const line = document.getElementById('line').getBoundingClientRect();+ return { box: t.offsetWidth, words: Math.round(w.width), center: Math.round(w.left + w.width / 2), line: [line.left, line.top] };+ });+ const was = await read();+ assert.equal(was.box, 260, 'as wide as the card');+ assert.equal(await stage.ask('tweak', { clip: 'card', at: '#title', patch: { style: { fontSize: '30px' } } }), true);+ const now = await read();+ assert.ok(Math.abs(now.box - now.words) <= 1, `the box hugs the words (${now.box} for ${now.words})`);+ assert.ok(Math.abs(now.center - was.center) <= 1, `still centered where they were (${now.center}, was ${was.center})`);+ samePlace(now.line, was.line, 'the line under them did not move');+ } finally {+ await stage.close();+ }+});++test('more words leave the others where they are: previewed, and typed in place', { timeout: 60_000 }, async () => {+ const dir = mkdtempSync(join(tmpdir(), 'of-stage-words-'));+ writeFileSync(join(dir, 'row.html'), `<!doctype html><body style="margin:0">+<div id="row" style="position:absolute;left:10px;top:40px;display:flex;align-items:baseline;gap:10px">+ <b id="num" style="font:700 40px monospace;line-height:1">18</b><span id="words" style="font:600 20px sans-serif">founders</span>+</div>+<script>window.film={duration:2,frame(){}}</script></body>`);+ writeFileSync(join(dir, 'film.html'), filmHtml({ stage: { w: 320, h: 180 }, tracks: [{ clips: [{ id: 'row', src: 'row.html' }] }] }));+ const stage = await stageOf(dir);+ try {+ const page = stage.page.frames().find((f) => f.url().endsWith('row.html'));+ const read = () => page.evaluate(() => {+ const w = document.getElementById('words').getBoundingClientRect();+ const n = document.getElementById('num');+ return { words: [Math.round(w.left * 10) / 10, Math.round(w.top * 10) / 10], num: n.textContent, width: n.offsetWidth };+ });+ const was = await read();+ assert.equal(await stage.ask('tweak', { clip: 'row', at: '#num', patch: { text: '1999' } }), true);+ const previewed = await read();+ assert.equal(previewed.num, '1999');+ assert.ok(previewed.width > was.width, 'its box took the longer words');+ samePlace(previewed.words, was.words, 'the words beside it did not move');+ await stage.ask('release', { clip: 'row' });+ /* typed in place: the same as each key goes */+ const num = await stage.ask('hit', { x: 20, y: 60, detail: true });+ assert.equal(await stage.ask('textStart', { handle: num.handle }), true);+ await stage.page.keyboard.type('2026');+ const typing = await read();+ assert.equal(typing.num, '2026');+ samePlace(typing.words, was.words, 'typing, the words beside it stay');+ await stage.ask('textFinish', { commit: true });+ samePlace((await read()).words, was.words, 'kept, they stay');+ } finally {+ await stage.close();+ }+});diff --git a/packages/openfilm/studio/server/transcripts.mjs b/packages/openfilm/studio/server/transcripts.mjsindex 3289ea9..e21f6c4 100644--- a/packages/openfilm/studio/server/transcripts.mjs+++ b/packages/openfilm/studio/server/transcripts.mjs@@ -17,7 +17,7 @@import { readdir, readFile, stat } from 'node:fs/promises';import { basename, dirname, extname, join } from 'node:path';import { writeAtomic } from './atomic.mjs';-import { inProject, inside } from './files.mjs';+import { inProject, inside, trashFile } from './files.mjs';import { alignTranslation, replacePiece, segmentLine } from './subtitle-segments.mjs';/** @typedef {{ token: string, startSec: number, endSec?: number }} Word */@@ -204,6 +204,19 @@ export async function transcriptIsCurrent(root, src) {return Boolean(media && words && words.mtimeMs >= media.mtimeMs);}+/**+ * A sound or video made again in place (a voice-over spoken again under its name, a service's answer saved over the+ * last one): its transcript and translations are the old take's, and go to the trash. Subtitles are never timed by+ * another take's words; when the new one came with timed words, its own transcript is written after.+ * @param {string} root @param {string} src+ */+export async function dropTranscripts(root, src) {+ for (const rel of [transcriptPath(src), ...(await translationsOf(root, src)).map((language) => transcriptPath(src, language))]) {+ const abs = inProject(root, rel);+ if (abs && await stat(abs).catch(() => null)) await trashFile(root, rel).catch(() => {});+ }+}+/** The languages `src` has a translation in (`name.<language>.vtt` beside it). @param {string} root @param {string} src */export async function translationsOf(root, src) {const name = basename(src, extname(src));diff --git a/packages/openfilm/studio/ui/src/components/FilmInspector.tsx b/packages/openfilm/studio/ui/src/components/FilmInspector.tsxindex 45dcc76..55ed6e7 100644--- a/packages/openfilm/studio/ui/src/components/FilmInspector.tsx+++ b/packages/openfilm/studio/ui/src/components/FilmInspector.tsx@@ -142,6 +142,8 @@ export const FilmInspector = React.memo(function FilmInspector({width,onClose,onEditStyle,+ onEditSimilar,+ countSimilar,onPreviewStyle,onEditText,onPreviewText,@@ -185,6 +187,10 @@ export const FilmInspector = React.memo(function FilmInspector({onClose: () => void;/** Writes a style. Resolves to an error when refused (the panel then paints the old value back). */onEditStyle: (loc: string, patch: StageElementStyle) => Promise<string | null>;+ /** A style for every layer like this one (its tag and classes) in the film's pages: "All like it". */+ onEditSimilar?: (element: StageElement, patch: StageElementStyle) => Promise<string | null>;+ /** How many page clips "All like it" reaches. */+ countSimilar?: (element: StageElement) => Promise<number>;/** Paints a style on the picture without writing it. */onPreviewStyle: (loc: string, patch: StageElementStyle) => void;onEditText: (edit: StageTextEdit) => Promise<StageEditFailure | null>;@@ -286,6 +292,8 @@ export const FilmInspector = React.memo(function FilmInspector({geometry={geometry}inlineTextFailure={inlineTextFailure}onEditStyle={onEditStyle}+ {...(onEditSimilar ? { onEditSimilar } : {})}+ {...(countSimilar ? { countSimilar } : {})}onPreviewStyle={onPreviewStyle}onEditText={onEditText}onPreviewText={onPreviewText}@@ -732,16 +740,20 @@ function layerStyle(element: StageElement, set: LookSet): Record<string, string* Which copies a style change goes to when a list renders several. Words are not scoped: copies showing the same* words come from one template, so changing them changes all (see TextSection).*/-type Scope = 'one' | 'all';+type Scope = 'one' | 'all' | 'similar';function LayerPanel({- element, geometry, inlineTextFailure, onEditStyle, onPreviewStyle, onEditText, onPreviewText,+ element, geometry, inlineTextFailure, onEditStyle, onEditSimilar, countSimilar, onPreviewStyle, onEditText, onPreviewText,onEditOverride, onPreviewOverride, onSelectParent, onSelectClip, onEditMask,}: {element: StageElement;geometry?: StageGeometry | null;inlineTextFailure?: InlineTextFailure | null;onEditStyle: (loc: string, patch: StageElementStyle) => Promise<string | null>;+ /** A style for every layer like this one (its tag and classes) in the film's pages: "All like it". */+ onEditSimilar?: (element: StageElement, patch: StageElementStyle) => Promise<string | null>;+ /** How many page clips "All like it" reaches. */+ countSimilar?: (element: StageElement) => Promise<number>;onPreviewStyle: (loc: string, patch: StageElementStyle) => void;onEditText: (edit: StageTextEdit) => Promise<StageEditFailure | null>;onPreviewText?: (loc: string, value: string) => void;@@ -766,6 +778,15 @@ function LayerPanel({};const copies = element.instances != null && element.instances > 1 ? element.instances : 0;const [scope, setScope] = React.useState<Scope>('one');+ /* "All like it": text with a class to go by; how many page clips it reaches, once counted */+ const likeIt = element.kind === 'text' && Boolean(element.similar) && Boolean(onEditSimilar);+ const [likeCount, setLikeCount] = React.useState<number | null>(null);+ React.useEffect(() => {+ if (!likeIt || !countSimilar) return undefined;+ let live = true;+ void countSimilar(element).then((n) => { if (live) setLikeCount(n); });+ return () => { live = false; };+ }, [likeIt, countSimilar, element.similar, element.clipLoc]); // eslint-disable-line react-hooks/exhaustive-depsconst loc = element.loc;const bare = sourceLocBare(loc) ?? loc;@@ -802,7 +823,7 @@ function LayerPanel({previewedKeys.current.clear();onPreviewStyle(styleLoc, patch);setRefusal(null);- const failed = await onEditStyle(styleLoc, patch);+ const failed = scope === 'similar' && onEditSimilar ? await onEditSimilar(element, patch) : await onEditStyle(styleLoc, patch);if (!failed) return;/* refused: take the change back off the picture, or it keeps a look the film does not have */const back: StageElementStyle = {};@@ -916,15 +937,22 @@ function LayerPanel({<><Header icon={kindIcon(kind, 13)} name={element.label} meta={layerKindLabel(t, kind)} /><Breadcrumb element={element} onSelectParent={onSelectParent} onSelectClip={onSelectClip} />- {copies ? (+ {copies || likeIt ? (<div className="px-3 pb-2.5"><Segmentedlabel={t('inspector.scope')}value={scope}onChange={(v) => setScope(v as Scope)}options={[- { value: 'one', label: t('inspector.scopeOne').replace('{i}', String(element.instance ?? 1)) },- { value: 'all', label: t('inspector.scopeAll').replace('{n}', String(copies)) },+ copies+ ? { value: 'one', label: t('inspector.scopeOne').replace('{i}', String(element.instance ?? 1)) }+ : { value: 'one', label: t('inspector.scopeThis') },+ ...(copies ? [{ value: 'all', label: t('inspector.scopeAll').replace('{n}', String(copies)) }] : []),+ /* the look of every title like it, in every page that has one (lib/similar-layers); words and places stay each one's */+ ...(likeIt ? [{+ value: 'similar', title: t('inspector.scopeSimilarHint'),+ label: likeCount && likeCount > 1 ? t('inspector.scopeSimilarIn').replace('{n}', String(likeCount)) : t('inspector.scopeSimilar'),+ }] : []),]}/></div>diff --git a/packages/openfilm/studio/ui/src/components/FilmStageSelect.tsx b/packages/openfilm/studio/ui/src/components/FilmStageSelect.tsxindex a4e3c14..6845692 100644--- a/packages/openfilm/studio/ui/src/components/FilmStageSelect.tsx+++ b/packages/openfilm/studio/ui/src/components/FilmStageSelect.tsx@@ -34,6 +34,7 @@*/import * as React from 'react';import { isSelectAll as isSelectAllKey, type KeyLike } from '@/lib/select-all-key';+import { doubleClickAim, keepPress, type Press } from '@/lib/double-press';import {AlignCenterHorizontal, AlignCenterVertical, AlignEndHorizontal, AlignEndVertical, AlignHorizontalDistributeCenter,AlignStartHorizontal, AlignStartVertical, AlignVerticalDistributeCenter, Crop as CropIcon, MessageSquarePlus,@@ -63,7 +64,7 @@ import {positionOf, roundOf, transformOfVisible, visibleFrame, type CropAspect, type Rect,} from '@/lib/stage-crop';import {- alignShift, groupDragged, type GroupDrag, HANDLE_AT, insideBox, insideFrame, movedOffset, pivotCenter, resizedFrame, sameClientRect, sameGuides,+ alignShift, groupDragged, type GroupDrag, type GroupPull, HANDLE_AT, insideBox, layerInGroup, pullGroup, insideFrame, movedOffset, pivotCenter, resizedFrame, sameClientRect, sameGuides,sameLayerGeometry, scaledLayer, turnedLayer, unionRect, type AlignEdge, type ClientBox, type FramePivot,type LayerGeometry, type StageFrame,} from '@/lib/stage-gesture';@@ -88,11 +89,15 @@ export type { LayerGeometry } from '@/lib/stage-gesture';/** Pulled open this far (stage px, both ways) it is a marquee; less is a click (a shaky hand keeps its click). */const MARQUEE_MIN_PX = 8;const PICK_BLUE = FRAME_BLUE;-/** Two presses this close in time are one double-click (the system's own interval is about this). */-const DOUBLE_PRESS_MS = 500;/** Snap this close (screen px: the feel must not change with the picture's scale). */const SNAP_PX = 6;const NO_GUIDES: readonly MgGuide[] = [];+/**+ * A key pressed in the inspector (a menu gone through with the arrows, a field): its own, not a nudge. Heard first by+ * the nudges (they listen in the capture phase), the arrows moved the layer while a menu was gone through, and the+ * write took back what the menu showed.+ */+const inspectorKey = (target: EventTarget | null): boolean => Boolean((target as Element | null)?.closest?.('[data-film-inspector], [role="listbox"], [role="menu"]'));/** A selected layer lost in a redraw is let go after this long without finding it again. */const LOST_PICK_GRACE_MS = 1200;/** The selection is measured again each time the playhead enters another half second. */@@ -125,6 +130,8 @@ export interface StageLayer {instances?: number;kind: StageNodeKind;tag: string;+ /** layers like it in any page: its tag and classes (none: it has no class) */+ similar?: string;label: string;text?: { value: string; shape: 'children' | 'prop' | 'value' };style: StageElementStyle;@@ -174,6 +181,11 @@ export interface StageClipEdit {export interface FilmStageControl {/** Show style keys on a layer before they are kept (`null` takes a key back to the page's own). */previewStyle(element: StageElement, patch: StageElementStyle | Record<string, string | number | null>): void;+ /**+ * What a layer needs besides `style` so that no other layer moves when it is kept (its place in the page held, as+ * Figma keeps a restyled layer from moving the others): style to write with it, {} when nothing else moves.+ */+ holdStyle(element: StageElement, style: Record<string, string | number | null>, text?: string): Promise<Record<string, string | null>>;/** Show a layer's words before they are kept. */previewText(element: StageElement, value: string): void;/**@@ -181,6 +193,11 @@ export interface FilmStageControl {* box stays put (CSS scales and turns about its unmoved box), and returned with the patch, to be written so.*/previewOverride(element: StageElement, patch: OverrideGeometry, pivot?: FramePivot): Promise<OverrideGeometry>;+ /**+ * What is written with an edit of a layer (`patch` null: its override taken away), besides it: for a line with blocks+ * written inside it (a subtitle inside its title), the overrides that keep those where they were and as they looked.+ */+ riders(element: StageElement, patch: OverrideGeometry | null): Promise<{ at: string; n?: number; patch: OverrideGeometry }[]>;/** Stop showing what was not kept on the element's clip (an edit was refused). */release(element: StageElement): void;/** The layer read again (its style after an override was taken away). */@@ -367,7 +384,8 @@ interface FilmStageSelectProps {/** Several layers moved or aligned together: written in one go (one undo step). */onTransformLayers?: (list: ReadonlyArray<{ element: StageElement; g: LayerGeometry }>) => void;/** A line typed in place: its words written. Refused: a message (the words go back). */- onEditTextInline?: (element: StageElement, value: string) => Promise<string | null>;+ /** `hold`: what kept the other layers in place as the words were typed, written with them. */+ onEditTextInline?: (element: StageElement, value: string, hold?: Record<string, string | null>) => Promise<string | null>;/** Where the thing in hand is now (stage px), for the inspector's X / Y / W / H / turn; reported when it changes. */onGeometry?: (geometry: StageGeometry | null) => void;/** The context menu's entries for what was right-clicked (see components/stage-menu). */@@ -588,6 +606,7 @@ function FilmStageSelectImpl({clipKind: 'mg',...(layer.text ? { text: { loc: layer.loc, value: layer.text.value, shape: layer.text.shape } } : {}),tag: layer.tag,+ ...(layer.similar ? { similar: layer.similar } : {}),style: layer.style,declaredStyle: [],parents: layer.parents,@@ -599,8 +618,8 @@ function FilmStageSelectImpl({/* ── in-place text editing state (the film runs the text box; it says when it ends) ── */const [editingText, setEditingTextState] = React.useState<string | null>(null);const textEdit = React.useRef<{ element: StageElement; layer: StageLayer } | null>(null);- /** The last press on the picture, in stage px: a double-click is aimed by its first click (see onStageDoubleClick). */- const lastPressRef = React.useRef<{ x: number; y: number; at: number } | null>(null);+ /** The last presses on the picture, in stage px: a double-click is aimed by its first click (see onStageDoubleClick). */+ const pressesRef = React.useRef<Press[]>([]);/* while a line is typed in place the picture takes the pointer (move the caret, select words, click away to blur) */const setEditingText = React.useCallback((loc: string | null) => {const frame = previewRef.current?.frame?.();@@ -642,6 +661,10 @@ function FilmStageSelectImpl({/* ── several layers moved together ── */type MultiDragItem = MultiItem & { base: LayerGeometry; scale: number; now: LayerGeometry };const multiDrag = React.useRef<{ x: number; y: number; moved: boolean; rect0: ClientBox; items: MultiDragItem[] } | null>(null);+ /* several layers scaled or turned by their box's handles (Figma's): each one's center at the press (stage px) */+ const layersPull = React.useRef<(GroupPull & { x: number; y: number; moved: boolean; box0: ClientBox; items: MultiDragItem[]; centers: { x: number; y: number }[] }) | null>(null);+ /** The box around several layers while they are turned: the box they had, turned as far. */+ const [layersTurn, setLayersTurn] = React.useState<{ box: ClientBox; turn: number } | null>(null);/* several whole clips selected (on the timeline, or by a marquee): a press on one of them drags them all */const clipsDrag = React.useRef<(GroupDrag & {x: number; y: number; moved: boolean; items: ClipsDragItem[];@@ -669,7 +692,7 @@ function FilmStageSelectImpl({return p ? { ...it, layer: { ...it.layer, rect: p.rect, frame: p.frame, geo: { ...it.layer.geo, g: items[i]!.now } } } : it;});setMulti(next);- if (!multiDrag.current) setPicked((cur) => (cur ? { ...cur, rect: unionRect(next.map((i) => i.layer.rect)) } : cur));+ if (!multiDrag.current && !layersPull.current?.moved) setPicked((cur) => (cur ? { ...cur, rect: unionRect(next.map((i) => i.layer.rect)) } : cur));}), [ask, setMulti, setPicked]);/** Each layer moved by a screen offset (turned back by each one's parents' turn, at each one's scale), shown. */const shiftLayers = (items: MultiDragItem[], dxOf: (i: number) => number, dyOf: (i: number) => number) => {@@ -722,7 +745,7 @@ function FilmStageSelectImpl({const fresh = await ask<(StageLayer | null)[]>('layers', {refs: many.map((it) => ({ handle: it.layer.handle, clip: it.layer.clip, at: it.layer.at, n: it.layer.n })),});- if (!fresh || multiRef.current !== many || multiDrag.current) return;+ if (!fresh || multiRef.current !== many || multiDrag.current || layersPull.current) return;const next = many.flatMap((it, i) => {const layer = fresh[i];if (!layer) return [];@@ -1016,7 +1039,7 @@ function FilmStageSelectImpl({const value = e.value;const nextElement = { ...element, ...(element.text ? { text: { ...element.text, value } } : {}) };setPicked((cur) => (cur?.element?.loc === element.loc ? { ...cur, element: nextElement } : cur));- void onEditTextInline(element, value).then((failed) => {+ void onEditTextInline(element, value, e.hold).then((failed) => {if (!failed) return;void ask('release', { clip: layer.clip });setPicked((cur) => (cur?.element?.loc === element.loc ? { ...cur, element } : cur));@@ -1075,6 +1098,7 @@ function FilmStageSelectImpl({}/* the arrows are the playhead's unless the picture has the keyboard (arrowsMove) */if (e.key.startsWith('Arrow') && !arrowsMoveRef.current) return;+ if (e.key.startsWith('Arrow') && inspectorKey(e.target)) return;if (e.key.startsWith('Arrow') && multiRef.current && onTransformLayers && !multiRef.current.some((it) => lockedClip(it.element.clipLoc)) && !e.metaKey && !e.ctrlKey && !e.altKey) {if (!typingNow()) {e.preventDefault();@@ -1312,6 +1336,10 @@ function FilmStageSelectImpl({const at = named(element);if (at) void ask('tweak', { ...at, patch: { style: clean(patch as Record<string, string | number | null | undefined>) } }).then(syncGeomSoon);},+ async holdStyle(element, style, text) {+ const at = named(element);+ return (at ? await ask<Record<string, string | null>>('hold', { ...at, style: clean(style), ...(text != null ? { text } : {}) }) : null) ?? {};+ },previewText(element, value) {const at = named(element);if (at) void ask('tweak', { ...at, patch: { text: value } }).then(syncGeomSoon);@@ -1330,6 +1358,10 @@ function FilmStageSelectImpl({syncGeomSoon();return patch;},+ async riders(element, patch) {+ const at = named(element);+ return (at ? await ask<{ at: string; n?: number; patch: OverrideGeometry }[]>('riders', { ...at, patch }) : null) ?? [];+ },release(element) {if (element.clipId) void ask('release', { clip: element.clipId }).then(syncGeomSoon);},@@ -1601,7 +1633,7 @@ function FilmStageSelectImpl({const onKey = (e: KeyboardEvent) => {if (!e.key.startsWith('Arrow') || !arrowsMoveRef.current || e.defaultPrevented || e.metaKey || e.ctrlKey || e.altKey) return;const el = e.target as HTMLElement | null;- if (inHostPanel(el) || el?.closest('input, textarea, select, [contenteditable="true"], [data-timeline]')) return;+ if (inHostPanel(el) || inspectorKey(el) || el?.closest('input, textarea, select, [contenteditable="true"], [data-timeline]')) return;nudgeGroupRef.current(e);};window.addEventListener('keydown', onKey, true);@@ -1777,7 +1809,7 @@ function FilmStageSelectImpl({}), [ask, stage]);/** Measured only with nothing being dragged, drawn or cropped. */const measureAt = (p: { x: number; y: number } | null) => {- const busy = gestureRef.current || layerDrag.current || multiDrag.current || draftRef.current || cropRef.current || textEdit.current;+ const busy = gestureRef.current || layerDrag.current || multiDrag.current || layersPull.current || draftRef.current || cropRef.current || textEdit.current;if (!p || !pickedRef.current || busy) {altHeld.current = false;setMeasure(null);@@ -1924,8 +1956,8 @@ function FilmStageSelectImpl({const token = ++pressSeq.current;/* where the double-click began, in the film's own pixels: its first click can open the inspector, and the pictureshrinks under the hand before the second — taken where the second landed on screen, it typed in another line */- const first = lastPressRef.current;- const at = first && e.timeStamp - first.at < DOUBLE_PRESS_MS ? { x: first.x, y: first.y } : { x: p.x / vs, y: p.y / vs };+ const first = doubleClickAim(pressesRef.current, e.timeStamp);+ const at = first ? { x: first.x, y: first.y } : { x: p.x / vs, y: p.y / vs };const inside = held && insideBox(held.rect, at.x, at.y);/* cropping, a double-click keeps the crop (as in Figma); on the video or still in hand, it starts one */if (cropRef.current) { finishCrop(true); return; }@@ -2136,6 +2168,16 @@ function FilmStageSelectImpl({});return;}+ const lp = layersPull.current;+ if (lp) {+ layersPull.current = null;+ e.stopPropagation();+ setDragging(false);+ setLayersTurn(null);+ if (!lp.moved) return;+ void multiPaint.idle().then(() => onTransformLayers?.(lp.items.map((it) => ({ element: it.element, g: it.now }))));+ return;+ }const md = multiDrag.current;if (md) {multiDrag.current = null;@@ -2442,8 +2484,7 @@ function FilmStageSelectImpl({if (!outside) onPause?.();const p = local(e);/* the first press of a double-click is kept: the second one lands where the picture has moved to */- const prev = lastPressRef.current;- if (!prev || e.timeStamp - prev.at > DOUBLE_PRESS_MS) lastPressRef.current = { x: p.x / viewScaleNow(), y: p.y / viewScaleNow(), at: e.timeStamp };+ pressesRef.current = keepPress(pressesRef.current, { x: p.x / viewScaleNow(), y: p.y / viewScaleNow(), at: e.timeStamp });/* ⌘ held: a box to point at in the chat, whatever is under the press (nothing is selected or moved) */if (onRegion && regionKeyHeld(e)) {/* a box for the chat is meant at once, wherever it starts */@@ -2572,9 +2613,31 @@ function FilmStageSelectImpl({currentTarget.setPointerCapture(pointerId);return;}- /* several selected: a press inside their box drags them all (Shift adds / takes away instead) */+ /* several selected: a press inside their box drags them all (Shift adds / takes away instead); its handles scale+ them or turn them together, as Figma does a selection */const many = multiRef.current;const held0 = pickedRef.current;+ const pullKind = groupHit?.getAttribute('data-group-kind');+ if (many && held0 && onTransformLayers && !many.some((it) => lockedClip(it.element.clipLoc)) && (pullKind === 'scale' || pullKind === 'stretch' || pullKind === 'rotate')) {+ const handle = pullKind === 'rotate' ? null : groupHit!.getAttribute('data-group-handle');+ const g = held0.rect;+ const mid = { x: g.left + g.width / 2, y: g.top + g.height / 2 };+ const at = (h: string, far: boolean) => ({+ x: h.includes('e') || h.includes('w') ? g.left + (h.includes('e') !== far ? g.width : 0) : mid.x,+ y: h.includes('n') || h.includes('s') ? g.top + (h.includes('s') !== far ? g.height : 0) : mid.y,+ });+ const items = multiItemsNow();+ layersPull.current = {+ kind: pullKind === 'rotate' ? 'rotate' : 'scale', handle,+ x: p.x, y: p.y, moved: false, items, centers: items.map((it) => ({ x: it.layer.frame.cx, y: it.layer.frame.cy })),+ dx: 0, dy: 0, kx: 1, ky: 1, turn: 0, box0: g,+ anchor: pullKind === 'rotate' ? mid : handle ? at(handle, true) : mid,+ from: handle ? at(handle, false) : { x: 0, y: 0 },+ a0: Math.atan2(stagePt.y - mid.y, stagePt.x - mid.x),+ };+ currentTarget.setPointerCapture(pointerId);+ return;+ }if (many && held0 && !e.shiftKey && onTransformLayers && !many.some((it) => lockedClip(it.element.clipLoc)) && insideBox(scaleBox(held0.rect, vs), p.x, p.y)) {multiDrag.current = { x: p.x, y: p.y, moved: false, rect0: held0.rect, items: multiItemsNow() };currentTarget.setPointerCapture(pointerId);@@ -2630,13 +2693,14 @@ function FilmStageSelectImpl({return;}}- /* about to draw: the previous selection goes first (two boxes on screen would read as two selected), and the- timeline's with it; but a press on a clip selected there (several: no handles) is not a press elsewhere */+ /* about to draw: the previous selection's box goes first (two boxes on screen would read as two selected); the+ inspector and the timeline keep it until the release says what is selected instead (or nothing), so the+ inspector goes from one layer to the next, not through the film's settings. A press on a clip selected on+ the timeline (several: no handles) is not a press elsewhere */cycleFrom.current = e.altKey ? pickedRef.current?.layer?.handle ?? null : null;pressedFrom.current = pickedRef.current?.layer?.handle ?? null;const clearPrevious = () => {dropPick();- onSelectElement?.(null);setHover(null);};const linked = linkedLocs ?? [];@@ -2701,13 +2765,13 @@ function FilmStageSelectImpl({setRegionDraft(regionDraftRef.current);return;}- const held = regionKeyHeld(e) && !gestureRef.current && !layerDrag.current && !multiDrag.current && !draftRef.current;+ const held = regionKeyHeld(e) && !gestureRef.current && !layerDrag.current && !multiDrag.current && !layersPull.current && !draftRef.current;setModAt(held ? p : null);/* nothing is hovered while ⌘ is held: what a box takes in is not one layer */if (held) { setHover(null); return; }}/* Alt held with something in hand: measuring, not hovering */- if (e.altKey && pickedRef.current && !gestureRef.current && !layerDrag.current && !multiDrag.current && !draftRef.current && !cropRef.current) {+ if (e.altKey && pickedRef.current && !gestureRef.current && !layerDrag.current && !multiDrag.current && !layersPull.current && !draftRef.current && !cropRef.current) {measureAt(local(e));setHover(null);return;@@ -2723,26 +2787,38 @@ function FilmStageSelectImpl({if (d.kind === 'move') {d.dx = (p.x - d.x) / vs;d.dy = (p.y - d.y) / vs;- } else if (d.kind === 'rotate') {- /* Shift: in steps of 15°, as a single clip turns */- const turn = ((Math.atan2(at.y - d.anchor.y, at.x - d.anchor.x) - d.a0) * 180) / Math.PI;- d.turn = e.shiftKey ? Math.round(turn / 15) * 15 : turn;- setGroupTurn({ box: d.box0, turn: d.turn });} else {- const ux = d.from.x - d.anchor.x, uy = d.from.y - d.anchor.y;- const along = (v: number, u: number, a: number) => Math.max(0.02, (v - a) / (Math.abs(u) > 1e-6 ? u : 1e-6));- if (d.handle && d.handle.length === 2) {- /* a corner: along the frame's diagonal, both ways alike (a page is never stretched) */- const k = Math.max(0.02, ((at.x - d.anchor.x) * ux + (at.y - d.anchor.y) * uy) / Math.max(1e-6, ux * ux + uy * uy));- d.kx = d.ky = k;- } else if (d.handle === 'e' || d.handle === 'w') d.kx = along(at.x, ux, d.anchor.x);- else d.ky = along(at.y, uy, d.anchor.y);+ /* a corner scales both ways alike (a page is never stretched); Shift turns in steps of 15° */+ pullGroup(d, at, e.shiftKey);+ if (d.kind === 'rotate') setGroupTurn({ box: d.box0, turn: d.turn });}const moved = d.items.map((it) => ({ loc: it.loc, box: it.box, t: groupDragged(d, it.t0, it.box) }));setClipsMoved(moved);clipsPlace.push(d.items.map((it, i) => ({ clip: it.id, box: compactBox(moved[i]!.t, it.box) })));return;}+ if (layersPull.current) {+ const d = layersPull.current;+ const p = local(e);+ const vs = viewScaleNow();+ if (!d.moved && Math.hypot(p.x - d.x, p.y - d.y) < 4) return;+ if (!d.moved) { d.moved = true; setDragging(true); setHover(null); }+ pullGroup(d, { x: p.x / vs, y: p.y / vs }, e.shiftKey);+ if (d.kind === 'rotate') setLayersTurn({ box: d.box0, turn: d.turn });+ /* each one's center goes with the box, each one scaled or turned as much */+ d.items.forEach((it, i) => {+ const m = layerInGroup(d, d.centers[i]!, it.base);+ it.now = { ...it.base, t: movedOffset(it.base, m.dx * vs, m.dy * vs, it.layer.geo.parentR, it.scale), s: m.s, r: m.r };+ });+ multiPaint.push(d.items.map((it) => ({ ...it })));+ /* scaled: the box goes with the hand (turned, it is drawn turned, layersTurn) */+ if (d.kind === 'scale') {+ const b = d.box0;+ const left = d.anchor.x + (b.left - d.anchor.x) * d.kx, top = d.anchor.y + (b.top - d.anchor.y) * d.ky;+ setPicked((cur) => (cur ? { ...cur, rect: { left, top, width: b.width * d.kx, height: b.height * d.ky } } : cur));+ }+ return;+ }if (multiDrag.current) {const d = multiDrag.current;const p = local(e);@@ -2883,7 +2959,20 @@ function FilmStageSelectImpl({maskBox ? outline(layerFrame) : (<StageTransformFrame frame={layerFrame} scope="layer" edges moving={dragging} label={sizeLabel(layerFrame)} bounds={stageBounds} />)- ) : (+ ) : multi && onTransformLayers && !multi.some((it) => lockedClip(it.element.clipLoc)) ? (() => {+ /* several layers: their box's handles scale and turn them together (layersPull) */+ const g = layersTurn?.box ?? picked.rect;+ return (+ <StageTransformFrame+ frame={{ cx: (g.left + g.width / 2) * vsNow, cy: (g.top + g.height / 2) * vsNow, w: g.width * vsNow, h: g.height * vsNow, r: layersTurn?.turn ?? 0 }}+ scope="group"+ edges+ moving={dragging}+ label={layersTurn ? `${Math.round(layersTurn.turn)}°` : `${Math.round(g.width)} × ${Math.round(g.height)}`}+ bounds={stageBounds}+ />+ );+ })() : (/* what cannot be moved (a locked layer, a group, a clip without its size yet): its drawn shape */<Boxrect={scaleBox(picked.layer?.rect ?? picked.rect, vsNow)}diff --git a/packages/openfilm/studio/ui/src/components/MenuSelect.tsx b/packages/openfilm/studio/ui/src/components/MenuSelect.tsxindex 433d93b..c4f310b 100644--- a/packages/openfilm/studio/ui/src/components/MenuSelect.tsx+++ b/packages/openfilm/studio/ui/src/components/MenuSelect.tsx@@ -2,7 +2,9 @@* A field that opens a menu of choices, drawn as Studio's own menus are (a card in the panel's colors, the chosen one* checked), in place of the browser's own list: that one is drawn by the system, light in a dark Studio, and with* the field's text color it read as if every choice were disabled. Choices may come in groups, each under a small- * heading. The arrow keys move through them, Enter picks, Esc closes.+ * heading. The arrow keys move through them, Enter picks, Esc closes. With `onPreview`, the choice under the pointer (or+ * the keys) is shown before it is picked, as Figma shows a font or a blend mode on the canvas; closing without a pick+ * takes it back (`onRevert`).*/import React from 'react';import { Check, ChevronDown } from 'lucide-react';@@ -12,13 +14,17 @@ export type MenuChoice = { value: string; label: string };export type MenuGroup = { label?: string; choices: MenuChoice[] };export function MenuSelect({- label, value, groups, onPick, disabled = false, className = '', size = 'sm', prefix,+ label, value, groups, onPick, onPreview, onRevert, disabled = false, className = '', size = 'sm', prefix,}: {/** what it chooses, for screen readers (the field shows the choice) */label: string;value: string;groups: MenuGroup[];onPick: (value: string) => void;+ /** Show a choice before it is picked (hovered, or reached with the keys). */+ onPreview?: (value: string) => void;+ /** Take the shown choice back: the menu closed without a pick, or the pointer left the list. */+ onRevert?: () => void;disabled?: boolean;className?: string;/** `sm`: the inspector's fields; `md`: a dialog's (the export's) */@@ -33,6 +39,20 @@ export function MenuSelect({const listRef = React.useRef<HTMLDivElement | null>(null);const all = React.useMemo(() => groups.flatMap((g) => g.choices), [groups]);const current = all.find((c) => c.value === value);+ /* a choice is shown on the picture (onPreview), not picked yet. What was chosen when the menu opened: the field+ reads what is on the picture, so while a choice is shown, `value` is that one */+ const previewing = React.useRef(false);+ const origin = React.useRef(value);+ if (!open) origin.current = value;+ const show = (choice: MenuChoice | undefined) => {+ if (!onPreview) return;+ if (choice && choice.value !== origin.current) { previewing.current = true; onPreview(choice.value); }+ else if (previewing.current) { previewing.current = false; onRevert?.(); }+ };+ /* closed without a pick: what was shown goes */+ React.useEffect(() => {+ if (!open && previewing.current) { previewing.current = false; onRevert?.(); }+ }, [open]); // eslint-disable-line react-hooks/exhaustive-deps/* opened: as wide as the field, the chosen one in view and lit for the keys */React.useLayoutEffect(() => {@@ -53,9 +73,12 @@ export function MenuSelect({}, [open, active]);const pick = (choice: MenuChoice) => {+ /* picked: the preview is the pick now (a pick of the one chosen before only takes the preview back) */+ const changed = choice.value !== origin.current;+ if (changed) previewing.current = false;setOpen(false);triggerRef.current?.focus();- if (choice.value !== value) onPick(choice.value);+ if (changed) onPick(choice.value);};const onKeyDown = (e: React.KeyboardEvent) => {if (disabled) return;@@ -63,10 +86,11 @@ export function MenuSelect({if (e.key === 'ArrowDown' || e.key === 'ArrowUp' || e.key === 'Enter' || e.key === ' ') { e.preventDefault(); setOpen(true); }return;}- if (e.key === 'ArrowDown') { e.preventDefault(); setActive((i) => Math.min(all.length - 1, i + 1)); }- else if (e.key === 'ArrowUp') { e.preventDefault(); setActive((i) => Math.max(0, i - 1)); }- else if (e.key === 'Home') { e.preventDefault(); setActive(0); }- else if (e.key === 'End') { e.preventDefault(); setActive(all.length - 1); }+ const go = (i: number) => { e.preventDefault(); setActive(i); show(all[i]); };+ if (e.key === 'ArrowDown') go(Math.min(all.length - 1, active + 1));+ else if (e.key === 'ArrowUp') go(Math.max(0, active - 1));+ else if (e.key === 'Home') go(0);+ else if (e.key === 'End') go(all.length - 1);else if ((e.key === 'Enter' || e.key === ' ') && all[active]) { e.preventDefault(); pick(all[active]); }else if (e.key === 'Tab') setOpen(false);};@@ -97,6 +121,7 @@ export function MenuSelect({role="listbox"aria-label={label}onKeyDown={onKeyDown}+ onMouseLeave={() => show(undefined)}className="relative overflow-y-auto rounded-[8px] border border-[var(--border)] bg-[var(--panel)] p-1 shadow-[var(--shadow-lg)]"style={{ width: Math.max(width, 160), maxHeight: 'min(320px, var(--popover-max-h, 320px))' }}>@@ -118,7 +143,7 @@ export function MenuSelect({role="option"aria-selected={chosen}data-index={i}- onMouseEnter={() => setActive(i)}+ onMouseEnter={() => { setActive(i); show(choice); }}onMouseDown={(e) => e.preventDefault()}onClick={() => pick(choice)}className={`flex w-full items-center gap-2 rounded-[5px] px-2 text-left text-[var(--text)] ${size === 'md' ? 'h-8 text-[13px]' : 'h-7 text-[11.5px]'} ${i === active ? 'bg-[var(--bg-hover)]' : ''}`}diff --git a/packages/openfilm/studio/ui/src/components/ProjectView.tsx b/packages/openfilm/studio/ui/src/components/ProjectView.tsxindex cde233d..79dfe27 100644--- a/packages/openfilm/studio/ui/src/components/ProjectView.tsx+++ b/packages/openfilm/studio/ui/src/components/ProjectView.tsx@@ -40,7 +40,7 @@ import { splitHalves } from '@/lib/timeline-split';import { assetDropInsert, assetRunInsert, assetRunMs, pendingDropOf, soundLaneHit, withPendingBlocks, type AssetDropTarget, type AssetLaneHit, type AssetRunInsert, type PendingDrop } from '@/lib/asset-timeline';import { isComputerFileDrag, type ResourceDragItem } from '@/lib/resource-drag';import { ladderStepMs, THUMB_CELL_PX, thumbAnchorTimes } from '@/lib/thumb-cache';-import { clipThumbId } from '@/lib/thumb-frame';+import { clipThumbId, overridesToken } from '@/lib/thumb-frame';import { useLoadedThumbs } from '@/lib/use-loaded-thumbs';import { droppedOverrides, overrideTarget, undoneFields, type DroppedEdits, type PersonEdit } from '@/lib/dropped-overrides';import { filmFrameBox, type FilmFrameId } from '@/lib/film-frame';@@ -96,6 +96,7 @@ import { FontScopeContext, type FontScope } from '@/lib/use-fonts';import { stageMenu, stageRecoveryMenu, type StageArrangeResult, type StageMenuContext } from './stage-menu';import type { StageEditFailure, StageTextEdit } from './FilmInspector';import { layerTarget, pageClipOf, recoverLayer } from '@/lib/stage-layers';+import { pageClips, pageMayHave, similarEdits } from '@/lib/similar-layers';import type { MaskSession } from '@/lib/stage-mask';import type { MaskShape } from '@/lib/clip-mask';import { nextOverrides, overrideOf, type OverrideGeometry } from '@/lib/film-overrides';@@ -1271,7 +1272,11 @@ export function ProjectView({ projectId, onProject, onLeave }: {const found = doc ? pageClipOf(doc, element) : null;const target = layerTarget(element, allCopies);if (!found || !target) return t('timeline.editFail');- const next = nextOverrides(found.clip.overrides, target, patch);+ let next = nextOverrides(found.clip.overrides, target, patch);+ /* the blocks written inside a line keep their place and look: their overrides go with it, one edit */+ for (const r of await stageControl.current?.riders(element, patch) ?? []) {+ next = nextOverrides(next ?? undefined, { at: r.at, ...(r.n != null ? { n: r.n } : {}) }, r.patch);+ }/* the panel shows the new numbers at once; refused, they go back */const now = next ? overrideOf(next, target) : null;const before = element.override ?? null;@@ -1306,7 +1311,11 @@ export function ProjectView({ projectId, onProject, onLeave }: {const target = layerTarget(element);if (!found || !target) continue;const prev = byClip.has(found.loc) ? byClip.get(found.loc) ?? undefined : found.clip.overrides;- byClip.set(found.loc, nextOverrides(prev, target, patch));+ let next = nextOverrides(prev, target, patch);+ for (const r of await stageControl.current?.riders(element, patch) ?? []) {+ next = nextOverrides(next ?? undefined, { at: r.at, ...(r.n != null ? { n: r.n } : {}) }, r.patch);+ }+ byClip.set(found.loc, next);}if (!byClip.size) return t('timeline.editFail');return editBlocks([...byClip].map(([loc, value]) => ({ loc, prop: 'overrides', value: value as unknown as Record<string, unknown> | null })));@@ -1316,10 +1325,38 @@ export function ProjectView({ projectId, onProject, onLeave }: {if (!held?.clipLoc) return t('timeline.editFail');const clean: Record<string, string | number | null> = {};for (const [key, v] of Object.entries(patch)) clean[key] = v ?? null;- const message = await editStageOverrideRef.current(held, { style: clean } as OverrideGeometry);+ /* kept with what holds the other layers where they are (a wider font would push the words beside it on) */+ const hold = await stageControl.current?.holdStyle(held, clean) ?? {};+ const message = await editStageOverrideRef.current(held, { style: { ...clean, ...hold } } as OverrideGeometry);if (!message) setStageElement((cur) => (cur && (cur.loc === loc || cur.loc?.split('#')[0] === loc) ? { ...cur, style: { ...cur.style, ...patch } } : cur));return message;}, [t]);+ /* "All like it": the style for every layer of its tag and classes, in each page clip that may have one+ (lib/similar-layers), read from the pages' own files */+ const similarClips = React.useCallback(async (element: StageElement): Promise<{ doc: FilmDoc; holds: Set<string> } | null> => {+ const doc = filmRef.current as unknown as FilmDoc | null;+ if (!doc || !element.similar) return null;+ const srcs = [...new Set(pageClips(doc).map((c) => c.src))];+ const sources = await Promise.all(srcs.map((src) => fetch(`${folder}${src.split('/').map(encodeURIComponent).join('/')}`, { cache: 'no-store' })+ .then((r) => (r.ok ? r.text() : ''), () => '')));+ return { doc, holds: new Set(srcs.filter((_, i) => pageMayHave(sources[i]!, element.similar!))) };+ }, [folder]);+ const countSimilar = React.useCallback(async (element: StageElement): Promise<number> => {+ const found = await similarClips(element);+ const own = found ? pageClipOf(found.doc, element)?.loc : undefined;+ return found ? pageClips(found.doc).filter((c) => c.loc === own || found.holds.has(c.src)).length : 0;+ }, [similarClips]);+ const editStageStyleSimilar = React.useCallback(async (element: StageElement, patch: StageElementStyle): Promise<string | null> => {+ const found = await similarClips(element);+ const own = found ? pageClipOf(found.doc, element) : null;+ if (!found || !own || !element.similar) return t('timeline.editFail');+ const clean: Record<string, string | number | null> = {};+ for (const [key, v] of Object.entries(patch)) clean[key] = v ?? null;+ const edits = similarEdits(found.doc, element.similar, clean, (src) => found.holds.has(src), own.loc);+ const message = await editBlocks(edits.map((e) => ({ loc: e.loc, prop: 'overrides', value: e.value as unknown as Record<string, unknown> | null })));+ if (!message) setStageElement((cur) => (cur && cur.loc === element.loc ? { ...cur, style: { ...cur.style, ...patch } } : cur));+ return message;+ }, [similarClips, editBlocks, t]);const previewStageStyle = React.useCallback((_loc: string, patch: StageElementStyle) => {const held = stageElementRef.current;if (held) stageControl.current?.previewStyle(held, patch);@@ -1330,11 +1367,14 @@ export function ProjectView({ projectId, onProject, onLeave }: {}, []);/* `on`: the element typed in, when it is known (typing in place); the inspector edits the one it shows. A double-clickselects and types at once, so the element held may still be the one before: written to that, the edit failed */- const editStageText = React.useCallback(async (e: StageTextEdit, on?: StageElement): Promise<StageEditFailure | null> => {+ /* kept with what holds the other layers where they are (more words would push those beside them on): `hold`, worked+ out as the words were typed in place, else asked for now */+ const editStageText = React.useCallback(async (e: StageTextEdit, on?: StageElement, typedHold?: Record<string, string | null>): Promise<StageEditFailure | null> => {const held = on ?? stageElementRef.current;if (!held?.clipLoc) return { error: t('timeline.editFail') };+ const hold = typedHold ?? await stageControl.current?.holdStyle(held, {}, e.value) ?? {};stageControl.current?.previewText(held, e.value);- const failed = await editStageOverrideRef.current(held, { text: e.value } as OverrideGeometry);+ const failed = await editStageOverrideRef.current(held, { text: e.value, ...(Object.keys(hold).length ? { style: hold } : {}) } as OverrideGeometry);if (failed) { stageControl.current?.previewText(held, e.expect); return { error: failed }; }setStageElement((el) => (el?.text?.loc === e.loc ? { ...el, text: { ...el.text, value: e.value }, label: e.value.slice(0, 40) } : el));return null;@@ -1351,10 +1391,10 @@ export function ProjectView({ projectId, onProject, onLeave }: {void editStageOverrides(list.map(({ element, g }) => ({ element, patch: { t: g.t, s: compactScale(g.s[0], g.s[1]), r: g.r } }))).then((failed) => { if (failed) toast.show(failed); });}, [editStageOverrides, toast]);- const editStageTextInline = React.useCallback(async (element: StageElement, v: string) => {+ const editStageTextInline = React.useCallback(async (element: StageElement, v: string, hold?: Record<string, string | null>) => {const text = element.text;if (!text) return t('timeline.editFail');- const failed = await editStageText({ loc: text.loc, value: v, expect: text.value }, element);+ const failed = await editStageText({ loc: text.loc, value: v, expect: text.value }, element, hold ?? {});if (failed) {setInlineTextFailure((cur) => ({ loc: text.loc, value: v, failure: failed, seq: (cur?.seq ?? 0) + 1 }));return failed.error;@@ -1507,6 +1547,12 @@ export function ProjectView({ projectId, onProject, onLeave }: {for (const b of what.blocks) refer(refOfBlock(b));if (what.range) refer(rangeRef(projectId, what.range, blocksRef.current));}, [projectId]); // eslint-disable-line react-hooks/exhaustive-deps+ /* the ruler points at time for the chat: a click, that moment; a drag, that span */+ const referRuler = React.useCallback((what: { atMs: number } | { range: FilmRange }) => {+ const refer = studioHost?.refer;+ if (!refer) return;+ refer('atMs' in what ? momentRef(projectId, what.atMs) : rangeRef(projectId, what.range, blocksRef.current));+ }, [projectId]); // eslint-disable-line react-hooks/exhaustive-depsconst referFromStage = React.useCallback((picks: StagePick[]) => {const refer = studioHost?.refer;if (!refer) return;@@ -1577,11 +1623,26 @@ export function ProjectView({ projectId, onProject, onLeave }: {/* a sound made again in place is heard as it is now, not as it was decoded */React.useEffect(() => { player.current.setVersions((src) => mtimeOf.get(src)); }, [mtimeOf]);refSource.current = { projectId, version: (path) => mtimeOf.get(path) };+ /* a page clip's pictures are drawn with the changes made inside it (its overrides), by its id: changed, asked again */+ const tweakTokens = React.useMemo(() => {+ const out = new Map<string, string>();+ for (const track of film.film?.tracks ?? []) {+ for (const clip of track.clips) {+ const token = clip.id && filmSrcIsPage(clip.src) ? overridesToken(clip.overrides) : '';+ if (token) out.set(clip.id!, token);+ }+ }+ return out;+ }, [film.film]);const thumbs = React.useMemo(() => {const out = new Map<string, string>();if (!thumbView || !(thumbView.pxPerMs > 0)) return out;- /* a page's frames change with the files it loads too (filesTick: asked again, an unchanged one answered 304) */- const media = (path: string, ms: number) => `/api/projects/${encodeURIComponent(projectId)}/media?what=frame&path=${encodeURIComponent(path)}&ms=${Math.round(ms)}&w=${THUMB_CELL_PX * 2}&v=${mtimeOf.get(path) ?? 0}${filmSrcIsPage(path) ? `.${filesTick}` : ''}`;+ /* a page's frames change with the files it loads too (filesTick: asked again, an unchanged one answered 304), and+ with the clip's own changes (`clip`, and its token in the version) */+ const media = (path: string, ms: number, clip?: string) => {+ const token = clip ? tweakTokens.get(clip) : undefined;+ return `/api/projects/${encodeURIComponent(projectId)}/media?what=frame&path=${encodeURIComponent(path)}&ms=${Math.round(ms)}&w=${THUMB_CELL_PX * 2}&v=${mtimeOf.get(path) ?? 0}${filmSrcIsPage(path) ? `.${filesTick}` : ''}${token ? `.${token}&clip=${encodeURIComponent(clip!)}` : ''}`;+ };const span = thumbView.toMs - thumbView.fromMs;const fromMs = Math.max(0, thumbView.fromMs - span);const toMs = thumbView.toMs + span;@@ -1592,7 +1653,8 @@ export function ProjectView({ projectId, onProject, onLeave }: {const margins: Array<[string, string]> = [];for (const b of pictures) {const still = /\.(png|jpe?g|webp|avif|gif|bmp|svg)$/i.test(b.src!);- const key = (into: number) => `${mtimeOf.get(b.src!) ?? 0}:${clipThumbId(b)}@${into}#v`;+ const token = b.clipId ? tweakTokens.get(b.clipId) ?? '' : '';+ const key = (into: number) => `${mtimeOf.get(b.src!) ?? 0}${token ? `.${token}` : ''}:${clipThumbId(b)}@${into}#v`;if (still) { out.set(key(0), `${folder}${b.src!.split('/').map(encodeURIComponent).join('/')}`); continue; }const speed = b.speed && b.speed > 0 ? b.speed : 1;const trimMs = (b.anchor?.trimFrom ?? 0) * 1000;@@ -1614,14 +1676,14 @@ export function ProjectView({ projectId, onProject, onLeave }: {const own = { startMs: trimMs, endMs: trimMs + (b.endMs - b.startMs) * speed };for (const at of [...grid, ...thumbAnchorTimes({ times: grid, spans: [own] })]) {const drawn = b.kind === 'mg' ? Math.round(Math.max(at, Math.min(at + stepMs / 2, lastMs))) : at;- if (at >= own.startMs - stepMs && at <= own.endMs) out.set(key(at), media(b.src!, drawn));- else margins.push([key(at), media(b.src!, drawn)]);+ if (at >= own.startMs - stepMs && at <= own.endMs) out.set(key(at), media(b.src!, drawn, b.clipId));+ else margins.push([key(at), media(b.src!, drawn, b.clipId)]);}}for (const [k, u] of margins) if (!out.has(k)) out.set(k, u);return out;// eslint-disable-next-line react-hooks/exhaustive-deps- }, [thumbView, timelineTracks, projectId, mtimeOf, folder, filesTick]);+ }, [thumbView, timelineTracks, projectId, mtimeOf, folder, filesTick, tweakTokens]);/* drawn once loaded: a zoom keeps the pictures it has until the new shots arrive */const shownThumbs = useLoadedThumbs(thumbs);const posters = React.useMemo(() => {@@ -2602,6 +2664,8 @@ export function ProjectView({ projectId, onProject, onLeave }: {width={inspectorShownWidth}onClose={closeInspector}onEditStyle={editStageStyle}+ onEditSimilar={editStageStyleSimilar}+ countSimilar={countSimilar}onPreviewStyle={previewStageStyle}onPreviewText={previewStageText}onEditText={editStageText}@@ -2656,7 +2720,7 @@ export function ProjectView({ projectId, onProject, onLeave }: {onRangeChange={setMarkedRange}trackMenuExtra={trackMenuExtra}{...(studioHost?.refer ? {- onReferBlocks: referBlocks, rangeRef: markedRangeRef, onReferSelection: referFromTimeline,+ onReferBlocks: referBlocks, rangeRef: markedRangeRef, onReferSelection: referFromTimeline, onReferRuler: referRuler,} : {})}commandsRef={timelineCommands}trackNames={trackNames}diff --git a/packages/openfilm/studio/ui/src/components/Timeline.tsx b/packages/openfilm/studio/ui/src/components/Timeline.tsxindex 8dac4ba..dd22ad9 100644--- a/packages/openfilm/studio/ui/src/components/Timeline.tsx+++ b/packages/openfilm/studio/ui/src/components/Timeline.tsx@@ -18,6 +18,7 @@ import {Download,Eye,EyeOff,+ Film,ListRestart,Lock,LockOpen,@@ -55,6 +56,7 @@ import {} from 'lucide-react';import { useT } from '@/i18n';import { shortcutHint } from '@/lib/shortcut-hint';+import { ownTrackHeight } from '@/lib/track-heights';import type { Op } from '@/api';import type { ClipEdit } from '@/lib/film-clip-edits';import { laneOfBlock } from '@/lib/timeline-clipboard';@@ -144,6 +146,7 @@ import type { PlaybackClock } from './LiveTime';import { CaptionsIcon } from './player-controls/controls-bar';import { isTypingTarget } from './typing-target';import { Popover } from './Popover';+import { MarkerGlyph } from './TimelineMarkers';import { SUBTITLE_ROW_H, SubtitleRow, SubtitleRowHead, useSubtitleRowShown, type SubtitleRowHandle, type SubtitleRowSpec } from './SubtitleRow';import { SoloButton, TrackResizeHandle } from './timeline-head-extras';import { clipAtPoint, markReplaceTarget } from './replace-drop';@@ -178,6 +181,22 @@ const PLAYHEAD_GRAB_PX = 4;/* the range bar's hit height (the visible bar is thinner, see .tl-range-thumb; 5 px could hardly be hit) */const H_SCROLL_H = 10;const RULER_H = 20;+/** The markers' row under the ruler (only while there are markers). */+const MARKER_ROW_H = 16;++/** A marker's line through the timeline: where, in its color; `held` while it is dragged (else hovered, fainter). */+export interface MarkerGuide { ms: number; color: string; held: boolean }++/** What the markers' row is drawn with (Timeline's `markerLane`). */+export interface MarkerLaneView {+ pxPerMs: number;+ /** the row's length, px */+ widthPx: number;+ /** a time a marker is dragged to, snapped as clips are (when snapping is on) */+ snap: (ms: number) => number;+ /** the line through the timeline where a marker is hovered or dragged (null: none) */+ guide: (g: MarkerGuide | null) => void;+}/*** Row heights. A clip is three layers — info strip (name + length), preview band (thumbnails / waveform), audio band —* and a low waveform shows nothing, while "did this cut land mid-word" can only be seen on it.@@ -233,19 +252,21 @@ const TL = {function laneTone(track: TimelineTrack, selected: boolean): string {if (track.hidden) return TL.head;- return selected ? TL.laneOn : TL.lane;+ const tone = selected ? TL.laneOn : TL.lane;+ /* the main track: a band of its own, a shade toward the text (lighter in the dark, darker in the light) */+ return track.main ? `color-mix(in srgb, ${tone} 93%, var(--tl-text))` : tone;}-/** A row's height: the person's for its track (lib/track-heights), else the timeline's own for its kind. */-function rowH(track: Pick<TimelineTrack, 'kind' | 'height'>): number {- return track.height ?? TRACK_H[track.kind];+/** A row's height: the person's for its track (lib/track-heights), else the timeline's own (the main track taller). */+function rowH(track: Pick<TimelineTrack, 'kind' | 'height' | 'main'>): number {+ return track.height ?? ownTrackHeight(track);}/*** The height that shows every row (toolbar + ruler + rows + padding + range bar). The editor uses it for the* timeline's default height.*/-export function timelineFitHeight(tracks: readonly Pick<TimelineTrack, 'kind' | 'height'>[]): number {+export function timelineFitHeight(tracks: readonly Pick<TimelineTrack, 'kind' | 'height' | 'main'>[]): number {const lanes = tracks.reduce((h, tr) => h + rowH(tr) + GAP, 0);return TOOLBAR_H + RULER_H + lanes + GAP + 2 * TL_VPAD + H_SCROLL_H;}@@ -535,6 +556,12 @@ export interface TimelineProps {* at the playhead (`atMs`) for a point picked on the ruler. Without it the bar has no such button.*/onReferSelection?: (what: { blocks: readonly TimelineBlock[]; range: FilmRange | null; atMs: number | null }) => void;+ /**+ * With the app's chat: the ruler points at time for it. A click there is that moment, a drag that span, each one+ * a pill in the chat; the playhead does not go there (it is dragged by its knob, or a click in the tracks' empty+ * space). Without it the ruler moves the playhead.+ */+ onReferRuler?: (what: { atMs: number } | { range: FilmRange }) => void;/** The bar's Duplicate: copies of the clips right after them. */onDuplicate?: (blocks: readonly TimelineBlock[]) => Promise<string | null>;/** The bar's Play range: from the range's start, stopping at its end. */@@ -646,12 +673,14 @@ export interface TimelineProps {subtitleRow?: SubtitleRowSpec;/** Solo (Studio's playback only): the tracks soloed (film.html indexes), and a click on a head's S (⌥: alone). */solo?: { tracks: ReadonlySet<number>; onToggle: (docIndex: number, only: boolean) => void };- /** Drawn on the ruler, scrolling with it (the markers, components/TimelineMarkers). */- rulerOverlay?: (pxPerMs: number) => React.ReactNode;+ /** The markers' row under the ruler (under the subtitle row when it shows), while there are any. */+ markerLane?: { shown: boolean; render: (lane: MarkerLaneView) => React.ReactNode };+ /** The toolbar's Add marker (left of split); M does the same. */+ onAddMarker?: () => void;+ /** The ruler's own right-click menu, at a moment (film ms): adding a marker there. */+ rulerMenu?: (atMs: number) => ContextMenuEntry[];/** Drawn on each row, over its clips (the clips' markers). */trackOverlay?: (track: TimelineTrack, pxPerMs: number) => React.ReactNode;- /** More buttons in the toolbar, before the zoom (the markers' list). */- toolbarExtra?: React.ReactNode;/** More times a drag snaps to (the markers), ms. */snapPointsMs?: readonly number[];/** A track's head edge dragged (a height, `done` at the end) or double-clicked (null: its own height). */@@ -885,6 +914,7 @@ export const Timeline = React.memo(function Timeline({rangeRef,selectionBar = false,onReferSelection,+ onReferRuler,onDuplicate,onPlayRange,totalMs,@@ -923,9 +953,10 @@ export const Timeline = React.memo(function Timeline({onRenameTrack,subtitleRow,solo,- rulerOverlay,+ markerLane,+ onAddMarker,+ rulerMenu,trackOverlay,- toolbarExtra,snapPointsMs,onResizeTrack,onReplaceDrop,@@ -1262,6 +1293,15 @@ export const Timeline = React.memo(function Timeline({const subRowRef = React.useRef<SubtitleRowHandle | null>(null);const subRowShown = Boolean(subtitleRow) && subRowOn && hasLanes;const subRowH = subRowShown ? SUBTITLE_ROW_H : 0;+ const markerRowH = markerLane?.shown ? MARKER_ROW_H : 0;+ /* a marker hovered or dragged: its line through the whole timeline */+ const [markerGuide, setMarkerGuide] = React.useState<MarkerGuide | null>(null);+ React.useEffect(() => { if (!markerRowH) setMarkerGuide(null); }, [markerRowH]);+ /* a marker dragged snaps as a clip does (to clip edges and the playhead), while snapping is on */+ const snapMarker = React.useCallback((ms: number) => {+ if (!snap) return ms;+ return snapMs(ms, snapTargetsFor(allBlocks, null, timeMsRef.current), pxPerMs).ms;+ }, [snap, allBlocks, pxPerMs]);const drag = useBlockDrag({allBlocks,lanes: tracks,@@ -1608,6 +1648,53 @@ export const Timeline = React.memo(function Timeline({window.addEventListener('pointerup', up);}, [playable, msAtClientX, laneAtY, contentY, tracks, selectOne, onScrubCommit, warmPlayheadAt, setSelected, withLinked, onEditOps, pxPerMs]);+ /*+ * With the app's chat, the ruler points at time for it (onReferRuler): a click there is that moment, a drag that+ * span, drawn as it is pulled. A press on the playhead (its knob, ±4 px) still drags it.+ */+ const [referDraft, setReferDraft] = React.useState<FilmRange | null>(null);+ /* where the pointer is on the ruler (film ms), shown as the moment a click would refer; not near the playhead (its+ knob is dragged there) */+ const [rulerHoverMs, setRulerHoverMs] = React.useState<number | null>(null);+ /* the ruler's own menu, open (rulerMenu) */+ const [rulerMenuAt, setRulerMenuAt] = React.useState<{ x: number; y: number; items: ContextMenuEntry[] } | null>(null);+ const hoverRuler = React.useCallback((e: React.PointerEvent) => {+ if (!onReferRuler || !playable) return;+ /* on a marker's pin: the pin is what a click there is about */+ setRulerHoverMs(playheadGapPx(e.clientX, timeMsRef.current) > PLAYHEAD_GRAB_PX ? Math.max(0, Math.min(totalMs, msAtClientX(e.clientX))) : null);+ }, [onReferRuler, playable, playheadGapPx, totalMs, msAtClientX]);+ const startRefer = React.useCallback((e: React.PointerEvent) => {+ if (e.button !== 0 || !playable || !onReferRuler) return;+ e.preventDefault();+ const startX = e.clientX;+ const at = (clientX: number) => Math.max(0, Math.min(totalMs, msAtClientX(clientX)));+ const fromMs = at(e.clientX);+ let dragged = false;+ let atX = e.clientX;+ let raf = 0;+ const flush = (): void => { raf = 0; setReferDraft(orderedRange(fromMs, at(atX))); };+ const edge = edgeAutoScroll(scrollRef, () => { if (!raf) raf = requestAnimationFrame(flush); });+ const move = (ev: PointerEvent) => {+ atX = ev.clientX;+ if (!dragged && Math.abs(ev.clientX - startX) < DRAG_THRESHOLD_PX) return;+ dragged = true;+ if (!raf) raf = requestAnimationFrame(flush);+ edge.at(ev.clientX);+ };+ const up = (ev: PointerEvent) => {+ window.removeEventListener('pointermove', move);+ window.removeEventListener('pointerup', up);+ edge.stop();+ if (raf) cancelAnimationFrame(raf);+ setReferDraft(null);+ const range = orderedRange(fromMs, at(ev.clientX));+ if (!dragged) onReferRuler({ atMs: fromMs });+ else if (range) onReferRuler({ range });+ };+ window.addEventListener('pointermove', move, { passive: true });+ window.addEventListener('pointerup', up);+ }, [msAtClientX, onReferRuler, playable, totalMs]);+/*** A press within ±4 px of the playhead line in the empty track space grabs the playhead. Not over a clip: a clip a* few pixels wide under the line could not be selected, dragged or trimmed (the ruler and the knob drag the@@ -2071,6 +2158,9 @@ export const Timeline = React.memo(function Timeline({const seekRef = React.useRef<HTMLDivElement | null>(null);const focusTimeline = React.useCallback((e: React.PointerEvent) => {if (e.button !== 0 || isTypingTarget(e.target)) return;+ /* a press in something portaled out of the timeline (a marker's card) is not on it; a marker's pin opens its card,+ which takes the keys */+ if (!e.currentTarget.contains(e.target as Node) || (e.target as Element).closest?.('[data-marker]')) return;window.setTimeout(() => {if (!isTypingTarget(document.activeElement)) seekRef.current?.focus({ preventScroll: true });}, 0);@@ -2223,6 +2313,8 @@ export const Timeline = React.memo(function Timeline({const renamingHere = renaming != null && renaming === track.docIndex && track.index === 1;/* typing a name always has its field; a name given shows when the row has room for it */const nameLine = renamingHere || (Boolean(given) && h >= HEAD_NAME_MIN_H);+ /* the main track is marked by its look (its band, the magnet by its badge), not by a name that reads as one */+ const mainHead = Boolean(track.main) && track.index === 1;/* the track a pill hovered in the app's chat points at */const chatLit = chatTrack != null && track.docIndex === chatTrack;return (@@ -2241,7 +2333,8 @@ export const Timeline = React.memo(function Timeline({onPointerDown={(e) => startLaneReorder(e, laneIndex)}onDoubleClick={() => { if (onRenameTrack && track.docIndex != null && track.index === 1) setRenaming(track.docIndex); }}onContextMenu={(e) => openHeadMenu(e, track)}- title={laneTitle}+ title={mainHead ? `${laneTitle} · ${t('timeline.mainTrack')}` : laneTitle}+ data-main-track={mainHead ? '' : undefined}style={{marginTop: GAP,height: h,@@ -2335,6 +2428,9 @@ export const Timeline = React.memo(function Timeline({) : nameLine && given ? (<span className="col-span-full w-full min-w-0 justify-self-stretch truncate px-1 text-[10.5px] font-medium leading-[14px]" style={{ color: TL.text }} data-track-name="">{given}</span>) : null}+ {mainHead ? (+ <Magnet size={10} aria-label={t('timeline.mainTrack')} className="pointer-events-none absolute bottom-1 left-1.5" style={{ color: 'var(--tl-accent)' }} />+ ) : null}{onResizeTrack && track.docIndex != null ? <TrackResizeHandle height={h} onResize={(height, done) => onResizeTrack(track, height, done)} /> : null}</div>);@@ -3525,6 +3621,12 @@ export const Timeline = React.memo(function Timeline({</>) : null}+ {onAddMarker ? (+ <ToolButton onClick={onAddMarker} disabled={!playable} label={t('editorTools.markerAdd')} hint={shortcutHint('M')}>+ <MarkerGlyph size={16} />+ </ToolButton>+ ) : null}+{onSplit ? (<ToolButtononClick={() => { void runSplit(selectedBlocks.filter((b) => drag.can(b).resize)); }}@@ -3588,7 +3690,6 @@ export const Timeline = React.memo(function Timeline({</ToolButton></span></> : null}- {toolbarExtra}<ToolButtononClick={() => zoomBy(1 / ZOOM_STEP)}@@ -3650,12 +3751,13 @@ export const Timeline = React.memo(function Timeline({{/* as tall as the ruler, so heads line up with rows; no bottom line, it is not a column of its own */}<div style={{ height: RULER_H }} />{subRowShown ? <SubtitleRowHead onAdd={() => subRowRef.current?.addAtPlayhead()} /> : null}+ {markerRowH ? <div style={{ height: markerRowH }} /> : null}<divref={headRef}className="relative overflow-hidden"/* the same padding as the track area, so both scroll the same height and scrollTop stays in sync */style={{- height: `calc(100% - ${RULER_H + subRowH}px - ${H_SCROLL_H}px)`,+ height: `calc(100% - ${RULER_H + subRowH + markerRowH}px - ${H_SCROLL_H}px)`,paddingTop: padTop,paddingBottom: TL_VPAD,}}@@ -3707,15 +3809,25 @@ export const Timeline = React.memo(function Timeline({aria-valuemax={Math.round(totalMs)}aria-valuenow={Math.round(timeMs)}aria-valuetext={`${formatTimecode(timeMs)} / ${formatTimecode(totalMs)}`}- onPointerDown={(e) => (e.shiftKey && onRangeChange ? startRange(e) : startScrub(e, true))}+ onPointerDown={(e) => (e.shiftKey && onRangeChange ? startRange(e)+ : onReferRuler && playheadGapPx(e.clientX, timeMsRef.current) > PLAYHEAD_GRAB_PX ? startRefer(e)+ : startScrub(e, true))}onKeyDown={onKeyDown}+ onPointerMove={hoverRuler}+ onPointerLeave={() => setRulerHoverMs(null)}+ onContextMenu={rulerMenu ? (e) => {+ e.preventDefault();+ const items = rulerMenu(Math.max(0, Math.min(totalMs, msAtClientX(e.clientX))));+ if (items.length) setRulerMenuAt({ x: e.clientX, y: e.clientY, items });+ } : undefined}/* the point picked on the ruler is let go with the timeline's focus */onBlur={() => setBarFocus((f) => (f === 'point' ? null : f))}className="relative h-full touch-none select-none outline-none"- style={{ width: trackPx, cursor: 'col-resize' }}+ /* pointing at time for the chat: a hand, and the moment under it drawn (the playhead's knob keeps its own) */+ style={{ width: trackPx, cursor: onReferRuler ? (rulerHoverMs == null && !referDraft ? 'col-resize' : 'pointer') : 'col-resize' }}>{tickMarks}- {rulerOverlay?.(pxPerMs)}+ {onReferRuler ? <RulerRefer hoverMs={rulerHoverMs} draft={referDraft} pxPerMs={pxPerMs} widthPx={trackPx} /> : null}</div></div>@@ -3725,6 +3837,26 @@ export const Timeline = React.memo(function Timeline({onError={setDropError} />) : null}+ {/* the markers' row: pins along it, scrolling with the tracks; a press between them moves the playhead (and+ drags it) as in the tracks' empty space, a right-click adds a marker there */}+ {markerRowH && markerLane ? (+ <div+ data-marker-lane=""+ onPointerDown={(e) => startScrub(e)}+ className="relative shrink-0 touch-none overflow-hidden select-none"+ style={{ height: markerRowH, background: TL.head }}+ onContextMenu={rulerMenu ? (e) => {+ e.preventDefault();+ const items = rulerMenu(Math.max(0, Math.min(totalMs, msAtClientX(e.clientX))));+ if (items.length) setRulerMenuAt({ x: e.clientX, y: e.clientY, items });+ } : undefined}+ >+ <div className="absolute inset-y-0 left-0" style={{ width: trackPx, transform: `translate3d(${-view.left}px,0,0)` }}>+ {markerLane.render({ pxPerMs, widthPx: trackPx, snap: snapMarker, guide: setMarkerGuide })}+ </div>+ </div>+ ) : null}+{/* track area: backgrounds fixed to the view, clips scroll horizontally above them */}<div className="relative min-h-0 min-w-0 flex-1" style={{ background: TL.head }}>{hasLanes ? (@@ -3757,10 +3889,14 @@ export const Timeline = React.memo(function Timeline({{!hasLanes && !landings.length ? (/* empty timeline: a dashed drop hint. Drops are handled on the whole right column, so dropping on thehint adds the first clip and its track. */- <div className="flex h-full w-full items-center justify-center px-6">- <div className="pointer-events-none flex h-[44px] w-full max-w-[720px] items-center gap-2.5 rounded-[6px] border border-dashed border-[var(--border)] px-4">- <Video size={15} className="shrink-0 text-[var(--text-faint)]" aria-hidden />- <p className="truncate text-[11px] leading-relaxed text-[var(--text-faint)]">+ <div className="flex h-full w-full items-center py-3 pl-1 pr-3">+ <div+ data-timeline-empty=""+ className="pointer-events-none flex h-[60px] w-full items-center gap-2 rounded-[4px] border border-dashed px-4"+ style={{ background: TL.lane, borderColor: TL.line }}+ >+ <Film size={15} className="shrink-0" style={{ color: TL.faint }} aria-hidden />+ <p className="truncate text-[12px]" style={{ color: TL.text }}>{t('timeline.emptyStudio')}</p></div>@@ -3865,6 +4001,24 @@ export const Timeline = React.memo(function Timeline({</div>) : null}+ {/* a marker's guide, through the ruler and every row: where it is while it is hovered or dragged */}+ {markerGuide ? (+ <div+ className="pointer-events-none absolute inset-y-0 z-[25] overflow-hidden"+ style={{ left: headW - PLAYHEAD_BLEED, right: 0, top: TOOLBAR_H, bottom: H_SCROLL_H }}+ >+ <div+ data-marker-guide=""+ className="absolute inset-y-0 left-0 w-0"+ style={{+ borderLeft: `1px dashed ${markerGuide.color}`,+ opacity: markerGuide.held ? 1 : 0.6,+ transform: `translate3d(${markerGuide.ms * pxPerMs - scrollLeft + PLAYHEAD_BLEED}px,0,0)`,+ }}+ />+ </div>+ ) : null}+{/* the snap guide: only when snapped to an edge; a thin accent line, nothing more */}{snapGuideMs != null ? (<div@@ -3956,6 +4110,7 @@ export const Timeline = React.memo(function Timeline({{menu && menuItems.length ? (<ContextMenu x={menu.x} y={menu.y} items={menuItems} onClose={closeMenu} />) : null}+ {rulerMenuAt ? <ContextMenu x={rulerMenuAt.x} y={rulerMenuAt.y} items={rulerMenuAt.items} onClose={() => setRulerMenuAt(null)} /> : null}</div>);});@@ -4346,3 +4501,48 @@ function edgeAutoScroll(},};}++/** A moment as the chat's pills write it: m:ss.s. */+const chatClock = (ms: number): string => {+ const tenths = Math.round(ms / 100);+ const m = Math.floor(tenths / 600);+ return `${m}:${String(((tenths % 600) / 10).toFixed(1)).padStart(4, '0')}`;+};++/**+ * What a click or a drag on the ruler gives the chat, drawn where it will be: under the pointer a line and a tag with+ * the moment (the chat's mark and its time, as its pill will read), while dragged the span and its two ends. In the+ * timeline's accent, as its switches; it takes no pointer.+ */+function RulerRefer({ hoverMs, draft, pxPerMs, widthPx }: { hoverMs: number | null; draft: FilmRange | null; pxPerMs: number; widthPx: number }) {+ if (!draft && hoverMs == null) return null;+ const tag = (x: number, text: string) => {+ const flip = x > widthPx - 96;+ return (+ <span+ className="absolute top-1/2 flex h-[15px] items-center gap-[3px] whitespace-nowrap rounded-[4px] px-[5px] text-[10px] font-medium leading-none tabular-nums text-white shadow-[0_1px_3px_rgba(0,0,0,.35)]"+ style={{ left: x, background: 'var(--tl-accent)', transform: `translate(${flip ? 'calc(-100% - 5px)' : '5px'}, -50%)` }}+ >+ <MessageSquarePlus size={9} aria-hidden />+ {text}+ </span>+ );+ };+ if (draft) {+ const left = draft.startMs * pxPerMs;+ const width = Math.max(1, (draft.endMs - draft.startMs) * pxPerMs);+ return (+ <div aria-hidden className="pointer-events-none absolute inset-0">+ <div className="absolute inset-y-0" style={{ left, width, background: 'color-mix(in srgb, var(--tl-accent) 22%, transparent)', boxShadow: 'inset 1px 0 var(--tl-accent), inset -1px 0 var(--tl-accent)' }} />+ {tag(left + width, `${chatClock(draft.startMs)}–${chatClock(draft.endMs)}`)}+ </div>+ );+ }+ const x = hoverMs! * pxPerMs;+ return (+ <div aria-hidden className="pointer-events-none absolute inset-0">+ <div className="absolute inset-y-0 w-px" style={{ left: x, background: 'var(--tl-accent)' }} />+ {tag(x, chatClock(hoverMs!))}+ </div>+ );+}diff --git a/packages/openfilm/studio/ui/src/components/TimelineMarkers.tsx b/packages/openfilm/studio/ui/src/components/TimelineMarkers.tsxindex 760a3eb..3c278c4 100644--- a/packages/openfilm/studio/ui/src/components/TimelineMarkers.tsx+++ b/packages/openfilm/studio/ui/src/components/TimelineMarkers.tsx@@ -1,180 +1,289 @@/**- * Markers on the timeline (lib/markers): flags on the ruler for the film's, notches on the clips for a clip's, and the- * list of them all behind the toolbar's flag button. A click goes to a marker, a drag moves a flag along the ruler,- * a double-click names it, a right-click names, colors or deletes it.+ * Markers on the timeline (lib/markers): pins on their own row under the ruler for the film's, notches on the clips for+ * a clip's. A click goes to a marker and opens its card (its name, its color, delete), a drag moves a pin along its+ * row, a right-click opens the card too. M (or the toolbar's button) adds one at the playhead (M on one opens its card),+ * ⇧M / ⌥M go to the next / the one before.*/import * as React from 'react';import { createPortal } from 'react-dom';-import { Flag, Trash2 } from 'lucide-react';+import { Trash2 } from 'lucide-react';import { useT } from '@/i18n';-import { MARKER_COLORS, MARKER_PAINT, type Marker, type MarkerColor, type PlacedMarker } from '@/lib/markers';+import { MARKER_COLORS, MARKER_NAME_MAX, MARKER_PAINT, type Marker, type MarkerColor, type PlacedMarker } from '@/lib/markers';import { formatTimecode, snapToFrame } from '@/lib/timecode';-import { shortcutHint } from '@/lib/shortcut-hint';-import { ContextMenu, type ContextMenuEntry } from './ContextMenu';-import { Popover } from './Popover';-import { Tooltip } from './Tooltip';++import type { MarkerLaneView } from './Timeline';export interface MarkerActions {onSeek: (ms: number) => void;onChange: (id: string, change: { name?: string; color?: MarkerColor; t?: number }) => void;onRemove: (id: string) => void;- /** The marker whose name is asked for (M again on a marker), and that it was opened. */+ /** The marker whose card is asked for (M again on a marker), and that it was opened. */naming: string | null;onNamed: () => void;}-/** What is open for one marker: its name being typed (at x, y on the screen), or its menu. */-type Open = { id: string; x: number; y: number; what: 'name' | 'menu' } | null;+/** The card open for one marker, under (x, y) on the screen; `name`: its name field takes the keys at once. */+type Open = { id: string; x: number; y: number; name: boolean } | null;-/** The menu and the name field for markers, shared by the flags and the notches. */-function useMarkerPanels(list: readonly PlacedMarker[], actions: MarkerActions) {- const t = useT();+/** The card shared by the pins and the notches, and what opens it. */+function useMarkerCard(list: readonly PlacedMarker[], actions: MarkerActions) {const [open, setOpen] = React.useState<Open>(null);- const marker = open ? list.find((p) => p.marker.id === open.id)?.marker : undefined;- /* a click goes to the marker a moment later, unless a second click makes it a double-click (its name): gone there- at once, the playhead (its knob on the ruler, its line on the rows) would sit on the marker and take the second */- const pending = React.useRef<{ id: string; timer: number } | null>(null);- React.useEffect(() => () => { if (pending.current) window.clearTimeout(pending.current.timer); }, []);- /** A press on a marker: the second of a double-click names it (true); a first, nothing yet. */- const secondClick = (e: React.PointerEvent, p: PlacedMarker): boolean => {- if (pending.current?.id !== p.marker.id) return false;- window.clearTimeout(pending.current.timer);- pending.current = null;- setOpen({ id: p.marker.id, x: e.clientX, y: e.clientY, what: 'name' });- return true;- };- /** A click on a marker that was not a drag: to it, unless a second click follows. */- const clicked = (p: PlacedMarker) => {- pending.current = { id: p.marker.id, timer: window.setTimeout(() => { pending.current = null; actions.onSeek(p.ms); }, 260) };- };- /* M on a marker already there names it: its field opens under it (the playhead's knob may cover the flag itself) */+ const placed = open ? list.find((p) => p.marker.id === open.id) : undefined;+ /* M on a marker already there: its card, its name field ready (the playhead's knob may cover the pin itself) */const { naming, onNamed } = actions;React.useEffect(() => {if (!naming || !list.some((p) => p.marker.id === naming)) return;const el = document.querySelector(`[data-marker="${CSS.escape(naming)}"], [data-clip-marker="${CSS.escape(naming)}"]`);const r = el?.getBoundingClientRect();- if (r) setOpen({ id: naming, x: r.left + 4, y: r.bottom, what: 'name' });+ if (r) setOpen({ id: naming, x: r.left + r.width / 2, y: r.bottom, name: true });onNamed();}, [naming, list, onNamed]);- const items = (m: Marker): ContextMenuEntry[] => [- { id: 'rename', label: t('editorTools.markerRename'), onSelect: () => setOpen((o) => (o ? { ...o, what: 'name' } : o)) },- {- id: 'color',- label: t('editorTools.markerColor'),- submenu: MARKER_COLORS.map((c) => ({- id: `color:${c}`,- label: t(`editorTools.colors.${c}`),- checked: m.color === c,- icon: <span className="block h-2.5 w-2.5 rounded-full" style={{ background: MARKER_PAINT[c] }} />,- onSelect: () => actions.onChange(m.id, { color: c }),- })),- },- { id: 'sep', separator: true },- { id: 'delete', label: t('editorTools.markerDelete'), danger: true, icon: <Trash2 size={14} />, onSelect: () => actions.onRemove(m.id) },- ];- const panels = marker && open ? (- open.what === 'menu'- ? <ContextMenu x={open.x} y={open.y} items={items(marker)} onClose={() => setOpen((o) => (o?.what === 'menu' ? null : o))} />- : createPortal(- <MarkerNameField marker={marker} x={open.x} y={open.y} onDone={(name) => { if (name != null) actions.onChange(marker.id, { name }); setOpen(null); }} />,- document.body,- )+ /* a marker that went (undone, its clip deleted): its card goes with it */+ React.useEffect(() => { if (open && !placed) setOpen(null); }, [open, placed]);+ const card = placed && open ? createPortal(+ <MarkerCard key={placed.marker.id} placed={placed} x={open.x} y={open.y} focusName={open.name} actions={actions} onClose={() => setOpen(null)} />,+ document.body,) : null;- return { setOpen, panels, secondClick, clicked };+ /** Open a marker's card under its pin. */+ const openAt = (el: Element, p: PlacedMarker, name = false) => {+ const r = el.getBoundingClientRect();+ setOpen({ id: p.marker.id, x: r.left + r.width / 2, y: r.bottom, name });+ };+ return { open, setOpen, openAt, card };}-/** A marker's name, typed in a small field where it was double-clicked: Enter keeps it, Esc leaves it. */-function MarkerNameField({ marker, x, y, onDone }: { marker: Marker; x: number; y: number; onDone: (name: string | null) => void }) {+/**+ * A marker's card: its name (typed in, kept on Enter or when the card is left), its color (kept at a click), where it+ * is, and delete (also Delete / ⌫ while the card has the keys). Esc or a press elsewhere closes it, the name kept.+ */+function MarkerCard({ placed, x, y, focusName, actions, onClose }: {+ placed: PlacedMarker; x: number; y: number; focusName: boolean; actions: MarkerActions; onClose: () => void;+}) {const t = useT();- const done = React.useRef(false);- const finish = (name: string | null) => { if (!done.current) { done.current = true; onDone(name); } };+ const { marker } = placed;+ const box = React.useRef<HTMLDivElement | null>(null);+ const field = React.useRef<HTMLInputElement | null>(null);+ const keepName = () => {+ const name = field.current?.value ?? '';+ if (name.trim() !== (marker.name ?? '')) actions.onChange(marker.id, { name });+ };+ const close = () => { keepName(); onClose(); };+ const closeRef = React.useRef(close);+ closeRef.current = close;+ React.useEffect(() => {+ if (focusName) { field.current?.focus(); field.current?.select(); } else box.current?.focus();+ /* a press elsewhere: the name as typed is kept, the card goes */+ const away = (e: PointerEvent) => { if (!box.current?.contains(e.target as Node)) closeRef.current(); };+ window.addEventListener('pointerdown', away, true);+ return () => window.removeEventListener('pointerdown', away, true);+ }, [focusName]);+ const W = 224;return (- <div className="fixed z-[10001] rounded-[6px] border p-1 shadow-lg" style={{ left: Math.max(8, Math.min(x - 90, window.innerWidth - 200)), top: y + 8, background: 'var(--tl-head)', borderColor: 'var(--tl-line)' }}- data-popover-panel="">- <input- autoFocus- defaultValue={marker.name ?? ''}- maxLength={80}- aria-label={t('editorTools.markerName')}- placeholder={t('editorTools.markerName')}- onFocus={(e) => e.currentTarget.select()}- onKeyDown={(e) => {- e.stopPropagation();- if (e.key === 'Enter') finish(e.currentTarget.value);- if (e.key === 'Escape') finish(null);- }}- onBlur={(e) => finish(e.currentTarget.value)}- className="h-[24px] w-[180px] rounded-[4px] bg-[var(--bg)] px-1.5 text-[12px] text-[var(--text)] outline-none ring-1 ring-[var(--tl-accent)]"- />+ <div+ ref={box}+ tabIndex={-1}+ role="dialog"+ aria-label={marker.name || t('editorTools.markerUntitled')}+ data-popover-panel=""+ data-marker-card={marker.id}+ /* portaled, but React still bubbles its events up to the pin's ruler or clip (which take the press for+ themselves, and the field would never get the keys): the card's presses stay in the card */+ onPointerDown={(e) => e.stopPropagation()}+ onClick={(e) => e.stopPropagation()}+ onDoubleClick={(e) => e.stopPropagation()}+ onContextMenu={(e) => e.stopPropagation()}+ onKeyDown={(e) => {+ /* the card's keys are its own: not the timeline's (Delete there deletes clips) */+ e.stopPropagation();+ if (e.key === 'Escape') { e.preventDefault(); close(); return; }+ if ((e.key === 'Delete' || e.key === 'Backspace') && e.target === box.current) { e.preventDefault(); actions.onRemove(marker.id); onClose(); }+ }}+ className="fixed z-[10001] flex flex-col gap-2 rounded-[8px] border p-2 shadow-[var(--shadow-lg)] outline-none"+ style={{ left: Math.max(8, Math.min(x - W / 2, window.innerWidth - W - 8)), top: y + 6, width: W, background: 'var(--tl-head)', borderColor: 'var(--tl-line)' }}+ >+ <div className="flex items-center gap-1.5">+ <span className="shrink-0"><MarkerGlyph size={13} fill={MARKER_PAINT[marker.color]} /></span>+ <input+ ref={field}+ defaultValue={marker.name ?? ''}+ maxLength={MARKER_NAME_MAX}+ aria-label={t('editorTools.markerName')}+ placeholder={t('editorTools.markerUntitled')}+ onKeyDown={(e) => {+ e.stopPropagation();+ if (e.key === 'Enter') { e.preventDefault(); close(); }+ if (e.key === 'Escape') { e.preventDefault(); close(); }+ }}+ onBlur={keepName}+ className="h-[24px] min-w-0 flex-1 rounded-[4px] bg-[var(--bg)] px-1.5 text-[12px] text-[var(--text)] outline-none ring-1 ring-[var(--tl-line)] focus:ring-[var(--tl-accent)]"+ />+ </div>+ <div className="flex items-center gap-1" role="radiogroup" aria-label={t('editorTools.markerColor')}>+ {MARKER_COLORS.map((c) => (+ <button+ key={c}+ type="button"+ role="radio"+ aria-checked={marker.color === c}+ aria-label={t(`editorTools.colors.${c}`)}+ title={t(`editorTools.colors.${c}`)}+ onClick={() => actions.onChange(marker.id, { color: c })}+ className="flex h-[22px] w-[22px] items-center justify-center rounded-full outline-none transition hover:bg-black/5 focus-visible:ring-1 focus-visible:ring-[var(--tl-accent)] dark:hover:bg-white/10"+ >+ <span className="block h-3 w-3 rounded-full" style={{ background: MARKER_PAINT[c], boxShadow: marker.color === c ? `0 0 0 2px var(--tl-head), 0 0 0 3.5px ${MARKER_PAINT[c]}` : undefined }} />+ </button>+ ))}+ </div>+ <div className="flex items-center justify-between border-t pt-1.5" style={{ borderColor: 'var(--tl-line)' }}>+ <span className="font-mono text-[10.5px] tabular-nums" style={{ color: 'var(--tl-faint)' }}>{formatTimecode(placed.ms)}</span>+ <button+ type="button"+ onClick={() => { actions.onRemove(marker.id); onClose(); }}+ className="flex items-center gap-1 rounded-[4px] px-1.5 py-0.5 text-[11px] outline-none transition hover:bg-black/5 focus-visible:ring-1 focus-visible:ring-[var(--tl-accent)] dark:hover:bg-white/10"+ style={{ color: 'var(--text-dim)' }}+ >+ <Trash2 size={12} aria-hidden />{t('editorTools.markerDelete')}+ </button>+ </div></div>);}-/** The film's markers on the ruler, inside its scrolled strip. */-export function RulerMarkers({ placed, pxPerMs, actions }: { placed: readonly PlacedMarker[]; pxPerMs: number; actions: MarkerActions }) {+/** A marker's name and time, on a tag in its color beside its pin (hovered, or while it is dragged). */+function MarkerTag({ marker, ms, flip }: { marker: Marker; ms: number; flip: boolean }) {const t = useT();- const { setOpen, panels, secondClick, clicked } = useMarkerPanels(placed, actions);+ return (+ <span+ className="pointer-events-none absolute top-1/2 z-[3] flex h-[14px] items-center gap-1 whitespace-nowrap rounded-[3px] px-[5px] text-[10px] font-medium leading-none tabular-nums shadow-[0_1px_3px_rgba(0,0,0,.35)]"+ style={{ left: 0, background: MARKER_PAINT[marker.color], color: '#111', transform: `translate(${flip ? 'calc(-100% - 9px)' : '9px'}, -50%)` }}+ >+ <span className="max-w-[160px] truncate">{marker.name || t('editorTools.markerUntitled')}</span>+ <span style={{ opacity: 0.65 }}>{formatTimecode(ms, { hours: false })}</span>+ </span>+ );+}++/** The marker's shape: a tab pointing down at its moment. Outlined for the toolbar's button, filled for a pin. */+export function MarkerGlyph({ size = 16, fill, ring }: { size?: number; fill?: string; ring?: boolean }) {+ if (!fill) {+ return (+ <svg width={size} height={size} viewBox="0 0 16 16" fill="none" aria-hidden>+ <path d="M4 2.5h8a1 1 0 0 1 1 1v6.3a1 1 0 0 1-.38.78l-4 3.2a1 1 0 0 1-1.24 0l-4-3.2A1 1 0 0 1 3 9.8V3.5a1 1 0 0 1 1-1Z" stroke="currentColor" strokeWidth="1.5" strokeLinejoin="round" />+ </svg>+ );+ }+ return (+ <svg width={size * 0.8} height={size} viewBox="0 0 10 12.5" aria-hidden className="block overflow-visible">+ <path+ d="M1.2.5h7.6a.7.7 0 0 1 .7.7v6.9a.7.7 0 0 1-.26.54L5.44 11.8a.7.7 0 0 1-.88 0L.76 8.64A.7.7 0 0 1 .5 8.1V1.2a.7.7 0 0 1 .7-.7Z"+ fill={fill}+ stroke={ring ? '#fff' : 'rgba(0,0,0,.4)'}+ strokeWidth={ring ? 1.4 : 0.8}+ />+ </svg>+ );+}++/**+ * The film's markers on their row under the ruler: a pin each. Hovered: its name and time, and its line through the+ * timeline. Dragged: it moves (snapping as clips do), its line and time shown all the way. A click goes there and+ * opens its card; a right-click opens the card with its name ready.+ */+export function LaneMarkers({ placed, lane, actions }: { placed: readonly PlacedMarker[]; lane: MarkerLaneView; actions: MarkerActions }) {+ const { open, openAt, card } = useMarkerCard(placed, actions);const [moving, setMoving] = React.useState<{ id: string; ms: number } | null>(null);+ const [hover, setHover] = React.useState<string | null>(null);+ const { pxPerMs, widthPx, snap, guide } = lane;+ /* the click after a drag is not one */+ const wasDragged = React.useRef(false);const press = (e: React.PointerEvent, p: PlacedMarker) => {- if (e.button !== 0) return;- /* the ruler under it scrubs: a flag is pressed on its own */e.preventDefault();e.stopPropagation();- if (secondClick(e, p)) return;+ if (e.button !== 0) return;const el = e.currentTarget as HTMLElement;el.setPointerCapture(e.pointerId);const x0 = e.clientX;+ const color = MARKER_PAINT[p.marker.color];let to = p.ms;let dragged = false;const move = (ev: PointerEvent) => {if (!dragged && Math.abs(ev.clientX - x0) < 4) return;dragged = true;- to = snapToFrame(Math.max(0, p.ms + (ev.clientX - x0) / pxPerMs));+ to = snapToFrame(snap(Math.max(0, p.ms + (ev.clientX - x0) / pxPerMs)));setMoving({ id: p.marker.id, ms: to });+ guide({ ms: to, color, held: true });};const up = () => {el.removeEventListener('pointermove', move);el.removeEventListener('pointerup', up);el.removeEventListener('pointercancel', up);setMoving(null);- if (dragged) actions.onChange(p.marker.id, { t: to / 1000 });- else clicked(p);+ guide(null);+ if (dragged && to !== p.ms) actions.onChange(p.marker.id, { t: to / 1000 });+ wasDragged.current = dragged;};el.addEventListener('pointermove', move);el.addEventListener('pointerup', up);el.addEventListener('pointercancel', up);};+ const film = placed.filter((p) => !p.clip);+ /* where each pin is drawn, left to right: a name runs up to the next pin */+ const xs = film.map((p) => (moving?.id === p.marker.id ? moving.ms : p.ms) * pxPerMs).sort((a, b) => a - b);return (<>- {placed.filter((p) => !p.clip).map((p) => {+ {film.map((p) => {const ms = moving?.id === p.marker.id ? moving.ms : p.ms;- const color = MARKER_PAINT[p.marker.color];- const name = p.marker.name ?? '';+ const x = ms * pxPerMs;+ const on = open?.id === p.marker.id;+ const tagged = moving?.id === p.marker.id || (hover === p.marker.id && !on);+ const room = Math.min(160, (xs.find((o) => o > x) ?? widthPx) - x - 14);return (<divkey={p.marker.id}- className="absolute top-0 z-[2] flex h-full cursor-pointer items-start"- style={{ left: ms * pxPerMs - 1 }}+ className="absolute inset-y-0 z-[2] flex w-[14px] -translate-x-1/2 cursor-ew-resize items-center justify-center"+ /* one at the very start is drawn whole, a few px in (its line stays on the moment) */+ style={{ left: Math.max(x, 7) }}data-marker={p.marker.id}- title={`${name || t('editorTools.markerUntitled')} · ${formatTimecode(ms)}`}+ aria-label={p.marker.name || undefined}onPointerDown={(e) => press(e, p)}+ onPointerEnter={() => {+ setHover(p.marker.id);+ if (!moving) guide({ ms: p.ms, color: MARKER_PAINT[p.marker.color], held: false });+ }}+ onPointerLeave={() => {+ setHover((h) => (h === p.marker.id ? null : h));+ if (!moving) guide(null);+ }}+ onClick={(e) => {+ /* a click: there, and its card */+ e.stopPropagation();+ if (wasDragged.current) { wasDragged.current = false; return; }+ actions.onSeek(p.ms);+ openAt(e.currentTarget, p);+ }}onDoubleClick={(e) => e.stopPropagation()}- onContextMenu={(e) => { e.preventDefault(); e.stopPropagation(); setOpen({ id: p.marker.id, x: e.clientX, y: e.clientY, what: 'menu' }); }}+ onContextMenu={(e) => { e.preventDefault(); e.stopPropagation(); openAt(e.currentTarget, p, true); }}>- {/* the flag: a pole down the ruler and a pennant at its top */}- <span className="block h-full w-[2px]" style={{ background: color }} />- <svg width="9" height="10" viewBox="0 0 9 10" className="block shrink-0" aria-hidden>- <path d="M0 0 H9 L6 4.5 L9 9 H0 Z" fill={color} />- </svg>- {name ? (- <span className="ml-0.5 max-w-[120px] truncate rounded-[2px] px-0.5 text-[9.5px] font-medium leading-[11px]"- style={{ color: '#111', background: color }}>{name}</span>+ <MarkerGlyph size={12} fill={MARKER_PAINT[p.marker.color]} ring={on} />+ {tagged ? (+ <span className="absolute inset-y-0 left-1/2">+ <MarkerTag marker={p.marker} ms={ms} flip={x > widthPx - 180} />+ </span>+ ) : p.marker.name && room >= 16 ? (+ /* its name beside it, as far as the next pin */+ <span+ className="pointer-events-none absolute left-[12px] top-1/2 -translate-y-1/2 truncate whitespace-nowrap text-[10px] leading-none"+ style={{ maxWidth: room, color: 'var(--tl-text)' }}+ data-marker-name=""+ >+ {p.marker.name}+ </span>) : null}</div>);})}- {panels}+ {card}</>);}@@ -188,8 +297,8 @@ export function ClipMarkers({ placed, pxPerMs, height, actions }: {actions: MarkerActions;}) {const t = useT();- const { setOpen, panels, secondClick, clicked } = useMarkerPanels(placed, actions);- if (!placed.length) return null;+ const { openAt, card } = useMarkerCard(placed, actions);+ if (!placed.length) return card;return (<>{placed.map((p) => (@@ -199,97 +308,22 @@ export function ClipMarkers({ placed, pxPerMs, height, actions }: {data-clip-marker={p.marker.id}style={{ left: p.ms * pxPerMs - 4, top: height - 10, width: 8, height: 9 }}title={`${p.marker.name || t('editorTools.markerUntitled')} · ${formatTimecode(p.ms)}`}- onPointerDown={(e) => { if (e.button === 0) { e.preventDefault(); e.stopPropagation(); if (!secondClick(e, p)) clicked(p); } }}+ onPointerDown={(e) => {+ if (e.button !== 0) return;+ e.preventDefault();+ e.stopPropagation();+ actions.onSeek(p.ms);+ openAt(e.currentTarget, p);+ }}onDoubleClick={(e) => e.stopPropagation()}- onContextMenu={(e) => { e.preventDefault(); e.stopPropagation(); setOpen({ id: p.marker.id, x: e.clientX, y: e.clientY, what: 'menu' }); }}+ onContextMenu={(e) => { e.preventDefault(); e.stopPropagation(); openAt(e.currentTarget, p, true); }}><svg width="8" height="9" viewBox="0 0 8 9" aria-hidden className="block"><path d="M0 9 L4 1 L8 9 Z" fill={MARKER_PAINT[p.marker.color]} stroke="rgba(0,0,0,0.45)" strokeWidth="0.8" /></svg></div>))}- {panels}+ {card}</>);}--/** The toolbar's flag button: every marker in time order, to go to, name, color or delete. */-export function MarkersButton({ placed, actions, onAdd, nowMs }: {- placed: readonly PlacedMarker[];- actions: MarkerActions;- onAdd: () => void;- nowMs: number;-}) {- const t = useT();- const [open, setOpen] = React.useState(false);- const label = t('editorTools.markers');- return (- <Popover- open={open}- onOpenChange={setOpen}- placement="auto"- align="end"- trigger={- <Tooltip side="bottom" label={label}>- <button- type="button"- aria-label={label}- aria-expanded={open}- data-markers-button=""- onMouseDown={(e) => e.preventDefault()}- className="relative flex h-[28px] w-[28px] shrink-0 items-center justify-center rounded-[6px] outline-none transition hover:bg-black/5 focus-visible:ring-1 focus-visible:ring-[var(--tl-accent)] dark:hover:bg-white/10"- style={{ color: 'var(--text-dim)' }}- >- <Flag size={15} />- {placed.length ? (- <span className="absolute right-[2px] top-[2px] min-w-[12px] rounded-full px-[3px] text-[8.5px] font-semibold leading-[12px] text-white"- style={{ background: 'var(--tl-accent)' }}>{placed.length}</span>- ) : null}- </button>- </Tooltip>- }- >- <div className="flex max-h-[min(360px,var(--popover-max-h))] w-[300px] flex-col rounded-[8px] border shadow-lg" data-markers-list=""- style={{ background: 'var(--tl-head)', borderColor: 'var(--tl-line)' }}>- <div className="flex items-center justify-between border-b px-2.5 py-1.5" style={{ borderColor: 'var(--tl-line)' }}>- <span className="text-[12px] font-medium" style={{ color: 'var(--tl-text)' }}>{label}</span>- <button type="button" onClick={onAdd} className="rounded-[4px] px-1.5 py-0.5 text-[11px] hover:bg-black/5 dark:hover:bg-white/10" style={{ color: 'var(--tl-text)' }}>- {t('editorTools.markerAdd')} <span style={{ color: 'var(--tl-faint)' }}>M</span>- </button>- </div>- {placed.length ? (- <ul className="min-h-0 flex-1 overflow-y-auto p-1">- {placed.map((p) => (- <li key={p.marker.id} className="group flex items-center gap-1.5 rounded-[4px] px-1.5 py-1"- style={{ background: Math.abs(p.ms - nowMs) < 1 ? 'var(--tl-line)' : undefined }}>- <button type="button" aria-label={t(`editorTools.colors.${p.marker.color}`)} title={t('editorTools.markerColor')}- onClick={() => actions.onChange(p.marker.id, { color: MARKER_COLORS[(MARKER_COLORS.indexOf(p.marker.color) + 1) % MARKER_COLORS.length] })}- className="h-3 w-3 shrink-0 rounded-full" style={{ background: MARKER_PAINT[p.marker.color] }} />- <button type="button" onClick={() => actions.onSeek(p.ms)} className="shrink-0 font-mono text-[10.5px] tabular-nums hover:underline"- style={{ color: 'var(--tl-text)' }}>{formatTimecode(p.ms, { hours: false })}</button>- <input- key={p.marker.name ?? ''}- defaultValue={p.marker.name ?? ''}- placeholder={p.clip ? t('editorTools.markerOnClip').replace('{clip}', p.clip.title) : t('editorTools.markerUntitled')}- aria-label={t('editorTools.markerName')}- maxLength={80}- onKeyDown={(e) => { e.stopPropagation(); if (e.key === 'Enter') e.currentTarget.blur(); }}- onBlur={(e) => { if (e.currentTarget.value.trim() !== (p.marker.name ?? '')) actions.onChange(p.marker.id, { name: e.currentTarget.value }); }}- className="h-[22px] min-w-0 flex-1 rounded-[3px] bg-transparent px-1 text-[11.5px] text-[var(--text)] outline-none focus:bg-[var(--bg)] focus:ring-1 focus:ring-[var(--tl-accent)]"- />- <button type="button" aria-label={t('editorTools.markerDelete')} title={t('editorTools.markerDelete')} onClick={() => actions.onRemove(p.marker.id)}- className="shrink-0 rounded-[3px] p-0.5 opacity-0 transition group-hover:opacity-100 focus:opacity-100 hover:bg-black/5 dark:hover:bg-white/10"- style={{ color: 'var(--text-dim)' }}><Trash2 size={12} /></button>- </li>- ))}- </ul>- ) : (- <p className="px-3 py-3 text-[11.5px] leading-snug" style={{ color: 'var(--tl-faint)' }}>{t('editorTools.markerNone')}</p>- )}- <p className="border-t px-2.5 py-1 text-[10.5px]" style={{ borderColor: 'var(--tl-line)', color: 'var(--tl-faint)' }}>- {t('editorTools.markerKeys').replace('{next}', shortcutHint('M', { shift: true })).replace('{prev}', shortcutHint('M', { alt: true }))}- </p>- </div>- </Popover>- );-}diff --git a/packages/openfilm/studio/ui/src/components/inspector-fields.tsx b/packages/openfilm/studio/ui/src/components/inspector-fields.tsxindex 81a1b6d..61d3817 100644--- a/packages/openfilm/studio/ui/src/components/inspector-fields.tsx+++ b/packages/openfilm/studio/ui/src/components/inspector-fields.tsx@@ -641,12 +641,15 @@ export function Segmented({/** A menu of named choices, as a field. */export function SelectField({- label, value, options, onCommit, prefix,+ label, value, options, onCommit, onPreview, onRevert, prefix,}: {label: string;value: string | Mixed;options: { value: string; label: string }[];onCommit: (next: string) => void;+ /** Show a choice on the picture while it is hovered in the menu; `onRevert` takes it back. */+ onPreview?: (next: string) => void;+ onRevert?: () => void;/** A word before the menu (Blend), as the other fields have their mark. */prefix?: string;}) {@@ -663,7 +666,9 @@ export function SelectField({prefix={prefix}value={current}groups={[{ choices: [...extra, ...options] }]}- onPick={(next) => { if (next !== '' && next !== current) onCommit(next); }}+ /* MenuSelect picks only a choice other than the one when it opened; while one is shown, `current` is that one */+ onPick={(next) => { if (next !== '' && (onPreview || next !== current)) onCommit(next); }}+ {...(onPreview ? { onPreview: (next: string) => { if (next !== '') onPreview(next); }, ...(onRevert ? { onRevert } : {}) } : {})}/></div></Tooltip>diff --git a/packages/openfilm/studio/ui/src/components/inspector-look.tsx b/packages/openfilm/studio/ui/src/components/inspector-look.tsxindex 9e7f5b7..61222d7 100644--- a/packages/openfilm/studio/ui/src/components/inspector-look.tsx+++ b/packages/openfilm/studio/ui/src/components/inspector-look.tsx@@ -589,7 +589,34 @@ function CornerGlyph({ corner }: { corner: Corner }) {* Opacity, corner radius (`radius`: for what has corners — a box, a picture, a line with its own fill) and blend,* as Figma's Appearance.*/+/**+ * What the things in hand looked like before a menu showed something else on the picture (a hovered font, weight,+ * size or blend mode, as Figma shows one on the canvas before it is picked): the stage measures a layer again after+ * each preview, so by then it reads as the preview. Giving up (`restore`) paints that back; a pick (`commit`) keeps it.+ */+function usePreviewed(look: Look) {+ const before = React.useRef<Record<string, string> | null>(null);+ const remember = (props: readonly string[]) => {+ const now = look.common((read) => Object.fromEntries(props.map((p) => [p, read(p) ?? TYPE_NEUTRAL[p] ?? ''])));+ if (now !== MIXED) before.current = { ...now, ...before.current };+ };+ const restore = () => {+ const was = before.current;+ before.current = null;+ if (was) { look.preview(() => was); previewSettled(); } else look.revert();+ };+ /** Show an edit, what it changes remembered first. */+ const preview = (edit: LookEdit) => {+ const keys = look.common((read) => Object.keys(edit(read)).sort());+ remember(keys === MIXED ? [] : keys);+ look.preview(edit);+ };+ const commit = (edit: LookEdit) => { before.current = null; look.commit(edit); };+ return { before, remember, restore, preview, commit };+}+export function AppearanceSection({ look, t, children, radius = false }: { look: Look; t: T; children?: React.ReactNode; radius?: boolean }) {+ const previewed = usePreviewed(look);const opacity = look.common((read) => {const v = Number(read('opacity') ?? 1);return Number.isFinite(v) ? round(v * 100) : 100;@@ -619,7 +646,9 @@ export function AppearanceSection({ look, t, children, radius = false }: { look:prefix={t('inspector.blendShort')}value={blend}options={BLEND_MODES.map((b) => ({ value: b, label: t(`inspector.blendModes.${b}`) }))}- onCommit={(v) => look.commit(() => ({ 'mix-blend-mode': v === 'normal' ? null : v }))}+ onPreview={(v) => previewed.preview(() => ({ 'mix-blend-mode': v }))}+ onRevert={previewed.restore}+ onCommit={(v) => previewed.commit(() => ({ 'mix-blend-mode': v === 'normal' ? null : v }))}/>{children}</Section>@@ -677,6 +706,7 @@ export function FillSection({ look, t, fills, add }: {* so the layout does not move as it is added or thickened.*/export function StrokeSection({ look, t }: { look: Look; t: T }) {+ const previewed = usePreviewed(look);const stroke = look.common((read) => placedStrokeOf(read));const has = stroke !== undefined;const edit = (part: Partial<PlacedStroke> | ((now: PlacedStroke) => Partial<PlacedStroke>)): LookEdit => (read) => {@@ -722,7 +752,9 @@ export function StrokeSection({ look, t }: { look: Look; t: T }) {options={(['inside', 'center', 'outside'] as const).filter((p) => cut === false || p === 'inside' || p === position).map((p) => ({ value: p, label: t(`inspector.strokePositions.${p}`) }))}- onCommit={(v) => look.commit(edit({ position: v as StrokePosition }))}+ onPreview={(v) => previewed.preview(edit({ position: v as StrokePosition }))}+ onRevert={previewed.restore}+ onCommit={(v) => previewed.commit(edit({ position: v as StrokePosition }))}/><NumFieldlabel={t('inspector.strokeWidth')}@@ -1115,20 +1147,12 @@ export function TypographySection({ look, t, svg, sample }: { look: Look; t: T;const known = knownAvailable(family, installed, project, page);return known == null ? !familyRenders(family) : !known;}, [family, installed, project, page, ready]);- /*- * What the type was before a preview showed something else on the picture: the stage measures the layer again after- * each preview, so its values are the preview's by then. Families tried are worked out from it, and giving up paints- * it back.- */- const before = React.useRef<Record<string, string> | null>(null);- const remember = (props: readonly string[]) => {- const now = look.common((read) => Object.fromEntries(props.map((p) => [p, read(p) ?? TYPE_NEUTRAL[p] ?? ''])));- if (now !== MIXED) before.current = { ...now, ...before.current };- };- const restore = () => {- const was = before.current;- before.current = null;- if (was) { look.preview(() => was); previewSettled(); } else look.revert();+ /* families tried are worked out from what the type was before (usePreviewed) */+ const { before, remember, restore, preview, commit } = usePreviewed(look);+ /* a weight (and its slant) from the menu's value; italic taken off a style that had it */+ const weightEdit = (v: string): LookEdit | null => {+ const next = parseStyleValue(v);+ return next ? (read) => ({ 'font-weight': String(next.weight), 'font-style': next.italic ? 'italic' : isItalic(read('font-style')) ? 'normal' : null }) : null;};/*** A new family, the fallbacks kept; a weight or an italic it does not have snapped to what it has. Shown only@@ -1191,10 +1215,9 @@ export function TypographySection({ look, t, svg, sample }: { look: Look; t: T;label={t('inspector.fontWeight')}value={style === MIXED ? MIXED : styleValue(style)}options={weightOptions(styles, t)}- onCommit={(v) => {- const next = parseStyleValue(v);- if (next) look.commit((read) => ({ 'font-weight': String(next.weight), 'font-style': next.italic ? 'italic' : isItalic(read('font-style')) ? 'normal' : null }));- }}+ onPreview={(v) => { const edit = weightEdit(v); if (edit) preview(edit); }}+ onRevert={restore}+ onCommit={(v) => { const edit = weightEdit(v); if (edit) commit(edit); }}/><NumFieldlabel={t('inspector.fontSize')}@@ -1204,7 +1227,14 @@ export function TypographySection({ look, t, svg, sample }: { look: Look; t: T;min={1}max={5000}{...bind(sizeEdit)}- trailing={<FontSizeMenu t={t} onPick={(n) => look.commit(() => ({ 'font-size': `${n}px` }))} />}+ trailing={(+ <FontSizeMenu+ t={t}+ onPreview={(n) => preview(() => ({ 'font-size': `${n}px` }))}+ onRevert={restore}+ onPick={(n) => commit(() => ({ 'font-size': `${n}px` }))}+ />+ )}/></Row><Row>@@ -1256,13 +1286,18 @@ export function TypographySection({ look, t, svg, sample }: { look: Look; t: T;);}-/** The font size field's menu: the common sizes, as Figma's. */-function FontSizeMenu({ t, onPick }: { t: T; onPick: (n: number) => void }) {+/** The font size field's menu: the common sizes, as Figma's; the one under the pointer shown before it is picked. */+function FontSizeMenu({ t, onPick, onPreview, onRevert }: { t: T; onPick: (n: number) => void; onPreview: (n: number) => void; onRevert: () => void }) {const [open, setOpen] = React.useState(false);+ const previewing = React.useRef(false);+ const show = (n: number | null) => {+ if (n != null) { previewing.current = true; onPreview(n); }+ else if (previewing.current) { previewing.current = false; onRevert(); }+ };return (<Popoveropen={open}- onOpenChange={setOpen}+ onOpenChange={(next) => { if (!next) show(null); setOpen(next); }}placement="auto"align="end"trigger={(@@ -1281,6 +1316,7 @@ function FontSizeMenu({ t, onPick }: { t: T; onPick: (n: number) => void }) {data-film-inspector=""role="listbox"aria-label={t('inspector.fontSizes')}+ onMouseLeave={() => show(null)}className="w-[72px] overflow-y-auto rounded-[8px] border border-[var(--border)] bg-[var(--panel)] py-1 shadow-[var(--shadow-lg)]"style={{ maxHeight: 'min(320px, var(--popover-max-h, 320px))' }}>@@ -1290,7 +1326,8 @@ function FontSizeMenu({ t, onPick }: { t: T; onPick: (n: number) => void }) {type="button"role="option"aria-selected={false}- onClick={() => { setOpen(false); onPick(n); }}+ onMouseEnter={() => show(n)}+ onClick={() => { previewing.current = false; setOpen(false); onPick(n); }}className="block w-full px-3 py-[3px] text-left text-[11.5px] tabular-nums text-[var(--text)] hover:bg-[var(--bg-hover)]">{n}diff --git a/packages/openfilm/studio/ui/src/components/settings/AgentsSection.tsx b/packages/openfilm/studio/ui/src/components/settings/AgentsSection.tsxindex 0344e1d..7d4c20d 100644--- a/packages/openfilm/studio/ui/src/components/settings/AgentsSection.tsx+++ b/packages/openfilm/studio/ui/src/components/settings/AgentsSection.tsx@@ -13,9 +13,9 @@* then on (Find automatically goes back). "Why isn't it found?" says what was searched, with a report to copy.*/import React from 'react';-import { Check, Copy, Download, KeyRound, Loader2 } from 'lucide-react';+import { Check, Copy, Download, KeyRound, Loader2, X } from 'lucide-react';import { useLanguage, useT } from '@/i18n';-import { studioHost, type HostAgent, type HostAgentDiagnosis, type HostAgentId, type HostAgentUsage, type HostByokFormat } from '@/lib/host';+import { BYOK_EFFORTS, studioHost, type HostAgent, type HostAgentDiagnosis, type HostAgentId, type HostAgentUsage, type HostByokEffort, type HostByokEffortLevel, type HostByokFormat } from '@/lib/host';/** What is left of the plan: what Codex can say (the app's agent-usage.mjs; Claude Code's own report is wrong for Team accounts). */const PLAN_AGENTS: readonly HostAgentId[] = ['codex'];@@ -342,6 +342,58 @@ const byokModels = (agent: HostAgent) => (agent.byok?.models?.length ? agent.byo/** Model names typed in one field, separated by commas or new lines: trimmed, each once. */const modelNames = (text: string) => [...new Set(text.split(/[,\n]/).map((m) => m.trim()).filter(Boolean))];+/** A model of the own key, with the efforts it offers and the one it starts on; a new one offers Auto alone. */+interface ByokModelRow extends HostByokEffort { name: string }+const byokRows = (agent: HostAgent): ByokModelRow[] =>+ byokModels(agent).map((name) => ({ name, ...(agent.byok?.efforts?.[name] ?? { levels: ['auto'], effort: 'auto' }) }));++/**+ * One model of the own key: its name, the efforts it takes (each a toggle, at least one on) and, when it takes more than+ * one, the one a conversation starts on.+ */+function ByokModel({ row, onChange, onRemove }: { row: ByokModelRow; onChange: (row: ByokModelRow) => void; onRemove: () => void }) {+ const t = useT();+ const name = (level: HostByokEffortLevel) => t(`settings.agents.effortLevels.${level}`);+ const toggle = (level: HostByokEffortLevel) => {+ const on = row.levels.includes(level);+ if (on && row.levels.length === 1) return;+ const levels = BYOK_EFFORTS.filter((l) => (l === level ? !on : row.levels.includes(l)));+ onChange({ ...row, levels, effort: levels.includes(row.effort) ? row.effort : levels[0]! });+ };+ return (+ <div className="flex flex-col gap-2 rounded-[8px] border border-[var(--border)] px-3 py-2.5">+ <div className="flex min-h-8 items-center gap-2">+ <span className="min-w-0 flex-1 truncate font-mono text-[13.5px] text-[var(--text)]" title={row.name}>{row.name}</span>+ {row.levels.length > 1 ? (+ <>+ <span className="shrink-0 text-[13px] text-[var(--text-muted)]">{t('settings.agents.effortStart')}</span>+ <SettingsSelect<HostByokEffortLevel> label={t('settings.agents.effortStart')} value={row.effort} width={128}+ onChange={(effort) => onChange({ ...row, effort })} options={row.levels.map((l) => ({ value: l, label: name(l) }))} />+ </>+ ) : null}+ <button type="button" onClick={onRemove} aria-label={t('settings.agents.removeModel').replace('{name}', row.name)} title={t('settings.agents.removeModel').replace('{name}', row.name)}+ className="flex h-7 w-7 shrink-0 items-center justify-center rounded-[6px] text-[var(--text-faint)] transition hover:bg-[var(--bg-hover)] hover:text-[var(--text)]">+ <X size={14} aria-hidden />+ </button>+ </div>+ <div role="group" aria-label={`${t('settings.agents.efforts')} · ${row.name}`} className="flex flex-wrap items-center gap-1">+ {BYOK_EFFORTS.map((level) => {+ const on = row.levels.includes(level);+ return (+ <button key={level} type="button" aria-pressed={on} onClick={() => toggle(level)}+ className={`inline-flex h-7 items-center gap-1 rounded-full border px-2.5 text-[12.5px] transition ${+ on ? 'border-[var(--text-muted)] bg-[var(--fill-tsp)] text-[var(--text)]' : 'border-[var(--border)] text-[var(--text-muted)] hover:border-[var(--border-strong)] hover:text-[var(--text)]'+ }`}>+ {on ? <Check size={12} aria-hidden /> : null}+ {name(level)}+ </button>+ );+ })}+ </div>+ </div>+ );+}+/*** The person's own key: the API format, its address (the official one, or any service that speaks it), the models (the* chat's menu switches between them) and the key. Checked before it is kept.@@ -352,7 +404,16 @@ function ByokForm({ agent, onDone }: { agent: HostAgent; onDone: () => void }) {const [format, setFormat] = React.useState<HostByokFormat>(agent.byok?.format ?? 'responses');const official = FORMATS.find((f) => f.value === format)!;const [baseUrl, setBaseUrl] = React.useState(agent.byok?.baseUrl ?? official.address);- const [models, setModels] = React.useState(byokModels(agent).join(', '));+ const [rows, setRows] = React.useState<ByokModelRow[]>(() => byokRows(agent));+ const [draft, setDraft] = React.useState('');+ /* names typed (several at once, separated by commas, as before), each added once, offering Auto alone */+ const addModels = () => {+ const names = modelNames(draft).filter((name) => !rows.some((r) => r.name === name));+ if (names.length) setRows((list) => [...list, ...names.map((name): ByokModelRow => ({ name, levels: ['auto'], effort: 'auto' }))]);+ setDraft('');+ };+ /* what is kept: a name still in the field counts as added */+ const named = () => [...rows, ...modelNames(draft).filter((name) => !rows.some((r) => r.name === name)).map((name): ByokModelRow => ({ name, levels: ['auto'], effort: 'auto' }))];const [key, setKey] = React.useState('');const [state, setState] = React.useState<{ busy: boolean; error: string | null }>({ busy: false, error: null });/* another format: its official address, unless the person typed one of their own */@@ -363,11 +424,12 @@ function ByokForm({ agent, onDone }: { agent: HostAgent; onDone: () => void }) {const save = () => {if (!host) return;setState({ busy: true, error: null });- void host.configureByok({ format, baseUrl, models: modelNames(models), ...(key ? { key } : {}) })- .then((r) => { if (r.ok) { setKey(''); setState({ busy: false, error: null }); onDone(); } else setState({ busy: false, error: r.error ?? t('settings.agents.failed') }); })+ const kept = named();+ void host.configureByok({ format, baseUrl, models: kept.map((r) => r.name), efforts: Object.fromEntries(kept.map((r) => [r.name, { levels: r.levels, effort: r.effort }])), ...(key ? { key } : {}) })+ .then((r) => { if (r.ok) { setKey(''); setDraft(''); setState({ busy: false, error: null }); onDone(); } else setState({ busy: false, error: r.error ?? t('settings.agents.failed') }); }).catch((e: unknown) => setState({ busy: false, error: e instanceof Error ? e.message : String(e) }));};- const ready = modelNames(models).length > 0 && baseUrl.trim() && (key.trim() || agent.byok);+ const ready = named().length > 0 && baseUrl.trim() && (key.trim() || agent.byok);const label = (text: string) => <span className="w-[92px] shrink-0 text-[13px] text-[var(--text-muted)]">{text}</span>;return (<SettingsInset>@@ -381,9 +443,20 @@ function ByokForm({ agent, onDone }: { agent: HostAgent; onDone: () => void }) {{label(t('settings.agents.address'))}<SettingsField value={baseUrl} onChange={(e) => setBaseUrl(e.target.value)} placeholder={official.address} aria-label={t('settings.agents.address')} spellCheck={false} /></div>- <div className="flex items-center gap-2">- {label(t('settings.agents.model'))}- <SettingsField value={models} onChange={(e) => setModels(e.target.value)} placeholder={t('settings.agents.modelPlaceholder')} aria-label={t('settings.agents.model')} spellCheck={false} />+ <div className="flex items-start gap-2">+ <span className="flex h-10 items-center">{label(t('settings.agents.model'))}</span>+ <div className="flex min-w-0 flex-1 flex-col gap-2">+ {rows.map((row) => (+ <ByokModel key={row.name} row={row} onChange={(next) => setRows((list) => list.map((r) => (r.name === row.name ? next : r)))}+ onRemove={() => setRows((list) => list.filter((r) => r.name !== row.name))} />+ ))}+ <div className="flex items-center gap-2">+ <SettingsField value={draft} onChange={(e) => setDraft(e.target.value)} placeholder={t('settings.agents.modelPlaceholder')} aria-label={t('settings.agents.model')} spellCheck={false}+ onKeyDown={(e) => { if (e.key === 'Enter' && draft.trim()) { e.preventDefault(); addModels(); } }} />+ <SettingsButton onClick={addModels} disabled={!draft.trim()}>{t('settings.agents.addModel')}</SettingsButton>+ </div>+ {rows.length ? <p className="text-[12.5px] leading-relaxed text-[var(--text-faint)]">{t('settings.agents.effortHint')}</p> : null}+ </div></div><div className="flex items-center gap-2">{label(t('settings.agents.key'))}diff --git a/packages/openfilm/studio/ui/src/components/settings/ProvidersSection.tsx b/packages/openfilm/studio/ui/src/components/settings/ProvidersSection.tsxindex f7ca1da..a0d03e1 100644--- a/packages/openfilm/studio/ui/src/components/settings/ProvidersSection.tsx+++ b/packages/openfilm/studio/ui/src/components/settings/ProvidersSection.tsx@@ -5,11 +5,15 @@* ElevenLabs, OpenAI, Google Gemini, fal.ai, Groq, Pexels, Pixabay, Brave Search and Tavily connect with the person's* own key, which pays that service directly when it charges (a key in the environment shows as such and stays there).* Which of them makes each kind of media, and with which model, is Settings → Models.+ *+ * Under them, the person's own services: any service at an address (one they run on this computer, their own voice,+ * an API not listed), with its key and a few words about what it does. Their agent reaches each with `openfilm get+ * call`; OpenFilm knows nothing of what it answers (studio/server/custom-services.mjs).*/import React from 'react';-import { ExternalLink, Plus } from 'lucide-react';+import { ExternalLink, Plus, Server } from 'lucide-react';import { useT } from '@/i18n';-import { useProviders, type OwnProvider, type ProviderVerb, type ProvidersHook } from '@/lib/providers';+import { useProviders, type OwnProvider, type OwnService, type OwnServiceFields, type ProviderVerb, type ProvidersHook } from '@/lib/providers';import { ProviderMark } from './ProviderMark';import { SettingsButton, SettingsField, SettingsGroup, SettingsInset, SettingsItem, SettingsPage, SettingsStatus, settingsError, settingsLink } from './ui';@@ -27,12 +31,108 @@ export function ProvidersSection() {<SettingsGroup footnote={t('settings.providers.keyNote')}>{rows.map((provider) => <KeyRow key={provider.id} provider={provider} hook={providers} />)}</SettingsGroup>+ <OwnServices hook={providers} />{/* a key that was refused says why under its own field (KeyRow), not down here past the list */}- {providers.problem && !rows.some((p) => p.id === providers.problemFor) ? <p className={settingsError}>{providers.problem}</p> : null}+ {providers.problem && !rows.some((p) => p.id === providers.problemFor) && !ownProblem(providers) ? <p className={settingsError}>{providers.problem}</p> : null}</SettingsPage>);}+/** Whether the last change that failed was to one of the person's own services (it is said under its form). */+const ownProblem = (hook: ProvidersHook) => hook.problemFor === 'custom' || Boolean(hook.state?.custom.some((s) => s.id === hook.problemFor));++/** The person's own services: each with what it does, and a form to add one. */+function OwnServices({ hook }: { hook: ProvidersHook }) {+ const t = useT();+ const services = hook.state?.custom ?? [];+ const [open, setOpen] = React.useState<string | null>(null);+ if (!hook.state) return null;+ return (+ <SettingsGroup title={t('settings.providers.own.title')} footnote={t('settings.providers.own.note')}>+ {services.map((service) => (+ <SettingsItem+ key={service.id}+ mark={<Server size={16} />}+ title={service.name}+ badge={service.last4 ? <SettingsStatus tone="muted"><span className="font-mono tabular-nums">•••• {service.last4}</span></SettingsStatus> : null}+ description={(+ <>+ <span className="font-mono text-[13px] text-[var(--text-faint)]">{service.url}</span>+ {service.note ? <span className="mt-0.5 block whitespace-pre-line">{service.note}</span> : null}+ </>+ )}+ control={open === service.id ? null : (+ <>+ <SettingsButton variant="quiet" disabled={hook.busy === service.id} onClick={() => void hook.removeService(service.id)}>{t('settings.providers.own.remove')}</SettingsButton>+ <SettingsButton onClick={() => setOpen(service.id)}>{t('settings.providers.own.edit')}</SettingsButton>+ </>+ )}+ >+ {open === service.id ? <ServiceForm hook={hook} service={service} onDone={() => setOpen(null)} /> : null}+ </SettingsItem>+ ))}+ {open === '' ? (+ <div className="py-4"><ServiceForm hook={hook} service={null} onDone={() => setOpen(null)} /></div>+ ) : (+ <div className="py-4">+ <SettingsButton onClick={() => setOpen('')}><Plus size={15} strokeWidth={2} />{t('settings.providers.own.add')}</SettingsButton>+ </div>+ )}+ </SettingsGroup>+ );+}++/** A service's name, address, key, where the key goes and what it does: added (`service` null) or changed. */+function ServiceForm({ hook, service, onDone }: { hook: ProvidersHook; service: OwnService | null; onDone: () => void }) {+ const t = useT();+ const [fields, setFields] = React.useState<OwnServiceFields>({+ name: service?.name ?? '', url: service?.url ?? '', auth: service?.auth ?? DEFAULT_AUTH, note: service?.note ?? '',+ });+ const [key, setKey] = React.useState('');+ const [dropKey, setDropKey] = React.useState(false);+ const busy = hook.busy === (service?.id ?? 'custom');+ const set = (name: keyof OwnServiceFields) => (e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => setFields((f) => ({ ...f, [name]: e.target.value }));+ const save = async () => {+ if (busy || !fields.name.trim() || !fields.url.trim()) return;+ /* changed without a key typed: the one it has stays (unless taken away) */+ const sent: OwnServiceFields = { ...fields, ...(key.trim() ? { key: key.trim() } : dropKey || !service ? { key: '' } : {}) };+ if (await hook.saveService(service?.id ?? null, sent)) onDone();+ };+ const label = (text: string, field: React.ReactNode) => (+ <label className="flex flex-col gap-1.5 text-[13.5px] text-[var(--text-muted)]">{text}{field}</label>+ );+ return (+ /* under its row, it lines up with the row's words; a new one stands on its own */+ <form className={service ? 'pl-[52px]' : ''} onSubmit={(e) => { e.preventDefault(); void save(); }} onKeyDown={(e) => { if (e.key === 'Escape') { e.preventDefault(); onDone(); } }}>+ <SettingsInset>+ {label(t('settings.providers.own.name'), <SettingsField autoFocus value={fields.name} onChange={set('name')} placeholder={t('settings.providers.own.namePlaceholder')} />)}+ {label(t('settings.providers.own.url'), <SettingsField value={fields.url} onChange={set('url')} placeholder="http://localhost:8000" spellCheck={false} className="font-mono placeholder:font-sans" />)}+ {label(t('settings.providers.own.key'), (+ <SettingsField type="password" autoComplete="off" spellCheck={false} value={key} onChange={(e) => { setKey(e.target.value); setDropKey(false); }}+ placeholder={service?.last4 && !dropKey ? t('settings.providers.own.keyKeep').replaceAll('{last4}', service.last4) : t('settings.providers.own.keyOptional')}+ className="font-mono placeholder:font-sans" />+ ))}+ {service?.last4 && !key && !dropKey ? (+ <button type="button" className={`${settingsLink} self-start text-[13.5px]`} onClick={() => setDropKey(true)}>{t('settings.providers.own.removeKey')}</button>+ ) : null}+ {label(t('settings.providers.own.auth'), <SettingsField value={fields.auth} onChange={set('auth')} spellCheck={false} className="font-mono" />)}+ {label(t('settings.providers.own.what'), (+ <textarea value={fields.note} onChange={set('note')} rows={3} maxLength={600} placeholder={t('settings.providers.own.whatPlaceholder')}+ className="w-full min-w-0 resize-y rounded-[8px] border border-[var(--border)] bg-transparent px-3 py-2 text-[14px] leading-[1.5] text-[var(--text)] outline-none transition placeholder:text-[var(--text-faint)] hover:border-[var(--border-strong)] focus:border-[var(--text-muted)]" />+ ))}+ {hook.problem && hook.problemFor === (service?.id ?? 'custom') ? <p className={settingsError} role="alert">{hook.problem}</p> : null}+ <div className="flex justify-end gap-2">+ <SettingsButton variant="quiet" onClick={onDone}>{t('settings.providers.cancel')}</SettingsButton>+ <SettingsButton type="submit" variant="primary" disabled={busy || !fields.name.trim() || !fields.url.trim()}>{t('settings.providers.save')}</SettingsButton>+ </div>+ </SettingsInset>+ </form>+ );+}++/** Where a key goes unless the person says otherwise (the server's DEFAULT_AUTH). */+const DEFAULT_AUTH = 'Authorization: Bearer {key}';+/** A provider in the list: its mark, its name (and what is next to it), a line about it, and what can be done. */function Row({ id, name, badge, children, action, below }: {id: string;diff --git a/packages/openfilm/studio/ui/src/components/use-editor-tools.tsx b/packages/openfilm/studio/ui/src/components/use-editor-tools.tsxindex 412cabb..559e428 100644--- a/packages/openfilm/studio/ui/src/components/use-editor-tools.tsx+++ b/packages/openfilm/studio/ui/src/components/use-editor-tools.tsx@@ -5,7 +5,7 @@* here; the edits go through the page's own edit, one write and one undo step each.*/import * as React from 'react';-import { AudioLines, Flag, HardDriveUpload, Replace, Snowflake } from 'lucide-react';+import { AudioLines, HardDriveUpload, Replace, Snowflake } from 'lucide-react';import { api, type Clip, type Film, type Op, type ProjectSettings } from '@/api';import { useT } from '@/i18n';@@ -22,11 +22,12 @@ import type { KeyCommand } from '@/lib/timeline-keys';import type { TimelineBlock, TimelineTrack } from '@/lib/timeline-layout';import type { MediaListing } from '@/lib/workspace-resources';import { followOf, readTimelinePrefs } from '@/lib/timeline-prefs';-import { sizeOf, trackHeightsOf, withTrackHeight, TRACK_SIZES, type TrackSize } from '@/lib/track-heights';+import { ownTrackHeight, sizeOf, trackHeightsOf, withTrackHeight, TRACK_SIZES, type TrackSize } from '@/lib/track-heights';import type { Player } from '@/editor/player';import type { OutsideStep } from '@/editor/use-film';import type { ContextMenuEntry } from './ContextMenu';-import { ClipMarkers, MarkersButton, RulerMarkers, type MarkerActions } from './TimelineMarkers';+import type { MarkerLaneView } from './Timeline';+import { ClipMarkers, LaneMarkers, MarkerGlyph, type MarkerActions } from './TimelineMarkers';type Toast = { show: (text: string) => void; showError: (text: string) => void };@@ -34,8 +35,6 @@ type Toast = { show: (text: string) => void; showError: (text: string) => void }const REPLACE_PROBE_MS = 8000;const REPLACE_GIVE_UP_MS = 30_000;-/** The timeline's own heights, by kind of track (Timeline's TRACK_H). */-const OWN_H = TRACK_SIZES.medium;/** Whether a clip on the timeline is heard: a sound, or a video with a sound of its own that is not turned off. */const sounding = (b: TimelineBlock) => Boolean(b.clipId && b.loc && b.src) && (@@ -110,8 +109,9 @@ export function useEditorTools({placedRef.current = placed;const snapPointsMs = React.useMemo(() => placed.map((p) => p.ms), [placed]);- const addMarkerHere = React.useCallback((on?: TimelineBlock | null) => {- const atMs = timeRef.current;+ /* `atMs`: where (the playhead when not said) */+ const addMarkerHere = React.useCallback((on?: TimelineBlock | null, at?: number) => {+ const atMs = at ?? timeRef.current;const clip = on === undefined ? markerClip(tracksRef.current, selectedClipIds, atMs) : on;const { list, added } = addMarker(markersRef.current, atMs, clip);/* one there already: M again names it, as Premiere's */@@ -171,12 +171,12 @@ export function useEditorTools({if (track.docIndex == null) return;if (!done && height != null) { setLive({ docIndex: track.docIndex, height }); return; }/* back to its own height, or the one dragged to (the timeline's own one is not kept) */- setHeights([track.docIndex], height == null || height === OWN_H[track.kind] ? null : height);+ setHeights([track.docIndex], height == null || height === ownTrackHeight(track) ? null : height);}, [setHeights]);const headMenuExtra = React.useCallback((track: TimelineTrack): ContextMenuEntry[] => {const docIndex = track.docIndex;if (docIndex == null) return [];- const now = heights.get(docIndex) ?? OWN_H[track.kind];+ const now = heights.get(docIndex) ?? ownTrackHeight(track);const size = sizeOf(now, track.kind);const sizes = (Object.keys(TRACK_SIZES) as TrackSize[]);const all = filmRef.current?.tracks.map((_, i) => i) ?? [];@@ -361,7 +361,7 @@ export function useEditorTools({submenu: choices,});const on = atMs >= one.startMs && atMs < one.endMs;- out.push({ id: 'clip-marker', label: t('editorTools.markerAddClip'), icon: <Flag size={14} />, shortcut: 'M', disabled: !on, hint: t('editorTools.freezeOutside'), onSelect: () => addMarkerHere(one) });+ out.push({ id: 'clip-marker', label: t('editorTools.markerAddClip'), icon: <MarkerGlyph size={14} />, shortcut: 'M', disabled: !on, hint: t('editorTools.freezeOutside'), onSelect: () => addMarkerHere(one) });}return out;}, [measuring, media, normalize, freeze, replaceEntries, addMarkerHere, timeRef, t]);@@ -376,17 +376,19 @@ export function useEditorTools({}return by;}, [placed, tracks]);- const rulerOverlay = React.useCallback((pxPerMs: number) => <RulerMarkers placed={placed} pxPerMs={pxPerMs} actions={markerActions} />, [placed, markerActions]);+ const markerLane = React.useMemo(() => ({+ shown: placed.some((p) => !p.clip),+ render: (lane: MarkerLaneView) => <LaneMarkers placed={placed} lane={lane} actions={markerActions} />,+ }), [placed, markerActions]);+ const onAddMarker = React.useCallback(() => addMarkerHere(), [addMarkerHere]);+ /* the ruler's own menu: a marker there (on the film), as M puts one at the playhead */+ const rulerMenu = React.useCallback((atMs: number): ContextMenuEntry[] => [+ { id: 'add-marker', label: t('editorTools.markerAdd'), icon: <MarkerGlyph size={14} />, shortcut: 'M', onSelect: () => addMarkerHere(null, atMs) },+ ], [addMarkerHere, t]);const trackOverlay = React.useCallback((track: TimelineTrack, pxPerMs: number) => {const here = clipMarkersByTrack.get(track.lane);- return here ? <ClipMarkers placed={here} pxPerMs={pxPerMs} height={track.height ?? OWN_H[track.kind]} actions={markerActions} /> : null;+ return here ? <ClipMarkers placed={here} pxPerMs={pxPerMs} height={track.height ?? ownTrackHeight(track)} actions={markerActions} /> : null;}, [clipMarkersByTrack, markerActions]);- const [nowMs, setNowMs] = React.useState(0);- const toolbarExtra = (- <span onPointerDown={() => setNowMs(timeRef.current)}>- <MarkersButton placed={placed} actions={markerActions} onAdd={() => addMarkerHere()} nowMs={nowMs} />- </span>- );/** the mix's level now, for the meters */const levels = React.useCallback(() => player.current.levels(), [player]);@@ -394,7 +396,7 @@ export function useEditorTools({return {/** the timeline's tracks with the heights they are drawn at */tracks: shownTracks,- timeline: { solo: soloProp, rulerOverlay, trackOverlay, toolbarExtra, snapPointsMs, onResizeTrack, onReplaceDrop },+ timeline: { solo: soloProp, markerLane, onAddMarker, rulerMenu, trackOverlay, snapPointsMs, onResizeTrack, onReplaceDrop },menuExtra,/** what can replace a clip (the inspector's Replace) */replaceEntries,diff --git a/packages/openfilm/studio/ui/src/components/useBlockDrag.ts b/packages/openfilm/studio/ui/src/components/useBlockDrag.tsindex 592875c..a2b01e5 100644--- a/packages/openfilm/studio/ui/src/components/useBlockDrag.ts+++ b/packages/openfilm/studio/ui/src/components/useBlockDrag.ts@@ -417,10 +417,11 @@ export function useBlockDrag(opts: {? flushShiftMs({ startMs: out.startMs, endMs: out.endMs }, destLane?.occupied ?? [], SNAP_THRESHOLD_PX / pxPerMs): null;/* along its own track (alone, or with its linked clips, none changing track), onto another clip: it goes in- between instead of cutting it (moveEditMode) */+ between instead of cutting it (moveEditMode). Onto another, not one in hand: those move with it (dragged past+ the gap to the next selected clip, it is not landing on that one) */const alongOwnTrack = grouped ? shift.ok && shift.delta === 0 : drop.action === 'stay';const wouldLand = { startMs: out.startMs + (flush?.shiftMs ?? 0), endMs: out.endMs + (flush?.shiftMs ?? 0) };- const overlaps = alongOwnTrack && rangeOverlaps(wouldLand, singleLanes[homeLaneIndex]?.occupied ?? []);+ const overlaps = alongOwnTrack && rangeOverlaps(wouldLand, groupLanes[homeLaneIndex]?.occupied ?? []);/* the track it lands on: the magnet inserts on the main track; elsewhere with the magnet it covers nothing (a cliplanding on another goes to a free track, see planMove), ⌥ inserting there */const landsOn = groupOpens != null ? null : drop.action === 'move' ? drop.trackIndex : drop.action === 'insert' ? null : homeLane?.docIndex;diff --git a/packages/openfilm/studio/ui/src/editor/Preview.tsx b/packages/openfilm/studio/ui/src/editor/Preview.tsxindex 83e41f0..c7da8ee 100644--- a/packages/openfilm/studio/ui/src/editor/Preview.tsx+++ b/packages/openfilm/studio/ui/src/editor/Preview.tsx@@ -52,7 +52,8 @@ export type PreviewHandle = {* while the picture had focus (not while typing in it), or another load of the film is shown (its handles are new).*/export type StageEvent =- | { event: 'text'; handle: number; commit: boolean; value?: string; before?: string }+ /* `hold`: what kept the other layers in place as the words were typed (style for the same override, null takes a key away) */+ | { event: 'text'; handle: number; commit: boolean; value?: string; before?: string; hold?: Record<string, string | null> }| { event: 'key'; key: KeyboardEvent }| { event: 'reset' };@@ -229,7 +230,11 @@ export const Preview = forwardRef<PreviewHandle, Props>(function Preview({ foldetell({ event: 'key', key });onKeyRef.current?.(key);} else if (m.event === 'text') {- tell({ event: 'text', handle: Number(m.handle), commit: Boolean(m.commit), ...(typeof m.value === 'string' ? { value: m.value, before: String(m.before ?? '') } : {}) });+ tell({+ event: 'text', handle: Number(m.handle), commit: Boolean(m.commit),+ ...(typeof m.value === 'string' ? { value: m.value, before: String(m.before ?? '') } : {}),+ ...(m.hold && typeof m.hold === 'object' ? { hold: m.hold as Record<string, string | null> } : {}),+ });}} else if (m.type === 'error') {setFrames((list) => list.map((f) => (f.key === key ? { ...f, error: String(m.message) } : f)));diff --git a/packages/openfilm/studio/ui/src/editor/undo.test.ts b/packages/openfilm/studio/ui/src/editor/undo.test.tsindex cacd6fc..4d3a5cf 100644--- a/packages/openfilm/studio/ui/src/editor/undo.test.ts+++ b/packages/openfilm/studio/ui/src/editor/undo.test.ts@@ -73,6 +73,24 @@ test('a track the edit emptied comes back with its flags: muted stays muted, hidroundTrip(locked, [{ op: 'remove', clip: 'v' }]);});+test('a whole track deleted comes back as one track, its clips in their places, not a track each', () => {+ const pages = [{ src: 'p1.html', id: 'p1' }, { src: 'p2.html', id: 'p2', at: 8 }, { src: 'p3.html', id: 'p3', at: 12 }, { src: 'p4.html', id: 'p4', at: 26 }];+ const f = film(pages, [{ src: 'v.mp4', id: 'v', time: [0, 90] }], [{ src: 'w.wav', id: 'w1', at: 7 }, { src: 'w.wav', id: 'w2', at: 11 }], [{ src: 'm.wav', id: 'm' }]);+ const remove = (...clips: string[]): Op[] => clips.map((clip) => ({ op: 'remove', clip }));+ const gone = apply(f, remove('p1', 'p2', 'p3', 'p4'));+ assert.equal(gone.tracks.length, 3);+ assert.equal(apply(gone, inverse(f, gone, remove('p1', 'p2', 'p3', 'p4'))).tracks.length, 4);+ roundTrip(f, remove('p1', 'p2', 'p3', 'p4'));+ /* two whole tracks, one between tracks still there, in any order */+ roundTrip(f, remove('w2', 'p3', 'p1', 'w1', 'p4', 'p2'));+ /* and with a clip left on the track below moved meanwhile (its index is the one after the delete) */+ roundTrip(f, [...remove('p4', 'p1', 'p2', 'p3', 'w1', 'w2'), ...props(['m', 'at', 2])]);+ /* a muted track emptied keeps its flag, once */+ const muted = structuredClone(f);+ muted.tracks[2].muted = true;+ roundTrip(muted, remove('w1', 'w2'));+});+test('undoing a delete the agent already put back: the clip is not put back twice', () => {const f = film([{ src: 'a.html', id: 'a' }, { src: 'a.html', id: 'a2', at: 7.5 }]);const after = apply(f, [{ op: 'remove', clip: 'a2' }]);diff --git a/packages/openfilm/studio/ui/src/editor/undo.ts b/packages/openfilm/studio/ui/src/editor/undo.tsindex 785aebc..a530a28 100644--- a/packages/openfilm/studio/ui/src/editor/undo.ts+++ b/packages/openfilm/studio/ui/src/editor/undo.ts@@ -94,25 +94,63 @@ export function inverse(before: Film, after: Film, ops: Op[]): Op[] {const flagsBack = (home: ReturnType<typeof trackNow>, track: number) => {if (!('newTrack' in home)) return;for (const field of ['locked', 'hidden', 'muted'] as const) {- if (before.tracks[track]?.[field]) undo.push({ op: 'track', track: home.newTrack, field, value: true });+ if (before.tracks[track]?.[field]) undo.push(goneTrack({ op: 'track', track: home.newTrack, field, value: true }, track));}};+ /* the ops that make a track the edit left empty again, by the track it was in `before` */+ const gone = new Map<Op, number>();+ const goneTrack = (op: Op, track: number): Op => { gone.set(op, track); return op; };for (const id of ids) {const old = was.get(id);const cur = now.get(id);if (!old && cur) { undo.push({ op: 'remove', clip: id }); continue; }if (!old) continue;const home = trackNow(before, after, old.track, ids);- if (!cur) { flagsBack(home, old.track); undo.push({ op: 'insert', clip: copy(old.row), ...home }); continue; }+ if (!cur) {+ flagsBack(home, old.track);+ const op: Op = { op: 'insert', clip: copy(old.row), ...home };+ undo.push('newTrack' in home ? goneTrack(op, old.track) : op);+ continue;+ }if (!('track' in home && home.track === cur.track) || (old.row.at ?? 0) !== (cur.row.at ?? 0)) {flagsBack(home, old.track);- undo.push({ op: 'move', clip: id, at: old.row.at ?? 0, ...home });+ const op: Op = { op: 'move', clip: id, at: old.row.at ?? 0, ...home };+ undo.push('newTrack' in home ? goneTrack(op, old.track) : op);}const then = old.row as Record<string, unknown>;const nowRow = cur.row as Record<string, unknown>;const changed = [...new Set([...fieldsOf(old.row), ...fieldsOf(cur.row)])].filter((field) => JSON.stringify(then[field]) !== JSON.stringify(nowRow[field]));if (changed.length) undo.push({ op: 'fields', clip: id, fields: Object.fromEntries(changed.map((field) => [field, field in then ? copy(then[field]) : null])) });}- return undo.reverse();+ return reopenTracks(undo.reverse(), gone);}+/**+ * A track the edit left empty comes back once, with all of its clips: the first of them opens it (`newTrack`), the+ * others go onto it (`track`), or each would open a track of its own (deleting a whole track's clips and undoing put+ * every clip on a new track). These go last, after what works on tracks still there (their indexes are `after`'s, and+ * a track opened before them would shift them), from the top track down: a `newTrack` is a place among `after`'s+ * tracks, and each track reopened above it moves it down one.+ */+function reopenTracks(ops: Op[], gone: ReadonlyMap<Op, number>): Op[] {+ const out: Op[] = ops.filter((op) => !gone.has(op));+ const byTrack = new Map<number, Op[]>();+ for (const op of ops) {+ const was = gone.get(op);+ if (was != null) byTrack.set(was, [...(byTrack.get(was) ?? []), op]);+ }+ let above = 0;+ for (const was of [...byTrack.keys()].sort((a, b) => a - b)) {+ let at: number | null = null;+ for (const op of byTrack.get(was)!) {+ if (op.op === 'track') { if (at != null) out.push({ ...op, track: at }); continue; }+ if (op.op !== 'insert' && op.op !== 'move') continue;+ const { newTrack, ...rest } = op;+ if (at != null) { out.push({ ...rest, track: at }); continue; }+ at = (newTrack ?? 0) + above;+ out.push({ ...rest, newTrack: at });+ }+ if (at != null) above += 1;+ }+ return out;+}diff --git a/packages/openfilm/studio/ui/src/i18n/en.ts b/packages/openfilm/studio/ui/src/i18n/en.tsindex e41cacf..b2ed513 100644--- a/packages/openfilm/studio/ui/src/i18n/en.ts+++ b/packages/openfilm/studio/ui/src/i18n/en.ts@@ -904,6 +904,10 @@ export const en = {scope: 'Apply to',scopeOne: 'This copy (#{i})',scopeAll: 'All {n} copies',+ scopeThis: "This text",+ scopeSimilar: "All like it",+ scopeSimilarIn: "All like it · {n}",+ scopeSimilarHint: "Its look (color, size, font…) goes to every text like it in the film: the same tag and classes, in every page. Words and places stay each one’s.",timing: 'Timing',mixed: 'Mixed',clearOverride: 'Clear my style changes',@@ -1186,6 +1190,7 @@ export const en = {snap: 'Snap',snapOn: 'On: drags snap to clip edges, the playhead and markers',snapOff: 'Off: clips land exactly where you let go',+ mainTrack: 'Main track',magnet: 'Main track magnet',magnetOn: 'On: clips on the main track stay end to end, with no gaps',magnetOff: 'Off: clips on the main track stay where you put them',@@ -1232,18 +1237,13 @@ export const en = {solo: "Solo: hear this track alone, here only (⌥-click: only this one)",unsolo: "Stop soloing this track",resizeTrack: "Drag to make the track taller or shorter · double-click: its usual height",- markers: "Markers",markerAdd: "Add marker",markerAddClip: "Add marker on clip",markerAddedOn: "Marker added on {clip}",- markerRename: "Rename",markerColor: "Color",markerDelete: "Delete marker",markerName: "Marker name",markerUntitled: "Marker",- markerOnClip: "On {clip}",- markerNone: "No markers yet. Press M to add one at the playhead.",- markerKeys: "{next} next · {prev} previous",colors: {blue: "Blue",green: "Green",@@ -1476,7 +1476,13 @@ export const en = {format: 'API format',address: 'Address',model: 'Models',- modelPlaceholder: 'As the service names them, separated by commas',+ modelPlaceholder: 'A model\'s name, as the service names it',+ addModel: 'Add',+ removeModel: 'Remove {name}',+ efforts: 'Reasoning effort',+ effortStart: 'Starts on',+ effortHint: 'Tick the efforts each model takes: the chat offers those. Auto sends none, so the service decides.',+ effortLevels: { auto: 'Auto', off: 'Off', minimal: 'Minimal', low: 'Low', medium: 'Medium', high: 'High', xhigh: 'Extra high', max: 'Max' },key: 'API key',keyPlaceholder: 'Paste your API key',keyKept: 'Key ending in {last4} (leave empty to keep it)',@@ -1575,6 +1581,23 @@ export const en = {cancel: 'Cancel',keyEnding: 'Key ending in {last4}',useEnv: 'Use the key in ${env}',+ own: {+ title: "Your own services",+ note: "Any service you run or use, on this computer or online (your own voice, a transcription server, an API not listed here). Your agent reaches it at its address, and its key stays on this computer. Say what it does, so your agent knows when to use it.",+ add: "Add a service",+ edit: "Edit",+ remove: "Remove",+ name: "Name",+ namePlaceholder: "e.g. My voice",+ url: "Address",+ key: "Key",+ keyOptional: "Optional",+ keyKeep: "Leave empty to keep the key ending in {last4}",+ removeKey: "Remove the key",+ auth: "Where the key goes",+ what: "What it does",+ whatPlaceholder: "e.g. Speaks a script in my voice: POST /tts with {\"text\": \"…\"} answers a WAV. Docs: https://…",+ },verbs: {tts: 'Voice-over',voice: 'Voices',@@ -1594,7 +1617,7 @@ export const en = {goConnect: 'Connect a provider',none: 'None',connectNamed: 'Connect {names}',- otherBody: 'Want a service or model that isn’t listed? Your agent can use any other one: tell it which, and give it the service’s API docs and your key. What it makes lands in Assets like everything else.',+ otherBody: "Want a service or model that isn’t listed? Add it under Providers → Your own services, with its address and key: your agent can then use it. What it makes lands in Assets like everything else.",about: {tts: 'Speaks a script, and finds voices to speak it',music: 'Makes a soundtrack from a description',diff --git a/packages/openfilm/studio/ui/src/i18n/es.ts b/packages/openfilm/studio/ui/src/i18n/es.tsindex 811d2d1..f529944 100644--- a/packages/openfilm/studio/ui/src/i18n/es.ts+++ b/packages/openfilm/studio/ui/src/i18n/es.ts@@ -903,6 +903,10 @@ export const es: Words = {scope: 'Aplicar a',scopeOne: 'Esta copia (n.º {i})',scopeAll: 'Las {n} copias',+ scopeThis: "Este texto",+ scopeSimilar: "Todos los iguales",+ scopeSimilarIn: "Todos los iguales · {n}",+ scopeSimilarHint: "Su aspecto (color, tamaño, fuente…) pasa a todos los textos iguales de la película: la misma etiqueta y clases, en todas las páginas. Las palabras y la posición de cada uno se mantienen.",timing: 'Tiempo',mixed: 'Varios',clearOverride: 'Quitar mis cambios de estilo',@@ -1185,6 +1189,7 @@ export const es: Words = {snap: 'Ajuste',snapOn: 'Activado: se ajusta a los bordes de los clips, al cabezal y a los marcadores',snapOff: 'Desactivado: se coloca justo donde lo sueltas',+ mainTrack: 'Pista principal',magnet: 'Imán de la pista principal',magnetOn: 'Activado: los clips de la pista principal quedan seguidos, sin huecos',magnetOff: 'Desactivado: los clips de la pista principal se quedan donde los pones',@@ -1231,18 +1236,13 @@ export const es: Words = {solo: "Solo: escucha solo esta pista, solo aquí (⌥-clic: solo esta)",unsolo: "Dejar de escuchar esta pista en solo",resizeTrack: "Arrastra para cambiar la altura de la pista · doble clic: su altura habitual",- markers: "Marcadores",markerAdd: "Añadir marcador",markerAddClip: "Añadir marcador en el clip",markerAddedOn: "Marcador añadido en {clip}",- markerRename: "Cambiar nombre",markerColor: "Color",markerDelete: "Eliminar marcador",markerName: "Nombre del marcador",markerUntitled: "Marcador",- markerOnClip: "En {clip}",- markerNone: "Aún no hay marcadores. Pulsa M para añadir uno en el cabezal.",- markerKeys: "{next} siguiente · {prev} anterior",colors: {blue: "Azul",green: "Verde",@@ -1475,7 +1475,13 @@ export const es: Words = {format: 'Formato de API',address: 'Dirección',model: 'Modelos',- modelPlaceholder: 'Como los llama el servicio, separados por comas',+ modelPlaceholder: 'El nombre de un modelo, como lo llama el servicio',+ addModel: 'Añadir',+ removeModel: 'Quitar {name}',+ efforts: 'Esfuerzo de razonamiento',+ effortStart: 'Empieza en',+ effortHint: 'Marca los esfuerzos que admite cada modelo: el chat ofrece esos. Automático no envía ninguno, así decide el servicio.',+ effortLevels: { auto: 'Automático', off: 'Sin razonar', minimal: 'Mínimo', low: 'Bajo', medium: 'Medio', high: 'Alto', xhigh: 'Muy alto', max: 'Máximo' },key: 'Clave de API',keyPlaceholder: 'Pega tu clave de API',keyKept: 'Clave terminada en {last4} (déjalo vacío para conservarla)',@@ -1574,6 +1580,23 @@ export const es: Words = {cancel: 'Cancelar',keyEnding: 'Clave terminada en {last4}',useEnv: 'Usar la clave de ${env}',+ own: {+ title: "Tus propios servicios",+ note: "Cualquier servicio que ejecutes o uses, en este ordenador o en línea (tu propia voz, un servidor de transcripción, una API que no está en la lista). Tu agente lo usa en su dirección y su clave se queda en este ordenador. Di qué hace, para que tu agente sepa cuándo usarlo.",+ add: "Añadir un servicio",+ edit: "Editar",+ remove: "Quitar",+ name: "Nombre",+ namePlaceholder: "p. ej. Mi voz",+ url: "Dirección",+ key: "Clave",+ keyOptional: "Opcional",+ keyKeep: "Déjala vacía para conservar la clave que termina en {last4}",+ removeKey: "Quitar la clave",+ auth: "Dónde va la clave",+ what: "Qué hace",+ whatPlaceholder: "p. ej. Lee un guion con mi voz: POST /tts con {\"text\": \"…\"} devuelve un WAV. Documentación: https://…",+ },verbs: {tts: 'Locución',voice: 'Voces',@@ -1593,7 +1616,7 @@ export const es: Words = {goConnect: 'Conectar un proveedor',none: 'Ninguno',connectNamed: 'Conectar {names}',- otherBody: '¿Quieres un servicio o modelo que no aparece en la lista? Tu agente puede usar cualquier otro: dile cuál y dale la documentación de la API del servicio y tu clave. Lo que cree llega a Recursos como todo lo demás.',+ otherBody: "¿Quieres un servicio o modelo que no aparece en la lista? Añádelo en Proveedores → Tus propios servicios, con su dirección y su clave: tu agente podrá usarlo. Lo que cree llega a Recursos como todo lo demás.",about: {tts: 'Lee un guion en voz alta y encuentra voces para leerlo',music: 'Crea una banda sonora a partir de una descripción',diff --git a/packages/openfilm/studio/ui/src/i18n/ja.ts b/packages/openfilm/studio/ui/src/i18n/ja.tsindex 0946dd0..5df29c0 100644--- a/packages/openfilm/studio/ui/src/i18n/ja.ts+++ b/packages/openfilm/studio/ui/src/i18n/ja.ts@@ -903,6 +903,10 @@ export const ja: Words = {scope: '適用先',scopeOne: 'このコピー(#{i})',scopeAll: '{n} 個のコピーすべて',+ scopeThis: "このテキスト",+ scopeSimilar: "同じ種類すべて",+ scopeSimilarIn: "同じ種類すべて · {n}",+ scopeSimilarHint: "見た目(色・サイズ・フォントなど)を、フィルム内の同じ種類のテキスト(タグとクラスが同じもの、すべてのページ)に反映します。文字と位置はそれぞれのままです。",timing: 'タイミング',mixed: '混在',clearOverride: 'スタイルの変更をクリア',@@ -1185,6 +1189,7 @@ export const ja: Words = {snap: 'スナップ',snapOn: 'オン:クリップの端、再生ヘッド、マーカーにスナップします',snapOff: 'オフ:離した位置にそのまま置きます',+ mainTrack: 'メイントラック',magnet: 'メイントラックマグネット',magnetOn: 'オン:メイントラックのクリップは隙間なく並びます',magnetOff: 'オフ:メイントラックのクリップは置いた位置のままです',@@ -1231,18 +1236,13 @@ export const ja: Words = {solo: "ソロ:このトラックだけを聴く(ここでのみ。⌥クリック:このトラックだけ)",unsolo: "このトラックのソロを解除",resizeTrack: "ドラッグでトラックの高さを変更 · ダブルクリック:標準の高さ",- markers: "マーカー",markerAdd: "マーカーを追加",markerAddClip: "クリップにマーカーを追加",markerAddedOn: "{clip} にマーカーを追加しました",- markerRename: "名前を変更",markerColor: "色",markerDelete: "マーカーを削除",markerName: "マーカー名",markerUntitled: "マーカー",- markerOnClip: "{clip} 上",- markerNone: "マーカーはまだありません。M で再生ヘッドの位置に追加します。",- markerKeys: "{next} 次へ · {prev} 前へ",colors: {blue: "青",green: "緑",@@ -1475,7 +1475,13 @@ export const ja: Words = {format: 'API 形式',address: 'アドレス',model: 'モデル',- modelPlaceholder: 'サービスでのモデル名。複数ある場合はカンマで区切る',+ modelPlaceholder: 'サービスでのモデル名',+ addModel: '追加',+ removeModel: '{name} を削除',+ efforts: '推論の強さ',+ effortStart: '最初の強さ',+ effortHint: '各モデルが対応する推論の強さにチェックを入れてください。チャットではそれだけが選べます。「自動」は何も送らず、サービスに任せます。',+ effortLevels: { auto: '自動', off: 'オフ', minimal: '最小', low: '低', medium: '中', high: '高', xhigh: '特高', max: '最大' },key: 'API キー',keyPlaceholder: 'API キーを貼り付け',keyKept: '末尾が {last4} のキー(空欄のままにすると現在のキーを使います)',@@ -1574,6 +1580,23 @@ export const ja: Words = {cancel: 'キャンセル',keyEnding: '末尾が {last4} のキー',useEnv: '環境変数 ${env} のキーを使う',+ own: {+ title: "自分のサービス",+ note: "自分で動かしている、または使っている任意のサービス(このコンピュータ上でもオンラインでも可:自分の声、文字起こしサーバー、ここにない API など)。エージェントはそのアドレスで利用し、キーはこのコンピュータに残ります。何ができるかを書いておくと、エージェントがいつ使うか判断できます。",+ add: "サービスを追加",+ edit: "編集",+ remove: "削除",+ name: "名前",+ namePlaceholder: "例:自分の声",+ url: "アドレス",+ key: "キー",+ keyOptional: "省略可",+ keyKeep: "空欄のままにすると末尾 {last4} のキーを保持します",+ removeKey: "キーを削除",+ auth: "キーの送り先",+ what: "できること",+ whatPlaceholder: "例:原稿を自分の声で読み上げる:POST /tts に {\"text\": \"…\"} を送ると WAV を返す。ドキュメント:https://…",+ },verbs: {tts: 'ナレーション',voice: 'ボイス',@@ -1593,7 +1616,7 @@ export const ja: Words = {goConnect: 'プロバイダーを接続',none: 'なし',connectNamed: '{names} を接続',- otherBody: '一覧にないサービスやモデルを使いたい場合は、エージェントにどれを使うか伝え、そのサービスの API ドキュメントとキーを渡せば、他のどんなものでも使えます。作成されたものは、他と同じく素材に入ります。',+ otherBody: "一覧にないサービスやモデルを使いたい場合は、「プロバイダ → 自分のサービス」にアドレスとキーを追加すると、エージェントが使えるようになります。作成されたものは、他と同じく素材に入ります。",about: {tts: '台本を読み上げ、読み上げる声を探します',music: '説明からサウンドトラックを作成します',diff --git a/packages/openfilm/studio/ui/src/i18n/ko.ts b/packages/openfilm/studio/ui/src/i18n/ko.tsindex 167e8f0..ae4d513 100644--- a/packages/openfilm/studio/ui/src/i18n/ko.ts+++ b/packages/openfilm/studio/ui/src/i18n/ko.ts@@ -903,6 +903,10 @@ export const ko: Words = {scope: '적용 대상',scopeOne: '이 사본(#{i})',scopeAll: '사본 {n}개 모두',+ scopeThis: "이 텍스트",+ scopeSimilar: "같은 종류 모두",+ scopeSimilarIn: "같은 종류 모두 · {n}",+ scopeSimilarHint: "모양(색, 크기, 글꼴 등)을 필름의 같은 종류 텍스트(태그와 클래스가 같은 것, 모든 페이지)에 적용합니다. 글자와 위치는 각자 그대로입니다.",timing: '타이밍',mixed: '혼합',clearOverride: '스타일 변경 지우기',@@ -1185,6 +1189,7 @@ export const ko: Words = {snap: '스냅',snapOn: '켬: 클립 가장자리, 재생 헤드, 마커에 스냅합니다',snapOff: '끔: 손을 놓은 자리에 그대로 놓입니다',+ mainTrack: '메인 트랙',magnet: '메인 트랙 자석',magnetOn: '켬: 메인 트랙의 클립이 빈틈없이 붙어 있습니다',magnetOff: '끔: 메인 트랙의 클립이 놓은 자리에 그대로 있습니다',@@ -1231,18 +1236,13 @@ export const ko: Words = {solo: "솔로: 이 트랙만 듣기, 여기서만 (⌥ 클릭: 이 트랙만)",unsolo: "이 트랙 솔로 해제",resizeTrack: "드래그하여 트랙 높이 조절 · 더블 클릭: 기본 높이",- markers: "마커",markerAdd: "마커 추가",markerAddClip: "클립에 마커 추가",markerAddedOn: "{clip}에 마커를 추가했습니다",- markerRename: "이름 바꾸기",markerColor: "색상",markerDelete: "마커 삭제",markerName: "마커 이름",markerUntitled: "마커",- markerOnClip: "{clip} 위",- markerNone: "아직 마커가 없습니다. M을 눌러 재생 헤드 위치에 추가하세요.",- markerKeys: "{next} 다음 · {prev} 이전",colors: {blue: "파랑",green: "초록",@@ -1474,7 +1474,13 @@ export const ko: Words = {format: 'API 형식',address: '주소',model: '모델',- modelPlaceholder: '서비스에서 쓰는 모델 이름, 여러 개는 쉼표로 구분',+ modelPlaceholder: '서비스에서 쓰는 모델 이름',+ addModel: '추가',+ removeModel: '{name} 삭제',+ efforts: '추론 강도',+ effortStart: '시작 강도',+ effortHint: '모델마다 지원하는 추론 강도를 체크하세요. 채팅에는 체크한 것만 나옵니다. "자동"은 아무것도 보내지 않고 서비스에 맡깁니다.',+ effortLevels: { auto: '자동', off: '끔', minimal: '최소', low: '낮음', medium: '중간', high: '높음', xhigh: '매우 높음', max: '최대' },key: 'API 키',keyPlaceholder: 'API 키 붙여넣기',keyKept: '{last4}(으)로 끝나는 키(유지하려면 비워 두세요)',@@ -1573,6 +1579,23 @@ export const ko: Words = {cancel: '취소',keyEnding: '{last4}(으)로 끝나는 키',useEnv: '환경 변수 ${env}의 키 사용',+ own: {+ title: "내 서비스",+ note: "직접 실행하거나 사용하는 모든 서비스(이 컴퓨터에서든 온라인이든: 내 목소리, 음성 인식 서버, 여기에 없는 API 등). 에이전트는 주소로 접근하며, 키는 이 컴퓨터에만 남습니다. 무엇을 하는지 적어 두면 에이전트가 언제 쓸지 압니다.",+ add: "서비스 추가",+ edit: "편집",+ remove: "삭제",+ name: "이름",+ namePlaceholder: "예: 내 목소리",+ url: "주소",+ key: "키",+ keyOptional: "선택 사항",+ keyKeep: "비워 두면 끝자리가 {last4}인 키를 유지합니다",+ removeKey: "키 삭제",+ auth: "키를 넣을 곳",+ what: "하는 일",+ whatPlaceholder: "예: 대본을 내 목소리로 읽음: POST /tts에 {\"text\": \"…\"}를 보내면 WAV를 반환. 문서: https://…",+ },verbs: {tts: '보이스오버',voice: '음성',@@ -1592,7 +1615,7 @@ export const ko: Words = {goConnect: '제공업체 연결',none: '없음',connectNamed: '{names} 연결',- otherBody: '목록에 없는 서비스나 모델을 원하세요? 에이전트는 다른 어떤 서비스든 사용할 수 있습니다. 어떤 서비스인지 알려 주고, 그 서비스의 API 문서와 내 키를 주세요. 만든 결과물은 다른 것과 마찬가지로 에셋에 들어갑니다.',+ otherBody: "목록에 없는 서비스나 모델을 원하세요? ‘제공업체 → 내 서비스’에 주소와 키를 추가하면 에이전트가 사용할 수 있습니다. 만든 결과물은 다른 것과 마찬가지로 에셋에 들어갑니다.",about: {tts: '대본을 읽고, 읽을 음성을 찾습니다',music: '설명으로 사운드트랙을 만듭니다',diff --git a/packages/openfilm/studio/ui/src/i18n/zh-CN.ts b/packages/openfilm/studio/ui/src/i18n/zh-CN.tsindex 09b2fc0..7c515ce 100644--- a/packages/openfilm/studio/ui/src/i18n/zh-CN.ts+++ b/packages/openfilm/studio/ui/src/i18n/zh-CN.ts@@ -904,6 +904,10 @@ export const zhCN: Words = {scope: '应用于',scopeOne: '此副本(#{i})',scopeAll: '全部 {n} 个副本',+ scopeThis: "此文字",+ scopeSimilar: "所有同类",+ scopeSimilarIn: "所有同类 · {n} 段",+ scopeSimilarHint: "样式(颜色、字号、字体等)会同步到片子里所有同类文字:标签和 class 相同,跨所有页面。文字内容和位置各自保留。",timing: '时间',mixed: '混合',clearOverride: '清除我对样式的修改',@@ -1186,6 +1190,7 @@ export const zhCN: Words = {snap: '吸附',snapOn: '开:拖动时吸附到片段边缘、播放头和标记',snapOff: '关:松手在哪就放在哪',+ mainTrack: '主轨',magnet: '主轨磁吸',magnetOn: '开:主轨上的片段自动首尾相接,不留空隙',magnetOff: '关:主轨上的片段放在哪就在哪',@@ -1232,18 +1237,13 @@ export const zhCN: Words = {solo: "独听:只听这条轨道,仅在此处(⌥ 点击:只独听这一条)",unsolo: "取消独听这条轨道",resizeTrack: "拖动调整轨道高度 · 双击:恢复默认高度",- markers: "标记",markerAdd: "添加标记",markerAddClip: "在片段上添加标记",markerAddedOn: "已在 {clip} 上添加标记",- markerRename: "重命名",markerColor: "颜色",markerDelete: "删除标记",markerName: "标记名称",markerUntitled: "标记",- markerOnClip: "在 {clip} 上",- markerNone: "还没有标记。按 M 在播放头处添加。",- markerKeys: "{next} 下一个 · {prev} 上一个",colors: {blue: "蓝色",green: "绿色",@@ -1476,7 +1476,13 @@ export const zhCN: Words = {format: 'API 格式',address: '地址',model: '模型',- modelPlaceholder: '与服务中的写法一致,多个用逗号分隔',+ modelPlaceholder: '模型名,与服务中的写法一致',+ addModel: '添加',+ removeModel: '移除 {name}',+ efforts: '推理档位',+ effortStart: '起始档位',+ effortHint: '勾选每个模型支持的推理档位,聊天里只会出现这些档位。「自动」不发送推理参数,由服务决定。',+ effortLevels: { auto: '自动', off: '不思考', minimal: '极低', low: '低', medium: '中', high: '高', xhigh: '超高', max: '最高' },key: 'API 密钥',keyPlaceholder: '粘贴你的 API 密钥',keyKept: '尾号为 {last4} 的密钥(留空则保留)',@@ -1575,6 +1581,23 @@ export const zhCN: Words = {cancel: '取消',keyEnding: '尾号为 {last4} 的密钥',useEnv: '使用环境变量 ${env} 里的 key',+ own: {+ title: "自己的服务",+ note: "你自己运行或使用的任何服务,在这台电脑上或在线上都可以(你自己的声音、语音转写服务、这里没列出的 API)。智能体按地址访问它,密钥只留在这台电脑上。写清它能做什么,智能体才知道什么时候用它。",+ add: "添加服务",+ edit: "编辑",+ remove: "移除",+ name: "名称",+ namePlaceholder: "例如:我的声音",+ url: "地址",+ key: "密钥",+ keyOptional: "可选",+ keyKeep: "留空则保留末尾为 {last4} 的密钥",+ removeKey: "移除密钥",+ auth: "密钥放在哪里",+ what: "它能做什么",+ whatPlaceholder: "例如:用我的声音朗读文案:POST /tts,内容 {\"text\": \"…\"},返回 WAV。文档:https://…",+ },verbs: {tts: '配音',voice: '音色',@@ -1594,7 +1617,7 @@ export const zhCN: Words = {goConnect: '连接服务商',none: '无',connectNamed: '连接 {names}',- otherBody: '想用列表里没有的服务或模型?智能体可以使用任何其他服务:告诉它用哪个,并提供该服务的 API 文档和你的密钥。生成的内容会和其他内容一样出现在素材中。',+ otherBody: "想用列表里没有的服务或模型?在“服务商 → 自己的服务”里添加它的地址和密钥,智能体就能使用。生成的内容会和其他内容一样出现在素材中。",about: {tts: '朗读脚本,并寻找合适的音色',music: '根据描述生成配乐',diff --git a/packages/openfilm/studio/ui/src/lib/double-press.test.ts b/packages/openfilm/studio/ui/src/lib/double-press.test.tsnew file mode 100644index 0000000..6f677e1--- /dev/null+++ b/packages/openfilm/studio/ui/src/lib/double-press.test.ts@@ -0,0 +1,26 @@+import { test } from 'node:test';+import assert from 'node:assert/strict';+import { doubleClickAim, keepPress, type Press } from './double-press.ts';++const press = (x: number, at: number): Press => ({ x, y: 0, at });++test('a double-click is aimed by its first press, even where the picture moved before the second', () => {+ const presses = [press(100, 1000), press(140, 1150)];+ assert.deepEqual(doubleClickAim(presses, 1160), press(100, 1000));+});++test('a click elsewhere just before a double-click does not aim it', () => {+ let presses: Press[] = [];+ presses = keepPress(presses, press(10, 1000));+ presses = keepPress(presses, press(300, 1180));+ presses = keepPress(presses, press(300, 1260));+ assert.equal(presses.length, 2);+ assert.deepEqual(doubleClickAim(presses, 1270), press(300, 1180));+});++test('presses too far apart, or long before, are not a double-click', () => {+ assert.equal(doubleClickAim([press(100, 1000), press(100, 1600)], 1610), null);+ assert.equal(doubleClickAim([press(100, 1000), press(100, 1100)], 1700), null);+ assert.equal(doubleClickAim([press(100, 1000)], 1010), null);+ assert.equal(doubleClickAim([], 1010), null);+});diff --git a/packages/openfilm/studio/ui/src/lib/double-press.ts b/packages/openfilm/studio/ui/src/lib/double-press.tsnew file mode 100644index 0000000..f284e3f--- /dev/null+++ b/packages/openfilm/studio/ui/src/lib/double-press.ts@@ -0,0 +1,22 @@+/** A press on the picture: where, in the film's own pixels, and when (an event's timeStamp, ms). */+export interface Press { x: number; y: number; at: number }++/** Two presses this close in time are one double-click (the system's own interval is about this). */+export const DOUBLE_PRESS_MS = 500;++/** The presses kept: the last two, since a double-click is aimed by the first of its pair. */+export function keepPress(presses: readonly Press[], press: Press): Press[] {+ return [...presses.slice(-1), press];+}++/**+ * Where a double-click at `now` was aimed: its first press. The picture can move under the hand between the two (the+ * first click opens the inspector, which shrinks it), so the second press is not where it was meant. The press just+ * before the second one, not the first in a while: a click elsewhere a moment earlier was not aimed at it. Null when+ * the last two presses are not the double-click's (the double-click's own point is then the best there is).+ */+export function doubleClickAim(presses: readonly Press[], now: number): Press | null {+ const [first, second] = presses.length >= 2 ? presses.slice(-2) : [];+ if (!first || !second) return null;+ return second.at - first.at < DOUBLE_PRESS_MS && now - second.at < DOUBLE_PRESS_MS ? first : null;+}diff --git a/packages/openfilm/studio/ui/src/lib/group-drag.test.ts b/packages/openfilm/studio/ui/src/lib/group-drag.test.tsindex 9633b41..bd953fe 100644--- a/packages/openfilm/studio/ui/src/lib/group-drag.test.ts+++ b/packages/openfilm/studio/ui/src/lib/group-drag.test.ts@@ -1,6 +1,6 @@import { test } from 'node:test';import assert from 'node:assert/strict';-import { groupDragged, type GroupDrag } from './stage-gesture.ts';+import { groupDragged, layerInGroup, pullGroup, type GroupDrag, type GroupPull } from './stage-gesture.ts';const t = (x: number, y: number, s = 1, rotate = 0) => ({ x, y, scaleX: s, scaleY: s, rotate });const size = { w: 100, h: 50 };@@ -29,3 +29,31 @@ test('turned, each clip goes round the middle of the frame and turns by as much'const d = drag({ kind: 'rotate', turn: 90, anchor: { x: 0, y: 0 } });near(groupDragged(d, t(50, -25), size), t(-50, 75, 1, 90));});++test('several layers scaled from a corner: each center keeps its place in proportion, each one scaled as much', () => {+ const d = drag({ kind: 'scale', kx: 2, ky: 2, anchor: { x: 100, y: 100 } });+ const g = { t: [5, 5] as [number, number], s: [1, 0.5] as [number, number], r: 30 };+ assert.deepEqual(layerInGroup(d, { x: 100, y: 100 }, g), { dx: 0, dy: 0, s: [2, 1], r: 30 }, 'at the corner that stays: not moved');+ assert.deepEqual(layerInGroup(d, { x: 150, y: 120 }, g), { dx: 50, dy: 20, s: [2, 1], r: 30 });+});++test('several layers turned: each center goes round the box\'s middle, each one turned as much', () => {+ const m = layerInGroup(drag({ kind: 'rotate', turn: 90, anchor: { x: 0, y: 0 } }), { x: 10, y: 0 }, { t: [0, 0], s: [1, 1], r: 10 });+ assert.ok(Math.abs(m.dx - -10) < 1e-9 && Math.abs(m.dy - 10) < 1e-9, `${m.dx}, ${m.dy}`);+ assert.equal(m.r, 100);+});++test('a selection\'s box pulled: a corner scales both ways alike, an edge one way, the ring turns (Shift: 15° steps)', () => {+ const pull = (p: Partial<GroupPull>): GroupPull => ({ ...drag({}), handle: null, from: { x: 0, y: 0 }, a0: 0, ...p });+ const corner = pull({ kind: 'scale', handle: 'se', anchor: { x: 0, y: 0 }, from: { x: 100, y: 50 } });+ pullGroup(corner, { x: 200, y: 100 }, false);+ assert.deepEqual([corner.kx, corner.ky], [2, 2]);+ const edge = pull({ kind: 'scale', handle: 'e', anchor: { x: 0, y: 25 }, from: { x: 100, y: 25 } });+ pullGroup(edge, { x: 150, y: 80 }, false);+ assert.deepEqual([edge.kx, edge.ky], [1.5, 1]);+ const ring = pull({ kind: 'rotate', anchor: { x: 0, y: 0 } });+ pullGroup(ring, { x: 0, y: 10 }, false);+ assert.ok(Math.abs(ring.turn - 90) < 1e-9);+ pullGroup(ring, { x: 10, y: 4 }, true);+ assert.equal(ring.turn, 15);+});diff --git a/packages/openfilm/studio/ui/src/lib/host.ts b/packages/openfilm/studio/ui/src/lib/host.tsindex 83de7b3..74c79cd 100644--- a/packages/openfilm/studio/ui/src/lib/host.ts+++ b/packages/openfilm/studio/ui/src/lib/host.ts@@ -79,7 +79,7 @@ export interface StudioHost {locate?: (id: HostAgentId, words?: { title?: string; button?: string }) => Promise<{ ok: boolean; error?: string; code?: 'not-a-program' | 'did-not-start' | string }>;useAutomatic?: (id: HostAgentId) => Promise<unknown>;diagnose?: (id: HostAgentId) => Promise<HostAgentDiagnosis | null>;- configureByok: (config: { format: HostByokFormat; baseUrl?: string; models: string[]; key?: string }) => Promise<{ ok: boolean; error?: string; code?: string }>;+ configureByok: (config: { format: HostByokFormat; baseUrl?: string; models: string[]; efforts?: Record<string, HostByokEffort>; key?: string }) => Promise<{ ok: boolean; error?: string; code?: string }>;onChange: (listener: (agents: HostAgent[]) => void) => () => void;};/**@@ -176,9 +176,17 @@ export interface HostAgent {chosen?: string | null;error?: string;account?: HostAgentAccount;- byok?: { format: HostByokFormat; baseUrl: string; model: string; models?: string[]; last4: string };+ byok?: { format: HostByokFormat; baseUrl: string; model: string; models?: string[]; efforts?: Record<string, HostByokEffort>; last4: string };}+/**+ * The reasoning efforts a model of the person's own key offers, in this order (`auto`: none is sent, the service decides),+ * and the one a conversation starts on. A model given none offers `auto` alone.+ */+export const BYOK_EFFORTS = ['auto', 'off', 'minimal', 'low', 'medium', 'high', 'xhigh', 'max'] as const;+export type HostByokEffortLevel = (typeof BYOK_EFFORTS)[number];+export interface HostByokEffort { levels: HostByokEffortLevel[]; effort: HostByokEffortLevel }+/*** What is left of an agent's plan: each window's share used (0–100), how long it is (minutes) and when it resets (ms);* `kind`: five_hour · seven_day · window (another length).diff --git a/packages/openfilm/studio/ui/src/lib/providers.ts b/packages/openfilm/studio/ui/src/lib/providers.tsindex 6f6d90c..9462c91 100644--- a/packages/openfilm/studio/ui/src/lib/providers.ts+++ b/packages/openfilm/studio/ui/src/lib/providers.ts@@ -26,8 +26,26 @@ export interface OwnProvider {last4: string | null;}+/**+ * One of the person's own services (studio/server/custom-services.mjs): any service at an address, reached by their+ * agent with `openfilm get call`. `auth`: the header its key goes in, `{key}` standing for it.+ */+export interface OwnService {+ id: string;+ name: string;+ url: string;+ auth: string;+ note: string;+ last4: string | null;+}++/** What Settings sends for a service: no `key` keeps the one it has, '' takes it away. */+export type OwnServiceFields = { name: string; url: string; auth: string; note: string; key?: string };+export interface ProvidersState {providers: OwnProvider[];+ /** the person's own services */+ custom: OwnService[];/** the person's choice for each kind of media: a provider id, or null (none) */use: Record<string, string | null>;/** the model each kind of media is made with */@@ -70,6 +88,9 @@ export interface ProvidersHook {useEnvKey: (id: string) => Promise<boolean>;/** who makes that kind of media: a provider id with its model, or '' (none) */choose: (verb: ProviderVerb, provider: string, pick?: { model?: string }) => Promise<void>;+ /** one of the person's own services added (id null) or changed; resolves whether it was kept */+ saveService: (id: string | null, fields: OwnServiceFields) => Promise<boolean>;+ removeService: (id: string) => Promise<void>;}/** The providers while Settings → Providers or Models is shown. */@@ -111,5 +132,7 @@ export function useProviders(): ProvidersHook {removeKey: async (id) => { await change(id, `/${encodeURIComponent(id)}/key`, { method: 'DELETE' }); },useEnvKey: (id) => change(id, `/${encodeURIComponent(id)}/key`, { method: 'PUT', json: { env: true } }),choose: async (verb, provider, pick) => { await change(verb, '/use', { method: 'PUT', json: { verb, provider: provider || null, ...pick } }); },+ saveService: (id, fields) => change(id ?? 'custom', id ? `/custom/${encodeURIComponent(id)}` : '/custom', { method: id ? 'PUT' : 'POST', json: fields }),+ removeService: async (id) => { await change(id, `/custom/${encodeURIComponent(id)}`, { method: 'DELETE' }); },};}diff --git a/packages/openfilm/studio/ui/src/lib/similar-layers.test.ts b/packages/openfilm/studio/ui/src/lib/similar-layers.test.tsnew file mode 100644index 0000000..6055a8e--- /dev/null+++ b/packages/openfilm/studio/ui/src/lib/similar-layers.test.ts@@ -0,0 +1,34 @@+import { test } from 'node:test';+import assert from 'node:assert/strict';+import type { FilmDoc } from './film.ts';+import { pageMayHave, similarClasses, similarEdits } from './similar-layers.ts';++const doc = (tracks: unknown): FilmDoc => ({ stage: { w: 1920, h: 1080 }, tracks } as unknown as FilmDoc);++test('the classes of a selector, each a whole name in a page\'s source', () => {+ assert.deepEqual(similarClasses('h2.title.big'), ['title', 'big']);+ assert.deepEqual(similarClasses('p.a\\:b'), ['a:b']);+ assert.equal(pageMayHave('<h2 class="title big">Shanghai</h2>', 'h2.title.big'), true);+ assert.equal(pageMayHave('<h2 class="subtitle big">', 'h2.title.big'), false, 'subtitle is not title');+ assert.equal(pageMayHave("el.className = 'title'; el.classList.add('big')", 'h2.title.big'), true, 'classes made in code are named there');+ assert.equal(pageMayHave('<h2>', 'h2'), false, 'no class, nothing to go by');+ assert.equal(pageMayHave('<style>.title { color: red }</style><p class="note">My title</p>', 'h2.title'), false, 'a rule or a word is not a layer');+});++test('the style goes to every page clip that may have layers like it, the picked one\'s always, the rest untouched', () => {+ const film = doc([+ { clips: [+ { src: 'hook.html', overrides: [{ at: 'body > h2.title', t: [10, 0] }] },+ { src: 'weights.html' },+ { src: 'assets/clip.mp4' },+ ] },+ { clips: [{ src: 'ending.html', overrides: [{ at: 'h2.title', style: { color: '#fff' } }] }] },+ ]);+ const pages: Record<string, string> = { 'hook.html': '<h2 class="title">', 'weights.html': '<p>no titles</p>', 'ending.html': '<h2 class="title">' };+ const edits = similarEdits(film, 'h2.title', { color: '#ffd447', fontSize: 64 }, (src) => pageMayHave(pages[src] ?? '', 'h2.title'), 'film.html#1.0');+ assert.deepEqual(edits.map((e) => e.loc), ['film.html#0.0', 'film.html#1.0']);+ /* its own tweak stays; the look for all like it goes after it */+ assert.deepEqual(edits[0]!.value, [{ at: 'body > h2.title', t: [10, 0] }, { at: 'h2.title', style: { color: '#ffd447', fontSize: 64 } }]);+ /* one already for all like it: changed, not doubled */+ assert.deepEqual(edits[1]!.value, [{ at: 'h2.title', style: { color: '#ffd447', fontSize: 64 } }]);+});diff --git a/packages/openfilm/studio/ui/src/lib/similar-layers.ts b/packages/openfilm/studio/ui/src/lib/similar-layers.tsnew file mode 100644index 0000000..b348a71--- /dev/null+++ b/packages/openfilm/studio/ui/src/lib/similar-layers.ts@@ -0,0 +1,46 @@+/**+ * A look given to every layer like one (the inspector's "All like it"): the titles of a film are usually several+ * layers of the same kind (`h2.title`) in several pages, and restyling them one by one is tedious.+ *+ * A layer like it is one with its tag and classes (the stage's `similar`, stage.js). The style goes into each page+ * clip's `overrides` with that selector and no `n`, so it reaches every match in the page, as a style for all copies+ * does. Only clips whose page's source names every one of those classes get it: a page that never has them would show+ * the change as lost. Pages that make their classes in code name them in that code, so they are found the same way.+ */+import { nextOverrides, type OverrideGeometry } from './film-overrides.ts';+import { filmDocLoc, filmSrcIsPage, type FilmDoc } from './film.ts';++/** The classes a `similar` selector names: `h2.title.big` → title, big. */+export function similarClasses(selector: string): string[] {+ return [...selector.matchAll(/\.((?:\\.|[\w-])+)/g)].map((m) => m[1]!.replace(/\\(.)/g, '$1'));+}++/**+ * Whether a page's source puts every class of the selector on something: in a `class` attribute or a string its code+ * uses (`classList.add('title')`), never a stylesheet's rule (a page styling `.title` may have none) or its words.+ */+export function pageMayHave(source: string, selector: string): boolean {+ const classes = similarClasses(selector);+ const code = source.replace(/<style[\s\S]*?<\/style>/gi, '');+ const quoted = [...code.matchAll(/(["'`])((?:\\.|(?!\1)[^\\])*?)\1/g)].map((m) => m[2]).join(' ');+ return classes.length > 0 && classes.every((c) => new RegExp(`(^|[^\\w-])${c.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}($|[^\\w-])`).test(quoted));+}++/** The page clips of the film (its place in film.html, its page) in film order. */+export function pageClips(doc: FilmDoc): { loc: string; src: string; overrides: FilmDoc['tracks'][number]['clips'][number]['overrides'] }[] {+ return doc.tracks.flatMap((track, ti) => track.clips.flatMap((clip, ci) => (+ filmSrcIsPage(clip.src) ? [{ loc: filmDocLoc(ti, ci), src: clip.src.replace(/#.*$/, ''), overrides: clip.overrides }] : []+ )));+}++/**+ * The `overrides` each clip gets for `style` on the layers like `selector`: the clips `holds` says may have them, and+ * always `own` (the clip of the layer picked).+ */+export function similarEdits(+ doc: FilmDoc, selector: string, style: Record<string, string | number | null>, holds: (src: string) => boolean, own: string,+): { loc: string; value: ReturnType<typeof nextOverrides> }[] {+ return pageClips(doc)+ .filter((c) => c.loc === own || holds(c.src))+ .map((c) => ({ loc: c.loc, value: nextOverrides(c.overrides, { at: selector }, { style } as OverrideGeometry) }));+}diff --git a/packages/openfilm/studio/ui/src/lib/stage-gesture.ts b/packages/openfilm/studio/ui/src/lib/stage-gesture.tsindex 0c08f47..963a77c 100644--- a/packages/openfilm/studio/ui/src/lib/stage-gesture.ts+++ b/packages/openfilm/studio/ui/src/lib/stage-gesture.ts@@ -213,6 +213,49 @@ export interface GroupDrag {anchor: { x: number; y: number };}+/** A selection's box pulled by one of its handles: where the pull started (`from`, the handle; `a0`, the angle to it). */+export interface GroupPull extends GroupDrag {+ handle: string | null;+ from: { x: number; y: number };+ a0: number;+}++/**+ * The box of several things pulled by a handle to `at` (stage px): a corner scales both ways alike (along the box's+ * diagonal), an edge one way, the ring turns it about its middle (Shift: in steps of 15°, as one thing turns).+ */+export function pullGroup(d: GroupPull, at: { x: number; y: number }, shift: boolean): void {+ if (d.kind === 'rotate') {+ const turn = ((Math.atan2(at.y - d.anchor.y, at.x - d.anchor.x) - d.a0) * 180) / Math.PI;+ d.turn = shift ? Math.round(turn / 15) * 15 : turn;+ return;+ }+ if (d.kind !== 'scale') return;+ const ux = d.from.x - d.anchor.x, uy = d.from.y - d.anchor.y;+ const along = (v: number, u: number, a: number) => Math.max(0.02, (v - a) / (Math.abs(u) > 1e-6 ? u : 1e-6));+ if (d.handle && d.handle.length === 2) {+ const k = Math.max(0.02, ((at.x - d.anchor.x) * ux + (at.y - d.anchor.y) * uy) / Math.max(1e-6, ux * ux + uy * uy));+ d.kx = d.ky = k;+ } else if (d.handle === 'e' || d.handle === 'w') d.kx = along(at.x, ux, d.anchor.x);+ else d.ky = along(at.y, uy, d.anchor.y);+}++/**+ * One of several layers scaled or turned together by their box's handles (as Figma does a selection): its center+ * (`center`, stage px) goes with the box, scaled or turned about the same point, and it takes the same scale or turn+ * itself. How far its center goes (stage px), and its own scale and angle after.+ */+export function layerInGroup(d: GroupDrag, center: { x: number; y: number }, g: LayerGeometry): { dx: number; dy: number; s: [number, number]; r: number } {+ const cx = center.x - d.anchor.x, cy = center.y - d.anchor.y;+ const round = (n: number) => Math.round(n * 10000) / 10000;+ if (d.kind === 'scale') return { dx: cx * (d.kx - 1), dy: cy * (d.ky - 1), s: [round(g.s[0] * d.kx), round(g.s[1] * d.ky)], r: g.r };+ if (d.kind === 'rotate') {+ const a = (d.turn * Math.PI) / 180;+ return { dx: cx * Math.cos(a) - cy * Math.sin(a) - cx, dy: cx * Math.sin(a) + cy * Math.cos(a) - cy, s: g.s, r: Math.round((g.r + d.turn) * 10) / 10 };+ }+ return { dx: d.dx, dy: d.dy, s: g.s, r: g.r };+}+/*** Where one of several clips dragged together is now. `t0` is where it was (its unturned top left, stage px, and its* scale), `size` its picture's own size: its center moves with the others, scaled or turned about the same point, sodiff --git a/packages/openfilm/studio/ui/src/lib/stage-types.ts b/packages/openfilm/studio/ui/src/lib/stage-types.tsindex b76d5c5..19981c9 100644--- a/packages/openfilm/studio/ui/src/lib/stage-types.ts+++ b/packages/openfilm/studio/ui/src/lib/stage-types.ts@@ -75,6 +75,8 @@ export type StageElement = {/** This layer's override now. */override?: NodeOverride | null;tag?: string;+ /** The selector of layers like it in any page (its tag and classes: `h2.title`); none when it has no class. */+ similar?: string;/** The layer's computed style; `declaredStyle` lists the keys its own code sets. */style?: StageElementStyle;declaredStyle?: readonly string[];diff --git a/packages/openfilm/studio/ui/src/lib/thumb-frame.test.ts b/packages/openfilm/studio/ui/src/lib/thumb-frame.test.tsnew file mode 100644index 0000000..3f77105--- /dev/null+++ b/packages/openfilm/studio/ui/src/lib/thumb-frame.test.ts@@ -0,0 +1,14 @@+import { test } from 'node:test';+import assert from 'node:assert/strict';+import { overridesToken } from './thumb-frame.ts';++test('a page clip\'s overrides give its thumbnails a token: none without a layer\'s, another for another change', () => {+ assert.equal(overridesToken(undefined), '');+ assert.equal(overridesToken([]), '');+ assert.equal(overridesToken([{ fade: [0.5, 0.5] }]), '', 'its fades alone change no picture');+ const moved = overridesToken([{ at: '#title', t: [10, 0] }]);+ assert.ok(moved);+ assert.equal(overridesToken([{ at: '#title', t: [10, 0] }]), moved, 'the same changes, the same token');+ assert.notEqual(overridesToken([{ at: '#title', t: [20, 0] }]), moved);+ assert.notEqual(overridesToken([{ at: '#title', t: [10, 0], style: { fontSize: '90px' } }]), moved);+});diff --git a/packages/openfilm/studio/ui/src/lib/thumb-frame.ts b/packages/openfilm/studio/ui/src/lib/thumb-frame.tsindex 237b5e1..e034c98 100644--- a/packages/openfilm/studio/ui/src/lib/thumb-frame.ts+++ b/packages/openfilm/studio/ui/src/lib/thumb-frame.ts@@ -61,3 +61,16 @@ export function indexClipThumbs(}return by;}++/**+ * A page clip's thumbnails are drawn with its overrides (SPEC §1), the changes made inside it in Studio: as a short+ * token for a frame's address and key, '' when it has none of a layer's (its fades alone change no picture). Another+ * change, another token: the timeline asks for the clip's pictures again.+ */+export function overridesToken(overrides: readonly unknown[] | undefined): string {+ if (!overrides?.some((o) => typeof o === 'object' && o !== null && 'at' in o)) return '';+ const text = JSON.stringify(overrides);+ let h = 0x811c9dc5;+ for (let i = 0; i < text.length; i++) { h ^= text.charCodeAt(i); h = Math.imul(h, 0x01000193); }+ return (h >>> 0).toString(36);+}diff --git a/packages/openfilm/studio/ui/src/lib/timeline-clipboard.test.ts b/packages/openfilm/studio/ui/src/lib/timeline-clipboard.test.tsindex 248bb45..fa95704 100644--- a/packages/openfilm/studio/ui/src/lib/timeline-clipboard.test.ts+++ b/packages/openfilm/studio/ui/src/lib/timeline-clipboard.test.ts@@ -196,6 +196,16 @@ test('⌥-drag copies, as CapCut: the clip stays and a copy lands where it is leassert.deepEqual(run(planCopies(model, { ids: ['t1'], deltaMs: 1000, pointerMs: 1500, magnet: false, sync: false }))[0], [['t1', 0, null], ['t1-2', 1, null]]);});+test('⌥-drag copies of clips with the magnet go in at the insert point by the first of them, whichever is in hand', () => {+ const film: Film = { tracks: [{ clips: [filmClip('a', 0, 2), filmClip('b', 2, 2), filmClip('c', 5, 1)] }] };+ const tracks = [track(0, [blockOf('a', 0, 0, 0, 2000), blockOf('b', 0, 1, 2000, 4000), blockOf('c', 0, 2, 5000, 6000)], { role: 'video' })];+ const model = editModelOf(tracks);+ const run = (plan: { ops: unknown[] } | null) => spans(applyOps(structuredClone(film) as never, plan!.ops as never) as unknown as Film);+ /* b and c in hand by c, let go past the end: the copies go on after c, b's first, the gap between them kept */+ assert.deepEqual(run(planCopies(model, { ids: ['c', 'b'], deltaMs: 1000, pointerMs: 6500, magnet: true, sync: false, linkage: true }))[0],+ [['a', 0, null], ['b', 2, null], ['c', 5, null], ['b-2', 6, null], ['c-2', 9, null]]);+});+test('copies of linked clips pasted together are linked to each other, not to the originals', () => {const tracks = [track(0, [blockOf('v', 0, 0, 0, 2000)]), track(1, [blockOf('s', 1, 0, 0, 2000), blockOf('t', 1, 1, 3000, 4000)])];const model = editModelOf(tracks, new Map([['v', 'v'], ['s', 'v'], ['t', 'x']]));diff --git a/packages/openfilm/studio/ui/src/lib/timeline-clipboard.ts b/packages/openfilm/studio/ui/src/lib/timeline-clipboard.tsindex 5b0df9a..5007582 100644--- a/packages/openfilm/studio/ui/src/lib/timeline-clipboard.ts+++ b/packages/openfilm/studio/ui/src/lib/timeline-clipboard.ts@@ -323,10 +323,12 @@ export function planCopies(const main = mainTrackOf(model);const lead = picked[0]!;const offset = Math.max(spec.deltaMs, -Math.min(...picked.map((x) => x.c.startMs)));- /* the group keeps its shape; on the main track it goes in at the insert point under the hand */+ /* the group keeps its shape; on the main track its first clip goes in at the insert point under the hand, whichever+ is in hand */const leadDest = spec.to?.get(lead.c.id) ?? lead.track;+ const firstMs = Math.min(...picked.map((x) => x.c.startMs));let shift = offset;- if (spec.magnet && leadDest === main) shift = insertPointOn(model, main, spec.pointerMs, lead.c.startMs + offset) - lead.c.startMs;+ if (spec.magnet && leadDest === main) shift = insertPointOn(model, main, spec.pointerMs, firstMs + offset) - firstMs;const spans = picked.map(({ c, track }) => {const startMs = Math.max(0, c.startMs + shift);return { id: c.id, startMs, endMs: startMs + c.endMs - c.startMs, dest: spec.to?.get(c.id) ?? (track as TrackDest) };diff --git a/packages/openfilm/studio/ui/src/lib/timeline-edit.test.ts b/packages/openfilm/studio/ui/src/lib/timeline-edit.test.tsindex 50fb95d..a8955ae 100644--- a/packages/openfilm/studio/ui/src/lib/timeline-edit.test.ts+++ b/packages/openfilm/studio/ui/src/lib/timeline-edit.test.ts@@ -296,6 +296,16 @@ test('with the magnet a clip taken off the main track closes it up, wherever itassert.deepEqual(after(t, moved), [[['t2', 2, 3], ['t1', 3, 4]], [['a', 0, 4]]]);});+test('with the magnet clips moved together go in at the insert point by the first of them, whichever is in hand', () => {+ /* b and c (a gap between them) in hand by c, dragged on past the end: back after a, the gap kept, a not cut */+ const m = film([], [['a', 0, 10], ['b', 10, 20], ['c', 25, 30]]);+ const plan = planMove(m, { ids: ['c', 'b'], deltaMs: 5000, pointerMs: 32_000, mode: 'insert', magnet: true, ...LINK })!;+ assert.deepEqual(after(m, plan).flat(), [['a', 0, 10], ['b', 10, 20], ['c', 25, 30]]);+ /* in hand by c, let go on a's first half: they go in before a */+ const front = planMove(m, { ids: ['c', 'b'], deltaMs: -24_000, pointerMs: 2000, mode: 'insert', magnet: true, ...LINK })!;+ assert.deepEqual(after(m, front).flat(), [['b', 0, 10], ['c', 15, 20], ['a', 20, 30]]);+});+test('with the magnet a clip let go on another off the main track covers nothing: it goes to a free track', () => {/* two title tracks; t3 dragged onto t1's time on the lower one goes up to the free one */const m = model({ role: 'mg', clips: [['t2', 4, 5]] }, { role: 'mg', clips: [['t1', 0, 2], ['t3', 3, 4]] }, { role: 'video', clips: [['a', 0, 6]] });diff --git a/packages/openfilm/studio/ui/src/lib/timeline-edit.ts b/packages/openfilm/studio/ui/src/lib/timeline-edit.tsindex 000017f..7f72a4b 100644--- a/packages/openfilm/studio/ui/src/lib/timeline-edit.ts+++ b/packages/openfilm/studio/ui/src/lib/timeline-edit.ts@@ -902,9 +902,10 @@ export function planMove(model: EditModel, spec: MoveSpec): (EditPlan & { landMs: spec.magnet && mainIndex != null && spans.has(mainIndex) ? new Map([[mainIndex, spans.get(mainIndex)!]]) : null;const shifts = closing ? closeSpans(d, closing, spec) : [];if (spec.mode === 'insert' && typeof mainDest === 'number') {- /* the insert point, worked out on the track as it was (where the hand is), then carried through the lift */- const point = insertPointOn(model, mainDest, spec.pointerMs, landMs, hand);- landMs = Math.max(-minOffset, mapThrough(point, mainDest, shifts));+ /* the insert point, worked out on the track as it was (where the hand is), then carried through the lift; the+ first of them goes in there, whichever is in hand (the group keeps its shape after it) */+ const point = insertPointOn(model, mainDest, spec.pointerMs, landMs + minOffset, hand);+ landMs = Math.max(-minOffset, mapThrough(point, mainDest, shifts) - minOffset);const starts = [...lifted.values()].map((c) => landMs + offset(c));const at = Math.min(...starts);const durMs = Math.max(...[...lifted.values()].map((c) => landMs + offset(c) + lenOf(c))) - at;diff --git a/packages/openfilm/studio/ui/src/lib/timeline-layout.test.ts b/packages/openfilm/studio/ui/src/lib/timeline-layout.test.tsindex 14fbe62..4ab765e 100644--- a/packages/openfilm/studio/ui/src/lib/timeline-layout.test.ts+++ b/packages/openfilm/studio/ui/src/lib/timeline-layout.test.ts@@ -116,3 +116,11 @@ test('every rate a project can have puts a labeled tick on each whole second, at}}});++test('the main track is marked: the lowest picture track, as the magnet takes it', () => {+ const two = { stage: doc.stage, tracks: [{ clips: [{ src: 'assets/footage/product.mp4', id: 'top', time: [0, 2] }] }, doc.tracks[0]!] };+ const top = { key: 'top#0.0', src: 'assets/footage/product.mp4', startMs: 0, durMs: 2000, inMs: 0, sourceDurMs: 10_000, loc: 'film.html#0.0', track: { index: 0, name: 'video', kind: 'video' }, clipId: 'top' };+ const low = (id: string, clip: number, startMs: number, durMs: number, inMs: number) => ({ ...video(id, clip, startMs, durMs, inMs), loc: `film.html#1.${clip}`, track: { index: 1, name: 'video', kind: 'video' } });+ const rows = layoutTracks({ scenes: [], sounds: [], captions: [], doc: two, videos: [top, low('open', 0, 0, 2000, 2000), low('open-b', 1, 2000, 2000, 4000), low('slow', 2, 4000, 6000, 6000)] });+ assert.deepEqual(rows.map((r) => [r.docIndex, Boolean(r.main)]), [[0, false], [1, true]]);+});diff --git a/packages/openfilm/studio/ui/src/lib/timeline-layout.ts b/packages/openfilm/studio/ui/src/lib/timeline-layout.tsindex f9a8af9..3d45fa6 100644--- a/packages/openfilm/studio/ui/src/lib/timeline-layout.ts+++ b/packages/openfilm/studio/ui/src/lib/timeline-layout.ts@@ -438,6 +438,11 @@ export interface TimelineTrack {incomplete?: boolean;/** How tall the row is drawn, px, when the person set it (lib/track-heights); else the timeline's own for its kind. */height?: number;+ /**+ * The main track's rows: the lowest picture track, where the magnet acts (lib/timeline-edit mainTrackOf), marked and+ * drawn taller as CapCut draws its main track.+ */+ main?: boolean;blocks: TimelineBlock[];}@@ -1033,8 +1038,11 @@ export function layoutTracks(evaluated: TimelineFilm): TimelineTrack[] {);}- /* badges last, per drawn row top to bottom (see badgeRows) */- return badgeRows(tracks);+ /* badges last, per drawn row top to bottom (see badgeRows); the main track marked (the lowest picture track) */+ const badged = badgeRows(tracks);+ const pictures = badged.filter((r) => r.role !== 'audio' && r.docIndex != null).map((r) => r.docIndex!);+ const main = pictures.length ? Math.max(...pictures) : null;+ return main == null ? badged : badged.map((r) => (r.docIndex === main ? { ...r, main: true } : r));}/**diff --git a/packages/openfilm/studio/ui/src/lib/track-heights.ts b/packages/openfilm/studio/ui/src/lib/track-heights.tsindex 944c99c..3e1878a 100644--- a/packages/openfilm/studio/ui/src/lib/track-heights.ts+++ b/packages/openfilm/studio/ui/src/lib/track-heights.ts@@ -20,6 +20,14 @@ export const TRACK_SIZES = {} as const;export type TrackSize = keyof typeof TRACK_SIZES;+/** The main track's own height: taller than the others, as CapCut draws its main track. */+export const MAIN_TRACK_H = 68;++/** A track's own height (none set by the person): the main track's, else medium for its kind. */+export function ownTrackHeight(track: { kind: TimelineLaneKind; main?: boolean }): number {+ return track.main ? MAIN_TRACK_H : TRACK_SIZES.medium[track.kind];+}+export function clampTrackHeight(h: number): number {return Math.round(Math.max(TRACK_MIN_H, Math.min(TRACK_MAX_H, h)));}