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

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

fix/studio-design-rules → main 0 commits 2 threads · 2 unresolved

  • Merged. fix/studio-design-rules was merged into main as c25dd51.

Review

Sign in to review this change.
6 files changed +76 −9
src/core/empty_words.ts +1
@@ -30,6 +30,7 @@ const TABLE = {
3030 preview: { sentence: "No merged preview in this file." },
3131 results: { sentence: "No results." },
3232 components: { sentence: "No components." },
33 componentMatch: { sentence: "No component matches.", reason: "Try another word." },
3334 dependencies: { sentence: "No dependencies listed." },
3435 people: { sentence: "Nobody has joined yet.", label: "Invite someone", href: "/admin/limits" },
3536 // 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 @@
77 *
88 * data-disclose="attr" a button that sets or clears `attr` on its closest [data-disclose-scope]
99 * (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
1013 */
1114 export const BEHAVIORS_BUDGET = 2 * 1024;
1215
@@ -35,5 +38,17 @@ export const behaviorsJs = /* js */ `
3538 });
3639 d.addEventListener('forge:swapped', disclose);
3740 disclose();
41 Array.prototype.forEach.call(d.querySelectorAll('[data-live-css]'), function (m) {

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.

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 });
3853 })();
3954 `;
src/web/empty_words.ts +1
@@ -41,6 +41,7 @@ const ICONS = {
4141 preview: "search",
4242 results: "check",
4343 components: "search",
44 componentMatch: "search",
4445 dependencies: "search",
4546 releases: "activity",
4647 appReleases: "tag",
src/web/studio/design.ts +7 −9
@@ -20,7 +20,8 @@ import { notFoundPage, pageHead, signInFirst } from "../chrome.ts";
2020 import { readForm } from "../form.ts";
2121 import { html, type Html } from "../html.ts";
2222 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";
2425 import { badge, btn, card, dot, empty, field, kbd, listCard, meter, pill, row, stat, tabs, timeAgo } from "../ui.ts";
2526 import { homeTabs } from "./home_tabs.ts";
2627
@@ -297,13 +298,10 @@ const DESIGN_CSS = `.design-roles{margin:0 0 calc(var(--theme-space,4px)*4)}
297298 `;
298299
299300 /**
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.
302303 */
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>`;
307305
308306 const canEdit = (ctx: Ctx): boolean => ctx.auth.user?.isAdmin === true;
309307
@@ -440,7 +438,7 @@ export function designPage(ctx: Ctx): Response {
440438 { id: "design-history" },
441439 )}`}`;
442440 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 });
444442 }
445443
446444 /**
@@ -586,7 +584,7 @@ export function componentsPage(ctx: Ctx): Response {
586584 ${btn("Filter", { variant: "secondary" })}
587585 </form>
588586 ${shown.length === 0
589 ? empty({ title: "No component matches", note: "Try another word." })
587 ? emptyList("componentMatch")
590588 : shown.map(
591589 (e) => html`<section id="c-${e.id}">
592590 <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", () => {
4343 (window.document.querySelector("button") as unknown as HTMLElement).click();
4444 expect(window.document.body.hasAttribute("data-open")).toBe(false);
4545 });
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", () => {
137137 expect(res.status).toBe(200);
138138 expect(text).toContain("Blocked");
139139 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.");
140149 });
141150
142151 const rows = () => themeHistory(app.db, FORGE_THEME.org, FORGE_THEME.name, 100).length;

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs