add copy codeblock button
Apply suggestions from code review Co-authored-by: Danny <Rapptz@users.noreply.github.com> Change to icon, change according to slice's review
This commit is contained in:
		
							
								
								
									
										31
									
								
								docs/_static/copy.js
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								docs/_static/copy.js
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@@ -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
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										30
									
								
								docs/_static/style.css
									
									
									
									
										vendored
									
									
								
							@@ -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;
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user