askrobots / dbbasic-browser
Public
A desktop browser with x402 built in: when a page asks for payment, it pays in stablecoins and shows the content.
danq9
toolbar: Back and Forward are greyed out when there's nowhere to go
cc08c7a
1 week, 2 days ago
History
zoom.ts · 3.0 KB · 78 lines
| 1 | /** |
| 2 | * Page zoom (⌘+ ⌘− ⌘0), remembered per site as other browsers do: zoom one page of a site and its other pages, now |
| 3 | * and after a restart, open at that size. The steps are Chrome's. Only sites are remembered (http and https, by host |
| 4 | * name); the browser's own pages (cards, reader, kept copies) zoom for as long as the tab shows them. |
| 5 | * |
| 6 | * Electron-free so it is unit-tested; main.ts applies the factors to pages and saves the file. |
| 7 | */ |
| 8 | |
| 9 | export const ZOOM_STEPS = [0.25, 0.33, 0.5, 0.67, 0.75, 0.8, 0.9, 1, 1.1, 1.25, 1.5, 1.75, 2, 2.5, 3, 4, 5] as const; |
| 10 | const MIN = 0.25; |
| 11 | const MAX = 5; |
| 12 | /** Sites remembered at most; the oldest change goes first. */ |
| 13 | export const MAX_SITES = 2000; |
| 14 | |
| 15 | export type ZoomMove = "in" | "out" | "reset"; |
| 16 | |
| 17 | /** The next step up or down from a factor, which need not be a step itself (a pinch, an old saved value). */ |
| 18 | export function stepZoom(factor: number, move: ZoomMove): number { |
| 19 | if (move === "reset") return 1; |
| 20 | const f = Number.isFinite(factor) && factor > 0 ? factor : 1; |
| 21 | const eps = 0.005; |
| 22 | if (move === "in") return ZOOM_STEPS.find((s) => s > f + eps) ?? MAX; |
| 23 | return [...ZOOM_STEPS].reverse().find((s) => s < f - eps) ?? MIN; |
| 24 | } |
| 25 | |
| 26 | /** What a page's zoom is remembered under: its host name for a web page, null for anything else. */ |
| 27 | export function zoomSite(url: string): string | null { |
| 28 | try { |
| 29 | const u = new URL(url); |
| 30 | return u.protocol === "https:" || u.protocol === "http:" ? u.hostname.toLowerCase() || null : null; |
| 31 | } catch { |
| 32 | return null; |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | /** "125%" */ |
| 37 | export const zoomLabel = (factor: number): string => `${Math.round(factor * 100)}%`; |
| 38 | |
| 39 | const clean = (f: unknown): number | null => (typeof f === "number" && Number.isFinite(f) && f >= MIN && f <= MAX ? f : null); |
| 40 | |
| 41 | /** The remembered sizes. Only sites not at 100% are kept. */ |
| 42 | export class ZoomStore { |
| 43 | private sites = new Map<string, number>(); |
| 44 | |
| 45 | /** `saved` is the file's text, or null when there is none; a damaged file is treated as empty. */ |
| 46 | constructor(saved: string | null, private save: (text: string) => void) { |
| 47 | if (!saved) return; |
| 48 | try { |
| 49 | const j = JSON.parse(saved) as { v?: number; sites?: Record<string, unknown> }; |
| 50 | if (j && j.v === 1 && j.sites && typeof j.sites === "object") { |
| 51 | for (const [host, f] of Object.entries(j.sites)) { |
| 52 | const c = clean(f); |
| 53 | if (c !== null && c !== 1 && host) this.sites.set(host, c); |
| 54 | } |
| 55 | } |
| 56 | } catch { |
| 57 | /* start empty */ |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | get(site: string | null): number { |
| 62 | return (site && this.sites.get(site)) || 1; |
| 63 | } |
| 64 | |
| 65 | set(site: string | null, factor: number): void { |
| 66 | if (!site) return; |
| 67 | const c = clean(factor); |
| 68 | if (c === null) return; |
| 69 | this.sites.delete(site); // re-inserted last: newest change |
| 70 | if (Math.abs(c - 1) > 0.001) this.sites.set(site, c); |
| 71 | while (this.sites.size > MAX_SITES) this.sites.delete(this.sites.keys().next().value as string); |
| 72 | this.save(JSON.stringify({ v: 1, sites: Object.fromEntries(this.sites) })); |
| 73 | } |
| 74 | |
| 75 | get size(): number { |
| 76 | return this.sites.size; |
| 77 | } |
| 78 | } |