From 19042fd5a2d9e35e02787fca13e36acac5336d33 Mon Sep 17 00:00:00 2001 From: CoachLogic Date: Thu, 19 Nov 2026 07:58:02 +0100 Subject: [PATCH 01/65] feat: add keyboard command palette engine --- src/editor/CommandPalette.js | 165 +++++++++++++++++++++++++++++++++++ 1 file changed, 165 insertions(+) create mode 100644 src/editor/CommandPalette.js diff --git a/src/editor/CommandPalette.js b/src/editor/CommandPalette.js new file mode 100644 index 0000000..98038c2 --- /dev/null +++ b/src/editor/CommandPalette.js @@ -0,0 +1,165 @@ +/** + * CommandPalette — keyboard-first command search for the editor workspace. + * Commands are declarative and execute only when explicitly selected. + */ +const normalize = (value) => String(value || "").trim().toUpperCase().replace(/\s+/g, " "); + +export class CommandPalette { + constructor({ root = document, input, overlay, registry, onExecute = () => {} }) { + if (!input || !overlay || !registry) throw new TypeError("CommandPalette requires input, overlay and registry."); + this.root = root; + this.input = input; + this.overlay = overlay; + this.registry = registry; + this.onExecute = onExecute; + this.query = ""; + this.activeIndex = 0; + this.opened = false; + this.commands = new Map(); + this.onKeyDown = (event) => this.handleKeyDown(event); + this.onInput = () => { this.query = this.input.value; this.activeIndex = 0; this.render(); }; + this.onClick = (event) => { + const item = event.target.closest("[data-command-id]"); + if (!item) return; + this.execute(item.dataset.commandId); + }; + this.root.addEventListener("keydown", this.onKeyDown, true); + this.input.addEventListener("input", this.onInput); + this.overlay.addEventListener("click", this.onClick); + } + + register(command) { + if (!command || !/^[a-z0-9][a-z0-9-]*$/i.test(command.id || "") || typeof command.execute !== "function") { + throw new TypeError("A command requires a valid id and execute function."); + } + if (this.commands.has(command.id)) throw new Error(`Command "${command.id}" is already registered.`); + const entry = Object.freeze({ + label: command.id.toUpperCase(), + description: "", + aliases: [], + keywords: [], + enabled: () => true, + ...command, + }); + this.commands.set(entry.id, entry); + return () => this.commands.delete(entry.id); + } + + list(query = this.query) { + const q = normalize(query); + const tokens = q.split(" ").filter(Boolean); + return [...this.commands.values()].map((command) => { + if (!command.enabled()) return null; + const candidates = [command.label, command.id, ...command.aliases, ...command.keywords].map(normalize); + let score = 0; + if (!q) score = 1; + else if (candidates.some((value) => value === q)) score = 100; + else if (candidates.some((value) => value.startsWith(q))) score = 80; + else if (tokens.every((token) => candidates.some((value) => value.includes(token)))) score = 50; + else return null; + return { command, score }; + }).filter(Boolean).sort((a, b) => b.score - a.score || a.command.label.localeCompare(b.command.label)) + .map(({ command }) => command); + } + + open(initial = "") { + this.opened = true; + this.query = initial; + this.activeIndex = 0; + this.overlay.hidden = false; + this.input.value = initial; + this.render(); + this.input.focus(); + this.input.setSelectionRange(this.input.value.length, this.input.value.length); + } + + close({ restoreFocus = false } = {}) { + this.opened = false; + this.overlay.hidden = true; + this.input.value = ""; + this.query = ""; + if (restoreFocus) this.root.activeElement?.blur?.(); + } + + handleKeyDown(event) { + const target = event.target; + const isEditable = target?.matches?.("input, textarea, select, [contenteditable='true']") || target?.isContentEditable; + if (this.opened) { + if (event.key === "Escape") { event.preventDefault(); event.stopPropagation(); this.close(); return; } + if (event.key === "ArrowDown" || event.key === "ArrowUp") { + event.preventDefault(); + const items = this.list(); + if (items.length) this.activeIndex = (this.activeIndex + (event.key === "ArrowDown" ? 1 : -1) + items.length) % items.length; + this.render(); return; + } + if (event.key === "Tab") { + const selected = this.list()[this.activeIndex]; + if (selected) { event.preventDefault(); this.input.value = selected.label; this.query = selected.label; this.activeIndex = 0; this.render(); } + return; + } + if (event.key === "Enter") { + event.preventDefault(); event.stopPropagation(); + const selected = this.list()[this.activeIndex]; + if (selected) this.execute(selected.id); + return; + } + return; + } + if (isEditable || event.defaultPrevented || event.ctrlKey || event.metaKey || event.altKey) return; + if (event.key === "F3" || (event.key === " " && event.shiftKey)) { + event.preventDefault(); this.open(); return; + } + if (event.key.length === 1 && !event.repeat) { + event.preventDefault(); + this.open(event.key); + } + } + + execute(id) { + const command = this.commands.get(id); + if (!command || !command.enabled()) return false; + this.close(); + try { + command.execute(); + this.onExecute(command); + return true; + } catch (error) { + this.onExecute(command, error); + throw error; + } + } + + render() { + const items = this.list(); + const list = this.overlay.querySelector("[data-command-results]"); + list.replaceChildren(); + if (!items.length) { + const empty = this.root.createElement("div"); + empty.className = "command-empty"; + empty.textContent = "No matching commands"; + list.append(empty); + return; + } + items.forEach((command, index) => { + const button = this.root.createElement("button"); + button.type = "button"; + button.className = "command-result"; + button.dataset.commandId = command.id; + button.setAttribute("aria-selected", String(index === this.activeIndex)); + const title = this.root.createElement("span"); + title.textContent = command.label; + const description = this.root.createElement("small"); + description.textContent = command.description; + button.append(title, description); + list.append(button); + }); + } + + dispose() { + this.root.removeEventListener("keydown", this.onKeyDown, true); + this.input.removeEventListener("input", this.onInput); + this.overlay.removeEventListener("click", this.onClick); + this.close(); + this.commands.clear(); + } +} From 0b714f4397cd98aa716e4e5cffc01ccb89974d48 Mon Sep 17 00:00:00 2001 From: CoachLogic Date: Thu, 19 Nov 2026 07:58:04 +0100 Subject: [PATCH 02/65] feat: add non-destructive orbit preview controller --- src/editor/OrbitPreview.js | 71 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 src/editor/OrbitPreview.js diff --git a/src/editor/OrbitPreview.js b/src/editor/OrbitPreview.js new file mode 100644 index 0000000..8ea5503 --- /dev/null +++ b/src/editor/OrbitPreview.js @@ -0,0 +1,71 @@ +/** + * OrbitPreview — non-destructive camera turntable around the active model. + * The model's transforms and animation mixer are never modified. + */ +export class OrbitPreview { + constructor({ camera, controls, getModel, speed = 0.45, axis = "y" }) { + if (!camera || !controls || typeof getModel !== "function") throw new TypeError("OrbitPreview requires camera, controls and getModel."); + this.camera = camera; + this.controls = controls; + this.getModel = getModel; + this.speed = speed; + this.axis = axis; + this.active = false; + this.center = null; + this.radius = 0; + this.angle = 0; + this.onManualStart = () => this.stop(); + this.controls.addEventListener?.("start", this.onManualStart); + } + + start() { + const model = this.getModel(); + if (!model) return false; + model.updateWorldMatrix?.(true, true); + const box = new globalThis.THREE.Box3().setFromObject(model); + if (box.isEmpty()) return false; + this.center = box.getCenter(new globalThis.THREE.Vector3()); + const offset = this.camera.position.clone().sub(this.center); + this.radius = Math.max(offset.length(), 0.001); + this.angle = Math.atan2(offset.x, offset.z); + this.height = offset.y; + this.controls.target.copy(this.center); + this.controls.update(); + this.active = true; + return true; + } + + stop() { this.active = false; } + toggle() { return this.active ? (this.stop(), false) : this.start(); } + setSpeed(value) { + const speed = Number(value); + if (!Number.isFinite(speed) || speed < 0.02 || speed > 3) throw new RangeError("Orbit speed must be between 0.02 and 3 radians per second."); + this.speed = speed; + } + setAxis(axis) { + if (!["x", "y", "z"].includes(axis)) throw new TypeError("Orbit axis must be x, y or z."); + this.axis = axis; + if (this.active) this.start(); + } + + update(deltaSeconds) { + if (!this.active || !this.center) return; + const dt = Math.max(0, Math.min(Number(deltaSeconds) || 0, 0.1)); + this.angle += this.speed * dt; + const c = Math.cos(this.angle), s = Math.sin(this.angle); + const h = this.height; + let offset; + if (this.axis === "x") offset = new globalThis.THREE.Vector3(this.radius * c, this.radius * s, h); + else if (this.axis === "z") offset = new globalThis.THREE.Vector3(this.radius * c, h, this.radius * s); + else offset = new globalThis.THREE.Vector3(this.radius * s, h, this.radius * c); + this.camera.position.copy(this.center).add(offset); + this.camera.lookAt(this.center); + this.controls.target.copy(this.center); + this.controls.update(); + } + + dispose() { + this.stop(); + this.controls.removeEventListener?.("start", this.onManualStart); + } +} From a28f38e01b9bd1b7522179eae15c66c0d7a8b064 Mon Sep 17 00:00:00 2001 From: CoachLogic Date: Thu, 19 Nov 2026 07:58:06 +0100 Subject: [PATCH 03/65] test: cover command palette matching and execution --- tests/command-palette.test.js | 36 +++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 tests/command-palette.test.js diff --git a/tests/command-palette.test.js b/tests/command-palette.test.js new file mode 100644 index 0000000..b60323b --- /dev/null +++ b/tests/command-palette.test.js @@ -0,0 +1,36 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { CommandPalette } from '../src/editor/CommandPalette.js'; + +test('CommandPalette ranks prefix matches and executes selected commands', () => { + const listeners = new Map(); + const makeElement = () => ({ + hidden: true, value: '', children: [], dataset: {}, + addEventListener(type, fn) { (this.listeners ||= new Map()).set(type, fn); }, + removeEventListener() {}, + querySelector() { return this.list ||= { replaceChildren() { this.children = []; }, append(item) { this.children.push(item); } }; }, + append(...items) { this.children.push(...items); }, + replaceChildren() { this.children = []; }, + setAttribute() {}, focus() {}, setSelectionRange() {} + }); + const root = { activeElement: null, addEventListener(type, fn) { listeners.set(type, fn); }, removeEventListener() {}, createElement: makeElement }; + const input = makeElement(), overlay = makeElement(); + const palette = new CommandPalette({ root, input, overlay, registry: {} }); + let opened = 0; + palette.register({ id: 'light', label: 'LIGHT', execute: () => opened++ }); + palette.register({ id: 'lighting-reset', label: 'LIGHTING RESET', execute: () => opened += 10 }); + assert.equal(palette.list('LIG')[0].id, 'light'); + assert.equal(palette.execute('light'), true); + assert.equal(opened, 1); + palette.dispose(); +}); + +test('CommandPalette ignores editable fields and rejects duplicate commands', () => { + const root = { addEventListener() {}, removeEventListener() {}, createElement: () => ({}) }; + const input = { addEventListener() {}, removeEventListener() {} }; + const overlay = { addEventListener() {}, removeEventListener() {}, hidden: true, querySelector: () => ({ replaceChildren() {} }) }; + const palette = new CommandPalette({ root, input, overlay, registry: {} }); + palette.register({ id: 'light', execute() {} }); + assert.throws(() => palette.register({ id: 'light', execute() {} }), /already registered/); + palette.dispose(); +}); From 5113e70003dd8257150c977d405c5d6a88161fdb Mon Sep 17 00:00:00 2001 From: CoachLogic Date: Thu, 19 Nov 2026 07:58:48 +0100 Subject: [PATCH 04/65] refactor: inject Three.js into orbit preview --- src/editor/OrbitPreview.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/editor/OrbitPreview.js b/src/editor/OrbitPreview.js index 8ea5503..9fa25e6 100644 --- a/src/editor/OrbitPreview.js +++ b/src/editor/OrbitPreview.js @@ -3,9 +3,9 @@ * The model's transforms and animation mixer are never modified. */ export class OrbitPreview { - constructor({ camera, controls, getModel, speed = 0.45, axis = "y" }) { - if (!camera || !controls || typeof getModel !== "function") throw new TypeError("OrbitPreview requires camera, controls and getModel."); - this.camera = camera; + constructor({ THREE, camera, controls, getModel, speed = 0.45, axis = "y" }) { + if (!THREE || !camera || !controls || typeof getModel !== "function") throw new TypeError("OrbitPreview requires camera, controls and getModel."); + this.THREE = THREE;\n this.camera = camera; this.controls = controls; this.getModel = getModel; this.speed = speed; @@ -22,9 +22,9 @@ export class OrbitPreview { const model = this.getModel(); if (!model) return false; model.updateWorldMatrix?.(true, true); - const box = new globalThis.THREE.Box3().setFromObject(model); + const box = new this.THREE.Box3().setFromObject(model); if (box.isEmpty()) return false; - this.center = box.getCenter(new globalThis.THREE.Vector3()); + this.center = box.getCenter(new this.THREE.Vector3()); const offset = this.camera.position.clone().sub(this.center); this.radius = Math.max(offset.length(), 0.001); this.angle = Math.atan2(offset.x, offset.z); @@ -55,9 +55,9 @@ export class OrbitPreview { const c = Math.cos(this.angle), s = Math.sin(this.angle); const h = this.height; let offset; - if (this.axis === "x") offset = new globalThis.THREE.Vector3(this.radius * c, this.radius * s, h); - else if (this.axis === "z") offset = new globalThis.THREE.Vector3(this.radius * c, h, this.radius * s); - else offset = new globalThis.THREE.Vector3(this.radius * s, h, this.radius * c); + if (this.axis === "x") offset = new this.THREE.Vector3(this.radius * c, this.radius * s, h); + else if (this.axis === "z") offset = new this.THREE.Vector3(this.radius * c, h, this.radius * s); + else offset = new this.THREE.Vector3(this.radius * s, h, this.radius * c); this.camera.position.copy(this.center).add(offset); this.camera.lookAt(this.center); this.controls.target.copy(this.center); From 640c3571f1c1982c514a5a33d9c4501cca4ba2c6 Mon Sep 17 00:00:00 2001 From: CoachLogic Date: Thu, 19 Nov 2026 08:00:44 +0100 Subject: [PATCH 05/65] fix: pause orbit preview during manual navigation --- src/editor/OrbitPreview.js | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/editor/OrbitPreview.js b/src/editor/OrbitPreview.js index 9fa25e6..0b4cf38 100644 --- a/src/editor/OrbitPreview.js +++ b/src/editor/OrbitPreview.js @@ -1,20 +1,23 @@ /** * OrbitPreview — non-destructive camera turntable around the active model. - * The model's transforms and animation mixer are never modified. + * It changes the camera only; model transforms and animation state are untouched. */ export class OrbitPreview { constructor({ THREE, camera, controls, getModel, speed = 0.45, axis = "y" }) { - if (!THREE || !camera || !controls || typeof getModel !== "function") throw new TypeError("OrbitPreview requires camera, controls and getModel."); - this.THREE = THREE;\n this.camera = camera; + if (!THREE || !camera || !controls || typeof getModel !== "function") throw new TypeError("OrbitPreview requires THREE, camera, controls and getModel."); + this.THREE = THREE; + this.camera = camera; this.controls = controls; this.getModel = getModel; this.speed = speed; this.axis = axis; this.active = false; + this.paused = false; this.center = null; this.radius = 0; this.angle = 0; - this.onManualStart = () => this.stop(); + this.height = 0; + this.onManualStart = () => this.pause(); this.controls.addEventListener?.("start", this.onManualStart); } @@ -26,22 +29,27 @@ export class OrbitPreview { if (box.isEmpty()) return false; this.center = box.getCenter(new this.THREE.Vector3()); const offset = this.camera.position.clone().sub(this.center); - this.radius = Math.max(offset.length(), 0.001); + this.radius = Math.max(Math.hypot(offset.x, offset.z), 0.001); this.angle = Math.atan2(offset.x, offset.z); this.height = offset.y; this.controls.target.copy(this.center); this.controls.update(); this.active = true; + this.paused = false; return true; } - stop() { this.active = false; } + pause() { if (this.active) this.paused = true; } + resume() { if (this.active) this.paused = false; else return this.start(); return true; } + stop() { this.active = false; this.paused = false; } toggle() { return this.active ? (this.stop(), false) : this.start(); } + setSpeed(value) { const speed = Number(value); if (!Number.isFinite(speed) || speed < 0.02 || speed > 3) throw new RangeError("Orbit speed must be between 0.02 and 3 radians per second."); this.speed = speed; } + setAxis(axis) { if (!["x", "y", "z"].includes(axis)) throw new TypeError("Orbit axis must be x, y or z."); this.axis = axis; @@ -49,15 +57,14 @@ export class OrbitPreview { } update(deltaSeconds) { - if (!this.active || !this.center) return; + if (!this.active || this.paused || !this.center) return; const dt = Math.max(0, Math.min(Number(deltaSeconds) || 0, 0.1)); this.angle += this.speed * dt; const c = Math.cos(this.angle), s = Math.sin(this.angle); - const h = this.height; let offset; - if (this.axis === "x") offset = new this.THREE.Vector3(this.radius * c, this.radius * s, h); - else if (this.axis === "z") offset = new this.THREE.Vector3(this.radius * c, h, this.radius * s); - else offset = new this.THREE.Vector3(this.radius * s, h, this.radius * c); + if (this.axis === "x") offset = new this.THREE.Vector3(this.height, this.radius * s, this.radius * c); + else if (this.axis === "z") offset = new this.THREE.Vector3(this.radius * c, this.radius * s, this.height); + else offset = new this.THREE.Vector3(this.radius * s, this.height, this.radius * c); this.camera.position.copy(this.center).add(offset); this.camera.lookAt(this.center); this.controls.target.copy(this.center); From 92abe9b8a78c7460857c836667db45500135ab32 Mon Sep 17 00:00:00 2001 From: CoachLogic Date: Thu, 19 Nov 2026 08:00:46 +0100 Subject: [PATCH 06/65] feat: integrate command mode and orbit preview in viewer --- viewer.html | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/viewer.html b/viewer.html index 84a39b5..ca5933a 100644 --- a/viewer.html +++ b/viewer.html @@ -115,6 +115,33 @@ body.light [data-tooltip]::after { background:#fff; color:#222; border-color:rgba(0,0,0,.14); } @media (max-width:700px) { #workspace-main { top:64px; right:62px; width:min(340px,calc(100vw - 78px)); height:calc(100vh - 88px); max-width:calc(100vw - 70px); } #toolbar { left:8px; top:8px; gap:5px; flex-wrap:wrap; max-width:calc(100vw - 70px); } #toolbar button { padding:7px 9px; font-size:11px; } #scene-panel { right:8px; } } + + /* P1 command mode */ + #command-overlay { position:fixed; inset:0; z-index:120; display:flex; align-items:flex-start; justify-content:center; padding:12vh 16px 16px; background:rgba(0,0,0,.28); pointer-events:auto; } + #command-overlay[hidden] { display:none; } + #command-box { width:min(560px,calc(100vw - 32px)); overflow:hidden; border:1px solid rgba(255,255,255,.18); border-radius:12px; background:rgba(27,29,34,.98); color:#f2f3f5; box-shadow:0 24px 80px rgba(0,0,0,.5); backdrop-filter:blur(18px); } + #command-input { width:100%; height:54px; padding:0 18px; border:0; border-bottom:1px solid rgba(255,255,255,.12); outline:none; background:transparent; color:inherit; font:500 16px/1.2 system-ui,sans-serif; } + #command-results { max-height:min(420px,55vh); overflow:auto; padding:7px; } + .command-result { width:100%; display:flex; align-items:center; justify-content:space-between; gap:16px; padding:11px 12px; border:0; border-radius:7px; background:transparent; color:inherit; text-align:left; cursor:pointer; } + .command-result[aria-selected="true"],.command-result:hover { background:rgba(229,155,82,.16); } + .command-result span { font-size:13px; font-weight:650; letter-spacing:.025em; } + .command-result small { color:#9ca5b2; font-size:11px; } + .command-empty { padding:18px 12px; color:#9ca5b2; font-size:12px; } + #command-hint { padding:9px 12px; border-top:1px solid rgba(255,255,255,.1); color:#8993a0; font-size:10px; } + body.light #command-box { background:rgba(250,251,253,.99); color:#222; border-color:rgba(0,0,0,.16); } + body.light #command-input { border-color:rgba(0,0,0,.12); } + body.light .command-result[aria-selected="true"],body.light .command-result:hover { background:rgba(170,105,42,.12); } + body.light .command-result small,body.light .command-empty,body.light #command-hint { color:#68717e; } + #orbit-controls { position:fixed; left:50%; bottom:22px; transform:translateX(-50%); z-index:25; display:flex; align-items:center; gap:12px; padding:10px 14px; border:1px solid rgba(255,255,255,.16); border-radius:10px; background:rgba(28,30,34,.94); color:#eee; pointer-events:auto; box-shadow:0 12px 34px rgba(0,0,0,.3); } + #orbit-controls[hidden] { display:none; } + #orbit-controls button { border:1px solid rgba(255,255,255,.18); border-radius:6px; padding:6px 9px; background:rgba(255,255,255,.08); color:inherit; cursor:pointer; } + #orbit-controls label { display:flex; align-items:center; gap:8px; color:#b8c0ca; font-size:11px; } + #orbit-speed { width:100px; accent-color:#e59b52; } + #orbit-status { min-width:52px; color:#9ca5b2; font-size:10px; } + body.light #orbit-controls { background:rgba(250,251,253,.98); color:#222; border-color:rgba(0,0,0,.16); } + body.light #orbit-controls button { color:#222; border-color:rgba(0,0,0,.14); background:rgba(0,0,0,.045); } + @media (max-width:600px) { #orbit-controls { bottom:58px; width:calc(100vw - 90px); justify-content:center; flex-wrap:wrap; } } + /* Light theme override */ body.light { background: #f5f5f5; color: #222; } body.light #loading { background: #f5f5f5; } @@ -135,8 +162,19 @@ + + + +