summary refs log tree commit diff stats
path: root/tools/dochack
diff options
context:
space:
mode:
authorAbishek PY <43115551+vj-abishek@users.noreply.github.com>2021-10-22 15:40:32 +0530
committerGitHub <noreply@github.com>2021-10-22 12:10:32 +0200
commitd6345874236e03d21f4f6acbad34546ce27052b7 (patch)
tree34e93fa6b74492f9d865bd89701692bb827c9b19 /tools/dochack
parent5e2b9341f343c3f80f92cfd160a0186f23521a42 (diff)
downloadNim-d6345874236e03d21f4f6acbad34546ce27052b7.tar.gz
feat: copy to clipboard (#18963)
* feat: copy to clipboard

* fix: CI failure related issue

* fix: CI failure issue

* fix: copy to clipboard button bug

* feat: copy pragmadots value to clipboard
Diffstat (limited to 'tools/dochack')
-rw-r--r--tools/dochack/dochack.nim56
1 files changed, 56 insertions, 0 deletions
diff --git a/tools/dochack/dochack.nim b/tools/dochack/dochack.nim
index 4a491cf88..83e81f1c0 100644
--- a/tools/dochack/dochack.nim
+++ b/tools/dochack/dochack.nim
@@ -341,3 +341,59 @@ proc search*() {.exportc.} =
 
   if timer != nil: clearTimeout(timer)
   timer = setTimeout(wrapper, 400)
+
+proc copyToClipboard*() {.exportc.} =
+    {.emit: """
+
+    function updatePreTags() {
+
+      const allPreTags = document.querySelectorAll("pre")
+    
+      allPreTags.forEach((e) => {
+      
+          const div = document.createElement("div")
+          div.classList.add("copyToClipBoard")
+    
+          const preTag = document.createElement("pre")
+          preTag.innerHTML = e.innerHTML
+    
+          const button = document.createElement("button")
+          button.value = e.textContent.replace('...', '') 
+          button.classList.add("copyToClipBoardBtn")
+    
+          div.appendChild(preTag)
+          div.appendChild(button)
+    
+          e.outerHTML = div.outerHTML
+      
+      })
+    }
+
+
+    function copyTextToClipboard(e) {
+        const clipBoardContent = e.target.value
+        navigator.clipboard.writeText(clipBoardContent).then(function() {
+            e.target.style.setProperty("--clipboard-image", "var(--clipboard-image-selected)")
+        }, function(err) {
+            console.error("Could not copy text: ", err);
+        });
+    }
+
+    window.addEventListener("click", (e) => {
+        if (e.target.classList.contains("copyToClipBoardBtn")) {
+            copyTextToClipboard(e)
+          }
+    })
+
+    window.addEventListener("mouseover", (e) => {
+        if (e.target.nodeName === "PRE") {
+            e.target.nextElementSibling.style.setProperty("--clipboard-image", "var(--clipboard-image-normal)")
+        }
+    })
+    
+    window.addEventListener("DOMContentLoaded", updatePreTags)
+
+    """
+    .}
+    
+copyToClipboard()
\ No newline at end of file