oficina / docs Public

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

Plain git — no account needed to clone.

Download

Download this file

1/* share-pop.js: the editor's Share as a popover under its button rather
2 than a page of its own (spec 2026-11-11-editor-i2-design.md, "Share as
3 a popover", and its revision 2).
4
5 The panel is the server's: GET /d/{sqid}/share?panel=1 from a script
6 answers "docs-share-panel" (share_panel.go), every control in it a
7 real form with panel=1. A submit is sent with fetch, and the answer,
8 which the 303 brings back to the panel, replaces the panel. Nothing is
9 composed here, so the server stays the one authority and the share
10 page stays the way in with no script.
11
12 Wide (36rem and up): a manual popover under the button. A pointerdown
13 outside closes it, except on Share, which does nothing while it is
14 open (Paul: "should do nothing. It's causing a flash"). Narrow: a
15 modal dialog across the top, which keeps focus inside it. Esc closes
16 either and returns focus to Share, or to the Menu trigger when Share
17 was a row in estilo's Menu sheet.
18
19 Busy: one request at a time; a second submit while one is out is
20 refused, and a switch flipped then goes back. A request past 500ms
21 marks its control aria-busy (the GET rule's shape; Paul ruled the busy
22 hold is for POSTs a page makes, and these redraw a panel). A failure
23 puts the switch back and says so in the panel. */
24(function () {
25 "use strict";
26 var bar = document.querySelector("[estilo-bar]");
27 if (!bar || !window.fetch || !window.DOMParser) return;
28 var links = bar.querySelectorAll('a[href$="/share"]');
29 if (!links.length) return;
30 var href = links[0].getAttribute("href");
31
32 var pop = document.createElement("dialog");
33 pop.setAttribute("docs-share-pop", "");
34 pop.setAttribute("aria-labelledby", "docs-share-heading");
35 document.body.appendChild(pop);
36
37 var narrow = window.matchMedia("(max-width: 35.99rem)");
38 var canPopover = typeof pop.showPopover === "function";
39 var opener = null;
40 var busy = false;
41 var drawn = false;
42 var opening = 0;
43
44 var isOpen = function () {
45 return pop.open || (canPopover && pop.hasAttribute("popover") && pop.matches(":popover-open"));
46 };
47
48 // Under the bar, its inline end on the button's. Placed, then measured
49 // and corrected: what an inset is measured from (the viewport with or
50 // without a classic scrollbar, in the top layer) differs by engine, so
51 // the panel's own box is the one reading that is right everywhere.
52 var place = function () {
53 var b = bar.getBoundingClientRect();
54 var r = opener.getBoundingClientRect();
55 var rtl = getComputedStyle(document.documentElement).direction === "rtl";
56 pop.style.insetBlockStart = Math.round(b.bottom + 6) + "px";
57 var end = rtl ? r.left : document.documentElement.clientWidth - r.right;
58 pop.style.insetInlineEnd = Math.max(8, Math.round(end)) + "px";
59 if (!isOpen()) return;
60 var p = pop.getBoundingClientRect();
61 var off = rtl ? p.left - r.left : r.right - p.right;
62 if (Math.abs(off) >= 1) pop.style.insetInlineEnd = Math.max(8, Math.round(end - off)) + "px";
63 };
64
65 var show = function () {
66 if (!narrow.matches && canPopover) {
67 pop.setAttribute("popover", "manual");
68 place();
69 pop.showPopover();
70 place();
71 } else {
72 pop.removeAttribute("popover");
73 pop.style.insetBlockStart = "";
74 pop.style.insetInlineEnd = "";
75 pop.showModal();
76 }
77 };
78
79 var close = function (restore) {
80 if (!isOpen()) return;
81 if (pop.hasAttribute("popover")) pop.hidePopover();
82 else pop.close();
83 if (restore && opener && opener.isConnected) opener.focus({ preventScroll: true });
84 };
85
86 var failed = function () {
87 var panel = pop.querySelector("[docs-share-panel]");
88 var text = panel ? panel.getAttribute("data-failed") : "";
89 var old = pop.querySelector("[docs-share-error]");
90 if (old) old.remove();
91 var p = document.createElement("p");
92 p.setAttribute("docs-share-error", "");
93 p.setAttribute("role", "alert");
94 p.textContent = text;
95 var head = pop.querySelector("[docs-share-head]");
96 if (head) head.after(p);
97 else pop.replaceChildren(p);
98 };
99
100 // The panel from an answer; anything else (a 404, a sign-in page, a
101 // proxy's error) is a failure, not something to draw. That failure is
102 // marked answered: the server said something, which the share page can
103 // say better. A fetch that never got an answer (offline) rejects
104 // without the mark.
105 var load = function (url, init, still) {
106 return fetch(url, Object.assign({ credentials: "same-origin", headers: { Accept: "text/html" } }, init || {}))
107 .then(function (r) {
108 return r.text();
109 })
110 .then(function (text) {
111 var doc = new DOMParser().parseFromString(text, "text/html");
112 var panel = doc.querySelector("[docs-share-panel]");
113 if (!panel) {
114 var err = new Error("no panel");
115 err.answered = true;
116 throw err;
117 }
118 // A load for a popover since closed, or reopened, draws nothing.
119 if (still && !still()) return false;
120 pop.replaceChildren(document.importNode(panel, true));
121 pop.removeAttribute("aria-busy");
122 return true;
123 });
124 };
125
126 // Which control to give focus back to after a redraw: the same form's,
127 // found again by its action and address.
128 var keyOf = function (form) {
129 var a = form.querySelector('input[name="address"][type="hidden"]');
130 return form.getAttribute("action") + "|" + (a ? a.value : "") + "|" + (form.hasAttribute("docs-share-invite") ? "invite" : "");
131 };
132 var focusKey = function (key) {
133 var forms = pop.querySelectorAll("form");
134 for (var i = 0; i < forms.length; i++) {
135 if (keyOf(forms[i]) === key) {
136 var c = forms[i].hasAttribute("docs-share-invite") ? forms[i].querySelector('input[type="email"]') : forms[i].querySelector("input[type=checkbox], button");
137 if (c) { c.focus({ preventScroll: true }); return; }
138 }
139 }
140 var email = pop.querySelector('[docs-share-invite] input[type="email"]');
141 if (email) email.focus({ preventScroll: true });
142 };
143
144 var open = function (from) {
145 if (isOpen()) return;
146 opener = from;
147 var more = from.closest("details[estilo-bar-more]");
148 if (more) {
149 more.open = false;
150 opener = more.querySelector("summary") || from;
151 }
152 drawn = false;
153 pop.replaceChildren();
154 pop.setAttribute("aria-busy", "true");
155 show();
156 // This opening's load, and whether it is still the open one: closed
157 // (Escape, a click outside) or opened again since, it is not.
158 var mine = ++opening;
159 var still = function () { return mine === opening && isOpen(); };
160 load(href + "?panel=1", null, still)
161 .then(function (shown) {
162 if (!shown) return;
163 drawn = true;
164 var email = pop.querySelector('[docs-share-invite] input[type="email"]');
165 if (email) email.focus({ preventScroll: true });
166 })
167 .catch(function (err) {
168 // Nothing drawn yet, so nothing to say a failure in and no close
169 // button (review on #106). The popover closes and, only if the
170 // server answered with something that is not the panel (the
171 // sign-in page for an expired session, a 404) and the person
172 // still has it open, Share goes to the share page, which is what
173 // Share is with no script and handles both itself. A request
174 // that got no answer (offline) stays on the document: leaving
175 // would put the browser's error page where the editor was, and
176 // the words typed offline with it. failed() is for a redraw.
177 var go = !!(err && err.answered) && still();
178 if (mine !== opening) return;
179 pop.removeAttribute("aria-busy");
180 close(false);
181 if (go) location.assign(href);
182 });
183 };
184
185 Array.prototype.forEach.call(links, function (a) {
186 a.addEventListener("click", function (e) {
187 if (e.button !== 0 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) return;
188 e.preventDefault();
189 open(a);
190 });
191 });
192
193 pop.addEventListener("submit", function (e) {
194 var form = e.target;
195 e.preventDefault();
196 if (busy || !drawn) return;
197 busy = true;
198 var control = e.submitter || form.querySelector("input[type=checkbox]");
199 var timer = setTimeout(function () {
200 if (control) control.setAttribute("aria-busy", "true");
201 }, 500);
202 var key = keyOf(form);
203 var body = new URLSearchParams(new FormData(form));
204 load(form.getAttribute("action"), { method: "POST", body: body })
205 .then(function () {
206 focusKey(key);
207 })
208 .catch(function () {
209 var sw = form.querySelector("[docs-share-switch] input");
210 if (sw) sw.checked = !sw.checked;
211 if (control) control.removeAttribute("aria-busy");
212 failed();
213 })
214 .finally(function () {
215 clearTimeout(timer);
216 busy = false;
217 });
218 });
219
220 // A switch submits its rung's form; the direction is the form's own.
221 pop.addEventListener("change", function (e) {
222 var input = e.target;
223 if (!input.closest || !input.closest("[docs-share-switch]")) return;
224 if (busy) {
225 input.checked = !input.checked;
226 return;
227 }
228 if (input.form) input.form.requestSubmit();
229 });
230
231 pop.addEventListener("click", function (e) {
232 var t = e.target.closest ? e.target.closest("button") : null;
233 if (t && t.hasAttribute("docs-share-close")) {
234 close(true);
235 } else if (t && t.hasAttribute("docs-share-copy")) {
236 var input = pop.querySelector("[docs-share-link] input");
237 if (input && navigator.clipboard) {
238 navigator.clipboard.writeText(input.value).then(function () {
239 t.textContent = t.getAttribute("data-copied") || t.textContent;
240 });
241 }
242 } else if (!t && e.target === pop && pop.open && !pop.hasAttribute("popover")) {
243 // The phone sheet's backdrop: a click outside the dialog's box.
244 var r = pop.getBoundingClientRect();
245 if (e.clientY > r.bottom || e.clientY < r.top || e.clientX < r.left || e.clientX > r.right) close(true);
246 }
247 });
248
249 // The modal's own Esc: closed here so focus goes back where it came from.
250 pop.addEventListener("cancel", function (e) {
251 e.preventDefault();
252 close(true);
253 });
254
255 document.addEventListener("pointerdown", function (e) {
256 if (!isOpen() || !pop.hasAttribute("popover") || pop.contains(e.target)) return;
257 for (var i = 0; i < links.length; i++) if (links[i].contains(e.target)) return;
258 close(false);
259 });
260
261 document.addEventListener("keydown", function (e) {
262 if (e.key === "Escape" && !e.defaultPrevented && isOpen() && pop.hasAttribute("popover")) {
263 e.preventDefault();
264 close(true);
265 }
266 });
267
268 window.addEventListener("resize", function () {
269 if (isOpen() && pop.hasAttribute("popover")) place();
270 });
271})();
272