Public: anyone can read this; the crew can change it.
Studio Design keeps to the page rules: the live theme is a shared behavior, the empty filter is in the table #16378
Check · Review · Land · Live done — Landed and live in production.
fix/studio-design-rules → main 0 commits 2 threads · 2 unresolved
6 files
changed +76
−9
src/core/empty_words.ts +1
| @@ -30,6 +30,7 @@ const TABLE = { | ||
| 30 | 30 | preview: { sentence: "No merged preview in this file." }, |
| 31 | 31 | results: { sentence: "No results." }, |
| 32 | 32 | components: { sentence: "No components." }, |
| 33 | componentMatch: { sentence: "No component matches.", reason: "Try another word." }, | |
| 33 | 34 | dependencies: { sentence: "No dependencies listed." }, |
| 34 | 35 | people: { sentence: "Nobody has joined yet.", label: "Invite someone", href: "/admin/limits" }, |
| 35 | 36 | // Lists whose place exists only inside a page that already offers the next step (a form just |
src/web/behaviors.ts +15
| @@ -7,6 +7,9 @@ | ||
| 7 | 7 | * |
| 8 | 8 | * data-disclose="attr" a button that sets or clears `attr` on its closest [data-disclose-scope] |
| 9 | 9 | * (labels in data-label-on / data-label-off; aria-expanded is kept) |
| 10 | * data-live-css="stream" data-live-href="/x.css" | |
| 11 | * the page's stylesheet linked at /x.css is fetched again, as /x.css?v=N, | |
| 12 | * each time the event stream sends a `theme` event with a new version | |
| 10 | 13 | */ |
| 11 | 14 | export const BEHAVIORS_BUDGET = 2 * 1024; |
| 12 | 15 | |
| @@ -35,5 +38,17 @@ export const behaviorsJs = /* js */ ` | ||
| 35 | 38 | }); |
| 36 | 39 | d.addEventListener('forge:swapped', disclose); |
| 37 | 40 | disclose(); |
| 41 | Array.prototype.forEach.call(d.querySelectorAll('[data-live-css]'), function (m) { | |
| 42 | var base = m.getAttribute('data-live-href'); | |
| 43 | var l = base && d.querySelector('link[href^="' + base + '"]'); | |
| 44 | if (!l || typeof EventSource === 'undefined') return; | |
| 45 | var seen = null; | |
| 46 | new EventSource(m.getAttribute('data-live-css')).addEventListener('theme', function (e) { | |
| 47 | var v; | |
| 48 | try { v = JSON.parse(e.data).version; } catch (x) { return; } | |
| 49 | if (seen !== null && v !== seen) l.setAttribute('href', base + '?v=' + v); | |
| 50 | seen = v; | |
| 51 | }); | |
| 52 | }); | |
| 38 | 53 | })(); |
| 39 | 54 | `; |
src/web/empty_words.ts +1
| @@ -41,6 +41,7 @@ const ICONS = { | ||
| 41 | 41 | preview: "search", |
| 42 | 42 | results: "check", |
| 43 | 43 | components: "search", |
| 44 | componentMatch: "search", | |
| 44 | 45 | dependencies: "search", |
| 45 | 46 | releases: "activity", |
| 46 | 47 | appReleases: "tag", |
src/web/studio/design.ts +7 −9
| @@ -20,7 +20,8 @@ import { notFoundPage, pageHead, signInFirst } from "../chrome.ts"; | ||
| 20 | 20 | import { readForm } from "../form.ts"; |
| 21 | 21 | import { html, type Html } from "../html.ts"; |
| 22 | 22 | import { page, redirect } from "../layout.ts"; |
| 23 | import { emptyLine } from "../empty_words.ts"; | |
| 23 | import { emptyLine, emptyList } from "../empty_words.ts"; | |
| 24 | import { behaviorsJs } from "../behaviors.ts"; | |
| 24 | 25 | import { badge, btn, card, dot, empty, field, kbd, listCard, meter, pill, row, stat, tabs, timeAgo } from "../ui.ts"; |
| 25 | 26 | import { homeTabs } from "./home_tabs.ts"; |
| 26 | 27 | |
| @@ -297,13 +298,10 @@ const DESIGN_CSS = `.design-roles{margin:0 0 calc(var(--theme-space,4px)*4)} | ||
| 297 | 298 | `; |
| 298 | 299 | |
| 299 | 300 | /** |
| 300 | * On this page only: a publish is pushed here and the stylesheet is fetched again, so the page | |
| 301 | * changes without a reload. | |
| 301 | * The live theme (`data-live-css`, a shared behavior): each publish's version comes down the | |
outdated
This 'On this page only…' comment describes the script that was removed. Delete it. The comment below it is the one that applies to LIVE_CSS. |
||
| 302 | * stream and the page links the theme again, so this page shows a publish at once. | |
| 302 | 303 | */ |
| 303 | const LIVE_JS = `(function(){var l=document.querySelector('link[href^="/design/theme.css"]');if(!l||typeof EventSource==='undefined')return; | |
| 304 | var seen=null,s=new EventSource('/design/theme/forge/live'); | |
| 305 | s.addEventListener('theme',function(e){var v;try{v=JSON.parse(e.data).version}catch(x){return} | |
| 306 | if(seen!==null&&v!==seen)l.setAttribute('href','/design/theme.css?v='+v);seen=v})})();`; | |
| 304 | const LIVE_CSS = html`<span hidden data-live-css="/design/theme/forge/live" data-live-href="/design/theme.css"></span>`; | |
| 307 | 305 | |
| 308 | 306 | const canEdit = (ctx: Ctx): boolean => ctx.auth.user?.isAdmin === true; |
| 309 | 307 | |
| @@ -440,7 +438,7 @@ export function designPage(ctx: Ctx): Response { | ||
| 440 | 438 | { id: "design-history" }, |
| 441 | 439 | )}`}`; |
| 442 | 440 | notePhase(ctx.request, "render", performance.now() - began); |
| 443 | return page({ ctx, title: "Design", body, styleCss, scriptJs: LIVE_JS }); | |
| 441 | return page({ ctx, title: "Design", body: html`${body}${LIVE_CSS}`, styleCss, scriptJs: behaviorsJs }); | |
| 444 | 442 | } |
| 445 | 443 | |
| 446 | 444 | /** |
| @@ -586,7 +584,7 @@ export function componentsPage(ctx: Ctx): Response { | ||
| 586 | 584 | ${btn("Filter", { variant: "secondary" })} |
| 587 | 585 | </form> |
| 588 | 586 | ${shown.length === 0 |
| 589 | ? empty({ title: "No component matches", note: "Try another word." }) | |
| 587 | ? emptyList("componentMatch") | |
| 590 | 588 | : shown.map( |
| 591 | 589 | (e) => html`<section id="c-${e.id}"> |
| 592 | 590 | <h2>${e.name}</h2><p class="muted small">${e.note}</p> |
test/behaviors.test.ts +43
| @@ -43,3 +43,46 @@ test("a button with no scope does nothing", () => { | ||
| 43 | 43 | (window.document.querySelector("button") as unknown as HTMLElement).click(); |
| 44 | 44 | expect(window.document.body.hasAttribute("data-open")).toBe(false); |
| 45 | 45 | }); |
| 46 | ||
| 47 | /** | |
| 48 | * A page with the live-stylesheet marker, its EventSource stubbed: the streams it opened, by URL. | |
| 49 | */ | |
| 50 | function live(body: string) { | |
| 51 | const window = new Window({ url: "https://forge.test/" }); | |
| 52 | const streams: { url: string; emit: (data: string) => void }[] = []; | |
| 53 | class StubSource { | |
| 54 | private readonly heard: ((e: { data: string }) => void)[] = []; | |
| 55 | constructor(readonly url: string) { | |
| 56 | streams.push({ url, emit: (data) => this.heard.forEach((f) => f({ data })) }); | |
| 57 | } | |
| 58 | addEventListener(kind: string, f: (e: { data: string }) => void) { | |
| 59 | if (kind === "theme") this.heard.push(f); | |
| 60 | } | |
| 61 | } | |
| 62 | (window as unknown as { EventSource: unknown }).EventSource = StubSource; | |
| 63 | window.document.body.innerHTML = body; | |
| 64 | window.eval(behaviorsJs); | |
| 65 | return { window, streams, href: () => window.document.querySelector("link")?.getAttribute("href") }; | |
| 66 | } | |
| 67 | ||
| 68 | test("a live stylesheet takes each new version from its stream: the first says what is linked, a later one swaps `?v=`", () => { | |
| 69 | const page = live(`<link rel="stylesheet" href="/x.css"><span hidden data-live-css="/x/live" data-live-href="/x.css"></span>`); | |
| 70 | expect(page.streams.map((s) => s.url)).toEqual(["/x/live"]); | |
| 71 | const stream = page.streams[0]!; | |
| 72 | stream.emit(JSON.stringify({ version: 3 })); | |
| 73 | expect(page.href()).toBe("/x.css"); | |
| 74 | stream.emit(JSON.stringify({ version: 3 })); | |
| 75 | expect(page.href()).toBe("/x.css"); | |
| 76 | stream.emit("not json"); | |
| 77 | expect(page.href()).toBe("/x.css"); | |
| 78 | stream.emit(JSON.stringify({ version: 4 })); | |
| 79 | expect(page.href()).toBe("/x.css?v=4"); | |
| 80 | // The swapped link still starts with the base, so the next version replaces it again. | |
| 81 | stream.emit(JSON.stringify({ version: 5 })); | |
| 82 | expect(page.href()).toBe("/x.css?v=5"); | |
| 83 | }); | |
| 84 | ||
| 85 | test("a live marker with no matching stylesheet opens no stream", () => { | |
| 86 | expect(live(`<link rel="stylesheet" href="/other.css"><span hidden data-live-css="/x/live" data-live-href="/x.css"></span>`).streams).toEqual([]); | |
| 87 | expect(live(`<link rel="stylesheet" href="/x.css">`).streams).toEqual([]); | |
| 88 | }); |
test/design-theme.test.ts +9
| @@ -137,6 +137,15 @@ describe("design tab over http", () => { | ||
| 137 | 137 | expect(res.status).toBe(200); |
| 138 | 138 | expect(text).toContain("Blocked"); |
| 139 | 139 | expect(text).toContain("pv-new-dark"); |
| 140 | // The live theme is the shared behavior, not a script of the page's own (#13783). | |
| 141 | expect(text).toContain('data-live-css="/design/theme/forge/live"'); | |
| 142 | expect(text).toContain('data-live-href="/design/theme.css"'); | |
| 143 | }); | |
| 144 | ||
| 145 | test("a filter that matches nothing says so in the table's words", async () => { | |
| 146 | const text = await (await fetch(`${base}/design/components?q=zzzznothing`, { headers: { cookie: admin } })).text(); | |
| 147 | expect(text).toContain("No component matches."); | |
| 148 | expect(text).toContain("Try another word."); | |
| 140 | 149 | }); |
| 141 | 150 | |
| 142 | 151 | const rows = () => themeHistory(app.db, FORGE_THEME.org, FORGE_THEME.name, 100).length; |
new above
offline
Needs a happy-dom test in test/behaviors.test.ts (stub EventSource, check the first event leaves the href alone, a new version sets ?v=N, bad JSON is ignored). Without it, deleting this block fails nothing.