Skip to content

Open source CapCut alternative.

TypeScript

fix(cut): keep copying when the system clipboard refuses

David Le5a3b7009fcba
diff --git a/site/src/cut/lib/cutClipboard.test.ts b/site/src/cut/lib/cutClipboard.test.ts
index 41677f4a..12899779 100644
--- a/site/src/cut/lib/cutClipboard.test.ts
+++ b/site/src/cut/lib/cutClipboard.test.ts
@@ -1,6 +1,13 @@
import { afterAll, beforeEach, describe, expect, test } from "bun:test";
-import { clipboardHtml, pasteCutPayload, payloadAssets, payloadFromHtml, type CutClipboardPayload } from "./cutClipboard";
+import {
+ clipboardHtml,
+ pasteCutPayload,
+ payloadAssets,
+ payloadFromHtml,
+ writeCutClipboard,
+ type CutClipboardPayload,
+} from "./cutClipboard";
import { richDoc } from "./fixtures/richDoc";
import { EMPTY_LIBRARY } from "./assetRef";
import { setPlayhead } from "./playhead";
@@ -66,6 +73,32 @@ describe("the clipboard flavor", () => {
});
});
+describe("writing the system clipboard", () => {
+ const saved = Object.getOwnPropertyDescriptor(globalThis, "navigator");
+ const savedItem = (globalThis as { ClipboardItem?: unknown }).ClipboardItem;
+ const withClipboard = (write: () => Promise<void>) => {
+ Object.defineProperty(globalThis, "navigator", { value: { clipboard: { write, writeText: write } }, configurable: true });
+ Object.assign(globalThis, { ClipboardItem: class {} });
+ };
+ afterAll(() => {
+ if (saved) Object.defineProperty(globalThis, "navigator", saved);
+ Object.assign(globalThis, { ClipboardItem: savedItem });
+ });
+
+ test("a page the clipboard refuses keeps the copy in its own tab", async () => {
+ // The ChatGPT card's frame blocks the Clipboard API by permissions policy.
+ withClipboard(() => Promise.reject(new DOMException("blocked by a permissions policy", "NotAllowedError")));
+
+ expect(await writeCutClipboard("@c0", payloadOf(["c0"]))).toBeUndefined();
+ });
+
+ test("any other clipboard failure still surfaces", async () => {
+ withClipboard(() => Promise.reject(new DOMException("bad blob", "DataError")));
+
+ await expect(writeCutClipboard("@c0", payloadOf(["c0"]))).rejects.toThrow("bad blob");
+ });
+});
+
describe("pasting into another project", () => {
beforeEach(() => {
copies.length = 0;
diff --git a/site/src/cut/lib/cutClipboard.ts b/site/src/cut/lib/cutClipboard.ts
index b05c724e..a48be6ca 100644
--- a/site/src/cut/lib/cutClipboard.ts
+++ b/site/src/cut/lib/cutClipboard.ts
@@ -76,23 +76,35 @@ export function payloadFromHtml(html: string | null | undefined): CutClipboardPa
}
}
+// What a browser throws when the page may not write the clipboard: the
+// ChatGPT card's frame blocks it by permissions policy.
+const CLIPBOARD_REFUSED = "NotAllowedError";
+
/** Put a copy on the system clipboard: the tokens as text, the payload in
* the HTML flavor. A browser without `ClipboardItem` takes the text alone,
- * and the copy pastes within its own tab from the timeline clipboard. */
+ * and the copy pastes within its own tab from the timeline clipboard, as it
+ * does on a page the clipboard refuses. */
export async function writeCutClipboard(text: string, payload: CutClipboardPayload | null): Promise<void> {
const clip = navigator.clipboard;
if (!clip) return;
- if (payload && typeof ClipboardItem !== "undefined" && typeof clip.write === "function") {
- const html = clipboardHtml(text, payload);
- await clip.write([
- new ClipboardItem({
- "text/plain": new Blob([text], { type: "text/plain" }),
- "text/html": new Blob([html], { type: "text/html" }),
- }),
- ]);
- return;
+ try {
+ if (payload && typeof ClipboardItem !== "undefined" && typeof clip.write === "function") {
+ const html = clipboardHtml(text, payload);
+ await clip.write([
+ new ClipboardItem({
+ "text/plain": new Blob([text], { type: "text/plain" }),
+ "text/html": new Blob([html], { type: "text/html" }),
+ }),
+ ]);
+ return;
+ }
+ await clip.writeText(text);
+ } catch (err) {
+ if (err instanceof DOMException && err.name === CLIPBOARD_REFUSED) {
+ return;
+ }
+ throw err;
}
- await clip.writeText(text);
}
/** Copy timeline selection to both the editor clipboard and the system clipboard. */