Open source CapCut alternative.
TypeScript
feat(site): the library page takes pasted files and links
David Le73a7b9f5f44b
diff --git a/site/src/cut/components/LibraryView.tsx b/site/src/cut/components/LibraryView.tsxindex 091cf0f9..93eba24f 100644--- a/site/src/cut/components/LibraryView.tsx+++ b/site/src/cut/components/LibraryView.tsx@@ -86,7 +86,8 @@ import {type LibraryTemplateItem,type LibraryData,} from "@/cut/lib/library";-import { normalizeLink } from "@/cut/lib/link";+import { linkFromText, normalizeLink } from "@/cut/lib/link";+import { isPasteTarget } from "@/cut/lib/shortcutGate";import { lightboxItemFromLibrary, useLightbox } from "@/cut/lib/lightbox";import { reportActivity } from "@/cut/lib/tabActivity";import { useNewProjectTarget } from "@/cut/lib/newProject";@@ -516,8 +517,8 @@ export function LibraryView() {// The link's tile goes up at once — shaped by what that kind of link usually// holds — and the dialog closes, so the wait happens in the library rather// than in front of it.- const importUrl = async () => {- const value = normalizeLink(url);+ const importLink = async (raw: string) => {+ const value = normalizeLink(raw);if (!value) return;const { residency, folderId } = landing(openFolder);if (!live(residency)) return;@@ -564,6 +565,31 @@ export function LibraryView() {await run();};+ // ⌘V takes what a drop takes: files copied from the desktop upload into the+ // open folder, and a copied link imports the way the link field imports it.+ // A field with the caret in it keeps its own paste.+ const intake = useRef({ upload, importLink });+ useEffect(() => {+ intake.current = { upload, importLink };+ });+ useEffect(() => {+ const onPaste = (e: ClipboardEvent) => {+ if (e.defaultPrevented || isPasteTarget(e.target)) return;+ const files = Array.from(e.clipboardData?.files ?? []);+ if (files.length > 0) {+ e.preventDefault();+ void intake.current.upload(files);+ return;+ }+ const link = linkFromText(e.clipboardData?.getData("text/plain") ?? "");+ if (!link) return;+ e.preventDefault();+ void intake.current.importLink(link);+ };+ window.addEventListener("paste", onPaste);+ return () => window.removeEventListener("paste", onPaste);+ }, []);+const remove = async () => {if (!deleting) return;setDeleting(null);@@ -1048,13 +1074,13 @@ export function LibraryView() {className="pl-8"onChange={(e) => setUrl(e.target.value)}onKeyDown={(e) => {- if (e.key === "Enter") void importUrl();+ if (e.key === "Enter") void importLink(url);}}/></div><Buttondisabled={!url.trim()}- onClick={() => void importUrl()}+ onClick={() => void importLink(url)}><LinkIcon /> Import</Button>diff --git a/site/src/cut/lib/link.test.ts b/site/src/cut/lib/link.test.tsnew file mode 100644index 00000000..233caf34--- /dev/null+++ b/site/src/cut/lib/link.test.ts@@ -0,0 +1,18 @@+import { describe, expect, test } from "bun:test";++import { linkFromText } from "./link";++describe("linkFromText", () => {+ test("one address pastes as a link, with or without its scheme", () => {+ expect(linkFromText("https://youtube.com/watch?v=abc")).toBe("https://youtube.com/watch?v=abc");+ expect(linkFromText(" youtube.com/watch?v=abc\n")).toBe("https://youtube.com/watch?v=abc");+ });++ test("anything that is not one address is left alone", () => {+ expect(linkFromText("")).toBeNull();+ expect(linkFromText("beach sunset")).toBeNull();+ expect(linkFromText("@v2")).toBeNull();+ expect(linkFromText("https://a.com\nhttps://b.com")).toBeNull();+ expect(linkFromText("mailto:[email protected]")).toBeNull();+ });+});diff --git a/site/src/cut/lib/link.ts b/site/src/cut/lib/link.tsindex 0fc9f8d8..6ab782fb 100644--- a/site/src/cut/lib/link.ts+++ b/site/src/cut/lib/link.ts@@ -10,3 +10,19 @@ export function normalizeLink(value: string): string {if (/^[a-z][a-z0-9+.-]*:/i.test(v)) return v;return `https://${v}`;}++/** The one link a pasted text holds, normalized, or null when the text is+ * anything else: several lines, words with spaces, a bare name with no host.+ * A format check only; what the link points at is the importer's to judge. */+export function linkFromText(text: string): string | null {+ const v = text.trim();+ if (!v || /\s/.test(v)) return null;+ const href = normalizeLink(v);+ try {+ const u = new URL(href);+ if (u.protocol !== "http:" && u.protocol !== "https:") return null;+ return u.hostname.includes(".") ? href : null;+ } catch {+ return null;+ }+}