functions.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. (function () {
  2. var e = document.querySelector(".menu-open");
  3. e &&
  4. e.addEventListener("click", function () {
  5. document.body.classList.contains("is-menu-open") ? document.body.classList.remove("is-menu-open") : document.body.classList.add("is-menu-open");
  6. });
  7. var c = document.querySelector(".menu-close");
  8. c &&
  9. c.addEventListener("click", function () {
  10. document.body.classList.contains("is-menu-open") ? document.body.classList.remove("is-menu-open") : document.body.classList.add("is-menu-open");
  11. });
  12. var s = document.querySelector(".search-open");
  13. s &&
  14. s.addEventListener("click", function () {
  15. document.body.classList.contains("is-search-open") ? document.body.classList.remove("is-search-open") : document.body.classList.add("is-search-open");
  16. });
  17. var sc = document.querySelector(".search-close");
  18. sc &&
  19. sc.addEventListener("click", function () {
  20. document.body.classList.contains("is-search-open") ? document.body.classList.remove("is-search-open") : document.body.classList.add("is-search-open");
  21. });
  22. })();
  23. /* Copy to clipboard button on code blocks */
  24. (function () {
  25. function icon(paths) {
  26. return (
  27. '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" ' +
  28. 'stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">' +
  29. paths +
  30. "</svg>"
  31. );
  32. }
  33. var copyIcon = icon('<rect x="8" y="8" width="14" height="14" rx="2" ry="2"/><path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2"/>');
  34. var doneIcon = icon('<path d="M20 6 9 17l-5-5"/>');
  35. var failIcon = icon('<path d="M18 6 6 18"/><path d="m6 6 12 12"/>');
  36. if (!navigator.clipboard) return;
  37. document.querySelectorAll("pre").forEach(function (block) {
  38. var code = block.querySelector("code");
  39. if (!code) return;
  40. var button = document.createElement("button");
  41. button.className = "copy-code";
  42. button.type = "button";
  43. button.innerHTML = copyIcon;
  44. button.title = "Copy code";
  45. button.setAttribute("aria-label", "Copy code to clipboard");
  46. block.appendChild(button);
  47. button.addEventListener("click", function () {
  48. navigator.clipboard.writeText(code.innerText).then(
  49. function () {
  50. button.innerHTML = doneIcon;
  51. button.title = "Copied!";
  52. },
  53. function () {
  54. button.innerHTML = failIcon;
  55. button.title = "Copy failed";
  56. }
  57. );
  58. setTimeout(function () {
  59. button.innerHTML = copyIcon;
  60. button.title = "Copy code";
  61. }, 1000);
  62. });
  63. // Keep the button in place while the block scrolls horizontally.
  64. block.addEventListener("scroll", function () {
  65. button.style.transform = "translateX(" + block.scrollLeft + "px)";
  66. });
  67. });
  68. })();