diff --git a/.changes/planning/fix-17991-copy-chosen-design-2f5881f0.md b/.changes/planning/fix-17991-copy-chosen-design-2f5881f0.md new file mode 100644 index 0000000000..5076e1748b --- /dev/null +++ b/.changes/planning/fix-17991-copy-chosen-design-2f5881f0.md @@ -0,0 +1,7 @@ +--- +bump: patch +--- + +### Changed + +- Shared `proto-pin.js` synced where this plugin carries it: the frame helper answers a host page's copy-out ask with the page's copy-out text; no other change to this plugin. diff --git a/.changes/prototype/fix-17991-copy-chosen-design-694e6c08.md b/.changes/prototype/fix-17991-copy-chosen-design-694e6c08.md new file mode 100644 index 0000000000..40cf01c519 --- /dev/null +++ b/.changes/prototype/fix-17991-copy-chosen-design-694e6c08.md @@ -0,0 +1,7 @@ +--- +bump: minor +--- + +### Added + +- The notes host page's Copy menu has Copy chosen design: it copies the text the mockup's own copy-out terminator gives, which a mockup builds in `window.uiFeedbackCopyOut`. Framed by the host page, the mockup's data bar, marked `data-harness-chrome`, hides, and the bar's Sample data menu presses its controls; opened alone from `file://` the page keeps its own bar. diff --git a/docs/contracts/ui-feedback/README.md b/docs/contracts/ui-feedback/README.md index 430c0741e7..08ed8dd8c0 100644 --- a/docs/contracts/ui-feedback/README.md +++ b/docs/contracts/ui-feedback/README.md @@ -105,6 +105,14 @@ validator checks every note, takes all or none, skips a note already in the log, imported notes toward `x-maxNotesPerRun`, and refuses while a server runs on the data dir. The block is data like any note; it is never run as text. +Copy chosen design copies the mockup's own copy-out terminator text. A page whose terminator text +comes from a `window.uiFeedbackCopyOut()` function returning a string says so in the ready message +(`copyOut: true`). The host page then asks with `{type: "copy-out"}`, and the frame helper answers +`{type: "chosen", text}`. The host page keeps one reply per ask, and only when `chosenFrom` +(`frame-channel.mjs`) admits it: a non-empty string of at most 8000 code points, kept whole, with +no `<` before a letter, `!`, `/` or `?`, and no control character but tab and line breaks. It sets +the text as a textarea value and copies it; it never renders it as markup. + ## Controls `controls.schema.json` defines the controls manifest: the one list of what can vary on the framed diff --git a/lib/ui-feedback/frame-channel.mjs b/lib/ui-feedback/frame-channel.mjs index 366eb90be2..a1b395a1dd 100644 --- a/lib/ui-feedback/frame-channel.mjs +++ b/lib/ui-feedback/frame-channel.mjs @@ -48,6 +48,21 @@ export function namesFrom(m) { return out; } +/** The most code points a chosen design keeps: two note texts (note.schema.json's text maxLength is 4000). */ +const CHOSEN_MAX = 8000; +const MARKUP = /<[A-Za-z!/?]/; +const CONTROL = /[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f]/; + +/** + * The text of the frame's chosen-design reply (the mockup's copy-out terminator text), whole and as sent, or null when + * the message is not a `chosen` one, its text is not a non-empty string, runs past 8000 code points, or holds a tag, + * comment or processing instruction, or a control character other than tab and line breaks. + */ +export function chosenFrom(m) { + const t = m?.type === "chosen" && typeof m.text === "string" ? m.text : ""; + return t.trim() && [...t].length <= CHOSEN_MAX && !MARKUP.test(t) && !CONTROL.test(t) ? t : null; +} + /** * Calls `onBlocked` unless the returned function is called first, within `ms`. On a frame a CSP * blocks, Firefox fires no load event and Chrome fires one diff --git a/lib/ui-feedback/frame-channel.test.mjs b/lib/ui-feedback/frame-channel.test.mjs index 59fca93e34..febc6fbd08 100644 --- a/lib/ui-feedback/frame-channel.test.mjs +++ b/lib/ui-feedback/frame-channel.test.mjs @@ -1,7 +1,7 @@ import { strict as assert } from "node:assert"; import { readFileSync } from "node:fs"; import { test } from "node:test"; -import { awaitReady, frameTarget, fromFrame, namesFrom, READY_MS, viewFrom } from "./frame-channel.mjs"; +import { awaitReady, chosenFrom, frameTarget, fromFrame, namesFrom, READY_MS, viewFrom } from "./frame-channel.mjs"; const DEV = "http://localhost:5173"; const HOST = readFileSync(new URL("./host.html", import.meta.url), "utf8"); @@ -149,3 +149,26 @@ test("the frame's names for listed notes keep positive integer keys and string p ]); assert.deepEqual([...namesFrom({ names: "x" })], []); }); + +const CHOSEN = "direction: B Cards\nnotes: keep the status pill; a < b stays text\ntest-ids: B: shipments-row, shipments-title\n"; + +test("the frame's chosen design is kept whole, its lines and spacing as sent", () => { + assert.equal(chosenFrom({ type: "chosen", text: CHOSEN }), CHOSEN); +}); + +test("a chosen design of 8000 code points is kept and one of 8001 is refused, never cut", () => { + assert.equal(chosenFrom({ type: "chosen", text: "😀".repeat(8000) }), "😀".repeat(8000)); + assert.equal(chosenFrom({ type: "chosen", text: "😀".repeat(8001) }), null); +}); + +test("a chosen-design message of another type, or whose text is not a non-empty string, is refused", () => { + for (const m of [{ type: "named", text: CHOSEN }, { text: CHOSEN }, { type: "chosen", text: 7 }, { type: "chosen", text: ["a"] }, { type: "chosen" }, { type: "chosen", text: " \n " }, null, "chosen"]) { + assert.equal(chosenFrom(m), null, JSON.stringify(m)); + } +}); + +test("a chosen design holding markup or a control character is refused", () => { + for (const text of ['notes: ', "ab", "", "", "", "bell\u0007", "nul\u0000"]) { + assert.equal(chosenFrom({ type: "chosen", text }), null, JSON.stringify(text)); + } +}); diff --git a/lib/ui-feedback/host.html b/lib/ui-feedback/host.html index c3f6a08159..5677eb8059 100644 --- a/lib/ui-feedback/host.html +++ b/lib/ui-feedback/host.html @@ -234,6 +234,7 @@ Copy diff --git a/lib/ui-feedback/host.mjs b/lib/ui-feedback/host.mjs index a827d2bdf5..0a10f92604 100644 --- a/lib/ui-feedback/host.mjs +++ b/lib/ui-feedback/host.mjs @@ -1,5 +1,5 @@ import { draftStore, problems, readerName, use } from "/note-model.mjs"; -import { awaitReady, frameTarget, fromFrame, namesFrom, READY_MS, viewFrom } from "/frame-channel.mjs"; +import { awaitReady, chosenFrom, frameTarget, fromFrame, namesFrom, READY_MS, viewFrom } from "/frame-channel.mjs"; import { edges } from "/bar-edges.mjs"; const $ = (id) => document.getElementById(id); @@ -44,6 +44,7 @@ function takeControls(m) { : m.controls === false ? "The mockup's controls manifest is not JSON, so only its built-in controls are here." : m.controls == null ? "The mockup has no controls manifest." : `${controls.length} control(s) from the mockup's manifest.`; for (const b of [$("copy-config"), $("reset-controls"), $("copy-values")]) b.disabled = !controls.length; + $("copy-chosen").disabled = m.copyOut !== true; renderControls(); renderSteps(); } @@ -551,6 +552,30 @@ function copyAll() { else manual(); } +/** The mockup's copy-out terminator text, asked of the frame; only the one reply to an ask is copied, as text. */ +let wantChosen = false; +function askChosen() { + closeMenus(); + $("copy-button").focus(); + wantChosen = true; + toFrame({ type: "copy-out" }); +} +function copyChosen(m) { + wantChosen = false; + if ($("panel").hidden) setPanel(true); + selectTab(tabs[0], false); + if (typeof m.text !== "string") return say("The mockup's copy-out function gave nothing, so nothing was copied; see the mockup's console."); + const text = chosenFrom(m); + if (text === null) return say("The mockup's chosen design is not plain text of at most 8000 characters, so nothing was copied."); + $("block").value = text; + $("copyout").hidden = false; + $("block").select(); + const done = () => say("Copied the chosen design. Paste it to Claude."); + const manual = () => say("Select the text below and copy it, then paste it to Claude."); + if (navigator.clipboard) navigator.clipboard.writeText(text).then(done, manual); + else manual(); +} + function render() { const listening = noSession ? "No session: the notes server is not answering. Your notes stay on this page; Copy all notes makes a block to paste to Claude." @@ -682,6 +707,7 @@ addEventListener("message", (e) => { else if (m.type === "shown" && m.found === false && chosen) say("That note's place is not on the mockup now, so no box is drawn."); else if (m.type === "content" && fits(m.name, schema.properties.content)) { content = m.name; renderControls(); } else if (m.type === "want-text") askText(); + else if (m.type === "chosen" && wantChosen) copyChosen(m); else if (m.type === "key" && FRAME_KEYS.has(m.key)) shortcut(m.key); else if (m.type === "pinned" && picking === "element") { const note = noteFrom(m, "element"); @@ -719,6 +745,7 @@ $("dock").addEventListener("click", () => setPanel($("panel").hidden)); $("markers").addEventListener("click", () => setMarkers(!markersShown)); $("cancel").addEventListener("click", closeComposer); $("copy").addEventListener("click", copyAll); +$("copy-chosen").addEventListener("click", askChosen); $("copy-config").addEventListener("click", copyConfig); $("copy-values").addEventListener("click", () => { closeMenus(); $("copy-button").focus(); if ($("panel").hidden) setPanel(true); selectTab(tabs[1], false); copyConfig(); }); $("side").addEventListener("click", () => setSide($("panel").dataset.side === "start" ? "end" : "start")); @@ -794,7 +821,7 @@ try { if (loads > 1) { ready = false; picking = null; - for (const b of [...Object.values(buttons), $("quote"), $("controls-fields"), $("copy-config"), $("reset-controls"), $("copy-values"), $("vprev"), $("vnext")]) b.disabled = true; + for (const b of [...Object.values(buttons), $("quote"), $("controls-fields"), $("copy-config"), $("reset-controls"), $("copy-values"), $("copy-chosen"), $("vprev"), $("vnext")]) b.disabled = true; for (const b of $("data-list").children) b.disabled = true; say("The mockup loaded another page in its frame, so this page stopped listening to it. Reload to start again."); $("controls-status").textContent = "The mockup loaded another page in its frame, so these controls no longer reach it. Reload to start again."; diff --git a/lib/ui-feedback/proto-pin.js b/lib/ui-feedback/proto-pin.js index 25553e2e7e..89188a5e21 100644 --- a/lib/ui-feedback/proto-pin.js +++ b/lib/ui-feedback/proto-pin.js @@ -633,6 +633,7 @@ else if (m.type === "name") names(m.targets); else if (m.type === "set") setControls(m.values); else if (m.type === "press" && !mode) replay([m.step]); + else if (m.type === "copy-out") post({ type: "chosen", text: copyOut() }); else if (m.type === "focus") { const p = pins.find(x => x.n === m.n); if (!p) return; @@ -641,9 +642,20 @@ else scrollTo({ top: Math.max(0, at.y - innerHeight / 3), behavior }); } }); + /** The page's copy-out terminator text, from the function its own terminator calls; null when it has none. */ + function copyOut() { + try { + const t = typeof window.uiFeedbackCopyOut === "function" ? window.uiFeedbackCopyOut() : null; + return typeof t === "string" ? t : null; + } catch (err) { + console.error("ui-feedback: the page's uiFeedbackCopyOut threw", err); + return null; + } + } const marks = [...document.querySelectorAll("[data-data-control]")].map(stepOf).filter(Boolean); post({ type: "ready", + copyOut: typeof window.uiFeedbackCopyOut === "function", controls: window.uiFeedbackControls?.manifest() ?? null, data: [...new Map(marks.map(m => [JSON.stringify(m), m])).values()], content: document.querySelector('[data-content-set="demo"]') && document.querySelector('[data-content-set="worst"]') ? root.dataset.vsContent || null : null, diff --git a/plugins/planning/shared/web/proto-pin.js b/plugins/planning/shared/web/proto-pin.js index a230a91069..7b9b3c9e54 100644 --- a/plugins/planning/shared/web/proto-pin.js +++ b/plugins/planning/shared/web/proto-pin.js @@ -635,6 +635,7 @@ else if (m.type === "name") names(m.targets); else if (m.type === "set") setControls(m.values); else if (m.type === "press" && !mode) replay([m.step]); + else if (m.type === "copy-out") post({ type: "chosen", text: copyOut() }); else if (m.type === "focus") { const p = pins.find(x => x.n === m.n); if (!p) return; @@ -643,9 +644,20 @@ else scrollTo({ top: Math.max(0, at.y - innerHeight / 3), behavior }); } }); + /** The page's copy-out terminator text, from the function its own terminator calls; null when it has none. */ + function copyOut() { + try { + const t = typeof window.uiFeedbackCopyOut === "function" ? window.uiFeedbackCopyOut() : null; + return typeof t === "string" ? t : null; + } catch (err) { + console.error("ui-feedback: the page's uiFeedbackCopyOut threw", err); + return null; + } + } const marks = [...document.querySelectorAll("[data-data-control]")].map(stepOf).filter(Boolean); post({ type: "ready", + copyOut: typeof window.uiFeedbackCopyOut === "function", controls: window.uiFeedbackControls?.manifest() ?? null, data: [...new Map(marks.map(m => [JSON.stringify(m), m])).values()], content: document.querySelector('[data-content-set="demo"]') && document.querySelector('[data-content-set="worst"]') ? root.dataset.vsContent || null : null, diff --git a/plugins/prototype/notes-bridge/frame-channel.mjs b/plugins/prototype/notes-bridge/frame-channel.mjs index 06e7f4c324..da470892ab 100644 --- a/plugins/prototype/notes-bridge/frame-channel.mjs +++ b/plugins/prototype/notes-bridge/frame-channel.mjs @@ -50,6 +50,21 @@ export function namesFrom(m) { return out; } +/** The most code points a chosen design keeps: two note texts (note.schema.json's text maxLength is 4000). */ +const CHOSEN_MAX = 8000; +const MARKUP = /<[A-Za-z!/?]/; +const CONTROL = /[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f]/; + +/** + * The text of the frame's chosen-design reply (the mockup's copy-out terminator text), whole and as sent, or null when + * the message is not a `chosen` one, its text is not a non-empty string, runs past 8000 code points, or holds a tag, + * comment or processing instruction, or a control character other than tab and line breaks. + */ +export function chosenFrom(m) { + const t = m?.type === "chosen" && typeof m.text === "string" ? m.text : ""; + return t.trim() && [...t].length <= CHOSEN_MAX && !MARKUP.test(t) && !CONTROL.test(t) ? t : null; +} + /** * Calls `onBlocked` unless the returned function is called first, within `ms`. On a frame a CSP * blocks, Firefox fires no load event and Chrome fires one diff --git a/plugins/prototype/notes-bridge/host.html b/plugins/prototype/notes-bridge/host.html index af3fa87790..ab9432c20c 100644 --- a/plugins/prototype/notes-bridge/host.html +++ b/plugins/prototype/notes-bridge/host.html @@ -236,6 +236,7 @@ diff --git a/plugins/prototype/notes-bridge/host.mjs b/plugins/prototype/notes-bridge/host.mjs index d61f216804..56c90527ec 100644 --- a/plugins/prototype/notes-bridge/host.mjs +++ b/plugins/prototype/notes-bridge/host.mjs @@ -1,7 +1,7 @@ // GENERATED from lib/ui-feedback/host.mjs by scripts/sync-shared-copies.sh. Do not edit this copy: // edit the canonical source, then rerun the script. import { draftStore, problems, readerName, use } from "/note-model.mjs"; -import { awaitReady, frameTarget, fromFrame, namesFrom, READY_MS, viewFrom } from "/frame-channel.mjs"; +import { awaitReady, chosenFrom, frameTarget, fromFrame, namesFrom, READY_MS, viewFrom } from "/frame-channel.mjs"; import { edges } from "/bar-edges.mjs"; const $ = (id) => document.getElementById(id); @@ -46,6 +46,7 @@ function takeControls(m) { : m.controls === false ? "The mockup's controls manifest is not JSON, so only its built-in controls are here." : m.controls == null ? "The mockup has no controls manifest." : `${controls.length} control(s) from the mockup's manifest.`; for (const b of [$("copy-config"), $("reset-controls"), $("copy-values")]) b.disabled = !controls.length; + $("copy-chosen").disabled = m.copyOut !== true; renderControls(); renderSteps(); } @@ -553,6 +554,30 @@ function copyAll() { else manual(); } +/** The mockup's copy-out terminator text, asked of the frame; only the one reply to an ask is copied, as text. */ +let wantChosen = false; +function askChosen() { + closeMenus(); + $("copy-button").focus(); + wantChosen = true; + toFrame({ type: "copy-out" }); +} +function copyChosen(m) { + wantChosen = false; + if ($("panel").hidden) setPanel(true); + selectTab(tabs[0], false); + if (typeof m.text !== "string") return say("The mockup's copy-out function gave nothing, so nothing was copied; see the mockup's console."); + const text = chosenFrom(m); + if (text === null) return say("The mockup's chosen design is not plain text of at most 8000 characters, so nothing was copied."); + $("block").value = text; + $("copyout").hidden = false; + $("block").select(); + const done = () => say("Copied the chosen design. Paste it to Claude."); + const manual = () => say("Select the text below and copy it, then paste it to Claude."); + if (navigator.clipboard) navigator.clipboard.writeText(text).then(done, manual); + else manual(); +} + function render() { const listening = noSession ? "No session: the notes server is not answering. Your notes stay on this page; Copy all notes makes a block to paste to Claude." @@ -684,6 +709,7 @@ addEventListener("message", (e) => { else if (m.type === "shown" && m.found === false && chosen) say("That note's place is not on the mockup now, so no box is drawn."); else if (m.type === "content" && fits(m.name, schema.properties.content)) { content = m.name; renderControls(); } else if (m.type === "want-text") askText(); + else if (m.type === "chosen" && wantChosen) copyChosen(m); else if (m.type === "key" && FRAME_KEYS.has(m.key)) shortcut(m.key); else if (m.type === "pinned" && picking === "element") { const note = noteFrom(m, "element"); @@ -721,6 +747,7 @@ $("dock").addEventListener("click", () => setPanel($("panel").hidden)); $("markers").addEventListener("click", () => setMarkers(!markersShown)); $("cancel").addEventListener("click", closeComposer); $("copy").addEventListener("click", copyAll); +$("copy-chosen").addEventListener("click", askChosen); $("copy-config").addEventListener("click", copyConfig); $("copy-values").addEventListener("click", () => { closeMenus(); $("copy-button").focus(); if ($("panel").hidden) setPanel(true); selectTab(tabs[1], false); copyConfig(); }); $("side").addEventListener("click", () => setSide($("panel").dataset.side === "start" ? "end" : "start")); @@ -796,7 +823,7 @@ try { if (loads > 1) { ready = false; picking = null; - for (const b of [...Object.values(buttons), $("quote"), $("controls-fields"), $("copy-config"), $("reset-controls"), $("copy-values"), $("vprev"), $("vnext")]) b.disabled = true; + for (const b of [...Object.values(buttons), $("quote"), $("controls-fields"), $("copy-config"), $("reset-controls"), $("copy-values"), $("copy-chosen"), $("vprev"), $("vnext")]) b.disabled = true; for (const b of $("data-list").children) b.disabled = true; say("The mockup loaded another page in its frame, so this page stopped listening to it. Reload to start again."); $("controls-status").textContent = "The mockup loaded another page in its frame, so these controls no longer reach it. Reload to start again."; diff --git a/plugins/prototype/notes-bridge/proto-pin.js b/plugins/prototype/notes-bridge/proto-pin.js index a230a91069..7b9b3c9e54 100644 --- a/plugins/prototype/notes-bridge/proto-pin.js +++ b/plugins/prototype/notes-bridge/proto-pin.js @@ -635,6 +635,7 @@ else if (m.type === "name") names(m.targets); else if (m.type === "set") setControls(m.values); else if (m.type === "press" && !mode) replay([m.step]); + else if (m.type === "copy-out") post({ type: "chosen", text: copyOut() }); else if (m.type === "focus") { const p = pins.find(x => x.n === m.n); if (!p) return; @@ -643,9 +644,20 @@ else scrollTo({ top: Math.max(0, at.y - innerHeight / 3), behavior }); } }); + /** The page's copy-out terminator text, from the function its own terminator calls; null when it has none. */ + function copyOut() { + try { + const t = typeof window.uiFeedbackCopyOut === "function" ? window.uiFeedbackCopyOut() : null; + return typeof t === "string" ? t : null; + } catch (err) { + console.error("ui-feedback: the page's uiFeedbackCopyOut threw", err); + return null; + } + } const marks = [...document.querySelectorAll("[data-data-control]")].map(stepOf).filter(Boolean); post({ type: "ready", + copyOut: typeof window.uiFeedbackCopyOut === "function", controls: window.uiFeedbackControls?.manifest() ?? null, data: [...new Map(marks.map(m => [JSON.stringify(m), m])).values()], content: document.querySelector('[data-content-set="demo"]') && document.querySelector('[data-content-set="worst"]') ? root.dataset.vsContent || null : null, diff --git a/plugins/prototype/skills/explore-directions/SKILL.md b/plugins/prototype/skills/explore-directions/SKILL.md index 91d58c8b0a..40fd99662f 100644 --- a/plugins/prototype/skills/explore-directions/SKILL.md +++ b/plugins/prototype/skills/explore-directions/SKILL.md @@ -169,7 +169,7 @@ page with synthetic data and an in-page switcher. Assemble one per task: 3. **A copy-out terminator**, a small control that lifts the winning-variant key, notes and the proposed test ids back out as text you can paste into your durable answer. It lists the test ids per variant, read from the page's `data-testid` attributes rather than typed out, so the - list matches the markup. + list matches the markup. Build its text as [reference/feedback.md](reference/feedback.md#the-copy-out-function) says. Constraints: diff --git a/plugins/prototype/skills/explore-directions/assets/variant-switcher.html b/plugins/prototype/skills/explore-directions/assets/variant-switcher.html index cc1101acf1..90636caf5c 100644 --- a/plugins/prototype/skills/explore-directions/assets/variant-switcher.html +++ b/plugins/prototype/skills/explore-directions/assets/variant-switcher.html @@ -26,6 +26,8 @@ elements inside it are lost: wire variant interactions with listeners delegated from document. Chrome, not a contestant: leave its look alone. It takes the notes host page's light and dark tokens, and it hides when that page frames the mockup, whose command bar steps the variants. + Under that bar it also hides every element marked data-harness-chrome, such as the page's data + bar, whose controls the bar's Sample data menu presses. -->