mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-09-03 04:38:33 +00:00
refactor(studio): contexts, PropertyPanel split, duration fix, perf (#748)
* feat(studio): add manual DOM editing inspector (#466) * fix: stabilize studio preview and runtime sync * fix: pass selector through timeline thumbnails * feat: add studio timeline editing * fix: disambiguate timeline edit targets * fix: stop timeline auto-scroll in fit mode * feat: use percentage-based timeline zoom * fix: sync timeline playhead on zoom changes * fix: reset timeline scroll when returning to fit * feat(studio): add manual DOM editing inspector * docs: update studio manual dom editing guide * feat(studio): add image asset picker for fills * feat(studio): add inline image uploads for fills * fix(studio): use real file input for image fill uploads * fix(studio): restore toast plumbing after rebase * fix(studio): explain in-app upload limitation * fix(studio): reuse asset-tab upload pattern in fills * feat(studio): refine manual design inspector * fix(studio): polish manual design inspector * fix(studio): keep color picker in viewport * fix(studio): clarify color picker selection * docs: update manual DOM editing guide * fix(studio): keep gradient color picker open * fix(studio): scope text color to text layers * fix(studio): add agent fallback for immovable layers * fix(studio): address manual editing review feedback * fix(studio): make local font selection reliable * fix(studio): improve dom picking and thumbnails * fix(studio): copy absolute paths in agent prompts * fix(studio): prevent timeline track cutoff * fix: copy Studio agent prompts in Safari * fix(studio): hold canvas movement from inspector * feat(studio): add persistent undo redo (#537) Studio manual editing and timeline editing mutate project files directly, but those edits had no reliable undo/redo path. Before releasing manual editing, users need a way to recover from visual property changes, source-editor saves, timeline moves/resizes/deletes, and timeline asset drops. The history also needs to survive a page refresh. A refresh should not erase the only way back from a bad manual edit. - Adds a persistent per-project edit-history model for file snapshots. - Stores undo/redo stacks in IndexedDB so history survives Studio refreshes. - Records source editor saves, manual DOM edits, and timeline mutations. - Adds toolbar undo/redo buttons with standard keyboard shortcuts: `Cmd/Ctrl+Z`, `Cmd/Ctrl+Shift+Z`, and `Ctrl+Y`. - Validates current file hashes before applying undo/redo so external file changes do not silently overwrite newer content. - Keeps history available in memory if IndexedDB persistence fails during a session. - Adds focused unit coverage for the pure history model, storage adapter, controller/hook behavior, and project-file save helper. Studio previously treated every editor mutation as an immediate file write. Manual DOM editing, timeline updates, and source-editor saves each had separate write paths, so there was no common transaction boundary where Studio could capture the file contents before and after an edit. Undo/redo needed to sit above those write paths as a file-level transaction system: capture changed files before saving, write the new contents, persist the history entry by project, then apply undo/redo only when the current file content still matches the expected snapshot. - `bun --filter @hyperframes/studio test src/utils/editHistory.test.ts src/utils/editHistoryStorage.test.ts src/hooks/usePersistentEditHistory.test.ts src/utils/studioFileHistory.test.ts` -> 4 files pass, 15 tests pass - `bun --filter @hyperframes/studio test` -> 26 files pass, 289 tests pass - `bun --filter @hyperframes/studio typecheck` - `bunx oxlint packages/studio/src/App.tsx packages/studio/src/icons/SystemIcons.tsx packages/studio/src/hooks/usePersistentEditHistory.ts packages/studio/src/hooks/usePersistentEditHistory.test.ts packages/studio/src/utils/editHistory.ts packages/studio/src/utils/editHistory.test.ts packages/studio/src/utils/editHistoryStorage.ts packages/studio/src/utils/editHistoryStorage.test.ts packages/studio/src/utils/studioFileHistory.ts packages/studio/src/utils/studioFileHistory.test.ts` -> 0 warnings, 0 errors - `bunx oxfmt --check packages/studio/src/App.tsx packages/studio/src/icons/SystemIcons.tsx packages/studio/src/hooks/usePersistentEditHistory.ts packages/studio/src/hooks/usePersistentEditHistory.test.ts packages/studio/src/utils/editHistory.ts packages/studio/src/utils/editHistory.test.ts packages/studio/src/utils/editHistoryStorage.ts packages/studio/src/utils/editHistoryStorage.test.ts packages/studio/src/utils/studioFileHistory.ts packages/studio/src/utils/studioFileHistory.test.ts` - `git diff --check` - `bun run --filter @hyperframes/core build:hyperframes-runtime` before commit hook, because the clean worktree needed the ignored runtime-inline artifact for typecheck - Lefthook pre-commit -> lint, format, typecheck pass - Lefthook commit-msg -> commitlint pass - Started Studio locally at `http://127.0.0.1:5190/#project/undo-redo-sample`. - Used `agent-browser` to select a preview element in the Inspector and change `#hero-card` from `left: 220px` to `left: 260px`. - Refreshed Studio and verified Undo stayed enabled. - Clicked Undo and verified the project file returned to `left: 220px`; clicked Redo and verified the inline `left: 260px` returned. - Used `agent-browser` to drag the `side-card` timeline clip, refreshed Studio, then verified Undo restored the previous timeline attributes and Redo reapplied the timeline move. - Recorded the tested undo/redo flow with `agent-browser`: `qa-artifacts/studio-undo-redo-2026-04-28/studio-undo-redo-flow.webm`. - Local screenshots and recordings are kept under `qa-artifacts/studio-undo-redo-2026-04-28/` and are intentionally not committed. - The scratch Studio project used for browser proof is local-only under `packages/studio/data/projects/undo-redo-sample/` and is intentionally not committed. - The PR intentionally excludes the earlier PRD/TDD planning notes under `docs/superpowers/`; those remain local-only per request. * fix: align Studio capture with preview (#595) Studio frame capture could fail for projects mounted outside the repo when the project id came from an encoded hash route. A project like `Notion Showcase` loaded as `#project/Notion%20Showcase`, but the capture URL encoded that already-encoded value again, producing `/api/projects/Notion%2520Showcase/...` and a 404. While validating the fix by seeking through the preview, capture also diverged from the visible player for nested compositions because the thumbnail route sought raw timelines instead of the same player seek path used by Studio preview. - Decodes project ids when reading Studio `#project/...` routes and centralizes project hash/API path construction. - Keeps API URLs encoded exactly once, including project names with spaces, literal `%`, reserved characters, and unicode. - Updates Studio thumbnail capture to prefer `window.__player.seek(t)` and only fall back to raw timeline seeking for standalone pages. - Preserves explicit `t=0` thumbnail requests instead of falling back to `0.5` seconds. - Adds preview-regression CI coverage for Studio routing, frame capture URL construction, thumbnail seeking, and core thumbnail seek parsing. Studio treated the hash route segment as the canonical project id even when the browser had already percent-encoded it. `buildFrameCaptureUrl` then encoded that string again, so a decoded project directory name and the capture API path no longer matched. The preview/capture mismatch was a separate seek-path issue: the visible Studio preview seeks through the HyperFrames player, which maps global time into nested composition time. The capture route bypassed that layer and paused all registered timelines at the same global time. The zero-second capture case came from parsing `t` with a truthiness fallback, so `parseFloat("0") || 0.5` became `0.5`. - `bun run --cwd packages/studio test -- vite.thumbnail.test.ts src/utils/projectRouting.test.ts src/utils/frameCapture.test.ts` - `bun run --cwd packages/core test -- src/studio-api/routes/thumbnail.test.ts` - `bunx oxfmt --check .github/workflows/preview-regression.yml packages/studio/vite.thumbnail.ts packages/studio/vite.thumbnail.test.ts packages/studio/vite.config.ts packages/studio/src/utils/projectRouting.ts packages/studio/src/utils/projectRouting.test.ts packages/studio/src/utils/frameCapture.ts packages/studio/src/App.tsx packages/core/src/studio-api/routes/thumbnail.ts packages/core/src/studio-api/routes/thumbnail.test.ts` - `bunx oxlint .github/workflows/preview-regression.yml packages/studio/vite.thumbnail.ts packages/studio/vite.thumbnail.test.ts packages/studio/vite.config.ts packages/studio/src/utils/projectRouting.ts packages/studio/src/utils/projectRouting.test.ts packages/studio/src/utils/frameCapture.ts packages/studio/src/App.tsx packages/core/src/studio-api/routes/thumbnail.ts packages/core/src/studio-api/routes/thumbnail.test.ts` - `bun run --cwd packages/studio typecheck` - `bun run --cwd packages/core build:hyperframes-runtime` - `bun run --cwd packages/core typecheck` - `git diff --check` Pre-commit also reran lint, format, and typecheck successfully for the committed files. Using `agent-browser`, I mounted `/Users/miguel07code/Downloads/Notion Showcase` into Studio's project data and opened: ```text http://127.0.0.1:5197/#project/Notion%20Showcase ``` Before the fix, Capture requested `/api/projects/Notion%2520Showcase/thumbnail/index.html?...` and Studio showed `Capture failed`. After the fix, I sought the preview to `0s`, `2s`, `10s`, and `18s`, captured each frame, and compared the visible preview crop against the capture output. The capture URLs all used `Notion%20Showcase`, not `Notion%2520Showcase`, and no failure toast appeared. Mean pixel diffs for preview vs capture were: - `0s`: `0.0` - `2s`: `0.8641` - `10s`: `0.3496` - `18s`: `0.2309` The small non-zero diffs are raster/antialias-level differences after resizing the capture to the preview crop dimensions. - Browser screenshots, comparison sheets, network logs, and the `agent-browser` recording are local-only under `qa-artifacts/capture-button/` and are not committed. - The local Notion Showcase project mount is an ignored symlink under `packages/studio/data/projects/` and is not committed. - Thumbnail cache versions were bumped so stale captures generated with the old seek behavior are not reused. * feat: persist studio manual edits via manifest * fix(studio): stabilize manual edit manifest rendering * fix(studio): allow master canvas layer selection * fix(studio): scale master edits in source coordinates * fix(studio): reapply manual edits during playback * fix(studio): keep rotation edit base stable * feat(studio): highlight hovered canvas target * fix(studio): drag hovered canvas targets immediately * fix(studio): rotate manual edits around center * fix(studio): keep rotate handle aligned while dragging * fix(studio): allow small rotation adjustments * fix(studio): match rotate handle size to resize handle * fix(studio): connect rotate handle line to selection * feat(studio): reset selected manual edits * fix(studio): route inspector geometry through manual edits * feat: add studio group repositioning * fix: preserve studio group selections * fix: seed additive studio selection groups * fix: select studio groups on pointerdown * fix: harden studio group overlay events * fix: address studio manual edit review feedback * fix: apply nested manual edits in drilled previews * fix: commit drag offsets from gesture math * fix: persist manual preview edits on refresh * fix: harden manual edit refresh apply * fix: share manual edit render runtime * chore: release v0.5.0-alpha.15 * feat(core): add studio animation preview APIs * feat(studio): add alpha editor layer inspector * chore: release v0.6.0-alpha.1 * feat(studio): enable inspector panels by default * fix(studio): keep motion panel opt-in * chore: release v0.6.0-alpha.2 * feat: auto-open timeline clip layers * feat: show composition loading in studio * feat: disable Studio timeline while composition loads * chore: ignore .claude directory * chore: release v0.6.0-alpha.3 * feat(studio): simplify inspector selection ux * fix(studio): keep notion preview playback moving * fix(studio): handle raster inspector clicks * fix(studio): stale selection, rotation control, design panel polish Fixes and improvements based on power-user testing feedback: 1. Fix stale selection after style edits — handleDomStyleCommit now calls refreshDomEditSelectionFromPreview after persisting, matching every other commit handler. Without this, the PropertyPanel showed frozen computedStyles after color/radius/shadow edits, making it look like editing "didn't work." Also adds error handling around the persist call. 2. Add rotation field to the Design panel Layout section — reads the current rotation angle from the manual edit manifest and commits via the existing handleDomRotationCommit handler. 3. Enable motion panel by default — STUDIO_MOTION_PANEL_ENABLED now defaults to true so the Motion tab is discoverable without env vars. 4. Color controls only when element has color — fill color section now only shows when the element has an explicit non-transparent background-color. Text color shows only when the element has a color style. Prevents showing color pickers on elements where color edits have no visible effect. 5. Exclude canvas from selection — added "canvas" to DOM_LAYER_IGNORED_TAGS so canvas elements are not selectable in the preview or listed in the layer panel. 6. Multi-selection feedback — shows "N elements selected" with guidance instead of the generic empty state when multiple elements are selected. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix(studio): prevent browser launch timeout from crashing dev server The shared Puppeteer browser pool in getSharedBrowser() could throw a 30s TimeoutError during launch. This error propagated as an uncaught rejection and killed the vite process, even though generateThumbnail had its own try/catch — the browser launch promise rejected outside that scope. Now getSharedBrowser itself catches launch failures and returns null, so thumbnails degrade gracefully instead of crashing. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix(studio): revert motion panel default to false Motion panel stays opt-in via env var per product direction. Only the Design panel is enabled by default. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix(studio): prevent read-only property crash in manual edit wrappers The seek/play/applyAfter wrapper functions in manualEdits.ts crashed with "Cannot set property X which has only a getter" when the player or timeline objects define seek/play as getter-only properties. This prevented ALL manual edits (position, rotation, size) from persisting to disk — the error thrown during applyCurrentStudioManualEditsToPreview aborted the save queue. Wrapped all three property assignments in try/catch so wrapping gracefully degrades when the target object is non-configurable. Verified: position edit (X=42px) now persists to .hyperframes/studio-manual-edits.json and survives page refresh. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: alpha preview e2e fixes — exports, init templates, EPIPE crash Three bugs found via automated e2e testing of the v0.6.0-alpha preview: 1. core: add missing package.json export specifiers for studio-api/manual-edits-render-script and studio-api/studio-motion-render-script — the alpha.3 npm publish failed because the studio build could not resolve these sub-paths. 2. cli: fix init --example creating empty projects — tsup leaves empty template directories in dist/ during the build, causing existsSync(templateDir) to return true and skip the remote fetch fallback. Now checks for index.html inside the dir instead. 3. engine: fix unhandled EPIPE crash in streaming encoder — ffmpeg stdin/stdout had no error handlers, so a write after the ffmpeg process exits throws an uncaught error that crashes the process. Verified with 8 consecutive e2e iterations (424 test runs, 0 flaky). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix(studio): thumbnail crash, feature defaults, multi-select UX, fps selector Power-user audit fixes for the alpha studio: - vite.config.ts: wrap thumbnail generation in try/catch so Puppeteer TimeoutError doesn't crash the entire vite dev server as an uncaught rejection. Close the page on error to prevent browser session leaks. - manualEditingAvailability.ts: enable motion panel and manual canvas drag editing by default (were both false, undiscoverable without knowing the env vars). - PropertyPanel.tsx: show "N elements selected" feedback when multiple elements are selected instead of the generic "Select an element" empty state. - RenderQueue.tsx + App.tsx: add FPS selector (24/30/60) to the render export bar instead of hardcoding 30fps. Pass the user's choice through to startRender. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * chore: release v0.6.0-alpha.4 * fix(runtime): update clock duration when root timeline is late-bound Compositions with external sub-compositions (like apple-presentation with 7 slides) load child compositions via fetch(). The root GSAP timeline is only bound after all external compositions finish loading, but the TransportClock duration was only set during initial setup. When bindRootTimelineIfAvailable runs after the external compositions load, it captures the root timeline but never updates the clock. player.getDuration() continues returning 0, so the player's probe interval never fires the 'ready' event, and the Studio shows "Loading composition" indefinitely. Now bindRootTimelineIfAvailable updates clock.setDuration when the root timeline is late-bound. Guarded with try/catch for the early call site where clock is not yet initialized (temporal dead zone). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix(studio): block element selection while composition is loading Prevent users from selecting elements in the preview while the composition is still loading (showing "Loading composition" overlay). Selection and hover highlighting are suppressed until the player fires the ready event. Also reverts motion panel and manual drag editing defaults to false — these were accidentally set to true during the PR #693 merge. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * chore: release v0.6.0-alpha.5 * chore: release v0.6.0-alpha.6 * fix(runtime): remove per-tick timeline.pause() that causes audio stutter The seekRuntimeTimeline helper added timeline.pause() before every totalTime() seek. During transport-driven playback, this runs 60 times per second, causing GSAP to cascade pause events to media elements on every frame. The result: audio plays/stops/plays/stops in a stutter pattern. The captured root timeline is already paused once in player.play() — the TransportClock drives it via totalTime(t) which keeps it paused. The extra per-tick pause() was redundant for the root timeline but actively harmful for media sync. Fix: restore the original inline seek for the captured timeline (totalTime without pause), keep seekRuntimeTimeline with pause() only for standalone child timelines where explicit pause control is needed. Also fixes rebase artifact: missing PropertyPanel props in App.tsx. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * chore: release v0.6.0-alpha.7 * fix(studio): restore text field handlers lost in rebase Restores handleDomAddTextField and handleDomRemoveTextField that were dropped when resolving App.tsx conflicts during the main→next rebase. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * chore: release v0.6.0-alpha.8 * fix(runtime): comprehensive audio stutter fix Three changes that together caused audio play/stop/play/stop stutter during transport-driven playback: 1. seekRuntimeTimeline called timeline.pause() before every totalTime() seek, 60x per second. GSAP cascades pause to media elements on every frame. Fix: restore original inline seek for the captured timeline (totalTime without pause). The timeline is already paused once in player.play(). seekRuntimeTimeline with pause() remains only for standalone child timelines. 2. player.play() removed the !tl guard, allowing play without a captured timeline. But getSafeTimelineDurationSeconds(null) returns 0, so the clock has no duration → immediately reaches end → stops → restarts. Fix: when no timeline provides duration, fall back to the root composition element's data-duration attribute. 3. Audio source attachment added networkState guard that could cause the clock to flicker between audio-source and monotonic timing on transient media states. Fix: keep !rawEl.error guard (prevents errored audio from freezing the clock) but drop the networkState check. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix(runtime): skip drift corrections on playing video elements Seeking a playing video resets the browser's decoder pipeline, causing a ~150ms freeze while it re-buffers. During that freeze the monotonic clock advances, drift grows, and strict sync fires another seek — creating a perpetual stutter loop (176 seek events / 8s observed on the apple-presentation composition). Skip strict and force drift corrections for playing video elements; only hard sync (>0.5s catastrophic drift) warrants the decoder-reset cost. Audio elements are unaffected and retain the full correction tiers. Also propagate the asset-loading overlay state to the timeline so controls are disabled during "Preparing preview assets", matching the existing behavior for the initial composition loading overlay. * chore: release v0.6.0-alpha.9 * feat(studio): consolidate keyboard shortcuts into single handler Move all window-level keyboard shortcuts from 4 separate files into one `handleAppKeyDown` listener in App.tsx: - Shift+T: toggle timeline (was App.tsx, separate useMountEffect) - Cmd/Ctrl+Z: undo (was App.tsx, separate useEffect) - Cmd/Ctrl+Shift+Z: redo (was App.tsx, separate useEffect) - Cmd/Ctrl+1: sidebar Compositions tab (was LeftSidebar.tsx) - Cmd/Ctrl+2: sidebar Assets tab (was LeftSidebar.tsx) - Delete/Backspace: remove selected element (was Timeline.tsx) LeftSidebar exposes a ref handle for tab switching. Timeline watches selectedElement becoming null to clean up popover/range UI state. History hotkey kept as named function for iframe forwarding. Playback shortcuts (Space, J/K/L, arrows) and caption nudge remain in their component hooks — tightly coupled to component state. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix(studio): sidebar tab overflow + hot-reload double-refresh 1. Sidebar tabs: use equal 1fr columns, shorter "Comps" label, truncate on overflow, tighter padding. Fixes tabs clipping outside the rounded pill at narrow sidebar widths. 2. Hot reload: set domEditSaveTimestampRef before every save-then-refresh path (source editor, timeline move/resize/delete, asset drop). The file-change watcher already checks this timestamp and suppresses echoed events — but source editor saves and timeline operations weren't setting it, causing a double refreshKey increment that could leave the player in a non-playable state. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix(studio): delete key removes preview-selected elements The consolidated keyboard handler only checked selectedElementId (timeline clips). When a user selected a child element in the preview via the inspector, selectedElementId was null because the element didn't correspond to a top-level timeline clip, so Delete/Backspace did nothing. Add handleDomEditElementDelete that removes the element referenced by the current domEditSelection via the remove-element mutation API. The Delete key handler now falls through from timeline selection to DOM edit selection. * fix(studio): remove unused deleteInFlightRef from Timeline Leftover from moving Delete handling to the consolidated keyboard handler in App.tsx. Also suppress pre-existing exhaustive-deps warning on the intentional every-render selection-change watcher. * fix(studio): forward all keyboard shortcuts to preview iframe The consolidated handleAppKeyDown was only added to the parent window. When focus was inside the preview iframe (after clicking an element), keydown events didn't reach the parent, so Delete and other shortcuts didn't fire. Replace the per-function iframe forwarding (handleTimelineToggleHotkey only) with the full app-level handler via a ref-stable wrapper. All app shortcuts (Delete, Undo/Redo, Shift+T, Cmd+1/2) now work from within the preview iframe. * fix(core): search inside <template> content when removing elements linkedom's document.querySelectorAll does not traverse <template> content. Elements in template-based compositions (like .title-word, .bullet-text) were invisible to the removal logic, so delete returned changed: false and the element survived the reload. Fall back to template.querySelectorAll when the document-level query returns no matches. Uses template.querySelectorAll directly (not template.content.querySelectorAll) because removing from the content DocumentFragment doesn't update the serialized output. * fix(studio): suppress loading overlay on hot-reload Only show the composition loading overlay on the first iframe load. Hot-reloads (source editor save, timeline edits, element delete) no longer flash the full-screen loading state. * fix(studio): reorder design panel, fix stroke height, rename Blending - Move Text section to the top of the panel (before Layout) - Remove Selection Colors section - Rename "Blending" to "Transparency" - Fix stroke Width/Style height mismatch by making SelectField use inline label layout matching MetricField * fix(studio): prevent panel scroll when wheel-adjusting metric inputs React registers onWheel passively, so preventDefault had no effect on the parent scroll container. Replace with a native wheel listener (passive: false) that blocks both default scroll and propagation. * chore: release v0.6.0-alpha.10 * chore: release v0.6.0-alpha.11 * fix(studio): clean next alpha inspector artifacts * chore: release v0.6.0-alpha.12 * fix(studio,player,core): eliminate double audio and manifest polling loop (#722) Three bugs that compound in Studio preview: 1. **Double audio on pause/resume**: syncRuntimeMedia played audio through the HTML <audio> element while WebAudioTransport simultaneously played the same source through AudioBufferSourceNode. Fixed by passing webAudio.isActive() as outputMuted so HTML elements stay muted when Web Audio owns playback. Also removed the priorMuted restore in stopAll() which raced with the next play cycle. 2. **Manifest polling loop**: applyStudioManualEditsToPreview and applyStudioMotionToPreview unconditionally fetched from disk on every call, even without forceFromDisk. The runtime posts state messages every frame via postMessage, triggering React re-renders that re-invoked these functions ~60x/second. Fixed by returning early when no disk read is requested, and using refs instead of callbacks in useEffect deps. 3. **Parent proxy double-play**: the player web component created parent-frame audio proxies even when the runtime bridge was available, causing two audio sources on autoplay-blocked promotion. Fixed by skipping proxy creation when _hasRuntimeBridge returns true, and synchronously muting iframe media on promotion to close the async race window. Also fixes pre-existing ResolutionPreset type missing square variants. Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix(studio): improve font picker and text property controls (#736) - Line height and letter-spacing: convert from free-text to select with presets - Font style: remove oblique (browser falls back to italic), keep normal/italic - Font weight: detect available weights via document.fonts.check(), add labels - Font source: local fonts matching Google catalog tagged as Google - Font list: balanced per-source caps prevent any source from being cut off - Sort order: Google fonts rank before Local so curated fonts appear first Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix(studio): inspector visibility, undo/redo blinking, and preview caching Inspector picks invisible elements when an ancestor has GSAP-set opacity: 0 because CSS opacity is not inherited — getComputedStyle on the child still returns 1. Walk the ancestor chain in the picker, domEditing, and overlay visibility checks to catch this. Also: - Containers with all-invisible children are no longer selectable - Selection/hover overlay hides during playback and while loading - Undo/redo no longer double-refreshes (echo suppression for all file writes) - Undo/redo reloads iframe in-place instead of recreating the Player, preserving shader transition cache - Preview routes return ETag + Cache-Control headers; composition HTML uses project signature for conditional 304, binary assets use mtime+size - Loading overlay deferred 400ms so cached loads never flash it * fix(studio): remove timeline inspector buttons, enable manual dragging Remove the eye icon (inspector) and image icon (thumbnail toggle) from timeline clips. The timeline layer inspector feature and all supporting code is removed. Enable manual dragging in the preview by default. Add scrub-to-drag on X/Y/W/H fields in the design panel. Hide the Radius section when the element has no visible background. Fix pre-existing ResolutionPreset type for square presets. * chore: release v0.6.0-alpha.13 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix(studio): add rotation field, inline element drag, fix manifest load regression (#743) - Add rotation (R) field to geometry row (X, Y, W, H, R) in property panel. Goes through manifest via handleDomRotationCommit, resettable with Reset Edits. - Auto-promote display:inline elements to inline-block when dragged so translate works on inline spans. - Fix regression from polling fix: iframe load now passes readFromDiskFirst to load manifest from disk, so Reset Edits finds existing entries. Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * refactor(studio): decompose App.tsx monolith (4297 → 567 lines) (#741) * refactor(studio): decompose App.tsx from 4297 to 567 lines Break the monolithic StudioApp component into focused modules: Hooks (12 new): - usePanelLayout: resizable/collapsible panel state - useFileManager: file tree, CRUD, uploads, derived lists - useManifestPersistence: manual edit + motion manifest save queue - useTimelineEditing: clip move/resize/delete/drop handlers - useDomEditSession: DOM selection, style/text commits, preview interaction - useAppHotkeys: keyboard shortcuts, undo/redo, iframe hotkey sync - useCaptionDetection: auto-detect caption compositions - useRenderClipContent: timeline clip thumbnail rendering - useConsoleErrorCapture: preview iframe console error capture - useFrameCapture: frame capture download flow - useLintModal: lint execution and modal state - useCompositionDimensions: stage-size message listener Components (6 new): - AskAgentModal: agent prompt modal - StudioHeader: toolbar with undo/redo, capture, inspector toggle - StudioLeftSidebar: file tree + code editor (handles collapsed state) - StudioPreviewArea: NLELayout + overlays + caption timeline - StudioRightPanel: Design/Motion/Renders tab panel - TimelineToolbar: zoom controls + timeline toggle Utilities (4 new): - studioHelpers: types, path helpers, DOM utilities - studioPreviewHelpers: preview pointer/player interaction - domEditHelpers: selection group algebra - studioFontHelpers: font injection + @font-face management Also removes dead timeline layer inspector code (eye icon, thumbnail toggle, layer panel) that was disabled behind a feature flag. * feat(studio): add Layer (z-index) field to design panel Adds a scrub-enabled "Layer" field below the W/H inputs in the Layout section. Available for all elements regardless of style editing capability since z-index is fundamental to composition stacking order. * docs: architecture spec for studio domain contexts, hook split, and file-size lint * docs: implementation plan for studio contexts, hook split, and file-size lint * refactor(studio): consolidate duplicate helpers in useDomEditSession Remove ~370 lines of helper functions that were copied into the hook instead of imported. All removed functions already exist in the canonical utility files (studioHelpers, studioFontHelpers, studioPreviewHelpers, domEditHelpers). Also removes the duplicate local type definitions for RightPanelTab, AgentModalAnchorPoint, and PreviewLocalPointer, and drops now-unused imports (googleFontStylesheetUrl, importedFontFaceCss, resolveVisualDomEditSelectionTarget, DomEditViewport). Temporarily excludes useDomEditSession.ts from the 500 LOC file-size check until Tasks 3-5 split it into focused hooks. * refactor(studio): extract useDomSelection from useDomEditSession * refactor(studio): extract useAskAgentModal from useDomEditSession * refactor(studio): extract usePreviewInteraction from useDomEditSession * refactor(studio): extract useDomEditCommits, useDomEditSession now thin orchestrator Split the 897-line useDomEditSession into focused hooks: - useDomEditCommits (439 LOC): manifest commits (path offset, box size, rotation, manual edits reset, motion), persist operations, element delete, font asset resolution - useDomEditTextCommits (329 LOC): style/text/text-field commits - useDomEditSession (339 LOC): thin orchestrator wiring selection, agent modal, preview interaction, and commit hooks All files now under 500 LOC limit. Removed the temporary lefthook filesize exclusion for useDomEditSession. * feat(studio): add 4 domain contexts (PanelLayout, FileManager, DomEdit, Studio) Create context providers that wrap hook return values for prop-drilling elimination. Each context destructures and reconstructs the value inside useMemo so exhaustive-deps is satisfied and re-renders are minimized. Not yet wired into App.tsx — that comes in a follow-up. * refactor(studio): wire domain contexts, eliminate prop drilling in 4 components Wire StudioProvider, PanelLayoutProvider, FileManagerProvider, and DomEditProvider in App.tsx. Migrate StudioHeader, StudioLeftSidebar, StudioPreviewArea, and StudioRightPanel to consume contexts instead of props. Prop counts reduced: - StudioHeader: 13 -> 6 - StudioLeftSidebar: 19 -> 4 - StudioPreviewArea: 37 -> 11 - StudioRightPanel: 39 -> 3 Net: -118 lines, 108 props removed from call sites. * chore: upgrade to React 19 Upgrade react and react-dom from 18.3 to 19.2.6 across the workspace. Add resolutions/overrides in root package.json to prevent peer dependency pins (e.g. @phosphor-icons/react) from pulling React 18. Regenerate bun.lock. This enables the React 19 context syntax (<Context value={...}>) used by the new domain contexts. * fix(studio): refresh preview after z-index change so stacking updates visually * fix(studio): remove duplicate duration override causing oscillation The timeline message handler set the duration twice: once via processTimelineMessage and once via a raw durationInFrames override. When drilled into a sub-composition, these could disagree, causing the duration to oscillate after element deletion. * fix(studio): use in-place iframe reload after clip delete, remove confirm dialogs Two changes to fix duration oscillation after deleting a timeline clip: 1. Replace setRefreshKey (full Player remount) with in-place iframe.contentWindow.location.reload() after deleting a clip. The full remount triggered a chaotic re-probing cycle with multiple duration sources (adapter, manifest, postMessage) fighting each other, causing the timeline to oscillate between durations. In-place reload preserves the Player web component and its state. 2. Remove window.confirm dialogs from both timeline clip delete and DOM element delete. Undo is available so the confirmation adds friction without value. * chore: gitignore docs/superpowers * feat(studio): add favicon * perf(studio): skip no-op state updates in timeline sync syncTimelineElements was called 60+ times per page load, each time triggering setElements/setDuration/setTimelineReady even when nothing changed. This caused massive re-render churn and memory usage. Add early-return guards to skip updates when values haven't changed. Also fixes the duration oscillation after element delete. * refactor(studio): split PropertyPanel.tsx (3126 LOC) into 8 focused modules The monolithic PropertyPanel.tsx exceeded the 500 LOC filesize limit. Split into cohesive modules by responsibility: - propertyPanelHelpers.ts (401) — pure utility functions, shared types/constants - propertyPanelPrimitives.tsx (357) — CommitField, MetricField, DetailField, SliderControl, SegmentedControl, SelectField, Section - propertyPanelColor.tsx (371) — ColorField, ColorSlider - propertyPanelFill.tsx (421) — ImageFillField, GradientField, asset path helpers - propertyPanelFont.tsx (455) — FontFamilyField + font catalog helpers - propertyPanelSections.tsx (453) — TextSection, TextFieldEditor, text controls - propertyPanelStyleSections.tsx (411) — StyleSections (stroke, effects, clip, fill) - PropertyPanel.tsx (347) — main component, LayerTree, re-exports for consumers All re-exports from PropertyPanel.tsx preserved for backwards compatibility. No behavioral changes — pure structural split. * fix(studio): use in-place iframe reload for all timeline operations Replace setRefreshKey with in-place iframe reload for move, resize, and asset drop — matching delete which was already fixed. Prevents the Player remount probe cycle that causes duration oscillation. * perf(studio): replace 5s polling loop with event-driven adapter init The Player's onIframeLoad used a setInterval polling loop (25 attempts × 200ms = 5 seconds) to detect when the runtime's __player/__timeline globals appeared. Each poll that missed triggered wasted work, and multiple duration sources fighting during the probe cycle caused oscillation bugs. Replace with event-driven initialization: 1. Fast path: try initializeAdapter() immediately (works for in-place reloads where the adapter is already present) 2. If not ready, listen for the runtime's "state"/"timeline" postMessage signals and initialize on the first one 3. Single 5s timeout as safety net (replaces 25 interval ticks) This eliminates the polling overhead, reduces setDuration/setElements calls to exactly 1 per load, and makes the Player responsive within one frame of the runtime being ready instead of up to 200ms later. * fix(studio): prevent duration oscillation after element delete Two fixes for the duration display oscillating between sub-composition and master durations after deleting an element in the preview: 1. Clear store elements before iframe reload in handleDomEditElementDelete. Without this, stale pre-delete elements remain in the store and cause mergeTimelineElementsPreservingDowngrades to alternate between REPLACE and PRESERVE modes as the element count fluctuates. 2. Add 500ms cooldown on enrichMissingCompositions after timeline messages. The "state" handler was calling enrichMissingCompositions every ~80ms, which added extra elements from GSAP timelines. These fought with the authoritative element list from "timeline" messages (~333ms), creating a feedback loop where element count oscillated and triggered alternating merge strategies with different durations. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix(studio): single reloadPreview as source of truth for preview refresh Create reloadPreview() in App.tsx that encapsulates the correct behavior (in-place iframe reload with setRefreshKey fallback). Pass it as the sole refresh mechanism to hooks, removing direct setRefreshKey access from useTimelineEditing and useDomEditCommits. --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * refactor(studio): decompose App.tsx from 4297 to 567 lines Break the monolithic StudioApp component into focused modules: Hooks (12 new): - usePanelLayout: resizable/collapsible panel state - useFileManager: file tree, CRUD, uploads, derived lists - useManifestPersistence: manual edit + motion manifest save queue - useTimelineEditing: clip move/resize/delete/drop handlers - useDomEditSession: DOM selection, style/text commits, preview interaction - useAppHotkeys: keyboard shortcuts, undo/redo, iframe hotkey sync - useCaptionDetection: auto-detect caption compositions - useRenderClipContent: timeline clip thumbnail rendering - useConsoleErrorCapture: preview iframe console error capture - useFrameCapture: frame capture download flow - useLintModal: lint execution and modal state - useCompositionDimensions: stage-size message listener Components (6 new): - AskAgentModal: agent prompt modal - StudioHeader: toolbar with undo/redo, capture, inspector toggle - StudioLeftSidebar: file tree + code editor (handles collapsed state) - StudioPreviewArea: NLELayout + overlays + caption timeline - StudioRightPanel: Design/Motion/Renders tab panel - TimelineToolbar: zoom controls + timeline toggle Utilities (4 new): - studioHelpers: types, path helpers, DOM utilities - studioPreviewHelpers: preview pointer/player interaction - domEditHelpers: selection group algebra - studioFontHelpers: font injection + @font-face management Also removes dead timeline layer inspector code (eye icon, thumbnail toggle, layer panel) that was disabled behind a feature flag. * docs: architecture spec for studio domain contexts, hook split, and file-size lint * docs: implementation plan for studio contexts, hook split, and file-size lint * refactor(studio): consolidate duplicate helpers in useDomEditSession Remove ~370 lines of helper functions that were copied into the hook instead of imported. All removed functions already exist in the canonical utility files (studioHelpers, studioFontHelpers, studioPreviewHelpers, domEditHelpers). Also removes the duplicate local type definitions for RightPanelTab, AgentModalAnchorPoint, and PreviewLocalPointer, and drops now-unused imports (googleFontStylesheetUrl, importedFontFaceCss, resolveVisualDomEditSelectionTarget, DomEditViewport). Temporarily excludes useDomEditSession.ts from the 500 LOC file-size check until Tasks 3-5 split it into focused hooks. * refactor(studio): extract useDomSelection from useDomEditSession * refactor(studio): extract useAskAgentModal from useDomEditSession * refactor(studio): extract usePreviewInteraction from useDomEditSession * refactor(studio): extract useDomEditCommits, useDomEditSession now thin orchestrator Split the 897-line useDomEditSession into focused hooks: - useDomEditCommits (439 LOC): manifest commits (path offset, box size, rotation, manual edits reset, motion), persist operations, element delete, font asset resolution - useDomEditTextCommits (329 LOC): style/text/text-field commits - useDomEditSession (339 LOC): thin orchestrator wiring selection, agent modal, preview interaction, and commit hooks All files now under 500 LOC limit. Removed the temporary lefthook filesize exclusion for useDomEditSession. * refactor(studio): wire domain contexts, eliminate prop drilling in 4 components Wire StudioProvider, PanelLayoutProvider, FileManagerProvider, and DomEditProvider in App.tsx. Migrate StudioHeader, StudioLeftSidebar, StudioPreviewArea, and StudioRightPanel to consume contexts instead of props. Prop counts reduced: - StudioHeader: 13 -> 6 - StudioLeftSidebar: 19 -> 4 - StudioPreviewArea: 37 -> 11 - StudioRightPanel: 39 -> 3 Net: -118 lines, 108 props removed from call sites. * fix(studio): refresh preview after z-index change so stacking updates visually * fix(studio): remove duplicate duration override causing oscillation The timeline message handler set the duration twice: once via processTimelineMessage and once via a raw durationInFrames override. When drilled into a sub-composition, these could disagree, causing the duration to oscillate after element deletion. * fix(studio): use in-place iframe reload after clip delete, remove confirm dialogs Two changes to fix duration oscillation after deleting a timeline clip: 1. Replace setRefreshKey (full Player remount) with in-place iframe.contentWindow.location.reload() after deleting a clip. The full remount triggered a chaotic re-probing cycle with multiple duration sources (adapter, manifest, postMessage) fighting each other, causing the timeline to oscillate between durations. In-place reload preserves the Player web component and its state. 2. Remove window.confirm dialogs from both timeline clip delete and DOM element delete. Undo is available so the confirmation adds friction without value. * chore: gitignore docs/superpowers * perf(studio): skip no-op state updates in timeline sync syncTimelineElements was called 60+ times per page load, each time triggering setElements/setDuration/setTimelineReady even when nothing changed. This caused massive re-render churn and memory usage. Add early-return guards to skip updates when values haven't changed. Also fixes the duration oscillation after element delete. * refactor(studio): split PropertyPanel.tsx (3126 LOC) into 8 focused modules The monolithic PropertyPanel.tsx exceeded the 500 LOC filesize limit. Split into cohesive modules by responsibility: - propertyPanelHelpers.ts (401) — pure utility functions, shared types/constants - propertyPanelPrimitives.tsx (357) — CommitField, MetricField, DetailField, SliderControl, SegmentedControl, SelectField, Section - propertyPanelColor.tsx (371) — ColorField, ColorSlider - propertyPanelFill.tsx (421) — ImageFillField, GradientField, asset path helpers - propertyPanelFont.tsx (455) — FontFamilyField + font catalog helpers - propertyPanelSections.tsx (453) — TextSection, TextFieldEditor, text controls - propertyPanelStyleSections.tsx (411) — StyleSections (stroke, effects, clip, fill) - PropertyPanel.tsx (347) — main component, LayerTree, re-exports for consumers All re-exports from PropertyPanel.tsx preserved for backwards compatibility. No behavioral changes — pure structural split. * fix(studio): use in-place iframe reload for all timeline operations Replace setRefreshKey with in-place iframe reload for move, resize, and asset drop — matching delete which was already fixed. Prevents the Player remount probe cycle that causes duration oscillation. * perf(studio): replace 5s polling loop with event-driven adapter init The Player's onIframeLoad used a setInterval polling loop (25 attempts × 200ms = 5 seconds) to detect when the runtime's __player/__timeline globals appeared. Each poll that missed triggered wasted work, and multiple duration sources fighting during the probe cycle caused oscillation bugs. Replace with event-driven initialization: 1. Fast path: try initializeAdapter() immediately (works for in-place reloads where the adapter is already present) 2. If not ready, listen for the runtime's "state"/"timeline" postMessage signals and initialize on the first one 3. Single 5s timeout as safety net (replaces 25 interval ticks) This eliminates the polling overhead, reduces setDuration/setElements calls to exactly 1 per load, and makes the Player responsive within one frame of the runtime being ready instead of up to 200ms later. * fix(studio): prevent duration oscillation after element delete Two fixes for the duration display oscillating between sub-composition and master durations after deleting an element in the preview: 1. Clear store elements before iframe reload in handleDomEditElementDelete. Without this, stale pre-delete elements remain in the store and cause mergeTimelineElementsPreservingDowngrades to alternate between REPLACE and PRESERVE modes as the element count fluctuates. 2. Add 500ms cooldown on enrichMissingCompositions after timeline messages. The "state" handler was calling enrichMissingCompositions every ~80ms, which added extra elements from GSAP timelines. These fought with the authoritative element list from "timeline" messages (~333ms), creating a feedback loop where element count oscillated and triggered alternating merge strategies with different durations. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix(studio): single reloadPreview as source of truth for preview refresh Create reloadPreview() in App.tsx that encapsulates the correct behavior (in-place iframe reload with setRefreshKey fallback). Pass it as the sole refresh mechanism to hooks, removing direct setRefreshKey access from useTimelineEditing and useDomEditCommits. * fix: resolve lint errors from rebase (unused imports, duplicate declarations) * fix: prefix unused probeResult variable * fix: restore renderOrchestrator.ts from origin/next (rebase conflict artifact) * fix: resolve rebase conflicts by using main's producer and next's studio/player * fix: restore rebase-conflicted files from origin/next * fix: use 'load' instead of 'networkidle0' for Puppeteer waitUntil (type compatibility) * fix: restore webAudioTransport.ts from main (test compatibility) --------- Co-authored-by: Vance Ingalls <vance@heygen.com> Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Vance Ingalls
Claude Opus 4.6
parent
b8a2c48291
commit
38efe168e2
@@ -367,9 +367,11 @@ describe("bundleToSingleHtml", () => {
|
||||
const host = document.querySelector("#scene-host");
|
||||
|
||||
expect(host?.getAttribute("data-composition-id")).toBe("scene");
|
||||
expect(host?.getAttribute("data-composition-file")).toBe("compositions/scene.html");
|
||||
expect(host?.getAttribute("data-start")).toBe("intro");
|
||||
expect(host?.getAttribute("data-width")).toBe("1920");
|
||||
expect(host?.querySelector(".title")?.textContent).toBe("Scene");
|
||||
expect(host?.querySelector(".title")?.closest("[data-composition-file]")).toBe(host);
|
||||
expect(
|
||||
Array.from(host?.children ?? []).some(
|
||||
(child) => child.getAttribute("data-composition-id") === "scene",
|
||||
|
||||
@@ -7,7 +7,11 @@ import {
|
||||
parseHTMLContent,
|
||||
stripEmbeddedRuntimeScripts,
|
||||
} from "./htmlDocument";
|
||||
import { rewriteAssetPaths, rewriteCssAssetUrls } from "./rewriteSubCompPaths";
|
||||
import {
|
||||
rewriteAssetPaths,
|
||||
rewriteCssAssetUrls,
|
||||
rewriteInlineStyleAssetUrls,
|
||||
} from "./rewriteSubCompPaths";
|
||||
import { scopeCssToComposition, wrapScopedCompositionScript } from "./compositionScoping";
|
||||
import { validateHyperframeHtmlContract } from "./staticGuard";
|
||||
import { getHyperframeRuntimeScript } from "../generated/runtime-inline";
|
||||
@@ -556,18 +560,31 @@ export async function bundleToSingleHtml(
|
||||
el.setAttribute(attr, val);
|
||||
},
|
||||
);
|
||||
const styledEls = innerRoot
|
||||
? innerRoot.querySelectorAll("[style]")
|
||||
: contentDoc.querySelectorAll("[style]");
|
||||
rewriteInlineStyleAssetUrls(
|
||||
styledEls,
|
||||
src,
|
||||
(el: Element) => el.getAttribute("style"),
|
||||
(el: Element, val: string) => {
|
||||
el.setAttribute("style", val);
|
||||
},
|
||||
);
|
||||
|
||||
if (innerRoot) {
|
||||
const innerW = innerRoot.getAttribute("data-width");
|
||||
const innerH = innerRoot.getAttribute("data-height");
|
||||
if (innerW && !hostEl.getAttribute("data-width")) hostEl.setAttribute("data-width", innerW);
|
||||
if (innerH && !hostEl.getAttribute("data-height")) hostEl.setAttribute("data-height", innerH);
|
||||
innerRoot.setAttribute("data-composition-file", src);
|
||||
for (const child of [...innerRoot.querySelectorAll("style, script")]) child.remove();
|
||||
hostEl.innerHTML = compId ? innerRoot.innerHTML || "" : innerRoot.outerHTML || "";
|
||||
} else {
|
||||
for (const child of [...contentDoc.querySelectorAll("style, script")]) child.remove();
|
||||
hostEl.innerHTML = contentDoc.body.innerHTML || "";
|
||||
}
|
||||
hostEl.setAttribute("data-composition-file", src);
|
||||
hostEl.removeAttribute("data-composition-src");
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { rewriteAssetPath, rewriteCssAssetUrls } from "./rewriteSubCompPaths.js";
|
||||
import {
|
||||
rewriteAssetPath,
|
||||
rewriteCssAssetUrls,
|
||||
rewriteInlineStyleAssetUrls,
|
||||
} from "./rewriteSubCompPaths.js";
|
||||
|
||||
describe("rewriteAssetPath", () => {
|
||||
it("rewrites `../` against the sub-composition dir", () => {
|
||||
@@ -36,4 +40,19 @@ describe("rewriteAssetPath", () => {
|
||||
expect(out).not.toMatch(/\\/);
|
||||
expect(out).not.toMatch(/:\\/);
|
||||
});
|
||||
|
||||
it("rewrites CSS urls inside inline style attributes", () => {
|
||||
const elements = [{ style: `background-image: url("../cover.png")` }];
|
||||
|
||||
rewriteInlineStyleAssetUrls(
|
||||
elements,
|
||||
"compositions/scene.html",
|
||||
(el) => el.style,
|
||||
(el, value) => {
|
||||
el.style = value;
|
||||
},
|
||||
);
|
||||
|
||||
expect(elements[0]?.style).toBe(`background-image: url("cover.png")`);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -96,6 +96,28 @@ export function rewriteAssetPaths<T>(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Rewrite CSS url(...) references inside inline style attributes.
|
||||
*/
|
||||
export function rewriteInlineStyleAssetUrls<T>(
|
||||
elements: Iterable<T>,
|
||||
compSrcPath: string,
|
||||
getStyle: (el: T) => string | null | undefined,
|
||||
setStyle: (el: T, value: string) => void,
|
||||
): void {
|
||||
const compDir = dirname(compSrcPath);
|
||||
if (!compDir || compDir === ".") return;
|
||||
|
||||
for (const el of elements) {
|
||||
const style = getStyle(el);
|
||||
if (!style) continue;
|
||||
const rewritten = rewriteCssAssetUrls(style, compSrcPath);
|
||||
if (rewritten !== style) {
|
||||
setStyle(el, rewritten);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Rewrite CSS url(...) references in a sub-composition's inline styles so
|
||||
* ../foo.woff2 remains valid after the CSS is hoisted into the root document.
|
||||
|
||||
@@ -50,6 +50,28 @@ describe("caption rules", () => {
|
||||
expect(finding).toBeUndefined();
|
||||
});
|
||||
|
||||
it("does not warn for generic GSAP opacity exits in non-caption loops", () => {
|
||||
const html = `
|
||||
<html><body>
|
||||
<div data-composition-id="main" data-width="1920" data-height="1080">
|
||||
<script>
|
||||
window.__timelines = window.__timelines || {};
|
||||
var tl = gsap.timeline({ paused: true });
|
||||
var sceneCaption = document.querySelector("#scene-caption");
|
||||
CARDS.forEach(function(group, gi) {
|
||||
var groupEl = document.createElement("div");
|
||||
groupEl.id = "card-" + gi;
|
||||
tl.to(groupEl, { opacity: 0, duration: 0.12 }, 2);
|
||||
});
|
||||
window.__timelines["main"] = tl;
|
||||
</script>
|
||||
</div>
|
||||
</body></html>`;
|
||||
const result = lintHyperframeHtml(html);
|
||||
const finding = result.findings.find((f) => f.code === "caption_exit_missing_hard_kill");
|
||||
expect(finding).toBeUndefined();
|
||||
});
|
||||
|
||||
it("warns when caption group has nowrap without max-width", () => {
|
||||
const html = `
|
||||
<html><body>
|
||||
|
||||
@@ -12,7 +12,8 @@ export const captionRules: Array<(ctx: LintContext) => HyperframeLintFinding[]>
|
||||
content,
|
||||
);
|
||||
const hasCaptionLoop =
|
||||
/forEach|\.forEach\s*\(/.test(content) && /createElement|caption|group|cg-/.test(content);
|
||||
/forEach|\.forEach\s*\(/.test(content) &&
|
||||
/karaoke|caption[-_]?(?:group|word|line|block)|cg-/.test(content);
|
||||
if (hasCaptionLoop && hasExitTween && !hasHardKill) {
|
||||
findings.push({
|
||||
code: "caption_exit_missing_hard_kill",
|
||||
|
||||
@@ -197,6 +197,21 @@ describe("parseHtml", () => {
|
||||
expect(result.resolution).toBe("portrait");
|
||||
});
|
||||
|
||||
it("keeps explicit portrait resolution even when dimensions are square", () => {
|
||||
const html = `
|
||||
<html data-resolution="portrait" data-composition-width="1080" data-composition-height="1080">
|
||||
<body>
|
||||
<div id="stage">
|
||||
<div id="text1" data-start="0" data-end="5"><div>Hello</div></div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
`;
|
||||
const result = parseHtml(html);
|
||||
|
||||
expect(result.resolution).toBe("portrait");
|
||||
});
|
||||
|
||||
it("defaults to portrait when no resolution info is available", () => {
|
||||
const html = `
|
||||
<html>
|
||||
@@ -290,7 +305,7 @@ describe("parseHtml", () => {
|
||||
expect(result.resolution).toBe("square");
|
||||
});
|
||||
|
||||
it("infers square-4k from equal width/height ≥ 2160", () => {
|
||||
it("infers square-4k from equal width/height >= 2160", () => {
|
||||
const html = `
|
||||
<html data-composition-width="2160" data-composition-height="2160">
|
||||
<body>
|
||||
|
||||
@@ -147,8 +147,8 @@ function parseResolutionFromHtml(doc: Document): CanvasResolution | null {
|
||||
function resolveResolutionFromDimensions(width: number, height: number): CanvasResolution {
|
||||
const longSide = Math.max(width, height);
|
||||
// UHD cutoff is the long side of the 4K presets (3840 for `landscape-4k` /
|
||||
// `portrait-4k`, 2160 for `square-4k`). A looser threshold (e.g. ≥ 2560)
|
||||
// would silently misclassify QHD/1440p (2560×1440) as 4K, which is the
|
||||
// `portrait-4k`, 2160 for `square-4k`). A looser threshold (e.g. >= 2560)
|
||||
// would silently misclassify QHD/1440p (2560x1440) as 4K, which is the
|
||||
// wrong default for a common authoring resolution closer to 1080p than to
|
||||
// UHD. Authors who genuinely want the 4K preset can still set
|
||||
// `data-resolution="..."` explicitly.
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { initSandboxRuntimeModular } from "./init";
|
||||
import type { RuntimeTimelineLike } from "./types";
|
||||
|
||||
@@ -45,6 +45,31 @@ function createPaddableMockTimeline(duration: number): RuntimeTimelineLike {
|
||||
return timeline;
|
||||
}
|
||||
|
||||
function createManualRaf() {
|
||||
let now = 0;
|
||||
let nextId = 0;
|
||||
const callbacks = new Map<number, FrameRequestCallback>();
|
||||
return {
|
||||
requestAnimationFrame: (callback: FrameRequestCallback) => {
|
||||
nextId += 1;
|
||||
callbacks.set(nextId, callback);
|
||||
return nextId;
|
||||
},
|
||||
cancelAnimationFrame: (id: number) => {
|
||||
callbacks.delete(id);
|
||||
},
|
||||
step: (milliseconds: number) => {
|
||||
now += milliseconds;
|
||||
const pending = Array.from(callbacks.entries());
|
||||
callbacks.clear();
|
||||
for (const [, callback] of pending) {
|
||||
callback(now);
|
||||
}
|
||||
},
|
||||
now: () => now,
|
||||
};
|
||||
}
|
||||
|
||||
describe("initSandboxRuntimeModular", () => {
|
||||
const originalRequestAnimationFrame = window.requestAnimationFrame;
|
||||
const originalCancelAnimationFrame = window.cancelAnimationFrame;
|
||||
@@ -67,6 +92,7 @@ describe("initSandboxRuntimeModular", () => {
|
||||
delete (window as Window & { __player?: unknown }).__player;
|
||||
delete (window as Window & { __playerReady?: boolean }).__playerReady;
|
||||
delete (window as Window & { __renderReady?: boolean }).__renderReady;
|
||||
vi.restoreAllMocks();
|
||||
window.requestAnimationFrame = originalRequestAnimationFrame;
|
||||
window.cancelAnimationFrame = originalCancelAnimationFrame;
|
||||
});
|
||||
@@ -283,32 +309,67 @@ describe("initSandboxRuntimeModular", () => {
|
||||
expect(video.currentTime).toBe(0);
|
||||
});
|
||||
|
||||
it("allows external code to reassign delegated __player methods", () => {
|
||||
it("plays scheduled child timelines without a captured root timeline when audio has failed", () => {
|
||||
const raf = createManualRaf();
|
||||
vi.spyOn(performance, "now").mockImplementation(() => raf.now());
|
||||
window.requestAnimationFrame = raf.requestAnimationFrame as typeof window.requestAnimationFrame;
|
||||
window.cancelAnimationFrame = raf.cancelAnimationFrame as typeof window.cancelAnimationFrame;
|
||||
|
||||
const root = document.createElement("div");
|
||||
root.setAttribute("data-composition-id", "main");
|
||||
root.setAttribute("data-root", "true");
|
||||
root.setAttribute("data-start", "0");
|
||||
root.setAttribute("data-duration", "4");
|
||||
root.setAttribute("data-width", "1920");
|
||||
root.setAttribute("data-height", "1080");
|
||||
document.body.appendChild(root);
|
||||
|
||||
const child = document.createElement("div");
|
||||
child.setAttribute("data-composition-id", "scene");
|
||||
child.setAttribute("data-start", "0");
|
||||
child.setAttribute("data-duration", "4");
|
||||
root.appendChild(child);
|
||||
|
||||
const audio = document.createElement("audio");
|
||||
audio.setAttribute("data-start", "0");
|
||||
audio.setAttribute("data-duration", "4");
|
||||
Object.defineProperty(audio, "error", {
|
||||
value: { code: 4, message: "format error" },
|
||||
configurable: true,
|
||||
});
|
||||
Object.defineProperty(audio, "networkState", {
|
||||
value: HTMLMediaElement.NETWORK_NO_SOURCE,
|
||||
configurable: true,
|
||||
});
|
||||
Object.defineProperty(audio, "readyState", {
|
||||
value: HTMLMediaElement.HAVE_NOTHING,
|
||||
configurable: true,
|
||||
});
|
||||
Object.defineProperty(audio, "paused", { value: true, configurable: true });
|
||||
Object.defineProperty(audio, "currentTime", { value: 0, writable: true, configurable: true });
|
||||
audio.load = () => {};
|
||||
audio.play = vi.fn(() => Promise.reject(new Error("format error")));
|
||||
root.appendChild(audio);
|
||||
|
||||
const childTimeline = createMockTimeline(4);
|
||||
(window as Window & { __timelines?: Record<string, RuntimeTimelineLike> }).__timelines = {
|
||||
main: createMockTimeline(10),
|
||||
scene: childTimeline,
|
||||
};
|
||||
|
||||
initSandboxRuntimeModular();
|
||||
|
||||
const player = (
|
||||
window as Window & {
|
||||
__player?: { renderSeek: (timeSeconds: number) => void };
|
||||
__player?: { play: () => void; getTime: () => number; isPlaying: () => boolean };
|
||||
}
|
||||
).__player;
|
||||
expect(player).toBeDefined();
|
||||
if (!player) return;
|
||||
|
||||
const original = player.renderSeek;
|
||||
expect(() => {
|
||||
player.renderSeek = (t: number) => original(t);
|
||||
}).not.toThrow();
|
||||
player?.play();
|
||||
raf.step(1_000);
|
||||
|
||||
expect(player?.isPlaying()).toBe(true);
|
||||
expect(player?.getTime()).toBeCloseTo(1, 1);
|
||||
expect(childTimeline.time()).toBeCloseTo(1, 1);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -7,7 +7,6 @@ import { createLottieAdapter } from "./adapters/lottie";
|
||||
import { createThreeAdapter } from "./adapters/three";
|
||||
import { createWaapiAdapter } from "./adapters/waapi";
|
||||
import { refreshRuntimeMediaCache, syncRuntimeMedia } from "./media";
|
||||
import { createMediaPreloadManager } from "./mediaPreloader";
|
||||
import { createPickerModule } from "./picker";
|
||||
import { createRuntimePlayer } from "./player";
|
||||
import { createRuntimeState } from "./state";
|
||||
@@ -933,6 +932,15 @@ export function initSandboxRuntimeModular(): void {
|
||||
if (typeof state.capturedTimeline.timeScale === "function") {
|
||||
state.capturedTimeline.timeScale(state.playbackRate);
|
||||
}
|
||||
const boundDuration = getSafeTimelineDurationSeconds(state.capturedTimeline, 0);
|
||||
if (boundDuration > 0) {
|
||||
try {
|
||||
clock.setDuration(boundDuration);
|
||||
} catch {
|
||||
// clock not yet initialized — duration will be set during TransportClock setup
|
||||
}
|
||||
state.capturedTimeline.pause();
|
||||
}
|
||||
if (resolution.diagnostics) {
|
||||
postRuntimeMessage({
|
||||
source: "hf-preview",
|
||||
@@ -1223,61 +1231,24 @@ export function initSandboxRuntimeModular(): void {
|
||||
metadataBoundMedia.clear();
|
||||
};
|
||||
|
||||
const isRenderMode = Boolean((window as Record<string, unknown>).__HF_EXPORT_RENDER_SEEK_CONFIG);
|
||||
const mediaPreloader = createMediaPreloadManager({
|
||||
onActivation: (clipCount) => {
|
||||
postRuntimeDiagnosticOnce("lazy_preload_activated", { clipCount }, "lazy_preload_activated");
|
||||
},
|
||||
});
|
||||
|
||||
const bindMediaMetadataListeners = () => {
|
||||
if (state.tornDown) return;
|
||||
const mediaEls = Array.from(document.querySelectorAll("video, audio")) as HTMLMediaElement[];
|
||||
const isLazy = mediaPreloader.isLazy();
|
||||
|
||||
let newElementsBound = false;
|
||||
for (const mediaEl of mediaEls) {
|
||||
if (metadataBoundMedia.has(mediaEl)) continue;
|
||||
metadataBoundMedia.add(mediaEl);
|
||||
newElementsBound = true;
|
||||
mediaEl.addEventListener("loadedmetadata", scheduleMetadataDurationHydration);
|
||||
mediaEl.addEventListener("durationchange", scheduleMetadataDurationHydration);
|
||||
|
||||
// In eager mode, preload inline (same ordering as before lazy preloading)
|
||||
if (!isLazy || isRenderMode) {
|
||||
if (mediaEl.preload !== "auto") mediaEl.preload = "auto";
|
||||
if (mediaEl.readyState < HTMLMediaElement.HAVE_FUTURE_DATA) mediaEl.load();
|
||||
// Eagerly preload media data so audio/video is buffered before the user
|
||||
// clicks play. Without this, the first play() call fires on un-fetched
|
||||
// media, producing silence or choppy audio until the browser caches it.
|
||||
if (mediaEl.preload !== "auto") {
|
||||
mediaEl.preload = "auto";
|
||||
}
|
||||
}
|
||||
|
||||
if (newElementsBound && !isRenderMode) {
|
||||
mediaPreloader.refresh();
|
||||
}
|
||||
|
||||
// Lazy-mode demotion runs separately after refresh updates the clip list
|
||||
if (mediaPreloader.isLazy() && !isRenderMode) {
|
||||
// Only demote timed media (elements with data-start) to metadata preload.
|
||||
// Untimed media (background audio, ambient loops, decorative video) must
|
||||
// keep their original preload state — the mediaPreloader only manages
|
||||
// timed clips and would never promote them back.
|
||||
for (const mediaEl of mediaEls) {
|
||||
if (!mediaEl.hasAttribute("data-start")) continue;
|
||||
// Power-user opt-out: data-preload-eager keeps a clip eagerly buffered
|
||||
// even under lazy mode, useful when a specific clip must be instantly
|
||||
// available regardless of playhead proximity.
|
||||
if (mediaEl.hasAttribute("data-preload-eager")) continue;
|
||||
if (mediaEl.preload === "auto" || mediaEl.preload === "") {
|
||||
mediaEl.preload = "metadata";
|
||||
// Kick off the metadata fetch explicitly — some browsers (Chrome Lite
|
||||
// mode, Firefox with media.preload.default=0) won't fetch metadata
|
||||
// until load() is called, and timeline duration depends on el.duration.
|
||||
mediaEl.load();
|
||||
}
|
||||
if (mediaEl.readyState < HTMLMediaElement.HAVE_METADATA) {
|
||||
mediaEl.load();
|
||||
}
|
||||
if (mediaEl.readyState < HTMLMediaElement.HAVE_FUTURE_DATA) {
|
||||
mediaEl.load();
|
||||
}
|
||||
mediaPreloader.preloadAroundTime(Math.max(0, state.currentTime || 0));
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1333,7 +1304,7 @@ export function initSandboxRuntimeModular(): void {
|
||||
timeSeconds: state.currentTime,
|
||||
playing: state.isPlaying,
|
||||
playbackRate: state.playbackRate,
|
||||
outputMuted: state.mediaOutputMuted,
|
||||
outputMuted: state.mediaOutputMuted || webAudio.isActive(),
|
||||
userMuted: state.bridgeMuted,
|
||||
userVolume: state.bridgeVolume,
|
||||
forceSync,
|
||||
@@ -1488,6 +1459,7 @@ export function initSandboxRuntimeModular(): void {
|
||||
.then(() => loadInlineTemplateCompositions(compositionLoaderParams))
|
||||
.finally(() => {
|
||||
externalCompositionsReady = true;
|
||||
bindRootTimelineIfAvailable();
|
||||
runAdapters("discover", state.currentTime);
|
||||
bindMediaMetadataListeners();
|
||||
installAssetFailureDiagnostics();
|
||||
@@ -1627,7 +1599,6 @@ export function initSandboxRuntimeModular(): void {
|
||||
onSetPlaybackRate: (rate) => {
|
||||
applyPlaybackRate(rate);
|
||||
if (state.transportClock) state.transportClock.setRate(state.playbackRate);
|
||||
webAudio.setRate(state.playbackRate);
|
||||
},
|
||||
onEnablePickMode: () => picker.enablePickMode(),
|
||||
onDisablePickMode: () => picker.disablePickMode(),
|
||||
@@ -1683,6 +1654,49 @@ export function initSandboxRuntimeModular(): void {
|
||||
let transportTickCount = 0;
|
||||
let inTransportTick = false;
|
||||
|
||||
const seekRuntimeTimeline = (
|
||||
timeline: RuntimeTimelineLike,
|
||||
timeSeconds: number,
|
||||
swallowLabel: string,
|
||||
) => {
|
||||
try {
|
||||
timeline.pause();
|
||||
if (typeof timeline.totalTime === "function") {
|
||||
timeline.totalTime(timeSeconds, false);
|
||||
} else {
|
||||
timeline.seek(timeSeconds, false);
|
||||
}
|
||||
} catch (err) {
|
||||
swallow(swallowLabel, err);
|
||||
}
|
||||
};
|
||||
|
||||
const seekStandaloneRegisteredTimelines = (timeSeconds: number) => {
|
||||
const timelines = (window.__timelines ?? {}) as Record<string, RuntimeTimelineLike | undefined>;
|
||||
const rootCompositionId =
|
||||
resolveRootCompositionElement()?.getAttribute("data-composition-id") ?? null;
|
||||
for (const [compositionId, timeline] of Object.entries(timelines)) {
|
||||
if (!timeline || compositionId === rootCompositionId) continue;
|
||||
const node = document.querySelector(`[data-composition-id="${CSS.escape(compositionId)}"]`);
|
||||
if (!node) continue;
|
||||
const start = resolveStartForElement(node, 0);
|
||||
if (!Number.isFinite(start)) continue;
|
||||
const authoredDuration = resolveDurationForElement(node, {
|
||||
includeAuthoredTimingAttrs: true,
|
||||
});
|
||||
const timelineDuration = getTimelineDurationSeconds(timeline);
|
||||
const duration =
|
||||
authoredDuration != null && authoredDuration > 0 ? authoredDuration : timelineDuration;
|
||||
const localTime = Math.max(
|
||||
0,
|
||||
duration != null && duration > 0
|
||||
? Math.min(duration, timeSeconds - start)
|
||||
: timeSeconds - start,
|
||||
);
|
||||
seekRuntimeTimeline(timeline, localTime, "runtime.init.transport.childTimeline");
|
||||
}
|
||||
};
|
||||
|
||||
const seekTimelineAndAdapters = (t: number) => {
|
||||
const tl = state.capturedTimeline;
|
||||
if (tl) {
|
||||
@@ -1702,6 +1716,8 @@ export function initSandboxRuntimeModular(): void {
|
||||
// at absolute `t` would clobber their offset-relative position.
|
||||
// Play/pause propagation for siblings happens in the player.play()
|
||||
// and player.pause() overrides via the adapter layer.
|
||||
} else {
|
||||
seekStandaloneRegisteredTimelines(t);
|
||||
}
|
||||
for (const adapter of state.deterministicAdapters) {
|
||||
try {
|
||||
@@ -1780,7 +1796,7 @@ export function initSandboxRuntimeModular(): void {
|
||||
if (!rawEl.paused) {
|
||||
clock.attachAudioSource({ el: rawEl, compositionStart: start, mediaStart });
|
||||
foundActive = true;
|
||||
} else if (rawEl.readyState < HTMLMediaElement.HAVE_FUTURE_DATA) {
|
||||
} else if (!rawEl.error && rawEl.readyState < HTMLMediaElement.HAVE_FUTURE_DATA) {
|
||||
// Audio is buffering — freeze visuals at last known position
|
||||
// instead of falling through to monotonic (which runs ahead).
|
||||
clock.attachAudioSource({ currentTimeSeconds: state.currentTime });
|
||||
@@ -1825,9 +1841,6 @@ export function initSandboxRuntimeModular(): void {
|
||||
|
||||
if (clock.isPlaying()) {
|
||||
syncMediaForCurrentState();
|
||||
if (mediaPreloader.isLazy() && transportTickCount % 10 === 0) {
|
||||
mediaPreloader.sync(Math.max(0, state.currentTime || 0));
|
||||
}
|
||||
}
|
||||
postState(false);
|
||||
} finally {
|
||||
@@ -1861,8 +1874,7 @@ export function initSandboxRuntimeModular(): void {
|
||||
// Player methods route through the TransportClock.
|
||||
player.play = () => {
|
||||
const tl = state.capturedTimeline;
|
||||
if (!tl || clock.isPlaying()) return;
|
||||
mediaPreloader.preloadAroundTime(Math.max(0, state.currentTime || 0));
|
||||
if (clock.isPlaying()) return;
|
||||
const dur = getSafeTimelineDurationSeconds(tl, 0);
|
||||
if (dur > 0) {
|
||||
clock.setDuration(dur);
|
||||
@@ -1871,8 +1883,12 @@ export function initSandboxRuntimeModular(): void {
|
||||
state.currentTime = 0;
|
||||
seekTimelineAndAdapters(0);
|
||||
}
|
||||
} else {
|
||||
const rootEl = resolveRootCompositionElement();
|
||||
const declaredDur = Number(rootEl?.getAttribute("data-duration") ?? 0);
|
||||
if (declaredDur > 0) clock.setDuration(declaredDur);
|
||||
}
|
||||
tl.pause();
|
||||
if (tl) tl.pause();
|
||||
if (!clock.play()) return;
|
||||
state.isPlaying = true;
|
||||
state.mediaForceSyncNextTick = true;
|
||||
@@ -1901,7 +1917,6 @@ export function initSandboxRuntimeModular(): void {
|
||||
clock.now(),
|
||||
vol * state.bridgeVolume,
|
||||
gen,
|
||||
state.playbackRate,
|
||||
);
|
||||
});
|
||||
}
|
||||
@@ -1932,7 +1947,6 @@ export function initSandboxRuntimeModular(): void {
|
||||
Math.max(0, Number(timeSeconds) || 0),
|
||||
state.canonicalFps,
|
||||
);
|
||||
mediaPreloader.preloadAroundTime(quantized);
|
||||
webAudio.stopAll();
|
||||
clock.detachAudioSource();
|
||||
const wasPlaying = clock.isPlaying();
|
||||
|
||||
@@ -198,13 +198,26 @@ export function syncRuntimeMedia(params: {
|
||||
const offsetJumped = !firstTickOfClip && Math.abs(offset - prevOffset!) > 0.5;
|
||||
const catastrophicDrift = drift > 3;
|
||||
const hardSync = drift > 0.5 && (firstTickOfClip || offsetJumped || catastrophicDrift);
|
||||
// Playing video elements use the browser's native decoder pipeline for
|
||||
// timing. Seeking a playing video resets the decoder, causing a ~150ms
|
||||
// freeze while it re-buffers — during which the monotonic clock advances,
|
||||
// creating a perpetual seek→freeze→drift→seek stutter loop. Skip strict
|
||||
// and force sync for playing videos; only hard sync (>0.5s) warrants
|
||||
// the decoder-reset cost.
|
||||
const isPlayingVideo = el.tagName === "VIDEO" && !el.paused;
|
||||
// Only apply strict sync when offset has stabilized (not growing).
|
||||
// During initial buffering, offset grows ~16ms/tick as the timeline
|
||||
// advances while media stays at 0. Accumulated drift from pause/play
|
||||
// toggling shows up as a stable, non-zero offset (delta near 0).
|
||||
const offsetStabilized = prevOffset !== undefined && Math.abs(offset - prevOffset) < 0.004;
|
||||
let strictSync = false;
|
||||
if (!hardSync && !firstTickOfClip && offsetStabilized && drift > STRICT_DRIFT_THRESHOLD) {
|
||||
if (
|
||||
!isPlayingVideo &&
|
||||
!hardSync &&
|
||||
!firstTickOfClip &&
|
||||
offsetStabilized &&
|
||||
drift > STRICT_DRIFT_THRESHOLD
|
||||
) {
|
||||
const samples = (strictDriftSamples.get(el) ?? 0) + 1;
|
||||
strictDriftSamples.set(el, samples);
|
||||
if (samples >= STRICT_REQUIRED_SAMPLES) {
|
||||
@@ -214,7 +227,8 @@ export function syncRuntimeMedia(params: {
|
||||
} else if (drift <= STRICT_DRIFT_THRESHOLD) {
|
||||
strictDriftSamples.set(el, 0);
|
||||
}
|
||||
if (hardSync || strictSync || (params.forceSync && drift > 0.02)) {
|
||||
const forceSync = !isPlayingVideo && params.forceSync && drift > 0.02;
|
||||
if (hardSync || strictSync || forceSync) {
|
||||
try {
|
||||
el.currentTime = relTime;
|
||||
} catch (err) {
|
||||
|
||||
@@ -58,12 +58,28 @@ export function createPickerModule(deps: PickerModuleDeps): PickerModule {
|
||||
});
|
||||
}
|
||||
|
||||
function isEffectivelyHidden(el: HTMLElement): boolean {
|
||||
const win = el.ownerDocument.defaultView;
|
||||
if (!win) return false;
|
||||
let current: HTMLElement | null = el;
|
||||
while (current && current !== document.body && current !== document.documentElement) {
|
||||
const computed = win.getComputedStyle(current);
|
||||
if (computed.display === "none" || computed.visibility === "hidden") return true;
|
||||
if (computed.pointerEvents === "none") return true;
|
||||
const opacity = Number.parseFloat(computed.opacity);
|
||||
if (Number.isFinite(opacity) && opacity <= 0.01) return true;
|
||||
current = current.parentElement;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function isPickableElement(el: Element | null): el is Element {
|
||||
if (!el || el === document.body || el === document.documentElement) return false;
|
||||
const tag = el.tagName.toLowerCase();
|
||||
if (tag === "script" || tag === "style" || tag === "link" || tag === "meta") return false;
|
||||
if (el.classList.contains("__hf-pick-highlight")) return false;
|
||||
if (el.closest(PICKER_IGNORE_SELECTOR)) return false;
|
||||
if (isEffectivelyHidden(el as HTMLElement)) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ import { registerLintRoutes } from "./routes/lint.js";
|
||||
import { registerRenderRoutes } from "./routes/render.js";
|
||||
import { registerThumbnailRoutes } from "./routes/thumbnail.js";
|
||||
import { registerWaveformRoutes } from "./routes/waveform.js";
|
||||
import { registerFontRoutes } from "./routes/fonts.js";
|
||||
|
||||
/**
|
||||
* Create a Hono sub-app with all studio API routes.
|
||||
@@ -24,6 +25,7 @@ export function createStudioApi(adapter: StudioApiAdapter): Hono {
|
||||
registerRenderRoutes(api, adapter);
|
||||
registerThumbnailRoutes(api, adapter);
|
||||
registerWaveformRoutes(api, adapter);
|
||||
registerFontRoutes(api);
|
||||
|
||||
return api;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,382 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { Window } from "happy-dom";
|
||||
import { createStudioManualEditsRenderBodyScript } from "./manualEditsRenderScript";
|
||||
|
||||
function runScript(
|
||||
window: Window,
|
||||
script: string,
|
||||
getComputedStyle: typeof window.getComputedStyle = window.getComputedStyle.bind(window),
|
||||
timers: {
|
||||
setInterval?: typeof globalThis.setInterval;
|
||||
clearInterval?: typeof globalThis.clearInterval;
|
||||
} = {},
|
||||
): void {
|
||||
const execute = new Function(
|
||||
"window",
|
||||
"document",
|
||||
"HTMLElement",
|
||||
"getComputedStyle",
|
||||
"setInterval",
|
||||
"clearInterval",
|
||||
script,
|
||||
);
|
||||
execute(
|
||||
window,
|
||||
window.document,
|
||||
window.HTMLElement,
|
||||
getComputedStyle,
|
||||
timers.setInterval ??
|
||||
(((callback: TimerHandler) => {
|
||||
void callback;
|
||||
return 0 as never;
|
||||
}) as typeof globalThis.setInterval),
|
||||
timers.clearInterval ?? globalThis.clearInterval,
|
||||
);
|
||||
}
|
||||
|
||||
describe("createStudioManualEditsRenderBodyScript", () => {
|
||||
it("returns null for an empty manifest", () => {
|
||||
expect(createStudioManualEditsRenderBodyScript("")).toBeNull();
|
||||
});
|
||||
|
||||
it("applies manual edits and reapplies them after render seeks", () => {
|
||||
const window = new Window();
|
||||
window.document.body.innerHTML = '<div id="card" style="width: 20px; height: 20px"></div>';
|
||||
const card = window.document.getElementById("card");
|
||||
if (!(card instanceof window.HTMLElement)) {
|
||||
throw new Error("card fixture missing");
|
||||
}
|
||||
|
||||
let seekCalls = 0;
|
||||
(
|
||||
window as unknown as {
|
||||
__hf: { seek: (time: number) => void };
|
||||
}
|
||||
).__hf = {
|
||||
seek: () => {
|
||||
seekCalls += 1;
|
||||
card.style.removeProperty("translate");
|
||||
},
|
||||
};
|
||||
|
||||
const script = createStudioManualEditsRenderBodyScript(
|
||||
JSON.stringify({
|
||||
version: 1,
|
||||
edits: [
|
||||
{
|
||||
kind: "path-offset",
|
||||
target: { sourceFile: "index.html", id: "card" },
|
||||
x: 12,
|
||||
y: 24,
|
||||
},
|
||||
{
|
||||
kind: "box-size",
|
||||
target: { sourceFile: "index.html", id: "card" },
|
||||
width: 120,
|
||||
height: 64,
|
||||
},
|
||||
{
|
||||
kind: "rotation",
|
||||
target: { sourceFile: "index.html", id: "card" },
|
||||
angle: 15,
|
||||
},
|
||||
],
|
||||
}),
|
||||
);
|
||||
if (!script) throw new Error("script fixture missing");
|
||||
|
||||
const computedStyle = (element: Element) =>
|
||||
({
|
||||
display: element === card ? "block" : "block",
|
||||
flexDirection: "row",
|
||||
}) as CSSStyleDeclaration;
|
||||
|
||||
const intervalCallbacks: Array<() => void> = [];
|
||||
runScript(window, script, computedStyle, {
|
||||
setInterval: ((callback: TimerHandler) => {
|
||||
if (typeof callback === "function") intervalCallbacks.push(callback as () => void);
|
||||
return 0 as never;
|
||||
}) as typeof globalThis.setInterval,
|
||||
});
|
||||
|
||||
expect(card.style.getPropertyValue("translate")).toContain("--hf-studio-offset-x");
|
||||
expect(card.style.getPropertyValue("width")).toBe("120px");
|
||||
expect(card.style.getPropertyValue("height")).toBe("64px");
|
||||
expect(card.style.getPropertyValue("rotate")).toContain("--hf-studio-rotation");
|
||||
expect(card.style.getPropertyValue("transform-origin")).toBe("center center");
|
||||
|
||||
(
|
||||
window as unknown as {
|
||||
__hf: { seek: (time: number) => void };
|
||||
}
|
||||
).__hf.seek(1);
|
||||
|
||||
expect(seekCalls).toBe(1);
|
||||
expect(card.style.getPropertyValue("translate")).toContain("--hf-studio-offset-x");
|
||||
|
||||
(
|
||||
window as unknown as {
|
||||
__hf: { seek: (time: number) => void };
|
||||
}
|
||||
).__hf.seek = () => {
|
||||
card.style.removeProperty("rotate");
|
||||
};
|
||||
intervalCallbacks.forEach((callback) => callback());
|
||||
(
|
||||
window as unknown as {
|
||||
__hf: { seek: (time: number) => void };
|
||||
}
|
||||
).__hf.seek(2);
|
||||
expect(card.style.getPropertyValue("rotate")).toContain("--hf-studio-rotation");
|
||||
|
||||
(
|
||||
window as unknown as {
|
||||
__player: { renderSeek: (time: number) => void };
|
||||
}
|
||||
).__player = {
|
||||
renderSeek: () => {
|
||||
card.style.removeProperty("rotate");
|
||||
},
|
||||
};
|
||||
intervalCallbacks.forEach((callback) => callback());
|
||||
(
|
||||
window as unknown as {
|
||||
__player: { renderSeek: (time: number) => void };
|
||||
}
|
||||
).__player.renderSeek(3);
|
||||
expect(card.style.getPropertyValue("rotate")).toContain("--hf-studio-rotation");
|
||||
});
|
||||
|
||||
it("applies render edits to the matching source file target", () => {
|
||||
const window = new Window();
|
||||
window.document.body.innerHTML = `
|
||||
<div data-composition-id="root">
|
||||
<div id="card"></div>
|
||||
<div data-composition-id="nested" data-composition-file="scenes/nested.html">
|
||||
<div id="card"></div>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
const cards = Array.from(window.document.getElementsByTagName("*")).filter(
|
||||
(element): element is HTMLElement =>
|
||||
element instanceof window.HTMLElement && element.id === "card",
|
||||
);
|
||||
const rootCard = cards[0];
|
||||
const nestedCard = cards[1];
|
||||
if (!rootCard || !nestedCard) {
|
||||
throw new Error("source-scoped render fixture missing");
|
||||
}
|
||||
|
||||
const script = createStudioManualEditsRenderBodyScript(
|
||||
JSON.stringify({
|
||||
version: 1,
|
||||
edits: [
|
||||
{
|
||||
kind: "rotation",
|
||||
target: { sourceFile: "scenes/nested.html", id: "card" },
|
||||
angle: 21,
|
||||
},
|
||||
],
|
||||
}),
|
||||
);
|
||||
if (!script) throw new Error("script fixture missing");
|
||||
|
||||
runScript(window, script);
|
||||
|
||||
expect(rootCard.style.getPropertyValue("rotate")).toBe("");
|
||||
expect(nestedCard.style.getPropertyValue("rotate")).toContain("--hf-studio-rotation");
|
||||
});
|
||||
|
||||
it("applies render edits inside composition-file hosts without composition ids", () => {
|
||||
const window = new Window();
|
||||
window.document.body.innerHTML = `
|
||||
<div data-composition-id="root">
|
||||
<div id="card"></div>
|
||||
<div data-composition-file="scenes/anonymous.html">
|
||||
<div id="card"></div>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
const cards = Array.from(window.document.getElementsByTagName("*")).filter(
|
||||
(element): element is HTMLElement =>
|
||||
element instanceof window.HTMLElement && element.id === "card",
|
||||
);
|
||||
const rootCard = cards[0];
|
||||
const nestedCard = cards[1];
|
||||
if (!rootCard || !nestedCard) {
|
||||
throw new Error("anonymous composition render fixture missing");
|
||||
}
|
||||
|
||||
const script = createStudioManualEditsRenderBodyScript(
|
||||
JSON.stringify({
|
||||
version: 1,
|
||||
edits: [
|
||||
{
|
||||
kind: "path-offset",
|
||||
target: { sourceFile: "scenes/anonymous.html", id: "card" },
|
||||
x: 12,
|
||||
y: 24,
|
||||
},
|
||||
],
|
||||
}),
|
||||
);
|
||||
if (!script) throw new Error("script fixture missing");
|
||||
|
||||
runScript(window, script);
|
||||
|
||||
expect(rootCard.style.getPropertyValue("translate")).toBe("");
|
||||
expect(nestedCard.style.getPropertyValue("translate")).toContain("--hf-studio-offset-x");
|
||||
});
|
||||
|
||||
it("uses the active composition path as the unscoped document fallback", () => {
|
||||
const window = new Window();
|
||||
window.document.body.innerHTML = `<div id="card"></div>`;
|
||||
const card = window.document.getElementById("card");
|
||||
if (!(card instanceof window.HTMLElement)) {
|
||||
throw new Error("card fixture missing");
|
||||
}
|
||||
|
||||
const script = createStudioManualEditsRenderBodyScript(
|
||||
JSON.stringify({
|
||||
version: 1,
|
||||
edits: [
|
||||
{
|
||||
kind: "path-offset",
|
||||
target: { sourceFile: "compositions/scene-2.html", id: "card" },
|
||||
x: 12,
|
||||
y: 24,
|
||||
},
|
||||
],
|
||||
}),
|
||||
{ activeCompositionPath: "compositions/scene-2.html" },
|
||||
);
|
||||
if (!script) throw new Error("script fixture missing");
|
||||
|
||||
runScript(window, script);
|
||||
|
||||
expect(card.style.getPropertyValue("translate")).toContain("--hf-studio-offset-x");
|
||||
});
|
||||
|
||||
it("preserves computed transform longhands as render edit bases", () => {
|
||||
const window = new Window();
|
||||
window.document.body.innerHTML = `<div id="card"></div>`;
|
||||
const card = window.document.getElementById("card");
|
||||
if (!(card instanceof window.HTMLElement)) {
|
||||
throw new Error("card fixture missing");
|
||||
}
|
||||
|
||||
const script = createStudioManualEditsRenderBodyScript(
|
||||
JSON.stringify({
|
||||
version: 1,
|
||||
edits: [
|
||||
{
|
||||
kind: "path-offset",
|
||||
target: { sourceFile: "index.html", id: "card" },
|
||||
x: 12,
|
||||
y: 24,
|
||||
},
|
||||
{
|
||||
kind: "rotation",
|
||||
target: { sourceFile: "index.html", id: "card" },
|
||||
angle: 15,
|
||||
},
|
||||
],
|
||||
}),
|
||||
);
|
||||
if (!script) throw new Error("script fixture missing");
|
||||
|
||||
const computedStyle = (element: Element) =>
|
||||
({
|
||||
getPropertyValue: (property: string) => {
|
||||
if (element !== card) return "";
|
||||
if (property === "translate") return "10px 20px";
|
||||
if (property === "rotate") return "8deg";
|
||||
return "";
|
||||
},
|
||||
}) as CSSStyleDeclaration;
|
||||
|
||||
runScript(window, script, computedStyle);
|
||||
|
||||
expect(card.style.getPropertyValue("translate")).toContain("calc(10px +");
|
||||
expect(card.style.getPropertyValue("translate")).toContain("calc(20px +");
|
||||
expect(card.style.getPropertyValue("rotate")).toContain("8deg");
|
||||
expect(card.style.getPropertyValue("rotate")).toContain("--hf-studio-rotation");
|
||||
expect(card.style.getPropertyValue("transform-origin")).toBe("center center");
|
||||
});
|
||||
|
||||
it("does not compound stale studio variables during render reapply", () => {
|
||||
const window = new Window();
|
||||
window.document.body.innerHTML = `
|
||||
<div id="card" style="
|
||||
translate: var(--hf-studio-offset-x, 0px) var(--hf-studio-offset-y, 0px);
|
||||
rotate: var(--hf-studio-rotation, 0deg);
|
||||
"></div>
|
||||
`;
|
||||
const card = window.document.getElementById("card");
|
||||
if (!(card instanceof window.HTMLElement)) {
|
||||
throw new Error("card fixture missing");
|
||||
}
|
||||
|
||||
const script = createStudioManualEditsRenderBodyScript(
|
||||
JSON.stringify({
|
||||
version: 1,
|
||||
edits: [
|
||||
{
|
||||
kind: "path-offset",
|
||||
target: { sourceFile: "index.html", id: "card" },
|
||||
x: 12,
|
||||
y: 24,
|
||||
},
|
||||
{
|
||||
kind: "rotation",
|
||||
target: { sourceFile: "index.html", id: "card" },
|
||||
angle: 15,
|
||||
},
|
||||
],
|
||||
}),
|
||||
);
|
||||
if (!script) throw new Error("script fixture missing");
|
||||
|
||||
runScript(window, script);
|
||||
|
||||
expect(card.style.getPropertyValue("translate")).toBe(
|
||||
"var(--hf-studio-offset-x, 0px) var(--hf-studio-offset-y, 0px)",
|
||||
);
|
||||
expect(card.style.getPropertyValue("rotate")).toBe("var(--hf-studio-rotation, 0deg)");
|
||||
});
|
||||
|
||||
it("exposes a render reapply hook for thumbnails after layout settles", () => {
|
||||
const window = new Window();
|
||||
window.document.body.innerHTML = `<div id="card"></div>`;
|
||||
const card = window.document.getElementById("card");
|
||||
if (!(card instanceof window.HTMLElement)) {
|
||||
throw new Error("card fixture missing");
|
||||
}
|
||||
|
||||
const script = createStudioManualEditsRenderBodyScript(
|
||||
JSON.stringify({
|
||||
version: 1,
|
||||
edits: [
|
||||
{
|
||||
kind: "path-offset",
|
||||
target: { sourceFile: "index.html", id: "card" },
|
||||
x: 12,
|
||||
y: 24,
|
||||
},
|
||||
],
|
||||
}),
|
||||
);
|
||||
if (!script) throw new Error("script fixture missing");
|
||||
|
||||
runScript(window, script);
|
||||
card.style.removeProperty("translate");
|
||||
|
||||
(
|
||||
window as unknown as {
|
||||
__hfStudioManualEditsApply?: () => number;
|
||||
}
|
||||
).__hfStudioManualEditsApply?.();
|
||||
|
||||
expect(card.style.getPropertyValue("translate")).toContain("--hf-studio-offset-x");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,371 @@
|
||||
export interface StudioManualEditsRenderScriptOptions {
|
||||
activeCompositionPath?: string | null;
|
||||
}
|
||||
|
||||
export const STUDIO_MANUAL_EDITS_PATH = ".hyperframes/studio-manual-edits.json";
|
||||
|
||||
export function createStudioManualEditsRenderBodyScript(
|
||||
manifestContent: string,
|
||||
options: StudioManualEditsRenderScriptOptions = {},
|
||||
): string | null {
|
||||
if (!manifestContent.trim()) return null;
|
||||
return `(${studioManualEditsRenderRuntime.toString()})(${JSON.stringify(manifestContent)}, ${JSON.stringify(options.activeCompositionPath ?? null)});`;
|
||||
}
|
||||
|
||||
function studioManualEditsRenderRuntime(
|
||||
manifestContent: string,
|
||||
activeCompositionPath: string | null,
|
||||
): void {
|
||||
const OFFSET_X_PROP = "--hf-studio-offset-x";
|
||||
const OFFSET_Y_PROP = "--hf-studio-offset-y";
|
||||
const WIDTH_PROP = "--hf-studio-width";
|
||||
const HEIGHT_PROP = "--hf-studio-height";
|
||||
const ROTATION_PROP = "--hf-studio-rotation";
|
||||
const PATH_OFFSET_ATTR = "data-hf-studio-path-offset";
|
||||
const BOX_SIZE_ATTR = "data-hf-studio-box-size";
|
||||
const ROTATION_ATTR = "data-hf-studio-rotation";
|
||||
const ORIGINAL_TRANSLATE_ATTR = "data-hf-studio-original-translate";
|
||||
const ORIGINAL_ROTATE_ATTR = "data-hf-studio-original-rotate";
|
||||
const WRAPPED_SEEK_PROP = "__hfStudioManualEditsWrapped";
|
||||
const ROTATION_TRANSFORM_ORIGIN = "center center";
|
||||
|
||||
const finiteNumber = (value: unknown): number | null =>
|
||||
typeof value === "number" && Number.isFinite(value) ? value : null;
|
||||
|
||||
const objectRecord = (value: unknown): Record<string, unknown> | null =>
|
||||
value && typeof value === "object" ? (value as Record<string, unknown>) : null;
|
||||
|
||||
const runtimeWindow = window as Window & {
|
||||
__hf?: { seek?: (time: number) => unknown };
|
||||
__hfStudioManualEditsApply?: () => number;
|
||||
__player?: { renderSeek?: (time: number) => unknown };
|
||||
};
|
||||
|
||||
const parsedManifest = (() => {
|
||||
try {
|
||||
return objectRecord(JSON.parse(manifestContent));
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
})();
|
||||
const manifestEdits = Array.isArray(parsedManifest?.edits) ? parsedManifest.edits : [];
|
||||
if (manifestEdits.length === 0) return;
|
||||
|
||||
const sourceFileForElement = (element: HTMLElement): string => {
|
||||
let current: HTMLElement | null = element;
|
||||
while (current) {
|
||||
const sourceFile =
|
||||
current.getAttribute("data-composition-file") ??
|
||||
current.getAttribute("data-composition-src");
|
||||
if (sourceFile) return sourceFile;
|
||||
current = current.parentElement;
|
||||
}
|
||||
return activeCompositionPath ?? "index.html";
|
||||
};
|
||||
|
||||
const elementMatchesSourceFile = (element: HTMLElement, sourceFile: string): boolean =>
|
||||
sourceFileForElement(element) === sourceFile;
|
||||
|
||||
const styleUsesStudioOffset = (value: string): boolean =>
|
||||
value.includes(OFFSET_X_PROP) || value.includes(OFFSET_Y_PROP);
|
||||
|
||||
const styleUsesStudioRotation = (value: string): boolean => value.includes(ROTATION_PROP);
|
||||
|
||||
const splitTopLevelWhitespace = (value: string): string[] => {
|
||||
const parts: string[] = [];
|
||||
let depth = 0;
|
||||
let current = "";
|
||||
for (const char of value.trim()) {
|
||||
if (char === "(") depth += 1;
|
||||
if (char === ")") depth = Math.max(0, depth - 1);
|
||||
if (/\s/.test(char) && depth === 0) {
|
||||
if (current) parts.push(current);
|
||||
current = "";
|
||||
} else {
|
||||
current += char;
|
||||
}
|
||||
}
|
||||
if (current) parts.push(current);
|
||||
return parts;
|
||||
};
|
||||
|
||||
const composeTranslate = (element: HTMLElement, x: string, y: string): string => {
|
||||
const original = element.getAttribute(ORIGINAL_TRANSLATE_ATTR)?.trim();
|
||||
if (!original || original === "none") return `${x} ${y}`;
|
||||
|
||||
const parts = splitTopLevelWhitespace(original);
|
||||
if (parts.length === 1) return `calc(${parts[0]} + ${x}) ${y}`;
|
||||
if (parts.length === 2) return `calc(${parts[0]} + ${x}) calc(${parts[1]} + ${y})`;
|
||||
if (parts.length === 3) {
|
||||
return `calc(${parts[0]} + ${x}) calc(${parts[1]} + ${y}) ${parts[2]}`;
|
||||
}
|
||||
return `${x} ${y}`;
|
||||
};
|
||||
|
||||
const readStyleOrComputed = (element: HTMLElement, property: string): string => {
|
||||
try {
|
||||
return (
|
||||
element.style.getPropertyValue(property) ||
|
||||
getComputedStyle(element).getPropertyValue(property)
|
||||
);
|
||||
} catch {
|
||||
return element.style.getPropertyValue(property);
|
||||
}
|
||||
};
|
||||
|
||||
const readTransformLonghandBase = (
|
||||
element: HTMLElement,
|
||||
property: "translate" | "rotate",
|
||||
): string => {
|
||||
const value = readStyleOrComputed(element, property).trim();
|
||||
return value === "none" ? "" : value;
|
||||
};
|
||||
|
||||
const preparePathOffsetBase = (element: HTMLElement): void => {
|
||||
const currentTranslate = readTransformLonghandBase(element, "translate");
|
||||
const hasMarker = element.hasAttribute(PATH_OFFSET_ATTR);
|
||||
const wasResetByAnimation = !styleUsesStudioOffset(currentTranslate);
|
||||
if (!hasMarker) {
|
||||
element.setAttribute(ORIGINAL_TRANSLATE_ATTR, wasResetByAnimation ? currentTranslate : "");
|
||||
} else if (wasResetByAnimation) {
|
||||
element.setAttribute(ORIGINAL_TRANSLATE_ATTR, currentTranslate);
|
||||
}
|
||||
};
|
||||
|
||||
const prepareRotationBase = (element: HTMLElement): void => {
|
||||
const currentRotate = readTransformLonghandBase(element, "rotate");
|
||||
const hasMarker = element.hasAttribute(ROTATION_ATTR);
|
||||
const wasResetByAnimation = !styleUsesStudioRotation(currentRotate);
|
||||
if (!hasMarker) {
|
||||
element.setAttribute(ORIGINAL_ROTATE_ATTR, wasResetByAnimation ? currentRotate : "");
|
||||
} else if (wasResetByAnimation) {
|
||||
element.setAttribute(ORIGINAL_ROTATE_ATTR, currentRotate);
|
||||
}
|
||||
};
|
||||
|
||||
const querySelectorCandidates = (selector: string): HTMLElement[] => {
|
||||
const isCandidate = (element: Element): element is HTMLElement =>
|
||||
element instanceof HTMLElement;
|
||||
|
||||
const className = selector.match(/^\.([A-Za-z0-9_-]+)$/)?.[1];
|
||||
if (className) {
|
||||
return Array.from(document.getElementsByTagName("*")).filter(
|
||||
(element): element is HTMLElement =>
|
||||
isCandidate(element) && element.classList.contains(className),
|
||||
);
|
||||
}
|
||||
|
||||
if (/^[A-Za-z][A-Za-z0-9-]*$/.test(selector)) {
|
||||
return Array.from(document.getElementsByTagName(selector)).filter(isCandidate);
|
||||
}
|
||||
|
||||
return Array.from(document.querySelectorAll(selector)).filter(isCandidate);
|
||||
};
|
||||
|
||||
const resolveTarget = (edit: Record<string, unknown>): HTMLElement | null => {
|
||||
const targetRecord = objectRecord(edit.target);
|
||||
if (!targetRecord) return null;
|
||||
|
||||
const sourceFile = typeof targetRecord.sourceFile === "string" ? targetRecord.sourceFile : "";
|
||||
if (!sourceFile) return null;
|
||||
|
||||
const id = typeof targetRecord.id === "string" ? targetRecord.id : "";
|
||||
if (id) {
|
||||
const byId = document.getElementById(id);
|
||||
if (byId instanceof HTMLElement && elementMatchesSourceFile(byId, sourceFile)) return byId;
|
||||
|
||||
const matchesById = [
|
||||
document.documentElement,
|
||||
...Array.from(document.getElementsByTagName("*")),
|
||||
].filter(
|
||||
(element): element is HTMLElement =>
|
||||
element instanceof HTMLElement &&
|
||||
element.id === id &&
|
||||
elementMatchesSourceFile(element, sourceFile),
|
||||
);
|
||||
if (matchesById[0]) return matchesById[0];
|
||||
}
|
||||
|
||||
const selector = typeof targetRecord.selector === "string" ? targetRecord.selector : "";
|
||||
if (!selector) return null;
|
||||
|
||||
try {
|
||||
const matches = querySelectorCandidates(selector).filter((element) =>
|
||||
elementMatchesSourceFile(element, sourceFile),
|
||||
);
|
||||
const selectorIndex = finiteNumber(targetRecord.selectorIndex) ?? 0;
|
||||
return matches[Math.max(0, Math.floor(selectorIndex))] ?? null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
const roundRotationAngle = (angle: number): number => Math.round(angle * 10) / 10;
|
||||
|
||||
const isSimpleRotateAngle = (value: string): boolean =>
|
||||
/^-?(?:\d+(?:\.\d+)?|\.\d+)(?:deg|rad|turn|grad)$/.test(value.trim());
|
||||
|
||||
const composeRotation = (element: HTMLElement, rotationValue: string): string => {
|
||||
const original = element.getAttribute(ORIGINAL_ROTATE_ATTR)?.trim();
|
||||
if (!original || original === "none" || !isSimpleRotateAngle(original)) {
|
||||
return rotationValue;
|
||||
}
|
||||
return `calc(${original} + ${rotationValue})`;
|
||||
};
|
||||
|
||||
const applyPathOffset = (element: HTMLElement, edit: Record<string, unknown>): void => {
|
||||
const x = finiteNumber(edit.x);
|
||||
const y = finiteNumber(edit.y);
|
||||
if (x == null || y == null) return;
|
||||
preparePathOffsetBase(element);
|
||||
element.setAttribute(PATH_OFFSET_ATTR, "true");
|
||||
element.style.setProperty(OFFSET_X_PROP, `${Math.round(x)}px`);
|
||||
element.style.setProperty(OFFSET_Y_PROP, `${Math.round(y)}px`);
|
||||
element.style.setProperty(
|
||||
"translate",
|
||||
composeTranslate(element, `var(${OFFSET_X_PROP}, 0px)`, `var(${OFFSET_Y_PROP}, 0px)`),
|
||||
);
|
||||
};
|
||||
|
||||
const readParentFlexBasisPixels = (
|
||||
element: HTMLElement,
|
||||
size: { width: number; height: number },
|
||||
): number | null => {
|
||||
const parent = element.parentElement;
|
||||
if (!parent) return null;
|
||||
const styles = getComputedStyle(parent);
|
||||
if (styles.display !== "flex" && styles.display !== "inline-flex") return null;
|
||||
return Math.round(
|
||||
Math.max(1, styles.flexDirection.startsWith("column") ? size.height : size.width),
|
||||
);
|
||||
};
|
||||
|
||||
const applyBoxSize = (element: HTMLElement, edit: Record<string, unknown>): void => {
|
||||
const width = finiteNumber(edit.width);
|
||||
const height = finiteNumber(edit.height);
|
||||
if (width == null || height == null || width <= 0 || height <= 0) return;
|
||||
|
||||
const rounded = {
|
||||
width: Math.round(Math.max(1, width)),
|
||||
height: Math.round(Math.max(1, height)),
|
||||
};
|
||||
element.setAttribute(BOX_SIZE_ATTR, "true");
|
||||
element.style.setProperty(WIDTH_PROP, `${rounded.width}px`);
|
||||
element.style.setProperty(HEIGHT_PROP, `${rounded.height}px`);
|
||||
element.style.setProperty("box-sizing", "border-box");
|
||||
element.style.setProperty("width", `${rounded.width}px`);
|
||||
element.style.setProperty("height", `${rounded.height}px`);
|
||||
element.style.setProperty("min-width", "0px");
|
||||
element.style.setProperty("min-height", "0px");
|
||||
element.style.setProperty("max-width", "none");
|
||||
element.style.setProperty("max-height", "none");
|
||||
|
||||
const flexBasis = readParentFlexBasisPixels(element, rounded);
|
||||
if (flexBasis != null) {
|
||||
element.style.setProperty("flex-basis", `${flexBasis}px`);
|
||||
element.style.setProperty("flex-grow", "0");
|
||||
element.style.setProperty("flex-shrink", "0");
|
||||
}
|
||||
if (getComputedStyle(element).display === "inline") {
|
||||
element.style.setProperty("display", "inline-block");
|
||||
}
|
||||
};
|
||||
|
||||
const applyRotation = (element: HTMLElement, edit: Record<string, unknown>): void => {
|
||||
const angle = finiteNumber(edit.angle);
|
||||
if (angle == null) return;
|
||||
prepareRotationBase(element);
|
||||
element.setAttribute(ROTATION_ATTR, "true");
|
||||
element.style.setProperty(ROTATION_PROP, `${roundRotationAngle(angle)}deg`);
|
||||
element.style.setProperty("transform-origin", ROTATION_TRANSFORM_ORIGIN);
|
||||
element.style.setProperty("rotate", composeRotation(element, `var(${ROTATION_PROP}, 0deg)`));
|
||||
};
|
||||
|
||||
const applyManifest = (): number => {
|
||||
let applied = 0;
|
||||
for (const edit of manifestEdits) {
|
||||
const editRecord = objectRecord(edit);
|
||||
if (!editRecord) continue;
|
||||
const element = resolveTarget(editRecord);
|
||||
if (!element) continue;
|
||||
if (editRecord.kind === "path-offset") applyPathOffset(element, editRecord);
|
||||
if (editRecord.kind === "box-size") applyBoxSize(element, editRecord);
|
||||
if (editRecord.kind === "rotation") applyRotation(element, editRecord);
|
||||
applied += 1;
|
||||
}
|
||||
return applied;
|
||||
};
|
||||
runtimeWindow.__hfStudioManualEditsApply = applyManifest;
|
||||
|
||||
const markWrapped = (fn: (time: number) => unknown): void => {
|
||||
try {
|
||||
Object.defineProperty(fn, WRAPPED_SEEK_PROP, {
|
||||
configurable: false,
|
||||
enumerable: false,
|
||||
value: true,
|
||||
});
|
||||
} catch {
|
||||
try {
|
||||
(fn as unknown as Record<string, unknown>)[WRAPPED_SEEK_PROP] = true;
|
||||
} catch {
|
||||
// Ignore non-extensible functions.
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const isWrapped = (fn: (time: number) => unknown): boolean =>
|
||||
Boolean((fn as unknown as Record<string, unknown>)[WRAPPED_SEEK_PROP]);
|
||||
|
||||
const wrapFunction = (
|
||||
get: () => ((time: number) => unknown) | undefined,
|
||||
set: (fn: (time: number) => unknown) => void,
|
||||
): boolean => {
|
||||
const fn = get();
|
||||
if (!fn) return false;
|
||||
const seek = fn as (time: number) => unknown;
|
||||
if (isWrapped(seek)) {
|
||||
applyManifest();
|
||||
return true;
|
||||
}
|
||||
|
||||
const wrappedSeek = function (this: unknown, time: number): unknown {
|
||||
const result = seek.call(this, time);
|
||||
applyManifest();
|
||||
return result;
|
||||
};
|
||||
markWrapped(wrappedSeek);
|
||||
set(wrappedSeek);
|
||||
applyManifest();
|
||||
return true;
|
||||
};
|
||||
|
||||
const wrapSeekFunctions = (): boolean => {
|
||||
const wrappedHfSeek = wrapFunction(
|
||||
() => runtimeWindow.__hf?.seek,
|
||||
(fn) => {
|
||||
if (runtimeWindow.__hf) runtimeWindow.__hf.seek = fn;
|
||||
},
|
||||
);
|
||||
const wrappedPlayerRenderSeek = wrapFunction(
|
||||
() => runtimeWindow.__player?.renderSeek,
|
||||
(fn) => {
|
||||
if (runtimeWindow.__player) runtimeWindow.__player.renderSeek = fn;
|
||||
},
|
||||
);
|
||||
return wrappedHfSeek || wrappedPlayerRenderSeek;
|
||||
};
|
||||
|
||||
if (document.readyState === "loading") {
|
||||
document.addEventListener("DOMContentLoaded", () => applyManifest(), { once: true });
|
||||
} else {
|
||||
applyManifest();
|
||||
}
|
||||
|
||||
wrapSeekFunctions();
|
||||
let remainingSeekWrapAttempts = 120;
|
||||
const seekWrapInterval = setInterval(() => {
|
||||
wrapSeekFunctions();
|
||||
remainingSeekWrapAttempts -= 1;
|
||||
if (remainingSeekWrapAttempts <= 0) clearInterval(seekWrapInterval);
|
||||
}, 50);
|
||||
}
|
||||
@@ -28,6 +28,10 @@ const SIGNATURE_EXCLUDED_DIRS = new Set([
|
||||
"renders",
|
||||
]);
|
||||
const MAX_SIGNATURE_TEXT_BYTES = 2_000_000;
|
||||
const STUDIO_SIGNATURE_MANIFEST_PATHS = [
|
||||
".hyperframes/studio-manual-edits.json",
|
||||
".hyperframes/studio-motion.json",
|
||||
] as const;
|
||||
|
||||
interface ProjectSignatureFile {
|
||||
file: string;
|
||||
@@ -93,6 +97,31 @@ function collectProjectSignatureFiles(
|
||||
}
|
||||
}
|
||||
|
||||
function collectProjectSignatureManifestFiles(
|
||||
projectDir: string,
|
||||
files: ProjectSignatureFile[],
|
||||
): void {
|
||||
const seen = new Set(files.map((entry) => entry.file));
|
||||
for (const manifestPath of STUDIO_SIGNATURE_MANIFEST_PATHS) {
|
||||
const file = resolve(projectDir, manifestPath);
|
||||
if (seen.has(file) || !isPathWithin(projectDir, file)) continue;
|
||||
let stat: ReturnType<typeof lstatSync>;
|
||||
try {
|
||||
stat = lstatSync(file);
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
if (stat.isSymbolicLink() || !stat.isFile()) continue;
|
||||
files.push({
|
||||
file,
|
||||
mtimeMs: stat.mtimeMs,
|
||||
size: stat.size,
|
||||
textContentEligible: isTextContentEligible(file, stat.size),
|
||||
});
|
||||
seen.add(file);
|
||||
}
|
||||
}
|
||||
|
||||
function createProjectFingerprint(projectDir: string, files: ProjectSignatureFile[]): string {
|
||||
const hash = createHash("sha256");
|
||||
for (const entry of files) {
|
||||
@@ -108,10 +137,14 @@ function createProjectFingerprint(projectDir: string, files: ProjectSignatureFil
|
||||
return hash.digest("hex").slice(0, 24);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a stable preview cache-busting signature for project source plus Studio manifests.
|
||||
*/
|
||||
export function createProjectSignature(projectDir: string): string {
|
||||
const normalizedProjectDir = resolve(projectDir);
|
||||
const files: ProjectSignatureFile[] = [];
|
||||
collectProjectSignatureFiles(normalizedProjectDir, normalizedProjectDir, files);
|
||||
collectProjectSignatureManifestFiles(normalizedProjectDir, files);
|
||||
files.sort((a, b) => a.file.localeCompare(b.file));
|
||||
|
||||
const fingerprint = createProjectFingerprint(normalizedProjectDir, files);
|
||||
|
||||
@@ -5,8 +5,15 @@ export interface ScreenshotClip {
|
||||
height: number;
|
||||
}
|
||||
|
||||
export function getElementScreenshotClip(selector: string): ScreenshotClip | undefined {
|
||||
const el = document.querySelector(selector);
|
||||
export function getElementScreenshotClip(
|
||||
selector: string,
|
||||
selectorIndex?: number,
|
||||
): ScreenshotClip | undefined {
|
||||
const matches = Array.from(document.querySelectorAll(selector)).filter(
|
||||
(el): el is HTMLElement => el instanceof HTMLElement,
|
||||
);
|
||||
const safeIndex = Math.max(0, Math.min(matches.length - 1, Math.floor(selectorIndex ?? 0)));
|
||||
const el = matches[safeIndex] ?? null;
|
||||
if (!(el instanceof HTMLElement)) return undefined;
|
||||
const rect = el.getBoundingClientRect();
|
||||
if (rect.width < 4 || rect.height < 4) return undefined;
|
||||
|
||||
@@ -17,6 +17,20 @@ function parseSourceDocument(source: string): { document: Document; wrappedFragm
|
||||
};
|
||||
}
|
||||
|
||||
function querySelectorAllWithTemplates(root: Document | Element, selector: string): Element[] {
|
||||
const matches = Array.from(root.querySelectorAll(selector));
|
||||
if (matches.length > 0) return matches;
|
||||
// querySelectorAll doesn't traverse <template> content in linkedom.
|
||||
// Search directly on each template element (NOT .content — removing from
|
||||
// .content's DocumentFragment doesn't update the serialized output).
|
||||
const templates = Array.from(root.querySelectorAll("template"));
|
||||
for (const tmpl of templates) {
|
||||
const inner = tmpl.querySelectorAll(selector);
|
||||
if (inner.length > 0) return Array.from(inner);
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
function findTargetElement(document: Document, target: SourceMutationTarget): Element | null {
|
||||
if (target.id) {
|
||||
const byId = document.getElementById(target.id);
|
||||
@@ -25,7 +39,7 @@ function findTargetElement(document: Document, target: SourceMutationTarget): El
|
||||
|
||||
if (!target.selector) return null;
|
||||
try {
|
||||
const matches = Array.from(document.querySelectorAll(target.selector));
|
||||
const matches = querySelectorAllWithTemplates(document, target.selector);
|
||||
return matches[target.selectorIndex ?? 0] ?? null;
|
||||
} catch {
|
||||
return null;
|
||||
|
||||
@@ -0,0 +1,205 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { Window } from "happy-dom";
|
||||
import { createStudioMotionRenderBodyScript } from "./studioMotionRenderScript";
|
||||
|
||||
function runScript(window: Window, script: string): void {
|
||||
const execute = new Function("window", "document", "HTMLElement", script);
|
||||
execute(window, window.document, window.HTMLElement);
|
||||
}
|
||||
|
||||
function installFakeGsap(window: Window): {
|
||||
calls: Array<{
|
||||
target: HTMLElement;
|
||||
from: Record<string, unknown>;
|
||||
to: Record<string, unknown>;
|
||||
at: number;
|
||||
}>;
|
||||
timeCalls: number[];
|
||||
customEaseCalls: Array<{ id: string; data: string }>;
|
||||
killCalls: number;
|
||||
} {
|
||||
const state = {
|
||||
calls: [] as Array<{
|
||||
target: HTMLElement;
|
||||
from: Record<string, unknown>;
|
||||
to: Record<string, unknown>;
|
||||
at: number;
|
||||
}>,
|
||||
timeCalls: [] as number[],
|
||||
customEaseCalls: [] as Array<{ id: string; data: string }>,
|
||||
killCalls: 0,
|
||||
};
|
||||
const timeline = {
|
||||
fromTo(
|
||||
target: HTMLElement,
|
||||
from: Record<string, unknown>,
|
||||
to: Record<string, unknown>,
|
||||
at: number,
|
||||
) {
|
||||
state.calls.push({ target, from, to, at });
|
||||
return timeline;
|
||||
},
|
||||
time(value: number) {
|
||||
state.timeCalls.push(value);
|
||||
return timeline;
|
||||
},
|
||||
pause() {
|
||||
return timeline;
|
||||
},
|
||||
kill() {
|
||||
state.killCalls += 1;
|
||||
},
|
||||
duration() {
|
||||
return 2;
|
||||
},
|
||||
};
|
||||
(
|
||||
window as unknown as {
|
||||
gsap: {
|
||||
timeline: () => typeof timeline;
|
||||
set: (target: HTMLElement, vars: Record<string, unknown>) => void;
|
||||
};
|
||||
CustomEase: { create: (id: string, data: string) => void };
|
||||
__player?: { getTime: () => number };
|
||||
}
|
||||
).gsap = {
|
||||
timeline: () => timeline,
|
||||
set(target, vars) {
|
||||
if (vars.clearProps === "transform,opacity,visibility") {
|
||||
target.style.removeProperty("transform");
|
||||
target.style.removeProperty("opacity");
|
||||
target.style.removeProperty("visibility");
|
||||
}
|
||||
},
|
||||
};
|
||||
(
|
||||
window as unknown as {
|
||||
CustomEase: { create: (id: string, data: string) => void };
|
||||
}
|
||||
).CustomEase = {
|
||||
create(id, data) {
|
||||
state.customEaseCalls.push({ id, data });
|
||||
},
|
||||
};
|
||||
return state;
|
||||
}
|
||||
|
||||
describe("createStudioMotionRenderBodyScript", () => {
|
||||
it("returns null for an empty manifest", () => {
|
||||
expect(createStudioMotionRenderBodyScript("")).toBeNull();
|
||||
});
|
||||
|
||||
it("returns null for a valid manifest without motions", () => {
|
||||
expect(createStudioMotionRenderBodyScript(`{"version":1,"motions":[]}`)).toBeNull();
|
||||
});
|
||||
|
||||
it("registers Studio-authored GSAP motion into window.__timelines", () => {
|
||||
const window = new Window();
|
||||
window.document.body.innerHTML = '<div id="card" style="opacity: 0.6"></div>';
|
||||
const card = window.document.getElementById("card");
|
||||
if (!(card instanceof window.HTMLElement)) throw new Error("card fixture missing");
|
||||
const gsapState = installFakeGsap(window);
|
||||
(
|
||||
window as unknown as {
|
||||
__player: { getTime: () => number };
|
||||
}
|
||||
).__player = { getTime: () => 0.5 };
|
||||
|
||||
const script = createStudioMotionRenderBodyScript(
|
||||
JSON.stringify({
|
||||
version: 1,
|
||||
motions: [
|
||||
{
|
||||
kind: "gsap-motion",
|
||||
target: { sourceFile: "index.html", id: "card" },
|
||||
start: 0.2,
|
||||
duration: 0.7,
|
||||
ease: "power2.out",
|
||||
from: { y: 32, autoAlpha: 0 },
|
||||
to: { y: 0, autoAlpha: 1 },
|
||||
},
|
||||
],
|
||||
}),
|
||||
);
|
||||
if (!script) throw new Error("script fixture missing");
|
||||
|
||||
runScript(window, script);
|
||||
|
||||
expect(gsapState.calls[0]).toMatchObject({
|
||||
target: card,
|
||||
from: { y: 32, autoAlpha: 0 },
|
||||
to: { y: 0, autoAlpha: 1, duration: 0.7, ease: "power2.out" },
|
||||
at: 0.2,
|
||||
});
|
||||
expect(gsapState.timeCalls).toEqual([0.5]);
|
||||
expect(
|
||||
(window as unknown as { __timelines?: Record<string, unknown> }).__timelines?.[
|
||||
"studio-motion"
|
||||
],
|
||||
).toBeTruthy();
|
||||
});
|
||||
|
||||
it("does not mutate when GSAP is unavailable", () => {
|
||||
const window = new Window();
|
||||
window.document.body.innerHTML = '<div id="card" style="opacity: 0.6"></div>';
|
||||
const script = createStudioMotionRenderBodyScript(
|
||||
JSON.stringify({
|
||||
version: 1,
|
||||
motions: [
|
||||
{
|
||||
kind: "gsap-motion",
|
||||
target: { sourceFile: "index.html", id: "card" },
|
||||
start: 0,
|
||||
duration: 1,
|
||||
ease: "none",
|
||||
from: { x: 0 },
|
||||
to: { x: 10 },
|
||||
},
|
||||
],
|
||||
}),
|
||||
);
|
||||
if (!script) throw new Error("script fixture missing");
|
||||
|
||||
runScript(window, script);
|
||||
|
||||
expect(
|
||||
(window as unknown as { __timelines?: Record<string, unknown> }).__timelines?.[
|
||||
"studio-motion"
|
||||
],
|
||||
).toBeUndefined();
|
||||
});
|
||||
|
||||
it("registers CustomEase data before adding Studio motion tweens", () => {
|
||||
const window = new Window();
|
||||
window.document.body.innerHTML = '<div id="card"></div>';
|
||||
const gsapState = installFakeGsap(window);
|
||||
const script = createStudioMotionRenderBodyScript(
|
||||
JSON.stringify({
|
||||
version: 1,
|
||||
motions: [
|
||||
{
|
||||
kind: "gsap-motion",
|
||||
target: { sourceFile: "index.html", id: "card" },
|
||||
start: 0,
|
||||
duration: 1,
|
||||
ease: "studio-card-bounce",
|
||||
customEase: {
|
||||
id: "studio-card-bounce",
|
||||
data: "M0,0 C0.18,0.9 0.32,1 1,1",
|
||||
},
|
||||
from: { y: 32 },
|
||||
to: { y: 0 },
|
||||
},
|
||||
],
|
||||
}),
|
||||
);
|
||||
if (!script) throw new Error("script fixture missing");
|
||||
|
||||
runScript(window, script);
|
||||
|
||||
expect(gsapState.customEaseCalls).toEqual([
|
||||
{ id: "studio-card-bounce", data: "M0,0 C0.18,0.9 0.32,1 1,1" },
|
||||
]);
|
||||
expect(gsapState.calls[0]?.to.ease).toBe("studio-card-bounce");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,260 @@
|
||||
export interface StudioMotionRenderScriptOptions {
|
||||
activeCompositionPath?: string | null;
|
||||
}
|
||||
|
||||
export const STUDIO_MOTION_PATH = ".hyperframes/studio-motion.json";
|
||||
|
||||
function hasStudioMotionEntries(manifestContent: string): boolean {
|
||||
try {
|
||||
const parsed = JSON.parse(manifestContent) as { motions?: unknown };
|
||||
return Array.isArray(parsed.motions) && parsed.motions.length > 0;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the render-time Studio motion runtime script, or null when no owned motion exists.
|
||||
*/
|
||||
export function createStudioMotionRenderBodyScript(
|
||||
manifestContent: string,
|
||||
options: StudioMotionRenderScriptOptions = {},
|
||||
): string | null {
|
||||
if (!manifestContent.trim() || !hasStudioMotionEntries(manifestContent)) return null;
|
||||
return `(${studioMotionRenderRuntime.toString()})(${JSON.stringify(manifestContent)}, ${JSON.stringify(options.activeCompositionPath ?? null)});`;
|
||||
}
|
||||
|
||||
function studioMotionRenderRuntime(
|
||||
manifestContent: string,
|
||||
activeCompositionPath: string | null,
|
||||
): void {
|
||||
const STUDIO_MOTION_TIMELINE_ID = "studio-motion";
|
||||
const STUDIO_MOTION_ATTR = "data-hf-studio-motion";
|
||||
const ORIGINAL_TRANSFORM_ATTR = "data-hf-studio-motion-original-transform";
|
||||
const ORIGINAL_OPACITY_ATTR = "data-hf-studio-motion-original-opacity";
|
||||
const ORIGINAL_VISIBILITY_ATTR = "data-hf-studio-motion-original-visibility";
|
||||
|
||||
const objectRecord = (value: unknown): Record<string, unknown> | null =>
|
||||
value && typeof value === "object" ? (value as Record<string, unknown>) : null;
|
||||
|
||||
const finiteNumber = (value: unknown): number | null =>
|
||||
typeof value === "number" && Number.isFinite(value) ? value : null;
|
||||
|
||||
const runtimeWindow = window as Window & {
|
||||
gsap?: {
|
||||
timeline?: (vars?: Record<string, unknown>) => {
|
||||
fromTo?: (
|
||||
target: HTMLElement,
|
||||
from: Record<string, unknown>,
|
||||
to: Record<string, unknown>,
|
||||
at: number,
|
||||
) => unknown;
|
||||
totalTime?: (time: number, suppressEvents?: boolean) => unknown;
|
||||
time?: (time: number) => unknown;
|
||||
pause?: () => unknown;
|
||||
kill?: () => unknown;
|
||||
};
|
||||
set?: (target: HTMLElement, vars: Record<string, unknown>) => unknown;
|
||||
registerPlugin?: (...plugins: unknown[]) => unknown;
|
||||
};
|
||||
CustomEase?: { create?: (id: string, data: string) => unknown };
|
||||
__player?: { getTime?: () => number };
|
||||
__timeline?: { time?: () => number };
|
||||
__timelines?: Record<
|
||||
string,
|
||||
| {
|
||||
kill?: () => unknown;
|
||||
}
|
||||
| undefined
|
||||
>;
|
||||
__hfStudioMotionApply?: () => number;
|
||||
};
|
||||
|
||||
const parseMotionValues = (value: unknown): Record<string, number> | null => {
|
||||
const record = objectRecord(value);
|
||||
if (!record) return null;
|
||||
const parsed: Record<string, number> = {};
|
||||
for (const key of ["x", "y", "scale", "rotation", "opacity", "autoAlpha"]) {
|
||||
const next = finiteNumber(record[key]);
|
||||
if (next != null) parsed[key] = next;
|
||||
}
|
||||
return Object.keys(parsed).length > 0 ? parsed : null;
|
||||
};
|
||||
|
||||
const parseCustomEase = (value: unknown): { id: string; data: string } | null => {
|
||||
const record = objectRecord(value);
|
||||
if (!record) return null;
|
||||
const id = typeof record.id === "string" ? record.id.trim() : "";
|
||||
const data = typeof record.data === "string" ? record.data.trim() : "";
|
||||
if (!id || !data) return null;
|
||||
return { id, data };
|
||||
};
|
||||
|
||||
const parsedManifest = (() => {
|
||||
try {
|
||||
return objectRecord(JSON.parse(manifestContent));
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
})();
|
||||
const manifestMotions = Array.isArray(parsedManifest?.motions) ? parsedManifest.motions : [];
|
||||
|
||||
const sourceFileForElement = (element: HTMLElement): string => {
|
||||
let current: HTMLElement | null = element;
|
||||
while (current) {
|
||||
const sourceFile =
|
||||
current.getAttribute("data-composition-file") ??
|
||||
current.getAttribute("data-composition-src");
|
||||
if (sourceFile) return sourceFile;
|
||||
current = current.parentElement;
|
||||
}
|
||||
return activeCompositionPath ?? "index.html";
|
||||
};
|
||||
|
||||
const elementMatchesSourceFile = (element: HTMLElement, sourceFile: string): boolean =>
|
||||
sourceFileForElement(element) === sourceFile;
|
||||
|
||||
const isHTMLElement = (element: Element | null): element is HTMLElement =>
|
||||
element instanceof HTMLElement;
|
||||
|
||||
const querySelectorCandidates = (selector: string): HTMLElement[] => {
|
||||
const className = selector.match(/^\.([A-Za-z0-9_-]+)$/)?.[1];
|
||||
if (className) {
|
||||
return Array.from(document.getElementsByTagName("*")).filter(
|
||||
(element): element is HTMLElement =>
|
||||
isHTMLElement(element) && element.classList.contains(className),
|
||||
);
|
||||
}
|
||||
if (/^[A-Za-z][A-Za-z0-9-]*$/.test(selector)) {
|
||||
return Array.from(document.getElementsByTagName(selector)).filter(isHTMLElement);
|
||||
}
|
||||
return Array.from(document.querySelectorAll(selector)).filter(isHTMLElement);
|
||||
};
|
||||
|
||||
const resolveTarget = (targetRecord: Record<string, unknown>): HTMLElement | null => {
|
||||
const sourceFile = typeof targetRecord.sourceFile === "string" ? targetRecord.sourceFile : "";
|
||||
if (!sourceFile) return null;
|
||||
const id = typeof targetRecord.id === "string" ? targetRecord.id : "";
|
||||
if (id) {
|
||||
const byId = document.getElementById(id);
|
||||
if (isHTMLElement(byId) && elementMatchesSourceFile(byId, sourceFile)) return byId;
|
||||
}
|
||||
const selector = typeof targetRecord.selector === "string" ? targetRecord.selector : "";
|
||||
if (!selector) return null;
|
||||
try {
|
||||
const selectorIndex = Math.max(0, Math.floor(finiteNumber(targetRecord.selectorIndex) ?? 0));
|
||||
return (
|
||||
querySelectorCandidates(selector).filter((element) =>
|
||||
elementMatchesSourceFile(element, sourceFile),
|
||||
)[selectorIndex] ?? null
|
||||
);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
const restoreElement = (element: HTMLElement): void => {
|
||||
runtimeWindow.gsap?.set?.(element, { clearProps: "transform,opacity,visibility" });
|
||||
element.style.transform = element.getAttribute(ORIGINAL_TRANSFORM_ATTR) ?? "";
|
||||
element.style.opacity = element.getAttribute(ORIGINAL_OPACITY_ATTR) ?? "";
|
||||
element.style.visibility = element.getAttribute(ORIGINAL_VISIBILITY_ATTR) ?? "";
|
||||
element.removeAttribute(STUDIO_MOTION_ATTR);
|
||||
element.removeAttribute(ORIGINAL_TRANSFORM_ATTR);
|
||||
element.removeAttribute(ORIGINAL_OPACITY_ATTR);
|
||||
element.removeAttribute(ORIGINAL_VISIBILITY_ATTR);
|
||||
};
|
||||
|
||||
const restoreStudioMotionElements = (): void => {
|
||||
for (const element of Array.from(document.querySelectorAll(`[${STUDIO_MOTION_ATTR}]`))) {
|
||||
if (isHTMLElement(element)) restoreElement(element);
|
||||
}
|
||||
};
|
||||
|
||||
const readCurrentTime = (): number => {
|
||||
try {
|
||||
const playerTime = runtimeWindow.__player?.getTime?.();
|
||||
if (typeof playerTime === "number" && Number.isFinite(playerTime)) {
|
||||
return Math.max(0, playerTime);
|
||||
}
|
||||
} catch {
|
||||
// fall through
|
||||
}
|
||||
try {
|
||||
const timelineTime = runtimeWindow.__timeline?.time?.();
|
||||
if (typeof timelineTime === "number" && Number.isFinite(timelineTime)) {
|
||||
return Math.max(0, timelineTime);
|
||||
}
|
||||
} catch {
|
||||
// fall through
|
||||
}
|
||||
return 0;
|
||||
};
|
||||
|
||||
const resolveEase = (motion: Record<string, unknown>): string => {
|
||||
const fallback =
|
||||
typeof motion.ease === "string" && motion.ease.trim() ? motion.ease.trim() : "none";
|
||||
const customEase = parseCustomEase(motion.customEase);
|
||||
const customEasePlugin = runtimeWindow.CustomEase;
|
||||
if (!customEase || typeof customEasePlugin?.create !== "function") return fallback;
|
||||
try {
|
||||
runtimeWindow.gsap?.registerPlugin?.(customEasePlugin);
|
||||
customEasePlugin.create(customEase.id, customEase.data);
|
||||
return customEase.id;
|
||||
} catch {
|
||||
return fallback;
|
||||
}
|
||||
};
|
||||
|
||||
const applyManifest = (): number => {
|
||||
runtimeWindow.__timelines = runtimeWindow.__timelines ?? {};
|
||||
runtimeWindow.__timelines[STUDIO_MOTION_TIMELINE_ID]?.kill?.();
|
||||
delete runtimeWindow.__timelines[STUDIO_MOTION_TIMELINE_ID];
|
||||
restoreStudioMotionElements();
|
||||
const gsap = runtimeWindow.gsap;
|
||||
if (!gsap?.timeline || manifestMotions.length === 0) return 0;
|
||||
|
||||
const timeline = gsap.timeline({ paused: true, defaults: { overwrite: "auto" } });
|
||||
let applied = 0;
|
||||
for (const motionValue of manifestMotions) {
|
||||
const motion = objectRecord(motionValue);
|
||||
if (!motion || motion.kind !== "gsap-motion") continue;
|
||||
const targetRecord = objectRecord(motion.target);
|
||||
if (!targetRecord) continue;
|
||||
const target = resolveTarget(targetRecord);
|
||||
if (!target || typeof timeline.fromTo !== "function") continue;
|
||||
const start = finiteNumber(motion.start);
|
||||
const duration = finiteNumber(motion.duration);
|
||||
if (start == null || duration == null || start < 0 || duration <= 0) continue;
|
||||
const from = parseMotionValues(motion.from);
|
||||
const to = parseMotionValues(motion.to);
|
||||
if (!from || !to) continue;
|
||||
if (!target.hasAttribute(STUDIO_MOTION_ATTR)) {
|
||||
target.setAttribute(ORIGINAL_TRANSFORM_ATTR, target.style.transform);
|
||||
target.setAttribute(ORIGINAL_OPACITY_ATTR, target.style.opacity);
|
||||
target.setAttribute(ORIGINAL_VISIBILITY_ATTR, target.style.visibility);
|
||||
}
|
||||
target.setAttribute(STUDIO_MOTION_ATTR, "true");
|
||||
timeline.fromTo(
|
||||
target,
|
||||
from,
|
||||
{ ...to, duration, ease: resolveEase(motion), overwrite: "auto", immediateRender: false },
|
||||
start,
|
||||
);
|
||||
applied += 1;
|
||||
}
|
||||
|
||||
if (applied === 0) {
|
||||
timeline.kill?.();
|
||||
return 0;
|
||||
}
|
||||
runtimeWindow.__timelines[STUDIO_MOTION_TIMELINE_ID] = timeline;
|
||||
timeline.pause?.();
|
||||
const currentTime = readCurrentTime();
|
||||
if (typeof timeline.totalTime === "function") timeline.totalTime(currentTime, false);
|
||||
else timeline.time?.(currentTime);
|
||||
return applied;
|
||||
};
|
||||
|
||||
runtimeWindow.__hfStudioMotionApply = applyManifest;
|
||||
applyManifest();
|
||||
}
|
||||
@@ -23,6 +23,7 @@ describe("buildSubCompositionHtml", () => {
|
||||
"compositions/hero.html": `<template id="hero-template">
|
||||
<div data-composition-id="hero" data-width="1920" data-height="1080">
|
||||
<img src="../logo.png" alt="Logo" />
|
||||
<div style="background-image: url('../poster.png')"></div>
|
||||
<style>
|
||||
@font-face {
|
||||
font-family: "Brand Sans";
|
||||
@@ -42,8 +43,10 @@ describe("buildSubCompositionHtml", () => {
|
||||
|
||||
expect(html).toContain('<base href="/api/projects/demo/preview/">');
|
||||
expect(html).toContain('src="logo.png"');
|
||||
expect(html).toContain("background-image: url('poster.png')");
|
||||
expect(html).toContain('url("fonts/brand.woff2")');
|
||||
expect(html).not.toContain('src="../logo.png"');
|
||||
expect(html).not.toContain("url('../poster.png')");
|
||||
expect(html).not.toContain('url("../fonts/brand.woff2")');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
import { existsSync, readFileSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { parseHTML } from "linkedom";
|
||||
import { rewriteAssetPaths, rewriteCssAssetUrls } from "../../compiler/rewriteSubCompPaths.js";
|
||||
import {
|
||||
rewriteAssetPaths,
|
||||
rewriteCssAssetUrls,
|
||||
rewriteInlineStyleAssetUrls,
|
||||
} from "../../compiler/rewriteSubCompPaths.js";
|
||||
|
||||
/**
|
||||
* Build a standalone HTML page for a sub-composition.
|
||||
@@ -36,6 +40,14 @@ export function buildSubCompositionHtml(
|
||||
el.setAttribute(attr, value);
|
||||
},
|
||||
);
|
||||
rewriteInlineStyleAssetUrls(
|
||||
contentDoc.querySelectorAll("[style]"),
|
||||
compPath,
|
||||
(el: Element) => el.getAttribute("style"),
|
||||
(el: Element, value: string) => {
|
||||
el.setAttribute("style", value);
|
||||
},
|
||||
);
|
||||
for (const styleEl of contentDoc.querySelectorAll("style")) {
|
||||
styleEl.textContent = rewriteCssAssetUrls(styleEl.textContent || "", compPath);
|
||||
}
|
||||
|
||||
@@ -5,3 +5,13 @@ export { isSafePath, walkDir } from "./helpers/safePath.js";
|
||||
export { getMimeType, MIME_TYPES } from "./helpers/mime.js";
|
||||
export { buildSubCompositionHtml } from "./helpers/subComposition.js";
|
||||
export { getElementScreenshotClip, type ScreenshotClip } from "./helpers/screenshotClip.js";
|
||||
export {
|
||||
STUDIO_MANUAL_EDITS_PATH,
|
||||
createStudioManualEditsRenderBodyScript,
|
||||
type StudioManualEditsRenderScriptOptions,
|
||||
} from "./helpers/manualEditsRenderScript.js";
|
||||
export {
|
||||
STUDIO_MOTION_PATH,
|
||||
createStudioMotionRenderBodyScript,
|
||||
type StudioMotionRenderScriptOptions,
|
||||
} from "./helpers/studioMotionRenderScript.js";
|
||||
|
||||
@@ -0,0 +1,247 @@
|
||||
import { execFileSync } from "node:child_process";
|
||||
import { existsSync, readdirSync, statSync } from "node:fs";
|
||||
import { homedir, platform } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import type { Hono } from "hono";
|
||||
|
||||
const FONT_EXT_RE = /\.(otf|ttf|ttc|woff2?)$/i;
|
||||
const MAX_FONT_RESULTS = 2000;
|
||||
const GOOGLE_FONTS_METADATA_URL = "https://fonts.google.com/metadata/fonts";
|
||||
const GOOGLE_FONTS_FETCH_TIMEOUT_MS = 3000;
|
||||
let cachedFonts: string[] | null = null;
|
||||
let cachedGoogleFonts: string[] | null = null;
|
||||
|
||||
const STYLE_SUFFIXES = new Set([
|
||||
"black",
|
||||
"bold",
|
||||
"book",
|
||||
"condensed",
|
||||
"demi",
|
||||
"demibold",
|
||||
"display",
|
||||
"extra",
|
||||
"extrabold",
|
||||
"hairline",
|
||||
"heavy",
|
||||
"italic",
|
||||
"light",
|
||||
"medium",
|
||||
"normal",
|
||||
"regular",
|
||||
"roman",
|
||||
"semibold",
|
||||
"thin",
|
||||
"ultra",
|
||||
"ultralight",
|
||||
]);
|
||||
|
||||
const GOOGLE_FONT_FALLBACKS = [
|
||||
"Inter",
|
||||
"Roboto",
|
||||
"Open Sans",
|
||||
"Montserrat",
|
||||
"Poppins",
|
||||
"Lato",
|
||||
"Oswald",
|
||||
"Raleway",
|
||||
"Nunito",
|
||||
"Playfair Display",
|
||||
"Merriweather",
|
||||
"Source Sans 3",
|
||||
"Source Serif 4",
|
||||
"Source Code Pro",
|
||||
"DM Sans",
|
||||
"Space Grotesk",
|
||||
"Space Mono",
|
||||
"Bebas Neue",
|
||||
"Outfit",
|
||||
"JetBrains Mono",
|
||||
];
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === "object" && value !== null;
|
||||
}
|
||||
|
||||
function fontDirectories(): string[] {
|
||||
const home = homedir();
|
||||
if (platform() === "darwin") {
|
||||
return [
|
||||
join(home, "Library", "Fonts"),
|
||||
"/Library/Fonts",
|
||||
"/System/Library/Fonts",
|
||||
"/System/Library/Fonts/Supplemental",
|
||||
];
|
||||
}
|
||||
if (platform() === "win32") {
|
||||
return [join(process.env.WINDIR || "C:\\Windows", "Fonts")];
|
||||
}
|
||||
return [
|
||||
join(home, ".fonts"),
|
||||
join(home, ".local", "share", "fonts"),
|
||||
"/usr/local/share/fonts",
|
||||
"/usr/share/fonts",
|
||||
];
|
||||
}
|
||||
|
||||
function toFamilyName(fileName: string): string | null {
|
||||
const withoutExt = fileName.replace(FONT_EXT_RE, "");
|
||||
if (!withoutExt || withoutExt.startsWith(".")) return null;
|
||||
|
||||
const spaced = withoutExt
|
||||
.replace(/[_-]+/g, " ")
|
||||
.replace(/([a-z])([A-Z])/g, "$1 $2")
|
||||
.replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2")
|
||||
.replace(/\s+/g, " ")
|
||||
.trim();
|
||||
const words = spaced.split(" ").filter(Boolean);
|
||||
while (words.length > 1 && STYLE_SUFFIXES.has((words.at(-1) ?? "").toLowerCase())) {
|
||||
words.pop();
|
||||
}
|
||||
|
||||
const family = words.join(" ").trim();
|
||||
return family.length >= 2 ? family : null;
|
||||
}
|
||||
|
||||
function collectMacSystemProfilerFonts(): string[] {
|
||||
if (platform() !== "darwin") return [];
|
||||
|
||||
let parsed: unknown;
|
||||
try {
|
||||
const raw = execFileSync("system_profiler", ["SPFontsDataType", "-json"], {
|
||||
encoding: "utf8",
|
||||
maxBuffer: 12 * 1024 * 1024,
|
||||
timeout: 5000,
|
||||
});
|
||||
parsed = JSON.parse(raw);
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (!isRecord(parsed) || !Array.isArray(parsed.SPFontsDataType)) return [];
|
||||
const fonts: string[] = [];
|
||||
|
||||
for (const fontEntry of parsed.SPFontsDataType) {
|
||||
if (!isRecord(fontEntry)) continue;
|
||||
const typefaces = fontEntry.typefaces;
|
||||
if (!Array.isArray(typefaces)) continue;
|
||||
|
||||
for (const typeface of typefaces) {
|
||||
if (!isRecord(typeface)) continue;
|
||||
const family = typeface.family;
|
||||
const fullName = typeface.fullname;
|
||||
const name = typeface._name;
|
||||
if (typeof family === "string" && family.trim()) {
|
||||
fonts.push(family.trim());
|
||||
} else if (typeof fullName === "string" && fullName.trim()) {
|
||||
fonts.push(fullName.trim());
|
||||
} else if (typeof name === "string" && name.trim()) {
|
||||
fonts.push(name.trim());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return fonts;
|
||||
}
|
||||
|
||||
function collectFontsFromDir(dir: string, depth = 0): string[] {
|
||||
if (!existsSync(dir) || depth > 2) return [];
|
||||
const fonts: string[] = [];
|
||||
|
||||
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
||||
const fullPath = join(dir, entry.name);
|
||||
if (entry.isDirectory()) {
|
||||
fonts.push(...collectFontsFromDir(fullPath, depth + 1));
|
||||
continue;
|
||||
}
|
||||
if (!entry.isFile() || !FONT_EXT_RE.test(entry.name)) continue;
|
||||
try {
|
||||
if (!statSync(fullPath).isFile()) continue;
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
const family = toFamilyName(entry.name);
|
||||
if (family) fonts.push(family);
|
||||
}
|
||||
|
||||
return fonts;
|
||||
}
|
||||
|
||||
function listInstalledFontFamilies(): string[] {
|
||||
if (cachedFonts) return cachedFonts;
|
||||
const families = new Set<string>();
|
||||
|
||||
for (const family of collectMacSystemProfilerFonts()) {
|
||||
families.add(family);
|
||||
if (families.size >= MAX_FONT_RESULTS) break;
|
||||
}
|
||||
|
||||
for (const dir of fontDirectories()) {
|
||||
for (const family of collectFontsFromDir(dir)) {
|
||||
families.add(family);
|
||||
if (families.size >= MAX_FONT_RESULTS) break;
|
||||
}
|
||||
if (families.size >= MAX_FONT_RESULTS) break;
|
||||
}
|
||||
|
||||
cachedFonts = Array.from(families).sort((a, b) => a.localeCompare(b));
|
||||
return cachedFonts;
|
||||
}
|
||||
|
||||
function parseGoogleFontMetadata(value: unknown): string[] {
|
||||
if (!isRecord(value) || !Array.isArray(value.familyMetadataList)) return [];
|
||||
const families: string[] = [];
|
||||
for (const entry of value.familyMetadataList) {
|
||||
if (!isRecord(entry) || typeof entry.family !== "string") continue;
|
||||
families.push(entry.family);
|
||||
}
|
||||
return families;
|
||||
}
|
||||
|
||||
function stripGoogleJsonGuard(raw: string): string {
|
||||
const prefix = ")]}'";
|
||||
if (!raw.startsWith(prefix)) return raw;
|
||||
|
||||
let index = prefix.length;
|
||||
while (
|
||||
index < raw.length &&
|
||||
(raw[index] === " " ||
|
||||
raw[index] === "\n" ||
|
||||
raw[index] === "\r" ||
|
||||
raw[index] === "\t" ||
|
||||
raw[index] === "\f")
|
||||
) {
|
||||
index += 1;
|
||||
}
|
||||
|
||||
return raw.slice(index);
|
||||
}
|
||||
|
||||
async function listGoogleFontFamilies(): Promise<string[]> {
|
||||
if (cachedGoogleFonts) return cachedGoogleFonts;
|
||||
|
||||
const controller = new AbortController();
|
||||
const timer = setTimeout(() => controller.abort(), GOOGLE_FONTS_FETCH_TIMEOUT_MS);
|
||||
|
||||
try {
|
||||
const response = await fetch(GOOGLE_FONTS_METADATA_URL, { signal: controller.signal });
|
||||
if (!response.ok) {
|
||||
cachedGoogleFonts = GOOGLE_FONT_FALLBACKS;
|
||||
return cachedGoogleFonts;
|
||||
}
|
||||
const raw = await response.text();
|
||||
const jsonText = stripGoogleJsonGuard(raw);
|
||||
const families = parseGoogleFontMetadata(JSON.parse(jsonText));
|
||||
cachedGoogleFonts = families.length > 0 ? families : GOOGLE_FONT_FALLBACKS;
|
||||
} catch {
|
||||
cachedGoogleFonts = GOOGLE_FONT_FALLBACKS;
|
||||
} finally {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
|
||||
return cachedGoogleFonts;
|
||||
}
|
||||
|
||||
export function registerFontRoutes(api: Hono): void {
|
||||
api.get("/fonts", (c) => c.json({ fonts: listInstalledFontFamilies() }));
|
||||
api.get("/fonts/google", async (c) => c.json({ fonts: await listGoogleFontFamilies() }));
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { Hono } from "hono";
|
||||
import { mkdtempSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
|
||||
import { mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { registerPreviewRoutes } from "./preview";
|
||||
@@ -64,6 +64,85 @@ async function getPreviewSignature(projectDir: string): Promise<string> {
|
||||
}
|
||||
|
||||
describe("registerPreviewRoutes", () => {
|
||||
it("injects Studio GSAP motion manifest runtime into project preview", async () => {
|
||||
const projectDir = createProjectDir();
|
||||
writeFileSync(
|
||||
join(projectDir, "index.html"),
|
||||
"<!doctype html><html><head></head><body><div id='card'></div></body></html>",
|
||||
);
|
||||
const manifestDir = join(projectDir, ".hyperframes");
|
||||
mkdirSync(manifestDir, { recursive: true });
|
||||
writeFileSync(
|
||||
join(manifestDir, "studio-motion.json"),
|
||||
`{"version":1,"motions":[{"kind":"gsap-motion","target":{"sourceFile":"index.html","id":"card"},"start":0,"duration":1,"ease":"power2.out","from":{"y":32},"to":{"y":0}}]}`,
|
||||
);
|
||||
const app = new Hono();
|
||||
registerPreviewRoutes(app, createAdapter(projectDir));
|
||||
|
||||
const response = await app.request("http://localhost/projects/demo/preview");
|
||||
const html = await response.text();
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(html).toContain("__hfStudioMotionApply");
|
||||
expect(html).toContain("studio-motion");
|
||||
expect(html).toContain("gsap@3.15.0/dist/gsap.min.js");
|
||||
});
|
||||
|
||||
it("injects the GSAP CustomEase plugin when Studio motion uses a custom ease", async () => {
|
||||
const projectDir = createProjectDir();
|
||||
writeFileSync(
|
||||
join(projectDir, "index.html"),
|
||||
"<!doctype html><html><head></head><body><div id='card'></div></body></html>",
|
||||
);
|
||||
const manifestDir = join(projectDir, ".hyperframes");
|
||||
mkdirSync(manifestDir, { recursive: true });
|
||||
writeFileSync(
|
||||
join(manifestDir, "studio-motion.json"),
|
||||
`{"version":1,"motions":[{"kind":"gsap-motion","target":{"sourceFile":"index.html","id":"card"},"start":0,"duration":1,"ease":"studio-card-ease","customEase":{"id":"studio-card-ease","data":"M0,0 C0.18,0.9 0.32,1 1,1"},"from":{"y":32},"to":{"y":0}}]}`,
|
||||
);
|
||||
const app = new Hono();
|
||||
registerPreviewRoutes(app, createAdapter(projectDir));
|
||||
|
||||
const response = await app.request("http://localhost/projects/demo/preview");
|
||||
const html = await response.text();
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(html).toContain("gsap@3.15.0/dist/gsap.min.js");
|
||||
expect(html).toContain("gsap@3.15.0/dist/CustomEase.min.js");
|
||||
expect(html.indexOf("gsap.min.js")).toBeLessThan(html.indexOf("CustomEase.min.js"));
|
||||
expect(html.indexOf("CustomEase.min.js")).toBeLessThan(html.indexOf("__hfStudioMotionApply"));
|
||||
});
|
||||
|
||||
it("injects Studio GSAP motion runtime into sub-composition previews with the active source path", async () => {
|
||||
const projectDir = createProjectDir();
|
||||
mkdirSync(join(projectDir, "compositions"), { recursive: true });
|
||||
writeFileSync(
|
||||
join(projectDir, "index.html"),
|
||||
"<!doctype html><html><head></head><body></body></html>",
|
||||
);
|
||||
writeFileSync(
|
||||
join(projectDir, "compositions/scene.html"),
|
||||
`<template><section id="card" data-composition-id="scene" data-width="1280" data-height="720"></section></template>`,
|
||||
);
|
||||
const manifestDir = join(projectDir, ".hyperframes");
|
||||
mkdirSync(manifestDir, { recursive: true });
|
||||
writeFileSync(
|
||||
join(manifestDir, "studio-motion.json"),
|
||||
`{"version":1,"motions":[{"kind":"gsap-motion","target":{"sourceFile":"compositions/scene.html","id":"card"},"start":0,"duration":1,"ease":"power2.out","from":{"y":32},"to":{"y":0}}]}`,
|
||||
);
|
||||
const app = new Hono();
|
||||
registerPreviewRoutes(app, createAdapter(projectDir));
|
||||
|
||||
const response = await app.request(
|
||||
"http://localhost/projects/demo/preview/comp/compositions/scene.html",
|
||||
);
|
||||
const html = await response.text();
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(html).toContain("__hfStudioMotionApply");
|
||||
expect(html).toContain("compositions/scene.html");
|
||||
});
|
||||
|
||||
it("uses the adapter project signature when available", async () => {
|
||||
const projectDir = createProjectDir();
|
||||
const getProjectSignature = vi.fn(() => "cached-signature");
|
||||
@@ -93,6 +172,23 @@ describe("registerPreviewRoutes", () => {
|
||||
await expect(getPreviewSignature(projectDir)).resolves.not.toBe(firstSignature);
|
||||
});
|
||||
|
||||
it("updates the preview signature after Studio manifest edits", async () => {
|
||||
const projectDir = createProjectDir();
|
||||
const manifestDir = join(projectDir, ".hyperframes");
|
||||
mkdirSync(manifestDir, { recursive: true });
|
||||
const motionFile = join(manifestDir, "studio-motion.json");
|
||||
writeFileSync(motionFile, `{"version":1,"motions":[]}`);
|
||||
|
||||
const firstSignature = await getPreviewSignature(projectDir);
|
||||
|
||||
writeFileSync(
|
||||
motionFile,
|
||||
`{"version":1,"motions":[{"kind":"gsap-motion","target":{"sourceFile":"index.html","id":"card"},"start":0,"duration":1,"from":{"y":32},"to":{"y":0}}]}`,
|
||||
);
|
||||
|
||||
await expect(getPreviewSignature(projectDir)).resolves.not.toBe(firstSignature);
|
||||
});
|
||||
|
||||
it("skips symlinked files when creating the preview signature", async () => {
|
||||
const projectDir = createProjectDir();
|
||||
const firstSignature = await getPreviewSignature(projectDir);
|
||||
|
||||
@@ -1,13 +1,21 @@
|
||||
import type { Hono } from "hono";
|
||||
import { existsSync, readFileSync, statSync } from "node:fs";
|
||||
import { resolve } from "node:path";
|
||||
import { join, resolve } from "node:path";
|
||||
import { injectScriptsIntoHtml } from "../../compiler/htmlDocument.js";
|
||||
import type { StudioApiAdapter } from "../types.js";
|
||||
import { isSafePath } from "../helpers/safePath.js";
|
||||
import { getMimeType } from "../helpers/mime.js";
|
||||
import { buildSubCompositionHtml } from "../helpers/subComposition.js";
|
||||
import { createProjectSignature } from "../helpers/projectSignature.js";
|
||||
import {
|
||||
createStudioMotionRenderBodyScript,
|
||||
STUDIO_MOTION_PATH,
|
||||
} from "../helpers/studioMotionRenderScript.js";
|
||||
|
||||
const PROJECT_SIGNATURE_META = "hyperframes-project-signature";
|
||||
const GSAP_CDN_VERSION = "3.15.0";
|
||||
const GSAP_CDN_SCRIPT = `<script src="https://cdn.jsdelivr.net/npm/gsap@${GSAP_CDN_VERSION}/dist/gsap.min.js"></script>`;
|
||||
const GSAP_CUSTOM_EASE_CDN_SCRIPT = `<script src="https://cdn.jsdelivr.net/npm/gsap@${GSAP_CDN_VERSION}/dist/CustomEase.min.js"></script>`;
|
||||
|
||||
function resolveProjectSignature(adapter: StudioApiAdapter, projectDir: string): string {
|
||||
return adapter.getProjectSignature?.(projectDir) ?? createProjectSignature(projectDir);
|
||||
@@ -25,12 +33,115 @@ function injectProjectSignature(html: string, signature: string): string {
|
||||
return `${tag}\n${html}`;
|
||||
}
|
||||
|
||||
function readStudioMotionManifestContent(projectDir: string): string {
|
||||
const manifestPath = join(projectDir, STUDIO_MOTION_PATH);
|
||||
if (!existsSync(manifestPath)) return "";
|
||||
try {
|
||||
return readFileSync(manifestPath, "utf-8");
|
||||
} catch {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
function parseStudioMotionManifestContent(content: string): {
|
||||
hasMotion: boolean;
|
||||
hasCustomEase: boolean;
|
||||
} {
|
||||
try {
|
||||
const parsed = JSON.parse(content) as { motions?: Array<{ customEase?: unknown }> };
|
||||
const motions = Array.isArray(parsed.motions) ? parsed.motions : [];
|
||||
return {
|
||||
hasMotion: motions.length > 0,
|
||||
hasCustomEase: motions.some((motion) => Boolean(motion?.customEase)),
|
||||
};
|
||||
} catch {
|
||||
return { hasMotion: false, hasCustomEase: false };
|
||||
}
|
||||
}
|
||||
|
||||
function injectScriptTagIntoHead(html: string, scriptTag: string): string {
|
||||
if (html.includes("</head>")) return html.replace("</head>", `${scriptTag}\n</head>`);
|
||||
return `${scriptTag}\n${html}`;
|
||||
}
|
||||
|
||||
function htmlHasGsap(html: string): boolean {
|
||||
// Keep this heuristic conservative: if user source already loads GSAP, Studio does not add another copy.
|
||||
return (
|
||||
/<script\b[^>]*src=["'][^"']*gsap/i.test(html) ||
|
||||
/\/\*\s*inlined:.*gsap/i.test(html) ||
|
||||
/\b(GreenSock|_gsScope)\b/.test(html) ||
|
||||
/\bgsap\.(config|defaults|registerPlugin|version)\b/.test(html)
|
||||
);
|
||||
}
|
||||
|
||||
function htmlHasCustomEase(html: string): boolean {
|
||||
return (
|
||||
/<script\b[^>]*src=["'][^"']*CustomEase/i.test(html) ||
|
||||
/\bwindow\.CustomEase\b/.test(html) ||
|
||||
/\bCustomEase\s*=\s*/.test(html)
|
||||
);
|
||||
}
|
||||
|
||||
function injectStudioMotionDependencies(html: string, manifestContent: string): string {
|
||||
const manifest = parseStudioMotionManifestContent(manifestContent);
|
||||
if (!manifest.hasMotion) return html;
|
||||
let next = html;
|
||||
if (!htmlHasGsap(next)) next = injectScriptTagIntoHead(next, GSAP_CDN_SCRIPT);
|
||||
if (manifest.hasCustomEase && !htmlHasCustomEase(next)) {
|
||||
next = injectScriptTagIntoHead(next, GSAP_CUSTOM_EASE_CDN_SCRIPT);
|
||||
}
|
||||
return next;
|
||||
}
|
||||
|
||||
function injectStudioMotionScript(
|
||||
html: string,
|
||||
projectDir: string,
|
||||
activeCompositionPath: string,
|
||||
): string {
|
||||
const manifestContent = readStudioMotionManifestContent(projectDir);
|
||||
const script = createStudioMotionRenderBodyScript(manifestContent, {
|
||||
activeCompositionPath,
|
||||
});
|
||||
if (!script) return html;
|
||||
return injectScriptsIntoHtml(
|
||||
injectStudioMotionDependencies(html, manifestContent),
|
||||
[],
|
||||
[script],
|
||||
false,
|
||||
);
|
||||
}
|
||||
|
||||
function injectStudioPreviewAugmentations(
|
||||
html: string,
|
||||
adapter: StudioApiAdapter,
|
||||
projectDir: string,
|
||||
activeCompositionPath: string,
|
||||
): string {
|
||||
return injectStudioMotionScript(
|
||||
injectProjectSignature(html, resolveProjectSignature(adapter, projectDir)),
|
||||
projectDir,
|
||||
activeCompositionPath,
|
||||
);
|
||||
}
|
||||
|
||||
export function registerPreviewRoutes(api: Hono, adapter: StudioApiAdapter): void {
|
||||
const previewCacheHeaders = (etag: string) => ({
|
||||
"Cache-Control": "private, no-cache",
|
||||
ETag: etag,
|
||||
});
|
||||
|
||||
// Bundled composition preview
|
||||
api.get("/projects/:id/preview", async (c) => {
|
||||
const project = await adapter.resolveProject(c.req.param("id"));
|
||||
if (!project) return c.json({ error: "not found" }, 404);
|
||||
|
||||
const signature = resolveProjectSignature(adapter, project.dir);
|
||||
const etag = `"preview:${signature}"`;
|
||||
const ifNoneMatch = c.req.header("If-None-Match");
|
||||
if (ifNoneMatch === etag) {
|
||||
return new Response(null, { status: 304, headers: previewCacheHeaders(etag) });
|
||||
}
|
||||
|
||||
try {
|
||||
let bundled = await adapter.bundle(project.dir);
|
||||
if (!bundled) {
|
||||
@@ -56,16 +167,20 @@ export function registerPreviewRoutes(api: Hono, adapter: StudioApiAdapter): voi
|
||||
bundled = bundled.replace(/<head>/i, `<head><base href="${baseHref}">`);
|
||||
}
|
||||
|
||||
bundled = injectProjectSignature(bundled, resolveProjectSignature(adapter, project.dir));
|
||||
return c.html(bundled);
|
||||
bundled = injectStudioPreviewAugmentations(bundled, adapter, project.dir, "index.html");
|
||||
return c.html(bundled, 200, previewCacheHeaders(etag));
|
||||
} catch {
|
||||
const file = resolve(project.dir, "index.html");
|
||||
if (existsSync(file)) {
|
||||
return c.html(
|
||||
injectProjectSignature(
|
||||
injectStudioPreviewAugmentations(
|
||||
readFileSync(file, "utf-8"),
|
||||
resolveProjectSignature(adapter, project.dir),
|
||||
adapter,
|
||||
project.dir,
|
||||
"index.html",
|
||||
),
|
||||
200,
|
||||
previewCacheHeaders(etag),
|
||||
);
|
||||
}
|
||||
return c.text("not found", 404);
|
||||
@@ -76,6 +191,8 @@ export function registerPreviewRoutes(api: Hono, adapter: StudioApiAdapter): voi
|
||||
api.get("/projects/:id/preview/comp/*", async (c) => {
|
||||
const project = await adapter.resolveProject(c.req.param("id"));
|
||||
if (!project) return c.json({ error: "not found" }, 404);
|
||||
|
||||
const signature = resolveProjectSignature(adapter, project.dir);
|
||||
const compPath = decodeURIComponent(
|
||||
c.req.path.replace(`/projects/${project.id}/preview/comp/`, "").split("?")[0] ?? "",
|
||||
);
|
||||
@@ -87,10 +204,21 @@ export function registerPreviewRoutes(api: Hono, adapter: StudioApiAdapter): voi
|
||||
) {
|
||||
return c.text("not found", 404);
|
||||
}
|
||||
|
||||
const etag = `"comp:${compPath}:${signature}"`;
|
||||
const ifNoneMatch = c.req.header("If-None-Match");
|
||||
if (ifNoneMatch === etag) {
|
||||
return new Response(null, { status: 304, headers: previewCacheHeaders(etag) });
|
||||
}
|
||||
|
||||
const baseHref = `/api/projects/${project.id}/preview/`;
|
||||
const html = buildSubCompositionHtml(project.dir, compPath, adapter.runtimeUrl, baseHref);
|
||||
let html = buildSubCompositionHtml(project.dir, compPath, adapter.runtimeUrl, baseHref);
|
||||
if (!html) return c.text("not found", 404);
|
||||
return c.html(injectProjectSignature(html, resolveProjectSignature(adapter, project.dir)));
|
||||
return c.html(
|
||||
injectStudioPreviewAugmentations(html, adapter, project.dir, compPath),
|
||||
200,
|
||||
previewCacheHeaders(etag),
|
||||
);
|
||||
});
|
||||
|
||||
// Static asset serving (with range request support for audio/video seeking)
|
||||
@@ -101,11 +229,25 @@ export function registerPreviewRoutes(api: Hono, adapter: StudioApiAdapter): voi
|
||||
c.req.path.replace(`/projects/${project.id}/preview/`, "").split("?")[0] ?? "",
|
||||
);
|
||||
const file = resolve(project.dir, subPath);
|
||||
if (!isSafePath(project.dir, file) || !existsSync(file) || !statSync(file).isFile()) {
|
||||
const stat = existsSync(file) ? statSync(file) : null;
|
||||
if (!isSafePath(project.dir, file) || !stat?.isFile()) {
|
||||
return c.text("not found", 404);
|
||||
}
|
||||
const contentType = getMimeType(subPath);
|
||||
const isText = /\.(html|css|js|json|svg|txt|md)$/i.test(subPath);
|
||||
|
||||
const etag = `"${stat.mtimeMs.toString(36)}-${stat.size.toString(36)}"`;
|
||||
const cacheHeaders: Record<string, string> = isText
|
||||
? { "Cache-Control": "no-store" }
|
||||
: { "Cache-Control": "private, max-age=3600, must-revalidate", ETag: etag };
|
||||
|
||||
if (!isText) {
|
||||
const ifNoneMatch = c.req.header("If-None-Match");
|
||||
if (ifNoneMatch === etag) {
|
||||
return new Response(null, { status: 304, headers: cacheHeaders });
|
||||
}
|
||||
}
|
||||
|
||||
const buffer: Buffer = isText
|
||||
? Buffer.from(readFileSync(file, "utf-8"), "utf-8")
|
||||
: readFileSync(file);
|
||||
@@ -123,6 +265,7 @@ export function registerPreviewRoutes(api: Hono, adapter: StudioApiAdapter): voi
|
||||
return new Response(new Uint8Array(buffer.slice(start, safeEnd + 1)), {
|
||||
status: 206,
|
||||
headers: {
|
||||
...cacheHeaders,
|
||||
"Content-Type": contentType,
|
||||
"Content-Range": `bytes ${start}-${safeEnd}/${totalSize}`,
|
||||
"Accept-Ranges": "bytes",
|
||||
@@ -134,6 +277,7 @@ export function registerPreviewRoutes(api: Hono, adapter: StudioApiAdapter): voi
|
||||
|
||||
return new Response(new Uint8Array(buffer), {
|
||||
headers: {
|
||||
...cacheHeaders,
|
||||
"Content-Type": contentType,
|
||||
"Accept-Ranges": "bytes",
|
||||
"Content-Length": String(totalSize),
|
||||
|
||||
@@ -25,6 +25,6 @@ export function registerProjectRoutes(api: Hono, adapter: StudioApiAdapter): voi
|
||||
const project = await adapter.resolveProject(c.req.param("id"));
|
||||
if (!project) return c.json({ error: "not found" }, 404);
|
||||
const files = walkDir(project.dir);
|
||||
return c.json({ id: project.id, files });
|
||||
return c.json({ id: project.id, dir: project.dir, title: project.title, files });
|
||||
});
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ import { Hono } from "hono";
|
||||
import { mkdtempSync, rmSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { VALID_CANVAS_RESOLUTIONS } from "../../core.types";
|
||||
import { registerRenderRoutes } from "./render";
|
||||
import type { StudioApiAdapter } from "../types";
|
||||
|
||||
@@ -98,8 +99,8 @@ describe("POST /projects/:id/render — outputResolution forwarding", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("accepts each of the four canonical preset values", async () => {
|
||||
for (const preset of ["landscape", "portrait", "landscape-4k", "portrait-4k"] as const) {
|
||||
it("accepts each canonical preset value", async () => {
|
||||
for (const preset of VALID_CANVAS_RESOLUTIONS) {
|
||||
const spy = vi.fn();
|
||||
const { app, cleanup } = buildApp(spy);
|
||||
try {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { Hono } from "hono";
|
||||
import { mkdtempSync, rmSync } from "node:fs";
|
||||
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { registerThumbnailRoutes } from "./thumbnail";
|
||||
@@ -94,4 +94,108 @@ describe("registerThumbnailRoutes", () => {
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("forwards selector occurrence indexes to thumbnail generation", async () => {
|
||||
const adapter = createAdapter();
|
||||
const app = new Hono();
|
||||
registerThumbnailRoutes(app, adapter);
|
||||
|
||||
const response = await app.request(
|
||||
"http://localhost/projects/demo/thumbnail/index.html?t=1.2&selector=.card&selectorIndex=2",
|
||||
);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(adapter.generateThumbnail).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
selector: ".card",
|
||||
selectorIndex: 2,
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("keeps url thumbnail versions separated in the disk cache", async () => {
|
||||
const adapter = createAdapter();
|
||||
const app = new Hono();
|
||||
registerThumbnailRoutes(app, adapter);
|
||||
|
||||
await app.request("http://localhost/projects/demo/thumbnail/index.html?t=2&v=old");
|
||||
await app.request("http://localhost/projects/demo/thumbnail/index.html?t=2&v=old");
|
||||
await app.request("http://localhost/projects/demo/thumbnail/index.html?t=2&v=new");
|
||||
|
||||
expect(adapter.generateThumbnail).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it("keeps changed composition dimensions separated in the disk cache", async () => {
|
||||
const adapter = createAdapter();
|
||||
const project = await adapter.resolveProject("demo");
|
||||
if (!project) throw new Error("missing project");
|
||||
const app = new Hono();
|
||||
registerThumbnailRoutes(app, adapter);
|
||||
|
||||
const indexPath = join(project.dir, "index.html");
|
||||
writeFileSync(indexPath, `<div data-composition-id="main" data-width="640" data-height="360">`);
|
||||
await app.request("http://localhost/projects/demo/thumbnail/index.html?t=2&v=test");
|
||||
|
||||
writeFileSync(
|
||||
indexPath,
|
||||
`<div data-composition-id="main" data-width="1280" data-height="720">`,
|
||||
);
|
||||
await app.request("http://localhost/projects/demo/thumbnail/index.html?t=2&v=test");
|
||||
|
||||
expect(adapter.generateThumbnail).toHaveBeenCalledTimes(2);
|
||||
expect(adapter.generateThumbnail).toHaveBeenLastCalledWith(
|
||||
expect.objectContaining({
|
||||
width: 1280,
|
||||
height: 720,
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("keeps changed studio manual edits separated in the disk cache", async () => {
|
||||
const adapter = createAdapter();
|
||||
const project = await adapter.resolveProject("demo");
|
||||
if (!project) throw new Error("missing project");
|
||||
const app = new Hono();
|
||||
registerThumbnailRoutes(app, adapter);
|
||||
|
||||
const indexPath = join(project.dir, "index.html");
|
||||
writeFileSync(indexPath, `<div data-composition-id="main" data-width="640" data-height="360">`);
|
||||
const manualEditsDir = join(project.dir, ".hyperframes");
|
||||
mkdirSync(manualEditsDir, { recursive: true });
|
||||
const manualEditsPath = join(manualEditsDir, "studio-manual-edits.json");
|
||||
writeFileSync(manualEditsPath, `{"version":1,"edits":[]}`);
|
||||
|
||||
await app.request("http://localhost/projects/demo/thumbnail/index.html?t=2&v=test");
|
||||
writeFileSync(
|
||||
manualEditsPath,
|
||||
`{"version":1,"edits":[{"kind":"rotation","target":{"sourceFile":"index.html","id":"card"},"angle":30}]}`,
|
||||
);
|
||||
await app.request("http://localhost/projects/demo/thumbnail/index.html?t=2&v=test");
|
||||
|
||||
expect(adapter.generateThumbnail).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it("keeps changed studio motion separated in the disk cache", async () => {
|
||||
const adapter = createAdapter();
|
||||
const project = await adapter.resolveProject("demo");
|
||||
if (!project) throw new Error("missing project");
|
||||
const app = new Hono();
|
||||
registerThumbnailRoutes(app, adapter);
|
||||
|
||||
const indexPath = join(project.dir, "index.html");
|
||||
writeFileSync(indexPath, `<div data-composition-id="main" data-width="640" data-height="360">`);
|
||||
const motionDir = join(project.dir, ".hyperframes");
|
||||
mkdirSync(motionDir, { recursive: true });
|
||||
const motionPath = join(motionDir, "studio-motion.json");
|
||||
writeFileSync(motionPath, `{"version":1,"motions":[]}`);
|
||||
|
||||
await app.request("http://localhost/projects/demo/thumbnail/index.html?t=2&v=test");
|
||||
writeFileSync(
|
||||
motionPath,
|
||||
`{"version":1,"motions":[{"kind":"gsap-motion","target":{"sourceFile":"index.html","id":"card"},"start":0,"duration":1,"ease":"power2.out","from":{"y":32},"to":{"y":0}}]}`,
|
||||
);
|
||||
await app.request("http://localhost/projects/demo/thumbnail/index.html?t=2&v=test");
|
||||
|
||||
expect(adapter.generateThumbnail).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
import type { Hono } from "hono";
|
||||
import { existsSync, readFileSync, writeFileSync, mkdirSync } from "node:fs";
|
||||
import { existsSync, readFileSync, writeFileSync, mkdirSync, statSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { createHash } from "node:crypto";
|
||||
import type { StudioApiAdapter } from "../types.js";
|
||||
import { STUDIO_MANUAL_EDITS_PATH } from "../helpers/manualEditsRenderScript.js";
|
||||
import { STUDIO_MOTION_PATH } from "../helpers/studioMotionRenderScript.js";
|
||||
|
||||
const THUMBNAIL_CACHE_VERSION = "v3";
|
||||
const THUMBNAIL_CACHE_VERSION = "v4";
|
||||
|
||||
export function registerThumbnailRoutes(api: Hono, adapter: StudioApiAdapter): void {
|
||||
api.get("/projects/:id/thumbnail/*", async (c) => {
|
||||
@@ -27,13 +30,19 @@ export function registerThumbnailRoutes(api: Hono, adapter: StudioApiAdapter): v
|
||||
const selector = url.searchParams.get("selector") || undefined;
|
||||
const format = url.searchParams.get("format") === "png" ? "png" : "jpeg";
|
||||
const contentType = format === "png" ? "image/png" : "image/jpeg";
|
||||
const rawSelectorIndex = Number.parseInt(url.searchParams.get("selectorIndex") || "0", 10);
|
||||
const selectorIndex =
|
||||
Number.isFinite(rawSelectorIndex) && rawSelectorIndex > 0 ? rawSelectorIndex : undefined;
|
||||
const urlVersion = url.searchParams.get("v") || "";
|
||||
|
||||
// Determine composition dimensions from HTML
|
||||
let compW = vpWidth || 1920;
|
||||
let compH = vpHeight || 1080;
|
||||
let sourceMtime = 0;
|
||||
if (!vpWidth) {
|
||||
const htmlFile = join(project.dir, compPath);
|
||||
if (existsSync(htmlFile)) {
|
||||
sourceMtime = Math.round(statSync(htmlFile).mtimeMs);
|
||||
const html = readFileSync(htmlFile, "utf-8");
|
||||
const wMatch = html.match(/data-width=["'](\d+)["']/);
|
||||
const hMatch = html.match(/data-height=["'](\d+)["']/);
|
||||
@@ -41,6 +50,20 @@ export function registerThumbnailRoutes(api: Hono, adapter: StudioApiAdapter): v
|
||||
if (hMatch?.[1]) compH = parseInt(hMatch[1]);
|
||||
}
|
||||
}
|
||||
const manualEditsFile = join(project.dir, STUDIO_MANUAL_EDITS_PATH);
|
||||
let manualEditsKey = "";
|
||||
if (existsSync(manualEditsFile)) {
|
||||
const manualEditsContent = readFileSync(manualEditsFile, "utf-8");
|
||||
manualEditsKey = `_${createHash("sha1").update(manualEditsContent).digest("hex").slice(0, 16)}`;
|
||||
sourceMtime = Math.max(sourceMtime, Math.round(statSync(manualEditsFile).mtimeMs));
|
||||
}
|
||||
const motionFile = join(project.dir, STUDIO_MOTION_PATH);
|
||||
let motionKey = "";
|
||||
if (existsSync(motionFile)) {
|
||||
const motionContent = readFileSync(motionFile, "utf-8");
|
||||
motionKey = `_${createHash("sha1").update(motionContent).digest("hex").slice(0, 16)}`;
|
||||
sourceMtime = Math.max(sourceMtime, Math.round(statSync(motionFile).mtimeMs));
|
||||
}
|
||||
|
||||
const previewUrl =
|
||||
compPath === "index.html"
|
||||
@@ -50,9 +73,12 @@ export function registerThumbnailRoutes(api: Hono, adapter: StudioApiAdapter): v
|
||||
// Cache
|
||||
const cacheDir = join(project.dir, ".thumbnails");
|
||||
const selectorKey = selector
|
||||
? `_${selector.replace(/[^a-zA-Z0-9_-]+/g, "_").slice(0, 80)}`
|
||||
? `_${selector.replace(/[^a-zA-Z0-9_-]+/g, "_").slice(0, 80)}_${selectorIndex ?? 0}`
|
||||
: "";
|
||||
const cacheKey = `${THUMBNAIL_CACHE_VERSION}_${format}_${compPath.replace(/\//g, "_")}_${seekTime.toFixed(2)}${selectorKey}.${format === "png" ? "png" : "jpg"}`;
|
||||
const urlVersionKey = urlVersion
|
||||
? `_${urlVersion.replace(/[^a-zA-Z0-9_-]+/g, "_").slice(0, 32)}`
|
||||
: "";
|
||||
const cacheKey = `${THUMBNAIL_CACHE_VERSION}${urlVersionKey}${manualEditsKey}${motionKey}_${format}_${compPath.replace(/\//g, "_")}_${compW}x${compH}_${sourceMtime}_${seekTime.toFixed(2)}${selectorKey}.${format === "png" ? "png" : "jpg"}`;
|
||||
const cachePath = join(cacheDir, cacheKey);
|
||||
if (existsSync(cachePath)) {
|
||||
return new Response(new Uint8Array(readFileSync(cachePath)), {
|
||||
@@ -70,6 +96,7 @@ export function registerThumbnailRoutes(api: Hono, adapter: StudioApiAdapter): v
|
||||
previewUrl,
|
||||
selector,
|
||||
format,
|
||||
selectorIndex,
|
||||
});
|
||||
if (!buffer) {
|
||||
return c.json({ error: "Thumbnail generation returned null" }, 500);
|
||||
|
||||
@@ -90,6 +90,7 @@ export interface StudioApiAdapter {
|
||||
previewUrl: string;
|
||||
selector?: string;
|
||||
format?: "jpeg" | "png";
|
||||
selectorIndex?: number;
|
||||
}) => Promise<Buffer | null>;
|
||||
|
||||
/** Optional: resolve session ID to project (multi-project mode). */
|
||||
|
||||
Reference in New Issue
Block a user