初始化政体志子站

收录 zhengquan-site 前后端完整代码:24 朝数据、24 幅权力结构图、
5 条沿革(67 节点 / 269 条细释)、44 个词条,以及部署配置与接口测试。
This commit is contained in:
袁永志 committed 2026-11-19 11:59:51 +08:00
commit aa12b9f910
58 files changed
+16253

No files matched your search

+119
View File
@@ -0,0 +1,119 @@
import { getBootstrap } from './api.js';
import { setBootstrap } from './store.js';
import { shell } from './layout.js';
import { parseHash, setTitle, scrollTop } from './util.js';
import { renderHome } from './views/home.js';
import { renderDynasty } from './views/dynasty.js';
import { renderThreads } from './views/threads.js';
import { renderThread } from './views/thread.js';
import { renderGlossary } from './views/glossary.js';
import { renderSearch } from './views/search.js';
const ROUTES = [
{ re: /^\/$/, view: renderHome },
{ re: /^\/dynasty\/([\w-]+)$/, view: renderDynasty, keys: ['slug'] },
{ re: /^\/threads$/, view: renderThreads },
{ re: /^\/thread\/([\w-]+)$/, view: renderThread, keys: ['slug'] },
{ re: /^\/glossary$/, view: renderGlossary },
{ re: /^\/search$/, view: renderSearch },
];
const app = document.getElementById('app');
let token = 0;
function notFound() {
return `
<div class="error-box">
<h1>此卷不存</h1>
<p>所寻的页面没有找到。可回到总目,或从检索入手。</p>
<a class="btn" href="#/">回到总目</a>
</div>`;
}
function errorView(message) {
return `
<div class="error-box">
<h1>展卷未成</h1>
<p>${message}</p>
<a class="btn" href="#/">回到总目</a>
</div>`;
}
async function render() {
const my = ++token;
const { path, query } = parseHash();
let match = null;
for (const route of ROUTES) {
const m = path.match(route.re);
if (m) {
match = { route, m };
break;
}
}
if (!match) {
app.innerHTML = shell(notFound(), path);
setTitle(['此卷不存']);
scrollTop();
return;
}
const params = {};
(match.route.keys || []).forEach((key, i) => {
params[key] = decodeURIComponent(match.m[i + 1]);
});
app.style.opacity = '.55';
try {
const result = await match.route.view({ params, query });
if (my !== token) return;
app.innerHTML = shell(result.html, path);
app.style.opacity = '';
setTitle([result.title]);
if (typeof result.mount === 'function') result.mount();
scrollTop();
} catch (err) {
if (my !== token) return;
console.error(err);
app.style.opacity = '';
app.innerHTML = shell(errorView(err.status === 404 ? '这一卷尚付阙如,或已被移置别处。' : '数据读取失败,请稍后再试。'), path);
setTitle(['展卷未成']);
}
}
document.addEventListener('submit', (e) => {
const form = e.target.closest('[data-quick-search]');
if (!form) return;
e.preventDefault();
const q = form.querySelector('input[name="q"]')?.value.trim();
if (q) location.hash = `#/search?q=${encodeURIComponent(q)}`;
else location.hash = '#/search';
});
document.addEventListener('keydown', (e) => {
const typing = /^(INPUT|TEXTAREA|SELECT)$/.test(document.activeElement?.tagName || '');
if ((e.key === '/' && !typing) || ((e.metaKey || e.ctrlKey) && e.key.toLowerCase() === 'k')) {
e.preventDefault();
const input = document.querySelector('[data-quick-search] input');
input?.focus();
input?.select();
}
if (e.key === 'Escape' && document.activeElement?.matches('[data-quick-search] input')) {
document.activeElement.blur();
}
});
window.addEventListener('hashchange', render);
(async function boot() {
try {
const data = await getBootstrap();
setBootstrap(data);
await render();
} catch (err) {
console.error(err);
app.innerHTML = shell(errorView('与数据服务的连接中断。请确认后端已启动,然后刷新页面。'), '/');
setTitle(['连接中断']);
}
})();