feat(ui): 会话流里的本机链接直接在预览窗打开
点 /app/task-x/daily.html 这类站内地址原来是浏览器默认的新标签, 预览窗只认 sessionId + 相对文件名,同一个地址没第二条进窗口的路。 现在 #chat-view 上加委托监听,命中本机 host 的链接就交给 pvOpenUrl: 桌面进右侧分栏,窄屏(含手机)进 #fs-viewer 全屏浮层,系统返回键可收起。 「本机」= 完全同源,或两端都是 localhost/127.0.0.1/[::1]/0.0.0.0 别名(端口不究), 部署在公网域名时同源自然命中。外链、//host、锚点、mailto、相对路径不拦; Ctrl/Cmd 点击与中键放行,保留「我就是要开新标签」这个出口。 pvOpenUrl 走 iframe 直挂,不经过文件读取链路,sandbox 沿用现有 HTML 预览的 allow-same-origin allow-scripts——data-page 页面要靠脚本渲染表格。URL 模式下 隐藏下载钮(不是文件,按名下载会拼错路径),刷新钮重建 iframe,新窗口钮开原地址, 地址条回填完整 URL 方便复制。窄屏不传 source,源码/复制钮自动隐藏。 地址条回车顺带认站内绝对路径:/app/... 原来当相对文件名去 fetch,注定 404, 现在同样进预览窗。移动端 popstate 的判定从 [src^=/preview/] 放宽到 .fs-html-frame, URL 预览也能被返回键收起。
This commit is contained in:
@@ -204,6 +204,52 @@ async function pvOpenOffice(name, sizeText) {
|
||||
pvState.url = url;
|
||||
}
|
||||
|
||||
// ---------- 站内地址预览:会话流里的链接 / 地址条回车 → 直接挂进预览窗 ----------
|
||||
// 「本机」判定:完全同源,或两端都是本机别名(localhost/127.0.0.1/[::1]/0.0.0.0,端口不究)。
|
||||
// 外链、锚点、mailto、相对路径都不算,调用方自行放行。
|
||||
function pvLocalHost(h) { return h === 'localhost' || h === '127.0.0.1' || h === '::1' || h === '[::1]' || h === '0.0.0.0'; }
|
||||
function pvSameHost(target) {
|
||||
let u;
|
||||
try { u = new URL(target, location.href); } catch (_) { return null; }
|
||||
if (u.origin === location.origin) return u;
|
||||
if (pvLocalHost(u.hostname) && pvLocalHost(location.hostname)) return u;
|
||||
return null;
|
||||
}
|
||||
function pvUrlTitle(url) {
|
||||
try {
|
||||
const u = new URL(url, location.href);
|
||||
return decodeURIComponent(u.pathname.split('/').filter(Boolean).pop() || '') || u.host;
|
||||
} catch (_) { return String(url); }
|
||||
}
|
||||
// iframe 直挂,不走文件读取链路:桌面进右侧分栏,窄屏进全屏浮层。
|
||||
// sandbox 与现有 HTML 预览一致——data-page 页面要靠脚本渲染表格。
|
||||
function pvOpenUrl(url, title) {
|
||||
const desk = pvDesktop();
|
||||
const name = title || pvUrlTitle(url);
|
||||
++pvSeq; // 在途的文件预览请求就此作废,别回来抢 DOM
|
||||
const mkFrame = () => {
|
||||
const fr = document.createElement('iframe');
|
||||
fr.className = desk ? 'pv-frame' : 'fs-html-frame';
|
||||
fr.title = name;
|
||||
fr.setAttribute('sandbox', 'allow-same-origin allow-scripts');
|
||||
fr.src = url;
|
||||
return fr;
|
||||
};
|
||||
if (desk) {
|
||||
pvOpenPane(); pvClearBody();
|
||||
pvHeader(name, '');
|
||||
pvState.name = null; // 不是文件:下载钮收起(按名下载会拼错路径)
|
||||
document.getElementById('pv-download').classList.add('hidden');
|
||||
pvToolbar({ refresh: true, popup: true, reload: () => pvOpenUrl(url, title), url });
|
||||
const t = document.getElementById('pv-title'); // 地址条显示完整地址,可选中复制
|
||||
t.value = url; t.title = url;
|
||||
document.getElementById('pv-body').appendChild(mkFrame());
|
||||
} else {
|
||||
pvOverlayShow(mkFrame(), { bar: true, name });
|
||||
if (!pvState.mobilePush) { try { history.pushState({ pvOffice: 1 }, ''); pvState.mobilePush = true; } catch (_) {} }
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- 统一入口:所有文件点击都走这 ----------
|
||||
async function openFilePreview(name, sizeText) {
|
||||
if (!state.sessionId) return;
|
||||
@@ -481,11 +527,16 @@ document.getElementById('pv-popup').addEventListener('click', () => {
|
||||
});
|
||||
// 地址条:点击/聚焦全选——长路径被截断时可选中看全文、直接 Ctrl+C 复制
|
||||
document.getElementById('pv-title').addEventListener('focus', (e) => e.target.select());
|
||||
// 地址条回车跳转:http(s) 网址新标签打开,其余按任务目录相对路径进预览
|
||||
// 地址条回车跳转:本机地址(含 /app/... 这类站内绝对路径)进预览窗,外部网址新开标签,
|
||||
// 其余按任务目录相对路径当文件预览。
|
||||
function pvClean(s) { return String(s || '').trim().replace(/^["']+|["']+$/g, ''); }
|
||||
function pvAddrGo(v) {
|
||||
if (/^https?:\/\//i.test(v)) { window.open(v, '_blank', 'noopener'); return; }
|
||||
if (/^https?:\/\//i.test(v)) {
|
||||
if (pvSameHost(v)) { document.getElementById('pv-title').blur(); pvOpenUrl(v); return; }
|
||||
window.open(v, '_blank', 'noopener'); return;
|
||||
}
|
||||
document.getElementById('pv-title').blur();
|
||||
if (v.charAt(0) === '/' && v.charAt(1) !== '/') { pvOpenUrl(v); return; }
|
||||
openFilePreview(v);
|
||||
}
|
||||
// ---------- 地址条文件名补全:候选取自 files.js 缓存的目录树(filesNodeAt),零请求 ----------
|
||||
@@ -570,11 +621,11 @@ document.getElementById('pv-pane').addEventListener('click', (e) => {
|
||||
const b = e.target.closest('.act-copy');
|
||||
if (b && typeof onCopyClick === 'function') onCopyClick(e);
|
||||
});
|
||||
// 移动端系统返回键:先收起 office 全屏预览
|
||||
// 移动端系统返回键:先收起 iframe 类全屏预览(office 网关 / 站内地址)
|
||||
window.addEventListener('popstate', () => {
|
||||
pvState.mobilePush = false;
|
||||
const fs = document.getElementById('fs-viewer');
|
||||
if (!fs.classList.contains('hidden') && fs.querySelector('.fs-html-frame[src^="/preview/"]')) closeFullscreen();
|
||||
if (!fs.classList.contains('hidden') && fs.querySelector('.fs-html-frame')) closeFullscreen();
|
||||
});
|
||||
// Esc 关闭预览分栏(未开全屏查看器时)
|
||||
window.addEventListener('keydown', (e) => {
|
||||
|
||||
@@ -705,6 +705,21 @@
|
||||
$('chat-view').addEventListener('click', imgViewerDelegate);
|
||||
$('fs-viewer').addEventListener('click', imgViewerDelegate);
|
||||
|
||||
// ---------- 会话流里的本机链接:进预览窗 ----------
|
||||
// 只拦本机 host(同源 / localhost 别名);外链、锚点、mailto、相对路径维持浏览器默认。
|
||||
// 修饰键与中键放行,留着「我就是要新开一个标签页」这个出口。
|
||||
function linkPreviewDelegate(e) {
|
||||
if (e.button !== 0 || e.ctrlKey || e.metaKey || e.shiftKey || e.altKey) return;
|
||||
const a = e.target.closest('a[href]');
|
||||
if (!a) return;
|
||||
const href = a.getAttribute('href') || '';
|
||||
if (!/^(https?:\/\/|\/(?!\/))/i.test(href) || !pvSameHost(href)) return;
|
||||
e.preventDefault();
|
||||
const txt = (a.textContent || '').trim();
|
||||
pvOpenUrl(href, txt && txt.length <= 40 ? txt : null);
|
||||
}
|
||||
$('chat-view').addEventListener('click', linkPreviewDelegate);
|
||||
|
||||
// 图片全屏查看器:transform 缩放+平移。双指捻合缩放(1~6x)、放大后单指/鼠标拖动、双击切 2.5x。
|
||||
// 第 3 参 list([{src,alt}] 或 [src]) 传入「同组图片」时开启左右滑动切换;第 4 参 at 指定起始下标。
|
||||
// 切图用双层 img 做真正的分页位移(旧图跟手滑出、新图同步滑入),并在开图时预取相邻图,
|
||||
|
||||
Reference in New Issue
Block a user