Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changes/planning/fix-17991-copy-chosen-design-2f5881f0.md
Original file line number Diff line number Diff line change
@@ -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.
7 changes: 7 additions & 0 deletions .changes/prototype/fix-17991-copy-chosen-design-694e6c08.md
Original file line number Diff line number Diff line change
@@ -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.
8 changes: 8 additions & 0 deletions docs/contracts/ui-feedback/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions lib/ui-feedback/frame-channel.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 24 additions & 1 deletion lib/ui-feedback/frame-channel.test.mjs
Original file line number Diff line number Diff line change
@@ -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");
Expand Down Expand Up @@ -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: <img src=x onerror="alert(1)">', "a</textarea>b", "<!-- x -->", "<?xml?>", "<svg/onload=alert(1)>", "bell\u0007", "nul\u0000"]) {
assert.equal(chosenFrom({ type: "chosen", text }), null, JSON.stringify(text));
}
});
1 change: 1 addition & 0 deletions lib/ui-feedback/host.html
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@
<button type="button" class="btn" id="copy-button" aria-label="Copy" aria-expanded="false" aria-controls="copy-menu"><svg viewBox="0 0 24 24" aria-hidden="true"><rect x="8" y="8" width="12" height="12" rx="2"/><path d="M16 8V5a1 1 0 00-1-1H5a1 1 0 00-1 1v10a1 1 0 001 1h3"/></svg><span class="lbl">Copy</span></button>
<div class="menu surface" id="copy-menu" role="group" aria-label="Copy out" hidden>
<button type="button" class="btn" id="copy">Copy all notes <span class="aside" id="copy-count"></span></button>
<button type="button" class="btn" id="copy-chosen" disabled>Copy chosen design</button>
<button type="button" class="btn" id="copy-values" disabled>Copy control values</button>
<p class="hint">Paste what you copy to Claude in the terminal.</p>
</div>
Expand Down
31 changes: 29 additions & 2 deletions lib/ui-feedback/host.mjs
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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.");
Comment thread
kyle-sexton marked this conversation as resolved.
Resolved
$("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."
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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"));
Expand Down Expand Up @@ -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.";
Expand Down
12 changes: 12 additions & 0 deletions lib/ui-feedback/proto-pin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions plugins/planning/shared/web/proto-pin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down
15 changes: 15 additions & 0 deletions plugins/prototype/notes-bridge/frame-channel.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions plugins/prototype/notes-bridge/host.html
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@
<button type="button" class="btn" id="copy-button" aria-label="Copy" aria-expanded="false" aria-controls="copy-menu"><svg viewBox="0 0 24 24" aria-hidden="true"><rect x="8" y="8" width="12" height="12" rx="2"/><path d="M16 8V5a1 1 0 00-1-1H5a1 1 0 00-1 1v10a1 1 0 001 1h3"/></svg><span class="lbl">Copy</span></button>
<div class="menu surface" id="copy-menu" role="group" aria-label="Copy out" hidden>
<button type="button" class="btn" id="copy">Copy all notes <span class="aside" id="copy-count"></span></button>
<button type="button" class="btn" id="copy-chosen" disabled>Copy chosen design</button>
<button type="button" class="btn" id="copy-values" disabled>Copy control values</button>
<p class="hint">Paste what you copy to Claude in the terminal.</p>
</div>
Expand Down
31 changes: 29 additions & 2 deletions plugins/prototype/notes-bridge/host.mjs
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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."
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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"));
Expand Down Expand Up @@ -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.";
Expand Down
12 changes: 12 additions & 0 deletions plugins/prototype/notes-bridge/proto-pin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down
Loading
Loading