docs.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /* globals i18n */
  2. if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
  3. document.head.insertAdjacentHTML('beforeend', `
  4. <meta name="darkreader-lock">
  5. `);
  6. }
  7. let asideNav;
  8. window.addEventListener('beforeunload', () => {
  9. sessionStorage.setItem('sidebar_scrollTop', asideNav.scrollTop);
  10. });
  11. window.addEventListener('keydown', (e) => {
  12. if (e.key === 'Escape') {
  13. location.hash = 'close';
  14. }
  15. });
  16. document.addEventListener('DOMContentLoaded', () => {
  17. asideNav = document.querySelector('aside > nav.docs');
  18. const sidebar_scrollTop = sessionStorage.getItem('sidebar_scrollTop');
  19. if (sidebar_scrollTop) {
  20. asideNav.scrollTo(0, sidebar_scrollTop);
  21. sessionStorage.removeItem('sidebar_scrollTop');
  22. }
  23. for (const el of document.querySelectorAll('div.highlight')) {
  24. /* eslint-disable @stylistic/max-len */
  25. el.insertAdjacentHTML('afterbegin', `
  26. <button class="copy" title="${i18n.copy_to_clipboard}">
  27. <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" viewBox="0 0 16 16">
  28. <path d="M4 1.5H3a2 2 0 0 0-2 2V14a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V3.5a2 2 0 0 0-2-2h-1v1h1a1 1 0 0 1 1 1V14a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V3.5a1 1 0 0 1 1-1h1z"/>
  29. <path d="M9.5 1a.5.5 0 0 1 .5.5v1a.5.5 0 0 1-.5.5h-3a.5.5 0 0 1-.5-.5v-1a.5.5 0 0 1 .5-.5zm-3-1A1.5 1.5 0 0 0 5 1.5v1A1.5 1.5 0 0 0 6.5 4h3A1.5 1.5 0 0 0 11 2.5v-1A1.5 1.5 0 0 0 9.5 0z"/>
  30. </svg>
  31. </button>
  32. `);
  33. /* eslint-enable @stylistic/max-len */
  34. const copyBtn = el.querySelector('button.copy');
  35. copyBtn.addEventListener('click', () => {
  36. const snippet = el.querySelector('code').innerText;
  37. if (navigator.clipboard) {
  38. navigator.clipboard.writeText(snippet);
  39. } else {
  40. // Fallback if no HTTPS
  41. const input = document.createElement('textarea');
  42. input.innerHTML = snippet;
  43. document.body.append(input);
  44. input.select();
  45. document.execCommand('copy');
  46. input.remove();
  47. }
  48. });
  49. }
  50. for (const el of document.querySelectorAll('img')) {
  51. if (el.parentNode.tagName !== 'A') {
  52. el.outerHTML = `<a href="${el.getAttribute('src')}">${el.outerHTML}</a>`;
  53. }
  54. }
  55. });