Git for the intent behind agent-written code. Records what your coding agent decided and why, and links every commit to it.
JavaScript ai-codingclaudeclaude-codeclaude-code-pluginclicoding-agentsdeveloper-toolsgitllmtypescript
feat(map): fit, drag and zoom the Shape window's diagram
Eren57763718436b
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex beecf91..84c32e0 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -6,6 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0## [Unreleased]### Added+- A diagram in the Shape's Open window opens fitted to the window and can be dragged, zoomed with the wheel or the **−** / **Fit** / **+** buttons, moved with the arrow keys and fitted again with a double-click, instead of being read through a horizontal scrollbar.- Captured work: `cogit captures` and the map's **Captured** list gather what the sessions left open — each session's pending items and each branch summary's open line — with the source message, who recorded it and how old it is; items waiting for you come first. Read-only and derived from the record; cogit keeps no planning state.## [0.26.0] - 2026-10-31diff --git a/docs/guide/map.md b/docs/guide/map.mdindex 7274319..0305c85 100644--- a/docs/guide/map.md+++ b/docs/guide/map.md@@ -108,7 +108,9 @@ A message that landed commits has three layers, in that order:The message details show a short preview and an **Open** button. Open shows the three layerswith 1/2/3 tabs (the number keys work too), starting on Shape. A message with nothing-recorded yet says "being prepared"; with `shapes` off, it says so.+recorded yet says "being prepared"; with `shapes` off, it says so. In the Open window a wide+diagram opens fitted to the window: drag it to move it, use the wheel or **−** / **Fit** / **+**+to zoom, the arrow keys to move it, and double-click to fit it again.diff --git a/packages/template/src/MermaidDiagram.tsx b/packages/template/src/MermaidDiagram.tsxindex 83a9971..964daa6 100644--- a/packages/template/src/MermaidDiagram.tsx+++ b/packages/template/src/MermaidDiagram.tsx@@ -1,4 +1,12 @@-import { useEffect, useRef, useState } from "react";+import {+ type KeyboardEvent,+ type PointerEvent,+ useCallback,+ useEffect,+ useRef,+ useState,+} from "react";+import { clampPan, fitView, type PanView, type Size, zoomAt } from "./panZoom.js";/*** Draws a Mermaid flowchart (Story 13.6). Mermaid is ~3.5 MB, so it is not@@ -68,19 +76,61 @@ export function naturalSize(svg: SVGSVGElement): void {svg.style.maxWidth = "none";}+/** How far an arrow key pans, in pixels. */+const KEY_STEP = 40;+/** Zoom per button press. */+const ZOOM_STEP = 1.25;+export function MermaidDiagram({source,className,fit = false,+ interactive = false,+ available = mermaidAvailable(),}: {source: string;className?: string;/** Shrink to the box (the panel's preview) instead of keeping the drawn size. */fit?: boolean;+ /**+ * Open fitted in a fixed viewport that drags and zooms (Story 14.2): the+ * Open window's diagram. The panel's preview leaves it off.+ */+ interactive?: boolean;+ /** Whether Mermaid can be loaded; the page's protocol decides by default. */+ available?: boolean;}) {const host = useRef<HTMLDivElement>(null);- const available = mermaidAvailable();+ const viewport = useRef<HTMLDivElement>(null);const [failed, setFailed] = useState(false);+ const [view, setView] = useState<PanView>({ x: 0, y: 0, scale: 1 });+ const [dragging, setDragging] = useState(false);+ const drag = useRef<{ id: number; x: number; y: number } | null>(null);++ const sizes = useCallback((): { box: Size; content: Size } | null => {+ if (viewport.current === null || host.current === null) return null;+ return {+ box: { width: viewport.current.clientWidth, height: viewport.current.clientHeight },+ // Layout size: the transform does not change it.+ content: { width: host.current.offsetWidth, height: host.current.offsetHeight },+ };+ }, []);++ const refit = useCallback(() => {+ const measured = sizes();+ if (measured !== null) setView(fitView(measured.box, measured.content));+ }, [sizes]);++ const move = useCallback(+ (next: (view: PanView) => PanView) => {+ setView((current) => {+ const measured = sizes();+ const moved = next(current);+ return measured === null ? moved : clampPan(moved, measured.box, measured.content);+ });+ },+ [sizes],+ );useEffect(() => {if (!available) return;@@ -94,6 +144,7 @@ export function MermaidDiagram({host.current.innerHTML = svg;const drawn = host.current.querySelector("svg");if (!fit && drawn !== null) naturalSize(drawn);+ if (interactive) refit();}).catch(() => {if (!cancelled) setFailed(true);@@ -101,7 +152,70 @@ export function MermaidDiagram({return () => {cancelled = true;};- }, [source, available, fit]);+ }, [source, available, fit, interactive, refit]);++ // A plain wheel over the diagram zooms around the pointer and never scrolls+ // the window behind it; React's wheel listener is passive, so this one is not.+ useEffect(() => {+ const box = viewport.current;+ if (!interactive || box === null) return;+ const onWheel = (event: WheelEvent) => {+ event.preventDefault();+ const rect = box.getBoundingClientRect();+ const factor = Math.exp(-event.deltaY * 0.0015);+ move((current) =>+ zoomAt(current, factor, event.clientX - rect.left, event.clientY - rect.top),+ );+ };+ box.addEventListener("wheel", onWheel, { passive: false });+ return () => box.removeEventListener("wheel", onWheel);+ }, [interactive, move]);++ // A resize keeps the user's scale and only pulls the diagram back in reach.+ useEffect(() => {+ if (!interactive) return;+ const onResize = () => move((current) => current);+ window.addEventListener("resize", onResize);+ return () => window.removeEventListener("resize", onResize);+ }, [interactive, move]);++ const zoomCentre = (factor: number) => {+ const measured = sizes();+ if (measured === null) return;+ move((current) => zoomAt(current, factor, measured.box.width / 2, measured.box.height / 2));+ };++ const onPointerDown = (event: PointerEvent<HTMLDivElement>) => {+ if (event.button !== 0) return;+ event.currentTarget.setPointerCapture(event.pointerId);+ drag.current = { id: event.pointerId, x: event.clientX, y: event.clientY };+ setDragging(true);+ };+ const onPointerMove = (event: PointerEvent<HTMLDivElement>) => {+ const start = drag.current;+ if (start === null || start.id !== event.pointerId) return;+ const dx = event.clientX - start.x;+ const dy = event.clientY - start.y;+ drag.current = { id: start.id, x: event.clientX, y: event.clientY };+ move((current) => ({ ...current, x: current.x + dx, y: current.y + dy }));+ };+ const endDrag = (event: PointerEvent<HTMLDivElement>) => {+ if (drag.current?.id !== event.pointerId) return;+ drag.current = null;+ setDragging(false);+ };+ const onKeyDown = (event: KeyboardEvent<HTMLDivElement>) => {+ const step: Record<string, [number, number]> = {+ ArrowLeft: [KEY_STEP, 0],+ ArrowRight: [-KEY_STEP, 0],+ ArrowUp: [0, KEY_STEP],+ ArrowDown: [0, -KEY_STEP],+ };+ const delta = step[event.key];+ if (delta === undefined) return;+ event.preventDefault();+ move((current) => ({ ...current, x: current.x + delta[0], y: current.y + delta[1] }));+ };const extra = className ? ` ${className}` : "";if (!available)@@ -114,5 +228,38 @@ export function MermaidDiagram({</div>);if (failed) return <pre className={`shape-source${extra}`}>{source}</pre>;- return <div ref={host} className={`shape-diagram${extra}`} />;+ if (!interactive) return <div ref={host} className={`shape-diagram${extra}`} />;+ return (+ <div className={`shape-diagram shape-diagram-interactive${extra}`}>+ <div+ ref={viewport}+ className={`shape-viewport${dragging ? " is-dragging" : ""}`}+ tabIndex={0}+ aria-label="Diagram: drag or use the arrow keys to move, the wheel to zoom"+ onPointerDown={onPointerDown}+ onPointerMove={onPointerMove}+ onPointerUp={endDrag}+ onPointerCancel={endDrag}+ onDoubleClick={refit}+ onKeyDown={onKeyDown}+ >+ <div+ ref={host}+ className="shape-pan-content"+ style={{ transform: `translate(${view.x}px, ${view.y}px) scale(${view.scale})` }}+ />+ </div>+ <div className="shape-zoom-controls">+ <button type="button" aria-label="Zoom out" onClick={() => zoomCentre(1 / ZOOM_STEP)}>+ −+ </button>+ <button type="button" aria-label="Fit the diagram" onClick={refit}>+ Fit+ </button>+ <button type="button" aria-label="Zoom in" onClick={() => zoomCentre(ZOOM_STEP)}>+ ++ </button>+ </div>+ </div>+ );}diff --git a/packages/template/src/ShapeModal.tsx b/packages/template/src/ShapeModal.tsxindex a13ff7a..a18832a 100644--- a/packages/template/src/ShapeModal.tsx+++ b/packages/template/src/ShapeModal.tsx@@ -367,7 +367,7 @@ export function ShapeLayers({enabled,onTab,regenerate,- renderMermaid = (source) => <MermaidDiagram source={source} />,+ renderMermaid = (source) => <MermaidDiagram source={source} interactive />,}: {node: ShapeNode;root: string;diff --git a/packages/template/src/panZoom.ts b/packages/template/src/panZoom.tsnew file mode 100644index 0000000..4ba908d--- /dev/null+++ b/packages/template/src/panZoom.ts@@ -0,0 +1,95 @@+/**+ * Pan and zoom maths for the Shape modal's diagram (Story 14.2), pure so the+ * tests need no browser. A view is `translate(x, y) scale(s)` with the origin at+ * the top-left; sizes are in CSS pixels, content sizes at scale 1.+ */++export interface Size {+ width: number;+ height: number;+}++export interface PanView {+ x: number;+ y: number;+ scale: number;+}++/** Below this, the opening fit stops shrinking: the labels must stay legible. */+export const FIT_FLOOR = 0.5;+export const MIN_SCALE = 0.25;+export const MAX_SCALE = 3;+/** Room kept between the content's edge and the viewport's when panning. */+export const PAN_PAD = 8;++const clamp = (value: number, low: number, high: number): number =>+ Math.min(high, Math.max(low, value));++/**+ * The scale a diagram opens at: small enough to show it whole, never above 1+ * (a narrow diagram is not enlarged) and never below the legibility floor.+ */+export function fitScale(viewport: Size, content: Size, floor: number = FIT_FLOOR): number {+ if (content.width <= 0 || content.height <= 0 || viewport.width <= 0 || viewport.height <= 0)+ return 1;+ const fit = Math.min(1, viewport.width / content.width, viewport.height / content.height);+ return Math.max(floor, fit);+}++/** The allowed range of one axis: content inside the viewport, or the viewport inside the content. */+function axisRange(view: number, content: number, pad: number): [number, number] {+ const a = pad;+ const b = view - content - pad;+ return [Math.min(a, b), Math.max(a, b)];+}++/**+ * Keeps the content reachable: a part smaller than the viewport stays inside+ * it, a larger one cannot be dragged past its edge plus the padding.+ */+export function clampPan(+ view: PanView,+ viewport: Size,+ content: Size,+ pad: number = PAN_PAD,+): PanView {+ const [minX, maxX] = axisRange(viewport.width, content.width * view.scale, pad);+ const [minY, maxY] = axisRange(viewport.height, content.height * view.scale, pad);+ return { scale: view.scale, x: clamp(view.x, minX, maxX), y: clamp(view.y, minY, maxY) };+}++/** Where one axis opens: centred when it fits, else at its start. */+function openAxis(view: number, content: number, pad: number): number {+ return content + 2 * pad <= view ? (view - content) / 2 : pad;+}++/** The opening view: fitted, centred when it fits, otherwise from the top-left. */+export function fitView(viewport: Size, content: Size, pad: number = PAN_PAD): PanView {+ const scale = fitScale(viewport, content);+ return clampPan(+ {+ scale,+ x: openAxis(viewport.width, content.width * scale, pad),+ y: openAxis(viewport.height, content.height * scale, pad),+ },+ viewport,+ content,+ pad,+ );+}++/**+ * Zooms by `factor` keeping the point under (`px`, `py`) — viewport+ * coordinates — where it is, within the scale bounds.+ */+export function zoomAt(+ view: PanView,+ factor: number,+ px: number,+ py: number,+ bounds: { min: number; max: number } = { min: MIN_SCALE, max: MAX_SCALE },+): PanView {+ const scale = clamp(view.scale * factor, bounds.min, bounds.max);+ const ratio = scale / view.scale;+ return { scale, x: px - (px - view.x) * ratio, y: py - (py - view.y) * ratio };+}diff --git a/packages/template/src/styles.css b/packages/template/src/styles.cssindex 8cf74ea..3fb2f99 100644--- a/packages/template/src/styles.css+++ b/packages/template/src/styles.css@@ -2827,6 +2827,68 @@ time {margin: 0 auto;}+/* The Open window's diagram (Story 14.2): a fixed viewport it drags and zooms in. */+.shape-diagram-interactive {+ position: relative;+ overflow: hidden;+}++.shape-viewport {+ position: relative;+ height: min(60vh, 560px);+ overflow: hidden;+ cursor: grab;+ touch-action: none;+ overscroll-behavior: contain;+}++.shape-viewport:focus-visible {+ outline: 2px solid var(--accent);+ outline-offset: 2px;+}++.shape-viewport.is-dragging {+ cursor: grabbing;+ user-select: none;+}++.shape-pan-content {+ position: absolute;+ top: 0;+ left: 0;+ width: max-content;+ transform-origin: 0 0;+}++.shape-diagram-interactive .shape-pan-content svg {+ max-width: none;+ margin: 0;+}++.shape-zoom-controls {+ position: absolute;+ top: 14px;+ right: 14px;+ display: flex;+ gap: 4px;+}++.shape-zoom-controls button {+ min-width: 28px;+ padding: 2px 8px;+ color: var(--text);+ background: var(--panel);+ border: 1px solid var(--chip-line);+ border-radius: 5px;+ font: inherit;+ font-size: 12px;+ cursor: pointer;+}++.shape-zoom-controls button:hover {+ background: var(--hover);+}+.shape-by {color: var(--muted);font-size: 11px;diff --git a/packages/template/test/panZoom.test.tsx b/packages/template/test/panZoom.test.tsxnew file mode 100644index 0000000..11543cf--- /dev/null+++ b/packages/template/test/panZoom.test.tsx@@ -0,0 +1,97 @@+import { createElement } from "react";+import { renderToStaticMarkup } from "react-dom/server";+import { describe, expect, it } from "vitest";+import { MermaidDiagram } from "../src/MermaidDiagram.js";+import { clampPan, fitScale, fitView, zoomAt } from "../src/panZoom.js";++const box = { width: 800, height: 500 };++describe("fitScale", () => {+ it("shrinks a wide diagram to the viewport", () => {+ expect(fitScale(box, { width: 1600, height: 400 })).toBe(0.5);+ expect(fitScale(box, { width: 1000, height: 250 })).toBe(0.8);+ expect(fitScale(box, { width: 400, height: 1000 })).toBe(0.5);+ });++ it("stops at the legibility floor and never enlarges", () => {+ expect(fitScale(box, { width: 8000, height: 300 })).toBe(0.5);+ expect(fitScale(box, { width: 200, height: 100 })).toBe(1);+ expect(fitScale(box, { width: 0, height: 0 })).toBe(1);+ });+});++describe("fitView", () => {+ it("centres a diagram that fits and starts a wider one at the top-left", () => {+ expect(fitView(box, { width: 200, height: 100 })).toEqual({ scale: 1, x: 300, y: 200 });+ const wide = fitView(box, { width: 8000, height: 300 });+ expect(wide.scale).toBe(0.5);+ expect(wide.x).toBe(8);+ expect(wide.y).toBe(175);+ });+});++describe("clampPan", () => {+ it("keeps a large diagram from leaving past its edge plus the padding", () => {+ const content = { width: 2000, height: 1000 };+ expect(clampPan({ scale: 1, x: 500, y: 500 }, box, content)).toEqual({ scale: 1, x: 8, y: 8 });+ expect(clampPan({ scale: 1, x: -5000, y: -5000 }, box, content)).toEqual({+ scale: 1,+ x: 800 - 2000 - 8,+ y: 500 - 1000 - 8,+ });+ });++ it("keeps a small diagram inside the viewport", () => {+ const content = { width: 100, height: 100 };+ expect(clampPan({ scale: 1, x: -50, y: 900 }, box, content)).toEqual({+ scale: 1,+ x: 8,+ y: 500 - 100 - 8,+ });+ });+});++describe("zoomAt", () => {+ it("keeps the point under the pointer still", () => {+ const view = zoomAt({ scale: 1, x: 10, y: 20 }, 2, 110, 120);+ expect(view.scale).toBe(2);+ // The content point under (110, 120) was (100, 100); it still is.+ expect((110 - view.x) / view.scale).toBe(100);+ expect((120 - view.y) / view.scale).toBe(100);+ });++ it("stays within 0.25 and 3", () => {+ expect(zoomAt({ scale: 2, x: 0, y: 0 }, 10, 0, 0).scale).toBe(3);+ expect(zoomAt({ scale: 0.5, x: 0, y: 0 }, 0.01, 0, 0).scale).toBe(0.25);+ });+});++describe("MermaidDiagram controls", () => {+ const source = "flowchart LR\nA --> B";++ it("gives the Open window's diagram its zoom controls and viewport", () => {+ const html = renderToStaticMarkup(+ createElement(MermaidDiagram, { source, interactive: true, available: true }),+ );+ expect(html).toContain("shape-viewport");+ expect(html).toContain('aria-label="Zoom out"');+ expect(html).toContain('aria-label="Fit the diagram"');+ expect(html).toContain('aria-label="Zoom in"');+ });++ it("leaves the panel's preview without them", () => {+ const html = renderToStaticMarkup(+ createElement(MermaidDiagram, { source, fit: true, available: true }),+ );+ expect(html).not.toContain("shape-viewport");+ expect(html).not.toContain("Zoom in");+ });++ it("keeps showing the source on a file:// page", () => {+ const html = renderToStaticMarkup(+ createElement(MermaidDiagram, { source, interactive: true, available: false }),+ );+ expect(html).toContain("shape-source");+ expect(html).not.toContain("Zoom in");+ });+});