cursedbelt 6.32.0 → 6.34.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/react/components/DataTable/useTableViewMode.d.ts +8 -8
- package/dist/react/components/DataTable/useTableViewMode.d.ts.map +1 -1
- package/dist/react/components/DataTable/useTableViewMode.js +8 -8
- package/dist/react/components/DataTable/useTableViewMode.js.map +1 -1
- package/dist/react/deep-link/index.d.ts +1 -1
- package/dist/react/deep-link/index.d.ts.map +1 -1
- package/dist/react/deep-link/index.js +1 -1
- package/dist/react/deep-link/index.js.map +1 -1
- package/dist/react/deep-link/links.d.ts +2 -3
- package/dist/react/deep-link/links.d.ts.map +1 -1
- package/dist/react/deep-link/links.js +4 -7
- package/dist/react/deep-link/links.js.map +1 -1
- package/dist/react/deep-link/resume.d.ts +17 -24
- package/dist/react/deep-link/resume.d.ts.map +1 -1
- package/dist/react/deep-link/resume.js +0 -127
- package/dist/react/deep-link/resume.js.map +1 -1
- package/dist/react/deep-link/useDeepLink.d.ts +4 -3
- package/dist/react/deep-link/useDeepLink.d.ts.map +1 -1
- package/dist/react/deep-link/useDeepLink.js +1 -1
- package/dist/react/deep-link/useDeepLink.js.map +1 -1
- package/dist/react/map-frame/MapFrame.d.ts +58 -8
- package/dist/react/map-frame/MapFrame.d.ts.map +1 -1
- package/dist/react/map-frame/MapFrame.js +114 -17
- package/dist/react/map-frame/MapFrame.js.map +1 -1
- package/dist/react/map-frame/geo.d.ts +66 -1
- package/dist/react/map-frame/geo.d.ts.map +1 -1
- package/dist/react/map-frame/geo.js +99 -1
- package/dist/react/map-frame/geo.js.map +1 -1
- package/dist/react/map-frame/index.d.ts +3 -3
- package/dist/react/map-frame/index.d.ts.map +1 -1
- package/dist/react/map-frame/index.js +2 -2
- package/dist/react/map-frame/index.js.map +1 -1
- package/package.json +1 -1
- package/src/react/components/DataTable/useTableViewMode.ts +9 -10
- package/src/react/deep-link/index.ts +0 -4
- package/src/react/deep-link/links.ts +5 -8
- package/src/react/deep-link/resume.spec.tsx +9 -215
- package/src/react/deep-link/resume.ts +13 -154
- package/src/react/deep-link/useDeepLink.tsx +5 -4
- package/src/react/map-frame/MapFrame.box.spec.tsx +45 -0
- package/src/react/map-frame/MapFrame.cover.spec.tsx +67 -0
- package/src/react/map-frame/MapFrame.tsx +131 -20
- package/src/react/map-frame/clusterFold.spec.ts +54 -0
- package/src/react/map-frame/fit.spec.ts +116 -0
- package/src/react/map-frame/geo.ts +132 -0
- package/src/react/map-frame/index.ts +12 -3
|
@@ -1,35 +1,20 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* The per-account place store's browser client — `/api/resume`, `cursedbelt-server/resume`.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* the
|
|
7
|
-
*
|
|
8
|
-
*
|
|
4
|
+
* ── 🔴 Nothing here redirects. The URL the reader ENTERED is the place ───────
|
|
5
|
+
* The owner, 2026-11-24: *"Since we have deep links available it should always go
|
|
6
|
+
* to the actual entered url instead of trying to force the browser to another
|
|
7
|
+
* url. I want all the apps … to switch to deep links and letting the user choose
|
|
8
|
+
* where they go without the forced redirecting anymore."* That reverses
|
|
9
|
+
* 2026-08-15's "land where you left off": this module's `useResume`, which sent a
|
|
10
|
+
* bare entry to the last stored href, was deleted with music, its one user. A
|
|
11
|
+
* bare entry opens the app's front screen, a bookmark or link opens its place,
|
|
12
|
+
* and no stored value is ever written into the location on load — in any app.
|
|
9
13
|
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* parallel record of "which folder, which tab, which track", which would be a
|
|
14
|
-
* second source of truth that drifts the first time a view gains a control.
|
|
15
|
-
*
|
|
16
|
-
* ── 🔴 A bare entry restores. An explicit deep link ALWAYS wins ──────────────
|
|
17
|
-
* Landing on `#/` or `/` with nothing after it is the only case that restores.
|
|
18
|
-
* If a shared link could be redirected to wherever the reader was last, every
|
|
19
|
-
* link in the fleet would silently bounce — a worse bug than the one being
|
|
20
|
-
* fixed. A visible way back to the true root is required of the app too, and the
|
|
21
|
-
* nav's own home item is it: `navItemProps` targets the view root, and resume
|
|
22
|
-
* only ever runs once per mount, so pressing home stays at home.
|
|
23
|
-
*
|
|
24
|
-
* ── Per user, server-side, not localStorage ─────────────────────────────────
|
|
25
|
-
* The owner comes BACK to an app, plausibly from another device — which is the
|
|
26
|
-
* whole point of the feature and the one thing `localStorage` cannot do. The
|
|
27
|
-
* store is `cursedbelt-server/resume`, keyed by the app's own user id.
|
|
14
|
+
* What remains is the small per-account key/value client it was built on, which
|
|
15
|
+
* a preference that is NOT a place still uses (`useTableViewMode`'s rows-or-cards).
|
|
16
|
+
* A preference may shape the screen at the URL; it may not change the URL.
|
|
28
17
|
*/
|
|
29
|
-
import React from 'react';
|
|
30
|
-
import type { DeepLinkLocation, DeepLinks } from './links.js';
|
|
31
|
-
import { useDeepLink } from './useDeepLink.js';
|
|
32
|
-
|
|
33
18
|
export interface ResumeClient {
|
|
34
19
|
/** The stored href, or null when nothing is stored (or the read failed). */
|
|
35
20
|
read(): Promise<string | null>;
|
|
@@ -98,129 +83,3 @@ export function createResumeClient(options: ResumeClientOptions = {}): ResumeCli
|
|
|
98
83
|
},
|
|
99
84
|
};
|
|
100
85
|
}
|
|
101
|
-
|
|
102
|
-
export interface UseResumeOptions<V extends string> {
|
|
103
|
-
client: ResumeClient;
|
|
104
|
-
/** Off until the session is known — a signed-out read is a wasted 401. */
|
|
105
|
-
enabled?: boolean;
|
|
106
|
-
/** How long a location must hold still before it is recorded. Default 800ms. */
|
|
107
|
-
debounceMs?: number;
|
|
108
|
-
/**
|
|
109
|
-
* Veto a restore. Called with the parsed stored location; return false and the
|
|
110
|
-
* reader stays at the root (a deleted album, a tenant that no longer exists).
|
|
111
|
-
*/
|
|
112
|
-
canRestore?(location: DeepLinkLocation<V>): boolean;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* The RAW entry URL, exactly as `useDeepLink`'s provider reads it.
|
|
117
|
-
*
|
|
118
|
-
* 🔴 Not `href`. `href` is the FORMATTED location, and formatting collapses the
|
|
119
|
-
* one distinction the stale-restore guard below depends on: a bare `/` and an
|
|
120
|
-
* explicit `#/` both format to `#/`, so "has the reader moved?" answered NO for
|
|
121
|
-
* the single most common way of moving — opening the bookmark and then pressing
|
|
122
|
-
* the app's own home/library item while the resume read is still in flight. The
|
|
123
|
-
* restore then landed on top of a place the reader had just asked for.
|
|
124
|
-
*
|
|
125
|
-
* Measured 2026-08-20 as a red `bun run verify`: `apps/music`'s phone specs sign
|
|
126
|
-
* in (entering bare), `goto("/#/")`, and wait for `.track-row`; under the full
|
|
127
|
-
* gate the read is slow enough to lose the race and the page was yanked to the
|
|
128
|
-
* visualizer. Green in isolation, red under load — which is what a race looks
|
|
129
|
-
* like from outside, and why it survived several sessions as "pre-existing".
|
|
130
|
-
*/
|
|
131
|
-
const rawEntryUrl = (): string =>
|
|
132
|
-
typeof window === 'undefined'
|
|
133
|
-
? ''
|
|
134
|
-
: `${window.location.pathname}${window.location.search}${window.location.hash}`;
|
|
135
|
-
|
|
136
|
-
export interface UseResumeResult {
|
|
137
|
-
/** False until the restore attempt has finished — render a spinner, not the root. */
|
|
138
|
-
ready: boolean;
|
|
139
|
-
/** True when this entry was restored rather than asked for. */
|
|
140
|
-
restored: boolean;
|
|
141
|
-
/** Forget the stored place — for a "start me at the top" control. */
|
|
142
|
-
forget(): void;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
export function useResume<V extends string = string>(options: UseResumeOptions<V>): UseResumeResult {
|
|
146
|
-
const { client, enabled = true, debounceMs = 800, canRestore } = options;
|
|
147
|
-
const { links, href, navigateTo, isBareEntry } = useDeepLink<V>();
|
|
148
|
-
const [ready, setReady] = React.useState(!enabled);
|
|
149
|
-
const [restored, setRestored] = React.useState(false);
|
|
150
|
-
const attempted = React.useRef(false);
|
|
151
|
-
const bareOnEntry = React.useRef(isBareEntry);
|
|
152
|
-
|
|
153
|
-
/**
|
|
154
|
-
* 🔴 The restore reads its collaborators through a ref, and the effect below
|
|
155
|
-
* depends on `enabled` ALONE.
|
|
156
|
-
*
|
|
157
|
-
* Every one of them — `navigateTo`, `client`, `canRestore` — has an identity
|
|
158
|
-
* that changes when the location changes, so a dependency list naming them
|
|
159
|
-
* re-runs the effect mid-read. The re-run bailed on `attempted`, its cleanup
|
|
160
|
-
* canceled the read in flight, `ready` never became true, and NOTHING WAS
|
|
161
|
-
* EVER RECORDED. It was invisible in a hook test (the read resolved before
|
|
162
|
-
* anything re-rendered) and reproduced every time in a browser, which is why
|
|
163
|
-
* `apps/music/__smoke/deepLinks.spec.ts` exists.
|
|
164
|
-
*/
|
|
165
|
-
const latest = React.useRef({ canRestore, client, links, navigateTo });
|
|
166
|
-
latest.current = { canRestore, client, links, navigateTo };
|
|
167
|
-
const hrefRef = React.useRef(href);
|
|
168
|
-
hrefRef.current = href;
|
|
169
|
-
|
|
170
|
-
React.useEffect(() => {
|
|
171
|
-
if (!enabled || attempted.current) return;
|
|
172
|
-
attempted.current = true;
|
|
173
|
-
// 🔴 The explicit-link case, and it is checked BEFORE the read so a slow
|
|
174
|
-
// endpoint can never race a reader who arrived with a real link.
|
|
175
|
-
if (!bareOnEntry.current) {
|
|
176
|
-
setReady(true);
|
|
177
|
-
return;
|
|
178
|
-
}
|
|
179
|
-
let unmounted = false;
|
|
180
|
-
// Where the reader was when the read STARTED. If they have moved by the time it
|
|
181
|
-
// answers — pressed a nav item, followed a link, been redirected by a sign-in — the
|
|
182
|
-
// restore is stale and must not drag them back. Same rule as "an explicit deep link
|
|
183
|
-
// wins", applied to a link that arrived a moment later.
|
|
184
|
-
const entryHref = hrefRef.current;
|
|
185
|
-
const entryUrl = rawEntryUrl();
|
|
186
|
-
void latest.current.client.read().then((stored) => {
|
|
187
|
-
// Both, because they catch different moves: `href` catches a navigation to
|
|
188
|
-
// a genuinely different place, and the raw URL catches the bare-to-`#/`
|
|
189
|
-
// move that formatting makes invisible. Either one means the reader has
|
|
190
|
-
// said where they want to be, and a stored place must never overrule that.
|
|
191
|
-
if (unmounted || hrefRef.current !== entryHref || rawEntryUrl() !== entryUrl) {
|
|
192
|
-
setReady(true);
|
|
193
|
-
return;
|
|
194
|
-
}
|
|
195
|
-
const { canRestore: veto, links: table, navigateTo: go } = latest.current;
|
|
196
|
-
if (stored) {
|
|
197
|
-
const parsed = (table as DeepLinks<V>).parse(stored);
|
|
198
|
-
if (!veto || veto(parsed)) {
|
|
199
|
-
// `replace`, so Back leaves the app rather than bouncing between the
|
|
200
|
-
// root and the restored place.
|
|
201
|
-
go(parsed, { replace: true });
|
|
202
|
-
setRestored(true);
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
setReady(true);
|
|
206
|
-
});
|
|
207
|
-
return () => {
|
|
208
|
-
unmounted = true;
|
|
209
|
-
};
|
|
210
|
-
}, [enabled]);
|
|
211
|
-
|
|
212
|
-
// Record where the reader is, once they have stopped moving. Recording the
|
|
213
|
-
// ROOT is deliberate and is how "take me back to the top" sticks.
|
|
214
|
-
React.useEffect(() => {
|
|
215
|
-
if (!enabled || !ready) return;
|
|
216
|
-
const timer = setTimeout(() => void client.write(href), debounceMs);
|
|
217
|
-
return () => clearTimeout(timer);
|
|
218
|
-
}, [client, debounceMs, enabled, href, ready]);
|
|
219
|
-
|
|
220
|
-
const forget = React.useCallback(() => void client.clear(), [client]);
|
|
221
|
-
return { ready, restored, forget };
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
/** Exported for apps that want to record a place without the hook (a player). */
|
|
225
|
-
export const resumeHrefOf = <V extends string>(links: DeepLinks<V>, location: DeepLinkLocation<V>): string =>
|
|
226
|
-
links.format(location);
|
|
@@ -47,12 +47,13 @@ export interface DeepLinkContextValue<V extends string = string> {
|
|
|
47
47
|
*
|
|
48
48
|
* 🔴 It cannot be derived from `href`, which is the formatted location: parsing
|
|
49
49
|
* and formatting a bookmark (`https://music…/`) produces `#/`, which is a real
|
|
50
|
-
* link to the front screen and deliberately not bare.
|
|
51
|
-
*
|
|
50
|
+
* link to the front screen and deliberately not bare. A bare entry opens the
|
|
51
|
+
* front screen like `#/` does — nothing may redirect it to a remembered place
|
|
52
|
+
* (the owner, 2026-11-24; `resume.ts`).
|
|
52
53
|
*/
|
|
53
54
|
entryWasBare: boolean;
|
|
54
55
|
navigate(patch: DeepLinkPatch<V>, options?: DeepLinkNavigateOptions): void;
|
|
55
|
-
/** Navigate to a whole location
|
|
56
|
+
/** Navigate to a whole location. */
|
|
56
57
|
navigateTo(location: DeepLinkLocation<V>, options?: DeepLinkNavigateOptions): void;
|
|
57
58
|
hrefFor(patch: DeepLinkPatch<V>): string;
|
|
58
59
|
}
|
|
@@ -103,7 +104,7 @@ export function DeepLinkProvider<V extends string>({
|
|
|
103
104
|
return { location, href: links.format(location), nonce: 0 };
|
|
104
105
|
});
|
|
105
106
|
// The entry URL, judged once, before anything normalizes it. See `entryWasBare`. A rescued
|
|
106
|
-
// path link names a place, so it is never bare
|
|
107
|
+
// path link names a place, so it is never bare.
|
|
107
108
|
const [entryWasBare] = React.useState(() => links.isBare(entry));
|
|
108
109
|
/** The raw URL the state initializer above actually read — the baseline for the
|
|
109
110
|
* missed-event re-read in the listener effect. `null` when a caller passed
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The mounted frame on a phone-shaped box (forge task 5916): it measures itself and asks for the
|
|
3
|
+
* ground the box shows, so Google's logo and attribution — in the render's corners — are in view, and
|
|
4
|
+
* its distance key sits in the visible corner above them. Reached through `MapFrame` alone, which
|
|
5
|
+
* exists on both sides of the fix.
|
|
6
|
+
*/
|
|
7
|
+
import { afterEach, describe, expect, test } from 'bun:test';
|
|
8
|
+
import { cleanup, render } from '@testing-library/react';
|
|
9
|
+
import { MapFrame, VIEW_HEIGHT } from './MapFrame.js';
|
|
10
|
+
|
|
11
|
+
const LONDON = { lat: 51.5074, lng: -0.1278 };
|
|
12
|
+
/** A 390 phone's Places box, in CSS px: 4:5, so the slice shows the middle 320 of 640 units. */
|
|
13
|
+
const PHONE = { width: 357, height: 446 };
|
|
14
|
+
/** Google's foot in render px (`ATTRIBUTION_FOOT`), restated so this file needs nothing the fix added. */
|
|
15
|
+
const FOOT = 24;
|
|
16
|
+
|
|
17
|
+
afterEach(() => {
|
|
18
|
+
cleanup();
|
|
19
|
+
for (const key of ['clientWidth', 'clientHeight'] as const) delete (HTMLElement.prototype as unknown as Record<string, unknown>)[key];
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
describe('MapFrame — measures its box and asks for what it shows', () => {
|
|
23
|
+
test("on a phone-shaped box the render covers the middle 320 units, and the distance key sits inside them, above Google's foot", () => {
|
|
24
|
+
Object.defineProperty(HTMLElement.prototype, 'clientWidth', { configurable: true, get: () => PHONE.width });
|
|
25
|
+
Object.defineProperty(HTMLElement.prototype, 'clientHeight', { configurable: true, get: () => PHONE.height });
|
|
26
|
+
const { getByTestId } = render(
|
|
27
|
+
<MapFrame center={LONDON} zoom={6} theme='light' label='A map'>
|
|
28
|
+
{() => null}
|
|
29
|
+
</MapFrame>,
|
|
30
|
+
);
|
|
31
|
+
expect(getByTestId('map-preload').getAttribute('src')).toContain('&z=6&w=320&h=400&');
|
|
32
|
+
const plate = getByTestId('map-scale').querySelector('rect') as SVGRectElement;
|
|
33
|
+
expect(Number(plate.getAttribute('x'))).toBeGreaterThanOrEqual(160);
|
|
34
|
+
expect(Number(plate.getAttribute('y')) + Number(plate.getAttribute('height'))).toBeLessThanOrEqual(VIEW_HEIGHT - FOOT);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test('an unmeasured box (0×0) asks for the whole viewport, as before', () => {
|
|
38
|
+
const { getByTestId } = render(
|
|
39
|
+
<MapFrame center={LONDON} zoom={6} theme='light' label='A map'>
|
|
40
|
+
{() => null}
|
|
41
|
+
</MapFrame>,
|
|
42
|
+
);
|
|
43
|
+
expect(getByTestId('map-preload').getAttribute('src')).toContain('&z=6&w=640&h=400&');
|
|
44
|
+
});
|
|
45
|
+
});
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The picture covers what the box SHOWS (forge task 5916): Google's logo and "Map data ©" line sit in
|
|
3
|
+
* the render's corners, so a whole-viewport render sliced into a phone's 4:5 box cropped both away —
|
|
4
|
+
* and the zoom keys a consumer floats at the frame's foot sat on what was left. A narrow cover also
|
|
5
|
+
* lets a zoom past the middle of a step draw from the level above, crisp, under Google's 640.
|
|
6
|
+
*/
|
|
7
|
+
import { describe, expect, test } from 'bun:test';
|
|
8
|
+
import { ATTRIBUTION_FOOT, attributionFoot, imagePlacement, mapImageSource, mapRender, VIEW_HEIGHT, VIEW_WIDTH } from './MapFrame.js';
|
|
9
|
+
|
|
10
|
+
const LONDON = { lat: 51.5074, lng: -0.1278 };
|
|
11
|
+
/** A 390 phone's Places box, in CSS px: 4:5, so the slice shows the middle 320 of 640 units. */
|
|
12
|
+
const PHONE = { width: 357, height: 446 };
|
|
13
|
+
|
|
14
|
+
describe('mapRender — one picture of the ground the box shows', () => {
|
|
15
|
+
test('the whole viewport at a whole level is the 640×400 it always was', () => {
|
|
16
|
+
expect(mapRender(6)).toEqual({ level: 6, width: 640, height: 400, cover: { width: 640, height: 400 } });
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
test("a phone's cover is half the width, at the same level", () => {
|
|
20
|
+
expect(mapRender(6, { width: 320, height: 400 })).toMatchObject({ level: 6, width: 320, height: 400 });
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
test('past the middle of a step, a narrow cover draws from the level ABOVE — shrunk, never stretched', () => {
|
|
24
|
+
const at = mapRender(4.6, { width: 320, height: 400 });
|
|
25
|
+
expect(at.level).toBe(5);
|
|
26
|
+
expect(at.width).toBe(Math.round(320 * 2 ** 0.4));
|
|
27
|
+
expect(at.width).toBeLessThanOrEqual(640);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
test('short of the middle it stays on the level below, so labels are never shown under 1/√2', () => {
|
|
31
|
+
expect(mapRender(4.3, { width: 320, height: 400 }).level).toBe(4);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test("the whole viewport cannot go up (Google's 640 cap), so it stays on the level below", () => {
|
|
35
|
+
expect(mapRender(4.6)).toMatchObject({ level: 4, width: Math.round(640 / 2 ** 0.6) });
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test('a sliver of a box grows to what /api/map draws at its own shape, and says what it covers', () => {
|
|
39
|
+
const at = mapRender(4.9, { width: 640, height: 60 });
|
|
40
|
+
expect(at.height).toBeGreaterThanOrEqual(80);
|
|
41
|
+
expect(at.cover.width / at.cover.height).toBeCloseTo(at.width / at.height, 6);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test("the URL names the render's level and size", () => {
|
|
45
|
+
expect(mapImageSource(LONDON, 4.6, 'dark', { width: 320, height: 400 })).toContain(`&z=5&w=${Math.round(320 * 2 ** 0.4)}&h=${Math.round(400 * 2 ** 0.4)}&`);
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
describe('imagePlacement — a cover is drawn over exactly that ground, centered', () => {
|
|
50
|
+
test('a 320×400 cover at the drawn zoom fills the middle of the viewport', () => {
|
|
51
|
+
const view = { center: LONDON, zoom: 6, width: VIEW_WIDTH, height: VIEW_HEIGHT };
|
|
52
|
+
const at = imagePlacement({ center: LONDON, zoom: 6, cover: { width: 320, height: 400 } }, view);
|
|
53
|
+
expect(at.x).toBeCloseTo(160, 6);
|
|
54
|
+
expect(at.y).toBeCloseTo(0, 6);
|
|
55
|
+
expect(at.width).toBeCloseTo(320, 6);
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
describe("attributionFoot — how high a control floats to clear Google's foot", () => {
|
|
60
|
+
test("a phone box at a whole level: Google's 24 render px at the box's 1.115 CSS px per unit", () => {
|
|
61
|
+
expect(attributionFoot(PHONE, 6)).toBe(Math.ceil(ATTRIBUTION_FOOT * (446 / 400)));
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
test('a picture from the level above is shrunk, and so is its foot', () => {
|
|
65
|
+
expect(attributionFoot(PHONE, 5.6)).toBeLessThan(attributionFoot(PHONE, 6));
|
|
66
|
+
});
|
|
67
|
+
});
|
|
@@ -121,6 +121,7 @@
|
|
|
121
121
|
import React from "react";
|
|
122
122
|
import { CanvasZoomHint } from "../canvas-hud/HudControls.js";
|
|
123
123
|
import { readWheel } from "../canvas-hud/useCanvasWheel.js";
|
|
124
|
+
import { useResizeObserver } from "../hooks/useResizeObserver.js";
|
|
124
125
|
import {
|
|
125
126
|
type LatLng,
|
|
126
127
|
MAX_ZOOM,
|
|
@@ -129,6 +130,8 @@ import {
|
|
|
129
130
|
scaleBar,
|
|
130
131
|
snapZoom,
|
|
131
132
|
unproject,
|
|
133
|
+
VIEW_HEIGHT,
|
|
134
|
+
VIEW_WIDTH,
|
|
132
135
|
type Viewport,
|
|
133
136
|
zoomAnchoredAt,
|
|
134
137
|
} from "./geo.js";
|
|
@@ -140,8 +143,7 @@ import {
|
|
|
140
143
|
* a fixed size is what keeps the cache key space tiny — see `/api/map`. The frame
|
|
141
144
|
* is then CSS-scaled to whatever the layout gives it.
|
|
142
145
|
*/
|
|
143
|
-
export
|
|
144
|
-
export const VIEW_HEIGHT = 400;
|
|
146
|
+
export { VIEW_HEIGHT, VIEW_WIDTH } from "./geo.js";
|
|
145
147
|
|
|
146
148
|
/**
|
|
147
149
|
* 🔴 How far the pointer must travel before this becomes a pan.
|
|
@@ -178,6 +180,9 @@ const ZOOM_PER_WHEEL_PIXEL = 1 / 380;
|
|
|
178
180
|
* a gate that stops guarding the thing it was written for. */
|
|
179
181
|
export const WHEEL_SETTLE_MS = 220;
|
|
180
182
|
|
|
183
|
+
/** How long a resized box must hold its size before the frame asks for a picture of its new shape. */
|
|
184
|
+
const BOX_REST_MS = 250;
|
|
185
|
+
|
|
181
186
|
/**
|
|
182
187
|
* Momentum: how fast a flick decays, as the time constant of an exponential.
|
|
183
188
|
*
|
|
@@ -199,14 +204,77 @@ interface SettledImage {
|
|
|
199
204
|
src: string;
|
|
200
205
|
center: LatLng;
|
|
201
206
|
zoom: number;
|
|
207
|
+
/** The viewport units the picture covers at `zoom`, centered on `center` — see {@link mapRender}. */
|
|
208
|
+
cover: { width: number; height: number };
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** The ground a render must cover, in viewport units: the whole 640×400 unless the frame knows its box. */
|
|
212
|
+
export type MapCover = { width: number; height: number };
|
|
213
|
+
const WHOLE_VIEW: MapCover = { width: VIEW_WIDTH, height: VIEW_HEIGHT };
|
|
214
|
+
/** Google refuses a render past 640 a side (before `scale`), and `/api/map` clamps under 120×80. */
|
|
215
|
+
const RENDER_MAX = 640;
|
|
216
|
+
const RENDER_MIN = { width: 120, height: 80 };
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* 🔴 How tall Google's own foot is in a render, in the render's logical pixels: the logo at its
|
|
220
|
+
* bottom-left and the "Map data ©" line at its bottom-right. Google's terms say neither may be
|
|
221
|
+
* obscured (forge task 5916: the zoom keys sat on the line on every phone), so the frame's distance key
|
|
222
|
+
* sits above it and {@link attributionFoot} tells a consumer how far to lift its own controls.
|
|
223
|
+
*/
|
|
224
|
+
export const ATTRIBUTION_FOOT = 24;
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Pure: the one picture that draws `cover` (viewport units, centered on the view) at `zoom` — which of
|
|
228
|
+
* Google's levels, and how many pixels wide and tall.
|
|
229
|
+
*
|
|
230
|
+
* 🔴 It covers what the box SHOWS, not the whole 640×400 (forge task 5916). The SVG slices the viewport
|
|
231
|
+
* into its box, so a phone's 4:5 box shows only the middle 320 units: a whole-viewport render paid for
|
|
232
|
+
* pixels nobody saw, and cropped Google's logo and attribution — which sit in the render's corners —
|
|
233
|
+
* clean off. A render of the visible area puts both in the box's own corners.
|
|
234
|
+
*
|
|
235
|
+
* And because a narrow cover is small, a zoom past the middle of a step is drawn from the level ABOVE,
|
|
236
|
+
* shrunk (up to 1/√2), instead of the level below stretched (up to 2×) — crisp, still one render, while
|
|
237
|
+
* it fits under Google's 640. Rounded to the NEARER level, never always up: a level above shown at half
|
|
238
|
+
* size halves Google's labels too. A whole zoom with the whole viewport is the 640×400 it always was.
|
|
239
|
+
*/
|
|
240
|
+
export function mapRender(zoom: number, cover: MapCover = WHOLE_VIEW): { level: number; width: number; height: number; cover: MapCover } {
|
|
241
|
+
const below = Math.floor(zoom + 1e-9);
|
|
242
|
+
const fits = (level: number) => cover.width * 2 ** (level - zoom) <= RENDER_MAX + 1e-6 && cover.height * 2 ** (level - zoom) <= RENDER_MAX + 1e-6;
|
|
243
|
+
const level = zoom - below >= 0.5 - 1e-9 && below + 1 <= MAX_ZOOM && fits(below + 1) ? below + 1 : below;
|
|
244
|
+
const factor = 2 ** (level - zoom);
|
|
245
|
+
// A sliver of a box still gets a picture `/api/map` will draw at its own shape: grow it, centered.
|
|
246
|
+
const grow = Math.max(1, RENDER_MIN.width / (cover.width * factor), RENDER_MIN.height / (cover.height * factor));
|
|
247
|
+
const width = Math.min(RENDER_MAX, Math.round(cover.width * factor * grow));
|
|
248
|
+
const height = Math.min(RENDER_MAX, Math.round(cover.height * factor * grow));
|
|
249
|
+
return { level, width, height, cover: { width: width / factor, height: height / factor } };
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Pure: how many CSS pixels at the foot of a frame `box` wide and tall Google's logo and attribution
|
|
254
|
+
* take at `zoom` — {@link ATTRIBUTION_FOOT} render pixels, as the frame stretches the render over the box.
|
|
255
|
+
* A control floated over the frame's bottom edge sits at least this high (forge task 5916). An
|
|
256
|
+
* unmeasured box (0×0) is read as the frame drawn at its logical size.
|
|
257
|
+
*/
|
|
258
|
+
export function attributionFoot(box: { width: number; height: number }, zoom: number): number {
|
|
259
|
+
const measured = box.width > 0 && box.height > 0;
|
|
260
|
+
const cssPerUnit = measured ? Math.max(box.width / VIEW_WIDTH, box.height / VIEW_HEIGHT) : 1;
|
|
261
|
+
const area = visibleArea(box);
|
|
262
|
+
const render = mapRender(zoom, { width: Math.round(area.width), height: Math.round(area.height) });
|
|
263
|
+
return Math.ceil(ATTRIBUTION_FOOT * 2 ** (zoom - render.level) * cssPerUnit);
|
|
202
264
|
}
|
|
203
265
|
|
|
204
|
-
/**
|
|
205
|
-
*
|
|
206
|
-
|
|
266
|
+
/**
|
|
267
|
+
* The URL of one rendered viewport. Pure, and exported so a test can name the picture it expects
|
|
268
|
+
* without restating the query string. {@link mapRender} chooses the level and the size: a zoom BETWEEN
|
|
269
|
+
* two of Google's levels (a `fitTight` view, forge task 5887) is a smaller picture the frame stretches
|
|
270
|
+
* (or shrinks) over the ground it covers, so the placement math stays true and the view still costs one
|
|
271
|
+
* render; a drag or a pinch, which commit whole levels, costs what it always did.
|
|
272
|
+
*/
|
|
273
|
+
export function mapImageSource(center: LatLng, zoom: number, theme: "light" | "dark", cover: MapCover = WHOLE_VIEW): string {
|
|
274
|
+
const render = mapRender(zoom, cover);
|
|
207
275
|
return (
|
|
208
276
|
`/api/map?lat=${center.lat.toFixed(4)}&lng=${center.lng.toFixed(4)}` +
|
|
209
|
-
`&z=${
|
|
277
|
+
`&z=${render.level}&w=${render.width}&h=${render.height}&theme=${theme}`
|
|
210
278
|
);
|
|
211
279
|
}
|
|
212
280
|
|
|
@@ -214,19 +282,20 @@ export function mapImageSource(center: LatLng, zoom: number, theme: "light" | "d
|
|
|
214
282
|
* Where a already-rendered viewport sits inside the one being drawn now.
|
|
215
283
|
*
|
|
216
284
|
* Pure, and the whole reason a committed pan does not flash: it is the same
|
|
217
|
-
* projection a pin gets, applied to
|
|
285
|
+
* projection a pin gets, applied to the picture — the ground it `cover`s (the whole 640×400 unless
|
|
286
|
+
* said), centered on its center. `scale` is the zoom
|
|
218
287
|
* difference (a double-click leaves a half-size image covering the middle of the
|
|
219
288
|
* frame until the closer render lands), and the position is the settled center
|
|
220
289
|
* projected into the current view, minus half the scaled picture.
|
|
221
290
|
*/
|
|
222
291
|
export function imagePlacement(
|
|
223
|
-
settled: { center: LatLng; zoom: number },
|
|
292
|
+
settled: { center: LatLng; zoom: number; cover?: MapCover },
|
|
224
293
|
view: Viewport,
|
|
225
294
|
): { x: number; y: number; width: number; height: number } {
|
|
226
295
|
const scale = 2 ** (view.zoom - settled.zoom);
|
|
227
296
|
const at = project(settled.center, view);
|
|
228
|
-
const width =
|
|
229
|
-
const height =
|
|
297
|
+
const width = (settled.cover ?? WHOLE_VIEW).width * scale;
|
|
298
|
+
const height = (settled.cover ?? WHOLE_VIEW).height * scale;
|
|
230
299
|
return { x: at.x - width / 2, y: at.y - height / 2, width, height };
|
|
231
300
|
}
|
|
232
301
|
|
|
@@ -282,8 +351,10 @@ export interface MapFrameProps extends Omit<React.HTMLAttributes<HTMLDivElement>
|
|
|
282
351
|
/** Announced to assistive technology. The map is decorative without one. */
|
|
283
352
|
label: string;
|
|
284
353
|
/** The URL of one rendered viewport. Defaults to {@link mapImageSource} — the app's own
|
|
285
|
-
* `/api/map` proxy, which `cursedbelt-server/maps-static` serves the shared half of.
|
|
286
|
-
|
|
354
|
+
* `/api/map` proxy, which `cursedbelt-server/maps-static` serves the shared half of. `cover` is
|
|
355
|
+
* the ground the picture must cover (the part of the viewport the frame's box shows), and the
|
|
356
|
+
* frame draws whatever comes back over exactly that; draw it with {@link mapRender}. */
|
|
357
|
+
imageSource?: (center: LatLng, zoom: number, theme: "light" | "dark", cover: MapCover) => string;
|
|
287
358
|
/**
|
|
288
359
|
* What a plain wheel means (DESIGN CNV-1, forge task 5761). `"fullscreen"` (the default) —
|
|
289
360
|
* a map that IS the page, like family's atlas — zooms on every wheel. `"embedded"` — a map
|
|
@@ -354,9 +425,44 @@ export function MapFrame({
|
|
|
354
425
|
height: VIEW_HEIGHT,
|
|
355
426
|
};
|
|
356
427
|
|
|
428
|
+
/**
|
|
429
|
+
* The frame's own box, measured before the first paint — and null until then, because the render
|
|
430
|
+
* covers what the box shows (`mapRender`) and a fetch for the wrong shape is a render nobody sees
|
|
431
|
+
* and somebody pays for. A window dragged wider changes the box every frame; the picture follows
|
|
432
|
+
* once it has held still for {@link BOX_REST_MS}. jsdom measures 0×0, which is the whole viewport.
|
|
433
|
+
*/
|
|
434
|
+
const rootRef = React.useRef<HTMLDivElement | null>(null);
|
|
435
|
+
const [box, setBox] = React.useState<{ width: number; height: number } | null>(null);
|
|
436
|
+
const boxRef = React.useRef(box);
|
|
437
|
+
const boxTimer = React.useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
438
|
+
useResizeObserver(
|
|
439
|
+
rootRef,
|
|
440
|
+
(element) => {
|
|
441
|
+
const next = { width: element.clientWidth, height: element.clientHeight };
|
|
442
|
+
const land = () => {
|
|
443
|
+
boxRef.current = next;
|
|
444
|
+
setBox(next);
|
|
445
|
+
};
|
|
446
|
+
if (boxTimer.current) clearTimeout(boxTimer.current);
|
|
447
|
+
const was = boxRef.current;
|
|
448
|
+
if (was === null) return land();
|
|
449
|
+
if (was.width === next.width && was.height === next.height) return;
|
|
450
|
+
boxTimer.current = setTimeout(land, BOX_REST_MS);
|
|
451
|
+
},
|
|
452
|
+
{ layout: true },
|
|
453
|
+
);
|
|
454
|
+
React.useEffect(() => () => {
|
|
455
|
+
if (boxTimer.current) clearTimeout(boxTimer.current);
|
|
456
|
+
}, []);
|
|
457
|
+
const area = visibleArea(box ?? { width: 0, height: 0 });
|
|
458
|
+
const cover: MapCover = { width: Math.round(area.width), height: Math.round(area.height) };
|
|
459
|
+
/** The committed picture's level and the ground it covers — read by the distance key, and stored
|
|
460
|
+
* with the picture when it lands. */
|
|
461
|
+
const render = mapRender(zoom, cover);
|
|
462
|
+
|
|
357
463
|
/** 🔴 Built from the PROPS, never from `effective`. The committed viewport is
|
|
358
464
|
* the only one that is allowed to cost money. */
|
|
359
|
-
const source = imageSource(center, zoom, theme);
|
|
465
|
+
const source = imageSource(center, zoom, theme, cover);
|
|
360
466
|
|
|
361
467
|
/** The imagery on screen, which is the last one that LOADED — not the one the
|
|
362
468
|
* props ask for. They differ only while a render is in flight. */
|
|
@@ -691,10 +797,10 @@ export function MapFrame({
|
|
|
691
797
|
};
|
|
692
798
|
|
|
693
799
|
const placement = settled ? imagePlacement(settled, view) : null;
|
|
694
|
-
const loading = source !== settled?.src && !failed.has(source);
|
|
800
|
+
const loading = box !== null && source !== settled?.src && !failed.has(source);
|
|
695
801
|
|
|
696
802
|
return (
|
|
697
|
-
<div {...rest} className={className} style={style ? { ...FRAME_STYLE, ...style } : FRAME_STYLE} data-map-frame="" data-interactive={Boolean(onViewChange)}>
|
|
803
|
+
<div {...rest} ref={rootRef} className={className} style={style ? { ...FRAME_STYLE, ...style } : FRAME_STYLE} data-map-frame="" data-interactive={Boolean(onViewChange)}>
|
|
698
804
|
{/*
|
|
699
805
|
* The render currently being fetched, off-screen.
|
|
700
806
|
*
|
|
@@ -713,7 +819,7 @@ export function MapFrame({
|
|
|
713
819
|
style={PRELOAD_STYLE}
|
|
714
820
|
draggable={false}
|
|
715
821
|
data-testid="map-preload"
|
|
716
|
-
onLoad={() => setSettled({ src: source, center, zoom })}
|
|
822
|
+
onLoad={() => setSettled({ src: source, center, zoom, cover: render.cover })}
|
|
717
823
|
onError={() => setFailed((current) => new Set(current).add(source))}
|
|
718
824
|
/>
|
|
719
825
|
) : null}
|
|
@@ -912,7 +1018,7 @@ export function MapFrame({
|
|
|
912
1018
|
)}
|
|
913
1019
|
{children({ project: (point) => project(point, view), view })}
|
|
914
1020
|
</g>
|
|
915
|
-
<ScaleKey view={view} />
|
|
1021
|
+
<ScaleKey view={view} area={area} foot={ATTRIBUTION_FOOT * 2 ** (view.zoom - (settled ? mapRender(settled.zoom, settled.cover).level : render.level))} />
|
|
916
1022
|
</svg>
|
|
917
1023
|
{wheel === "embedded" && onViewChange ? <CanvasZoomHint show={wheelHint} /> : null}
|
|
918
1024
|
</div>
|
|
@@ -941,13 +1047,18 @@ export function MapFrame({
|
|
|
941
1047
|
/** About how wide one character of the 12-unit label runs, so the plate holds a label longer than its bar. */
|
|
942
1048
|
const LABEL_EM = 6.6;
|
|
943
1049
|
|
|
944
|
-
|
|
1050
|
+
/**
|
|
1051
|
+
* 🔴 In the corner the box SHOWS (`area`), above Google's own foot (`foot`, viewport units): at x=12 of
|
|
1052
|
+
* the whole viewport it was cropped away on every phone, and at its foot it would sit on Google's logo
|
|
1053
|
+
* (forge task 5916).
|
|
1054
|
+
*/
|
|
1055
|
+
function ScaleKey({ view, area, foot }: { view: Viewport; area: { x: number; y: number; width: number; height: number }; foot: number }) {
|
|
945
1056
|
const bar = scaleBar(view);
|
|
946
1057
|
// A frame too small (or a zoom too far out) for even the smallest step of the
|
|
947
1058
|
// 1/2/5 series draws nothing rather than a bar labeled with a fraction.
|
|
948
1059
|
if (bar.widthUnits <= 0) return null;
|
|
949
|
-
const left = 12;
|
|
950
|
-
const bottom =
|
|
1060
|
+
const left = area.x + 12;
|
|
1061
|
+
const bottom = area.y + area.height - 8 - foot;
|
|
951
1062
|
return (
|
|
952
1063
|
/*
|
|
953
1064
|
* 🔴 No `aria-hidden`, and the graticule's comment beside it says why: the
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* clusterByScreen's drawn-radius fold (forge tasks 5888 → 5912, moved with the code from photos' Places
|
|
3
|
+
* map test): no two returned tokens' discs touch. It reaches the fold only through clusterByScreen,
|
|
4
|
+
* which existed before the fold did, so a run without the fix fails here rather than on an import.
|
|
5
|
+
*/
|
|
6
|
+
import { describe, expect, it } from "bun:test";
|
|
7
|
+
import { clusterByScreen, type LatLng, project, unproject } from "./geo.js";
|
|
8
|
+
|
|
9
|
+
describe("clusterByScreen with a drawn radius — no token is drawn over another (forge tasks 5888, 5912)", () => {
|
|
10
|
+
const view = { center: { lat: 50, lng: 5 }, zoom: 5, width: 640, height: 400 };
|
|
11
|
+
const at = (x: number, y: number): LatLng => unproject({ x, y }, view);
|
|
12
|
+
const pin = (id: string, x: number, y: number, weight: number) => ({ id, point: at(x, y), weight });
|
|
13
|
+
/** Photos' token: it grows with what it holds, within reach of a thumb at both ends. */
|
|
14
|
+
const tokenRadius = (weight: number): number => Math.min(16, 7 + Math.log2(1 + weight));
|
|
15
|
+
const tokens = (pins: ReturnType<typeof pin>[]) =>
|
|
16
|
+
clusterByScreen(pins, view, 22, { drawnRadius: tokenRadius }).map((c) => ({ ...c, ...project(c.point, view), r: tokenRadius(c.weight) }));
|
|
17
|
+
const touching = (list: ReturnType<typeof tokens>) =>
|
|
18
|
+
list.some((a, i) => list.slice(i + 1).some((b) => Math.hypot(a.x - b.x, a.y - b.y) < a.r + b.r + 2));
|
|
19
|
+
|
|
20
|
+
it("🔴 two heavy pins 24 units apart — past the fixed radius, discs that would touch — are one token holding both", () => {
|
|
21
|
+
const list = tokens([pin("london", 300, 200, 1600), pin("oxford", 324, 200, 174)]);
|
|
22
|
+
expect(list.map((t) => [t.id, t.weight, t.members.length])).toEqual([["london", 1774, 2]]);
|
|
23
|
+
// The heavier place keeps the seat: the token sits by London, not halfway to Oxford.
|
|
24
|
+
expect(list[0]?.x ?? 0).toBeLessThan(312);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it("without a drawn radius the fixed distance is the whole rule, as it always was", () => {
|
|
28
|
+
expect(clusterByScreen([pin("london", 300, 200, 1600), pin("oxford", 324, 200, 174)], view, 22).length).toBe(2);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("two light pins 24 units apart have room, and stay two", () => {
|
|
32
|
+
const list = tokens([pin("a", 300, 200, 1), pin("b", 324, 200, 1)]);
|
|
33
|
+
expect(list.map((t) => t.id).sort()).toEqual(["a", "b"]);
|
|
34
|
+
expect(tokenRadius(1) * 2 + 2).toBeLessThan(24);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("a row of heavy places ends with no two discs touching, nothing lost from the counts, heaviest first", () => {
|
|
38
|
+
const list = tokens([pin("rome", 300, 200, 259), pin("naples", 330, 200, 176), pin("bari", 362, 200, 120), pin("far", 500, 200, 300)]);
|
|
39
|
+
expect(touching(list)).toBe(false);
|
|
40
|
+
expect(list.reduce((n, t) => n + t.weight, 0)).toBe(855);
|
|
41
|
+
expect(list.map((t) => t.weight)).toEqual([...list.map((t) => t.weight)].sort((a, b) => b - a));
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("🔴 a radius with no ceiling — family's, which grows with a cluster's total — still ends with no two discs touching", () => {
|
|
45
|
+
const heaviest = 40;
|
|
46
|
+
const atlasRadius = (weight: number) => 6.5 + Math.sqrt(weight / heaviest) * 4.5;
|
|
47
|
+
const pins = [pin("a", 300, 200, 40), pin("b", 318, 200, 37), pin("c", 328, 200, 11), pin("d", 340, 214, 5)];
|
|
48
|
+
// Past the fixed radius the pairs are two clusters, and their discs touch: the fold is what parts them.
|
|
49
|
+
expect(touching(clusterByScreen(pins, view, 22).map((c) => ({ ...c, ...project(c.point, view), r: atlasRadius(c.weight) })))).toBe(true);
|
|
50
|
+
const list = clusterByScreen(pins, view, 22, { drawnRadius: atlasRadius }).map((c) => ({ ...c, ...project(c.point, view), r: atlasRadius(c.weight) }));
|
|
51
|
+
expect(touching(list)).toBe(false);
|
|
52
|
+
expect(list.reduce((n, t) => n + t.weight, 0)).toBe(93);
|
|
53
|
+
});
|
|
54
|
+
});
|