illust-preview.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. function OpenPreviewer(url) {
  2. const viewer = document.createElement("div");
  3. viewer.style.cssText = `
  4. height: 100vh;
  5. width: 100vw;
  6. position: fixed;
  7. top: 0;
  8. left: 0;
  9. background: rgba(0,0,0,.8);
  10. display: flex;
  11. flex-direction: column;
  12. padding: 0 3rem;
  13. overflow: scroll;
  14. `;
  15. const imageLink = url.replace(/c\/\d+x\d+.*?\//, "").replace(/square1200/, "master1200");
  16. const img = document.createElement("img");
  17. img.src = imageLink;
  18. img.style.cssText = `
  19. margin: 3rem auto;
  20. max-width: 90%;
  21. max-height: 90%;
  22. `
  23. viewer.appendChild(img);
  24. document.body.appendChild(viewer);
  25. viewer.onclick = () => {
  26. document.body.removeChild(viewer);
  27. };
  28. }
  29. function AddOverlay() {
  30. // Check out `_layout.jet.html`
  31. const type = document.querySelector('#artworkPreview').innerHTML
  32. let className, html;
  33. if (type === "cover") {
  34. className = "overlay-cover";
  35. html = "";
  36. } else if (type === "button") {
  37. className = "overlay-button";
  38. html = "↗";
  39. } else {
  40. return;
  41. }
  42. document.querySelectorAll('.artwork-small .artwork-image img').forEach(illust => {
  43. const url = illust.getAttribute("src");
  44. const button = document.createElement('div');
  45. button.setAttribute("class", className);
  46. button.innerHTML = html;
  47. illust.parentElement.parentElement.appendChild(button);
  48. button.onclick = (e) => {
  49. OpenPreviewer(url);
  50. };
  51. })
  52. }
  53. addEventListener('htmx:afterSwap', function (event) {
  54. console.log("%o", event)
  55. AddOverlay();
  56. });
  57. // Initialize (it will only run one time)
  58. AddOverlay();