From 7d6b0f8fadd8b7eafa7d1f2e49829e96258b3383 Mon Sep 17 00:00:00 2001 From: "Fabian @ Blax Software" Date: Mon, 26 Oct 2026 18:14:46 +0100 Subject: [PATCH] A public library of components: publish yours, add everybody's MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit "Publish to the library…" in the menu on a circuit's name (or Space) sends the component with every component it uses and their connector types, under your leaderboard name. The server checks it like any project, refuses what is missing, inside itself, nested too deep or too big to run, and counts its transistors and pins itself. Publishing again from the same browser makes the next version of the same entry; only that browser holds the entry's key, so only it can update or remove it. Challenge circuits and puzzle solutions stay private. The Library (Components in the bottom bar, Space, the main menu) lists every entry, newest first, searchable by name, description and author, with a picture of its box, its transistors, pins and what comes along. Add brings an entry into the open project and starts placing it: what the project has in exactly that form already is used as it is, the rest comes with new ids, a taken name gets a number, connector types join one with the same lines. The This panel says where a component came from and offers a newer version, which replaces it in place. A campaign only adds what is built from parts it has earned, and its challenges take nothing from the library. Components keep an optional "from" mark; projects without it load and compute as before. Co-Authored-By: Claude Opus 5.5 --- README.md | 6 + app/assets/css/main.css | 117 ++++++ app/components/editor/App.vue | 2 + app/components/editor/BottomBar.vue | 1 + app/components/editor/CircuitPane.vue | 33 +- app/components/editor/Help.vue | 1 + app/components/editor/LibraryDialog.vue | 173 +++++++++ .../editor/LibraryPublishDialog.vue | 137 +++++++ app/components/editor/MainMenu.vue | 7 + app/components/editor/QuickAdd.vue | 17 + app/components/editor/Toolbar.vue | 7 + app/composables/useEditor.ts | 139 +++++++- app/composables/usePlace.ts | 2 +- app/lib/logic/compile.ts | 2 +- app/lib/logic/library.ts | 323 +++++++++++++++++ app/lib/logic/ops.ts | 1 + app/lib/logic/project.ts | 12 + app/lib/logic/storage.ts | 27 ++ app/lib/logic/types.ts | 13 + server/api/library/[id].delete.ts | 14 + server/api/library/[id].get.ts | 8 + server/api/library/[id].put.ts | 19 + server/api/library/index.get.ts | 9 + server/api/library/index.post.ts | 24 ++ server/plugins/storage.ts | 2 +- server/utils/library.ts | 70 ++++ tests/library-api.test.ts | 95 +++++ tests/library.test.ts | 335 ++++++++++++++++++ 28 files changed, 1587 insertions(+), 9 deletions(-) create mode 100644 app/components/editor/LibraryDialog.vue create mode 100644 app/components/editor/LibraryPublishDialog.vue create mode 100644 app/lib/logic/library.ts create mode 100644 server/api/library/[id].delete.ts create mode 100644 server/api/library/[id].get.ts create mode 100644 server/api/library/[id].put.ts create mode 100644 server/api/library/index.get.ts create mode 100644 server/api/library/index.post.ts create mode 100644 server/utils/library.ts create mode 100644 tests/library-api.test.ts create mode 100644 tests/library.test.ts diff --git a/README.md b/README.md index f5f4f59..d3aa84e 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ Nuxt 4 (client-only editor, `ssr: false`), Tailwind CSS v4, Lucide icons via `@n - Gates with more inputs: a placed AND, OR, XOR, NAND or NOR (built in, from the examples, or your own) has an "Inputs" setting from 2 to 16. The wide version is a component built as a balanced tree of the two-input gate (NAND and NOR as the plain gate plus a final inverted one), made once per gate and width in "Wide gates" and reused; transistors count honestly and changes to the two-input gate carry through. Which gates qualify is read off their truth table ([app/lib/logic/wide.ts](app/lib/logic/wide.ts)). - Campaign (Projects → New campaign): you start with NAND, inputs, outputs, lamps and notes, and no examples. Each challenge opens once the ones it builds on are solved (NOT → AND, OR, SR Latch → …), and your own solution becomes a part you can place. Other basic parts unlock on the way: constants with NOT, the input panel with AND, hex display and pixel with the decoder, cables with Adder 2, the button with the SR latch, the clock with the D latch. Rules in [app/lib/logic/progress.ts](app/lib/logic/progress.ts). Sandbox projects keep everything open. - Puzzles: "Publish as puzzle" (This circuit) turns a circuit into a link `/puzzle/`. Only its behaviour is published, as a truth table, or for circuits with memory as a run of input changes from power-on; the circuit stays private. Whoever opens the link gets the puzzle in their project, sees the target and the author's transistor count, and after a pass their solution goes to the server, which re-runs the checks and counts the transistors itself (with a fixed work budget) and keeps each player's best. The board shows everyone's transistor counts and your place. The puzzle of the week is generated from the ISO week, the same for everyone: three or four inputs, one or two outputs, each depending on every input. Players are anonymous ids per browser. +- Library: everyone's published components in one public list (bottom bar → Components → Library, Space, or the main menu), searchable by name, description and author, each with its transistors, pins and what it uses. "Publish to the library…" in the menu on a circuit's name sends it with every component it uses and their connector types, under your leaderboard name; the server checks it like any project and counts transistors and pins itself. Publishing again from the same browser makes the next version of the same entry; only that browser has the entry's edit key, so only it can update or remove it. "Add" brings an entry into the open project and starts placing it: what the project has in exactly that form already is used as it is, the rest comes with new ids (a name that is taken gets a number) and connector types join one with the same lines. The This panel says where a component came from and offers a newer version, which replaces it in place. Challenge circuits and puzzle solutions are never published. A campaign can only add what is built from parts it has earned, and its challenges take none of it: they are solved with your own work. Logic in [app/lib/logic/library.ts](app/lib/logic/library.ts). - Sound (speaker in the top bar; volume and clock ticks in Settings, the gear in the top bar): clicks for placing, wiring, switches and buttons, a tick per clock cycle up to 8 Hz (off by default), and jingles for a solve, a failed check, unlocks and new records. All synthesised with Web Audio, no sound files ([app/lib/audio.ts](app/lib/audio.ts)). - Transistors: every component shows how many it takes, counted as a CMOS NAND (4 transistors) per gate at every depth: in the palette, its settings and the circuit stats. Each challenge keeps your record, the fewest transistors of any passing solution, and says so when you beat it. - Cables: a bundle gathers 2 to 32 wires into one cable, a split fans it out again in the same order; one bundle can feed several splits. Select the wires (a box across them, or Shift + click) and press "Bundle into cable" or Ctrl+G: the cable starts right after the sources on their middle row and ends right before the targets on theirs, so the wires keep running as they did (backwards too); an end steps aside when another part is in the way. "Place the ends yourself…" (Ctrl+Shift+G) lets you click where it starts and ends, with a preview; Esc cancels. "Unlink all" on a bundle or split removes only the wires going in or out; the cable stays. Drag the bundle and split later to move the ends. Up to 16 wires, the cable is drawn as a ribbon: each wire is its own thin line inside, in its own colour, lit while it carries a 1. The sleeve starts neutral and gets its own colour (cable or bundle/split settings). "Remove cable" turns it back into single wires. A cable end only joins another cable end, and the cable shows its width. @@ -51,6 +52,7 @@ All in [app/lib/logic/](app/lib/logic/), plain TypeScript with no Vue dependency - `analyze.ts` builds truth tables, counts NANDs, computes the longest gate path, and runs challenge tests. - `examples.ts` builds the example library with a small builder; `ops.ts` holds project edits (package selection, copy/paste, prune dangling wires, recursion check). - `routing.ts` lays out all wires of a circuit (free lanes, detours, separation, crossings, and the tidy layout). +- `library.ts` gathers a component with everything it uses for the public library, checks what comes from outside and adds entries to a project (shared with the server, [server/api/library/](server/api/library/)). - `progress.ts` holds the campaign rules; `puzzles.ts` makes, checks and scores puzzles and generates the weekly one (shared with the server, [server/api/puzzles/](server/api/puzzles/), storage in `data/puzzles`). - `cables.ts` bundles wires into cables (ends where you click, or next to sources and targets), takes them apart again, and lays out the ribbon lines (`offsetPath`, `ribbon`). - `panels.ts` docks input panels to components and keeps them in step; `labels.ts` does series names and names taken from pins. @@ -62,6 +64,10 @@ The editor UI is in [app/components/editor/](app/components/editor/) with state [server/api/p/](server/api/p/): `POST /api/p` stores a project and returns `{ id, key }`; `GET /api/p/:id` reads it; `PUT /api/p/:id` with header `x-edit-key` replaces it. Only a SHA-256 of the key is stored. Each link is one JSON file in `data/shares/` (Nitro fs storage). Limits: 2 MB and 100,000 nodes per project, 30 new links and 600 saves per IP per 10 minutes (in memory). The edit key lives only in the creator's browser (project index in localStorage), never in exported files. +## Library + +[server/api/library/](server/api/library/): `POST /api/library` publishes `{ player, root, components, connectors }` and returns `{ id, key, version }`; `PUT /api/library/:id` with header `x-edit-key` publishes the next version; `DELETE /api/library/:id` with the key takes it out. `GET /api/library?q=` lists every entry, newest first, narrowed to those with every word in name, description, group or author; `GET /api/library/:id` returns one with its components (`?meta`: without). Each entry is one JSON file in `data/library/entries/`, plus `data/library/index` for the list (both in the daily backup). Only a SHA-256 of the key is stored; authors show by their leaderboard name, never by their player id. Limits: 1 MB per entry, 30 publishes per IP per 10 minutes, 20,000 entries. + ## Accounts [server/api/auth/](server/api/auth/) (register, login, logout, me, password, delete) and [server/api/me/projects/](server/api/me/projects/) (list, get, put, delete). Passwords are hashed with scrypt; the session is a random token in an httpOnly, SameSite=Lax cookie, stored server-side as a SHA-256. API calls that change something are refused when their Origin is another site. Storage: `data/users`, `data/sessions`, `data/accounts//` with an index (revision per project, deletions for a year), one file per project, and savegames in `h//`. Each save sends the revision it started from; a mismatch answers 409 and the browser keeps the other version as a copy. Limits: 1000 projects and 100 MB per account, 2 MB per project, login 20 tries per 10 minutes per IP, 10 new accounts per hour per IP. diff --git a/app/assets/css/main.css b/app/assets/css/main.css index 690fa0b..a338339 100644 --- a/app/assets/css/main.css +++ b/app/assets/css/main.css @@ -3362,6 +3362,123 @@ kbd { color: var(--muted); } +/* ── Library: components everyone published ─────────────────────── */ +.modal.library-modal { + width: min(720px, 100%); +} + +.library-modal .search.part-search { + margin: 12px 0 4px; +} + +.lib-list { + display: grid; + gap: 6px; + margin-top: 10px; +} + +.lib-entry { + display: grid; + grid-template-columns: 56px minmax(0, 1fr) auto; + gap: 12px; + align-items: start; + padding: 10px; + border: 1px solid var(--border); + border-radius: 8px; + background: var(--panel-2); +} + +.lib-thumb { + width: 56px; + height: 56px; +} + +.lib-thumb rect { + fill: var(--chip); + stroke: var(--chip-stroke); + stroke-width: 0.12; +} + +.lib-thumb line { + stroke: var(--chip-stroke); + stroke-width: 0.12; +} + +.lib-title { + display: flex; + align-items: baseline; + min-width: 0; + font-weight: 600; +} + +.lib-by, +.lib-stats { + font-size: 12px; + color: var(--muted); +} + +.lib-desc { + display: -webkit-box; + margin-top: 4px; + overflow: hidden; + font-size: 12.5px; + line-height: 1.45; + color: #c3ccd6; + -webkit-line-clamp: 2; + -webkit-box-orient: vertical; +} + +.lib-stats { + display: flex; + flex-wrap: wrap; + gap: 4px 12px; + margin-top: 6px; + font-family: var(--font-mono); + font-size: 11px; +} + +.lib-lock { + display: flex; + align-items: center; + gap: 5px; + margin-top: 6px; + font-size: 12px; + color: #e8a23a; +} + +.lib-actions { + display: flex; + flex-direction: column; + align-items: stretch; + gap: 6px; +} + +@media (max-width: 560px) { + .lib-entry { + grid-template-columns: 40px minmax(0, 1fr); + } + + .lib-thumb { + width: 40px; + height: 40px; + } + + .lib-actions { + grid-column: 1 / -1; + flex-direction: row; + } +} + +/* A component from the library, in the This panel. */ +.lib-origin { + display: flex; + align-items: center; + gap: 6px; + margin: -6px 0 12px; + font-size: 12px; + color: var(--muted); +} + /* ── Face designer ──────────────────────────────────────────────── */ .modal.face-modal { width: min(900px, 100%); diff --git a/app/components/editor/App.vue b/app/components/editor/App.vue index db4cc7f..56a280c 100644 --- a/app/components/editor/App.vue +++ b/app/components/editor/App.vue @@ -81,5 +81,7 @@ watch( + + diff --git a/app/components/editor/BottomBar.vue b/app/components/editor/BottomBar.vue index 44951cc..aac7968 100644 --- a/app/components/editor/BottomBar.vue +++ b/app/components/editor/BottomBar.vue @@ -320,6 +320,7 @@ onBeforeUnmount(() => window.removeEventListener('keydown', onKey, true))
+
diff --git a/app/components/editor/CircuitPane.vue b/app/components/editor/CircuitPane.vue index 2130691..0b18a77 100644 --- a/app/components/editor/CircuitPane.vue +++ b/app/components/editor/CircuitPane.vue @@ -1,6 +1,7 @@ + + diff --git a/app/components/editor/LibraryPublishDialog.vue b/app/components/editor/LibraryPublishDialog.vue new file mode 100644 index 0000000..fbd6de9 --- /dev/null +++ b/app/components/editor/LibraryPublishDialog.vue @@ -0,0 +1,137 @@ + + +