Add copy buttons to code blocks and tool input and output

This commit is contained in:
Ian Gulliver
2026-10-15 16:52:20 -07:00
parent e9039cceb4
commit f9be3c3c9d
10 changed files with 81 additions and 14 deletions
+17
View File
@@ -39,6 +39,23 @@ export function replace(el: Element, ...children: Child[]): void {
append(el, ...children);
}
export function copyable(pre: HTMLPreElement): HTMLElement {
const button = h("button", { type: "button", class: "copy", title: "Copy to the clipboard" }, "copy");
button.onclick = async () => {
try {
await navigator.clipboard.writeText(pre.textContent ?? "");
} catch (err) {
void showAlert(errorText(err));
return;
}
button.textContent = "copied";
setTimeout(() => {
button.textContent = "copy";
}, 1500);
};
return h("div", { class: "copyable" }, pre, button);
}
export function ago(iso: string): string {
const seconds = Math.round((Date.now() - Date.parse(iso)) / 1000);
if (seconds < 60) {