oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1// paste.js is the paste box's Copy button. Every fenced block in a
2// rendered message is a paste box (chatmarkdown), on every page that shows
3// one, streamed or final. Copy puts the block's text on the clipboard
4// exactly as the code element holds it, less the newline that ends its
5// last line. It waits while the block is still streaming (aria-disabled).
6// If the clipboard is refused the text is copied from a field nobody sees,
7// or else selected for the person to copy, and that is announced. Without
8// script the button is hidden (equipo.css) and the text can be selected.
9(function () {
10 "use strict";
11 // Copy shows from here on (equipo.css hides it until then).
12 document.documentElement.setAttribute("data-paste", "");
13 var status = null;
14 // say announces through one polite status line for the whole page.
15 function say(text) {
16 if (!status) {
17 status = document.createElement("div");
18 status.setAttribute("role", "status");
19 status.setAttribute("equipo-visually-hidden", "");
20 document.body.append(status);
21 }
22 status.textContent = "";
23 // A fresh change, so a second copy is announced again.
24 setTimeout(function () { status.textContent = text; }, 60);
25 }
26 document.addEventListener("click", function (event) {
27 var button = event.target instanceof Element && event.target.closest("[equipo-paste-copy]");
28 if (!button || button.getAttribute("aria-disabled") === "true") return;
29 var box = button.closest("[equipo-paste]"), code = box && box.querySelector("pre > code");
30 if (!code) return;
31 var text = code.textContent.replace(/\r?\n$/, "");
32 // The word beside the icons is the button's name: Copy, then Copied
33 // with the tick (data-done) for a moment.
34 var word = button.querySelector("[equipo-paste-word]") || button;
35 if (!button.pasteLabel) button.pasteLabel = word.textContent;
36 function done() {
37 word.textContent = button.dataset.copied;
38 button.setAttribute("data-done", "");
39 say(button.dataset.copied);
40 clearTimeout(button.pasteTimer);
41 button.pasteTimer = setTimeout(function () {
42 word.textContent = button.pasteLabel;
43 button.removeAttribute("data-done");
44 }, 1800);
45 }
46 function fallback() {
47 var field = document.createElement("textarea"), copied = false;
48 field.value = text;
49 field.setAttribute("readonly", "");
50 field.setAttribute("equipo-visually-hidden", "");
51 document.body.append(field);
52 field.select();
53 try { copied = document.execCommand("copy"); } catch (e) { /* select it below */ }
54 field.remove();
55 button.focus();
56 if (copied) return done();
57 getSelection().selectAllChildren(code);
58 say(button.dataset.selected);
59 }
60 if (navigator.clipboard && window.isSecureContext) navigator.clipboard.writeText(text).then(done, fallback);
61 else fallback();
62 });
63})();
64