完善预览页面
This commit is contained in:
@@ -1274,8 +1274,16 @@ label.icon-btn { cursor: pointer; }
|
||||
body.pv-open.pv-max #app { width: 100%; margin-left: 0; }
|
||||
}
|
||||
.pv-head { flex: none; display: flex; align-items: center; gap: 8px; padding: 8px 10px 8px 14px; border-bottom: 1px solid var(--border); background: var(--drawer-bg); }
|
||||
.pv-title { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; font-weight: 600; }
|
||||
.pv-actions { flex: none; display: flex; align-items: center; gap: 6px; margin-left: 8px; }
|
||||
/* 地址条胶囊:路径 input + 内嵌动作钮,模拟浏览器地址栏 */
|
||||
.pv-addr { flex: 1; min-width: 0; display: flex; align-items: center; gap: 2px; background: var(--surface); border: 1px solid transparent; border-radius: 10px; padding: 2px 2px 2px 4px; }
|
||||
.pv-addr:focus-within { border-color: var(--border); background: var(--page); }
|
||||
.pv-title { flex: 1; min-width: 0; padding: 5px 6px; font-size: 13px; font-weight: 600; cursor: text; }
|
||||
.pv-addr .icon-btn { flex: none; width: 26px; height: 26px; border-radius: 7px; display: flex; align-items: center; justify-content: center; color: var(--gray); }
|
||||
.pv-addr .icon-btn:hover { color: var(--ink); }
|
||||
.pv-addr .icon-btn:active { background: rgba(0, 0, 0, .06); }
|
||||
.pv-actions { flex: none; display: flex; align-items: center; gap: 10px; margin-left: 12px; padding-right: 2px; }
|
||||
.pv-actions .icon-btn { width: 32px; height: 32px; border-radius: 8px; display: flex; align-items: center; justify-content: center; color: var(--ink); }
|
||||
.pv-actions .icon-btn:active { background: var(--surface); }
|
||||
.pv-body { flex: 1; min-height: 0; position: relative; overflow: auto; display: flex; flex-direction: column; background: #fff; }
|
||||
.pv-frame { flex: 1; width: 100%; height: 100%; border: 0; background: #fff; }
|
||||
.pv-doc { width: 100%; max-width: 860px; margin: 0 auto; padding: 16px 20px 40px; }
|
||||
|
||||
@@ -404,19 +404,23 @@
|
||||
<!-- ================= 文档预览右栏(桌面端≥960px;office/全部文档类型统一承载;移动端走全屏 #fs-viewer,本栏隐藏) -->
|
||||
<aside id="pv-pane" aria-label="文档预览">
|
||||
<header class="pv-head">
|
||||
<span class="pv-title" id="pv-title"></span>
|
||||
<span class="pv-actions">
|
||||
<button class="icon-btn hidden" id="pv-download" title="下载文件" aria-label="下载文件">
|
||||
<svg class="ic sm" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"></path><polyline points="7 10 12 15 17 10"></polyline><line x1="12" y1="15" x2="12" y2="3"></line></svg>
|
||||
</button>
|
||||
<!-- 地址条胶囊:路径 input(点击全选可复制) + 内嵌 刷新/新窗口,模拟浏览器地址栏 -->
|
||||
<span class="pv-addr">
|
||||
<input class="pv-title" id="pv-title" readonly aria-label="文件路径">
|
||||
<button class="icon-btn hidden" id="pv-refresh" title="重新载入" aria-label="重新载入">
|
||||
<svg class="ic sm" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="23 4 23 10 17 10"></polyline><path d="M20.49 15a9 9 0 1 1-2.12-9.36L23 10"></path></svg>
|
||||
</button>
|
||||
<button class="icon-btn hidden" id="pv-popup" title="新窗口打开" aria-label="新窗口打开">
|
||||
<svg class="ic sm" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6"></path><polyline points="15 3 21 3 21 9"></polyline><line x1="10" y1="14" x2="21" y2="3"></line></svg>
|
||||
</button>
|
||||
</span>
|
||||
<span class="pv-actions">
|
||||
<button class="icon-btn hidden" id="pv-download" title="下载文件" aria-label="下载文件">
|
||||
<svg class="ic sm" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"></path><polyline points="7 10 12 15 17 10"></polyline><line x1="12" y1="15" x2="12" y2="3"></line></svg>
|
||||
</button>
|
||||
<button class="icon-btn" id="pv-max" title="放大" aria-label="放大">
|
||||
<svg class="ic sm" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M8 3H5a2 2 0 0 0-2 2v3"></path><path d="M21 8V5a2 2 0 0 0-2-2h-3"></path><path d="M16 21h3a2 2 0 0 0 2-2v-3"></path><path d="M3 16v3a2 2 0 0 0 2 2h3"></path></svg>
|
||||
<svg class="ic sm hidden" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M8 3v3a2 2 0 0 1-2 2H3"></path><path d="M21 8h-3a2 2 0 0 1-2-2V3"></path><path d="M3 16h3a2 2 0 0 1 2 2v3"></path><path d="M16 21v-3a2 2 0 0 1 2-2h3"></path></svg>
|
||||
</button>
|
||||
<button class="icon-btn" id="pv-close" title="关闭预览" aria-label="关闭预览">✕</button>
|
||||
</span>
|
||||
@@ -442,5 +446,25 @@
|
||||
<script src="/js/files.js"></script>
|
||||
<script src="/js/preview.js"></script>
|
||||
<script src="/js/settings.js"></script>
|
||||
<script>
|
||||
// 启动自愈:外部工具改前端文件是非原子写入,极端时机下浏览器可能拿到半截或重复拼接的 HTML
|
||||
// (脚本缺半 → 白屏裸首页;重复注入 → 每个文件报 duplicate variable 后 JS 全挂)。
|
||||
// 这里检测关键脚本数量与入口全局,不对就自动整页重载一次(sessionStorage 防循环)。
|
||||
// 注意:js/ 脚本增减时需同步改这里的期望数 7。
|
||||
(function () {
|
||||
var ok = false;
|
||||
try {
|
||||
var n = document.querySelectorAll('script[src^="/js/"]').length;
|
||||
ok = n === 7 && typeof state !== 'undefined' &&
|
||||
typeof openFilePreview === 'function' && typeof restartNow === 'function';
|
||||
} catch (_) {}
|
||||
if (ok) { try { sessionStorage.removeItem('boot-heal'); } catch (_) {} return; }
|
||||
try {
|
||||
if (sessionStorage.getItem('boot-heal')) { sessionStorage.removeItem('boot-heal'); return; }
|
||||
sessionStorage.setItem('boot-heal', '1');
|
||||
} catch (_) { return; }
|
||||
location.reload();
|
||||
})();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -43,7 +43,7 @@ function pvToolbar(o) {
|
||||
function pvHeader(name, sizeText) {
|
||||
pvState.name = name;
|
||||
const t = document.getElementById('pv-title');
|
||||
t.textContent = String(name || '');
|
||||
t.value = String(name || '');
|
||||
t.title = sizeText ? name + ' · ' + sizeText : String(name || '');
|
||||
document.getElementById('pv-download').classList.toggle('hidden', !name);
|
||||
}
|
||||
@@ -88,6 +88,7 @@ function pvOpenPane() {
|
||||
function pvCloseDoc() {
|
||||
if (!document.body.classList.contains('pv-open')) return;
|
||||
document.body.classList.remove('pv-open', 'pv-max');
|
||||
if (typeof pvSetMaxUI === 'function') pvSetMaxUI(false); // 收起时复位放大钮图标(函数声明提升,定义在后亦可)
|
||||
pvClearBody();
|
||||
pvState.name = null; pvState.url = null;
|
||||
if (pvState.drawerWasOpen) {
|
||||
@@ -304,12 +305,17 @@ document.getElementById('pv-close').addEventListener('click', pvCloseDoc);
|
||||
document.getElementById('pv-download').addEventListener('click', () => {
|
||||
if (pvState.name && typeof downloadFile === 'function') downloadFile(pvState.name);
|
||||
});
|
||||
document.getElementById('pv-max').addEventListener('click', () => {
|
||||
const max = document.body.classList.toggle('pv-max');
|
||||
// 放大/还原钮双图标与 title 同步:退出全屏(pvCloseDoc)时也要复位
|
||||
function pvSetMaxUI(max) {
|
||||
const btn = document.getElementById('pv-max');
|
||||
const lbl = max ? '还原' : '放大';
|
||||
btn.title = lbl;
|
||||
btn.setAttribute('aria-label', lbl);
|
||||
btn.querySelectorAll('svg').forEach((s) => s.classList.toggle('hidden')); // 双图标互切
|
||||
}
|
||||
document.getElementById('pv-max').addEventListener('click', () => {
|
||||
const max = document.body.classList.toggle('pv-max');
|
||||
pvSetMaxUI(max);
|
||||
});
|
||||
document.getElementById('pv-refresh').addEventListener('click', () => {
|
||||
const f = document.getElementById('pv-body').querySelector('iframe');
|
||||
@@ -319,6 +325,8 @@ document.getElementById('pv-refresh').addEventListener('click', () => {
|
||||
document.getElementById('pv-popup').addEventListener('click', () => {
|
||||
if (pvState.url) window.open(pvState.url, '_blank', 'noopener');
|
||||
});
|
||||
// 地址条:点击/聚焦全选——长路径被截断时可选中看全文、直接 Ctrl+C 复制
|
||||
document.getElementById('pv-title').addEventListener('focus', (e) => e.target.select());
|
||||
// md 代码块复制钮与 fs-viewer 同一委托逻辑
|
||||
document.getElementById('pv-pane').addEventListener('click', (e) => {
|
||||
const b = e.target.closest('.act-copy');
|
||||
@@ -333,6 +341,7 @@ window.addEventListener('popstate', () => {
|
||||
// Esc 关闭右栏(未开全屏查看器时)
|
||||
window.addEventListener('keydown', (e) => {
|
||||
if (e.key !== 'Escape') return;
|
||||
if (document.activeElement && document.activeElement.id === 'pv-title') { document.activeElement.blur(); return; } // 地址条聚焦时:先退框不关栏
|
||||
if (!document.getElementById('fs-viewer').classList.contains('hidden')) return;
|
||||
pvCloseDoc();
|
||||
});
|
||||
|
||||
+36
-9
@@ -53,17 +53,44 @@ self.addEventListener('fetch', (event) => {
|
||||
// 跳过 API/SSE/预览/交付取件,避免缓存动态数据(预览页需实时;/f/ 同链内容会随重新 save 变化)
|
||||
if (url.pathname.startsWith('/api/') || url.pathname.startsWith('/sse') || url.pathname.startsWith('/preview') || url.pathname.startsWith('/f/')) return;
|
||||
|
||||
// vendor 预览页发出的根绝对请求(fetch('/') 自刷新、EventSource('/events') 等,Referer 是预览页):
|
||||
// 内容随文档变化,既不缓存,也不能把 office 文档页写进 '/' 这类 SPA 缓存键。
|
||||
if (request.referrer) {
|
||||
try {
|
||||
const rp = new URL(request.referrer).pathname;
|
||||
if (rp === '/preview' || rp.startsWith('/preview/')) return;
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
// HTML/登录页:网络优先
|
||||
if (request.mode === 'navigate' || request.destination === 'document') {
|
||||
event.respondWith(
|
||||
fetch(request)
|
||||
.then((resp) => {
|
||||
const copy = resp.clone();
|
||||
caches.open(RUNTIME_CACHE).then((cache) => cache.put(request, copy));
|
||||
return resp;
|
||||
})
|
||||
.catch(() => caches.match(request).then((r) => r || caches.match('/')))
|
||||
);
|
||||
// 静态站文件是绑定挂载、外部工具改动非原子:刷新时机撞上写入瞬间会拿到半截 HTML
|
||||
// (脚本/样式缺一半 → 无样式裸首页假象)。读成文本校验收尾标签,残破则稍候重试一次;
|
||||
// 两次都残破也照常返回,只有 ok 的快照才进缓存(残破页进缓存会被离线兜底永久化)。
|
||||
event.respondWith((async () => {
|
||||
let last = null;
|
||||
for (let i = 0; i < 2; i++) {
|
||||
try {
|
||||
const resp = await fetch(request);
|
||||
if (!resp.ok || resp.type !== 'basic') return resp;
|
||||
last = { txt: await resp.text(), resp };
|
||||
} catch (e) {
|
||||
if (last) break; // 已有残破快照:降级返回它
|
||||
if (i === 1) throw e;
|
||||
await new Promise((r) => setTimeout(r, 250));
|
||||
continue;
|
||||
}
|
||||
if (/<\/html>\s*$/i.test(last.txt)) break;
|
||||
if (i === 0) await new Promise((r) => setTimeout(r, 250));
|
||||
}
|
||||
const h = new Headers(last.resp.headers);
|
||||
// 重建响应体后 Content-Length 已失真,连同二进制透传头一并剔除(也是 Response 构造禁设项)
|
||||
['content-length', 'content-encoding', 'content-transfer-encoding'].forEach((k) => h.delete(k));
|
||||
const out = new Response(last.txt, { status: last.resp.status, statusText: last.resp.statusText, headers: h });
|
||||
const complete = /<\/html>\s*$/i.test(last.txt);
|
||||
if (complete) caches.open(RUNTIME_CACHE).then((cache) => cache.put(request, out.clone()));
|
||||
return out;
|
||||
})().catch(() => caches.match(request).then((r) => r || caches.match('/'))));
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -63,6 +63,19 @@ function serviceByReferer(req) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 前置守卫(server.js 需注册在 express.static 之前):vendor 预览页把「取自身新版 HTML」
|
||||
// 硬编码成根绝对 '/',打到 core 根上会被 SPA 首页吞掉 → 局部 diff 拿到外来页 → 触发
|
||||
// location.reload() 兜底(白屏一下)。这里只拦 '/':Referer 命中预览前缀时原样转发回
|
||||
// 对应服务(与兜底路由同一信任模型),其余路径仍由 mount() 末尾兜底。
|
||||
function earlyGuard(app) {
|
||||
app.use((req, res, next) => {
|
||||
if ((req.method !== 'GET' && req.method !== 'HEAD') || req.path !== '/') return next();
|
||||
const svc = serviceByReferer(req);
|
||||
if (!svc) return next();
|
||||
forward(svc, req, res, '/');
|
||||
});
|
||||
}
|
||||
|
||||
function mount(app) {
|
||||
for (const svc of services) {
|
||||
if (svc.ownership) log.warn('preview', svc.name + ': ownership \u5b57\u6bb5\u672a\u63a5\u7ebf\uff08\u5f53\u524d\u4fe1\u4efb\u6a21\u578b = \u767b\u5f55\u2248\u8fd0\u7ef4\uff0c\u4e0e data-page \u4e00\u81f4\uff09');
|
||||
@@ -81,4 +94,4 @@ function mount(app) {
|
||||
app.use(fb);
|
||||
}
|
||||
|
||||
module.exports = { mount };
|
||||
module.exports = { mount, earlyGuard };
|
||||
|
||||
@@ -58,6 +58,10 @@ app.use('/p', require('./routes/public-files'));
|
||||
|
||||
app.use(auth.middleware); // 其余请求需登录态(cookie 或 Basic 头)
|
||||
|
||||
// preview-proxy 前置守卫(见其注释):vendor 预览页用根绝对 '/' 取自身新版 HTML,
|
||||
// 必须在静态首页拦截之前转发回去;mount() 仍在文件末尾兜底其余路径。
|
||||
require('./routes/preview-proxy').earlyGuard(app);
|
||||
|
||||
app.use(express.static(path.join(__dirname, 'public')));
|
||||
|
||||
app.use('/api/sessions', require('./routes/sessions'));
|
||||
|
||||
Reference in New Issue
Block a user