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
|
{{- if .Page.Params.mermaid -}}
<link rel="stylesheet" href="{{ "css/mermaid.css" | relURL }}">
<script src="https://cdn.jsdelivr.net/npm/mermaid@10.6.1/dist/mermaid.min.js"></script>
<script>
mermaid.initialize({
startOnLoad: true,
flowchart: {
htmlLabels: false,
useMaxWidth: false,
nodeSpacing: 50,
rankSpacing: 50,
defaultRenderer: 'svg'
},
theme: 'default',
securityLevel: 'loose',
themeVariables: {
nodeBorder: '#000',
mainBkg: '#fff',
nodeTextColor: '#000',
fontSize: '14px'
}
});
</script>
<!-- 遮罩层 -->
<div class="mermaid-overlay" id="mermaidOverlay"></div>
<script>
document.addEventListener('DOMContentLoaded', () => {
// 自动调整节点尺寸
const adjustNodeSize = () => {
document.querySelectorAll('.mermaid .node').forEach(node => {
const rect = node.querySelector('rect');
const text = node.querySelector('text');
if (rect && text) {
const bbox = text.getBBox();
const padding = 16;
const minWidth = parseFloat(rect.getAttribute('width')) || 160;
const newWidth = Math.max(bbox.width + padding, minWidth);
rect.setAttribute('width', newWidth);
}
});
};
// 初始化调整
adjustNodeSize();
window.addEventListener('resize', adjustNodeSize);
// 为每个mermaid图表创建独立控制器
document.querySelectorAll('.mermaid-container').forEach(container => {
let currentScale = 1;
let initialClickX = null, initialClickY = null;
let dragOffsetX = 0, dragOffsetY = 0;
let isDragging = false;
let dragStartX = 0, dragStartY = 0;
let isZoomed = false;
const mermaidElement = container.querySelector('.mermaid');
const overlay = document.getElementById('mermaidOverlay');
const originalParent = container.parentElement;
const originalNextSibling = container.nextElementSibling;
// 应用变换
const applyScale = () => {
if (isZoomed && initialClickX !== null && initialClickY !== null) {
const containerRect = container.getBoundingClientRect();
const centerX = containerRect.width / 2;
const centerY = containerRect.height / 2;
const baseTranslateX = centerX - initialClickX * currentScale;
const baseTranslateY = centerY - initialClickY * currentScale;
const translateX = baseTranslateX + dragOffsetX;
const translateY = baseTranslateY + dragOffsetY;
mermaidElement.style.transform = `translate(${translateX}px, ${translateY}px) scale(${currentScale})`;
} else {
mermaidElement.style.transform = `scale(${currentScale})`;
}
};
// 进入放大模式
const handleContainerClick = (e) => {
if (!isZoomed) {
isZoomed = true;
container.classList.add('zoomed');
overlay.style.display = 'flex';
overlay.appendChild(container);
const containerRect = container.getBoundingClientRect();
initialClickX = e.clientX - containerRect.left;
initialClickY = e.clientY - containerRect.top;
dragOffsetX = 0;
dragOffsetY = 0;
const scaleX = window.innerWidth / containerRect.width;
const scaleY = window.innerHeight / containerRect.height;
currentScale = Math.min(scaleX, scaleY, 5);
applyScale();
}
};
// 退出放大模式
const exitZoom = () => {
if (isZoomed) {
isZoomed = false;
container.classList.remove('zoomed');
overlay.style.display = 'none';
if (originalNextSibling) {
originalParent.insertBefore(container, originalNextSibling);
} else {
originalParent.appendChild(container);
}
currentScale = 1;
initialClickX = null;
initialClickY = null;
dragOffsetX = 0;
dragOffsetY = 0;
applyScale();
}
};
// 滚轮事件处理
const handleWheel = (e) => {
if (isZoomed) {
e.preventDefault();
const sensitivity = 0.0006;
const delta = e.deltaY || e.wheelDelta;
const scaleDelta = 1 + (-delta * sensitivity);
currentScale = Math.min(Math.max(0.3, currentScale * scaleDelta), 5);
applyScale();
} else if (e.ctrlKey) {
e.preventDefault();
const sensitivity = 0.0006;
const delta = e.deltaY || e.wheelDelta;
const scaleDelta = 1 + (-delta * sensitivity);
currentScale = Math.min(Math.max(0.3, currentScale * scaleDelta), 5);
applyScale();
}
};
// 事件绑定
container.addEventListener('click', (e) => {
if (!isZoomed) handleContainerClick(e);
});
container.addEventListener('dblclick', exitZoom);
container.addEventListener('wheel', handleWheel);
// 拖拽处理
container.addEventListener('mousedown', (e) => {
if (isZoomed) {
isDragging = true;
dragStartX = e.clientX;
dragStartY = e.clientY;
container.style.cursor = 'grabbing';
}
});
const handleMouseMove = (e) => {
if (isDragging) {
const deltaX = e.clientX - dragStartX;
const deltaY = e.clientY - dragStartY;
dragOffsetX += deltaX;
dragOffsetY += deltaY;
dragStartX = e.clientX;
dragStartY = e.clientY;
applyScale();
}
};
const handleMouseUp = () => {
isDragging = false;
container.style.cursor = 'grab';
};
document.addEventListener('mousemove', handleMouseMove);
document.addEventListener('mouseup', handleMouseUp);
});
// 全局ESC按键监听
document.addEventListener('keydown', (e) => {
if (e.key === 'Escape') {
document.querySelectorAll('.mermaid-container').forEach(container => {
if (container.classList.contains('zoomed')) {
container.dispatchEvent(new Event('dblclick'));
}
});
}
});
});
</script>
{{- end -}}
|