MtF-wiki/assets/customize.js

114 lines
3.4 KiB
JavaScript
Raw Normal View History

2022-09-03 18:25:56 +08:00
// #region anchor
2022-09-04 15:11:19 +08:00
(() => {
window.addEventListener("hashchange", onHashChange, { capture: true });
document.addEventListener("DOMContentLoaded", onHashChange);
document.addEventListener("click", onLinkClick, { capture: true });
2022-09-03 18:25:56 +08:00
2022-09-04 15:11:19 +08:00
function onHashChange() {
if (!location.hash) return;
const navbar = document.querySelector(".navbar__wrapper");
const element = document.querySelector(decodeURIComponent(location.hash));
const rect = element.getBoundingClientRect();
const marginTop = Number.parseInt(getComputedStyle(element).marginTop, 10)
scrollTo({
top: rect.top + scrollY - marginTop - navbar.clientHeight,
behavior: "smooth",
});
}
2022-09-03 18:25:56 +08:00
2022-09-04 15:11:19 +08:00
function onLinkClick(event) {
2022-09-03 18:25:56 +08:00
const target = event.target;
if (target.tagName !== "A") return;
if (!target.classList.contains("anchor")) return;
event.preventDefault();
event.stopPropagation();
2022-09-04 15:11:19 +08:00
history.replaceState(undefined, document.title, target.href);
onHashChange();
}
})();
2022-09-03 18:25:56 +08:00
// #endregion
2022-02-09 00:51:38 +08:00
document.querySelectorAll("a[href]").forEach((link) => {
2021-12-25 10:55:57 +08:00
if (!/^https?:$/.test(link.protocol)) return;
2021-12-25 06:56:39 +08:00
if (link.hostname === location.hostname) return;
2022-02-09 00:51:38 +08:00
link.target = "_blank";
2021-12-25 06:56:39 +08:00
});
2022-02-09 00:51:38 +08:00
document.querySelectorAll("a[data-email]").forEach((element) => {
2021-12-25 06:56:39 +08:00
element.href = atob(element.dataset.email);
delete element.dataset.email;
2021-12-24 18:12:54 +08:00
});
2022-09-03 18:25:56 +08:00
// #region weixin media platform qrcode
document.addEventListener(
"click",
(event) => {
2022-09-03 18:25:56 +08:00
if (event.target.tagName !== "A") return;
if (event.target.hostname !== "open.weixin.qq.com") return;
event.preventDefault();
event.stopPropagation();
const qrcode = document.createElement("img");
qrcode.src = event.target.href;
qrcode.width = 430;
qrcode.height = 430;
2022-09-03 18:25:56 +08:00
swal(event.target.title, { content: qrcode });
},
{ capture: true }
);
// #endregion
// #region hidden photo
document.addEventListener(
"click",
(event) => {
if (!event.target.classList.contains("mask")) return;
2022-09-04 15:11:19 +08:00
const photo = event.target.closest(".hidden-photo");
if (!photo) return;
2022-09-03 18:25:56 +08:00
photo.classList.add("show");
},
2022-09-03 18:25:56 +08:00
{ capture: true }
);
2022-08-22 00:51:59 +08:00
2022-09-03 18:25:56 +08:00
// #endregion
2023-01-16 08:52:17 +08:00
document.querySelectorAll('table').forEach((table) => {
2023-02-28 21:59:43 +08:00
if (table.hasAttribute('merge')) tableMerge(table.rows)
2023-01-16 08:52:17 +08:00
})
function tableMerge(rows) {
const refs = new Map()
// Merge cells in a *column* first.
for (let cellIndex = 0; cellIndex < rows[0].cells.length; cellIndex++) {
for (let rowIndex = 1; rowIndex < rows.length; rowIndex++) {
const cell = rows[rowIndex].cells[cellIndex]
const prevCell = rows[rowIndex - 1].cells[cellIndex]
if (cell.innerHTML !== prevCell.innerHTML) continue
const mergedTo = refs.get(prevCell) || prevCell
refs.set(cell, mergedTo)
mergedTo.rowSpan += cell.rowSpan
cell.hidden = true
}
}
// Merge cells in a *row* then.
for (const row of rows) {
for (let cellIndex = 1; cellIndex < row.cells.length; cellIndex++) {
const cell = row.cells[cellIndex]
const prevCell = row.cells[cellIndex - 1]
if (refs.has(cell) || refs.has(prevCell)) continue
if (cell.innerHTML !== prevCell.innerHTML) continue
const mergedTo = refs.get(prevCell) || prevCell
if (cell.rowSpan !== mergedTo.rowSpan) continue
mergedTo.colSpan += cell.colSpan
refs.set(cell, mergedTo)
cell.hidden = true
}
}
for (const cell of refs.keys()) {
cell.remove()
}
refs.clear()
}