Skip to content

Open source CapCut alternative.

TypeScript

feat(chatgpt): the editor card asks for fullscreen and hands over the composer inset

David Le87a05d759cdf
diff --git a/site/scripts/eval-chatgpt-widget.ts b/site/scripts/eval-chatgpt-widget.ts
index 44e81727..a3feadf7 100644
--- a/site/scripts/eval-chatgpt-widget.ts
+++ b/site/scripts/eval-chatgpt-widget.ts
@@ -14,7 +14,7 @@ const server = Bun.serve({ port: 0, fetch(request) {
const url = new URL(request.url);
if (url.pathname === "/widget") return new Response(widgetHtml(url.origin), { headers: { "Content-Type": "text/html" } });
if (url.pathname === "/preview.mp4") return new Response(Bun.file(video));
- if (url.pathname === "/embed") return new Response(`<!doctype html><title>editor</title><p id="editor">Editor for ${url.searchParams.get("project")} via ${url.searchParams.get("code")}</p>`, { headers: { "Content-Type": "text/html" } });
+ if (url.pathname === "/embed") return new Response(`<!doctype html><title>editor</title><p id="editor">Editor for ${url.searchParams.get("project")} via ${url.searchParams.get("code")}</p><p id="inset"></p><script>addEventListener('message', (e) => { if (e.source === parent && e.data.type === 'donkeycut:host-inset') document.getElementById('inset').textContent = String(e.data.insetBottom); }); parent.postMessage({ type: 'donkeycut:host-inset?' }, '*');</script>`, { headers: { "Content-Type": "text/html" } });
if (url.pathname.startsWith("/clients/chatgpt/")) return new Response(Bun.file(path.join(root, "public/clients/chatgpt", path.basename(url.pathname))));
return new Response(hostHtml, { headers: { "Content-Type": "text/html" } });
} });
@@ -30,7 +30,7 @@ const result = (data, playback = null, editor = null) => dropNulls({content:[{ty
const reply = (id, result) => frame.contentWindow.postMessage({jsonrpc:'2.0',id,result},'*');
window.addEventListener('message', ({source,data}) => {
if(source !== frame.contentWindow || !data.method) return;
- if(data.method === 'ui/initialize') reply(data.id,{protocolVersion:data.params.protocolVersion,hostInfo:{name:'fixture',version:'1'},hostCapabilities:{serverTools:{},openLinks:{}},hostContext:{theme:'light',availableDisplayModes:['inline','fullscreen']}});
+ if(data.method === 'ui/initialize') reply(data.id,{protocolVersion:data.params.protocolVersion,hostInfo:{name:'fixture',version:'1'},hostCapabilities:{serverTools:{},openLinks:{}},hostContext:{theme:'light',displayMode:'inline',availableDisplayModes:['inline','fullscreen'],safeAreaInsets:{top:0,right:0,bottom:120,left:0}}});
if(data.method === 'ui/notifications/initialized') frame.contentWindow.postMessage({jsonrpc:'2.0',method:'ui/notifications/tool-result',params:result(view(false))},'*');
if(data.method === 'tools/call') {
const name = data.params.name; window.calls.push(name);
@@ -43,7 +43,7 @@ window.addEventListener('message', ({source,data}) => {
}
}
if(data.method === 'ui/open-link') { window.links.push(data.params.url); reply(data.id,{}); }
- if(data.method === 'ui/request-display-mode') { window.modes.push(data.params.mode); reply(data.id,{mode:data.params.mode}); }
+ if(data.method === 'ui/request-display-mode') { window.modes.push(data.params.mode); reply(data.id,{mode:window.modes.length > 1 ? data.params.mode : 'inline'}); }
});
</script></body></html>`;
const browser = await chromium.launch({ headless: true });
@@ -78,8 +78,13 @@ try {
await page.evaluate(() => { (window as unknown as { editable: boolean }).editable = true; });
await app.getByRole("button", { name: "Launch film" }).click();
await app.frameLocator("iframe.editor").locator("#editor").waitFor();
- assert.equal(await app.locator("button").count(), 0, "the editor card has no buttons of its own");
- assert.deepEqual(await page.evaluate(() => (window as unknown as { modes: string[] }).modes), ["fullscreen"], "the editor asks for the whole window");
+ assert.deepEqual(await page.evaluate(() => (window as unknown as { modes: string[] }).modes), ["fullscreen"], "the editor asks for the whole window on open");
+ assert.equal(await app.locator("button").count(), 1, "inline, the editor card keeps one control: fullscreen");
+ await app.frameLocator("iframe.editor").locator("#inset").filter({ hasText: /^0$/ }).waitFor();
+ await app.getByRole("button", { name: "Fullscreen" }).click();
+ await app.locator("button.fullscreen").waitFor({ state: "detached" });
+ await app.frameLocator("iframe.editor").locator("#inset").filter({ hasText: /^120$/ }).waitFor();
+ assert.deepEqual(await page.evaluate(() => (window as unknown as { modes: string[] }).modes), ["fullscreen", "fullscreen"], "the button asks again and the granted mode hides it");
await page.screenshot({ path: "/tmp/donkey-chatgpt-widget-editor.png" });
assert.deepEqual(errors, []);
console.log("PASS: project selection, repeated polling, native playback, URL recovery, hidden pause, Open in Donkey Cut, editor in the card, decoder teardown, lazy HLS.");
diff --git a/site/src/clients/chatgpt/ui/PreviewApp.tsx b/site/src/clients/chatgpt/ui/PreviewApp.tsx
index 8ac9fda1..c67a9952 100644
--- a/site/src/clients/chatgpt/ui/PreviewApp.tsx
+++ b/site/src/clients/chatgpt/ui/PreviewApp.tsx
@@ -1,17 +1,20 @@
import { ArtifactVideo } from "@donkeycut/artifact-player/ArtifactVideo";
+import { useEffect, useRef } from "react";
import { useProjectApp } from "./useProjectApp";
import "./preview.css";
export function PreviewApp() {
- const { ready, view, playback, download, editor, error, busy, visible, run, playbackFailed, openDonkey, openDownload } = useProjectApp();
+ const { ready, view, playback, download, editor, fullscreen, hostInset, error, busy, visible, run, playbackFailed, openDonkey, openDownload, requestFullscreen } = useProjectApp();
const project = view?.project;
const preview = view?.preview;
const rendering = preview?.status === "queued" || preview?.status === "running";
const exp = view?.export;
const exporting = exp?.status === "queued" || exp?.status === "running";
- // The editor is the card: its own chrome carries every control.
- if (editor && project) return <main className="editing">
- <iframe className="editor" src={editor.url} title={`Editing ${project.name}`} allow="fullscreen; clipboard-read; clipboard-write" />
+ // The editor is the card: its own chrome carries every control. Inline, one
+ // control asks the host for the whole window.
+ if (editor && project) return <main className="editing" data-mode={fullscreen ? "fullscreen" : "inline"}>
+ {!fullscreen && <button className="fullscreen" onClick={() => void requestFullscreen()} aria-label="Fullscreen">⤢</button>}
+ <EditorFrame url={editor.url} name={project.name} inset={hostInset} />
</main>;
return <main aria-busy={busy}>
<header><span className="brand">Donkey Cut</span><span>Cloud projects</span></header>
@@ -52,3 +55,20 @@ export function PreviewApp() {
</>}
</main>;
}
+
+/** The editor, framed. It learns how tall the host's bottom overlay is when
+ * it loads, whenever that changes, and whenever it asks. */
+function EditorFrame({ url, name, inset }: { url: string; name: string; inset: number }) {
+ const frame = useRef<HTMLIFrameElement>(null);
+ const origin = new URL(url).origin;
+ const tell = () => frame.current?.contentWindow?.postMessage({ type: "donkeycut:host-inset", insetBottom: inset }, origin);
+ useEffect(tell, [inset, origin]);
+ useEffect(() => {
+ const onMessage = (event: MessageEvent) => {
+ if (event.origin === origin && event.source === frame.current?.contentWindow && event.data?.type === "donkeycut:host-inset?") tell();
+ };
+ window.addEventListener("message", onMessage);
+ return () => window.removeEventListener("message", onMessage);
+ });
+ return <iframe ref={frame} className="editor" src={url} onLoad={tell} title={`Editing ${name}`} allow="fullscreen; clipboard-read; clipboard-write" />;
+}
diff --git a/site/src/clients/chatgpt/ui/preview.css b/site/src/clients/chatgpt/ui/preview.css
index cbc91c61..82cf0137 100644
--- a/site/src/clients/chatgpt/ui/preview.css
+++ b/site/src/clients/chatgpt/ui/preview.css
@@ -1,4 +1,4 @@
:root{font:14px/1.5 system-ui,sans-serif;color:#20201e;background:#faf9f7;color-scheme:light;--line:#dddcd6;--muted:#66645e;--panel:#fff}
:root[data-theme=dark]{color:#eeede8;background:#20201e;color-scheme:dark;--line:#45443f;--muted:#b6b3a8;--panel:#292925}
*{box-sizing:border-box}body{margin:0}main{padding:20px;max-width:840px;margin:auto}header{display:flex;justify-content:space-between;color:var(--muted);font-size:12px}.brand{font-weight:800;font-size:16px;color:inherit}h1{font-size:22px;line-height:1.2;margin:24px 0 16px;overflow-wrap:anywhere}video{width:100%;max-height:65vh;display:block;border-radius:12px;background:#000}button{font:inherit;color:inherit;background:var(--panel);border:1px solid var(--line);border-radius:10px;padding:10px 14px;cursor:pointer}button:hover{filter:brightness(.95)}button:disabled{opacity:.5;cursor:default}button:focus-visible{outline:2px solid #5d80cc;outline-offset:3px}.primary{background:#eac36a;color:#242018;border-color:#eac36a}nav{display:flex;flex-wrap:wrap;gap:8px;margin-top:16px}.projects{display:grid;gap:8px}.projects button{display:flex;justify-content:space-between;gap:16px;text-align:left}.projects button span:first-child{overflow-wrap:anywhere;min-width:0}.muted{color:var(--muted);font-size:12px}.error{color:#ba453b}progress{width:100%;accent-color:#d8a743}p{overflow-wrap:anywhere}
-.editing{padding:0;max-width:none}.editor{display:block;width:100%;height:100vh;min-height:600px;border:0;background:#000}
+.editing{padding:0;max-width:none;position:relative}.editor{display:block;width:100%;height:560px;border:0;background:#000}[data-mode=fullscreen] .editor{height:100vh}.fullscreen{position:absolute;top:8px;right:8px;z-index:1;width:32px;height:32px;padding:0;border-radius:8px;border:1px solid var(--line);background:var(--panel);color:inherit;font-size:16px;line-height:1;cursor:pointer;opacity:.85}.fullscreen:hover{opacity:1}
diff --git a/site/src/clients/chatgpt/ui/useProjectApp.ts b/site/src/clients/chatgpt/ui/useProjectApp.ts
index 1b020d8b..ff7fbf99 100644
--- a/site/src/clients/chatgpt/ui/useProjectApp.ts
+++ b/site/src/clients/chatgpt/ui/useProjectApp.ts
@@ -13,6 +13,8 @@ export function useProjectApp() {
const [playback, setPlayback] = useState<Playback | null>(null);
const [download, setDownload] = useState<Download | null>(null);
const [editor, setEditor] = useState<Editor | null>(null);
+ const [fullscreen, setFullscreen] = useState(false);
+ const [hostInset, setHostInset] = useState(0);
const [error, setError] = useState<string | null>(null);
const [busy, setBusy] = useState(false);
const [visible, setVisible] = useState(!document.hidden);
@@ -48,10 +50,23 @@ export function useProjectApp() {
setBusy(false);
try { receive(result); } catch (e) { setError((e as Error).message); }
};
- app.onhostcontextchanged = ({ theme }) => { if (theme) document.documentElement.dataset.theme = theme; };
+ // ChatGPT overlays its composer on the bottom of a fullscreen card and
+ // reports the covered band as a safe-area inset; the editor frame gets
+ // the number and keeps its own bottom controls above the band.
+ const applyContext = (context: ReturnType<App["getHostContext"]>) => {
+ if (!context) return;
+ if (context.theme) document.documentElement.dataset.theme = context.theme;
+ const full = context.displayMode === "fullscreen";
+ if (context.displayMode) setFullscreen(full);
+ // Measured from ChatGPT's desktop fullscreen when the host reports no inset.
+ setHostInset(full ? (context.safeAreaInsets?.bottom ?? 120) : 0);
+ };
+ app.onhostcontextchanged = (context) => { applyContext({ ...app.getHostContext(), ...context }); };
void app.connect().then(() => {
if (!live) return;
- document.documentElement.dataset.theme = app.getHostContext()?.theme ?? "light";
+ const context = app.getHostContext();
+ console.debug("[donkeycut] host context", context);
+ applyContext(context ?? { theme: "light" });
setReady(true);
}).catch(() => { if (live) setError("Connect this preview from ChatGPT to continue."); });
const visibility = () => setVisible(!document.hidden);
@@ -118,11 +133,16 @@ export function useProjectApp() {
const openDownload = () => { if (download) void openLink(download.url, "Could not open the download."); };
// The editor wants the whole window; the host grants what it supports.
- const requestMode = useCallback(async (mode: "fullscreen") => {
+ const requestFullscreen = useCallback(async () => {
const app = appRef.current;
- if (!app || !app.getHostContext()?.availableDisplayModes?.includes(mode)) return;
- try { await app.requestDisplayMode({ mode }); } catch { /* the inline card still works */ }
+ if (!app) return;
+ try {
+ const result = await app.requestDisplayMode({ mode: "fullscreen" });
+ const full = result.mode === "fullscreen";
+ setFullscreen(full);
+ setHostInset(full ? (app.getHostContext()?.safeAreaInsets?.bottom ?? 120) : 0);
+ } catch { /* the inline card still works */ }
}, []);
- useEffect(() => { if (editor) void requestMode("fullscreen"); }, [editor, requestMode]);
- return { ready, view, playback, download, editor, error, busy, visible, run, playbackFailed, openDonkey, openDownload };
+ useEffect(() => { if (editor) void requestFullscreen(); }, [editor, requestFullscreen]);
+ return { ready, view, playback, download, editor, fullscreen, hostInset, error, busy, visible, run, playbackFailed, openDonkey, openDownload, requestFullscreen };
}