Init Main project
This commit is contained in:
commit
9e581ae67f
220 changed files with 253987 additions and 0 deletions
162
src/client/ui/tooltip.ts
Normal file
162
src/client/ui/tooltip.ts
Normal file
|
|
@ -0,0 +1,162 @@
|
|||
// So like, wikipedia. oh wait no I should've written that to glossary.ts
|
||||
import { TERMINOLOGY_GLOSSARY, type GlossaryKey } from "../glossary.js";
|
||||
import { clear, h } from "./dom.js";
|
||||
|
||||
let tooltipEl: HTMLElement | null = null;
|
||||
let tooltipAnchor: HTMLElement | null = null;
|
||||
let tooltipPinned = false;
|
||||
let tooltipInited = false;
|
||||
|
||||
export function hideTooltip(): void {
|
||||
tooltipPinned = false;
|
||||
if (tooltipAnchor) {
|
||||
tooltipAnchor.removeAttribute("aria-describedby");
|
||||
tooltipAnchor = null;
|
||||
}
|
||||
if (tooltipEl) {
|
||||
tooltipEl.hidden = true;
|
||||
}
|
||||
}
|
||||
|
||||
function ensureTooltipEl(): HTMLElement {
|
||||
if (tooltipEl && tooltipEl.isConnected) return tooltipEl;
|
||||
tooltipEl = h("div", {
|
||||
id: "hud-tooltip",
|
||||
class: "hud-tooltip",
|
||||
role: "tooltip",
|
||||
hidden: true,
|
||||
});
|
||||
document.body.appendChild(tooltipEl);
|
||||
return tooltipEl;
|
||||
}
|
||||
|
||||
function showTooltipFor(anchor: HTMLElement): void {
|
||||
const key = anchor.getAttribute("data-tip-key") as GlossaryKey | null;
|
||||
if (!key) return;
|
||||
const entry = TERMINOLOGY_GLOSSARY[key];
|
||||
if (!entry) return;
|
||||
const tip = ensureTooltipEl();
|
||||
clear(tip);
|
||||
tip.append(
|
||||
h("div", { class: "hud-tooltip-cat" }, entry.category),
|
||||
h("div", { class: "hud-tooltip-title" }, entry.title),
|
||||
h("div", { class: "hud-tooltip-body" }, entry.summary),
|
||||
);
|
||||
if (entry.formula) {
|
||||
tip.appendChild(h("div", { class: "hud-tooltip-formula" }, entry.formula));
|
||||
}
|
||||
tip.hidden = false;
|
||||
tip.style.left = "0px";
|
||||
tip.style.top = "0px";
|
||||
if (tooltipAnchor && tooltipAnchor !== anchor) {
|
||||
tooltipAnchor.removeAttribute("aria-describedby");
|
||||
}
|
||||
tooltipAnchor = anchor;
|
||||
anchor.setAttribute("aria-describedby", "hud-tooltip");
|
||||
const pad = 10;
|
||||
const rect = anchor.getBoundingClientRect();
|
||||
const tipRect = tip.getBoundingClientRect();
|
||||
const vw = window.innerWidth;
|
||||
const vh = window.innerHeight;
|
||||
let left = rect.left + rect.width / 2 - tipRect.width / 2;
|
||||
left = Math.max(pad, Math.min(vw - tipRect.width - pad, left));
|
||||
let top = rect.top - tipRect.height - 8;
|
||||
if (top < pad) {
|
||||
top = Math.min(vh - tipRect.height - pad, rect.bottom + 8);
|
||||
}
|
||||
tip.style.left = `${Math.round(left)}px`;
|
||||
tip.style.top = `${Math.round(Math.max(pad, top))}px`;
|
||||
}
|
||||
|
||||
export function initTooltipController(): void {
|
||||
if (tooltipInited) return;
|
||||
tooltipInited = true;
|
||||
document.addEventListener("pointerover", (ev) => {
|
||||
const target = (ev.target as HTMLElement | null)?.closest?.(
|
||||
".tip-badge",
|
||||
) as HTMLElement | null;
|
||||
if (target) {
|
||||
tooltipPinned = false;
|
||||
showTooltipFor(target);
|
||||
}
|
||||
});
|
||||
document.addEventListener("pointerout", (ev) => {
|
||||
if (tooltipPinned) return;
|
||||
const fromBadge = (ev.target as HTMLElement | null)?.closest?.(
|
||||
".tip-badge",
|
||||
) as HTMLElement | null;
|
||||
if (!fromBadge) return;
|
||||
const toEl = ev.relatedTarget as HTMLElement | null;
|
||||
if (toEl?.closest?.(".tip-badge, #hud-tooltip")) return;
|
||||
hideTooltip();
|
||||
});
|
||||
document.addEventListener("focusin", (ev) => {
|
||||
const target = (ev.target as HTMLElement | null)?.closest?.(
|
||||
".tip-badge",
|
||||
) as HTMLElement | null;
|
||||
if (target) {
|
||||
showTooltipFor(target);
|
||||
}
|
||||
});
|
||||
document.addEventListener("focusout", (ev) => {
|
||||
if (tooltipPinned) return;
|
||||
const fromBadge = (ev.target as HTMLElement | null)?.closest?.(
|
||||
".tip-badge",
|
||||
) as HTMLElement | null;
|
||||
if (fromBadge) hideTooltip();
|
||||
});
|
||||
document.addEventListener("click", (ev) => {
|
||||
const badge = (ev.target as HTMLElement | null)?.closest?.(
|
||||
".tip-badge",
|
||||
) as HTMLElement | null;
|
||||
if (badge) {
|
||||
ev.preventDefault();
|
||||
ev.stopPropagation();
|
||||
if (
|
||||
tooltipPinned &&
|
||||
tooltipAnchor === badge &&
|
||||
tooltipEl &&
|
||||
!tooltipEl.hidden
|
||||
) {
|
||||
hideTooltip();
|
||||
} else {
|
||||
tooltipPinned = true;
|
||||
showTooltipFor(badge);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (!(ev.target as HTMLElement | null)?.closest?.("#hud-tooltip")) {
|
||||
hideTooltip();
|
||||
}
|
||||
});
|
||||
document.addEventListener("keydown", (ev) => {
|
||||
if (ev.key === "Escape") hideTooltip();
|
||||
});
|
||||
window.addEventListener("scroll", () => hideTooltip(), {
|
||||
capture: true,
|
||||
passive: true,
|
||||
});
|
||||
}
|
||||
|
||||
export function term(
|
||||
label: string,
|
||||
key: GlossaryKey,
|
||||
extraClass?: string,
|
||||
): HTMLElement {
|
||||
const entry = TERMINOLOGY_GLOSSARY[key];
|
||||
return h(
|
||||
"span",
|
||||
{ class: extraClass ? `term-wrap ${extraClass}` : "term-wrap" },
|
||||
h("span", { class: "term-text" }, label),
|
||||
h(
|
||||
"button",
|
||||
{
|
||||
type: "button",
|
||||
class: "tip-badge",
|
||||
"data-tip-key": key,
|
||||
"aria-label": `Info: ${entry?.title ?? label}`,
|
||||
},
|
||||
"?",
|
||||
),
|
||||
);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue