extra.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Link tabs, as per https://facelessuser.github.io/pymdown-extensions/extensions/tabbed/#linked-tabs
  2. const savedCodeTab = localStorage.getItem("savedTab");
  3. const codeTabs = document.querySelectorAll(".tabbed-set > input");
  4. for (const tab of codeTabs) {
  5. tab.addEventListener("click", () => {
  6. const current = document.querySelector(`label[for=${tab.id}]`);
  7. const pos = current.getBoundingClientRect().top;
  8. const labelContent = current.innerHTML;
  9. const labels = document.querySelectorAll(".tabbed-set > label, .tabbed-alternate > .tabbed-labels > label");
  10. for (const label of labels) {
  11. if (label.innerHTML === labelContent) {
  12. document.querySelector(`input[id=${label.getAttribute("for")}]`).checked = true;
  13. }
  14. }
  15. // Preserve scroll position
  16. const delta = (current.getBoundingClientRect().top) - pos;
  17. window.scrollBy(0, delta);
  18. // Save
  19. localStorage.setItem("savedTab", labelContent);
  20. });
  21. // Select saved tab
  22. const current = document.querySelector(`label[for=${tab.id}]`);
  23. const labelContent = current.innerHTML;
  24. if (savedCodeTab === labelContent) {
  25. tab.checked = true;
  26. }
  27. }
  28. // Lightbox for screenshot
  29. const lightbox = document.createElement("div");
  30. lightbox.classList.add("lightbox");
  31. document.body.appendChild(lightbox);
  32. const showScreenshotOverlay = (e, el, group, index) => {
  33. lightbox.classList.add("show");
  34. document.addEventListener("keydown", nextScreenshotKeyboardListener);
  35. return showScreenshot(e, group, index);
  36. };
  37. const showScreenshot = (e, group, index) => {
  38. const actualIndex = resolveScreenshotIndex(group, index);
  39. lightbox.innerHTML = "<div class=\"close-lightbox\"></div>" + screenshots[group][actualIndex].innerHTML;
  40. lightbox.querySelector("img").onclick = (e) => {
  41. return showScreenshot(e, group, actualIndex + 1);
  42. };
  43. currentScreenshotGroup = group;
  44. currentScreenshotIndex = actualIndex;
  45. e.stopPropagation();
  46. return false;
  47. };
  48. const nextScreenshot = (e) => {
  49. return showScreenshot(e, currentScreenshotGroup, currentScreenshotIndex + 1);
  50. };
  51. const previousScreenshot = (e) => {
  52. return showScreenshot(e, currentScreenshotGroup, currentScreenshotIndex - 1);
  53. };
  54. const resolveScreenshotIndex = (group, index) => {
  55. if (index < 0) {
  56. return screenshots[group].length - 1;
  57. } else if (index > screenshots[group].length - 1) {
  58. return 0;
  59. }
  60. return index;
  61. };
  62. const hideScreenshotOverlay = (e) => {
  63. lightbox.classList.remove("show");
  64. document.removeEventListener("keydown", nextScreenshotKeyboardListener);
  65. };
  66. const nextScreenshotKeyboardListener = (e) => {
  67. switch (e.keyCode) {
  68. case 37:
  69. previousScreenshot(e);
  70. break;
  71. case 39:
  72. nextScreenshot(e);
  73. break;
  74. }
  75. };
  76. let currentScreenshotGroup = "";
  77. let currentScreenshotIndex = 0;
  78. let screenshots = {};
  79. Array.from(document.getElementsByClassName("screenshots")).forEach((sg) => {
  80. const group = sg.id;
  81. screenshots[group] = [...sg.querySelectorAll("a")];
  82. screenshots[group].forEach((el, index) => {
  83. el.onclick = (e) => {
  84. return showScreenshotOverlay(e, el, group, index);
  85. };
  86. });
  87. });
  88. lightbox.onclick = hideScreenshotOverlay;