Skip to content

Open source CapCut alternative.

TypeScript

feat(site): card drag ghosts shrink to a thumbnail

David Lef11fb1b65dae
diff --git a/site/src/cut/components/LibraryView.tsx b/site/src/cut/components/LibraryView.tsx
index 62af4187..4055ce21 100644
--- a/site/src/cut/components/LibraryView.tsx
+++ b/site/src/cut/components/LibraryView.tsx
@@ -634,6 +634,7 @@ export function LibraryCard({
>
<div
ref={tileRef}
+ data-drag-object
className={cn(
"relative aspect-square cursor-grab overflow-hidden rounded-xl border bg-muted transition-shadow group-hover:shadow-[0_4px_20px_rgba(0,0,0,0.1)] active:cursor-grabbing",
selected || flash ? "border-[#0a84ff] ring-2 ring-[#0a84ff]" : "border-border"
@@ -680,6 +681,7 @@ export function LibraryCard({
// two of them collide. The length reads at rest, the size takes over
// on hover, where the + button is what the pointer is there for.
<span
+ data-drag-omit
className={cn(
"absolute right-1.5 bottom-1.5 rounded-md bg-black/65 px-1.5 py-0.5 font-mono text-[10px] text-white tabular-nums",
a.type !== "video" && "opacity-0 transition-opacity group-hover:opacity-100"
diff --git a/site/src/cut/components/SidePanel.tsx b/site/src/cut/components/SidePanel.tsx
index 6cd9989e..78e20667 100644
--- a/site/src/cut/components/SidePanel.tsx
+++ b/site/src/cut/components/SidePanel.tsx
@@ -1012,6 +1012,7 @@ function AssetCard({ asset, projectId }: { asset: MediaAsset; projectId: string
>
<div
ref={tileRef}
+ data-drag-object
className={cn(
"relative aspect-square overflow-hidden rounded-lg border border-border bg-muted transition-colors group-hover:border-input",
flash && "ring-2 ring-[#0a84ff] ring-offset-1"
@@ -1043,7 +1044,10 @@ function AssetCard({ asset, projectId }: { asset: MediaAsset; projectId: string
/>
)}
{asset.type === "video" && (
- <span className="absolute right-1 bottom-1 rounded-[5px] bg-black/65 px-1 py-px font-mono text-[9.5px] text-white tabular-nums">
+ <span
+ data-drag-omit
+ className="absolute right-1 bottom-1 rounded-[5px] bg-black/65 px-1 py-px font-mono text-[9.5px] text-white tabular-nums"
+ >
{formatTime(asset.duration)}
</span>
)}
diff --git a/site/src/cut/lib/assetDrag.ts b/site/src/cut/lib/assetDrag.ts
index 48fa6524..1ed215eb 100644
--- a/site/src/cut/lib/assetDrag.ts
+++ b/site/src/cut/lib/assetDrag.ts
@@ -132,15 +132,26 @@ export function clearElementDrag() {
inFlightElement = null;
}
-/** Use the card itself as the drag ghost: a clone at rendered size, so the
- * ghost matches the card exactly — rounded corners, fills, labels. Live
- * `<video>`/`<canvas>` content is baked into the clone (clones of those paint
- * blank), and hover-revealed controls drop out since the clone is not hovered.
- * The clone lives off-screen just long enough for the browser to snapshot it. */
-export function setCardDragImage(e: React.DragEvent, el: HTMLElement) {
+/** Longest side of a card drag ghost, px. */
+const CARD_GHOST_MAX = 72;
+
+/** Use the card itself as the drag ghost: a scaled-down clone, so the
+ * ghost matches the card exactly — rounded corners, fills, labels. A card can
+ * narrow the ghost to just its picture: the ghost clones the node marked
+ * `data-drag-object` when one exists, and drops anything marked
+ * `data-drag-omit` (badges riding on the picture). Live `<video>`/`<canvas>`
+ * content is baked into the clone (clones of those paint blank), and
+ * hover-revealed controls drop out since the clone is not hovered. The clone
+ * lives off-screen just long enough for the browser to snapshot it. */
+export function setCardDragImage(e: React.DragEvent, host: HTMLElement) {
+ const el = host.querySelector<HTMLElement>("[data-drag-object]") ?? host;
const rect = el.getBoundingClientRect();
const clone = el.cloneNode(true) as HTMLElement;
- const srcMedia = el.querySelectorAll<HTMLElement>("video, canvas");
+ clone.querySelectorAll("[data-drag-omit]").forEach((n) => n.remove());
+ // Skip media inside omitted nodes so both lists pair up by index.
+ const srcMedia = Array.from(el.querySelectorAll<HTMLElement>("video, canvas")).filter(
+ (n) => !n.closest("[data-drag-omit]")
+ );
clone.querySelectorAll<HTMLElement>("video, canvas").forEach((node, i) => {
const src = srcMedia[i];
if (!src) return;
@@ -176,16 +187,27 @@ export function setCardDragImage(e: React.DragEvent, el: HTMLElement) {
}
node.replaceWith(c);
});
- clone.style.position = "absolute";
- clone.style.top = "-1000px";
- clone.style.left = "-1000px";
clone.style.width = `${rect.width}px`;
clone.style.height = `${rect.height}px`;
clone.style.margin = "0";
- clone.style.pointerEvents = "none";
- document.body.appendChild(clone);
- e.dataTransfer.setDragImage(clone, e.clientX - rect.left, e.clientY - rect.top);
- setTimeout(() => clone.remove(), 0);
+ // Compact ghost: the card at full size blankets the rows it is dragged
+ // across, so it shrinks to a thumbnail, scaled around the grab point.
+ const scale = Math.min(1, CARD_GHOST_MAX / Math.max(rect.width, rect.height));
+ clone.style.transform = `scale(${scale})`;
+ clone.style.transformOrigin = "top left";
+ const wrap = document.createElement("div");
+ wrap.style.cssText =
+ "position:absolute;top:-1000px;left:-1000px;pointer-events:none;" +
+ `width:${rect.width * scale}px;height:${rect.height * scale}px;`;
+ wrap.appendChild(clone);
+ document.body.appendChild(wrap);
+ // A grab outside the snapshot (on the card's label) holds the nearest edge.
+ e.dataTransfer.setDragImage(
+ wrap,
+ Math.min(Math.max(e.clientX - rect.left, 0), rect.width) * scale,
+ Math.min(Math.max(e.clientY - rect.top, 0), rect.height) * scale
+ );
+ setTimeout(() => wrap.remove(), 0);
}
/** The dragged thing itself as the ghost: the tile's `data-drag-object` node —