Public: anyone can read this; the crew can change it.
The map zooms by wheel, pinch and +/−, with "fit all", and the zoom is kept in the address #15611
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/15587-the-map-zooms-by-wheel-pinch-and-with-fi → main 0 commits fork 4 threads · 4 unresolved
docs/features/map.md +2
| @@ -21,3 +21,5 @@ Focus a map (click it or Tab to it) and it answers keys, only while focus is ins | ||
| 21 | 21 | - **`+`** (or `=`) zooms in, **`-`** zooms out, **`0`** fits the drawing again. |
| 22 | 22 | |
| 23 | 23 | A polite live line under the map (`data-map-live`) says what is on screen after the view settles (400 ms after the last key): `Showing 3 of 12 issues`, counting the cards that overlap the visible frame. The focused map has an accent outline. The table of keys is `src/web/map_keys.ts`; the page script embeds its functions, so the tests run the same code. |
| 24 | ||
| 25 | **Fit** (the button, or `0`) frames the whole drawing, every node inside the canvas, down to the zoom floor (0.35); the plain first view still cuts a tall map at a level's foot. Decision: a plain wheel scrolls the page and does not zoom; zooming by wheel takes ⌘ or Ctrl held (a trackpad pinch sends that), because the map sits in a page of lanes that must still scroll; the view stays in the address. |
src/web/pages/map.ts +8 −5
| @@ -54,7 +54,7 @@ import { | ||
| 54 | 54 | repoPalette, |
| 55 | 55 | } from "../chrome.ts"; |
| 56 | 56 | import { liveRegionsJs } from "../client.ts"; |
| 57 | import { clamp, hashOf, parseHash, viewAddress, viewFromAddress } from "../space_view.ts"; | |
| 57 | import { clamp, fitAll, hashOf, parseHash, viewAddress, viewFromAddress } from "../space_view.ts"; | |
| 58 | 58 | import { html, type Html } from "../html.ts"; |
| 59 | 59 | import { page } from "../layout.ts"; |
| 60 | 60 | import { btn, disclosure, empty, row } from "../ui.ts"; |
| @@ -1241,7 +1241,8 @@ export const mapCss = /* css */ ` | ||
| 1241 | 1241 | * The first canvas on the page keeps its view in the address fragment |
| 1242 | 1242 | * (#x=…&y=…&z=…, `parseHash`/`hashOf` of space_view.ts, shipped as source): |
| 1243 | 1243 | * written debounced with replaceState, read once when the view is first made, |
| 1244 | * cleared by Fit. A swap keeps the view by id, so it never rereads the hash. | |
| 1244 | * cleared by Fit, which frames the whole drawing (`fitAll`, down to the zoom floor). A swap keeps | |
| 1245 | * the view by id, so it never rereads the hash. | |
| 1245 | 1246 | */ |
| 1246 | 1247 | export const mapViewJs = /* js */ ` |
| 1247 | 1248 | (function () { |
| @@ -1252,6 +1253,7 @@ export const mapViewJs = /* js */ ` | ||
| 1252 | 1253 | ${showingLine.toString()} |
| 1253 | 1254 | var MIN = 0.35, MAX = 2.5; |
| 1254 | 1255 | ${clamp} |
| 1256 | ${fitAll} | |
| 1255 | 1257 | ${parseHash} |
| 1256 | 1258 | ${hashOf} |
| 1257 | 1259 | ${viewAddress} |
| @@ -1293,7 +1295,7 @@ export const mapViewJs = /* js */ ` | ||
| 1293 | 1295 | live.textContent = showingLine(countInView(rects, [c.left, c.top, c.right, c.bottom]), nodes.length); |
| 1294 | 1296 | }, QUIET); |
| 1295 | 1297 | } |
| 1296 | function setup(canvas, refit) { | |
| 1298 | function setup(canvas, refit, frameAll) { | |
| 1297 | 1299 | if (!canvas.id || !canvas.querySelector('.map-svg') || !canvas.clientWidth) return; |
| 1298 | 1300 | var W = +canvas.getAttribute('data-w') || 1, H = +canvas.getAttribute('data-h') || 1; |
| 1299 | 1301 | var cw = canvas.clientWidth; |
| @@ -1319,6 +1321,7 @@ export const mapViewJs = /* js */ ` | ||
| 1319 | 1321 | var from = !refit && !hashRead && primary(canvas) ? viewFromAddress(location.hash, { world: { w: W, h: H }, weekStart: 0, now: 0 }, fitK, [MIN, MAX], cw, ch) : null; |
| 1320 | 1322 | if (primary(canvas)) hashRead = true; |
| 1321 | 1323 | if (from) v = views[canvas.id] = from; |
| 1324 | if (refit && frameAll) { var all = fitAll(W, H, cw, ch, MIN, MAX); v.k = all.k; v.x = all.x; v.y = all.y; } | |
| 1322 | 1325 | if (refit) remember(canvas, true); |
| 1323 | 1326 | } |
| 1324 | 1327 | v.cw = cw; v.ch = ch; v.fitK = fitK; |
| @@ -1391,7 +1394,7 @@ export const mapViewJs = /* js */ ` | ||
| 1391 | 1394 | var tool = e.target.closest('[data-map]'); |
| 1392 | 1395 | if (!tool) return; |
| 1393 | 1396 | var what = tool.getAttribute('data-map'); |
| 1394 | if (what === 'fit') setup(canvas, true); else zoom(canvas, what === 'in' ? 1.25 : 0.8); | |
| 1397 | if (what === 'fit') setup(canvas, true, true); else zoom(canvas, what === 'in' ? 1.25 : 0.8); | |
outdated
Same attribute toggle, duplicated in the keydown handler. One parameter removes both. |
||
| 1395 | 1398 | }, true); |
| 1396 | 1399 | document.addEventListener('keydown', function (e) { |
| 1397 | 1400 | var canvas = canvasOf(e); |
| @@ -1401,7 +1404,7 @@ export const mapViewJs = /* js */ ` | ||
| 1401 | 1404 | var what = mapAction(KEYS, e.key, e.ctrlKey, e.metaKey, e.altKey); |
| 1402 | 1405 | if (!what) return; |
| 1403 | 1406 | e.preventDefault(); |
| 1404 | if (what === 'fit') { setup(canvas, true); views[canvas.id].touched = true; announce(canvas); } | |
| 1407 | if (what === 'fit') { setup(canvas, true, true); views[canvas.id].touched = true; announce(canvas); } | |
| 1405 | 1408 | else if (what === 'zoom-in') zoom(canvas, 1.25); |
| 1406 | 1409 | else if (what === 'zoom-out') zoom(canvas, 0.8); |
| 1407 | 1410 | else pan(canvas, what === 'pan-left' ? STEP : what === 'pan-right' ? -STEP : 0, what === 'pan-up' ? STEP : what === 'pan-down' ? -STEP : 0); |
src/web/space_view.ts +10
| @@ -167,3 +167,13 @@ export function firstFound(rows: any[][], query: string, T: number): { row: any[ | ||
| 167 | 167 | |
| 168 | 168 | /** The source the page ships for these functions. */ |
| 169 | 169 | export const viewJs = [clamp, ease, placeAt, crewAt, parseHash, hashOf, pickIssue, findBodies, firstFound].map((f) => f.toString()).join("\n"); |
| 170 | ||
| 171 | /** | |
| 172 | * The view that frames the whole drawing (W×H) in a canvas (cw×ch): the | |
| 173 | * largest zoom that shows every node, held to the zoom range, centred. | |
| 174 | * Shipped as source to the map script, so it uses nothing outside itself. | |
| 175 | */ | |
| 176 | export function fitAll(W: number, H: number, cw: number, ch: number, lo: number, hi: number): { k: number; x: number; y: number } { | |
| 177 | const k = Math.min(hi, Math.max(lo, Math.min(cw / W, ch / H))); | |
| 178 | return { k, x: (W - cw / k) / 2, y: (H - ch / k) / 2 }; | |
| 179 | } |
test/map-view-address.test.ts +67
| @@ -50,3 +50,70 @@ describe("the map view in the address", () => { | ||
| 50 | 50 | expect(mapViewJs).toContain("c.id !== id"); |
| 51 | 51 | }); |
| 52 | 52 | }); |
| 53 | ||
| 54 | describe("fit all", () => { | |
| 55 | type Box = [number, number, number, number]; | |
| 56 | test("when the drawing fits the zoom range, every corner lands inside the canvas", async () => { | |
| 57 | const { fitAll } = await import("../src/web/space_view.ts"); | |
outdated
Cases 2 and 3 floor at k=0.35 and skip every assertion, so only case 1 is tested. The name says every node lands inside the canvas, which is false at the floor. Add fitting cases and a floored case that asserts the actual result. |
||
| 58 | const cases: Box[] = [[1000, 800, 600, 300], [400, 3000, 900, 2000], [2000, 500, 1000, 500], [300, 200, 900, 600]]; | |
| 59 | for (const [W, H, cw, ch] of cases) { | |
| 60 | const v = fitAll(W, H, cw, ch, 0.35, 2.5); | |
| 61 | expect(v.k).toBeGreaterThan(0.35); | |
| 62 | expect(v.k).toBeLessThan(2.5 + 1e-9); | |
| 63 | for (const [wx, wy] of [[0, 0], [W, 0], [0, H], [W, H]] as [number, number][]) { | |
| 64 | const sx = (wx - v.x) * v.k, sy = (wy - v.y) * v.k; | |
| 65 | expect(sx).toBeGreaterThanOrEqual(-0.01); | |
| 66 | expect(sx).toBeLessThanOrEqual(cw + 0.01); | |
| 67 | expect(sy).toBeGreaterThanOrEqual(-0.01); | |
| 68 | expect(sy).toBeLessThanOrEqual(ch + 0.01); | |
| 69 | } | |
| 70 | } | |
| 71 | }); | |
outdated
Counting the substring data-frame-all passes with dead code. This would not fail if Fit stopped framing everything. Test the behaviour. |
||
| 72 | test("past the floor it stops at the floor, centred, and says so by overflowing evenly", async () => { | |
| 73 | const { fitAll } = await import("../src/web/space_view.ts"); | |
| 74 | const v = fitAll(400, 3000, 900, 300, 0.35, 2.5); | |
| 75 | expect(v.k).toBe(0.35); | |
| 76 | expect(v.y + 300 / 0.35 / 2).toBeCloseTo(1500); | |
| 77 | expect(v.x + 900 / 0.35 / 2).toBeCloseTo(200); | |
| 78 | expect(fitAll(100, 100, 1000, 1000, 0.35, 2.5).k).toBe(2.5); | |
| 79 | }); | |
| 80 | test("the shipped script's Fit button and 0 key frame the whole drawing, and plain setup does not", () => { | |
| 81 | const handlers: Record<string, ((e: any) => void)[]> = {}; | |
| 82 | const boxes: string[] = []; | |
| 83 | const attrs: Record<string, string> = { "data-w": "1000", "data-h": "800", "data-cap": "0.72" }; | |
| 84 | const svg = { setAttribute: (_: string, v: string) => boxes.push(v) }; | |
| 85 | const canvas: any = { | |
| 86 | id: "c1", clientWidth: 600, | |
| 87 | querySelector: (q: string) => (q === ".map-svg" ? svg : null), | |
| 88 | querySelectorAll: () => [], | |
| 89 | getAttribute: (n: string) => attrs[n] ?? null, | |
| 90 | hasAttribute: () => false, | |
| 91 | classList: { toggle() {}, add() {}, contains: () => false }, | |
| 92 | style: {}, | |
| 93 | closest: () => canvas, | |
| 94 | getBoundingClientRect: () => ({ left: 0, top: 0, right: 600, bottom: 300 }), | |
| 95 | }; | |
| 96 | const doc: any = { | |
| 97 | querySelector: () => canvas, querySelectorAll: () => [canvas], | |
| 98 | addEventListener: (t: string, f: any) => (handlers[t] ||= []).push(f), | |
| 99 | }; | |
| 100 | const win: any = { innerHeight: 400, addEventListener() {} }; | |
| 101 | const hist = { state: null, replaceState() {} }; | |
| 102 | new Function("document", "window", "location", "history", "setTimeout", "clearTimeout", mapViewJs)( | |
| 103 | doc, win, { hash: "", pathname: "/map", search: "" }, hist, () => 0, () => {}, | |
| 104 | ); | |
| 105 | const view = (s: string) => s.split(" ").map(Number); | |
| 106 | const first = view(boxes.at(-1)!); | |
| 107 | expect(first[2]).toBeCloseTo(1000); // plain setup fits the width (k = 0.6) | |
| 108 | expect(first[3]!).toBeLessThan(800); // and cuts the tall drawing | |
| 109 | boxes.length = 0; | |
| 110 | const tool = { getAttribute: () => "fit" }; | |
| 111 | handlers.click![0]!({ target: { closest: (q: string) => (q === "[data-map]" ? tool : canvas) }, preventDefault() {}, stopPropagation() {} }); | |
| 112 | const [x, y, w, h] = view(boxes.at(-1)!) as [number, number, number, number]; | |
| 113 | // canvas is 600 x (cut to 288): the whole 1000 x 800 plane is inside the viewBox | |
| 114 | expect(x).toBeLessThanOrEqual(0.01); | |
| 115 | expect(y).toBeLessThanOrEqual(0.01); | |
| 116 | expect(x + w).toBeGreaterThanOrEqual(1000 - 0.01); | |
| 117 | expect(y + h).toBeGreaterThanOrEqual(800 - 0.01); | |
| 118 | }); | |
| 119 | }); |
Fit all is implemented as a flag set on the canvas and removed after setup(). Pass a frameAll argument to setup(canvas, refit, frameAll) instead; a thrown error here would leave the attribute stuck.