| 1 | // fixtures/share-link.mjs — the share page's Copy button. |
| 2 | // |
| 3 | // The published link is shown exactly once (suite ruling 16: the server |
| 4 | // keeps a hash, not the URL), so copying it is the whole job of that |
| 5 | // moment. Without script the page still works: the link is in a |
| 6 | // read-only field that selects on focus, and a person copies it by hand. |
| 7 | // With script this reveals the Copy button, writes the link to the |
| 8 | // clipboard, and says so in the polite status line beside it. |
| 9 | // |
| 10 | // It carries no copy of its own: the "copied" wording is rendered by the |
| 11 | // server from the catalog into data-copied. |
| 12 | |
| 13 | export function mountCopy(root) { |
| 14 | const input = root.querySelector('[docs-share-url-input]') |
| 15 | const button = root.querySelector('[docs-share-copy]') |
| 16 | const status = root.querySelector('[docs-share-copy-status]') |
| 17 | if (!input || !button) return false |
| 18 | input.addEventListener('focus', () => input.select()) |
| 19 | button.hidden = false |
| 20 | button.addEventListener('click', async () => { |
| 21 | input.focus() |
| 22 | input.select() |
| 23 | let ok = false |
| 24 | try { |
| 25 | if (navigator.clipboard && navigator.clipboard.writeText) { |
| 26 | await navigator.clipboard.writeText(input.value) |
| 27 | ok = true |
| 28 | } |
| 29 | } catch { |
| 30 | ok = false |
| 31 | } |
| 32 | if (!ok) { |
| 33 | try { |
| 34 | ok = document.execCommand('copy') |
| 35 | } catch { |
| 36 | ok = false |
| 37 | } |
| 38 | } |
| 39 | if (status) status.textContent = ok ? button.getAttribute('data-copied') || '' : '' |
| 40 | }) |
| 41 | return true |
| 42 | } |
| 43 | |
| 44 | const root = document.querySelector('[docs-share-public]') |
| 45 | if (root) mountCopy(root) |
| 46 | |