Skip to content

Open source CapCut alternative.

TypeScript

feat(site): open projects in a new tab on cmd-click

David Le3f03921d3432
diff --git a/site/src/cut/components/ProjectsHome.tsx b/site/src/cut/components/ProjectsHome.tsx
index 3ccc18d4..0b7b5378 100644
--- a/site/src/cut/components/ProjectsHome.tsx
+++ b/site/src/cut/components/ProjectsHome.tsx
@@ -40,7 +40,7 @@ import {
TooltipProvider,
TooltipTrigger,
} from "@/components/ui/tooltip";
-import { additiveClick } from "@/cut/lib/hostKeys";
+import { additiveClick, newTabClick } from "@/cut/lib/hostKeys";
import { MEDIA_CORS } from "@/cut/lib/mediaCors";
import { capturePosterWhenReady, readPoster } from "@/cut/lib/posterCache";
import { apiJson } from "@/cut/lib/api";
@@ -147,6 +147,9 @@ type DeleteSet = {
};
const NO_PICK: DeleteSet = { folders: [], projects: [] };
+// MouseEvent.button for the middle (wheel) button.
+const MIDDLE_BUTTON = 1;
+
// What a shelf's refusal says, when it says anything.
const errorOf = async (res: Response | null) =>
res ? (await apiJson<object>(res)).error : undefined;
@@ -827,6 +830,28 @@ export function ProjectsHome() {
/>
);
+ // A project click opens it here. ⌘-click (Ctrl elsewhere) or a middle click
+ // opens it in a new tab, the way a link does; shift-click adds it to the pick.
+ const openProject = (e: React.MouseEvent, p: ProjectSummary) => {
+ const href = projectHref(base, p.id, "projects", openFolder);
+ if (e.button === MIDDLE_BUTTON || newTabClick(e)) {
+ e.preventDefault();
+ window.open(href, "_blank", "noopener");
+ return;
+ }
+
+ if (e.button !== 0) {
+ return;
+ }
+
+ if (additiveClick(e)) {
+ e.preventDefault();
+ pickTile(e, p.id, order);
+ return;
+ }
+ router.push(href);
+ };
+
// Similar-shape tiles get their own band of wrapped rows, so a wide tile
// never shares a row with a tall one. Bands are ordered by their most
// recently edited project, recency kept within each band.
@@ -857,14 +882,8 @@ export function ProjectsHome() {
className="group max-w-full cursor-pointer"
draggable={live(r)}
onDragStart={(e) => onProjectDragStart(e, p)}
- onClick={(e) => {
- if (additiveClick(e)) {
- e.preventDefault();
- pickTile(e, p.id, order);
- return;
- }
- router.push(projectHref(base, p.id, "projects", openFolder));
- }}
+ onClick={(e) => openProject(e, p)}
+ onAuxClick={(e) => openProject(e, p)}
>
<div
className={cn(
@@ -941,14 +960,8 @@ export function ProjectsHome() {
)}
draggable={live(r)}
onDragStart={(e) => onProjectDragStart(e, p)}
- onClick={(e) => {
- if (additiveClick(e)) {
- e.preventDefault();
- pickTile(e, p.id, order);
- return;
- }
- router.push(projectHref(base, p.id, "projects", openFolder));
- }}
+ onClick={(e) => openProject(e, p)}
+ onAuxClick={(e) => openProject(e, p)}
>
<span className="flex min-w-0 items-center gap-2.5">
<Film className="size-4 shrink-0 text-muted-foreground" />
diff --git a/site/src/cut/lib/hostKeys.test.ts b/site/src/cut/lib/hostKeys.test.ts
index c7d6c9f5..02f6dddc 100644
--- a/site/src/cut/lib/hostKeys.test.ts
+++ b/site/src/cut/lib/hostKeys.test.ts
@@ -1,5 +1,5 @@
import { describe, expect, test } from "bun:test";
-import { additiveClickOn, snapHeldOffOn } from "./hostKeys";
+import { additiveClickOn, newTabClickOn, snapHeldOffOn } from "./hostKeys";
const mods = (m: Partial<{ metaKey: boolean; ctrlKey: boolean; shiftKey: boolean }>) => ({
metaKey: false,
@@ -25,4 +25,12 @@ describe("platform modifiers", () => {
expect(additiveClickOn(true, mods({ shiftKey: true }))).toBe(true);
expect(additiveClickOn(false, mods({ shiftKey: true }))).toBe(true);
});
+
+ test("a new tab opens on cmd on a Mac and ctrl elsewhere", () => {
+ expect(newTabClickOn(true, mods({ metaKey: true }))).toBe(true);
+ expect(newTabClickOn(true, mods({ ctrlKey: true }))).toBe(false);
+ expect(newTabClickOn(false, mods({ ctrlKey: true }))).toBe(true);
+ expect(newTabClickOn(false, mods({ metaKey: true }))).toBe(false);
+ expect(newTabClickOn(true, mods({ shiftKey: true }))).toBe(false);
+ });
});
diff --git a/site/src/cut/lib/hostKeys.ts b/site/src/cut/lib/hostKeys.ts
index bdbc4cf3..78f438b1 100644
--- a/site/src/cut/lib/hostKeys.ts
+++ b/site/src/cut/lib/hostKeys.ts
@@ -30,5 +30,10 @@ export const additiveClickOn = (apple: boolean, e: Mods): boolean =>
/** Whether this drag is asking for snapping to be held off. */
export const snapHeldOffOn = (apple: boolean, e: Mods): boolean => (apple ? e.metaKey : e.ctrlKey);
+/** Whether this click asks to open its target in a new tab, the way a link
+ * does: ⌘ on a Mac, Ctrl elsewhere. */
+export const newTabClickOn = (apple: boolean, e: Mods): boolean => (apple ? e.metaKey : e.ctrlKey);
+
export const additiveClick = (e: Mods): boolean => additiveClickOn(APPLE_KEYS, e);
+export const newTabClick = (e: Mods): boolean => newTabClickOn(APPLE_KEYS, e);
export const snapHeldOff = (e: Mods): boolean => snapHeldOffOn(APPLE_KEYS, e);