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