chore: 迁移 mermaid 绘图技能到 meta 层
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>LPT 图表索引</title>
|
||||
<style>
|
||||
body { margin: 0 auto; max-width: 1600px; padding: 24px; background: #fafafa; font-family: "PingFang SC", "Microsoft YaHei", sans-serif; color: #333; }
|
||||
h1 { font-size: 20px; }
|
||||
.tip { color: #777; font-size: 13px; line-height: 1.8; }
|
||||
ul { list-style: none; padding: 0; }
|
||||
li { background: #fff; border: 1px solid #e0e0e0; border-radius: 8px; padding: 12px 16px; margin: 12px 0; }
|
||||
li a { font-size: 15px; color: #1a56b0; text-decoration: none; }
|
||||
li a:hover { text-decoration: underline; }
|
||||
li p { margin: 6px 0 0; color: #777; font-size: 13px; }
|
||||
code { font-family: "SF Mono", Menlo, Consolas, monospace; font-size: 12.5px; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>LPT 图表索引</h1>
|
||||
<p class="tip">每个业务主题一个文件夹,图源为同目录 <code>.mmd</code> 纯文本,渲染页靠 <code>../viewer.js</code> 共享渲染器。<br/>
|
||||
本地预览:在 <code>docs/diagrams</code> 目录执行 <code>python3 -m http.server 8000</code>,再访问
|
||||
<code>http://localhost:8000/<主题>/index.html</code>。</p>
|
||||
<ul>
|
||||
</ul>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,263 @@
|
||||
/*
|
||||
* LPT 共享 mermaid 渲染器
|
||||
* 由 .agents/skills/mermaid-diagram/scripts/new_figure.py 铺设到 docs/diagrams/viewer.js
|
||||
*
|
||||
* 用法(渲染页底部):
|
||||
* <script src="../viewer.js?v=2"></script>
|
||||
* <script>MermaidViewer.init();</script>
|
||||
*
|
||||
* 页面里每个图位写成:
|
||||
* <div class="diagram" data-src="xxx.mmd?v=1" data-height="72vh">加载中…</div>
|
||||
*
|
||||
* 默认只读视图一眼看全;点「查看」进入全屏交互:Ctrl+滚轮缩放、拖拽平移、Esc 退出。
|
||||
*/
|
||||
(function (global) {
|
||||
'use strict';
|
||||
|
||||
var MERMAID_CDN = 'https://cdn.jsdelivr.net/npm/mermaid@11/dist/mermaid.min.js';
|
||||
var seq = 0;
|
||||
|
||||
function ensureMermaid() {
|
||||
if (global.mermaid) return Promise.resolve(global.mermaid);
|
||||
return new Promise(function (resolve, reject) {
|
||||
var s = document.createElement('script');
|
||||
s.src = MERMAID_CDN;
|
||||
s.async = true;
|
||||
s.onload = function () {
|
||||
if (global.mermaid) resolve(global.mermaid);
|
||||
else reject(new Error('mermaid 已下载但未挂载到 window'));
|
||||
};
|
||||
s.onerror = function () {
|
||||
reject(new Error('无法加载 mermaid(离线环境?):' + MERMAID_CDN));
|
||||
};
|
||||
document.head.appendChild(s);
|
||||
});
|
||||
}
|
||||
|
||||
function configureMermaid(mermaid) {
|
||||
mermaid.initialize({
|
||||
startOnLoad: false,
|
||||
securityLevel: 'loose',
|
||||
theme: 'base',
|
||||
themeVariables: {
|
||||
fontFamily: '"PingFang SC","Microsoft YaHei","Hiragino Sans GB",sans-serif',
|
||||
fontSize: '14px'
|
||||
},
|
||||
flowchart: { htmlLabels: true, useMaxWidth: false, curve: 'basis' },
|
||||
er: { useMaxWidth: false }
|
||||
});
|
||||
}
|
||||
|
||||
function injectStyle() {
|
||||
if (document.getElementById('mv-style')) return;
|
||||
var css = [
|
||||
'.mv-diagram{background:#fff;border:1px solid #e0e0e0;border-radius:8px;margin:10px 0;padding:0;overflow:hidden}',
|
||||
'.mv-bar{display:flex;align-items:center;gap:8px;padding:6px 12px;border-bottom:1px solid #eee;background:#fbfbfb;font-size:12.5px;color:#666}',
|
||||
'.mv-bar code{font-family:Menlo,Consolas,monospace}',
|
||||
'.mv-bar .mv-spacer{flex:1}',
|
||||
'.mv-btn{border:1px solid #1a56b0;background:#fff;color:#1a56b0;border-radius:4px;padding:2px 10px;font-size:12.5px;cursor:pointer}',
|
||||
'.mv-btn:hover:not([disabled]){background:#e8f0fe}',
|
||||
'.mv-btn[disabled]{opacity:.45;cursor:default}',
|
||||
'.mv-stage{display:flex;justify-content:center;align-items:flex-start;overflow:auto;padding:16px}',
|
||||
'.mv-stage svg{max-width:100%;height:auto;display:block}',
|
||||
'.mv-err{color:#c0392b;font-size:13px;white-space:pre-wrap;font-family:Menlo,Consolas,monospace}',
|
||||
'.mv-overlay{position:fixed;top:0;right:0;bottom:0;left:0;z-index:9999;background:rgba(20,20,20,.92);display:flex;flex-direction:column}',
|
||||
'.mv-obar{display:flex;align-items:center;gap:8px;padding:8px 14px;color:#eee;font-size:13px;flex-wrap:wrap}',
|
||||
'.mv-obar code{font-family:Menlo,Consolas,monospace;color:#9fd0ff}',
|
||||
'.mv-obar .mv-spacer{flex:1}',
|
||||
'.mv-obtn{border:1px solid #888;background:#2a2a2a;color:#eee;border-radius:4px;padding:3px 10px;font-size:13px;cursor:pointer}',
|
||||
'.mv-obtn:hover{background:#3a3a3a}',
|
||||
'.mv-viewport{flex:1;overflow:hidden;cursor:grab;background:#fff}',
|
||||
'.mv-viewport.mv-grabbing{cursor:grabbing}',
|
||||
'.mv-canvas{transform-origin:0 0;will-change:transform}',
|
||||
'.mv-canvas svg{display:block;background:#fff}'
|
||||
].join('');
|
||||
var style = document.createElement('style');
|
||||
style.id = 'mv-style';
|
||||
style.textContent = css;
|
||||
document.head.appendChild(style);
|
||||
}
|
||||
|
||||
function openOverlay(title, svgMarkup) {
|
||||
var overlay = document.createElement('div');
|
||||
overlay.className = 'mv-overlay';
|
||||
overlay.innerHTML =
|
||||
'<div class="mv-obar"><code>' + title + '</code><span class="mv-spacer"></span>' +
|
||||
'<button type="button" class="mv-obtn" data-act="out">缩小</button>' +
|
||||
'<button type="button" class="mv-obtn" data-act="in">放大</button>' +
|
||||
'<button type="button" class="mv-obtn" data-act="reset">重置</button>' +
|
||||
'<span>Ctrl+滚轮缩放 · 拖拽平移 · Esc 退出</span>' +
|
||||
'<button type="button" class="mv-obtn" data-act="close">关闭</button></div>' +
|
||||
'<div class="mv-viewport"><div class="mv-canvas"></div></div>';
|
||||
|
||||
var viewport = overlay.querySelector('.mv-viewport');
|
||||
var canvas = overlay.querySelector('.mv-canvas');
|
||||
canvas.innerHTML = svgMarkup;
|
||||
document.body.appendChild(overlay);
|
||||
|
||||
var scale = 1;
|
||||
var tx = 0;
|
||||
var ty = 0;
|
||||
|
||||
function apply() {
|
||||
canvas.style.transform = 'translate(' + tx + 'px,' + ty + 'px) scale(' + scale + ')';
|
||||
}
|
||||
|
||||
function fit() {
|
||||
scale = 1;
|
||||
tx = Math.max(0, (viewport.clientWidth - canvas.offsetWidth) / 2);
|
||||
ty = Math.max(0, (viewport.clientHeight - canvas.offsetHeight) / 2);
|
||||
apply();
|
||||
}
|
||||
|
||||
function zoomAt(factor, cx, cy) {
|
||||
var next = Math.min(8, Math.max(0.2, scale * factor));
|
||||
tx = cx - (cx - tx) * (next / scale);
|
||||
ty = cy - (cy - ty) * (next / scale);
|
||||
scale = next;
|
||||
apply();
|
||||
}
|
||||
|
||||
viewport.addEventListener('wheel', function (e) {
|
||||
if (!e.ctrlKey) return;
|
||||
e.preventDefault();
|
||||
var rect = viewport.getBoundingClientRect();
|
||||
zoomAt(e.deltaY < 0 ? 1.12 : 1 / 1.12, e.clientX - rect.left, e.clientY - rect.top);
|
||||
}, { passive: false });
|
||||
|
||||
var dragging = false;
|
||||
var lastX = 0;
|
||||
var lastY = 0;
|
||||
|
||||
function onMove(e) {
|
||||
if (!dragging) return;
|
||||
tx += e.clientX - lastX;
|
||||
ty += e.clientY - lastY;
|
||||
lastX = e.clientX;
|
||||
lastY = e.clientY;
|
||||
apply();
|
||||
e.preventDefault();
|
||||
}
|
||||
|
||||
function onUp() {
|
||||
dragging = false;
|
||||
viewport.classList.remove('mv-grabbing');
|
||||
}
|
||||
|
||||
viewport.addEventListener('mousedown', function (e) {
|
||||
dragging = true;
|
||||
lastX = e.clientX;
|
||||
lastY = e.clientY;
|
||||
viewport.classList.add('mv-grabbing');
|
||||
e.preventDefault();
|
||||
});
|
||||
viewport.addEventListener('contextmenu', function (e) { e.preventDefault(); });
|
||||
|
||||
window.addEventListener('mousemove', onMove);
|
||||
window.addEventListener('mouseup', onUp);
|
||||
|
||||
function close() {
|
||||
window.removeEventListener('mousemove', onMove);
|
||||
window.removeEventListener('mouseup', onUp);
|
||||
document.removeEventListener('keydown', onKey);
|
||||
if (overlay.parentNode) overlay.parentNode.removeChild(overlay);
|
||||
}
|
||||
|
||||
function onKey(e) {
|
||||
if (e.key === 'Escape') close();
|
||||
}
|
||||
document.addEventListener('keydown', onKey);
|
||||
|
||||
overlay.querySelector('.mv-obar').addEventListener('click', function (e) {
|
||||
var act = e.target && e.target.getAttribute && e.target.getAttribute('data-act');
|
||||
if (!act) return;
|
||||
if (act === 'close') return close();
|
||||
if (act === 'reset') return fit();
|
||||
zoomAt(act === 'in' ? 1.2 : 1 / 1.2, viewport.clientWidth / 2, viewport.clientHeight / 2);
|
||||
});
|
||||
|
||||
requestAnimationFrame(fit);
|
||||
}
|
||||
|
||||
function renderOne(container, mermaid) {
|
||||
var src = container.getAttribute('data-src');
|
||||
var height = container.getAttribute('data-height') || '72vh';
|
||||
|
||||
container.classList.add('mv-diagram');
|
||||
container.innerHTML = '';
|
||||
|
||||
var bar = document.createElement('div');
|
||||
bar.className = 'mv-bar';
|
||||
var nameEl = document.createElement('code');
|
||||
nameEl.textContent = src;
|
||||
bar.appendChild(nameEl);
|
||||
var spacer = document.createElement('span');
|
||||
spacer.className = 'mv-spacer';
|
||||
bar.appendChild(spacer);
|
||||
var btn = document.createElement('button');
|
||||
btn.type = 'button';
|
||||
btn.className = 'mv-btn';
|
||||
btn.textContent = '查看';
|
||||
btn.disabled = true;
|
||||
bar.appendChild(btn);
|
||||
|
||||
var stage = document.createElement('div');
|
||||
stage.className = 'mv-stage';
|
||||
stage.style.height = height;
|
||||
|
||||
container.appendChild(bar);
|
||||
container.appendChild(stage);
|
||||
|
||||
return fetch(src, { cache: 'no-cache' })
|
||||
.then(function (resp) {
|
||||
if (!resp.ok) throw new Error('图源加载失败:HTTP ' + resp.status + ' ' + src);
|
||||
return resp.text();
|
||||
})
|
||||
.then(function (code) {
|
||||
return mermaid.render('mv-' + (seq++) + '-' + Date.now(), code);
|
||||
})
|
||||
.then(function (res) {
|
||||
stage.innerHTML = res.svg;
|
||||
var svg = stage.querySelector('svg');
|
||||
if (svg) {
|
||||
svg.removeAttribute('height');
|
||||
svg.style.maxWidth = '100%';
|
||||
svg.style.height = 'auto';
|
||||
}
|
||||
btn.disabled = false;
|
||||
btn.addEventListener('click', function () { openOverlay(src, res.svg); });
|
||||
})
|
||||
.catch(function (err) {
|
||||
stage.innerHTML = '';
|
||||
var box = document.createElement('div');
|
||||
box.className = 'mv-err';
|
||||
box.textContent = '渲染失败:' + (err && err.message ? err.message : err);
|
||||
stage.appendChild(box);
|
||||
console.error('[mermaid-viewer]', src, err);
|
||||
});
|
||||
}
|
||||
|
||||
function init(options) {
|
||||
options = options || {};
|
||||
var selector = options.selector || '.diagram';
|
||||
if (document.querySelector('.mv-diagram')) injectStyle();
|
||||
var containers = Array.prototype.slice.call(document.querySelectorAll(selector));
|
||||
if (!containers.length) return Promise.resolve([]);
|
||||
injectStyle();
|
||||
return ensureMermaid()
|
||||
.then(function (mermaid) {
|
||||
configureMermaid(mermaid);
|
||||
return Promise.all(containers.map(function (c) { return renderOne(c, mermaid); }));
|
||||
})
|
||||
.catch(function (err) {
|
||||
console.error('[mermaid-viewer] 初始化失败', err);
|
||||
containers.forEach(function (c) {
|
||||
c.classList.add('mv-diagram');
|
||||
c.innerHTML = '<div class="mv-stage"><div class="mv-err">初始化失败:' +
|
||||
(err && err.message ? err.message : err) + '</div></div>';
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
global.MermaidViewer = { init: init, version: '2' };
|
||||
})(window);
|
||||
Reference in New Issue
Block a user