clipboard.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /**
  2. The code below is based on the Doxygen Awesome project, see
  3. https://github.com/jothepro/doxygen-awesome-css
  4. MIT License
  5. Copyright (c) 2021 - 2022 jothepro
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in all
  13. copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. SOFTWARE.
  21. */
  22. let clipboard_title = "Copy to clipboard"
  23. let clipboard_icon = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24"><path d="M0 0h24v24H0V0z" fill="none"/><path d="M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z"/></svg>`
  24. let clipboard_successIcon = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24"><path d="M0 0h24v24H0V0z" fill="none"/><path d="M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41L9 16.17z"/></svg>`
  25. let clipboard_successDuration = 1000
  26. $(function() {
  27. if(navigator.clipboard) {
  28. const fragments = document.getElementsByClassName("fragment")
  29. for(const fragment of fragments) {
  30. const clipboard_div = document.createElement("div")
  31. clipboard_div.classList.add("clipboard")
  32. clipboard_div.innerHTML = clipboard_icon
  33. clipboard_div.title = clipboard_title
  34. $(clipboard_div).click(function() {
  35. const content = this.parentNode.cloneNode(true)
  36. // filter out line number and folded fragments from file listings
  37. content.querySelectorAll(".lineno, .ttc, .foldclosed").forEach((node) => { node.remove() })
  38. let text = content.textContent
  39. // remove trailing newlines and trailing spaces from empty lines
  40. text = text.replace(/^\s*\n/gm,'\n').replace(/\n*$/,'')
  41. navigator.clipboard.writeText(text);
  42. this.classList.add("success")
  43. this.innerHTML = clipboard_successIcon
  44. window.setTimeout(() => { // switch back to normal icon after timeout
  45. this.classList.remove("success")
  46. this.innerHTML = clipboard_icon
  47. }, clipboard_successDuration);
  48. })
  49. fragment.insertBefore(clipboard_div, fragment.firstChild)
  50. }
  51. }
  52. })