|
1 | 1 | // ``function*`` denotes a generator in JavaScript, see
|
2 | 2 | // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*
|
3 | 3 | function* getHideableCopyButtonElements(rootElement) {
|
4 |
| - // yield all elements with the "go" (Generic.Output), |
5 |
| - // "gp" (Generic.Prompt), or "gt" (Generic.Traceback) CSS class |
6 |
| - for (const el of rootElement.querySelectorAll('.go, .gp, .gt')) { |
7 |
| - yield el |
8 |
| - } |
9 |
| - // tracebacks (.gt) contain bare text elements that need to be |
10 |
| - // wrapped in a span to hide or show the element |
11 |
| - for (let el of rootElement.querySelectorAll('.gt')) { |
12 |
| - while ((el = el.nextSibling) && el.nodeType !== Node.DOCUMENT_NODE) { |
13 |
| - // stop wrapping text nodes when we hit the next output or |
14 |
| - // prompt element |
15 |
| - if (el.nodeType === Node.ELEMENT_NODE && el.matches(".gp, .go")) { |
16 |
| - break |
17 |
| - } |
18 |
| - // if the node is a text node with content, wrap it in a |
19 |
| - // span element so that we can control visibility |
20 |
| - if (el.nodeType === Node.TEXT_NODE && el.textContent.trim()) { |
21 |
| - const wrapper = document.createElement("span") |
22 |
| - el.after(wrapper) |
23 |
| - wrapper.appendChild(el) |
24 |
| - el = wrapper |
25 |
| - } |
26 |
| - yield el |
27 |
| - } |
| 4 | + // yield all elements with the "go" (Generic.Output), |
| 5 | + // "gp" (Generic.Prompt), or "gt" (Generic.Traceback) CSS class |
| 6 | + for (const el of rootElement.querySelectorAll(".go, .gp, .gt")) { |
| 7 | + yield el; |
| 8 | + } |
| 9 | + // tracebacks (.gt) contain bare text elements that need to be |
| 10 | + // wrapped in a span to hide or show the element |
| 11 | + for (let el of rootElement.querySelectorAll(".gt")) { |
| 12 | + while ((el = el.nextSibling) && el.nodeType !== Node.DOCUMENT_NODE) { |
| 13 | + // stop wrapping text nodes when we hit the next output or |
| 14 | + // prompt element |
| 15 | + if (el.nodeType === Node.ELEMENT_NODE && el.matches(".gp, .go")) { |
| 16 | + break; |
| 17 | + } |
| 18 | + // if the node is a text node with content, wrap it in a |
| 19 | + // span element so that we can control visibility |
| 20 | + if (el.nodeType === Node.TEXT_NODE && el.textContent.trim()) { |
| 21 | + const wrapper = document.createElement("span"); |
| 22 | + el.after(wrapper); |
| 23 | + wrapper.appendChild(el); |
| 24 | + el = wrapper; |
| 25 | + } |
| 26 | + yield el; |
28 | 27 | }
|
| 28 | + } |
29 | 29 | }
|
30 | 30 |
|
| 31 | +// Extract copyable text from the code block ignoring the |
| 32 | +// prompts and output. |
| 33 | +function getCopyableText(rootElement) { |
| 34 | + rootElement = rootElement.cloneNode(true); |
| 35 | + // get all the elements with the "gp" (Generic.Prompt), |
| 36 | + // "go" (Generic.Output), or "gt" (Generic.Traceback) CSS class |
| 37 | + const tracebacks = rootElement.querySelectorAll(".gt"); |
| 38 | + for (const el of tracebacks) { |
| 39 | + while ( |
| 40 | + el.nextSibling && |
| 41 | + (el.nextSibling.nodeType !== Node.DOCUMENT_NODE || |
| 42 | + !el.nextSibling.matches(".gp, .go")) |
| 43 | + ) { |
| 44 | + el.nextSibling.remove(); |
| 45 | + } |
| 46 | + } |
| 47 | + rootElement.re |
| 48 | + const elements = rootElement.querySelectorAll(".gp, .go, .gt"); |
| 49 | + // remove the prompts and output from the code block |
| 50 | + for (const el of elements) { |
| 51 | + el.remove(); |
| 52 | + } |
| 53 | + // get the text content of the code block |
| 54 | + return rootElement.innerText; |
| 55 | +} |
31 | 56 |
|
32 | 57 | const loadCopyButton = () => {
|
33 |
| - /* Add a [>>>] button in the top-right corner of code samples to hide |
34 |
| - * the >>> and ... prompts and the output and thus make the code |
35 |
| - * copyable. */ |
36 |
| - const hide_text = "Hide the prompts and output" |
37 |
| - const show_text = "Show the prompts and output" |
| 58 | + /* Add a [>>>] button in the top-right corner of code samples to hide |
| 59 | + * the >>> and ... prompts and the output and thus make the code |
| 60 | + * copyable. */ |
| 61 | + // const hide_text = _("Hide the prompts and output") |
| 62 | + // const show_text = _("Show the prompts and output") |
38 | 63 |
|
39 |
| - const button = document.createElement("span") |
40 |
| - button.classList.add("copybutton") |
41 |
| - button.innerText = ">>>" |
42 |
| - button.title = hide_text |
43 |
| - button.dataset.hidden = "false" |
44 |
| - const buttonClick = event => { |
45 |
| - // define the behavior of the button when it's clicked |
46 |
| - event.preventDefault() |
47 |
| - const buttonEl = event.currentTarget |
48 |
| - const codeEl = buttonEl.nextElementSibling |
49 |
| - if (buttonEl.dataset.hidden === "false") { |
50 |
| - // hide the code output |
51 |
| - for (const el of getHideableCopyButtonElements(codeEl)) { |
52 |
| - el.hidden = true |
53 |
| - } |
54 |
| - buttonEl.title = show_text |
55 |
| - buttonEl.dataset.hidden = "true" |
56 |
| - } else { |
57 |
| - // show the code output |
58 |
| - for (const el of getHideableCopyButtonElements(codeEl)) { |
59 |
| - el.hidden = false |
60 |
| - } |
61 |
| - buttonEl.title = hide_text |
62 |
| - buttonEl.dataset.hidden = "false" |
63 |
| - } |
64 |
| - } |
| 64 | + const button = document.createElement("button"); |
| 65 | + button.classList.add("copybutton"); |
| 66 | + button.type = "button"; |
| 67 | + button.innerText = _("Copy"); |
| 68 | + button.title = _("Copy code to clipboard"); |
| 69 | + // button.dataset.hidden = "false" |
| 70 | + let timeout; |
| 71 | + const buttonClick = (event) => { |
| 72 | + // define the behavior of the button when it's clicked |
| 73 | + // event.preventDefault() |
| 74 | + clearTimeout(timeout); |
| 75 | + const buttonEl = event.currentTarget; |
| 76 | + const codeEl = buttonEl.nextElementSibling; |
| 77 | + navigator.clipboard.writeText(getCopyableText(codeEl)); |
| 78 | + buttonEl.innerText = _("Copied!"); |
| 79 | + timeout = setTimeout(() => { |
| 80 | + buttonEl.innerText = _("Copy"); |
| 81 | + }, 1500); |
| 82 | + // if (buttonEl.dataset.hidden === "false") { |
| 83 | + // // hide the code output |
| 84 | + // for (const el of getHideableCopyButtonElements(codeEl)) { |
| 85 | + // el.hidden = true |
| 86 | + // } |
| 87 | + // buttonEl.title = show_text |
| 88 | + // buttonEl.dataset.hidden = "true" |
| 89 | + // } else { |
| 90 | + // // show the code output |
| 91 | + // for (const el of getHideableCopyButtonElements(codeEl)) { |
| 92 | + // el.hidden = false |
| 93 | + // } |
| 94 | + // buttonEl.title = hide_text |
| 95 | + // buttonEl.dataset.hidden = "false" |
| 96 | + // } |
| 97 | + }; |
65 | 98 |
|
66 |
| - const highlightedElements = document.querySelectorAll( |
67 |
| - ".highlight-python .highlight," |
68 |
| - + ".highlight-python3 .highlight," |
69 |
| - + ".highlight-pycon .highlight," |
70 |
| - + ".highlight-pycon3 .highlight," |
71 |
| - + ".highlight-default .highlight" |
72 |
| - ) |
| 99 | + const highlightedElements = document.querySelectorAll( |
| 100 | + ".highlight-python .highlight," + |
| 101 | + ".highlight-python3 .highlight," + |
| 102 | + ".highlight-pycon .highlight," + |
| 103 | + ".highlight-pycon3 .highlight," + |
| 104 | + ".highlight-default .highlight" |
| 105 | + ); |
73 | 106 |
|
74 |
| - // create and add the button to all the code blocks that contain >>> |
75 |
| - highlightedElements.forEach(el => { |
76 |
| - el.style.position = "relative" |
| 107 | + // create and add the button to all the code blocks that contain >>> |
| 108 | + highlightedElements.forEach((el) => { |
| 109 | + el.style.position = "relative"; |
77 | 110 |
|
78 |
| - // if we find a console prompt (.gp), prepend the (deeply cloned) button |
79 |
| - const clonedButton = button.cloneNode(true) |
80 |
| - // the onclick attribute is not cloned, set it on the new element |
81 |
| - clonedButton.onclick = buttonClick |
82 |
| - if (el.querySelector(".gp") !== null) { |
83 |
| - el.prepend(clonedButton) |
84 |
| - } |
85 |
| - }) |
86 |
| -} |
| 111 | + // if we find a console prompt (.gp), prepend the (deeply cloned) button |
| 112 | + const clonedButton = button.cloneNode(true); |
| 113 | + // the onclick attribute is not cloned, set it on the new element |
| 114 | + clonedButton.onclick = buttonClick; |
| 115 | + el.prepend(clonedButton); |
| 116 | + console.log("adding button"); |
| 117 | + }); |
| 118 | +}; |
87 | 119 |
|
88 | 120 | if (document.readyState !== "loading") {
|
89 |
| - loadCopyButton() |
| 121 | + loadCopyButton(); |
90 | 122 | } else {
|
91 |
| - document.addEventListener("DOMContentLoaded", loadCopyButton) |
| 123 | + document.addEventListener("DOMContentLoaded", loadCopyButton); |
92 | 124 | }
|
| 125 | + |
| 126 | +console.log("test!"); |
0 commit comments