From 4dfd97d19245385e8f1b98f3eb18e57a416d40f1 Mon Sep 17 00:00:00 2001 From: "Fabian @ Blax Software" Date: Mon, 26 Oct 2026 23:04:32 +0100 Subject: [PATCH] A browser back and forward through the circuits (mouse side buttons) Every step into or out of a circuit, and to another project, is a browser history entry holding the circuit and its crumbs. The mouse's back and forward buttons, Alt + arrows and the browser's buttons go through them as visited; a reload, or going back to an entry from before one, opens that entry's circuit. In Chrome the mouse's back button on the first entry goes up a crumb instead of leaving the editor. Share and puzzle pages open their link only on the first load, so history landing on them does not reload it, and the account sync starts once per page load. Co-Authored-By: Claude Opus 5.5 --- README.md | 2 +- app/components/editor/Help.vue | 3 +- app/composables/useEditor.ts | 110 ++++++++++++++++-- app/pages/index.vue | 5 +- app/pages/p/[id].vue | 3 +- app/pages/puzzle/[id].vue | 3 +- tests/nav-history.test.ts | 198 +++++++++++++++++++++++++++++++++ 7 files changed, 309 insertions(+), 15 deletions(-) create mode 100644 tests/nav-history.test.ts diff --git a/README.md b/README.md index d22eec8..a2c14fd 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Nuxt 4 (client-only editor, `ssr: false`), Tailwind CSS v4, Lucide icons via `@n ## What it does - Parts: NAND, input switch, output pin, lamp, push button, clock, constant 1, ground, hex display, pixel, cable bundle and split, input panel, note. -- Layout like Figma: two floating rounded bars over the canvas, a panel on the left for the circuit you are in and one on the right for what is selected. The top bar, across the whole width: menu (pause menu) with the project name, "Main" and the way into the open component (each name leads back; every other circuit is a component and opens with a double-click, or the pencil in the Components list), Challenges (campaign, puzzle of the week, puzzles, challenges in a drop-down), the clock, search, undo, sound, settings, help, share and account. The bottom bar: your pinned parts first (NAND, AND, OR, NOT, XOR, input, output, lamp, clock to start with; pin from the lists, right-click to unpin, kept per browser, components by name), then one button per kind of part (Logic, Inputs, Outputs, Wiring, Notes, Connectors, Components) that opens its parts on pointing, and a search that opens quick add. +- Layout like Figma: two floating rounded bars over the canvas, a panel on the left for the circuit you are in and one on the right for what is selected. The top bar, across the whole width: menu (pause menu) with the project name, "Main" and the way into the open component (each name leads back; every other circuit is a component and opens with a double-click, or the pencil in the Components list); every step into or out of a circuit, and to another project, is an entry in the browser's history, so the mouse's back and forward buttons (and Alt + ← →, the browser's buttons) go through them as visited, crumbs included, also after a reload. In Chrome the mouse's back button never leaves the editor: on the first entry it goes up a crumb. A share or puzzle address opens its link only when the page is loaded with it, not when going back or forward lands on it, Challenges (campaign, puzzle of the week, puzzles, challenges in a drop-down), the clock, search, undo, sound, settings, help, share and account. The bottom bar: your pinned parts first (NAND, AND, OR, NOT, XOR, input, output, lamp, clock to start with; pin from the lists, right-click to unpin, kept per browser, components by name), then one button per kind of part (Logic, Inputs, Outputs, Wiring, Notes, Connectors, Components) that opens its parts on pointing, and a search that opens quick add. - Small windows: the bars keep their steady centred width on wide screens and are never narrower than 760px as far as the panels leave room; they move off the centre rather than under a panel. On a narrow bar the words go first, then the names between Main and the open component fold into "…" (a menu leading back to each), and sound, settings and help (then undo, redo and share, then challenges and search) move into a "⋯" menu; Main is always there. From 821 to 1100px the selection's panel floats over the canvas edge between the bars instead of narrowing the canvas; below that the panels sit under the canvas. Drop-downs, flyouts and the search box stay on screen. - Quick add (Space, /, Shift+A, Ctrl+K): parts and actions at the mouse. Shift+Enter (or Ctrl+Enter, or the pencil on its line) opens the chosen component to edit it instead of placing it, and typing a name lists "Open " for every circuit of the project, also those that cannot be placed here; the one you are in shows as where you are. Going to one on the way here goes back along the way ([app/lib/nav.ts](app/lib/nav.ts)). - Lamps and output pins pass their signal on: the pin on their right carries the same value, so a lamp can sit in the middle of a wire. diff --git a/app/components/editor/Help.vue b/app/components/editor/Help.vue index ef208c7..d9a7c3a 100644 --- a/app/components/editor/Help.vue +++ b/app/components/editor/Help.vue @@ -26,7 +26,8 @@ const SHORTCUTS: [string, string][] = [ ['Mouse wheel', 'Zoom'], ['N, I, O, L, B, C, H, T, P, X', 'Place NAND, input, output, lamp, button, clock, hex display, note, input panel, pixel'], ['Bar at the bottom', 'Point at a kind of part to see them all; click to place, drag onto the canvas, pin the ones you use most to the bar itself (right-click a pinned part to unpin it)'], - ['Main (top bar)', 'Back to the main circuit; the names after it lead back to each component you entered (on a narrow bar the ones in between fold into …)'], + ['Mouse back / forward buttons, Alt + ← →', 'Back and forward through the circuits you went into and out of, like pages in the browser (the browser\'s own buttons do the same)'], + ['Main (top bar)','Back to the main circuit; the names after it lead back to each component you entered (on a narrow bar the ones in between fold into …)'], ['Power off and on (circuit name in the top bar, or Space)', 'Latches, flip-flops and memory keep their bits when you go into a component and back, and in the saved project; this starts the circuit again from all zeros'], ['⋯ (top bar, small window)', 'Sound, settings, help and whatever else no longer fits in the bar'], ['[', 'Hide or show the panel on the left (This and Pins)'], diff --git a/app/composables/useEditor.ts b/app/composables/useEditor.ts index 07c1607..def9a94 100644 --- a/app/composables/useEditor.ts +++ b/app/composables/useEditor.ts @@ -530,10 +530,41 @@ function create() { } // ── Navigation ──────────────────────────────────────────────────── - function open(id: string, push = true) { + // Every step to another circuit is also an entry in the browser's history, + // so its back and forward (the mouse's side buttons, Alt + ← →, the + // browser's buttons) go through the circuits the way they were visited, + // the crumbs along with them. + + /** Where the editor is, as kept in a browser history entry. */ + interface NavEntry { + /** Project, open circuit, and the crumbs to it. */ + p: string + id: string + nav: string[] + /** Steps from the first entry the editor wrote. */ + i: number + } + + const browserHistory = typeof history !== 'undefined' && typeof history.pushState === 'function' + const entryNow = () => (history.state as { lg?: NavEntry } | null)?.lg + /** Going back or forward to an entry: what that changes is written into it, not as a new one. */ + let restoring = false + + /** Writes where the editor is into the browser's history: as a new entry (`push`), or into the current one. */ + function record(push: boolean) { + if (!browserHistory) return + push &&= !restoring + const i = (entryNow()?.i ?? 0) + (push ? 1 : 0) + // Whatever else the entry holds (the router's own bookkeeping) stays as it is. + const state = { ...(history.state ?? {}), lg: { p: meta.value.id, id: def.value.id, nav: [...nav.value], i } satisfies NavEntry } + if (push) history.pushState(state, '') + else history.replaceState(state, '') + } + + /** Shows another circuit; what the one left was holding is kept for when it opens again. */ + function show(id: string) { if (id === def.value.id || !getDef.value(id)) return keepState() - if (push) nav.value.push(def.value.id) project.value.openId = id clearSelection() placing.value = null @@ -542,9 +573,39 @@ function create() { rebuild() } + /** Shows a circuit with the crumbs to it: a new step in the browser's history when it is another circuit. */ + function go(id: string, crumbs: string[]) { + if (!getDef.value(id)) return + const moved = id !== def.value.id + nav.value = crumbs + show(id) + record(moved) + } + + /** Goes into a circuit, one step further along the way. */ + function open(id: string) { + if (id !== def.value.id) go(id, [...nav.value, def.value.id]) + } + + /** The browser went back or forward (or reloaded): to the circuit of that entry, in its project, as far as they still exist. */ + function restoreEntry(s: NavEntry | undefined) { + if (!s) return + restoring = true + try { + if (s.p !== meta.value.id && listProjects().some(m => m.id === s.p)) switchProject(s.p) + if (s.p === meta.value.id && getDef.value(s.id)) { + nav.value = s.nav.filter(id => getDef.value(id) && id !== s.id) + show(s.id) + } + record(false) + } finally { + restoring = false + } + } + + /** One step back along the crumbs. */ function back() { - const id = nav.value.pop() - if (id) open(id, false) + goTo(nav.value.length - 1) } /** The project's top circuit: "main", or else the first one no other circuit uses. */ @@ -555,18 +616,15 @@ function create() { return (list.find(d => !used.has(d.id) && !d.challenge) ?? list[0]!).id }) - /** Back to the top circuit; the way back is forgotten. */ + /** Back to the top circuit; the crumbs are forgotten, the browser's back still leads where you were. */ function goMain() { - nav.value = [] - open(mainId.value, false) + go(mainId.value, []) } /** Back to step `i` of the way here (0 is where it started). */ function goTo(i: number) { const id = nav.value[i] - if (!id) return - nav.value = nav.value.slice(0, i) - open(id, false) + if (id) go(id, nav.value.slice(0, i)) } // ── Editing ─────────────────────────────────────────────────────── @@ -1948,6 +2006,8 @@ function create() { // ── Projects ────────────────────────────────────────────────────── function activate(m: ProjectMeta, p: Project) { + // Another project is a step in the browser's history; a newer save of this one is not. + const moved = m.id !== meta.value.id placeholder = false meta.value = m project.value = p @@ -1960,6 +2020,7 @@ function create() { for (const k of Object.keys(views)) delete views[k] linkState.value = m.link ? (m.link.key ? 'saved' : 'viewer') : 'none' projects.value = listProjects() + record(moved) syncUrl() // A project that arrives (another computer's newer save, a savegame) runs from the bits it brings, even with the same circuit open. simRoot = '' @@ -2200,6 +2261,25 @@ function create() { document.addEventListener('visibilitychange', () => document.visibilityState === 'hidden' && saveState(true)) setInterval(saveState, 30_000) + if (browserHistory) { + window.addEventListener('popstate', e => restoreEntry((e.state as { lg?: NavEntry } | null)?.lg)) + // The mouse's back button on the first entry would leave the editor: it goes up the crumbs instead, + // or stays (Chrome lets a page stop it; other browsers go back as usual). + window.addEventListener('mouseup', e => { + if (e.button !== 3 || screen.value !== 'editor' || (entryNow()?.i ?? 0) > 0) return + e.preventDefault() + back() + }) + } + + /** The page the app was loaded with opens its link once; the browser's back or forward landing on such a page later does not. */ + let landed = false + function firstPage(): boolean { + const first = !landed + landed = true + return first + } + // ── Account ─────────────────────────────────────────────────────── // Signed in, every project in this browser is also kept in the account and // other devices get it on their next sync. Each upload names the revision @@ -2492,7 +2572,11 @@ function create() { } /** Checks the session with the server, then syncs; again on focus and every minute. */ + let accountStarted = false async function startAccount() { + // Once per page load, however often the editor is shown. + if (accountStarted) return + accountStarted = true try { const res = await authCall<{ user: AccountUser | null; status: number }>('GET', '/api/auth/me') setAccount(res.user) @@ -2519,8 +2603,14 @@ function create() { } rebuild() + // A reload, or back or forward to an entry from before one: where that entry was. A fresh tab starts its first entry here. + if (browserHistory) { + if (entryNow()) restoreEntry(entryNow()) + else record(false) + } return { + firstPage, project, meta, projects, diff --git a/app/pages/index.vue b/app/pages/index.vue index 564e8bd..c87df97 100644 --- a/app/pages/index.vue +++ b/app/pages/index.vue @@ -1,6 +1,9 @@