Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 115 additions & 41 deletions frontend/src/components/CanvasController.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useRef } from "react";
import { useCallback, useEffect, useRef } from "react";
import { useCanvasCamera } from "../hooks/useCanvasCamera";
import { getArtifactCardSize, getCameraFrame } from "../lib/canvas";
import {
Expand All @@ -8,6 +8,13 @@ import {
import type { Artifact } from "../types/artifacts";
import type { Workspace } from "../services/workspaces";

const VIEWPORT_INSETS = {
paddingTop: 84,
paddingLeft: 84,
paddingBottom: 64,
paddingRight: 64,
};

interface Props {
artifacts: Artifact[];
workspace: Workspace | null;
Expand All @@ -19,54 +26,121 @@ export default function CanvasController({
workspace,
isReady,
}: Props) {

const camera = useCanvasCamera();
const focusedWorkspaceId = useRef<number | null>(null);

useEffect(() => {
if (!workspace || !isReady || focusedWorkspaceId.current === workspace.id) return;
const frameWorkspace = useCallback(
(animate = true) => {
if (!workspace) return;

const cards = artifacts.map((artifact) => {
const element = document.querySelector<HTMLElement>(
`[data-artifact-id="${artifact.id}"]`,
);
const size = element
? { width: element.offsetWidth, height: element.offsetHeight }
: getArtifactCardSize(artifact);

return { x: artifact.x, y: artifact.y, ...size };
});

const cameraFrame = getCameraFrame(
cards,
{ width: window.innerWidth, height: window.innerHeight },
{
...VIEWPORT_INSETS,
workspaceHome: getWorkspaceHome(workspace),
},
);

const renderCenter = toRenderPosition(cameraFrame.center);
const zoom = cameraFrame.zoom;

if (!animate) {
// Start close to home, then settle into it. This makes entering a
// workspace feel like arriving at a place instead of a camera jump.
camera.setInitialPosition({
x: window.innerWidth / 2 - renderCenter.x * zoom + 54,
y: window.innerHeight / 2 - renderCenter.y * zoom + 36,
zoom,
});

const frame = window.requestAnimationFrame(() => {
camera.centerOn(
renderCenter,
{
width: window.innerWidth,
height: window.innerHeight,
},
{ zoom },
);
});

const cards = artifacts.map((artifact) => {
const element = document.querySelector<HTMLElement>(
`[data-artifact-id="${artifact.id}"]`,
return () => window.cancelAnimationFrame(frame);
}

camera.centerOn(
renderCenter,
{
width: window.innerWidth,
height: window.innerHeight,
},
{ zoom, animate: true },
);
const size = element
? { width: element.offsetWidth, height: element.offsetHeight }
: getArtifactCardSize(artifact);

return { x: artifact.x, y: artifact.y, ...size };
});
const cameraFrame = getCameraFrame(
cards,
{ width: window.innerWidth, height: window.innerHeight },
);
const logicalCenter = artifacts.length === 0
? getWorkspaceHome(workspace)
: cameraFrame.center;
const zoom = artifacts.length === 0 ? 1 : cameraFrame.zoom;

const renderCenter =
toRenderPosition(logicalCenter);

// Start close to home, then settle into it. This makes entering a
// workspace feel like arriving at a place instead of a camera jump.
camera.setInitialPosition({
x: window.innerWidth / 2 - renderCenter.x * zoom + 54,
y: window.innerHeight / 2 - renderCenter.y * zoom + 36,
zoom,
});

const frame = window.requestAnimationFrame(() => {
camera.centerOn(renderCenter, {
width: window.innerWidth,
height: window.innerHeight,
}, { zoom });
});
},
[artifacts, camera, workspace],
);

useEffect(() => {
if (!workspace || !isReady || focusedWorkspaceId.current === workspace.id) return;

focusedWorkspaceId.current = workspace.id;
const cleanup = frameWorkspace(false);
return () => {
if (cleanup) cleanup();
};
}, [frameWorkspace, isReady, workspace]);

useEffect(() => {
if (!workspace || !isReady) return;

let resizeTimer: ReturnType<typeof setTimeout> | null = null;
const handleResize = () => {
if (resizeTimer) clearTimeout(resizeTimer);
resizeTimer = setTimeout(() => {
frameWorkspace(true);
}, 150);
};

window.addEventListener("resize", handleResize);
return () => {
if (resizeTimer) clearTimeout(resizeTimer);
window.removeEventListener("resize", handleResize);
};
}, [frameWorkspace, isReady, workspace]);

useEffect(() => {
if (!workspace || !isReady) return;

const handleKeyDown = (event: KeyboardEvent) => {
if ((event.metaKey || event.ctrlKey) && (event.key === "0" || event.code === "Digit0")) {
const target = event.target as HTMLElement | null;
if (
target &&
(target.tagName === "INPUT" ||
target.tagName === "TEXTAREA" ||
target.isContentEditable)
) {
return;
}
event.preventDefault();
frameWorkspace(true);
}
};

return () => window.cancelAnimationFrame(frame);
}, [artifacts, camera, isReady, workspace]);
window.addEventListener("keydown", handleKeyDown);
return () => window.removeEventListener("keydown", handleKeyDown);
}, [frameWorkspace, isReady, workspace]);

return null;
}
2 changes: 1 addition & 1 deletion frontend/src/components/Viewport.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default function Viewport({
>

{children}
<p className="nook-viewport__hint" aria-hidden="true">Hold space to pan · scroll to zoom</p>
<p className="nook-viewport__hint" aria-hidden="true">Hold space to pan · scroll to zoom · ⌘0 to fit</p>

</div>

Expand Down
124 changes: 112 additions & 12 deletions frontend/src/lib/canvas.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { describe, expect, it } from "vitest";
import { getCameraFrame, getMainArtifactCluster } from "./canvas";
import {
EMPTY_WORKSPACE_CARDS,
getCameraFrame,
getMainArtifactCluster,
} from "./canvas";

describe("canvas spatial framing", () => {
it("keeps a cluster at zoom one when padding already fits", () => {
Expand All @@ -15,7 +19,7 @@ describe("canvas spatial framing", () => {
expect(frame.zoom).toBe(1);
});

it("selects largest spatial group and calculates centers from card centers", () => {
it("selects largest spatial group and calculates center from true bounding box", () => {
const nearby = [
{ x: 100, y: 100, width: 260, height: 140 },
{ x: 440, y: 100, width: 280, height: 300 },
Expand All @@ -31,16 +35,13 @@ describe("canvas spatial framing", () => {

expect(cluster).toHaveLength(3);
const frame = getCameraFrame(cards, { width: 1200, height: 900 });
const centers = nearby.map((card) => ({
x: card.x + card.width / 2,
y: card.y + card.height / 2,
}));
expect(frame.center.x).toBeCloseTo(
centers.reduce((sum, point) => sum + point.x, 0) / centers.length,
);
expect(frame.center.y).toBeCloseTo(
centers.reduce((sum, point) => sum + point.y, 0) / centers.length,
);
const minX = Math.min(...nearby.map((c) => c.x));
const maxX = Math.max(...nearby.map((c) => c.x + c.width));
const minY = Math.min(...nearby.map((c) => c.y));
const maxY = Math.max(...nearby.map((c) => c.y + c.height));

expect(frame.center.x).toBeCloseTo((minX + maxX) / 2);
expect(frame.center.y).toBeCloseTo((minY + maxY) / 2);
});

it("fits selected card bounds with padding and never zooms above one", () => {
Expand Down Expand Up @@ -72,4 +73,103 @@ describe("canvas spatial framing", () => {
expect(screenBounds.top).toBeGreaterThanOrEqual(padding - 0.001);
expect(screenBounds.bottom).toBeLessThanOrEqual(viewport.height - padding + 0.001);
});

describe("empty workspace laptop responsiveness", () => {
const safeInsets = {
paddingTop: 84,
paddingLeft: 84,
paddingBottom: 64,
paddingRight: 64,
};

const targetLaptopViewports = [
{ name: "13.6-inch MacBook Air (1470x956)", width: 1470, height: 956 },
{ name: "13.6-inch MacBook Air scaled (1280x832)", width: 1280, height: 832 },
{ name: "14.2-inch MacBook Pro (1512x982)", width: 1512, height: 982 },
{ name: "Windows 14-inch 150% scaling (1280x720)", width: 1280, height: 720 },
{ name: "Windows 14-inch 125% scaling (1536x864)", width: 1536, height: 864 },
{ name: "Standard laptop 1366x768", width: 1366, height: 768 },
];

targetLaptopViewports.forEach(({ name, width, height }) => {
it(`fits empty workspace notes within screen on ${name}`, () => {
const viewport = { width, height };
const frame = getCameraFrame([], viewport, safeInsets);

expect(frame.zoom).toBeLessThanOrEqual(1);
expect(frame.zoom).toBeGreaterThan(0.1);

const minX = Math.min(...EMPTY_WORKSPACE_CARDS.map((c) => c.x));
const maxX = Math.max(...EMPTY_WORKSPACE_CARDS.map((c) => c.x + c.width));
const minY = Math.min(...EMPTY_WORKSPACE_CARDS.map((c) => c.y));
const maxY = Math.max(...EMPTY_WORKSPACE_CARDS.map((c) => c.y + c.height));

const screenLeft = viewport.width / 2 + (minX - frame.center.x) * frame.zoom;
const screenRight = viewport.width / 2 + (maxX - frame.center.x) * frame.zoom;
const screenTop = viewport.height / 2 + (minY - frame.center.y) * frame.zoom;
const screenBottom = viewport.height / 2 + (maxY - frame.center.y) * frame.zoom;

// Empty notes must stay strictly within the visible clear area without scrolling
expect(screenLeft).toBeGreaterThanOrEqual(safeInsets.paddingLeft - 0.001);
expect(screenRight).toBeLessThanOrEqual(viewport.width - safeInsets.paddingRight + 0.001);
expect(screenTop).toBeGreaterThanOrEqual(safeInsets.paddingTop - 0.001);
expect(screenBottom).toBeLessThanOrEqual(viewport.height - safeInsets.paddingBottom + 0.001);
});
});

it("respects custom workspaceHome offset for empty workspace", () => {
const home = { x: 500, y: -300 };
const viewport = { width: 1280, height: 720 };
const frame = getCameraFrame([], viewport, { ...safeInsets, workspaceHome: home });

const minX = Math.min(...EMPTY_WORKSPACE_CARDS.map((c) => c.x + home.x));
const maxX = Math.max(...EMPTY_WORKSPACE_CARDS.map((c) => c.x + home.x + c.width));
const minY = Math.min(...EMPTY_WORKSPACE_CARDS.map((c) => c.y + home.y));
const maxY = Math.max(...EMPTY_WORKSPACE_CARDS.map((c) => c.y + home.y + c.height));

const screenLeft = viewport.width / 2 + (minX - frame.center.x) * frame.zoom;
const screenRight = viewport.width / 2 + (maxX - frame.center.x) * frame.zoom;
const screenTop = viewport.height / 2 + (minY - frame.center.y) * frame.zoom;
const screenBottom = viewport.height / 2 + (maxY - frame.center.y) * frame.zoom;

expect(screenLeft).toBeGreaterThanOrEqual(safeInsets.paddingLeft - 0.001);
expect(screenRight).toBeLessThanOrEqual(viewport.width - safeInsets.paddingRight + 0.001);
expect(screenTop).toBeGreaterThanOrEqual(safeInsets.paddingTop - 0.001);
expect(screenBottom).toBeLessThanOrEqual(viewport.height - safeInsets.paddingBottom + 0.001);
});
});

describe("populated workspace laptop responsiveness", () => {
const safeInsets = {
paddingTop: 84,
paddingLeft: 84,
paddingBottom: 64,
paddingRight: 64,
};

it("fits multiple cards spaced across the workspace on smaller 1280x720 display", () => {
const cards = [
{ x: -400, y: -200, width: 380, height: 220 },
{ x: 100, y: -100, width: 280, height: 320 },
{ x: 300, y: 150, width: 385, height: 240 },
];
const viewport = { width: 1280, height: 720 };
const frame = getCameraFrame(cards, viewport, safeInsets);

const minX = Math.min(...cards.map((c) => c.x));
const maxX = Math.max(...cards.map((c) => c.x + c.width));
const minY = Math.min(...cards.map((c) => c.y));
const maxY = Math.max(...cards.map((c) => c.y + c.height));

const screenLeft = viewport.width / 2 + (minX - frame.center.x) * frame.zoom;
const screenRight = viewport.width / 2 + (maxX - frame.center.x) * frame.zoom;
const screenTop = viewport.height / 2 + (minY - frame.center.y) * frame.zoom;
const screenBottom = viewport.height / 2 + (maxY - frame.center.y) * frame.zoom;

expect(screenLeft).toBeGreaterThanOrEqual(safeInsets.paddingLeft - 0.001);
expect(screenRight).toBeLessThanOrEqual(viewport.width - safeInsets.paddingRight + 0.001);
expect(screenTop).toBeGreaterThanOrEqual(safeInsets.paddingTop - 0.001);
expect(screenBottom).toBeLessThanOrEqual(viewport.height - safeInsets.paddingBottom + 0.001);
});
});
});
Loading
Loading