3.9 KiB · text History 0297141
// Bundled to f/js/app.js by esbuild (see the Justfile). Importing htmx here
// registers it globally and processes hx-* attributes on load, which is what
// powers hx-boost navigation and the forum's in-place fragments (reactions,
// bookmarks, watch toggles, replies, tag editing, the notification badge).
import "htmx.org";

// Small toast helper available as window.toast(msg, {kind}) for ad-hoc use.
window.toast = (msg, opts = {}) => {
  let host = document.getElementById("toast-host");
  if (!host) {
    host = document.createElement("div");
    host.id = "toast-host";
    host.className = "toast toast-end z-50";
    document.body.appendChild(host);
  }
  const alert = document.createElement("div");
  alert.className = `alert alert-${opts.kind || "info"} shadow-lg`;
  const span = document.createElement("span");
  span.textContent = msg;
  alert.appendChild(span);
  host.appendChild(alert);
  const ttl = opts.ttl ?? 4000;
  if (ttl > 0) setTimeout(() => alert.remove(), ttl);
  return alert;
};

// Focus layout reader (templates/forum/_reader.html, and the Focus topic
// page forum/topic_focus.html): ←/→ or k/j step through
// the category's topics, Escape closes, as cartosnap's shared trip pages
// photos and pin_flip's lightbox. The buttons do the work (they swap #reader
// and push the URL), so the keys only click them. Ignored while typing.
const READER_KEYS = {
  ArrowRight: "reader-next",
  j: "reader-next",
  ArrowLeft: "reader-prev",
  k: "reader-prev",
  Escape: "reader-close",
};
document.addEventListener("keydown", (e) => {
  if (e.defaultPrevented || e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) return;
  // The reader overlay, or a topic page in the Focus layout.
  if (!document.querySelector("[data-reader]")) return;
  const t = e.target;
  if (t instanceof Element && t.closest("input, textarea, select, [contenteditable]")) return;
  const id = READER_KEYS[e.key];
  const el = id && document.getElementById(id);
  if (!el) return;
  e.preventDefault();
  el.click();
});

// After the reader swaps in: move focus into it, show the current topic in
// the filmstrip, and stop the page behind it scrolling. On close, give focus
// back to the tile that was open.
let readerOpener = null;
document.addEventListener("htmx:afterSettle", (e) => {
  const reader = document.querySelector("#reader [data-reader]");
  document.documentElement.classList.toggle("overflow-hidden", !!reader);
  if (reader) {
    const id = reader.dataset.reader;
    readerOpener = id;
    document.getElementById("reader-title")?.focus({ preventScroll: true });
    document.getElementById(`strip-${id}`)?.scrollIntoView({ block: "nearest", inline: "center" });
    document.getElementById("reader-posts")?.scrollTo(0, 0);
  } else if (readerOpener && e.detail.target?.id === "reader") {
    const tile = document.querySelector(`[data-topic="${readerOpener}"]`);
    readerOpener = null;
    tile?.focus({ preventScroll: false });
  }
});
// A reader opened from the URL (?topic=) on a full page load.
document.addEventListener("DOMContentLoaded", () => {
  const reader = document.querySelector("#reader [data-reader]");
  if (!reader) return;
  document.documentElement.classList.add("overflow-hidden");
  readerOpener = reader.dataset.reader;
  document.getElementById("reader-title")?.focus({ preventScroll: true });
  document.getElementById(`strip-${reader.dataset.reader}`)?.scrollIntoView({ block: "nearest", inline: "center" });
});
// Back/forward restores a snapshot without settling: re-sync the scroll lock.
document.addEventListener("htmx:historyRestore", () => {
  document.documentElement.classList.toggle("overflow-hidden", !!document.querySelector("#reader [data-reader]"));
});

// The shader background behind every page, and /shaders' previews and
// settings (shader_background.js).
import "./shader_background";

// Live updates for signed-in users (the htmx ws extension), and Popular's
// periodic refresh condition (live.js).
import "./live";