Public: anyone can read this; the crew can change it.

small: the map keeps its position and zoom in the address, so a reload or a shared link shows the same view #12776

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/12754-small-the-map-keeps-its-position-and-zoo → main 6 commits fork 2 threads · 2 unresolved

  • Merged. scout/12754-small-the-map-keeps-its-position-and-zoo was merged into main as fb09333.

Review

Sign in to review this change.
4 files changed +110 −5
docs/design/size-budget.json +1 −1
@@ -8,7 +8,7 @@
88 "css": 43520
99 },
1010 "/map": {
11 "js": 26880,
11 "js": 28160,
1212 "css": 45056
1313 },
1414 "/:owner/:name": {
src/web/pages/map.ts +35 −4
@@ -54,6 +54,7 @@ import {
5454 repoPalette,
5555 } from "../chrome.ts";
5656 import { liveRegionsJs } from "../client.ts";
57 import { clamp, hashOf, parseHash, viewAddress, viewFromAddress } from "../space_view.ts";
5758 import { html, type Html } from "../html.ts";
5859 import { page } from "../layout.ts";
5960 import { btn, disclosure, empty, row } from "../ui.ts";
@@ -1236,6 +1237,11 @@ export const mapCss = /* css */ `
12361237 * under the reader, they are still looking where they were looking.
12371238 * Scroll zooms only with ⌘ or Ctrl (a trackpad pinch sends exactly that),
12381239 * because a page of lanes must still scroll like a page.
1240 *
1241 * The first canvas on the page keeps its view in the address fragment
1242 * (#x=…&y=…&z=…, `parseHash`/`hashOf` of space_view.ts, shipped as source):
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.
12391245 */
12401246 export const mapViewJs = /* js */ `
12411247 (function () {
@@ -1245,6 +1251,25 @@ export const mapViewJs = /* js */ `
12451251 ${countInView.toString()}
12461252 ${showingLine.toString()}
12471253 var MIN = 0.35, MAX = 2.5;
1254 ${clamp}
1255 ${parseHash}
1256 ${hashOf}
1257 ${viewAddress}
1258 ${viewFromAddress}
1259 var timer = 0, hashRead = false;
1260 function primary(canvas) { return document.querySelector('.map-canvas') === canvas; }
1261 function write(id, clear) {
1262 var c = document.querySelector('.map-canvas');
1263 if (!c || c.id !== id || !history.replaceState) return;
1264 history.replaceState(history.state, '', location.pathname + location.search + viewAddress(views[id], clear));
1265 }
1266 function remember(canvas, clear) {
1267 if (!primary(canvas)) return;
1268 clearTimeout(timer);
1269 if (clear) return write(canvas.id, true);
1270 var id = canvas.id;
1271 timer = setTimeout(function () { write(id, false); }, 200);
1272 }
12481273 var views = {};
12491274 var folds = {};
12501275 function all(root) { return Array.prototype.slice.call((root || document).querySelectorAll('.map-canvas')); }
@@ -1289,8 +1314,14 @@ export const mapViewJs = /* js */ `
12891314 canvas.classList.add('is-view');
12901315 canvas.style.height = ch + 'px';
12911316 var v = views[canvas.id];
1292 if (!v || refit || !v.touched) v = views[canvas.id] = { k: fitK, x: 0, y: 0, touched: false };
1293 v.cw = cw; v.ch = ch;
1317 if (!v || refit || !v.touched) {
1318 v = views[canvas.id] = { k: fitK, x: 0, y: 0, touched: false };
1319 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 if (primary(canvas)) hashRead = true;
1321 if (from) v = views[canvas.id] = from;
1322 if (refit) remember(canvas, true);
1323 }
1324 v.cw = cw; v.ch = ch; v.fitK = fitK;
12941325 apply(canvas);
12951326 }
12961327 function zoom(canvas, factor, cx, cy) {
@@ -1299,12 +1330,12 @@ export const mapViewJs = /* js */ `
12991330 if (k === v.k) return;
13001331 if (cx === undefined) { cx = v.cw / 2; cy = v.ch / 2; }
13011332 v.x += cx / v.k - cx / k; v.y += cy / v.k - cy / k; v.k = k; v.touched = true;
1302 apply(canvas);
1333 apply(canvas); remember(canvas);
13031334 }
13041335 function pan(canvas, dx, dy) {
13051336 var v = views[canvas.id]; if (!v) return;
13061337 v.x -= dx / v.k; v.y -= dy / v.k; v.touched = true;
outdated

Keyboard Fit (the 0 key handler, a few lines below) sets touched = true after setup(canvas, true). This timer reads v.touched when it fires, so it writes a fragment instead of clearing it. Decide clear versus write when the action happens.

1307 apply(canvas);
1338 apply(canvas); remember(canvas);
13081339 }
13091340 function canvasOf(e) { return e.target && e.target.closest ? e.target.closest('.map-canvas') : null; }
13101341 function at(canvas, e) { var r = canvas.getBoundingClientRect(); return [e.clientX - r.left, e.clientY - r.top]; }
src/web/space_view.ts +22
@@ -97,6 +97,28 @@ export function hashOf(view: any, fitK: number, live: boolean, T: number): strin
9797 return h;
9898 }
9999
100 /**
101 * The address fragment for a map canvas's view `v` ({x, y, k, cw, ch, fitK,
102 * touched}; x, y the top-left of what shows, in world units): the centre and
103 * the zoom against Fit, as `#x=…&y=…&z=…`. "" (no fragment) when `clear` (Fit
104 * was just pressed, decided then, not when a timer fires) or the view was
105 * never moved.
106 */
107 export function viewAddress(v: any, clear: boolean): string {
108 if (clear || !v || !v.touched) return "";
109 return "#" + hashOf({ x: v.x + v.cw / v.k / 2, y: v.y + v.ch / v.k / 2, k: v.k }, v.fitK, true, 0);
110 }
111
112 /**
113 * The view an address fragment names for a canvas `cw`×`ch` px, the inverse of
114 * `viewAddress`; null when it names none (junk, missing), so the map fits.
115 */
116 export function viewFromAddress(hash: string, S: any, fitK: number, lim: number[], cw: number, ch: number): any {
117 const p = parseHash(hash, S, fitK, lim);
118 if (!p.k) return null;
119 return { k: p.k, x: p.x - cw / p.k / 2, y: p.y - ch / p.k / 2, touched: true };
120 }
121
100122 /** The issue row whose cell holds world point (wx, wy) at `T`, nearest the centre; or null. */
101123 export function pickIssue(rows: any[][], clusters: any[][], T: number, wx: number, wy: number): any[] | null {
102124 let best: any[] | null = null;
test/map-view-address.test.ts added +52
@@ -0,0 +1,52 @@
1 import { describe, expect, test } from "bun:test";
2 import { hashOf, parseHash, viewAddress, viewFromAddress } from "../src/web/space_view.ts";
3 import { mapViewJs } from "../src/web/pages/map.ts";
4
5 const S = { world: { w: 1000, h: 800 }, weekStart: 0, now: 0 };
6 const lim = [0.35, 2.5];
7
8 describe("the map view in the address", () => {
9 test("write then parse restores the view", () => {
10 const h = hashOf({ x: 400, y: 300, k: 1.5 }, 0.75, true, 0);
11 expect(h).toBe("x=400&y=300&z=2.000");
12 expect(parseHash("#" + h, S, 0.75, lim)).toEqual({ x: 400, y: 300, k: 1.5 });
13 });
14 test("junk is ignored, so the map fits", () => {
15 for (const j of ["", "#", "#x=a&y=b&z=c", "#x=1&y=2", "#x=1&y=2&z=0", "#x=&y=&z="])
16 expect(parseHash(j, S, 1, lim)).toEqual({});
17 });
18 test("numbers are clamped into the plane and the zoom range", () => {
19 expect(parseHash("#x=-5&y=99999&z=100", S, 1, lim)).toEqual({ x: 0, y: 800, k: 2.5 });
20 });
21 test("the shipped script writes with replaceState, reads once, and Fit clears", () => {
22 expect(mapViewJs).toContain("history.replaceState");

These string-contains checks pass even when Fit leaves the fragment in place, as it does for the 0 key. Test the real behaviour: run the conversion, or the script against a stub of location and history, and assert that Fit clears the fragment and that centre and offset round-trip.

23 expect(mapViewJs).not.toContain("pushState");
24 expect(mapViewJs).toContain("function parseHash");
25 expect(mapViewJs).toContain("function hashOf");
26 expect(mapViewJs).toContain("if (refit) remember(canvas, true)");
27 expect(() => new Function(mapViewJs)).not.toThrow();
28 });
29 const view = { x: 100, y: 50, k: 1.5, cw: 600, ch: 300, fitK: 0.75, touched: true };
30 test("the address names the centre, and restoring it puts the same centre back", () => {
31 expect(viewAddress(view, false)).toBe("#x=300&y=150&z=2.000");
32 const back = viewFromAddress(viewAddress(view, false), S, 0.75, lim, 600, 300);
33 expect(back).toEqual({ k: 1.5, x: 100, y: 50, touched: true });
34 // a different canvas size keeps the centre, not the corner
35 const wide = viewFromAddress("#x=400&y=200&z=2.000", S, 0.75, lim, 1000, 400);
36 expect(wide.x + 1000 / wide.k / 2).toBeCloseTo(400);
37 expect(wide.y + 400 / wide.k / 2).toBeCloseTo(200);
38 });
39 test("Fit clears the address, even when the view is marked touched (the 0 key)", () => {
40 expect(viewAddress(view, true)).toBe("");
41 expect(viewAddress({ ...view, touched: false }, false)).toBe("");
42 expect(viewAddress(undefined, false)).toBe("");
43 });
44 test("an address that names no view restores none", () => {
45 expect(viewFromAddress("#x=1", S, 1, lim, 600, 300)).toBeNull();
46 expect(viewFromAddress("", S, 1, lim, 600, 300)).toBeNull();
47 });
48 test("Fit decides at the keypress, and the timer writes only for the first canvas", () => {
49 expect(mapViewJs).toContain("if (clear) return write(canvas.id, true)");
50 expect(mapViewJs).toContain("c.id !== id");
51 });
52 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs