283 lines
9.1 KiB
JavaScript
283 lines
9.1 KiB
JavaScript
document.addEventListener("DOMContentLoaded", () => {
|
||
document.querySelectorAll("a[href]").forEach((link) => {
|
||
link.addEventListener("mouseenter", () => {
|
||
link.title = link.getAttribute("href") || "";
|
||
});
|
||
});
|
||
|
||
document.querySelectorAll(".code-block").forEach((block) => {
|
||
const code = block.querySelector("pre code");
|
||
if (!code) {
|
||
return;
|
||
}
|
||
|
||
const rawText = code.innerText || code.textContent || "";
|
||
const lines = rawText.replace(/\n$/, "").split("\n");
|
||
if (lines.length > 1 && !code.dataset.lineNumbered) {
|
||
code.dataset.lineNumbered = "true";
|
||
const htmlLines = code.innerHTML.replace(/\n$/, "").split("\n");
|
||
code.innerHTML = htmlLines
|
||
.map((line, index) => `<span class="code-line" data-line="${index + 1}">${line || " "}</span>`)
|
||
.join("\n");
|
||
}
|
||
|
||
if (lines.length > 18) {
|
||
block.classList.add("is-collapsible", "is-collapsed");
|
||
const toggle = document.createElement("button");
|
||
toggle.type = "button";
|
||
toggle.className = "toggle-code-button";
|
||
toggle.textContent = "展开";
|
||
toggle.addEventListener("click", () => {
|
||
const collapsed = block.classList.toggle("is-collapsed");
|
||
toggle.textContent = collapsed ? "展开" : "收起";
|
||
});
|
||
block.appendChild(toggle);
|
||
}
|
||
|
||
const button = document.createElement("button");
|
||
button.type = "button";
|
||
button.className = "copy-code-button";
|
||
button.textContent = "复制";
|
||
|
||
button.addEventListener("click", async () => {
|
||
const text = rawText;
|
||
try {
|
||
await navigator.clipboard.writeText(text);
|
||
button.textContent = "已复制";
|
||
} catch (error) {
|
||
const textarea = document.createElement("textarea");
|
||
textarea.value = text;
|
||
textarea.style.position = "fixed";
|
||
textarea.style.left = "-9999px";
|
||
document.body.appendChild(textarea);
|
||
textarea.select();
|
||
document.execCommand("copy");
|
||
textarea.remove();
|
||
button.textContent = "已复制";
|
||
}
|
||
window.setTimeout(() => {
|
||
button.textContent = "复制";
|
||
}, 1200);
|
||
});
|
||
|
||
block.appendChild(button);
|
||
});
|
||
|
||
document.querySelectorAll(".media-block audio, .media-block video").forEach((player) => {
|
||
const figure = player.closest(".media-block");
|
||
const source = figure?.dataset.mediaSrc || player.currentSrc || player.src || "";
|
||
if (!figure || !source) {
|
||
return;
|
||
}
|
||
|
||
const fallback = document.createElement("div");
|
||
fallback.className = "media-fallback";
|
||
fallback.innerHTML = `内置播放器无法播放此媒体。<a href="${source}">用系统播放器打开</a>`;
|
||
figure.appendChild(fallback);
|
||
|
||
const showFallback = () => {
|
||
fallback.classList.add("is-visible");
|
||
};
|
||
|
||
player.addEventListener("error", showFallback);
|
||
player.addEventListener("stalled", showFallback);
|
||
player.addEventListener("emptied", showFallback);
|
||
player.querySelectorAll("source").forEach((sourceElement) => {
|
||
sourceElement.addEventListener("error", showFallback);
|
||
});
|
||
|
||
window.setTimeout(() => {
|
||
if (player.error || player.networkState === HTMLMediaElement.NETWORK_NO_SOURCE) {
|
||
showFallback();
|
||
}
|
||
}, 700);
|
||
});
|
||
|
||
const copyText = async (text) => {
|
||
try {
|
||
await navigator.clipboard.writeText(text);
|
||
} catch (error) {
|
||
const textarea = document.createElement("textarea");
|
||
textarea.value = text;
|
||
textarea.style.position = "fixed";
|
||
textarea.style.left = "-9999px";
|
||
document.body.appendChild(textarea);
|
||
textarea.select();
|
||
document.execCommand("copy");
|
||
textarea.remove();
|
||
}
|
||
};
|
||
|
||
document.querySelectorAll(".reader table").forEach((table) => {
|
||
if (table.closest(".table-wrapper")) {
|
||
return;
|
||
}
|
||
|
||
const wrapper = document.createElement("div");
|
||
wrapper.className = "table-wrapper";
|
||
|
||
const tools = document.createElement("div");
|
||
tools.className = "table-tools";
|
||
|
||
const copyButton = document.createElement("button");
|
||
copyButton.type = "button";
|
||
copyButton.className = "copy-table-button";
|
||
copyButton.textContent = "复制表格";
|
||
|
||
copyButton.addEventListener("click", async () => {
|
||
const rows = [...table.querySelectorAll("tr")].map((row) =>
|
||
[...row.querySelectorAll("th, td")]
|
||
.map((cell) => (cell.innerText || cell.textContent || "").replace(/\s+/g, " ").trim())
|
||
.join("\t")
|
||
);
|
||
await copyText(rows.join("\n"));
|
||
copyButton.textContent = "已复制";
|
||
window.setTimeout(() => {
|
||
copyButton.textContent = "复制表格";
|
||
}, 1200);
|
||
});
|
||
|
||
tools.appendChild(copyButton);
|
||
table.parentNode.insertBefore(wrapper, table);
|
||
wrapper.appendChild(tools);
|
||
wrapper.appendChild(table);
|
||
});
|
||
|
||
const lightbox = document.createElement("div");
|
||
lightbox.className = "image-lightbox";
|
||
lightbox.innerHTML = `
|
||
<div class="image-lightbox__toolbar">
|
||
<button type="button" data-action="zoom-out" title="缩小">-</button>
|
||
<button type="button" data-action="zoom-in" title="放大">+</button>
|
||
<button type="button" data-action="rotate-left" title="向左旋转">↺</button>
|
||
<button type="button" data-action="rotate-right" title="向右旋转">↻</button>
|
||
<button type="button" data-action="reset" title="重置">1:1</button>
|
||
<button type="button" data-action="close" title="关闭">×</button>
|
||
</div>
|
||
<img alt="">
|
||
<div class="image-lightbox__caption"></div>
|
||
`;
|
||
document.body.appendChild(lightbox);
|
||
|
||
const preview = lightbox.querySelector("img");
|
||
const caption = lightbox.querySelector(".image-lightbox__caption");
|
||
const toolbar = lightbox.querySelector(".image-lightbox__toolbar");
|
||
let scale = 1;
|
||
let rotation = 0;
|
||
|
||
const updatePreviewTransform = () => {
|
||
preview.style.transform = `scale(${scale}) rotate(${rotation}deg)`;
|
||
};
|
||
|
||
const resetPreviewTransform = () => {
|
||
scale = 1;
|
||
rotation = 0;
|
||
updatePreviewTransform();
|
||
};
|
||
|
||
const closeLightbox = () => {
|
||
lightbox.classList.remove("is-open");
|
||
preview.removeAttribute("src");
|
||
caption.textContent = "";
|
||
resetPreviewTransform();
|
||
};
|
||
|
||
document.querySelectorAll(".reader img").forEach((image) => {
|
||
image.addEventListener("click", () => {
|
||
preview.src = image.currentSrc || image.src;
|
||
preview.alt = image.alt || "";
|
||
caption.textContent = image.alt || image.title || "";
|
||
resetPreviewTransform();
|
||
lightbox.classList.add("is-open");
|
||
});
|
||
});
|
||
|
||
preview.addEventListener("click", (event) => {
|
||
event.stopPropagation();
|
||
});
|
||
|
||
preview.addEventListener("wheel", (event) => {
|
||
event.preventDefault();
|
||
scale = Math.max(0.25, Math.min(5, scale + (event.deltaY < 0 ? 0.12 : -0.12)));
|
||
updatePreviewTransform();
|
||
});
|
||
|
||
toolbar.addEventListener("click", (event) => {
|
||
event.stopPropagation();
|
||
const action = event.target?.dataset?.action;
|
||
if (action === "zoom-in") {
|
||
scale = Math.min(5, scale + 0.2);
|
||
} else if (action === "zoom-out") {
|
||
scale = Math.max(0.25, scale - 0.2);
|
||
} else if (action === "rotate-left") {
|
||
rotation -= 90;
|
||
} else if (action === "rotate-right") {
|
||
rotation += 90;
|
||
} else if (action === "reset") {
|
||
resetPreviewTransform();
|
||
return;
|
||
} else if (action === "close") {
|
||
closeLightbox();
|
||
return;
|
||
}
|
||
updatePreviewTransform();
|
||
});
|
||
|
||
lightbox.addEventListener("click", closeLightbox);
|
||
document.addEventListener("keydown", (event) => {
|
||
if (event.key === "Escape") {
|
||
closeLightbox();
|
||
} else if (lightbox.classList.contains("is-open") && (event.key === "+" || event.key === "=")) {
|
||
scale = Math.min(5, scale + 0.2);
|
||
updatePreviewTransform();
|
||
} else if (lightbox.classList.contains("is-open") && event.key === "-") {
|
||
scale = Math.max(0.25, scale - 0.2);
|
||
updatePreviewTransform();
|
||
} else if (lightbox.classList.contains("is-open") && event.key === "0") {
|
||
resetPreviewTransform();
|
||
}
|
||
});
|
||
|
||
const headingElements = [...document.querySelectorAll("h1[id], h2[id], h3[id], h4[id], h5[id], h6[id]")];
|
||
let bridge = null;
|
||
let ticking = false;
|
||
|
||
const reportScroll = () => {
|
||
ticking = false;
|
||
const documentElement = document.documentElement;
|
||
const maxScroll = Math.max(1, documentElement.scrollHeight - window.innerHeight);
|
||
const progress = Math.max(0, Math.min(100, (window.scrollY / maxScroll) * 100));
|
||
|
||
let activeId = "";
|
||
for (const heading of headingElements) {
|
||
if (heading.getBoundingClientRect().top <= 96) {
|
||
activeId = heading.id;
|
||
} else {
|
||
break;
|
||
}
|
||
}
|
||
|
||
if (bridge && bridge.reportScroll) {
|
||
bridge.reportScroll(progress, activeId);
|
||
}
|
||
};
|
||
|
||
const scheduleReport = () => {
|
||
if (!ticking) {
|
||
ticking = true;
|
||
window.requestAnimationFrame(reportScroll);
|
||
}
|
||
};
|
||
|
||
if (window.qt && window.qt.webChannelTransport) {
|
||
new QWebChannel(window.qt.webChannelTransport, (channel) => {
|
||
bridge = channel.objects.mdhelperBridge;
|
||
reportScroll();
|
||
});
|
||
}
|
||
|
||
window.addEventListener("scroll", scheduleReport, { passive: true });
|
||
window.addEventListener("resize", scheduleReport);
|
||
reportScroll();
|
||
});
|