@tjakoen/grain 0.1.12
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/LICENSE +202 -0
- package/PLAN.md +27 -0
- package/README.md +243 -0
- package/ai/accepts.test.ts +69 -0
- package/ai/accepts.ts +57 -0
- package/ai/ai.css +127 -0
- package/ai/client-door.test.ts +70 -0
- package/ai/client-door.ts +56 -0
- package/ai/contract.test.ts +158 -0
- package/ai/contract.ts +285 -0
- package/ai/interaction-layer.test.ts +270 -0
- package/ai/interaction-layer.ts +138 -0
- package/ai/manifest-dom.test.ts +176 -0
- package/ai/manifest-dom.ts +187 -0
- package/ai/manifest.test.ts +71 -0
- package/ai/manifest.ts +57 -0
- package/ai/markdown.test.ts +68 -0
- package/ai/markdown.ts +74 -0
- package/ai/model-chat.test.ts +131 -0
- package/ai/model-chat.ts +151 -0
- package/ai/model-reasoner.test.ts +152 -0
- package/ai/model-reasoner.ts +146 -0
- package/ai/model.test.ts +148 -0
- package/ai/model.ts +200 -0
- package/ai/reasoner-kit.test.ts +142 -0
- package/ai/reasoner-kit.ts +159 -0
- package/ai/reasoner.test.ts +227 -0
- package/ai/reasoner.ts +446 -0
- package/ai/timeline-log.test.ts +55 -0
- package/ai/timeline-log.ts +50 -0
- package/ai/vocab-reference.test.ts +43 -0
- package/ai/vocab-reference.ts +107 -0
- package/ai/webllm.test.ts +79 -0
- package/ai/webllm.ts +125 -0
- package/assets/figure-sample.svg +14 -0
- package/assets/sprite.svg +22 -0
- package/catalog/catalog.test.ts +37 -0
- package/catalog/catalog.ts +424 -0
- package/components/atoms/action-badge/action-badge.css +17 -0
- package/components/atoms/action-badge/action-badge.html +6 -0
- package/components/atoms/action-badge/action-badge.md +23 -0
- package/components/atoms/b-badge/b-badge.css +15 -0
- package/components/atoms/b-badge/b-badge.html +2 -0
- package/components/atoms/b-badge/b-badge.md +16 -0
- package/components/atoms/b-button/b-button.ai.md +22 -0
- package/components/atoms/b-button/b-button.css +87 -0
- package/components/atoms/b-button/b-button.html +5 -0
- package/components/atoms/b-button/b-button.md +77 -0
- package/components/atoms/b-icon/b-icon.css +10 -0
- package/components/atoms/b-icon/b-icon.html +5 -0
- package/components/atoms/b-icon/b-icon.md +36 -0
- package/components/atoms/b-icon-button/b-icon-button.ai.md +11 -0
- package/components/atoms/b-icon-button/b-icon-button.css +43 -0
- package/components/atoms/b-icon-button/b-icon-button.html +8 -0
- package/components/atoms/b-icon-button/b-icon-button.md +33 -0
- package/components/atoms/b-input/b-input.css +36 -0
- package/components/atoms/b-input/b-input.html +8 -0
- package/components/atoms/b-input/b-input.md +59 -0
- package/components/atoms/b-kbd/b-kbd.css +11 -0
- package/components/atoms/b-kbd/b-kbd.html +3 -0
- package/components/atoms/b-kbd/b-kbd.md +13 -0
- package/components/atoms/b-list/b-list.css +21 -0
- package/components/atoms/b-list/b-list.html +6 -0
- package/components/atoms/b-list/b-list.md +24 -0
- package/components/atoms/b-meter/b-meter.css +39 -0
- package/components/atoms/b-meter/b-meter.md +44 -0
- package/components/atoms/b-select/b-select.css +35 -0
- package/components/atoms/b-select/b-select.md +75 -0
- package/components/atoms/b-switch/b-switch.css +61 -0
- package/components/atoms/b-switch/b-switch.html +6 -0
- package/components/atoms/b-switch/b-switch.md +40 -0
- package/components/atoms/b-text/b-text.ai.md +29 -0
- package/components/atoms/b-text/b-text.css +2 -0
- package/components/atoms/b-text/b-text.html +3 -0
- package/components/atoms/b-text/b-text.md +23 -0
- package/components/atoms/code-block/code-block.css +42 -0
- package/components/atoms/code-block/code-block.md +23 -0
- package/components/examples.test.ts +23 -0
- package/components/molecules/callout/callout.css +19 -0
- package/components/molecules/callout/callout.md +15 -0
- package/components/molecules/card/card.css +28 -0
- package/components/molecules/card/card.md +30 -0
- package/components/molecules/chat-message/chat-message.css +83 -0
- package/components/molecules/chat-message/chat-message.md +52 -0
- package/components/molecules/chip-group/chip-group.css +35 -0
- package/components/molecules/chip-group/chip-group.md +42 -0
- package/components/molecules/content-index/content-index.css +87 -0
- package/components/molecules/content-index/content-index.md +27 -0
- package/components/molecules/content-source/content-source.css +6 -0
- package/components/molecules/content-source/content-source.md +13 -0
- package/components/molecules/figure/figure.css +19 -0
- package/components/molecules/figure/figure.md +20 -0
- package/components/molecules/lede/lede.css +17 -0
- package/components/molecules/lede/lede.md +17 -0
- package/components/molecules/made-with/made-with.css +25 -0
- package/components/molecules/made-with/made-with.md +35 -0
- package/components/molecules/nav-item/nav-item.css +19 -0
- package/components/molecules/nav-item/nav-item.md +21 -0
- package/components/molecules/stat-tile/stat-tile.css +34 -0
- package/components/molecules/stat-tile/stat-tile.md +50 -0
- package/components/molecules/status-list/status-list.css +47 -0
- package/components/molecules/status-list/status-list.md +62 -0
- package/components/molecules/tab/tab.css +32 -0
- package/components/molecules/tab/tab.md +27 -0
- package/components/molecules/table/table.css +23 -0
- package/components/molecules/table/table.md +18 -0
- package/components/organisms/activity-bar/activity-bar.css +36 -0
- package/components/organisms/activity-bar/activity-bar.md +43 -0
- package/components/organisms/app-shell/app-shell.css +193 -0
- package/components/organisms/app-shell/app-shell.md +58 -0
- package/components/organisms/app-shell/app-shell.test.ts +74 -0
- package/components/organisms/app-window/app-window.css +111 -0
- package/components/organisms/app-window/app-window.md +53 -0
- package/components/organisms/chat-log/chat-log.css +5 -0
- package/components/organisms/chat-log/chat-log.md +14 -0
- package/components/organisms/code-editor/code-editor.ai.md +31 -0
- package/components/organisms/code-editor/code-editor.css +111 -0
- package/components/organisms/code-editor/code-editor.md +122 -0
- package/components/organisms/console/console.css +112 -0
- package/components/organisms/console/console.md +66 -0
- package/components/organisms/file-tree/file-tree.css +85 -0
- package/components/organisms/file-tree/file-tree.md +49 -0
- package/components/organisms/note/note.css +19 -0
- package/components/organisms/note/note.md +20 -0
- package/components/organisms/notepad/notepad.css +60 -0
- package/components/organisms/notepad/notepad.md +40 -0
- package/components/organisms/side-rail/side-rail.css +55 -0
- package/components/organisms/side-rail/side-rail.md +24 -0
- package/components/organisms/sidebar-panel/sidebar-panel.css +69 -0
- package/components/organisms/sidebar-panel/sidebar-panel.md +47 -0
- package/components/organisms/status-bar/status-bar.css +33 -0
- package/components/organisms/status-bar/status-bar.md +28 -0
- package/components/organisms/tab-bar/tab-bar.css +48 -0
- package/components/organisms/tab-bar/tab-bar.md +26 -0
- package/components/organisms/timeline/timeline.css +51 -0
- package/components/organisms/timeline/timeline.md +42 -0
- package/components/organisms/topbar/topbar.css +8 -0
- package/components/organisms/topbar/topbar.md +26 -0
- package/components/responsive-polish.test.ts +43 -0
- package/components/tokens.test.ts +39 -0
- package/fonts/redaction-35-400.woff2 +0 -0
- package/fonts/redaction-400.woff2 +0 -0
- package/fonts/redaction-50-400.woff2 +0 -0
- package/fonts/redaction-70-400.woff2 +0 -0
- package/package.json +46 -0
- package/scripts/ai-dispatch.js +583 -0
- package/scripts/ai-dispatch.test.ts +85 -0
- package/scripts/ai-spotlight.d.ts +23 -0
- package/scripts/ai-spotlight.js +139 -0
- package/scripts/ai-spotlight.test.ts +31 -0
- package/scripts/catalog-peek.js +156 -0
- package/scripts/catalog-peek.test.ts +31 -0
- package/scripts/cmdk.js +159 -0
- package/scripts/demo-box.js +53 -0
- package/scripts/handoff.js +64 -0
- package/scripts/handoff.test.ts +36 -0
- package/scripts/lightbox.js +144 -0
- package/scripts/lightbox.test.ts +23 -0
- package/scripts/made-with.d.ts +2 -0
- package/scripts/made-with.js +22 -0
- package/scripts/made-with.test.ts +25 -0
- package/scripts/notepad.js +100 -0
- package/scripts/shell.js +204 -0
- package/scripts/shell.test.ts +50 -0
- package/scripts/tabs.js +127 -0
- package/scripts/tabs.test.ts +40 -0
- package/scripts/terminal.js +271 -0
- package/scripts/terminal.test.ts +36 -0
- package/scripts/theme-boot.js +15 -0
- package/scripts/theme.js +87 -0
- package/scripts/type-effect.js +25 -0
- package/scripts/xray.js +79 -0
- package/scripts/xray.test.ts +43 -0
- package/styles/cmdk.css +65 -0
- package/styles/global.css +80 -0
- package/styles/grain.css +47 -0
- package/styles/lightbox.css +78 -0
- package/styles/themes/_template.css +48 -0
- package/styles/themes/baguette.css +30 -0
- package/styles/themes/brioche.css +30 -0
- package/styles/variables.css +217 -0
package/scripts/shell.js
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
// grain/scripts/shell.js — the app-shell island: the rail toggle. Two behaviours off ONE
|
|
2
|
+
// control, chosen by viewport: on desktop it COLLAPSES the rail to icons (persisted); on
|
|
3
|
+
// mobile it OPENS the rail as an off-canvas drawer (with a scrim). Pure DOM + a class/attr
|
|
4
|
+
// toggle — no framework. It knows nothing about the AI door; it's plain UI chrome.
|
|
5
|
+
(() => {
|
|
6
|
+
"use strict";
|
|
7
|
+
const shell = document.querySelector(".app-shell");
|
|
8
|
+
if (!shell) return;
|
|
9
|
+
|
|
10
|
+
const mobile = window.matchMedia("(max-width: 768px)");
|
|
11
|
+
// "Effective mobile" is a CONTAINER question, not a viewport one. The mobile drawer/sheet LAYOUT is
|
|
12
|
+
// driven by `@container shell-frame (max-width: 768px)` on <body> (app-shell.css), which the
|
|
13
|
+
// viewport-toggle fires by clamping body's width — WITHOUT changing the real window, so
|
|
14
|
+
// `mobile.matches` (a viewport media query) stays false and every behaviour gated on it silently
|
|
15
|
+
// no-ops: the grab bar can't raise the sheet, the mode tabs can't reach the catalog, the rail
|
|
16
|
+
// hamburger collapses instead of opening the drawer (owner report: "chat + catalog broken when the
|
|
17
|
+
// viewport is toggled"). Gate on the SAME thing the CSS does — the shell-frame container's
|
|
18
|
+
// inline-size (= body's width) — so JS and layout agree in both a real narrow window and the toggle.
|
|
19
|
+
const isMobile = () => mobile.matches || document.body.getBoundingClientRect().width <= 768;
|
|
20
|
+
const KEY = "grain.shell.rail-collapsed";
|
|
21
|
+
|
|
22
|
+
const setCollapsed = (v) => {
|
|
23
|
+
shell.setAttribute("data-rail-collapsed", v ? "true" : "false");
|
|
24
|
+
try { localStorage.setItem(KEY, v ? "1" : "0"); } catch { /* private mode */ }
|
|
25
|
+
};
|
|
26
|
+
const setOpen = (v) => {
|
|
27
|
+
if (v) shell.setAttribute("data-rail-open", "true");
|
|
28
|
+
else shell.removeAttribute("data-rail-open");
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
// restore the desktop collapse preference (mobile ignores it — it's a drawer there)
|
|
32
|
+
try { if (localStorage.getItem(KEY) === "1") shell.setAttribute("data-rail-collapsed", "true"); } catch { /* ignore */ }
|
|
33
|
+
|
|
34
|
+
// the toggle(s): any control marked data-shell="rail-toggle"
|
|
35
|
+
for (const btn of shell.querySelectorAll('[data-shell="rail-toggle"]')) {
|
|
36
|
+
btn.addEventListener("click", () => {
|
|
37
|
+
if (isMobile()) setOpen(shell.getAttribute("data-rail-open") !== "true");
|
|
38
|
+
else setCollapsed(shell.getAttribute("data-rail-collapsed") !== "true");
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// VS Code-style pane visibility: hide/show the aside (the assistant column) and the whole
|
|
43
|
+
// console strip. Attribute-driven so app-shell.css owns the layout change; state persists.
|
|
44
|
+
const PANE_KEYS = { "aside-toggle": ["data-aside-hidden", "grain.shell.aside-hidden"],
|
|
45
|
+
"console-hide": ["data-console-hidden", "grain.shell.console-hidden"] };
|
|
46
|
+
for (const [name, [attr, key]] of Object.entries(PANE_KEYS)) {
|
|
47
|
+
try { if (localStorage.getItem(key) === "1") shell.setAttribute(attr, "true"); } catch { /* ignore */ }
|
|
48
|
+
for (const b of shell.querySelectorAll(`[data-shell="${name}"]`))
|
|
49
|
+
b.addEventListener("click", () => {
|
|
50
|
+
const on = shell.getAttribute(attr) !== "true";
|
|
51
|
+
on ? shell.setAttribute(attr, "true") : shell.removeAttribute(attr);
|
|
52
|
+
try { localStorage.setItem(key, on ? "1" : "0"); } catch { /* ignore */ }
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// focus the assistant's composer (e.g. a welcome page's "Ask…" start item). Links keep a real
|
|
57
|
+
// href fallback for no-JS; with JS we stay on the page and hand the cursor to the chat.
|
|
58
|
+
for (const el of shell.ownerDocument.querySelectorAll('[data-shell="focus-chat"]'))
|
|
59
|
+
el.addEventListener("click", (e) => {
|
|
60
|
+
const input = shell.querySelector(".assistant__composer input");
|
|
61
|
+
if (!input) return; // no assistant on this page → follow the link
|
|
62
|
+
e.preventDefault();
|
|
63
|
+
shell.removeAttribute("data-aside-hidden");
|
|
64
|
+
if (isMobile()) shell.setAttribute("data-aside-open", "true");
|
|
65
|
+
input.focus();
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
// expand/collapse the terminal feed — the console bar's own control AND the title-bar terminal
|
|
69
|
+
// button both toggle it (multiple triggers, so bind them all)
|
|
70
|
+
for (const t of shell.querySelectorAll('[data-shell="console-toggle"]'))
|
|
71
|
+
t.addEventListener("click", () => shell.toggleAttribute("data-console-open"));
|
|
72
|
+
|
|
73
|
+
// GROW the terminal to fill most of the shell (owner, 2026-07-06): opens the console if it
|
|
74
|
+
// was collapsed (growing a hidden feed makes no sense), then toggles the expanded state.
|
|
75
|
+
for (const g of shell.querySelectorAll('[data-shell="console-grow-toggle"]'))
|
|
76
|
+
g.addEventListener("click", () => {
|
|
77
|
+
if (!shell.hasAttribute("data-console-open")) shell.setAttribute("data-console-open", "");
|
|
78
|
+
shell.toggleAttribute("data-console-expanded");
|
|
79
|
+
});
|
|
80
|
+
const GROW_KEY = "grain.shell.console-expanded";
|
|
81
|
+
try { if (localStorage.getItem(GROW_KEY) === "1") shell.setAttribute("data-console-expanded", ""); } catch { /* ignore */ }
|
|
82
|
+
new MutationObserver(() => {
|
|
83
|
+
try { localStorage.setItem(GROW_KEY, shell.hasAttribute("data-console-expanded") ? "1" : "0"); } catch { /* private mode */ }
|
|
84
|
+
}).observe(shell, { attributes: true, attributeFilter: ["data-console-expanded"] });
|
|
85
|
+
|
|
86
|
+
// PERSIST the terminal's open state across MPA navigation. data-console-open has THREE writers
|
|
87
|
+
// (the toggle above, Ctrl+` in terminal.js, the terminal's `exit` command) — rather than chase
|
|
88
|
+
// every call site, make shell.js the single persister: restore on boot, then ONE MutationObserver
|
|
89
|
+
// writes the key whenever the attribute changes, whoever changed it (incl. future writers; `exit`
|
|
90
|
+
// → persists closed). Kept SEPARATE from PANE_KEYS above: those are inverse-semantics *-hidden
|
|
91
|
+
// click-toggles; this is a presence attribute observed at the source. (Key `grain.shell.console-open`.)
|
|
92
|
+
const CONSOLE_KEY = "grain.shell.console-open";
|
|
93
|
+
try { if (localStorage.getItem(CONSOLE_KEY) === "1") shell.setAttribute("data-console-open", ""); } catch { /* ignore */ }
|
|
94
|
+
new MutationObserver(() => {
|
|
95
|
+
try { localStorage.setItem(CONSOLE_KEY, shell.hasAttribute("data-console-open") ? "1" : "0"); } catch { /* private mode */ }
|
|
96
|
+
}).observe(shell, { attributes: true, attributeFilter: ["data-console-open"] });
|
|
97
|
+
|
|
98
|
+
// "open in terminal" (from the chat's thinking box): un-hide + expand the docked terminal so the
|
|
99
|
+
// full narration is visible (the chat only shows a compact thinking indicator during a run).
|
|
100
|
+
for (const b of shell.querySelectorAll('[data-shell="open-terminal"]'))
|
|
101
|
+
b.addEventListener("click", () => { shell.removeAttribute("data-console-hidden"); shell.setAttribute("data-console-open", ""); });
|
|
102
|
+
|
|
103
|
+
// OPTIONAL PANEL MODES (sidebar-panel): mode tabs ([data-shell-mode]) switch the assistant
|
|
104
|
+
// between its panes — data-mode on .assistant, hidden on the inactive panes, aria-selected on
|
|
105
|
+
// the tabs. Value-agnostic (the consumer names the modes); a panel with no panes has no tabs,
|
|
106
|
+
// so this is a no-op there. stopPropagation keeps a tab click off the mobile grab bar below.
|
|
107
|
+
const assistant = shell.querySelector(".assistant");
|
|
108
|
+
const setAsideMode = (mode) => {
|
|
109
|
+
if (!assistant) return;
|
|
110
|
+
assistant.setAttribute("data-mode", mode);
|
|
111
|
+
for (const p of assistant.querySelectorAll(".assistant__pane")) p.hidden = p.getAttribute("data-pane") !== mode;
|
|
112
|
+
for (const b of assistant.querySelectorAll("[data-shell-mode]")) b.setAttribute("aria-selected", b.getAttribute("data-shell-mode") === mode ? "true" : "false");
|
|
113
|
+
};
|
|
114
|
+
for (const b of shell.querySelectorAll(".assistant__modes [data-shell-mode]"))
|
|
115
|
+
b.addEventListener("click", (e) => {
|
|
116
|
+
e.stopPropagation();
|
|
117
|
+
setAsideMode(b.getAttribute("data-shell-mode"));
|
|
118
|
+
// on mobile the tabs sit INSIDE the grab bar: tapping one with the sheet closed must also
|
|
119
|
+
// RAISE the sheet (otherwise the tap looks swallowed — the pane it opened stays off-screen)
|
|
120
|
+
if (isMobile() && !shell.hasAttribute("data-aside-open")) shell.setAttribute("data-aside-open", "");
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
// mobile: the assistant is a bottom sheet — tap its header (the grab bar) to raise/lower it
|
|
124
|
+
shell.querySelector(".assistant__head")?.addEventListener("click", () => {
|
|
125
|
+
if (isMobile()) shell.toggleAttribute("data-aside-open");
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
// The narration feed lives in the docked terminal and STAYS there. During a run the terminal is
|
|
129
|
+
// collapsed to its bar; the chat's "thinking" box shows a small LIVE PREVIEW of the last few
|
|
130
|
+
// output lines (mirrored, not moved) with "open in terminal" to expand the full log.
|
|
131
|
+
// NB: a `replace` RenderOp swaps the surface NODE (outerHTML), so observe a STABLE ancestor
|
|
132
|
+
// (the console region) and re-query the feed each time — never bind to the surface node itself.
|
|
133
|
+
const consoleRegion = shell.querySelector(".app-shell__console");
|
|
134
|
+
const preview = shell.querySelector("[data-terminal-preview]");
|
|
135
|
+
if (consoleRegion && preview) {
|
|
136
|
+
// Mirror the last few narration lines into the chat, CLONING the real line nodes so each action
|
|
137
|
+
// keeps its own little box (the action-badge markup) — a compact live view of the terminal.
|
|
138
|
+
const mirror = () => {
|
|
139
|
+
const feed = consoleRegion.querySelector('[data-surface="console"]');
|
|
140
|
+
if (!feed) return;
|
|
141
|
+
const last = [...feed.children].slice(-3).map((n) => n.cloneNode(true));
|
|
142
|
+
preview.replaceChildren(...last);
|
|
143
|
+
};
|
|
144
|
+
new MutationObserver(mirror).observe(consoleRegion, { childList: true, subtree: true, characterData: true });
|
|
145
|
+
mirror();
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// the mobile scrim dismisses the drawer; so does following any nav link in the rail (the
|
|
149
|
+
// activity-bar app links AND the file-tree files — both live under .app-shell__rail now)
|
|
150
|
+
shell.querySelector(".app-shell__scrim")?.addEventListener("click", () => setOpen(false));
|
|
151
|
+
for (const a of shell.querySelectorAll(".app-shell__rail a"))
|
|
152
|
+
a.addEventListener("click", () => { if (isMobile()) setOpen(false); });
|
|
153
|
+
|
|
154
|
+
// crossing the breakpoint clears any stuck drawer / sheet state
|
|
155
|
+
mobile.addEventListener?.("change", () => { setOpen(false); shell.removeAttribute("data-aside-open"); });
|
|
156
|
+
|
|
157
|
+
// VIEWPORT PREVIEW (demo — owner 2026-07-07, made a REAL breakpoint drive 2026-07-14): cycles
|
|
158
|
+
// desktop → tablet → mobile → desktop by clamping `<body>`'s max-width, NOT `.app-shell`'s.
|
|
159
|
+
// `<body>` is the `@container` named `shell-frame` that app-shell.css's mobile/tablet layout
|
|
160
|
+
// breakpoints query (a container can't query its own condition, only a descendant's — see that
|
|
161
|
+
// file's comment) — so shrinking body here shrinks the SAME container a real narrow window
|
|
162
|
+
// shrinks, and the toggle now genuinely triggers the rail-drawer / hidden-aside / single-column
|
|
163
|
+
// layout, not just a framed width clamp. Still not a full device emulator (no simulated
|
|
164
|
+
// touch/DPR, and app-window.css's backdrop/title-bar collapse can't follow — that rule targets
|
|
165
|
+
// body itself, which can never legally container-query itself; deferred).
|
|
166
|
+
const VIEWPORTS = [null, "tablet", "mobile"];
|
|
167
|
+
for (const b of shell.querySelectorAll('[data-shell="viewport-toggle"]')) {
|
|
168
|
+
const label = b.querySelector("[data-viewport-name]");
|
|
169
|
+
b.addEventListener("click", () => {
|
|
170
|
+
const next = VIEWPORTS[(VIEWPORTS.indexOf(document.body.getAttribute("data-viewport")) + 1) % VIEWPORTS.length];
|
|
171
|
+
if (next) document.body.setAttribute("data-viewport", next); else document.body.removeAttribute("data-viewport");
|
|
172
|
+
if (label) label.textContent = next || "desktop";
|
|
173
|
+
// Toggling the viewport crosses the layout breakpoint WITHOUT a matchMedia `change` (that fires
|
|
174
|
+
// off the real window, which didn't move), so clear any stuck drawer/sheet the way the real
|
|
175
|
+
// breakpoint-cross handler below does — otherwise a sheet opened in one mode lingers in the next.
|
|
176
|
+
setOpen(false); shell.removeAttribute("data-aside-open");
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// mark the CURRENT route in the rail + tabs — aria-current is accessible AND already styled
|
|
181
|
+
// (nav-item/tab CSS targets [aria-current="page"]). The shared shell frame is identical on every
|
|
182
|
+
// page; this is what tells you where you are, from the URL. A tab/nav-item also claims its
|
|
183
|
+
// SUBPAGES (/notes claims /notes/slug) — exact match wins, then the longest prefix; "/" only
|
|
184
|
+
// ever matches exactly.
|
|
185
|
+
const here = location.pathname.replace(/\/+$/, "") || "/";
|
|
186
|
+
const byUrl = (sel) => { // longest-prefix URL match within one nav group
|
|
187
|
+
let best = null, bestLen = -1;
|
|
188
|
+
for (const a of shell.querySelectorAll(sel)) {
|
|
189
|
+
const href = (a.getAttribute("href") || "").replace(/\/+$/, "") || "/";
|
|
190
|
+
if (href === here) { best = a; bestLen = Infinity; break; }
|
|
191
|
+
if (href !== "/" && here.startsWith(href + "/") && href.length > bestLen) { best = a; bestLen = href.length; }
|
|
192
|
+
}
|
|
193
|
+
best?.setAttribute("aria-current", "page");
|
|
194
|
+
};
|
|
195
|
+
// the RAIL's app links: mark the item for the page's SECTION (data-section on .app-shell), so
|
|
196
|
+
// /calendar, /mail, … light their entry (they live in the side-rail's footer, not the narrow
|
|
197
|
+
// activity-bar). Falls back to URL match if unsectioned. (The file-tree's current-file marking
|
|
198
|
+
// is site.js's job — it walks the tree + unfolds ancestors.)
|
|
199
|
+
const section = shell.getAttribute("data-section");
|
|
200
|
+
const railItem = section && shell.querySelector(`.app-shell__rail [data-section="${section}"]`);
|
|
201
|
+
if (railItem) railItem.setAttribute("aria-current", "page"); else byUrl(".app-shell__rail a[data-section]");
|
|
202
|
+
// TABS = the section's submenus: mark by URL (a tab claims its own subpages too).
|
|
203
|
+
byUrl(".app-shell__tabs .tab");
|
|
204
|
+
})();
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// grain/scripts/shell.test.ts — DRIFT GUARD for the app-shell island. shell.js is a browser module
|
|
2
|
+
// (no DOM in bun; its behavior lives in the e2e). Here we guard the persistence CONTRACTS it relies
|
|
3
|
+
// on: the dotted house keys, and that every localStorage touch is wrapped in try/catch so private
|
|
4
|
+
// mode never throws. The console open-state persister is a single observer (three writers, one sink).
|
|
5
|
+
import { test, expect } from "bun:test";
|
|
6
|
+
import { readFileSync, readdirSync } from "node:fs";
|
|
7
|
+
import { join } from "node:path";
|
|
8
|
+
|
|
9
|
+
const js = readFileSync(join(import.meta.dir, "shell.js"), "utf8");
|
|
10
|
+
|
|
11
|
+
// grain root = one level up from scripts/. Walk it for CSS the shell attributes drive.
|
|
12
|
+
const grainRoot = join(import.meta.dir, "..");
|
|
13
|
+
function allCss(dir: string): string[] {
|
|
14
|
+
return readdirSync(dir, { recursive: true, encoding: "utf8" })
|
|
15
|
+
.filter((f) => f.endsWith(".css"))
|
|
16
|
+
.map((f) => join(dir, f));
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
test("persists the console open-state under grain.shell.console-open via one observer", () => {
|
|
20
|
+
expect(js).toMatch(/"grain\.shell\.console-open"/);
|
|
21
|
+
// restore on boot + observe the attribute at the source (captures all three writers)
|
|
22
|
+
expect(js).toMatch(/getItem\(CONSOLE_KEY\)\s*===\s*"1"/);
|
|
23
|
+
expect(js).toMatch(/attributeFilter:\s*\["data-console-open"\]/);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
// DEAD-KNOB GUARD (grain lesson 9; terminal-expand regressed in adede03). toggleAttribute() sets the
|
|
27
|
+
// attribute to "", which never equals "true" — so a value-form CSS selector [x="true"] for a toggled
|
|
28
|
+
// attribute never matches and the control flips while nothing moves. A boolean state must pick ONE
|
|
29
|
+
// idiom and match both sides: presence (toggleAttribute ↔ [x]) OR value (setAttribute ↔ [x="true"]).
|
|
30
|
+
test("no dead knobs: attrs set via toggleAttribute() have no value-form CSS selector", () => {
|
|
31
|
+
const toggled = [...js.matchAll(/toggleAttribute\("([a-z-]+)"/g)].map((m) => m[1]);
|
|
32
|
+
expect(toggled.length).toBeGreaterThan(0); // guard the guard: shell.js does toggle things
|
|
33
|
+
|
|
34
|
+
const css = allCss(grainRoot).map((f) => readFileSync(f, "utf8")).join("\n");
|
|
35
|
+
for (const attr of toggled) {
|
|
36
|
+
const valueForm = new RegExp(`\\[${attr}="`);
|
|
37
|
+
expect(css, `${attr} is presence-toggled but a CSS selector uses value form [${attr}="…"] — dead knob`)
|
|
38
|
+
.not.toMatch(valueForm);
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
test("every localStorage write is guarded by try/catch (private mode never throws)", () => {
|
|
43
|
+
// no bare setItem outside a try block
|
|
44
|
+
for (const m of js.matchAll(/localStorage\.setItem/g)) {
|
|
45
|
+
const before = js.slice(0, m.index);
|
|
46
|
+
const lastTry = before.lastIndexOf("try");
|
|
47
|
+
const lastCatch = before.lastIndexOf("catch");
|
|
48
|
+
expect(lastTry).toBeGreaterThan(lastCatch); // inside an open try { … }
|
|
49
|
+
}
|
|
50
|
+
});
|
package/scripts/tabs.js
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
// grain/scripts/tabs.js — OPEN TABS: the tab-bar becomes the editor's open-pages strip.
|
|
2
|
+
//
|
|
3
|
+
// The shell is an MPA — every navigation is a real page load — so "open tabs" is a client-side
|
|
4
|
+
// PROJECTION of where you've been (localStorage), never an SPA router. On load the island reads
|
|
5
|
+
// the stored list, appends the current path if it's new, and renders the strip; each tab is a
|
|
6
|
+
// real <a>, so navigation stays plain hypermedia. Zero-JS the strip shows only its server-rendered
|
|
7
|
+
// PINNED tabs (the markup's static <a data-pinned> children — never closable, never stored) and
|
|
8
|
+
// the page navigates by its other nav (e.g. a file-tree rail).
|
|
9
|
+
//
|
|
10
|
+
// OPT-IN: mark the strip `data-open-tabs` (on the .tab-bar nav). Labels come from a
|
|
11
|
+
// `data-tab-source` container elsewhere on the page (e.g. the explorer file-tree): the anchor
|
|
12
|
+
// there whose href matches the path lends its text — or its `data-tab-label` if set; a path with
|
|
13
|
+
// no source entry falls back to its last segment. The × (`.tab__close`, presentational in the
|
|
14
|
+
// molecule) is wired here as a REAL control: closing the active tab navigates to the neighbor
|
|
15
|
+
// (falling back to the first pinned tab); closing a background tab just drops it from the list.
|
|
16
|
+
// Pure UI chrome — it knows nothing about the AI door (like shell.js).
|
|
17
|
+
(() => {
|
|
18
|
+
"use strict";
|
|
19
|
+
if (window.grain && window.grain.tabs) return; // idempotent
|
|
20
|
+
const KEY = "grain.tabs.open";
|
|
21
|
+
const CAP = 20; // storage sanity; the strip itself scrolls
|
|
22
|
+
|
|
23
|
+
const norm = (p) => { try { p = new URL(p, location.origin).pathname; } catch { /* keep as-is */ }
|
|
24
|
+
return p.replace(/\/+$/, "") || "/"; };
|
|
25
|
+
const load = () => { try { return (JSON.parse(localStorage.getItem(KEY) || "[]") || []).map(norm); } catch { return []; } };
|
|
26
|
+
const save = (l) => { try { localStorage.setItem(KEY, JSON.stringify(l.slice(-CAP))); } catch { /* private mode */ } };
|
|
27
|
+
|
|
28
|
+
function init() {
|
|
29
|
+
const strip = document.querySelector("[data-open-tabs]");
|
|
30
|
+
if (!strip) return;
|
|
31
|
+
const here = norm(location.pathname);
|
|
32
|
+
const pinned = new Set([...strip.querySelectorAll("a[data-pinned]")].map((a) => norm(a.getAttribute("href") || "/")));
|
|
33
|
+
|
|
34
|
+
// the label source: the explorer tree (or any nav) marked data-tab-source
|
|
35
|
+
const source = document.querySelector("[data-tab-source]");
|
|
36
|
+
const labelOf = (path) => {
|
|
37
|
+
for (const a of source ? source.querySelectorAll("a[href]") : []) {
|
|
38
|
+
if (norm(a.getAttribute("href")) !== path) continue;
|
|
39
|
+
return a.getAttribute("data-tab-label") || a.textContent.trim() || path;
|
|
40
|
+
}
|
|
41
|
+
return path === "/" ? "/" : path.split("/").filter(Boolean).pop();
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
let list = load().filter((p) => !pinned.has(p));
|
|
45
|
+
if (!pinned.has(here) && !list.includes(here)) list.push(here);
|
|
46
|
+
save(list);
|
|
47
|
+
|
|
48
|
+
const render = () => {
|
|
49
|
+
for (const el of strip.querySelectorAll("a:not([data-pinned])")) el.remove();
|
|
50
|
+
for (const path of list) {
|
|
51
|
+
const a = document.createElement("a");
|
|
52
|
+
a.className = "tab";
|
|
53
|
+
a.href = path;
|
|
54
|
+
const label = document.createElement("span");
|
|
55
|
+
label.textContent = labelOf(path);
|
|
56
|
+
const close = document.createElement("span");
|
|
57
|
+
close.className = "tab__close";
|
|
58
|
+
close.setAttribute("role", "button");
|
|
59
|
+
close.setAttribute("tabindex", "0");
|
|
60
|
+
close.setAttribute("aria-label", `Close ${label.textContent}`);
|
|
61
|
+
close.style.pointerEvents = "auto"; // the molecule ships it presentational; this strip wires it live
|
|
62
|
+
close.textContent = "×";
|
|
63
|
+
const onClose = (e) => { e.preventDefault(); e.stopPropagation(); closeTab(path); };
|
|
64
|
+
close.addEventListener("click", onClose);
|
|
65
|
+
close.addEventListener("keydown", (e) => { if (e.key === "Enter" || e.key === " ") onClose(e); });
|
|
66
|
+
a.append(label, close);
|
|
67
|
+
if (path === here) a.setAttribute("aria-current", "page");
|
|
68
|
+
strip.appendChild(a);
|
|
69
|
+
}
|
|
70
|
+
// pinned tabs get their current-mark here too (shell.js may have run before we rendered)
|
|
71
|
+
for (const a of strip.querySelectorAll("a[data-pinned]"))
|
|
72
|
+
if (norm(a.getAttribute("href") || "/") === here) a.setAttribute("aria-current", "page");
|
|
73
|
+
|
|
74
|
+
// the explorer (data-tab-source) gets a close × on any file that's an open tab too —
|
|
75
|
+
// closing from the sidebar without switching to the page first. Idempotent: adds/removes
|
|
76
|
+
// the span to match `list` on every render, never duplicates.
|
|
77
|
+
for (const a of source ? source.querySelectorAll("a[href]") : []) {
|
|
78
|
+
const path = norm(a.getAttribute("href"));
|
|
79
|
+
const existing = a.querySelector(":scope > .file-tree__close");
|
|
80
|
+
if (!list.includes(path)) { existing?.remove(); continue; }
|
|
81
|
+
if (existing) continue;
|
|
82
|
+
const close = document.createElement("span");
|
|
83
|
+
close.className = "file-tree__close";
|
|
84
|
+
close.setAttribute("role", "button");
|
|
85
|
+
close.setAttribute("tabindex", "0");
|
|
86
|
+
close.setAttribute("aria-label", `Close ${a.textContent.trim()}`);
|
|
87
|
+
close.style.pointerEvents = "auto"; // the molecule ships it presentational; wire it live
|
|
88
|
+
close.textContent = "×";
|
|
89
|
+
const onClose = (e) => { e.preventDefault(); e.stopPropagation(); closeTab(path); };
|
|
90
|
+
close.addEventListener("click", onClose);
|
|
91
|
+
close.addEventListener("keydown", (e) => { if (e.key === "Enter" || e.key === " ") onClose(e); });
|
|
92
|
+
a.appendChild(close);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// "close all": only meaningful once something closable is actually open
|
|
96
|
+
for (const b of allBtns) b.hidden = list.length === 0;
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
const closeTab = (path) => {
|
|
100
|
+
const i = list.indexOf(path);
|
|
101
|
+
list = list.filter((p) => p !== path);
|
|
102
|
+
save(list);
|
|
103
|
+
if (path !== here) return render();
|
|
104
|
+
// closing the page you're ON: go to the neighbor tab, else the first pinned tab, else home
|
|
105
|
+
const next = list[Math.min(i, list.length - 1)]
|
|
106
|
+
|| (strip.querySelector("a[data-pinned]") || {}).getAttribute?.("href") || "/";
|
|
107
|
+
location.assign(next);
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
const closeAll = () => {
|
|
111
|
+
const wasHere = list.includes(here);
|
|
112
|
+
list = [];
|
|
113
|
+
save(list);
|
|
114
|
+
if (!wasHere) return render();
|
|
115
|
+
location.assign((strip.querySelector("a[data-pinned]") || {}).getAttribute?.("href") || "/");
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
const allBtns = document.querySelectorAll('[data-shell="tabs-close-all"]');
|
|
119
|
+
for (const b of allBtns) b.addEventListener("click", closeAll);
|
|
120
|
+
|
|
121
|
+
render();
|
|
122
|
+
window.grain = window.grain || {};
|
|
123
|
+
// refresh: re-resolve labels (e.g. after an island fills the data-tab-source tree lazily)
|
|
124
|
+
window.grain.tabs = { open: () => [...list], close: closeTab, closeAll, refresh: render };
|
|
125
|
+
}
|
|
126
|
+
if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", init); else init();
|
|
127
|
+
})();
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// grain/scripts/tabs.test.ts — DRIFT GUARD for the open-tabs island (behavior lives in the e2e;
|
|
2
|
+
// here we guard the contracts other pieces build on).
|
|
3
|
+
import { test, expect } from "bun:test";
|
|
4
|
+
import { readFileSync } from "node:fs";
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
|
|
7
|
+
const js = readFileSync(join(import.meta.dir, "tabs.js"), "utf8");
|
|
8
|
+
|
|
9
|
+
test("only mounts an opt-in strip (data-open-tabs)", () => {
|
|
10
|
+
expect(js).toMatch(/\[data-open-tabs\]/);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
test("pinned tabs are never stored or closable (data-pinned contract)", () => {
|
|
14
|
+
expect(js).toMatch(/a\[data-pinned\]/);
|
|
15
|
+
expect(js).toMatch(/filter\(\(p\) => !pinned\.has\(p\)\)/);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
test("labels come from the data-tab-source nav (the file-tree pairing)", () => {
|
|
19
|
+
expect(js).toMatch(/\[data-tab-source\]/);
|
|
20
|
+
expect(js).toMatch(/data-tab-label/);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
test("tabs are real anchors — hypermedia, not a router", () => {
|
|
24
|
+
expect(js).toMatch(/createElement\("a"\)/);
|
|
25
|
+
expect(js).not.toMatch(/preventDefault\(\);\s*history\.pushState/);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test("exposes the window.grain.tabs seam", () => {
|
|
29
|
+
expect(js).toMatch(/window\.grain\.tabs\s*=\s*\{[\s\S]*\bclose\b/);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test("close-all is opt-in (data-shell=\"tabs-close-all\") and exposed on the seam", () => {
|
|
33
|
+
expect(js).toMatch(/\[data-shell="tabs-close-all"\]/);
|
|
34
|
+
expect(js).toMatch(/window\.grain\.tabs\s*=\s*\{[\s\S]*\bcloseAll\b/);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test("the sidebar (data-tab-source) gets a live close affordance for open tabs, not static markup", () => {
|
|
38
|
+
expect(js).toMatch(/file-tree__close/);
|
|
39
|
+
expect(js).not.toMatch(/<span class="file-tree__close"/); // injected via createElement, not templated
|
|
40
|
+
});
|