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
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/12754-small-the-map-keeps-its-position-and-zoo → main 6 commits fork 2 threads · 2 unresolved
4 files
changed +110
−5
docs/design/size-budget.json +1 −1
| @@ -8,7 +8,7 @@ | ||
| 8 | 8 | "css": 43520 |
| 9 | 9 | }, |
| 10 | 10 | "/map": { |
| 11 | "js": 26880, | |
| 11 | "js": 28160, | |
| 12 | 12 | "css": 45056 |
| 13 | 13 | }, |
| 14 | 14 | "/:owner/:name": { |
src/web/pages/map.ts +35 −4
| @@ -54,6 +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 | 58 | import { html, type Html } from "../html.ts"; |
| 58 | 59 | import { page } from "../layout.ts"; |
| 59 | 60 | import { btn, disclosure, empty, row } from "../ui.ts"; |
| @@ -1236,6 +1237,11 @@ export const mapCss = /* css */ ` | ||
| 1236 | 1237 | * under the reader, they are still looking where they were looking. |
| 1237 | 1238 | * Scroll zooms only with ⌘ or Ctrl (a trackpad pinch sends exactly that), |
| 1238 | 1239 | * 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. | |
| 1239 | 1245 | */ |
| 1240 | 1246 | export const mapViewJs = /* js */ ` |
| 1241 | 1247 | (function () { |
| @@ -1245,6 +1251,25 @@ export const mapViewJs = /* js */ ` | ||
| 1245 | 1251 | ${countInView.toString()} |
| 1246 | 1252 | ${showingLine.toString()} |
| 1247 | 1253 | 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 | } | |
| 1248 | 1273 | var views = {}; |
| 1249 | 1274 | var folds = {}; |
| 1250 | 1275 | function all(root) { return Array.prototype.slice.call((root || document).querySelectorAll('.map-canvas')); } |
| @@ -1289,8 +1314,14 @@ export const mapViewJs = /* js */ ` | ||
| 1289 | 1314 | canvas.classList.add('is-view'); |
| 1290 | 1315 | canvas.style.height = ch + 'px'; |
| 1291 | 1316 | 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; | |
| 1294 | 1325 | apply(canvas); |
| 1295 | 1326 | } |
| 1296 | 1327 | function zoom(canvas, factor, cx, cy) { |
| @@ -1299,12 +1330,12 @@ export const mapViewJs = /* js */ ` | ||
| 1299 | 1330 | if (k === v.k) return; |
| 1300 | 1331 | if (cx === undefined) { cx = v.cw / 2; cy = v.ch / 2; } |
| 1301 | 1332 | 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); | |
| 1303 | 1334 | } |
| 1304 | 1335 | function pan(canvas, dx, dy) { |
| 1305 | 1336 | var v = views[canvas.id]; if (!v) return; |
| 1306 | 1337 | v.x -= dx / v.k; v.y -= dy / v.k; v.touched = true; |
| 1307 | apply(canvas); | |
| 1338 | apply(canvas); remember(canvas); | |
| 1308 | 1339 | } |
| 1309 | 1340 | function canvasOf(e) { return e.target && e.target.closest ? e.target.closest('.map-canvas') : null; } |
| 1310 | 1341 | 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 | ||
| 97 | 97 | return h; |
| 98 | 98 | } |
| 99 | 99 | |
| 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 | ||
| 100 | 122 | /** The issue row whose cell holds world point (wx, wy) at `T`, nearest the centre; or null. */ |
| 101 | 123 | export function pickIssue(rows: any[][], clusters: any[][], T: number, wx: number, wy: number): any[] | null { |
| 102 | 124 | 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 |
||
| 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 | }); |
new above
offline
Keyboard Fit (the
0key handler, a few lines below) setstouched = trueaftersetup(canvas, true). This timer readsv.touchedwhen it fires, so it writes a fragment instead of clearing it. Decide clear versus write when the action happens.