@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
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// grain/scripts/demo-box.js — a reusable, contained "demo box": a demonstration surface that runs
|
|
2
|
+
// a DECLARATIVE scripted sequence over its own contents. Keeps demo JavaScript out of pages — a page
|
|
3
|
+
// just writes markup + a JSON step list; this island runs it. Native ES module; the typing effect +
|
|
4
|
+
// reduced-motion rule come from the shared grain/scripts/type-effect.js (no duplicated animation).
|
|
5
|
+
//
|
|
6
|
+
// <div data-demo="loop"> data-demo = "loop" | "trigger" | "once"
|
|
7
|
+
// …content with #ids / .classes…
|
|
8
|
+
// <button data-demo-play>replay</button> (trigger mode)
|
|
9
|
+
// <script type="application/json" class="demo-box__steps">
|
|
10
|
+
// [ {"type":"#line","value":"Hello","caret":"#c","speed":45},
|
|
11
|
+
// {"attr":"#row","name":"data-grade","value":"grain"}, {"wait":1500},
|
|
12
|
+
// {"clear":"#row","name":"data-grade"}, {"text":"#b","value":"done"} ]
|
|
13
|
+
// </script>
|
|
14
|
+
// </div>
|
|
15
|
+
//
|
|
16
|
+
// Selectors are scoped to the box (contained). Reduced-motion runs ONE instant pass (no typing, no
|
|
17
|
+
// waits, no loop) so every demo lands on a sensible final state.
|
|
18
|
+
import { typeText, reducedMotion } from "/scripts/type-effect.js";
|
|
19
|
+
|
|
20
|
+
const rm = reducedMotion();
|
|
21
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
22
|
+
|
|
23
|
+
for (const box of document.querySelectorAll("[data-demo]")) setup(box);
|
|
24
|
+
|
|
25
|
+
function setup(box) {
|
|
26
|
+
const scriptEl = box.querySelector(".demo-box__steps");
|
|
27
|
+
if (!scriptEl) return;
|
|
28
|
+
let steps;
|
|
29
|
+
try { steps = JSON.parse(scriptEl.textContent); } catch { return; }
|
|
30
|
+
if (!Array.isArray(steps)) return;
|
|
31
|
+
const q = (sel) => (sel ? box.querySelector(sel) : null);
|
|
32
|
+
|
|
33
|
+
async function run(s) {
|
|
34
|
+
const el = q(s.type || s.attr || s.clear || s.text);
|
|
35
|
+
if (s.type != null) { await typeText(el, s.value ?? "", { speed: s.speed, caret: q(s.caret) }); }
|
|
36
|
+
else if (s.attr != null) { if (el) el.setAttribute(s.name, s.value); }
|
|
37
|
+
else if (s.clear != null) { if (el) el.removeAttribute(s.name); }
|
|
38
|
+
else if (s.text != null) { if (el) el.textContent = s.value ?? ""; }
|
|
39
|
+
else if (s.wait != null) { if (!rm) await sleep(s.wait); }
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const playOnce = async () => { for (const s of steps) await run(s); };
|
|
43
|
+
const mode = box.getAttribute("data-demo");
|
|
44
|
+
|
|
45
|
+
if (mode === "trigger") {
|
|
46
|
+
box.querySelector("[data-demo-play]")?.addEventListener("click", playOnce);
|
|
47
|
+
playOnce(); // auto-play once on load; replayable
|
|
48
|
+
} else if (mode === "loop") {
|
|
49
|
+
if (rm) playOnce(); else (async () => { for (;;) await playOnce(); })();
|
|
50
|
+
} else {
|
|
51
|
+
playOnce();
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// grain/scripts/handoff.js — hand page content off to an external service via a URL template.
|
|
2
|
+
//
|
|
3
|
+
// The generic shape behind "open this in <service>" buttons: a page composes some text (a
|
|
4
|
+
// prompt, a draft, a query), and instead of the visitor copy-pasting it into another tab, one
|
|
5
|
+
// click opens the target service with the text already in place. GRAIN stays vendor-neutral:
|
|
6
|
+
// the service lives entirely in the host's URL template — an AI chat, a search engine, a
|
|
7
|
+
// translator, an issue tracker are all the same one attribute.
|
|
8
|
+
//
|
|
9
|
+
// Self-contained: one <script> tag drops it onto any page. Declarative contract:
|
|
10
|
+
//
|
|
11
|
+
// [data-handoff] a trigger. Clicking it composes the URL and opens it in a new tab.
|
|
12
|
+
// data-handoff-url REQUIRED on the trigger. The URL template; `{payload}` marks where
|
|
13
|
+
// the (URI-encoded) payload goes. https:// or http:// only — every
|
|
14
|
+
// other scheme is refused at click time, never opened.
|
|
15
|
+
// e.g. https://claude.ai/new?q={payload}
|
|
16
|
+
// https://www.google.com/search?q={payload}
|
|
17
|
+
// data-handoff-source CSS selector for the payload element. A form control contributes
|
|
18
|
+
// its live .value; anything else its trimmed textContent. Falls back
|
|
19
|
+
// to data-handoff-payload (a literal) when absent; no source and no
|
|
20
|
+
// literal ⇒ empty payload (the template may be self-sufficient).
|
|
21
|
+
//
|
|
22
|
+
// The payload is encodeURIComponent'd BEFORE substitution, so template query strings stay
|
|
23
|
+
// well-formed whatever the text contains. No length cap here: very long payloads can exceed a
|
|
24
|
+
// target's URL limit — that's the target's error to surface, not this file's to silently trim.
|
|
25
|
+
// New tab opens with noopener: the target page must never get a handle back onto this one.
|
|
26
|
+
(() => {
|
|
27
|
+
"use strict";
|
|
28
|
+
if (window.grainHandoff) return; // idempotent
|
|
29
|
+
|
|
30
|
+
const SAFE_HANDOFF_URL = /^https?:\/\//i;
|
|
31
|
+
|
|
32
|
+
const payloadOf = (trigger) => {
|
|
33
|
+
const sel = trigger.dataset.handoffSource;
|
|
34
|
+
const src = sel ? document.querySelector(sel) : null;
|
|
35
|
+
if (src) return "value" in src && typeof src.value === "string" ? src.value : (src.textContent || "").trim();
|
|
36
|
+
return trigger.dataset.handoffPayload || "";
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/** Resolve a trigger's template + payload into the final URL, or "" when the template is
|
|
40
|
+
* missing, has an unsafe scheme, or doesn't parse. Pure over its inputs — exposed on
|
|
41
|
+
* window.grainHandoff for hosts that want to compose without a click (e.g. a preview). */
|
|
42
|
+
const compose = (template, payload) => {
|
|
43
|
+
if (!template || !SAFE_HANDOFF_URL.test(template)) return "";
|
|
44
|
+
const url = template.replaceAll("{payload}", encodeURIComponent(payload ?? ""));
|
|
45
|
+
try { new URL(url); } catch { return ""; }
|
|
46
|
+
return url;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
function init() {
|
|
50
|
+
document.addEventListener("click", (e) => {
|
|
51
|
+
// leave modified clicks alone — same courtesy as lightbox.js: cmd/ctrl/shift/middle-click
|
|
52
|
+
// keeps its native meaning on an <a> trigger.
|
|
53
|
+
if (e.defaultPrevented || e.button !== 0 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) return;
|
|
54
|
+
const trigger = e.target.closest && e.target.closest("[data-handoff]");
|
|
55
|
+
if (!trigger) return;
|
|
56
|
+
const url = compose(trigger.dataset.handoffUrl, payloadOf(trigger));
|
|
57
|
+
if (!url) return; // unsafe or unparseable: refuse quietly, no navigation
|
|
58
|
+
e.preventDefault();
|
|
59
|
+
window.open(url, "_blank", "noopener");
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
window.grainHandoff = { compose };
|
|
63
|
+
if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", init); else init();
|
|
64
|
+
})();
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// grain/scripts/handoff.test.ts — handoff.js is a browser-native IIFE (DOM globals at load
|
|
2
|
+
// time) that can't be imported into a bun test, same story as ai-dispatch.test.ts. What CAN
|
|
3
|
+
// be verified without a browser: the declarative contract callers author against, and the
|
|
4
|
+
// safety rules the header promises — scheme guard, encode-before-substitute, noopener.
|
|
5
|
+
import { test, expect } from "bun:test";
|
|
6
|
+
|
|
7
|
+
const js = await Bun.file(new URL("./handoff.js", import.meta.url)).text();
|
|
8
|
+
|
|
9
|
+
test("the handoff keeps its declarative contract", () => {
|
|
10
|
+
expect(js).toContain("[data-handoff]"); // the trigger attribute
|
|
11
|
+
expect(js).toContain("handoffUrl"); // the URL-template attribute
|
|
12
|
+
expect(js).toContain("handoffSource"); // the payload-source selector
|
|
13
|
+
expect(js).toContain("handoffPayload"); // the literal-payload fallback
|
|
14
|
+
expect(js).toContain("{payload}"); // the template slot
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
test("only http(s) templates are accepted — the scheme guard exists and is anchored", () => {
|
|
18
|
+
const re = js.match(/const SAFE_HANDOFF_URL = (\/.*\/i?);/)?.[1];
|
|
19
|
+
expect(re).toBeDefined();
|
|
20
|
+
const guard = new Function(`return ${re}`)() as RegExp;
|
|
21
|
+
expect(guard.test("https://claude.ai/new?q={payload}")).toBe(true);
|
|
22
|
+
expect(guard.test("http://localhost:4000/x?q={payload}")).toBe(true);
|
|
23
|
+
expect(guard.test("javascript:alert(1)")).toBe(false);
|
|
24
|
+
expect(guard.test("mailto:x@y.z?body={payload}")).toBe(false);
|
|
25
|
+
expect(guard.test(" https://padded.example")).toBe(false); // anchored: no leading junk
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test("the payload is URI-encoded before substitution, and the tab opens with noopener", () => {
|
|
29
|
+
// the one substitution site encodes inline — no raw-payload path exists
|
|
30
|
+
expect(js).toContain('replaceAll("{payload}", encodeURIComponent(');
|
|
31
|
+
expect(js).toContain('"noopener"');
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test("modified clicks pass through untouched (the lightbox courtesy)", () => {
|
|
35
|
+
expect(js).toMatch(/metaKey.*ctrlKey.*shiftKey.*altKey/);
|
|
36
|
+
});
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
// grain/scripts/lightbox.js — a design-system image modal (full-size viewer).
|
|
2
|
+
//
|
|
3
|
+
// Self-contained: one <script> tag drops it onto any page; a <link> to lightbox.css
|
|
4
|
+
// must precede it. Declarative contract, no markup to author beyond two attributes:
|
|
5
|
+
//
|
|
6
|
+
// [data-lightbox] a trigger. Clicking it opens the viewer on that image.
|
|
7
|
+
// If the trigger is an <a href="…">, the href is the FULL image
|
|
8
|
+
// — so with no JS (or a modified click) it still navigates there,
|
|
9
|
+
// which is the no-JS-safe fallback. Otherwise a child <img>'s
|
|
10
|
+
// currentSrc is used, or data-lightbox-src if set.
|
|
11
|
+
// [data-lightbox-group] an ancestor that groups triggers into one gallery: prev/next
|
|
12
|
+
// and the dot rail walk every [data-lightbox] inside it, in DOM
|
|
13
|
+
// order (INCLUDING ones a strip hides past a cap — the viewer is
|
|
14
|
+
// how you reach the rest). No group ⇒ the single image, no rail.
|
|
15
|
+
//
|
|
16
|
+
// Caption = data-lightbox-caption on the trigger, else the child <img> alt. Uses a native
|
|
17
|
+
// <dialog> (showModal) for the focus-trap + focus-restore + Escape + ::backdrop + top-layer
|
|
18
|
+
// for free — same pattern as cmdk.js. Monochrome via the page's design tokens.
|
|
19
|
+
(() => {
|
|
20
|
+
"use strict";
|
|
21
|
+
if (window.grainLightbox) return; // idempotent
|
|
22
|
+
|
|
23
|
+
let root, imgEl, capEl, dotsEl, countEl, prevBtn, nextBtn;
|
|
24
|
+
let group = [], index = 0;
|
|
25
|
+
|
|
26
|
+
// the full-size source for a trigger: an <a> points at it via href (the no-JS fallback);
|
|
27
|
+
// otherwise take an explicit data-lightbox-src, or the rendered <img>'s currentSrc/src.
|
|
28
|
+
const srcOf = (el) => {
|
|
29
|
+
if (el.dataset.lightboxSrc) return el.dataset.lightboxSrc;
|
|
30
|
+
if (el.tagName === "A" && el.getAttribute("href")) return el.href;
|
|
31
|
+
const img = el.matches("img") ? el : el.querySelector("img");
|
|
32
|
+
return img ? (img.currentSrc || img.src) : "";
|
|
33
|
+
};
|
|
34
|
+
const captionOf = (el) => {
|
|
35
|
+
if (el.dataset.lightboxCaption != null) return el.dataset.lightboxCaption;
|
|
36
|
+
const img = el.matches("img") ? el : el.querySelector("img");
|
|
37
|
+
return img ? (img.getAttribute("alt") || "") : "";
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
function build() {
|
|
41
|
+
root = document.createElement("dialog");
|
|
42
|
+
root.className = "lightbox";
|
|
43
|
+
root.setAttribute("aria-label", "Image viewer");
|
|
44
|
+
root.innerHTML = `
|
|
45
|
+
<button class="lightbox__close" type="button" aria-label="Close">
|
|
46
|
+
<svg class="icon" aria-hidden="true"><use href="/assets/sprite.svg#close"></use></svg>
|
|
47
|
+
</button>
|
|
48
|
+
<button class="lightbox__nav lightbox__nav--prev" type="button" aria-label="Previous image">
|
|
49
|
+
<svg class="icon" aria-hidden="true"><use href="/assets/sprite.svg#chevron-left"></use></svg>
|
|
50
|
+
</button>
|
|
51
|
+
<figure class="lightbox__figure">
|
|
52
|
+
<img class="lightbox__img" alt="">
|
|
53
|
+
<figcaption class="lightbox__caption"></figcaption>
|
|
54
|
+
</figure>
|
|
55
|
+
<button class="lightbox__nav lightbox__nav--next" type="button" aria-label="Next image">
|
|
56
|
+
<svg class="icon" aria-hidden="true"><use href="/assets/sprite.svg#chevron-right"></use></svg>
|
|
57
|
+
</button>
|
|
58
|
+
<div class="lightbox__dots" role="tablist" aria-label="Photos"></div>
|
|
59
|
+
<p class="lightbox__count" aria-live="polite"></p>`;
|
|
60
|
+
document.body.appendChild(root);
|
|
61
|
+
imgEl = root.querySelector(".lightbox__img");
|
|
62
|
+
capEl = root.querySelector(".lightbox__caption");
|
|
63
|
+
dotsEl = root.querySelector(".lightbox__dots");
|
|
64
|
+
countEl = root.querySelector(".lightbox__count");
|
|
65
|
+
prevBtn = root.querySelector(".lightbox__nav--prev");
|
|
66
|
+
nextBtn = root.querySelector(".lightbox__nav--next");
|
|
67
|
+
|
|
68
|
+
// light-dismiss: a click on the backdrop (the dialog itself, or the figure's empty
|
|
69
|
+
// gutter) closes; clicks on the image or a control do not.
|
|
70
|
+
root.addEventListener("click", (e) => {
|
|
71
|
+
if (e.target === root || e.target.classList.contains("lightbox__figure")) close();
|
|
72
|
+
});
|
|
73
|
+
root.querySelector(".lightbox__close").addEventListener("click", close);
|
|
74
|
+
prevBtn.addEventListener("click", () => move(-1));
|
|
75
|
+
nextBtn.addEventListener("click", () => move(1));
|
|
76
|
+
// key handling on the document, not the dialog: after a nav-button click focus can sit outside
|
|
77
|
+
// the dialog subtree, so a listener bound to <dialog> would miss the keystroke. Guard on open
|
|
78
|
+
// state. Close explicitly rather than leaning on the dialog's native cancel — reliable across
|
|
79
|
+
// engines and host key handlers.
|
|
80
|
+
document.addEventListener("keydown", (e) => {
|
|
81
|
+
if (!root.open) return;
|
|
82
|
+
if (e.key === "Escape") { e.preventDefault(); close(); return; }
|
|
83
|
+
if (group.length < 2) return;
|
|
84
|
+
if (e.key === "ArrowLeft") { e.preventDefault(); move(-1); }
|
|
85
|
+
else if (e.key === "ArrowRight") { e.preventDefault(); move(1); }
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function paintDots() {
|
|
90
|
+
// dots are the optional gallery rail: only for a real multi-image group, and only when the
|
|
91
|
+
// count is small enough to read as dots (a big set leans on the counter + arrows instead).
|
|
92
|
+
const many = group.length > 1;
|
|
93
|
+
const dotted = many && group.length <= 10;
|
|
94
|
+
dotsEl.hidden = !dotted;
|
|
95
|
+
countEl.hidden = !many;
|
|
96
|
+
prevBtn.hidden = nextBtn.hidden = !many;
|
|
97
|
+
if (many) countEl.textContent = `${index + 1} / ${group.length}`;
|
|
98
|
+
if (!dotted) { dotsEl.replaceChildren(); return; }
|
|
99
|
+
dotsEl.replaceChildren(...group.map((_, i) => {
|
|
100
|
+
const b = document.createElement("button");
|
|
101
|
+
b.type = "button"; b.className = "lightbox__dot"; b.setAttribute("role", "tab");
|
|
102
|
+
b.setAttribute("aria-label", `Photo ${i + 1}`);
|
|
103
|
+
if (i === index) b.setAttribute("aria-selected", "true");
|
|
104
|
+
b.addEventListener("click", () => show(i));
|
|
105
|
+
return b;
|
|
106
|
+
}));
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function show(i) {
|
|
110
|
+
index = (i + group.length) % group.length;
|
|
111
|
+
const trigger = group[index];
|
|
112
|
+
imgEl.src = srcOf(trigger);
|
|
113
|
+
const cap = captionOf(trigger);
|
|
114
|
+
imgEl.alt = cap;
|
|
115
|
+
capEl.textContent = cap;
|
|
116
|
+
capEl.hidden = !cap;
|
|
117
|
+
paintDots();
|
|
118
|
+
}
|
|
119
|
+
const move = (d) => show(index + d);
|
|
120
|
+
|
|
121
|
+
function open(trigger) {
|
|
122
|
+
const groupRoot = trigger.closest("[data-lightbox-group]");
|
|
123
|
+
group = groupRoot ? [...groupRoot.querySelectorAll("[data-lightbox]")] : [trigger];
|
|
124
|
+
if (!group.length) group = [trigger];
|
|
125
|
+
show(Math.max(0, group.indexOf(trigger)));
|
|
126
|
+
root.showModal();
|
|
127
|
+
}
|
|
128
|
+
function close() { if (root && root.open) root.close(); }
|
|
129
|
+
|
|
130
|
+
function init() {
|
|
131
|
+
build();
|
|
132
|
+
document.addEventListener("click", (e) => {
|
|
133
|
+
// leave modified clicks alone: cmd/ctrl/shift/middle-click on an <a> trigger should
|
|
134
|
+
// still open the full image in a new tab (the href is a real, useful link).
|
|
135
|
+
if (e.defaultPrevented || e.button !== 0 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) return;
|
|
136
|
+
const trigger = e.target.closest && e.target.closest("[data-lightbox]");
|
|
137
|
+
if (!trigger) return;
|
|
138
|
+
e.preventDefault();
|
|
139
|
+
open(trigger);
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
window.grainLightbox = { open, close };
|
|
143
|
+
if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", init); else init();
|
|
144
|
+
})();
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// grain/scripts/lightbox.test.ts — the viewer draws its close/prev/next chrome from the shared
|
|
2
|
+
// icon sprite via <use href="…#id">. A sprite id that doesn't exist fails silently (an empty
|
|
3
|
+
// <svg>), so guard that every id lightbox.js references is real — and that the file still ships
|
|
4
|
+
// its declarative contract (the two data-attributes callers author against).
|
|
5
|
+
import { test, expect } from "bun:test";
|
|
6
|
+
import { readFileSync } from "node:fs";
|
|
7
|
+
import { join } from "node:path";
|
|
8
|
+
|
|
9
|
+
const js = readFileSync(join(import.meta.dir, "lightbox.js"), "utf8");
|
|
10
|
+
const sprite = readFileSync(join(import.meta.dir, "..", "assets", "sprite.svg"), "utf8");
|
|
11
|
+
|
|
12
|
+
test("every sprite id the lightbox references exists in the sprite", () => {
|
|
13
|
+
const ids = [...js.matchAll(/sprite\.svg#([\w-]+)/g)].map((m) => m[1]);
|
|
14
|
+
expect(ids.length).toBeGreaterThan(0); // sanity: refs were parsed
|
|
15
|
+
const known = new Set([...sprite.matchAll(/id="([^"]+)"/g)].map((m) => m[1]));
|
|
16
|
+
expect(ids.filter((id) => !known.has(id))).toEqual([]);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
test("the lightbox keeps its declarative contract", () => {
|
|
20
|
+
expect(js).toContain("[data-lightbox]"); // the trigger attribute
|
|
21
|
+
expect(js).toContain("[data-lightbox-group]"); // the gallery-grouping attribute
|
|
22
|
+
expect(js).toContain("showModal"); // native <dialog>, not a bespoke scrim
|
|
23
|
+
});
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// grain/scripts/made-with — the system byline as a string, for server-side template-literal
|
|
2
|
+
// shells (pantry, greenroom, proof, mill, …). One source of truth for the CONTENT of the
|
|
3
|
+
// made-with molecule (components/molecules/made-with/) so the line can never drift per app.
|
|
4
|
+
// Plain ESM, no DOM: importable from Bun servers and browsers alike.
|
|
5
|
+
|
|
6
|
+
const GRAIN_URL = "https://tjakoen.github.io/grain";
|
|
7
|
+
const AUTHOR_URL = "https://tjakoen.github.io";
|
|
8
|
+
|
|
9
|
+
const LINE =
|
|
10
|
+
`made with <a href="${GRAIN_URL}">GRAIN</a> by <a href="${AUTHOR_URL}">tjakoen</a>`;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The byline markup. Block form (default) is a standalone page footer; inline form is a
|
|
14
|
+
* span for the right side of a status-bar row (after the __spacer).
|
|
15
|
+
* @param {{ inline?: boolean }} [opts]
|
|
16
|
+
* @returns {string}
|
|
17
|
+
*/
|
|
18
|
+
export function madeWith({ inline = false } = {}) {
|
|
19
|
+
return inline
|
|
20
|
+
? `<span class="made-with" data-inline>${LINE}</span>`
|
|
21
|
+
: `<footer class="made-with">${LINE}</footer>`;
|
|
22
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// grain/scripts/made-with.test.ts — the byline is a CONTRACT: every GRAIN app mounts this
|
|
2
|
+
// exact line, so its content and both form factors are pinned here (drift = fleet-wide drift).
|
|
3
|
+
import { test, expect } from "bun:test";
|
|
4
|
+
import { madeWith } from "./made-with.js";
|
|
5
|
+
|
|
6
|
+
test("block form: a footer.made-with linking GRAIN and tjakoen", () => {
|
|
7
|
+
const html = madeWith();
|
|
8
|
+
expect(html).toStartWith('<footer class="made-with">');
|
|
9
|
+
expect(html).toEndWith("</footer>");
|
|
10
|
+
expect(html).toContain('made with <a href="https://tjakoen.github.io/grain">GRAIN</a>');
|
|
11
|
+
expect(html).toContain('by <a href="https://tjakoen.github.io">tjakoen</a>');
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
test("inline form: a span with data-inline, same line", () => {
|
|
15
|
+
const html = madeWith({ inline: true });
|
|
16
|
+
expect(html).toStartWith('<span class="made-with" data-inline>');
|
|
17
|
+
expect(html).toEndWith("</span>");
|
|
18
|
+
expect(html).toContain(">GRAIN</a>");
|
|
19
|
+
expect(html).toContain(">tjakoen</a>");
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test("both forms carry identical content (one source of truth)", () => {
|
|
23
|
+
const inner = (s: string) => s.replace(/^<[^>]+>/, "").replace(/<\/[a-z]+>$/, "");
|
|
24
|
+
expect(inner(madeWith())).toBe(inner(madeWith({ inline: true })));
|
|
25
|
+
});
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
// grain/scripts/notepad.js — the NOTEPAD island (organisms/notepad; DEMO-PLAN piece 2).
|
|
2
|
+
//
|
|
3
|
+
// A rail pane that is the AI's memory made VISIBLE and EDITABLE. It adds three behaviours to a
|
|
4
|
+
// composed `.notepad` pane, no markup to write beyond the organism:
|
|
5
|
+
// 1. a source⇄rendered toggle (flips `data-mode`);
|
|
6
|
+
// 2. persistence — the markdown SOURCE is the canonical state, mirrored to localStorage
|
|
7
|
+
// (`grain.notepad`), so an AI note or a human commit survives MPA navigation + reload;
|
|
8
|
+
// 3. the human's Commit is already a normal `[data-action="note.replace"]` trigger through the
|
|
9
|
+
// ONE door (the dispatcher fires it, reading the textarea via `data-from`) — this island only
|
|
10
|
+
// OWNS the persistence + toggle, never a parallel write path.
|
|
11
|
+
//
|
|
12
|
+
// The DOM is a PROJECTION; the source is the truth. Each `.notepad__entry` carries its markdown in
|
|
13
|
+
// `data-md` (written by `note.*` ops / `notepadEntry`), so the whole pad's source is just the entries'
|
|
14
|
+
// `data-md` joined — the island watches the pane and re-derives it whenever an op lands (an AI append,
|
|
15
|
+
// or the human's committed replace). It watches the whole `.notepad` (subtree), NOT the body node,
|
|
16
|
+
// because a `note.replace` swaps the body's outerHTML — an observer bound to the body would go deaf.
|
|
17
|
+
//
|
|
18
|
+
// renderMarkdown + notepadEntry are imported from grain's OWN client-safe kit over the module server
|
|
19
|
+
// (§19.2) — the SAME renderer the chat settles with and the SAME entry markup the reasoner emits, so
|
|
20
|
+
// there is ONE markdown renderer and ONE entry shape on the page, never a drifting browser copy.
|
|
21
|
+
//
|
|
22
|
+
// OPT-IN: presence of a `.notepad` pane is the opt-in; drop the organism + one <script type="module">
|
|
23
|
+
// tag and it wires itself. Persona-neutral — "the AI", never any product's desk.
|
|
24
|
+
(() => {
|
|
25
|
+
"use strict";
|
|
26
|
+
if (window.grain && window.grain.notepad) return; // idempotent
|
|
27
|
+
|
|
28
|
+
const assetBase = new URL("..", import.meta.url).pathname.replace(/\/$/, "");
|
|
29
|
+
const KEY = "grain.notepad"; // canonical committed markdown source
|
|
30
|
+
const DRAFT_KEY = "grain.notepad.draft"; // the human's uncommitted textarea text (kept across reload)
|
|
31
|
+
|
|
32
|
+
const read = (k) => { try { return localStorage.getItem(k); } catch { return null; } };
|
|
33
|
+
const write = (k, v) => { try { v == null ? localStorage.removeItem(k) : localStorage.setItem(k, v); } catch { /* private mode */ } };
|
|
34
|
+
|
|
35
|
+
// Join every entry's markdown source, in order — the whole pad's canonical markdown.
|
|
36
|
+
const deriveSource = (body) =>
|
|
37
|
+
[...body.querySelectorAll(".notepad__entry")]
|
|
38
|
+
.map((e) => e.getAttribute("data-md") || "")
|
|
39
|
+
.filter((s) => s.trim().length)
|
|
40
|
+
.join("\n\n");
|
|
41
|
+
|
|
42
|
+
// Lazy module load (shared across panes): the kit is client-safe, served by the module server.
|
|
43
|
+
let kitPromise = null;
|
|
44
|
+
const kit = () => (kitPromise ??= import(assetBase + "/modules/grain/ai/reasoner-kit.js").catch(() => null));
|
|
45
|
+
|
|
46
|
+
async function wire(pane) {
|
|
47
|
+
if (pane.dataset.notepadWired) return;
|
|
48
|
+
pane.dataset.notepadWired = "1";
|
|
49
|
+
|
|
50
|
+
const body = () => pane.querySelector(".notepad__body");
|
|
51
|
+
const src = pane.querySelector(".notepad__source");
|
|
52
|
+
const toggle = pane.querySelector(".notepad__toggle");
|
|
53
|
+
const commit = pane.querySelector(".notepad__commit");
|
|
54
|
+
|
|
55
|
+
// ── toggle: flip source⇄rendered. Entering source shows the draft (or the canonical source). ──
|
|
56
|
+
if (toggle) toggle.addEventListener("click", () => {
|
|
57
|
+
const toSource = pane.getAttribute("data-mode") !== "source";
|
|
58
|
+
pane.setAttribute("data-mode", toSource ? "source" : "rendered");
|
|
59
|
+
if (toSource && src) { src.value = read(DRAFT_KEY) ?? read(KEY) ?? ""; src.focus(); }
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
// ── the human's working draft persists as they type (committing is the door's job) ──
|
|
63
|
+
if (src) src.addEventListener("input", () => write(DRAFT_KEY, src.value));
|
|
64
|
+
// Commit itself flows through the ONE door (the dispatcher fires the [data-action="note.replace"]
|
|
65
|
+
// and empties the field). We only retire the local draft here — once committed it's canonical, and
|
|
66
|
+
// sync (below) reflects that canonical straight back into the now-empty textarea.
|
|
67
|
+
if (commit) commit.addEventListener("click", () => write(DRAFT_KEY, null));
|
|
68
|
+
|
|
69
|
+
// ── restore persisted state: if the server rendered nothing but we have a saved pad, render it ──
|
|
70
|
+
const b0 = body();
|
|
71
|
+
const saved = read(KEY);
|
|
72
|
+
if (b0 && !b0.querySelector(".notepad__entry") && saved && saved.trim()) {
|
|
73
|
+
const m = await kit();
|
|
74
|
+
// Restored content is settled history — render it clean (grade is a LIVE provenance signal,
|
|
75
|
+
// not persisted; the SOURCE is what we keep). Falls back to no-op if the kit can't load.
|
|
76
|
+
if (m) b0.innerHTML = m.notepadEntry(saved, "user");
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// ── mirror the canonical source to localStorage whenever an op lands (append / replace) ──
|
|
80
|
+
const sync = () => {
|
|
81
|
+
const b = body();
|
|
82
|
+
if (!b) return;
|
|
83
|
+
const md = deriveSource(b);
|
|
84
|
+
write(KEY, md);
|
|
85
|
+
// Reflect the latest committed source into the textarea while the human isn't editing it and
|
|
86
|
+
// has no uncommitted draft (an AI append or a fresh commit lands here; a live draft is left be).
|
|
87
|
+
if (src && document.activeElement !== src && !read(DRAFT_KEY)) src.value = md;
|
|
88
|
+
};
|
|
89
|
+
new MutationObserver(sync).observe(pane, { childList: true, subtree: true });
|
|
90
|
+
sync(); // seed from whatever is present now (SSR entries or the just-restored ones)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const wireAll = () => document.querySelectorAll(".notepad").forEach(wire);
|
|
94
|
+
|
|
95
|
+
window.grain = window.grain || {};
|
|
96
|
+
window.grain.notepad = { wire, wireAll };
|
|
97
|
+
|
|
98
|
+
if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", wireAll);
|
|
99
|
+
else wireAll();
|
|
100
|
+
})();
|