feat(ui): 预览栏新增查看源码按钮
手机端 #fs-viewer 早有眼睛钮,桌面端预览栏只有下载和刷新:md / html / svg 这类渲染过的内容想看原文只能去终端开文件。 做法是渲染时把原文挂到节点的 __src,show() 带进 pvToolbar 的 o.source; 只有带 source 的类型才显示 #pv-src,点了在 #pv-body 里挂一个 pre 并隐藏预览 节点,再点移除。只做查看不做复制——桌面端选中即可复制,多一个按钮只是噪声。 超过 2MB 截断显示,避免大文件塞 pre 把面板卡住。 html/svg 顺手用同一份文本:r 只能读一次,svg 改为先取文本再自己包 blob。 代码类预览本身就是 .pv-pre,源码面板加 pv-src-pre 以便区分两套节点。
This commit is contained in:
@@ -503,6 +503,10 @@
|
|||||||
</button>
|
</button>
|
||||||
</span>
|
</span>
|
||||||
<span class="pv-actions">
|
<span class="pv-actions">
|
||||||
|
<!-- 源码钮:只在当前内容有源文本时出现(md/文本/代码/html/svg),点了在预览与原文间切换 -->
|
||||||
|
<button class="icon-btn hidden" id="pv-src" 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="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z"></path><circle cx="12" cy="12" r="3"></circle></svg>
|
||||||
|
</button>
|
||||||
<button class="icon-btn hidden" id="pv-download" title="下载文件" aria-label="下载文件">
|
<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>
|
<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>
|
||||||
|
|||||||
@@ -15,9 +15,11 @@ const PV_PLAIN = /\.(txt|log)$/i;
|
|||||||
// 常见代码/配置/数据文本扩展名;无扩展名的文件(Dockerfile/LICENSE 等)也按文本尝试,二进制嗅探兜底
|
// 常见代码/配置/数据文本扩展名;无扩展名的文件(Dockerfile/LICENSE 等)也按文本尝试,二进制嗅探兜底
|
||||||
const PV_TEXT = /\.(md|markdown|txt|log|csv|tsv|json|jsonl|xml|yml|yaml|toml|ini|conf|cfg|properties|env|py|pyw|js|mjs|cjs|ts|tsx|jsx|java|kt|c|h|cpp|hpp|cc|cs|go|rs|rb|php|swift|sh|bash|zsh|bat|ps1|sql|lua|r|dart|vue|css|less|scss|srt|vtt)$/i;
|
const PV_TEXT = /\.(md|markdown|txt|log|csv|tsv|json|jsonl|xml|yml|yaml|toml|ini|conf|cfg|properties|env|py|pyw|js|mjs|cjs|ts|tsx|jsx|java|kt|c|h|cpp|hpp|cc|cs|go|rs|rb|php|swift|sh|bash|zsh|bat|ps1|sql|lua|r|dart|vue|css|less|scss|srt|vtt)$/i;
|
||||||
const PV_TEXT_MAX = 512 * 1024; // 文本预览截断阈值(字符),超出提示下载看全文
|
const PV_TEXT_MAX = 512 * 1024; // 文本预览截断阈值(字符),超出提示下载看全文
|
||||||
|
const PV_SRC_MAX = 2 * 1024 * 1024; // 源码面板的显示上限,超过就只给前 2MB(大文件塞 pre 会卡)
|
||||||
let pvSeq = 0; // 预览序号:快速连点时旧请求不许再动 DOM
|
let pvSeq = 0; // 预览序号:快速连点时旧请求不许再动 DOM
|
||||||
// reload: 当前内容的"重新载入"动作(文本类无 iframe,按名重开);为 null 时回退到 reload iframe
|
// reload: 当前内容的"重新载入"动作(文本类无 iframe,按名重开);为 null 时回退到 reload iframe
|
||||||
const pvState = { name: null, url: null, blobUrl: null, reload: null, mobilePush: false };
|
const pvState = { name: null, url: null, blobUrl: null, reload: null, mobilePush: false,
|
||||||
|
source: null, srcOn: false, bodyNode: null, srcPre: null };
|
||||||
|
|
||||||
function pvDesktop() { return window.matchMedia('(min-width: 960px)').matches; }
|
function pvDesktop() { return window.matchMedia('(min-width: 960px)').matches; }
|
||||||
|
|
||||||
@@ -75,6 +77,40 @@ function pvToolbar(o) {
|
|||||||
document.getElementById('pv-popup').classList.toggle('hidden', !o.popup);
|
document.getElementById('pv-popup').classList.toggle('hidden', !o.popup);
|
||||||
pvState.reload = typeof o.reload === 'function' ? o.reload : null;
|
pvState.reload = typeof o.reload === 'function' ? o.reload : null;
|
||||||
pvState.url = o.url === undefined ? null : o.url;
|
pvState.url = o.url === undefined ? null : o.url;
|
||||||
|
// 源码钮:o.source 是原文(已渲染过的内容才有),没传就藏起来并回到预览态
|
||||||
|
pvState.source = o.source == null || o.source === '' ? null : String(o.source);
|
||||||
|
pvState.srcOn = false;
|
||||||
|
pvState.srcPre = null;
|
||||||
|
const sb = document.getElementById('pv-src');
|
||||||
|
sb.classList.toggle('hidden', pvState.source == null);
|
||||||
|
sb.classList.remove('active');
|
||||||
|
sb.title = '查看源码';
|
||||||
|
sb.setAttribute('aria-label', '查看源码');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 预览 ↔ 源码互切:不重建预览节点,只把它 display:none 再挂一个等宽 pre,来回切代价很低
|
||||||
|
function pvToggleSrc() {
|
||||||
|
const btn = document.getElementById('pv-src');
|
||||||
|
if (!btn || pvState.source == null || !pvState.bodyNode) return;
|
||||||
|
pvState.srcOn = !pvState.srcOn;
|
||||||
|
if (pvState.srcOn) {
|
||||||
|
let text = pvState.source;
|
||||||
|
if (text.length > PV_SRC_MAX) text = text.slice(0, PV_SRC_MAX) + '\n\n…(源码较大,仅显示前 ' + Math.round(PV_SRC_MAX / 1024) + ' KB)';
|
||||||
|
const pre = document.createElement('pre');
|
||||||
|
pre.className = 'pv-pre pv-src-pre'; // 代码类预览本身也是 .pv-pre,多一个类便于区分/定位
|
||||||
|
pre.textContent = text; // 不用 innerHTML:源码里的标签必须当文本
|
||||||
|
pvState.srcPre = pre;
|
||||||
|
document.getElementById('pv-body').appendChild(pre);
|
||||||
|
pvState.bodyNode.style.display = 'none';
|
||||||
|
} else {
|
||||||
|
if (pvState.srcPre && pvState.srcPre.parentNode) pvState.srcPre.parentNode.removeChild(pvState.srcPre);
|
||||||
|
pvState.srcPre = null;
|
||||||
|
pvState.bodyNode.style.display = '';
|
||||||
|
}
|
||||||
|
btn.classList.toggle('active', pvState.srcOn);
|
||||||
|
const lbl = pvState.srcOn ? '返回预览' : '查看源码';
|
||||||
|
btn.title = lbl;
|
||||||
|
btn.setAttribute('aria-label', lbl);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 每次展示内容都过一遍:标题显示相对路径(子目录可见),右上角下载钮随文件走
|
// 每次展示内容都过一遍:标题显示相对路径(子目录可见),右上角下载钮随文件走
|
||||||
@@ -90,6 +126,11 @@ function pvClearBody() {
|
|||||||
document.getElementById('pv-body').innerHTML = '';
|
document.getElementById('pv-body').innerHTML = '';
|
||||||
pvState.reload = null; // 换内容/关窗后旧的刷新动作立即失效
|
pvState.reload = null; // 换内容/关窗后旧的刷新动作立即失效
|
||||||
pvState.url = null; // 新窗口地址同理:不清会残留上一份内容的 blob/网关 URL
|
pvState.url = null; // 新窗口地址同理:不清会残留上一份内容的 blob/网关 URL
|
||||||
|
pvState.source = null; // 源码同样跟内容走,不然会把上一个文件的原文留在按钮里
|
||||||
|
pvState.srcOn = false;
|
||||||
|
pvState.bodyNode = null;
|
||||||
|
pvState.srcPre = null;
|
||||||
|
document.getElementById('pv-src').classList.add('hidden');
|
||||||
if (pvState.blobUrl) { try { URL.revokeObjectURL(pvState.blobUrl); } catch (_) {} pvState.blobUrl = null; }
|
if (pvState.blobUrl) { try { URL.revokeObjectURL(pvState.blobUrl); } catch (_) {} pvState.blobUrl = null; }
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -272,9 +313,12 @@ async function openFilePreview(name, sizeText) {
|
|||||||
}
|
}
|
||||||
const show = (node, tb) => {
|
const show = (node, tb) => {
|
||||||
if (my !== pvSeq) return;
|
if (my !== pvSeq) return;
|
||||||
|
// 节点上挂了 __src(原文)就带进工具条:预览栏据此显示源码钮。手机端的源码钮走 fs-viewer,不掺和。
|
||||||
|
if (node && node.__src != null) { tb = Object.assign({}, tb || tbAll, { source: node.__src }); delete node.__src; }
|
||||||
if (desk) {
|
if (desk) {
|
||||||
pvClearBody(); pvHeader(name, sizeText);
|
pvClearBody(); pvHeader(name, sizeText);
|
||||||
document.getElementById('pv-body').appendChild(node);
|
document.getElementById('pv-body').appendChild(node);
|
||||||
|
pvState.bodyNode = node;
|
||||||
pvToolbar(tb || tbAll);
|
pvToolbar(tb || tbAll);
|
||||||
} else pvOverlayShow(node, { bar: true, name: name });
|
} else pvOverlayShow(node, { bar: true, name: name });
|
||||||
};
|
};
|
||||||
@@ -337,11 +381,13 @@ async function openFilePreview(name, sizeText) {
|
|||||||
// 统一等 show() 之后登记,陈旧序号则立即撤销防泄漏。
|
// 统一等 show() 之后登记,陈旧序号则立即撤销防泄漏。
|
||||||
let newBlobUrl = null;
|
let newBlobUrl = null;
|
||||||
if (kind === 'html') {
|
if (kind === 'html') {
|
||||||
const src = rewriteHtmlDocSrcs(await r.text(), name); // 相对资源改写,否则 about:srcdoc/blob 基准下全 404
|
const raw = await r.text(); // 原文留着做源码面板,改写只用于内嵌显示
|
||||||
|
const src = rewriteHtmlDocSrcs(raw, name); // 相对资源改写,否则 about:srcdoc/blob 基准下全 404
|
||||||
const fr = document.createElement('iframe');
|
const fr = document.createElement('iframe');
|
||||||
fr.className = 'pv-frame';
|
fr.className = 'pv-frame';
|
||||||
fr.setAttribute('sandbox', 'allow-same-origin allow-scripts');
|
fr.setAttribute('sandbox', 'allow-same-origin allow-scripts');
|
||||||
fr.srcdoc = src;
|
fr.srcdoc = src;
|
||||||
|
fr.__src = raw;
|
||||||
newBlobUrl = URL.createObjectURL(new Blob([src], { type: 'text/html;charset=utf-8' }));
|
newBlobUrl = URL.createObjectURL(new Blob([src], { type: 'text/html;charset=utf-8' }));
|
||||||
tb = Object.assign({}, tbAll, { url: newBlobUrl }); // 新窗口=改写后的页面(与面板里同一份)
|
tb = Object.assign({}, tbAll, { url: newBlobUrl }); // 新窗口=改写后的页面(与面板里同一份)
|
||||||
node = fr;
|
node = fr;
|
||||||
@@ -352,10 +398,14 @@ async function openFilePreview(name, sizeText) {
|
|||||||
fr.className = 'pv-frame'; fr.title = String(name); fr.src = u; // 不加 sandbox:内建查看器插件需要 unrestricted 帧
|
fr.className = 'pv-frame'; fr.title = String(name); fr.src = u; // 不加 sandbox:内建查看器插件需要 unrestricted 帧
|
||||||
node = fr; tb = Object.assign({}, tbAll, { url: u });
|
node = fr; tb = Object.assign({}, tbAll, { url: u });
|
||||||
} else if (kind === 'img') {
|
} else if (kind === 'img') {
|
||||||
const u = URL.createObjectURL(await r.blob());
|
// r 只能读一次:svg 先取文本再自己包 blob(也顺便拿到源码面板要的原文)
|
||||||
|
const isSvg = /\.svg$/i.test(name);
|
||||||
|
const svgText = isSvg ? await r.text() : null;
|
||||||
|
const u = URL.createObjectURL(isSvg ? new Blob([svgText], { type: 'image/svg+xml;charset=utf-8' }) : await r.blob());
|
||||||
newBlobUrl = u;
|
newBlobUrl = u;
|
||||||
const im = document.createElement('img');
|
const im = document.createElement('img');
|
||||||
im.className = 'pv-img'; im.src = u; im.alt = String(name);
|
im.className = 'pv-img'; im.src = u; im.alt = String(name);
|
||||||
|
if (isSvg) im.__src = svgText;
|
||||||
const g = pvImgGroup(name);
|
const g = pvImgGroup(name);
|
||||||
im.addEventListener('click', () => openImageViewer(u, String(name).split('/').pop(), g && g.list, g && g.at));
|
im.addEventListener('click', () => openImageViewer(u, String(name).split('/').pop(), g && g.list, g && g.at));
|
||||||
node = im; tb = Object.assign({}, tbAll, { url: u });
|
node = im; tb = Object.assign({}, tbAll, { url: u });
|
||||||
@@ -415,13 +465,14 @@ async function pvTextNode(name, sizeText, r) {
|
|||||||
n.__tb = q.fmt
|
n.__tb = q.fmt
|
||||||
? { popup: true, url: URL.createObjectURL(new Blob([pvMdDoc(html, name)], { type: 'text/html;charset=utf-8' })) }
|
? { popup: true, url: URL.createObjectURL(new Blob([pvMdDoc(html, name)], { type: 'text/html;charset=utf-8' })) }
|
||||||
: { popup: true, url: pvTextUrl(text) };
|
: { popup: true, url: pvTextUrl(text) };
|
||||||
|
n.__src = text;
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
if (PV_PLAIN.test(low)) {
|
if (PV_PLAIN.test(low)) {
|
||||||
const wrap = document.createElement('div');
|
const wrap = document.createElement('div');
|
||||||
wrap.className = 'msg other';
|
wrap.className = 'msg other';
|
||||||
wrap.innerHTML = '<div class="bubble-ai pv-doc">' + esc(text) + '</div>';
|
wrap.innerHTML = '<div class="bubble-ai pv-doc">' + esc(text) + '</div>';
|
||||||
const n = wrap.firstChild; n.__tb = { popup: true, url: pvTextUrl(text) };
|
const n = wrap.firstChild; n.__tb = { popup: true, url: pvTextUrl(text) }; n.__src = text;
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
const frag = document.createDocumentFragment();
|
const frag = document.createDocumentFragment();
|
||||||
@@ -441,6 +492,7 @@ async function pvTextNode(name, sizeText, r) {
|
|||||||
holder.className = 'pv-plain';
|
holder.className = 'pv-plain';
|
||||||
holder.appendChild(frag);
|
holder.appendChild(frag);
|
||||||
holder.__tb = { popup: true, url: pvTextUrl(text) }; // 新窗口用全文,不受面板截断影响
|
holder.__tb = { popup: true, url: pvTextUrl(text) }; // 新窗口用全文,不受面板截断影响
|
||||||
|
holder.__src = text; // 源码面板同样给全文
|
||||||
return holder;
|
return holder;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -516,6 +568,7 @@ document.getElementById('pv-max').addEventListener('click', () => {
|
|||||||
const max = document.body.classList.toggle('pv-max');
|
const max = document.body.classList.toggle('pv-max');
|
||||||
pvSetMaxUI(max);
|
pvSetMaxUI(max);
|
||||||
});
|
});
|
||||||
|
document.getElementById('pv-src').addEventListener('click', pvToggleSrc);
|
||||||
document.getElementById('pv-refresh').addEventListener('click', () => {
|
document.getElementById('pv-refresh').addEventListener('click', () => {
|
||||||
if (typeof pvState.reload === 'function') { pvState.reload(); return; }
|
if (typeof pvState.reload === 'function') { pvState.reload(); return; }
|
||||||
const f = document.getElementById('pv-body').querySelector('iframe');
|
const f = document.getElementById('pv-body').querySelector('iframe');
|
||||||
|
|||||||
Reference in New Issue
Block a user