@@ -0,0 +1,120 @@
1+// Rats devnotes: Ctrl+click anything on this page and write a ticket about it.
2+// It works only on a loopback address, with `rat-king devnotes` running. See
3+// the rats skill. Embed it in development builds only.
4+(() => {
5+ const PORT = 7287;
6+ const LOOPBACK = /^(localhost|127\.0\.0\.1|\[::1\]|.+\.localhost)$/;
7+ if (!LOOPBACK.test(location.hostname) || window.__ratsDevnotes) return;
8+ window.__ratsDevnotes = true;
9+
10+ const NAMED = "li, tr, p, pre, img, video, audio, h1, h2, h3, h4, blockquote, figure, button, a, label, summary, input, textarea, select";
11+ const MAX_TEXT = 600;
12+ let float = null;
13+ const closeFloat = () => {
14+ float?.remove();
15+ float = null;
16+ };
17+
18+ const clickedText = (target) => {
19+ const selection = getSelection();
20+ if (selection?.toString() && selection.containsNode(target, true)) return selection.toString();
21+ const named = target.closest(NAMED);
22+ if (!named) return "";
23+ if (named instanceof HTMLImageElement) return named.alt || "An image";
24+ if (named instanceof HTMLMediaElement) return `A ${named.tagName.toLowerCase()}`;
25+ if ("value" in named && !(named instanceof HTMLButtonElement)) return String(named.value);
26+ return named.innerText || named.getAttribute("aria-label") || "";
27+ };
28+
29+ const nameOf = (element) => {
30+ const classes = typeof element.className === "string" ? element.className.trim().split(/\s+/).filter(Boolean).slice(0, 3) : [];
31+ return element.tagName.toLowerCase() + (element.id ? `#${element.id}` : "") + classes.map((name) => `.${name}`).join("");
32+ };
33+
34+ const contextOf = (target) => {
35+ const clean = clickedText(target).trim().replace(/\s+\n/g, "\n");
36+ const shown = clean.length > MAX_TEXT ? `${clean.slice(0, MAX_TEXT)}…` : clean;
37+ const quote = shown.split("\n").map((line) => `> ${line}`).join("\n");
38+ return `## Context\n\nFiled from \`${location.href}\`, on \`${nameOf(target)}\`.${shown ? `\n\n${quote}` : ""}\n`;
39+ };
40+
41+ const write = (target, x, y) => {
42+ closeFloat();
43+ const context = contextOf(target);
44+ const host = document.createElement("div");
45+ const root = host.attachShadow({ mode: "open" });
46+ root.innerHTML = `<style>
47+ :host { all: initial; position: fixed; z-index: 2147483647; width: 320px; padding: 8px; background: #1c1f22; box-shadow: 0 8px 32px #0009; }
48+ * { font: 13px/1.5 "JetBrains Mono", monospace; color: #d8dcdf; box-sizing: border-box; }
49+ textarea { display: block; width: 100%; height: 80px; padding: 8px 12px; border: 0; outline: 0; resize: none; background: #2a2e32; }
50+ p { margin: 8px 0 0; }
51+ [hidden] { display: none; }
52+ </style><textarea placeholder="What should change?" spellcheck="false" aria-label="New ticket"></textarea><p hidden></p>`;
53+ const note = root.querySelector("textarea");
54+ const message = root.querySelector("p");
55+ const say = (text) => {
56+ message.textContent = text;
57+ message.hidden = false;
58+ };
59+ let sending = false;
60+ const submit = async () => {
61+ const [title = "", ...rest] = note.value.trim().split("\n");
62+ if (!title.trim() || sending) return;
63+ sending = true;
64+ const more = rest.join("\n").trim();
65+ let reply;
66+ try {
67+ reply = await fetch(`http://127.0.0.1:${PORT}/ticket`, {
68+ method: "POST",
69+ headers: { "Content-Type": "application/json" },
70+ body: JSON.stringify({ title: title.trim(), body: more ? `${more}\n\n${context}` : context }),
71+ });
72+ } catch {
73+ sending = false;
74+ return say(`Nothing answers on port ${PORT}. Run rat-king devnotes in the project's folder.`);
75+ }
76+ const answer = await reply.json().catch(() => ({}));
77+ sending = false;
78+ if (!reply.ok) return say(answer.error ?? `The ticket was refused: ${reply.status}`);
79+ note.hidden = true;
80+ say(`Ticket ${answer.id}`);
81+ setTimeout(() => float === host && closeFloat(), 1500);
82+ };
83+ host.addEventListener("keydown", (event) => {
84+ event.stopPropagation();
85+ if (event.key === "Escape") closeFloat();
86+ else if (event.key === "Enter" && !event.shiftKey) {
87+ event.preventDefault();
88+ void submit();
89+ }
90+ });
91+ document.documentElement.append(host);
92+ const { width, height } = host.getBoundingClientRect();
93+ host.style.left = `${Math.max(0, Math.min(x, innerWidth - width))}px`;
94+ host.style.top = `${Math.max(0, Math.min(y, innerHeight - height))}px`;
95+ float = host;
96+ note.focus();
97+ };
98+
99+ // The press itself opens the form: it would clear a selection, a press on an
100+ // image starts a drag that never ends in a click, and a canvas takes the pointer.
101+ const inFloat = (event) => float && event.composedPath().includes(float);
102+ const isTicketClick = (event) => event.ctrlKey && event.button === 0 && !inFloat(event);
103+ const swallow = (event) => {
104+ event.preventDefault();
105+ event.stopPropagation();
106+ };
107+ // Stopped, not prevented: preventing it would cancel the mousedown.
108+ document.addEventListener("pointerdown", (event) => isTicketClick(event) && event.stopPropagation(), true);
109+ document.addEventListener("click", (event) => isTicketClick(event) && swallow(event), true);
110+ document.addEventListener(
111+ "mousedown",
112+ (event) => {
113+ if (inFloat(event)) return;
114+ if (!isTicketClick(event)) return closeFloat();
115+ swallow(event);
116+ write(event.target, event.clientX, event.clientY);
117+ },
118+ true,
119+ );
120+})();