about summary refs log tree commit diff
path: root/src/librustdoc/html/static/js
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume.gomez@huawei.com>2024-07-29 13:58:15 +0200
committerGuillaume Gomez <guillaume.gomez@huawei.com>2024-07-29 21:14:35 +0200
commita7cb1a5352e648bcf5ffb1403b6535275a6c511d (patch)
treecd4b7049db619ef503bf7265d66dbb930d73c607 /src/librustdoc/html/static/js
parent80d8270d8488957f62fbf0df7a19dfe596be92ac (diff)
downloadrust-a7cb1a5352e648bcf5ffb1403b6535275a6c511d.tar.gz
rust-a7cb1a5352e648bcf5ffb1403b6535275a6c511d.zip
Make the buttons remain when code example is clicked
Diffstat (limited to 'src/librustdoc/html/static/js')
-rw-r--r--src/librustdoc/html/static/js/main.js25
1 files changed, 23 insertions, 2 deletions
diff --git a/src/librustdoc/html/static/js/main.js b/src/librustdoc/html/static/js/main.js
index 40d65ae7910..e0ea234f9e7 100644
--- a/src/librustdoc/html/static/js/main.js
+++ b/src/librustdoc/html/static/js/main.js
@@ -1829,14 +1829,22 @@ href="https://doc.rust-lang.org/${channel}/rustdoc/read-documentation/search.htm
         copyContentToClipboard(codeElem.textContent);
     }
 
-    function addCopyButton(event) {
+    function getExampleWrap(event) {
         let elem = event.target;
         while (!hasClass(elem, "example-wrap")) {
             elem = elem.parentElement;
             if (elem.tagName === "body" || hasClass(elem, "docblock")) {
-                return;
+                return null;
             }
         }
+        return elem;
+    }
+
+    function addCopyButton(event) {
+        const elem = getExampleWrap(event);
+        if (elem === null) {
+            return;
+        }
         // Since the button will be added, no need to keep this listener around.
         elem.removeEventListener("mouseover", addCopyButton);
 
@@ -1858,7 +1866,20 @@ href="https://doc.rust-lang.org/${channel}/rustdoc/read-documentation/search.htm
         parent.appendChild(copyButton);
     }
 
+    function showHideCodeExampleButtons(event) {
+        const elem = getExampleWrap(event);
+        if (elem === null) {
+            return;
+        }
+        const buttons = elem.querySelector(".button-holder");
+        if (buttons === null) {
+            return;
+        }
+        buttons.classList.toggle("keep-visible");
+    }
+
     onEachLazy(document.querySelectorAll(".docblock .example-wrap"), elem => {
         elem.addEventListener("mouseover", addCopyButton);
+        elem.addEventListener("click", showHideCodeExampleButtons);
     });
 }());