feat(interview): 面经站上线,得物 Golang 一面 15 题
- 首页即汇总:左侧面经导航、中间正文、右侧本页题目目录;进 #/interview 默认打开第一篇 - 正文 15 题按原文整理、题号从 1 重排,答案用 details 折叠并留占位 - 主题与资料库共用 localStorage;窄屏左栏收成抽屉,打印时答案全展开 - 索引单源:assets/interviews.js 驱动左侧导航;portal.data.js 登记,README 与 interview/README.md 同步
This commit is contained in:
220
interview/assets/doc.js
Normal file
220
interview/assets/doc.js
Normal file
@@ -0,0 +1,220 @@
|
||||
/* ==========================================================================
|
||||
interview · 面经站交互
|
||||
--------------------------------------------------------------------------
|
||||
1. 主题:与资料库导航页共用 localStorage 里的选择
|
||||
2. 左侧面经导航:从 assets/interviews.js 渲染,高亮当前这篇
|
||||
3. 右侧「本页题目」:从页面里的 details.q 生成,滚动时高亮
|
||||
4. 折叠问答:展开计数、全部展开 / 收起、打印时全展开、#qN 直达
|
||||
==========================================================================*/
|
||||
(function () {
|
||||
"use strict";
|
||||
|
||||
var THEME_KEY = "materials.portal:theme";
|
||||
|
||||
function $(s, r) { return (r || document).querySelector(s); }
|
||||
function $$(s, r) { return Array.prototype.slice.call((r || document).querySelectorAll(s)); }
|
||||
function esc(s) {
|
||||
return String(s == null ? "" : s).replace(/[&<>"]/g, function (c) {
|
||||
return { "&": "&", "<": "<", ">": ">", '"': """ }[c];
|
||||
});
|
||||
}
|
||||
|
||||
/* ------------------------- 主题 ------------------------- */
|
||||
function applyTheme(t) {
|
||||
document.documentElement.setAttribute("data-theme", t);
|
||||
var b = $("#themeBtn");
|
||||
if (b) b.textContent = t === "dark" ? "浅色" : "深色";
|
||||
try { localStorage.setItem(THEME_KEY, t); } catch (e) {}
|
||||
}
|
||||
|
||||
function initTheme() {
|
||||
var t = document.documentElement.getAttribute("data-theme");
|
||||
if (t !== "light" && t !== "dark") t = "light";
|
||||
applyTheme(t);
|
||||
var b = $("#themeBtn");
|
||||
if (b) {
|
||||
b.addEventListener("click", function () {
|
||||
applyTheme(document.documentElement.getAttribute("data-theme") === "dark" ? "light" : "dark");
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------- 左侧:面经导航 ------------------- */
|
||||
function buildNav() {
|
||||
var host = $("#ivnav");
|
||||
if (!host || !window.INTERVIEWS) return;
|
||||
|
||||
var cur = document.body.getAttribute("data-interview") || "";
|
||||
var list = window.INTERVIEWS.slice().sort(function (a, b) {
|
||||
return String(b.date).localeCompare(String(a.date));
|
||||
});
|
||||
|
||||
host.innerHTML = list.map(function (it) {
|
||||
var date = String(it.date || "");
|
||||
var sub = [it.round, it.questions ? it.questions + " 题" : "", it.status || ""]
|
||||
.filter(Boolean).join(" · ");
|
||||
return '<a class="ivnav-a' + (it.id === cur ? " active" : "") + '" href="' + esc(it.file) + '"'
|
||||
+ (it.id === cur ? ' aria-current="page"' : "") + '>'
|
||||
+ '<span class="d"><b>' + esc(date.slice(5).replace("-", ".")) + '</b>'
|
||||
+ '<small>' + esc(date.slice(0, 4)) + '</small></span>'
|
||||
+ '<span class="m"><b>' + esc(it.company + " · " + it.role) + '</b>'
|
||||
+ '<small>' + esc(sub) + '</small></span>'
|
||||
+ '</a>';
|
||||
}).join("");
|
||||
|
||||
var n = $("#ivcount");
|
||||
if (n) n.textContent = list.length + " 篇";
|
||||
}
|
||||
|
||||
/* ------------------- 右侧:本页题目 ------------------- */
|
||||
function buildToc() {
|
||||
var box = $("#qtoc");
|
||||
var nav = $("#qtoc-nav");
|
||||
if (!box || !nav) return;
|
||||
|
||||
var qs = $$("details.q");
|
||||
if (!qs.length) { box.hidden = true; return; }
|
||||
|
||||
nav.innerHTML = qs.map(function (d) {
|
||||
var no = d.querySelector(".q-no");
|
||||
var tx = d.querySelector(".q-text");
|
||||
return '<a href="#' + d.id + '"><span class="n">' + esc(no ? no.textContent.trim() : "") + '</span>'
|
||||
+ '<span class="t">' + esc(tx ? tx.textContent.trim() : "") + '</span></a>';
|
||||
}).join("");
|
||||
|
||||
var n = $("#qtoc-n");
|
||||
if (n) n.textContent = qs.length;
|
||||
}
|
||||
|
||||
/* ------------------------- 折叠问答 ------------------------- */
|
||||
function initQA() {
|
||||
var qs = $$("details.q");
|
||||
if (!qs.length) return;
|
||||
|
||||
var countEl = $("[data-open-count]");
|
||||
var totalEl = $("[data-total-count]");
|
||||
if (totalEl) totalEl.textContent = qs.length;
|
||||
|
||||
function refreshCount() {
|
||||
if (!countEl) return;
|
||||
countEl.textContent = qs.filter(function (d) { return d.open; }).length;
|
||||
}
|
||||
|
||||
qs.forEach(function (d) { d.addEventListener("toggle", refreshCount); });
|
||||
refreshCount();
|
||||
|
||||
$$("[data-act]").forEach(function (btn) {
|
||||
btn.addEventListener("click", function () {
|
||||
var open = btn.getAttribute("data-act") === "expand";
|
||||
qs.forEach(function (d) { d.open = open; });
|
||||
refreshCount();
|
||||
});
|
||||
});
|
||||
|
||||
/* 打印 / 导出 PDF 时全部展开,打完恢复原本的展开状态 */
|
||||
var before = null;
|
||||
window.addEventListener("beforeprint", function () {
|
||||
before = qs.map(function (d) { return d.open; });
|
||||
qs.forEach(function (d) { d.open = true; });
|
||||
});
|
||||
window.addEventListener("afterprint", function () {
|
||||
if (!before) return;
|
||||
qs.forEach(function (d, i) { d.open = before[i]; });
|
||||
before = null;
|
||||
});
|
||||
|
||||
/* 地址栏带 #q3 打开时,把那一条也展开 */
|
||||
function openFromHash() {
|
||||
var h = location.hash.replace(/^#/, "");
|
||||
if (!h) return;
|
||||
var d = document.getElementById(h);
|
||||
if (d && d.tagName === "DETAILS") { d.open = true; refreshCount(); }
|
||||
}
|
||||
window.addEventListener("hashchange", openFromHash);
|
||||
openFromHash();
|
||||
}
|
||||
|
||||
/* ------------------- 右侧目录:滚动高亮 ------------------- */
|
||||
function spy() {
|
||||
var links = $$("#qtoc-nav a[href^='#']");
|
||||
if (!links.length) return;
|
||||
|
||||
var targets = links.map(function (a) {
|
||||
return document.getElementById(a.getAttribute("href").slice(1));
|
||||
}).filter(Boolean);
|
||||
if (!targets.length) return;
|
||||
|
||||
var y = window.scrollY + 150;
|
||||
var cur = targets[0];
|
||||
targets.forEach(function (el) {
|
||||
if (el.getBoundingClientRect().top + window.scrollY <= y) cur = el;
|
||||
});
|
||||
|
||||
links.forEach(function (a) {
|
||||
var on = a.getAttribute("href") === "#" + cur.id;
|
||||
a.classList.toggle("active", on);
|
||||
if (on) centerActive(a);
|
||||
});
|
||||
}
|
||||
|
||||
function centerActive(a) {
|
||||
var box = $("#qtoc");
|
||||
if (!box || box.scrollHeight <= box.clientHeight + 4) return;
|
||||
var r = a.getBoundingClientRect();
|
||||
var b = box.getBoundingClientRect();
|
||||
if (r.top < b.top + 8 || r.bottom > b.bottom - 8) {
|
||||
box.scrollTop += r.top - b.top - box.clientHeight * 0.35;
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------- 移动端抽屉 ------------------------- */
|
||||
function initDrawer() {
|
||||
var layout = $("#layout"), mask = $("#mask"), toggle = $("#tocToggle");
|
||||
if (!layout) return;
|
||||
if (toggle) {
|
||||
toggle.addEventListener("click", function () { layout.classList.toggle("toc-open"); });
|
||||
}
|
||||
if (mask) {
|
||||
mask.addEventListener("click", function () { layout.classList.remove("toc-open"); });
|
||||
}
|
||||
var sidebar = $(".sidebar");
|
||||
if (sidebar) {
|
||||
sidebar.addEventListener("click", function (e) {
|
||||
if (e.target.closest("a")) layout.classList.remove("toc-open");
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function initToTop() {
|
||||
var btn = $("#toTop");
|
||||
if (!btn) return;
|
||||
btn.addEventListener("click", function () {
|
||||
window.scrollTo({ top: 0, behavior: "smooth" });
|
||||
});
|
||||
function onScroll() { btn.classList.toggle("show", window.scrollY > 480); }
|
||||
window.addEventListener("scroll", onScroll, { passive: true });
|
||||
onScroll();
|
||||
}
|
||||
|
||||
function init() {
|
||||
initTheme();
|
||||
buildNav();
|
||||
buildToc();
|
||||
initQA();
|
||||
initDrawer();
|
||||
initToTop();
|
||||
spy();
|
||||
var ticking = false;
|
||||
window.addEventListener("scroll", function () {
|
||||
if (ticking) return;
|
||||
ticking = true;
|
||||
requestAnimationFrame(function () { ticking = false; spy(); });
|
||||
}, { passive: true });
|
||||
}
|
||||
|
||||
if (document.readyState === "loading") {
|
||||
document.addEventListener("DOMContentLoaded", init);
|
||||
} else {
|
||||
init();
|
||||
}
|
||||
})();
|
||||
Reference in New Issue
Block a user