Skip to content

Open source CapCut alternative.

TypeScript

fix(site): additive click and snap hold-off read the platform's modifier

David Le98272daa6802
diff --git a/site/src/cut/components/ProjectsHome.tsx b/site/src/cut/components/ProjectsHome.tsx
index dbfb4ef8..23d975c1 100644
--- a/site/src/cut/components/ProjectsHome.tsx
+++ b/site/src/cut/components/ProjectsHome.tsx
@@ -50,6 +50,7 @@ import {
TooltipProvider,
TooltipTrigger,
} from "@/components/ui/tooltip";
+import { additiveClick } from "@/cut/lib/hostKeys";
import { MEDIA_CORS } from "@/cut/lib/mediaCors";
import { capturePosterWhenReady, readPoster } from "@/cut/lib/posterCache";
import { quotaErrorMessage } from "@/cut/lib/backend/cloud";
@@ -714,7 +715,7 @@ export function ProjectsHome() {
draggable={live(r)}
onDragStart={(e) => onProjectDragStart(e, p)}
onClick={(e) => {
- if (e.shiftKey || e.metaKey) {
+ if (additiveClick(e)) {
e.preventDefault();
toggleSelect(p.id);
return;
@@ -798,7 +799,7 @@ export function ProjectsHome() {
draggable={live(r)}
onDragStart={(e) => onProjectDragStart(e, p)}
onClick={(e) => {
- if (e.shiftKey || e.metaKey) {
+ if (additiveClick(e)) {
e.preventDefault();
toggleSelect(p.id);
return;
diff --git a/site/src/cut/components/Timeline.tsx b/site/src/cut/components/Timeline.tsx
index af4a010d..d0737fee 100644
--- a/site/src/cut/components/Timeline.tsx
+++ b/site/src/cut/components/Timeline.tsx
@@ -45,6 +45,7 @@ import {
import { originalSettings, type ExportDoc } from "@/cut/lib/exportClient";
import { useExports } from "@/cut/lib/exportStore";
import { isDragActive, startDrag, subscribeDragActive } from "@/cut/lib/drag";
+import { additiveClick } from "@/cut/lib/hostKeys";
import { CLIP_GAP, laneDragFor, laneDragParts, startLaneMove, startLaneTrim, type LaneDrag, type LaneKind } from "@/cut/lib/laneTracks";
import { downloadMedia, ensurePeaks, importImage, importStockMusic, importStockVideo, peekEdgeFrame, requestEdgeFrame, revealMedia, stripFailedFor, subscribeStripStatus } from "@/cut/lib/media";
import { planFilmstrip, type FilmTile } from "@/cut/lib/filmstrip";
@@ -1266,8 +1267,8 @@ export function Timeline() {
e.stopPropagation();
const s = useEditor.getState();
if (s.readOnly) return;
- // ⌘/⇧-click folds the bar into the multi-selection, like any lane bar.
- const additive = e.metaKey || e.shiftKey;
+ // ⌘/Ctrl/⇧-click folds the bar into the multi-selection, like any lane bar.
+ const additive = additiveClick(e);
let landing: { start: number; anchor: Anchor | null } | null = null;
startDrag(e, {
onMove: (dx) => {
@@ -1405,7 +1406,7 @@ export function Timeline() {
const st = useEditor.getState();
if (st.playing) st.setPlaying(false);
st.seek(Math.max(0, timeAt(e.clientX)));
- const additive = e.shiftKey || e.metaKey;
+ const additive = additiveClick(e);
const base = additive
? st.multiSelection.filter((m): m is NonNullable<Selection> => !!m)
: [];
diff --git a/site/src/cut/components/desktopFolders.tsx b/site/src/cut/components/desktopFolders.tsx
index bdcf656b..e332da18 100644
--- a/site/src/cut/components/desktopFolders.tsx
+++ b/site/src/cut/components/desktopFolders.tsx
@@ -12,6 +12,7 @@ import {
} from "@/components/ui/dropdown-menu";
import { Input } from "@/components/ui/input";
import { setObjectDragImage } from "@/cut/lib/assetDrag";
+import { additiveClick } from "@/cut/lib/hostKeys";
import type { AssetRef } from "@/cut/lib/assetRef";
import { formatBytes } from "@/lib/bytes";
import { RefDropZone } from "./RefDropZone";
@@ -146,7 +147,7 @@ export function Marquee({
if (e.button !== 0 || (e.target as HTMLElement).closest(MARQUEE_SKIP)) return;
const startX = e.clientX;
const startY = e.clientY;
- const additive = e.shiftKey || e.metaKey;
+ const additive = additiveClick(e);
const base = new Set(additive ? selectedRef.current : []);
let moved = false;
diff --git a/site/src/cut/lib/hostKeys.test.ts b/site/src/cut/lib/hostKeys.test.ts
new file mode 100644
index 00000000..c7d6c9f5
--- /dev/null
+++ b/site/src/cut/lib/hostKeys.test.ts
@@ -0,0 +1,28 @@
+import { describe, expect, test } from "bun:test";
+import { additiveClickOn, snapHeldOffOn } from "./hostKeys";
+
+const mods = (m: Partial<{ metaKey: boolean; ctrlKey: boolean; shiftKey: boolean }>) => ({
+ metaKey: false,
+ ctrlKey: false,
+ shiftKey: false,
+ ...m,
+});
+
+describe("platform modifiers", () => {
+ test("a Mac adds with cmd and leaves ctrl to the context menu", () => {
+ expect(additiveClickOn(true, mods({ metaKey: true }))).toBe(true);
+ expect(additiveClickOn(true, mods({ ctrlKey: true }))).toBe(false);
+ expect(snapHeldOffOn(true, mods({ ctrlKey: true }))).toBe(false);
+ });
+
+ test("Windows and Linux add with ctrl", () => {
+ expect(additiveClickOn(false, mods({ ctrlKey: true }))).toBe(true);
+ expect(additiveClickOn(false, mods({ metaKey: true }))).toBe(false);
+ expect(snapHeldOffOn(false, mods({ ctrlKey: true }))).toBe(true);
+ });
+
+ test("shift adds on either", () => {
+ expect(additiveClickOn(true, mods({ shiftKey: true }))).toBe(true);
+ expect(additiveClickOn(false, mods({ shiftKey: true }))).toBe(true);
+ });
+});
diff --git a/site/src/cut/lib/hostKeys.ts b/site/src/cut/lib/hostKeys.ts
new file mode 100644
index 00000000..bdbc4cf3
--- /dev/null
+++ b/site/src/cut/lib/hostKeys.ts
@@ -0,0 +1,34 @@
+"use client";
+
+/**
+ * The modifiers a pointer holds, read the way the platform means them.
+ *
+ * Windows and Linux add to a selection with Ctrl. A Mac adds with ⌘ and keeps
+ * Ctrl for the secondary click: ctrl-click arrives as button 0 with `ctrlKey`
+ * set and opens the context menu, so reading it as "add" there would change
+ * the selection under the menu that is opening.
+ */
+
+interface Mods {
+ metaKey: boolean;
+ ctrlKey: boolean;
+ shiftKey?: boolean;
+}
+
+/** Whether this machine's selection modifier is ⌘. Read once: it is the OS,
+ * and it does not change under the page. */
+export const APPLE_KEYS: boolean = (() => {
+ if (typeof navigator === "undefined") return false;
+ const nav = navigator as Navigator & { userAgentData?: { platform?: string } };
+ return /mac|iphone|ipad|ipod/i.test(nav.userAgentData?.platform ?? nav.platform ?? "");
+})();
+
+/** Whether this click adds to the selection instead of replacing it. */
+export const additiveClickOn = (apple: boolean, e: Mods): boolean =>
+ !!e.shiftKey || (apple ? e.metaKey : e.ctrlKey);
+
+/** Whether this drag is asking for snapping to be held off. */
+export const snapHeldOffOn = (apple: boolean, e: Mods): boolean => (apple ? e.metaKey : e.ctrlKey);
+
+export const additiveClick = (e: Mods): boolean => additiveClickOn(APPLE_KEYS, e);
+export const snapHeldOff = (e: Mods): boolean => snapHeldOffOn(APPLE_KEYS, e);
diff --git a/site/src/cut/lib/laneTracks.ts b/site/src/cut/lib/laneTracks.ts
index ba9665a7..f0135fc1 100644
--- a/site/src/cut/lib/laneTracks.ts
+++ b/site/src/cut/lib/laneTracks.ts
@@ -30,6 +30,7 @@
import type React from "react";
import { refFromAsset, startPointerRefDrag } from "./assetRef";
import { startDrag } from "./drag";
+import { additiveClick, snapHeldOff } from "./hostKeys";
import { track0Clips, clipLen, getClipSpans, moveOverlayGroup, nextFreeStart, overlayLaneOrder, overlayLayers, projectDuration, reanchorTransitions, startTrimRipple, useEditor } from "./store";
import { playheadAt } from "./playhead";
import type {
@@ -678,7 +679,7 @@ function startGroupMove(
dt = Math.max(-minStart, effDx / ui.pps);
// Snap the grabbed item's edges; the whole set follows its delta.
let guide: number | null = null;
- if (!ev.metaKey) {
+ if (!snapHeldOff(ev)) {
const start = grabbed.start + dt;
const end = start + grabbed.len;
let best = { d: tol, dt, px: null as number | null };
@@ -816,7 +817,7 @@ export function startLaneMove<V = unknown>(
}
settleSnapBack();
const s = useEditor.getState();
- if (e.metaKey || e.shiftKey) {
+ if (additiveClick(e)) {
s.toggleSelect({ kind: laneSelectionKind(kind), id });
return;
}
@@ -1023,7 +1024,7 @@ export function startLaneMove<V = unknown>(
// Snap whichever edge of the moving item lands nearest a logical time.
let start = ds;
let guide: number | null = null;
- if (!ev.metaKey) {
+ if (!snapHeldOff(ev)) {
const end = start + len;
let best = { d: tol, start, px: null as number | null };
for (const T of targets) {
@@ -1256,7 +1257,7 @@ export function startLaneTrim(
if (desired >= free) {
// Room to the left: grow freely, snapping to logical times.
start = desired;
- const hit = ev.metaKey ? null : nearestSnap(start, targets, tol);
+ const hit = snapHeldOff(ev) ? null : nearestSnap(start, targets, tol);
if (hit !== null && hit >= free && hit <= maxStart) {
start = hit;
ui.onSnap(leftGuide(hit, ui.pps));
@@ -1375,7 +1376,7 @@ export function startLaneTrim(
if (desired <= ceil) {
// Room to grow: snap to logical times within the ceiling.
end = desired;
- const hit = ev.metaKey ? null : nearestSnap(end, targets, tol);
+ const hit = snapHeldOff(ev) ? null : nearestSnap(end, targets, tol);
if (hit !== null && hit > minEnd && hit <= ceil) {
end = hit;
ui.onSnap(rightGuide(end, ui.pps));