-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmermaid_editor.html
More file actions
655 lines (592 loc) · 24 KB
/
mermaid_editor.html
File metadata and controls
655 lines (592 loc) · 24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>AI 原力注入 - Mermaid 在线编辑器</title>
<style>
:root {
--bg: #f0f2f5;
--card-bg: #ffffff;
--text: #1a1a2e;
--btn-bg: #2f7d32;
--btn-hover: #256b28;
--btn-preview: #3a57af;
--btn-preview-hover: #2e4694;
--btn-download: #2f7d32;
--btn-download-hover: #256b28;
--border: #e0e0e0;
--shadow: 0 8px 24px rgba(0, 0, 0, 0.08);
--radius: 12px;
}
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', sans-serif;
background: var(--bg);
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
padding: 20px;
}
.main-container {
display: flex;
flex-wrap: wrap;
gap: 20px;
max-width: 1400px;
width: 100%;
margin: auto;
align-items: stretch;
}
.card {
background: var(--card-bg);
border-radius: var(--radius);
box-shadow: var(--shadow);
padding: 20px;
display: flex;
flex-direction: column;
}
.editor-card {
flex: 1 1 400px;
min-width: 340px;
}
.preview-card {
flex: 2 1 600px;
min-width: 400px;
position: relative;
}
.card-header {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 12px;
}
.card-title {
font-size: 1.1rem;
font-weight: 600;
color: var(--text);
letter-spacing: 0.3px;
}
textarea, #editor {
flex: 1;
width: 100%;
height: calc(100vh - 200px);
min-height: 600px;
border: 1px solid var(--border);
border-radius: 8px;
font-family: 'Fira Code', 'Cascadia Code', 'Consolas', monospace;
font-size: 14px;
line-height: 1.5;
background: #fafafa;
color: #222;
outline: none;
transition: border 0.2s;
}
#editor {
position: relative;
}
textarea:focus {
border-color: #3a57af;
box-shadow: 0 0 0 2px rgba(58,87,175,0.15);
}
.btn-group {
display: flex;
gap: 10px;
flex-wrap: wrap;
}
.btn {
display: inline-flex;
align-items: center;
gap: 6px;
padding: 10px 18px;
font-size: 0.9rem;
font-weight: 600;
border: none;
border-radius: 8px;
cursor: pointer;
letter-spacing: 0.3px;
transition: all 0.2s ease;
white-space: nowrap;
color: #fff;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.12);
}
.btn:active {
transform: translateY(1px);
}
.btn:disabled {
opacity: 0.5;
cursor: not-allowed;
pointer-events: none;
filter: grayscale(30%);
}
.btn-preview {
background: var(--btn-preview);
}
.btn-preview:hover:not(:disabled) {
background: var(--btn-preview-hover);
box-shadow: 0 6px 16px rgba(58,87,175,0.3);
}
.btn-download {
background: var(--btn-download);
}
.btn-download:hover:not(:disabled) {
background: var(--btn-download-hover);
box-shadow: 0 6px 16px rgba(47,125,50,0.35);
}
.btn-icon {
width: 16px;
height: 16px;
flex-shrink: 0;
stroke: currentColor;
fill: none;
stroke-width: 2.2;
stroke-linecap: round;
stroke-linejoin: round;
}
.preview-area {
flex: 1;
min-height: 300px;
border: 1px solid var(--border);
border-radius: 8px;
padding: 16px;
background: #fdfdfd;
overflow-x: auto;
display: flex;
align-items: center;
justify-content: center;
position: relative;
}
#mermaid-preview {
width: 100%;
display: flex;
align-items: center;
justify-content: center;
min-height: 260px;
}
#mermaid-preview svg {
max-width: 100%;
height: auto;
}
.placeholder {
color: #888;
font-size: 0.95rem;
text-align: center;
padding: 20px;
}
.error-box {
color: #c0392b;
background: #fff5f5;
border: 1px solid #f5c6cb;
padding: 14px;
border-radius: 8px;
font-size: 0.9rem;
max-width: 100%;
word-break: break-word;
font-family: monospace;
white-space: pre-wrap;
}
.fallback {
color: #d6336c;
margin-top: 12px;
font-size: 0.9rem;
text-align: center;
}
.fallback a {
color: #0d6efd;
font-weight: 600;
}
/* Toast */
.toast {
position: fixed;
top: 24px;
left: 50%;
transform: translateX(-50%) translateY(-120px);
background: #1a1a2e;
color: #fff;
padding: 10px 20px;
border-radius: 8px;
font-size: 0.9rem;
font-weight: 500;
z-index: 9999;
pointer-events: none;
transition: transform 0.3s cubic-bezier(0.22, 0.61, 0.36, 1);
box-shadow: 0 8px 20px rgba(0,0,0,0.2);
}
.toast.show {
transform: translateX(-50%) translateY(0);
}
.toast.success {
background: #2f7d32;
}
.toast.error {
background: #c0392b;
}
@media (max-width: 800px) {
.main-container {
flex-direction: column;
}
.editor-card, .preview-card {
flex: 1 1 auto;
min-width: 100%;
}
}
</style>
</head>
<body>
<div class="main-container">
<!-- 编辑器卡片 -->
<div class="card editor-card">
<div class="card-header">
<span class="card-title">✏️ Mermaid 源码</span>
<button id="btn-preview" class="btn btn-preview">
<svg class="btn-icon" viewBox="0 0 24 24">
<path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z"/>
<circle cx="12" cy="12" r="3"/>
</svg>
预览
</button>
</div>
<div id="editor">flowchart TD
P1["Phase 1<br>愿景 -> PRD"] --> P2["Phase 2<br>PRD -> UI/UX"]
P2 --> P3["Phase 3<br>PRD + UI/UX -> 领域模型"]
P3 --> P4["Phase 4<br>领域模型 -> 规格定义"]
P4 --> P5["Phase 5<br>规格 -> 实现与测试"]
P5 --> P6["Phase 6<br>质量保障与验收"]
P6 --> P7["Phase 7<br>部署与交付"]
P7 --> P8["Phase 8<br>变更与演进"]
P8 -. 反馈回路 .-> P4
P8 -. 反馈回路 .-> P5
classDef existing fill:#dff5e1,stroke:#2f7d32,color:#111;
classDef planned fill:#fff4d6,stroke:#b7791f,color:#111;
class P3,P4,P5,P8 existing;
class P1,P2,P6,P7 planned;</div>
<p class="fallback" id="fallback-msg" style="display:none;">
⚠️ Mermaid 库加载失败,请刷新重试或使用
<a href="https://mermaid.live/" target="_blank" rel="noopener">Mermaid Live Editor</a>
</p>
</div>
<!-- 预览卡片 -->
<div class="card preview-card">
<div class="card-header">
<span class="card-title">📊 预览</span>
<button id="btn-download" class="btn btn-download" disabled title="请先成功预览图表">
<svg class="btn-icon" viewBox="0 0 24 24">
<rect x="3" y="3" width="18" height="18" rx="3" ry="3"/>
<circle cx="8.5" cy="8.5" r="1.5"/>
<polyline points="21 15 16 10 5 21"/>
</svg>
下载 PNG
</button>
</div>
<div class="preview-area">
<div id="mermaid-preview">
<span class="placeholder">点击“预览”按钮查看图表</span>
</div>
</div>
</div>
</div>
<!-- Toast -->
<div class="toast" id="toast"></div>
<!-- Ace Editor -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.32.2/ace.js"></script>
<!-- 多 CDN 后备加载 Mermaid -->
<script>
(function() {
// ---------- Ace Editor 初始化 ----------
const editor = ace.edit("editor");
editor.setTheme("ace/theme/chrome");
editor.session.setMode("ace/mode/yaml"); // 使用 yaml 模式以获得一些基础颜色
editor.setOptions({
fontSize: "14px",
showPrintMargin: false,
wrap: true,
useWorker: false
});
// ---------- CDN 加载 ----------
const cdns = [
'https://cdn.jsdelivr.net/npm/mermaid@10/dist/mermaid.min.js',
'https://unpkg.com/mermaid@10/dist/mermaid.min.js',
'https://cdnjs.cloudflare.com/ajax/libs/mermaid/10.9.0/mermaid.min.js',
];
let mermaidLoaded = false;
const fallbackMsg = document.getElementById('fallback-msg');
const previewArea = document.getElementById('mermaid-preview');
const btnPreview = document.getElementById('btn-preview');
const btnDownload = document.getElementById('btn-download');
// 渲染计数器,避免ID冲突
let renderCounter = 0;
function tryLoad(index) {
if (index >= cdns.length) {
fallbackMsg.style.display = 'block';
setButtonsEnabled(false);
return;
}
const script = document.createElement('script');
script.src = cdns[index];
script.onload = () => {
if (!mermaidLoaded) {
mermaidLoaded = true;
initMermaid();
}
};
script.onerror = () => tryLoad(index + 1);
document.head.appendChild(script);
}
function initMermaid() {
if (typeof mermaid === 'undefined') {
fallbackMsg.style.display = 'block';
return;
}
mermaid.initialize({
startOnLoad: false, // 手动控制渲染
theme: 'default',
securityLevel: 'loose',
flowchart: { useMaxWidth: true, htmlLabels: true, curve: 'basis' },
});
setButtonsEnabled(true);
// 自动预览默认内容
doPreview();
}
function setButtonsEnabled(ready) {
// 预览按钮始终可用,但如果Mermaid未加载则禁用
if (btnPreview) {
btnPreview.disabled = !ready;
}
if (btnDownload) {
btnDownload.disabled = true; // 下载按钮初始禁用,预览成功后启用
}
}
// ---------- Toast ----------
let toastTimer = null;
function showToast(msg, type = '') {
const toast = document.getElementById('toast');
if (toastTimer) clearTimeout(toastTimer);
toast.textContent = msg;
toast.className = 'toast ' + type + ' show';
toastTimer = setTimeout(() => toast.classList.remove('show'), 2500);
}
// ---------- 渲染逻辑 ----------
async function renderMermaidCode(code) {
if (!code || code.trim() === '') {
throw new Error('请输入 Mermaid 代码');
}
// 每次使用唯一ID
renderCounter += 1;
const id = `mermaid-svg-${Date.now()}-${renderCounter}`;
const result = await mermaid.render(id, code.trim());
return result.svg;
}
async function doPreview() {
const code = editor.getValue();
if (!mermaidLoaded) {
showToast('⚠️ Mermaid 库尚未加载,请稍后', 'error');
return;
}
// 设置加载状态
btnPreview.disabled = true;
btnDownload.disabled = true;
previewArea.innerHTML = '<span class="placeholder">⏳ 正在渲染图表...</span>';
try {
const svgString = await renderMermaidCode(code);
previewArea.innerHTML = svgString;
// 确保SVG占满宽度
const svgEl = previewArea.querySelector('svg');
if (svgEl) {
svgEl.style.maxWidth = '100%';
svgEl.style.height = 'auto';
}
btnDownload.disabled = false;
showToast('✅ 渲染成功', 'success');
} catch (err) {
console.error('渲染失败:', err);
const errorMsg = err.message || '未知错误';
previewArea.innerHTML = `<div class="error-box">❌ 渲染失败:${escapeHtml(errorMsg)}</div>`;
btnDownload.disabled = true;
showToast('❌ 渲染失败,请检查代码', 'error');
} finally {
btnPreview.disabled = false;
}
}
function escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
// ---------- 下载 PNG(复用优化后的导出逻辑) ----------
function replaceForeignObjectsWithText(svgElement) {
const clonedSvg = svgElement.cloneNode(true);
const foreignObjects = clonedSvg.querySelectorAll('foreignObject');
foreignObjects.forEach((fo) => {
const div = fo.querySelector('div');
let rawText = '';
if (div) {
rawText = extractTextFromDiv(div);
} else {
rawText = (fo.textContent || '').trim();
}
if (!rawText) {
fo.remove();
return;
}
const textEl = document.createElementNS('http://www.w3.org/2000/svg', 'text');
const foX = parseFloat(fo.getAttribute('x')) || 0;
const foY = parseFloat(fo.getAttribute('y')) || 0;
const foW = parseFloat(fo.getAttribute('width')) || 100;
const foH = parseFloat(fo.getAttribute('height')) || 40;
// 尝试读取样式
let textAnchor = 'middle';
let anchorX = foX + foW / 2;
if (div) {
const style = div.getAttribute('style') || '';
const textAlign = extractStyleValue(style, 'text-align');
if (textAlign === 'left' || textAlign === 'start') {
textAnchor = 'start';
anchorX = foX + 4;
} else if (textAlign === 'right' || textAlign === 'end') {
textAnchor = 'end';
anchorX = foX + foW - 4;
}
}
const lines = rawText.split('\n').filter(l => l.trim().length > 0);
const fontSize = div ? (extractStyleValue(div.getAttribute('style') || '', 'font-size') || '14px') : '14px';
const fontFamily = div ? (extractStyleValue(div.getAttribute('style') || '', 'font-family') || 'sans-serif') : 'sans-serif';
const color = div ? (extractStyleValue(div.getAttribute('style') || '', 'color') || '#333') : '#333';
textEl.setAttribute('x', anchorX);
textEl.setAttribute('fill', color);
textEl.setAttribute('font-family', fontFamily);
textEl.setAttribute('font-size', fontSize);
textEl.setAttribute('text-anchor', textAnchor);
if (lines.length === 1) {
textEl.setAttribute('y', foY + foH / 2);
textEl.setAttribute('dominant-baseline', 'central');
textEl.textContent = lines[0];
} else {
const fsNum = parseFloat(fontSize) || 14;
const lineHeight = fsNum * 1.35;
const totalHeight = lines.length * lineHeight;
const startY = foY + foH / 2 - totalHeight / 2 + lineHeight * 0.8;
textEl.setAttribute('y', startY);
lines.forEach((line, i) => {
const tspan = document.createElementNS('http://www.w3.org/2000/svg', 'tspan');
tspan.setAttribute('x', anchorX);
if (i > 0) tspan.setAttribute('dy', lineHeight);
tspan.textContent = line;
textEl.appendChild(tspan);
});
}
fo.parentNode.replaceChild(textEl, fo);
});
// 移除残留的 foreignObject
clonedSvg.querySelectorAll('foreignObject').forEach(fo => fo.remove());
return clonedSvg;
}
function extractTextFromDiv(div) {
const html = div.innerHTML || '';
const text = html
.replace(/<br\s*\/?>/gi, '\n')
.replace(/ /gi, ' ')
.replace(/<[^>]*>/g, '');
const txt = document.createElement('textarea');
txt.innerHTML = text;
return txt.value.trim();
}
function extractStyleValue(styleString, property) {
if (!styleString) return null;
const regex = new RegExp(property.replace(/-/g, '\\-') + '\\s*:\\s*([^;]+)', 'i');
const match = styleString.match(regex);
return match ? match[1].trim() : null;
}
function getSVGDimensions(svgElement) {
const viewBox = svgElement.getAttribute('viewBox');
if (viewBox) {
const parts = viewBox.trim().split(/\s+/);
if (parts.length >= 4) return { width: parseFloat(parts[2]), height: parseFloat(parts[3]) };
}
let w = parseFloat(svgElement.getAttribute('width'));
let h = parseFloat(svgElement.getAttribute('height'));
if (!isNaN(w) && !isNaN(h) && w > 0 && h > 0) return { width: w, height: h };
const rect = svgElement.getBoundingClientRect();
if (rect.width > 0 && rect.height > 0) return { width: rect.width, height: rect.height };
return { width: 800, height: 600 };
}
function serializeSVG(svgElement) {
let svgString = new XMLSerializer().serializeToString(svgElement);
if (!svgString.includes('xmlns="http://www.w3.org/2000/svg"')) {
svgString = svgString.replace('<svg ', '<svg xmlns="http://www.w3.org/2000/svg" ');
}
if (!svgString.includes('xmlns:xlink')) {
svgString = svgString.replace('<svg ', '<svg xmlns:xlink="http://www.w3.org/1999/xlink" ');
}
return svgString;
}
async function exportToPNG() {
const svgElement = previewArea.querySelector('svg');
if (!svgElement) {
showToast('⚠️ 没有可导出的图表', 'error');
return;
}
btnDownload.disabled = true;
const originalText = btnDownload.textContent;
btnDownload.textContent = '⏳ 导出中...';
try {
const processedSvg = replaceForeignObjectsWithText(svgElement);
const dims = getSVGDimensions(svgElement);
const scale = Math.min(window.devicePixelRatio || 2, 3);
const canvasWidth = Math.ceil(dims.width * scale);
const canvasHeight = Math.ceil(dims.height * scale);
const svgString = serializeSVG(processedSvg);
const blob = new Blob([svgString], { type: 'image/svg+xml;charset=utf-8' });
const url = URL.createObjectURL(blob);
const img = new Image();
img.width = canvasWidth;
img.height = canvasHeight;
await new Promise((resolve, reject) => {
img.onload = resolve;
img.onerror = () => reject(new Error('SVG 图片加载失败'));
img.src = url;
setTimeout(() => reject(new Error('SVG 加载超时')), 12000);
});
const canvas = document.createElement('canvas');
canvas.width = canvasWidth;
canvas.height = canvasHeight;
const ctx = canvas.getContext('2d');
ctx.fillStyle = '#ffffff';
ctx.fillRect(0, 0, canvasWidth, canvasHeight);
ctx.scale(scale, scale);
ctx.drawImage(img, 0, 0, dims.width, dims.height);
URL.revokeObjectURL(url);
const link = document.createElement('a');
link.href = canvas.toDataURL('image/png', 1.0);
link.download = 'mermaid-图表.png';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
showToast('✅ PNG 下载成功', 'success');
} catch (err) {
console.error('导出失败:', err);
showToast('❌ 导出失败: ' + (err.message || '未知错误'), 'error');
} finally {
btnDownload.disabled = false;
btnDownload.textContent = originalText;
}
}
// ---------- 事件绑定 ----------
btnPreview.addEventListener('click', doPreview);
btnDownload.addEventListener('click', exportToPNG);
// 可选:Ctrl+Enter 快捷键预览
editor.commands.addCommand({
name: 'preview',
bindKey: {win: 'Ctrl-Enter', mac: 'Command-Enter'},
exec: function(editor) {
doPreview();
}
});
// 启动加载
tryLoad(0);
})();
</script>
</body>
</html>