|
| 1 | +/* Behaviour for the "Copy page" control. |
| 2 | + * |
| 3 | + * Every handler is delegated from `document`, because the theme swaps the |
| 4 | + * content in place when instant navigation is on: a listener bound to an |
| 5 | + * element of one page would not survive the move to the next. |
| 6 | + */ |
| 7 | +(function () { |
| 8 | + "use strict"; |
| 9 | + |
| 10 | + var RESET_AFTER_MS = 2000; |
| 11 | + |
| 12 | + /* Where the Markdown of a page is written, as an absolute URL. Two data |
| 13 | + attributes say how far the site root is from here and where this page sits |
| 14 | + below it; scripts/emit_markdown.py writes the file to match. The site's own |
| 15 | + name is a third, so this file is the same in every project that carries it. */ |
| 16 | + function markdownUrl(widget) { |
| 17 | + var base = (widget.dataset.copyBase || ".").replace(/\/$/, ""); |
| 18 | + var page = widget.dataset.copyPage || ""; |
| 19 | + var relative = page === "" ? "index.md" : page.replace(/\/$/, "") + ".md"; |
| 20 | + return new URL(base + "/" + relative, window.location.href).href; |
| 21 | + } |
| 22 | + |
| 23 | + function prompt(widget) { |
| 24 | + var title = widget.dataset.copyTitle || document.title; |
| 25 | + var site = widget.dataset.copySite || "project"; |
| 26 | + return ( |
| 27 | + "Read " + |
| 28 | + markdownUrl(widget) + |
| 29 | + ' -- the "' + |
| 30 | + title + |
| 31 | + '" page of the ' + |
| 32 | + site + |
| 33 | + " documentation -- so I can ask questions about it." |
| 34 | + ); |
| 35 | + } |
| 36 | + |
| 37 | + function destination(widget, name) { |
| 38 | + var question = encodeURIComponent(prompt(widget)); |
| 39 | + switch (name) { |
| 40 | + case "markdown": |
| 41 | + return markdownUrl(widget); |
| 42 | + case "chatgpt": |
| 43 | + return "https://chatgpt.com/?hints=search&q=" + question; |
| 44 | + case "claude": |
| 45 | + return "https://claude.ai/new?q=" + question; |
| 46 | + case "perplexity": |
| 47 | + return "https://www.perplexity.ai/search?q=" + question; |
| 48 | + default: |
| 49 | + return markdownUrl(widget); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + function write(text) { |
| 54 | + if (navigator.clipboard && window.isSecureContext) { |
| 55 | + return navigator.clipboard.writeText(text); |
| 56 | + } |
| 57 | + /* Insecure origins have no clipboard API; the old selection dance still |
| 58 | + works there, which keeps a local preview usable. */ |
| 59 | + return new Promise(function (resolve, reject) { |
| 60 | + var area = document.createElement("textarea"); |
| 61 | + area.value = text; |
| 62 | + area.style.position = "fixed"; |
| 63 | + area.style.opacity = "0"; |
| 64 | + document.body.appendChild(area); |
| 65 | + area.select(); |
| 66 | + var ok = document.execCommand("copy"); |
| 67 | + document.body.removeChild(area); |
| 68 | + ok ? resolve() : reject(new Error("copy refused")); |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + function announce(widget, label, state) { |
| 73 | + var slot = widget.querySelector("[data-copy-label]"); |
| 74 | + if (slot) slot.textContent = label; |
| 75 | + if (state) { |
| 76 | + widget.dataset.copyState = state; |
| 77 | + } else { |
| 78 | + delete widget.dataset.copyState; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + function copy(widget) { |
| 83 | + var reset = function () { |
| 84 | + window.setTimeout(function () { |
| 85 | + announce(widget, "Copy page", null); |
| 86 | + }, RESET_AFTER_MS); |
| 87 | + }; |
| 88 | + fetch(markdownUrl(widget)) |
| 89 | + .then(function (response) { |
| 90 | + if (!response.ok) throw new Error(String(response.status)); |
| 91 | + return response.text(); |
| 92 | + }) |
| 93 | + .then(write) |
| 94 | + .then(function () { |
| 95 | + announce(widget, "Copied", "copied"); |
| 96 | + reset(); |
| 97 | + }) |
| 98 | + .catch(function () { |
| 99 | + announce(widget, "Copy failed", null); |
| 100 | + reset(); |
| 101 | + }); |
| 102 | + } |
| 103 | + |
| 104 | + function close(widget) { |
| 105 | + var menu = widget.querySelector("[data-copy-menu]"); |
| 106 | + var toggle = widget.querySelector("[data-copy-toggle]"); |
| 107 | + if (menu) menu.hidden = true; |
| 108 | + if (toggle) toggle.setAttribute("aria-expanded", "false"); |
| 109 | + } |
| 110 | + |
| 111 | + function closeAll(except) { |
| 112 | + var widgets = document.querySelectorAll(".md-copy-page"); |
| 113 | + for (var i = 0; i < widgets.length; i++) { |
| 114 | + if (widgets[i] !== except) close(widgets[i]); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + document.addEventListener("click", function (event) { |
| 119 | + var target = event.target; |
| 120 | + if (!(target instanceof Element)) return; |
| 121 | + |
| 122 | + var widget = target.closest(".md-copy-page"); |
| 123 | + if (!widget) { |
| 124 | + closeAll(null); |
| 125 | + return; |
| 126 | + } |
| 127 | + closeAll(widget); |
| 128 | + |
| 129 | + if (target.closest("[data-copy-action]")) { |
| 130 | + event.preventDefault(); |
| 131 | + close(widget); |
| 132 | + copy(widget); |
| 133 | + return; |
| 134 | + } |
| 135 | + |
| 136 | + var toggle = target.closest("[data-copy-toggle]"); |
| 137 | + if (toggle) { |
| 138 | + event.preventDefault(); |
| 139 | + var menu = widget.querySelector("[data-copy-menu]"); |
| 140 | + if (!menu) return; |
| 141 | + /* The destinations are filled in on the way out rather than at load: |
| 142 | + the page under the widget may have changed since. */ |
| 143 | + var links = menu.querySelectorAll("[data-copy-open]"); |
| 144 | + for (var i = 0; i < links.length; i++) { |
| 145 | + links[i].href = destination(widget, links[i].dataset.copyOpen); |
| 146 | + } |
| 147 | + menu.hidden = !menu.hidden; |
| 148 | + toggle.setAttribute("aria-expanded", menu.hidden ? "false" : "true"); |
| 149 | + return; |
| 150 | + } |
| 151 | + |
| 152 | + if (target.closest("[data-copy-open]")) close(widget); |
| 153 | + }); |
| 154 | + |
| 155 | + document.addEventListener("keydown", function (event) { |
| 156 | + if (event.key === "Escape") closeAll(null); |
| 157 | + }); |
| 158 | +})(); |
0 commit comments