diff --git a/docs/ldoc.css b/docs/ldoc.css index 3381d9cc4..d8564cfe1 100644 --- a/docs/ldoc.css +++ b/docs/ldoc.css @@ -649,3 +649,17 @@ pre .url { color: #272fc2; text-decoration: underline; } color: #00000044; margin-top: 15px; } + +.copy-link { + font-size: 9px; + padding: 2px; + border-radius: 9px; + vertical-align: middle; + text-decoration: none; +} +.copy-link--success { + background-color: green; +} +.copy-link--failure { + background-color: red; +} diff --git a/docs/ldoc.ltp b/docs/ldoc.ltp index c306780f7..94255cb17 100644 --- a/docs/ldoc.ltp +++ b/docs/ldoc.ltp @@ -339,7 +339,7 @@ # end # for item in iter(k.items) do if not item.tags.hidden then
- + 🔗 $(display_name(item)) # if item.display_inheritance then @@ -559,6 +559,49 @@ target.classList.remove("open"); } }); + + const copyResultClasses = { + success : "copy-link--success", + failure: "copy-link--failure" + }; + + const removeCopyResultClasses = ($target) => + Object.values(copyResultClasses).forEach(c => $target.classList.remove(c)); + + document.querySelectorAll(".js-copy-link").forEach(copyLink => { + copyLink.addEventListener("click", function(e) { + e.preventDefault(); + const $target = e.target; + + removeCopyResultClasses($target); + + let link = $target.href; + if (!link) { + return; + } + if (link.startsWith("#")) { + const curr = window.location.pathname; + link = curr.substring(0, curr.indexOf("#")) + link; + } + + // We need to create a fake element to copy the text from + const fakeElement = document.createElement("textarea"); + fakeElement.value = link; + document.body.appendChild(fakeElement); + fakeElement.select(); + + let success = false; + try { + success = document.execCommand("copy"); + } catch(err) { + success = false; + } + + fakeElement.remove(); + $target.classList.add(success ? copyResultClasses.success : copyResultClasses.failure); + setInterval(() => removeCopyResultClasses($target), 1500); + }); + });