// resources/js/modules/term-tooltip.js // 原 term-tooltip.js 移至此处(从 js/ 根目录移入 modules/)。 // 所有阅读页公用(tipitaka read、anthology read、wiki show、blog show)。 // 在 reader.js 中 import,不在 app.js 全局加载(仅阅读页需要)。 // // 文件内容:将现有 term-tooltip.js 内容直接复制至此,不做修改。 // 待 reader.js 建立后在此 import: // import './modules/term-tooltip'; import * as bootstrap from "bootstrap"; (function () { "use strict"; // ── 缓存层 ──────────────────────────────────────────────────────── const cache = {}; // 下面的模板用 innerHTML 拼接,取回的文字先转义 const escapeHtml = (text) => String(text ?? "").replace(/[&<>"']/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'", })[c]); // 百科页上点术语直接跳转;其它页面(阅读页)不跳走,「查看完整条目」在新标签页打开 const onWikiPage = /^\/library\/wiki\//.test(window.location.pathname); // 词条的百科页地址:语言取当前百科页的,其它页面取 function wikiUrl(id) { const m = window.location.pathname.match(/^\/library\/wiki\/([^/]+)/); const lang = m ? decodeURIComponent(m[1]) : document.documentElement.lang || "zh-Hans"; return `/library/wiki/${encodeURIComponent(lang)}/${encodeURIComponent(id)}`; } const linkTarget = onWikiPage ? "" : ' target="_blank" rel="noopener"'; // v3:summary_text 是纯文本摘要(有摘要用摘要,没有取正文第一段) async function fetchTerm(id) { if (cache[id]) return cache[id]; const res = await fetch(`/api/v3/terms/${id}`, { headers: { Accept: "application/json" }, }); if (!res.ok) throw new Error(`fetchTerm ${id} failed: ${res.status}`); const { data } = await res.json(); cache[id] = { word: escapeHtml(data.word), meaning: escapeHtml(data.meaning), summary: escapeHtml(data.summary_text), url: wikiUrl(id), }; return cache[id]; } // ── 设备判断 ────────────────────────────────────────────────────── const isMobile = () => window.innerWidth < 768; // ── Skeleton 模板 ───────────────────────────────────────────────── function buildSkeletonContent() { return `