diff --git a/docs/_static/copy.js b/docs/_static/copy.js new file mode 100644 index 000000000..e02100620 --- /dev/null +++ b/docs/_static/copy.js @@ -0,0 +1,31 @@ +const COPY = "fa-copy"; +const COPIED = "fa-clipboard-check"; + +const copy = async (obj) => { + //
the icon element
 code 
+ await navigator.clipboard.writeText(obj.children[1].innerText).then( + () => { + let icon = obj.children[0].children[0]; + icon.className = icon.className.replace(COPY, COPIED); + setTimeout(() => (icon.className = icon.className.replace(COPIED, COPY)), 2500); + }, + (r) => alert('Could not copy codeblock:\n' + r.toString()) + ); +}; + +document.addEventListener("DOMContentLoaded", () => { + let allCodeblocks = document.querySelectorAll("div[class='highlight']"); + + for (let codeblock of allCodeblocks) { + codeblock.parentNode.className += " relative-copy"; + let copyEl = document.createElement("span"); + copyEl.addEventListener('click', () => copy(codeblock)); + copyEl.className = "copy"; + + let copyIcon = document.createElement("i"); + copyIcon.className = "fas " + COPY; + copyEl.append(copyIcon); + + codeblock.prepend(copyEl); + } +}); diff --git a/docs/_static/style.css b/docs/_static/style.css index f3449dd74..e0789974c 100644 --- a/docs/_static/style.css +++ b/docs/_static/style.css @@ -143,7 +143,7 @@ label.toggle input:checked + span.toggle-slider:before { transform: translateX(18px); } - + div.related { padding: 10px 10px; width: 100%; @@ -703,4 +703,30 @@ div.code-block-caption { background-color: transparent; border-left: none; } -} \ No newline at end of file +} + +.relative-copy { + position: relative; +} + +.copy { + cursor: pointer; + position: absolute; + width: 16px; + height: 16px; + top: 0px; + right: 0px; + border: 1px solid #C6C9CB; + line-height: 0.8em; + font-size: 0.9em; + font-family: monospace; + padding-left: 0.2em; + padding-right: 0.2em; + border-radius: 0px 3px 0px 0px; + text-align: center; +} + +.copy i { + display: inline; + vertical-align: middle; +} diff --git a/docs/conf.py b/docs/conf.py index 90d52e4de..a4d35fbb5 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -320,5 +320,6 @@ texinfo_documents = [ def setup(app): app.add_js_file('custom.js') + app.add_js_file('copy.js') if app.config.language == 'ja': app.config.intersphinx_mapping['py'] = ('https://docs.python.org/ja/3', None)