Browse Source

add copy codeblock button

Apply suggestions from code review

Co-authored-by: Danny <[email protected]>

Change to icon, change according to slice's review
pull/6176/head
NCPlayz 5 years ago
committed by Rapptz
parent
commit
ce0d0a20dd
  1. 31
      docs/_static/copy.js
  2. 30
      docs/_static/style.css
  3. 1
      docs/conf.py

31
docs/_static/copy.js

@ -0,0 +1,31 @@
const COPY = "fa-copy";
const COPIED = "fa-clipboard-check";
const copy = async (obj) => {
// <div><span class="copy"> <i class="fas ...">the icon element</i> </span><pre> code </pre></div>
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);
}
});

30
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;
}
}
}
.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;
}

1
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)

Loading…
Cancel
Save