@tnotesjs/core 0.0.1 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +84 -84
- package/dist/{chunk-CSSIRO3I.js → chunk-NI5B4S3T.js} +40 -3
- package/dist/cli/index.js +1 -1
- package/dist/vitepress/config/index.js +60 -17
- package/package.json +77 -77
- package/types/config.ts +61 -61
- package/types/index.ts +11 -11
- package/types/note.ts +33 -33
- package/vitepress/assets/icons/icon__check.svg +3 -3
- package/vitepress/assets/icons/icon__clipboard.svg +7 -7
- package/vitepress/assets/icons/icon__close.svg +1 -1
- package/vitepress/assets/icons/icon__collapse.svg +1 -1
- package/vitepress/assets/icons/icon__confirm.svg +1 -1
- package/vitepress/assets/icons/icon__copy.svg +4 -4
- package/vitepress/assets/icons/icon__focus.svg +1 -1
- package/vitepress/assets/icons/icon__fold.svg +3 -3
- package/vitepress/assets/icons/icon__folder.svg +1 -1
- package/vitepress/assets/icons/icon__fullscreen.svg +1 -1
- package/vitepress/assets/icons/icon__fullscreen_exit.svg +1 -1
- package/vitepress/assets/icons/icon__github.svg +3 -3
- package/vitepress/assets/icons/icon__mindmap.svg +1 -1
- package/vitepress/assets/icons/icon__next.svg +1 -1
- package/vitepress/assets/icons/icon__number_gray.svg +1 -1
- package/vitepress/assets/icons/icon__number_purple.svg +1 -1
- package/vitepress/assets/icons/icon__prev.svg +1 -1
- package/vitepress/assets/icons/icon__restore.svg +1 -1
- package/vitepress/assets/icons/icon__rotate.svg +4 -4
- package/vitepress/assets/icons/icon__search.svg +1 -1
- package/vitepress/assets/icons/icon__sidebar_collapsed.svg +1 -1
- package/vitepress/assets/icons/icon__sidebar_opened.svg +1 -1
- package/vitepress/assets/icons/icon__totop.svg +5 -5
- package/vitepress/assets/icons/icon__vscode.svg +5 -5
- package/vitepress/assets/icons/icon__zoom_fit.svg +1 -1
- package/vitepress/assets/icons/icon__zoom_in.svg +1 -1
- package/vitepress/assets/icons/icon__zoom_out.svg +1 -1
- package/vitepress/assets/icons/icon__zoom_reset.svg +1 -1
- package/vitepress/assets/icons/index.ts +38 -38
- package/vitepress/components/BilibiliOutsidePlayer/BilibiliOutsidePlayer.vue +20 -20
- package/vitepress/components/CodeBlockFullscreen/CodeBlockFullscreen.vue +373 -373
- package/vitepress/components/CodeBlockFullscreen/index.ts +115 -115
- package/vitepress/components/CodeBlockFullscreen/styles.css +64 -64
- package/vitepress/components/Discussions/Discussions.module.scss +32 -32
- package/vitepress/components/Discussions/Discussions.vue +211 -211
- package/vitepress/components/EnWordList/EnWordList.module.scss +124 -124
- package/vitepress/components/EnWordList/EnWordList.vue +543 -543
- package/vitepress/components/EnWordList/RightClickMenu.module.scss +22 -22
- package/vitepress/components/EnWordList/RightClickMenu.vue +66 -66
- package/vitepress/components/Footprints/Footprints.module.scss +93 -93
- package/vitepress/components/Footprints/Footprints.vue +377 -377
- package/vitepress/components/Layout/AboutModal.module.scss +233 -233
- package/vitepress/components/Layout/AboutModal.vue +105 -105
- package/vitepress/components/Layout/AboutPanel.vue +266 -266
- package/vitepress/components/Layout/ContentCollapse.vue +603 -603
- package/vitepress/components/Layout/CustomSidebar.vue +605 -605
- package/vitepress/components/Layout/DocBeforeControls.vue +139 -139
- package/vitepress/components/Layout/DocFooter.vue +225 -225
- package/vitepress/components/Layout/ImagePreview.module.scss +201 -201
- package/vitepress/components/Layout/ImagePreview.vue +281 -281
- package/vitepress/components/Layout/Layout.module.scss +661 -661
- package/vitepress/components/Layout/Layout.vue +542 -542
- package/vitepress/components/Layout/NoteStatus.vue +140 -140
- package/vitepress/components/Layout/SidebarItems.vue +263 -263
- package/vitepress/components/Layout/SidebarNavBefore.vue +92 -92
- package/vitepress/components/Layout/Swiper.vue +167 -167
- package/vitepress/components/Layout/ToggleFullContent.module.scss +11 -11
- package/vitepress/components/Layout/ToggleFullContent.vue +34 -34
- package/vitepress/components/Layout/ToggleSidebar.module.scss +11 -11
- package/vitepress/components/Layout/ToggleSidebar.vue +35 -35
- package/vitepress/components/Layout/composables/useCollapseControl.ts +88 -88
- package/vitepress/components/Layout/composables/useNoteConfig.ts +121 -121
- package/vitepress/components/Layout/composables/useNoteSave.ts +173 -173
- package/vitepress/components/Layout/composables/useNoteValidation.ts +85 -85
- package/vitepress/components/Layout/composables/useRedirect.ts +110 -110
- package/vitepress/components/Layout/composables/useVSCodeIntegration.ts +85 -85
- package/vitepress/components/Layout/homeReadme.data.ts +124 -124
- package/vitepress/components/LoadingPage/LoadingPage.vue +192 -192
- package/vitepress/components/MarkMap/MarkMap.module.scss +159 -159
- package/vitepress/components/MarkMap/MarkMap.vue +404 -404
- package/vitepress/components/Mermaid/Mermaid.module.scss +275 -275
- package/vitepress/components/Mermaid/Mermaid.vue +364 -364
- package/vitepress/components/NotesTable/NotesTable.module.scss +77 -77
- package/vitepress/components/NotesTable/NotesTable.vue +98 -98
- package/vitepress/components/NotesTable/README.md +67 -67
- package/vitepress/components/Settings/Settings.module.scss +433 -433
- package/vitepress/components/Settings/Settings.vue +306 -306
- package/vitepress/components/SidebarCard/MindMapView.vue +483 -483
- package/vitepress/components/SidebarCard/NotesTrendChart.vue +108 -108
- package/vitepress/components/SidebarCard/SidebarCard.vue +948 -948
- package/vitepress/components/Tooltip/Tooltip.vue +70 -70
- package/vitepress/components/constants.ts +91 -91
- package/vitepress/components/notesConfig.data.ts +73 -73
- package/vitepress/components/sidebar.data.ts +59 -59
- package/vitepress/components/tnotes-config.data.ts +21 -21
- package/vitepress/components/utils.ts +26 -26
- package/vitepress/config/index.ts +141 -141
- package/vitepress/configs/constants.ts +26 -26
- package/vitepress/configs/head.config.ts +25 -25
- package/vitepress/configs/index.ts +9 -9
- package/vitepress/configs/markdown-it.d.ts +23 -23
- package/vitepress/configs/markdown.config.ts +366 -366
- package/vitepress/configs/theme.config.ts +108 -108
- package/vitepress/plugins/buildProgressPlugin.ts +466 -390
- package/vitepress/plugins/getNoteByConfigIdPlugin.ts +107 -107
- package/vitepress/plugins/renameNotePlugin.ts +60 -60
- package/vitepress/plugins/updateConfigPlugin.ts +63 -63
- package/vitepress/theme/index.ts +95 -95
- package/vitepress/theme/styles/base.scss +50 -50
- package/vitepress/theme/styles/components/404.scss +31 -31
- package/vitepress/theme/styles/components/collapse.scss +175 -175
- package/vitepress/theme/styles/components/markmap.scss +101 -101
- package/vitepress/theme/styles/components/swiper.scss +255 -255
- package/vitepress/theme/styles/index.scss +25 -25
- package/vitepress/theme/styles/layout.scss +62 -62
- package/vitepress/theme/styles/utilities.scss +39 -39
- package/vitepress/theme/styles/vitepress-override.scss +25 -25
|
@@ -1,364 +1,364 @@
|
|
|
1
|
-
<template>
|
|
2
|
-
<div
|
|
3
|
-
ref="mermaidRef"
|
|
4
|
-
:class="[$style.mermaidWrapper, { [$style.showToolbar]: showToolbar }]"
|
|
5
|
-
@mouseenter="showToolbar = true"
|
|
6
|
-
@mouseleave="showToolbar = false"
|
|
7
|
-
>
|
|
8
|
-
<!-- 工具栏 -->
|
|
9
|
-
<div
|
|
10
|
-
v-if="!loading && !error"
|
|
11
|
-
:class="[$style.toolbar, { [$style.visible]: showToolbar }]"
|
|
12
|
-
>
|
|
13
|
-
<button
|
|
14
|
-
@click="zoomIn"
|
|
15
|
-
:class="[$style.toolbarBtn, $style.iconBtn]"
|
|
16
|
-
title="放大 (Ctrl + +)"
|
|
17
|
-
>
|
|
18
|
-
<img :src="icon__zoom_in" alt="放大" :class="$style.btnIcon" />
|
|
19
|
-
</button>
|
|
20
|
-
<button
|
|
21
|
-
@click="zoomOut"
|
|
22
|
-
:class="[$style.toolbarBtn, $style.iconBtn]"
|
|
23
|
-
title="缩小 (Ctrl + -)"
|
|
24
|
-
>
|
|
25
|
-
<img :src="icon__zoom_out" alt="缩小" :class="$style.btnIcon" />
|
|
26
|
-
</button>
|
|
27
|
-
<button
|
|
28
|
-
@click="resetZoom"
|
|
29
|
-
:class="[$style.toolbarBtn, $style.iconBtn]"
|
|
30
|
-
title="重置缩放 (Ctrl + 0)"
|
|
31
|
-
>
|
|
32
|
-
<img :src="icon__zoom_reset" alt="重置" :class="$style.btnIcon" />
|
|
33
|
-
</button>
|
|
34
|
-
<button
|
|
35
|
-
@click="fitToScreen"
|
|
36
|
-
:class="[$style.toolbarBtn, $style.iconBtn]"
|
|
37
|
-
title="适应屏幕"
|
|
38
|
-
>
|
|
39
|
-
<img :src="icon__zoom_fit" alt="适应屏幕" :class="$style.btnIcon" />
|
|
40
|
-
</button>
|
|
41
|
-
<button
|
|
42
|
-
@click="toggleFullscreen"
|
|
43
|
-
:class="[$style.toolbarBtn, $style.iconBtn]"
|
|
44
|
-
title="全屏 (F11)"
|
|
45
|
-
>
|
|
46
|
-
<img
|
|
47
|
-
v-if="isFullscreen"
|
|
48
|
-
:src="icon__fullscreen_exit"
|
|
49
|
-
alt="退出全屏"
|
|
50
|
-
:class="$style.btnIcon"
|
|
51
|
-
/>
|
|
52
|
-
<img
|
|
53
|
-
v-else
|
|
54
|
-
:src="icon__fullscreen"
|
|
55
|
-
alt="全屏"
|
|
56
|
-
:class="$style.btnIcon"
|
|
57
|
-
/>
|
|
58
|
-
</button>
|
|
59
|
-
<span :class="$style.zoomLevel">{{ Math.round(scale * 100) }}%</span>
|
|
60
|
-
</div>
|
|
61
|
-
|
|
62
|
-
<!-- 图表容器 -->
|
|
63
|
-
<div
|
|
64
|
-
:class="[$style.mermaidContainer, { [$style.fullscreen]: isFullscreen }]"
|
|
65
|
-
@click="handleContainerClick"
|
|
66
|
-
>
|
|
67
|
-
<div v-if="loading" :class="$style.mermaidLoading">
|
|
68
|
-
<div :class="$style.spinner"></div>
|
|
69
|
-
<p>加载图表中...</p>
|
|
70
|
-
</div>
|
|
71
|
-
<div v-else-if="error" :class="$style.mermaidError">
|
|
72
|
-
<span :class="$style.errorIcon">⚠️</span>
|
|
73
|
-
<p>{{ error }}</p>
|
|
74
|
-
</div>
|
|
75
|
-
<div
|
|
76
|
-
v-show="!loading && !error"
|
|
77
|
-
ref="diagramRef"
|
|
78
|
-
:class="[$style.mermaidDiagram, props.class]"
|
|
79
|
-
:style="{
|
|
80
|
-
transform: `scale(${scale})`,
|
|
81
|
-
transformOrigin: 'center center',
|
|
82
|
-
}"
|
|
83
|
-
@wheel.prevent="handleWheel"
|
|
84
|
-
></div>
|
|
85
|
-
</div>
|
|
86
|
-
</div>
|
|
87
|
-
</template>
|
|
88
|
-
|
|
89
|
-
<script setup lang="ts">
|
|
90
|
-
import mermaid from 'mermaid'
|
|
91
|
-
import { nextTick, onMounted, onBeforeUnmount, ref, watch } from 'vue'
|
|
92
|
-
import {
|
|
93
|
-
icon__zoom_in,
|
|
94
|
-
icon__zoom_out,
|
|
95
|
-
icon__zoom_reset,
|
|
96
|
-
icon__zoom_fit,
|
|
97
|
-
icon__fullscreen,
|
|
98
|
-
icon__fullscreen_exit,
|
|
99
|
-
} from '../../assets/icons'
|
|
100
|
-
|
|
101
|
-
const props = defineProps({
|
|
102
|
-
graph: {
|
|
103
|
-
type: String,
|
|
104
|
-
required: true,
|
|
105
|
-
},
|
|
106
|
-
id: {
|
|
107
|
-
type: String,
|
|
108
|
-
required: true,
|
|
109
|
-
},
|
|
110
|
-
class: {
|
|
111
|
-
type: String,
|
|
112
|
-
default: 'mermaid',
|
|
113
|
-
},
|
|
114
|
-
})
|
|
115
|
-
|
|
116
|
-
const mermaidRef = ref<HTMLElement | null>(null)
|
|
117
|
-
const diagramRef = ref<HTMLElement | null>(null)
|
|
118
|
-
const loading = ref(true)
|
|
119
|
-
const error = ref<string | null>(null)
|
|
120
|
-
const scale = ref(1)
|
|
121
|
-
const isFullscreen = ref(false)
|
|
122
|
-
const showToolbar = ref(false)
|
|
123
|
-
|
|
124
|
-
// ===================================
|
|
125
|
-
// #region 工具栏显示控制
|
|
126
|
-
// ===================================
|
|
127
|
-
let hideTimer: ReturnType<typeof setTimeout> | null = null
|
|
128
|
-
|
|
129
|
-
function handleContainerClick() {
|
|
130
|
-
// 移动端点击切换工具栏显示
|
|
131
|
-
if (window.innerWidth <= 768) {
|
|
132
|
-
showToolbar.value = !showToolbar.value
|
|
133
|
-
|
|
134
|
-
// 3秒后自动隐藏
|
|
135
|
-
if (showToolbar.value) {
|
|
136
|
-
if (hideTimer) clearTimeout(hideTimer)
|
|
137
|
-
hideTimer = setTimeout(() => {
|
|
138
|
-
showToolbar.value = false
|
|
139
|
-
}, 3000)
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
// #endregion
|
|
144
|
-
|
|
145
|
-
// ===================================
|
|
146
|
-
// #region 缩放控制
|
|
147
|
-
// ===================================
|
|
148
|
-
const MIN_SCALE = 0.1
|
|
149
|
-
const MAX_SCALE = 5
|
|
150
|
-
const SCALE_STEP = 0.1
|
|
151
|
-
|
|
152
|
-
function zoomIn() {
|
|
153
|
-
scale.value = Math.min(scale.value + SCALE_STEP, MAX_SCALE)
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
function zoomOut() {
|
|
157
|
-
scale.value = Math.max(scale.value - SCALE_STEP, MIN_SCALE)
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
function resetZoom() {
|
|
161
|
-
scale.value = 1
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
function fitToScreen() {
|
|
165
|
-
if (!diagramRef.value || !mermaidRef.value) return
|
|
166
|
-
|
|
167
|
-
const svg = diagramRef.value.querySelector('svg')
|
|
168
|
-
if (!svg) return
|
|
169
|
-
|
|
170
|
-
const containerRect = mermaidRef.value.getBoundingClientRect()
|
|
171
|
-
const svgRect = svg.getBoundingClientRect()
|
|
172
|
-
|
|
173
|
-
const scaleX = (containerRect.width * 0.9) / svgRect.width
|
|
174
|
-
const scaleY = (containerRect.height * 0.9) / svgRect.height
|
|
175
|
-
|
|
176
|
-
scale.value = Math.min(scaleX, scaleY, 1)
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
function handleWheel(e: WheelEvent) {
|
|
180
|
-
if (e.ctrlKey || e.metaKey) {
|
|
181
|
-
e.preventDefault()
|
|
182
|
-
const delta = e.deltaY > 0 ? -SCALE_STEP : SCALE_STEP
|
|
183
|
-
scale.value = Math.max(MIN_SCALE, Math.min(MAX_SCALE, scale.value + delta))
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
// #endregion
|
|
187
|
-
|
|
188
|
-
// ===================================
|
|
189
|
-
// #region 全屏控制
|
|
190
|
-
// ===================================
|
|
191
|
-
function toggleFullscreen() {
|
|
192
|
-
if (!mermaidRef.value) return
|
|
193
|
-
|
|
194
|
-
if (!isFullscreen.value) {
|
|
195
|
-
if (mermaidRef.value.requestFullscreen) {
|
|
196
|
-
mermaidRef.value.requestFullscreen()
|
|
197
|
-
}
|
|
198
|
-
} else {
|
|
199
|
-
if (document.exitFullscreen) {
|
|
200
|
-
document.exitFullscreen()
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
function handleFullscreenChange() {
|
|
206
|
-
isFullscreen.value = !!document.fullscreenElement
|
|
207
|
-
}
|
|
208
|
-
// #endregion
|
|
209
|
-
|
|
210
|
-
// ===================================
|
|
211
|
-
// #region 键盘快捷键
|
|
212
|
-
// ===================================
|
|
213
|
-
function handleKeydown(e: KeyboardEvent) {
|
|
214
|
-
if (e.ctrlKey || e.metaKey) {
|
|
215
|
-
if (e.key === '=' || e.key === '+') {
|
|
216
|
-
e.preventDefault()
|
|
217
|
-
zoomIn()
|
|
218
|
-
} else if (e.key === '-' || e.key === '_') {
|
|
219
|
-
e.preventDefault()
|
|
220
|
-
zoomOut()
|
|
221
|
-
} else if (e.key === '0') {
|
|
222
|
-
e.preventDefault()
|
|
223
|
-
resetZoom()
|
|
224
|
-
}
|
|
225
|
-
} else if (e.key === 'F11') {
|
|
226
|
-
e.preventDefault()
|
|
227
|
-
toggleFullscreen()
|
|
228
|
-
} else if (e.key === 'Escape' && isFullscreen.value) {
|
|
229
|
-
toggleFullscreen()
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
// #endregion
|
|
233
|
-
|
|
234
|
-
// ===================================
|
|
235
|
-
// #region Mermaid 渲染
|
|
236
|
-
// ===================================
|
|
237
|
-
// 获取当前主题
|
|
238
|
-
const getCurrentTheme = () => {
|
|
239
|
-
return document.documentElement.classList.contains('dark')
|
|
240
|
-
? 'dark'
|
|
241
|
-
: 'default'
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
// 初始化 Mermaid
|
|
245
|
-
const initMermaid = async () => {
|
|
246
|
-
try {
|
|
247
|
-
mermaid.initialize({
|
|
248
|
-
startOnLoad: false,
|
|
249
|
-
theme: getCurrentTheme(),
|
|
250
|
-
securityLevel: 'loose',
|
|
251
|
-
fontFamily: 'inherit',
|
|
252
|
-
})
|
|
253
|
-
} catch (err) {
|
|
254
|
-
console.error('Mermaid initialization error:', err)
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
// 渲染图表
|
|
259
|
-
const renderDiagram = async () => {
|
|
260
|
-
// 等待下一个 DOM 更新周期
|
|
261
|
-
await nextTick()
|
|
262
|
-
|
|
263
|
-
if (!diagramRef.value) {
|
|
264
|
-
// 再等待一小段时间确保 DOM 完全渲染
|
|
265
|
-
await new Promise((resolve) => setTimeout(resolve, 0))
|
|
266
|
-
if (!diagramRef.value) {
|
|
267
|
-
// console.warn('diagramRef is still null')
|
|
268
|
-
return
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
try {
|
|
273
|
-
loading.value = true
|
|
274
|
-
error.value = null
|
|
275
|
-
|
|
276
|
-
// 每次渲染前都重新初始化主题
|
|
277
|
-
mermaid.initialize({
|
|
278
|
-
startOnLoad: false,
|
|
279
|
-
theme: getCurrentTheme(),
|
|
280
|
-
securityLevel: 'loose',
|
|
281
|
-
fontFamily: 'inherit',
|
|
282
|
-
})
|
|
283
|
-
|
|
284
|
-
const { svg, bindFunctions } = await mermaid.render(
|
|
285
|
-
props.id,
|
|
286
|
-
decodeURIComponent(props.graph)
|
|
287
|
-
)
|
|
288
|
-
|
|
289
|
-
diagramRef.value.innerHTML = svg
|
|
290
|
-
|
|
291
|
-
// 绑定交互函数(如果有)
|
|
292
|
-
if (bindFunctions) {
|
|
293
|
-
bindFunctions(diagramRef.value)
|
|
294
|
-
}
|
|
295
|
-
} catch (err) {
|
|
296
|
-
error.value = `Failed to render diagram: ${err.message}`
|
|
297
|
-
console.error('Mermaid render error:', err)
|
|
298
|
-
} finally {
|
|
299
|
-
loading.value = false
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
// 监听主题变化
|
|
304
|
-
const observeTheme = () => {
|
|
305
|
-
const observer = new MutationObserver((mutations) => {
|
|
306
|
-
mutations.forEach((mutation) => {
|
|
307
|
-
if (
|
|
308
|
-
mutation.type === 'attributes' &&
|
|
309
|
-
mutation.attributeName === 'class'
|
|
310
|
-
) {
|
|
311
|
-
// 检查是否是主题变化
|
|
312
|
-
const isDark = document.documentElement.classList.contains('dark')
|
|
313
|
-
const wasDark = mutation.oldValue?.includes('dark')
|
|
314
|
-
|
|
315
|
-
// 只有当主题实际发生变化时才重新渲染
|
|
316
|
-
if ((isDark && !wasDark) || (!isDark && wasDark)) {
|
|
317
|
-
renderDiagram()
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
})
|
|
321
|
-
})
|
|
322
|
-
|
|
323
|
-
observer.observe(document.documentElement, {
|
|
324
|
-
attributes: true,
|
|
325
|
-
attributeFilter: ['class'],
|
|
326
|
-
attributeOldValue: true,
|
|
327
|
-
})
|
|
328
|
-
|
|
329
|
-
return observer
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
onMounted(async () => {
|
|
333
|
-
await initMermaid()
|
|
334
|
-
await renderDiagram()
|
|
335
|
-
|
|
336
|
-
// 观察主题变化
|
|
337
|
-
const themeObserver = observeTheme()
|
|
338
|
-
|
|
339
|
-
// 监听键盘事件
|
|
340
|
-
document.addEventListener('keydown', handleKeydown)
|
|
341
|
-
|
|
342
|
-
// 监听全屏变化
|
|
343
|
-
document.addEventListener('fullscreenchange', handleFullscreenChange)
|
|
344
|
-
|
|
345
|
-
// 清理函数
|
|
346
|
-
onBeforeUnmount(() => {
|
|
347
|
-
themeObserver.disconnect()
|
|
348
|
-
document.removeEventListener('keydown', handleKeydown)
|
|
349
|
-
document.removeEventListener('fullscreenchange', handleFullscreenChange)
|
|
350
|
-
})
|
|
351
|
-
})
|
|
352
|
-
|
|
353
|
-
// 当图表内容变化时重新渲染并重置缩放
|
|
354
|
-
watch(
|
|
355
|
-
() => props.graph,
|
|
356
|
-
() => {
|
|
357
|
-
renderDiagram()
|
|
358
|
-
resetZoom()
|
|
359
|
-
}
|
|
360
|
-
)
|
|
361
|
-
// #endregion
|
|
362
|
-
</script>
|
|
363
|
-
|
|
364
|
-
<style module src="./Mermaid.module.scss"></style>
|
|
1
|
+
<template>
|
|
2
|
+
<div
|
|
3
|
+
ref="mermaidRef"
|
|
4
|
+
:class="[$style.mermaidWrapper, { [$style.showToolbar]: showToolbar }]"
|
|
5
|
+
@mouseenter="showToolbar = true"
|
|
6
|
+
@mouseleave="showToolbar = false"
|
|
7
|
+
>
|
|
8
|
+
<!-- 工具栏 -->
|
|
9
|
+
<div
|
|
10
|
+
v-if="!loading && !error"
|
|
11
|
+
:class="[$style.toolbar, { [$style.visible]: showToolbar }]"
|
|
12
|
+
>
|
|
13
|
+
<button
|
|
14
|
+
@click="zoomIn"
|
|
15
|
+
:class="[$style.toolbarBtn, $style.iconBtn]"
|
|
16
|
+
title="放大 (Ctrl + +)"
|
|
17
|
+
>
|
|
18
|
+
<img :src="icon__zoom_in" alt="放大" :class="$style.btnIcon" />
|
|
19
|
+
</button>
|
|
20
|
+
<button
|
|
21
|
+
@click="zoomOut"
|
|
22
|
+
:class="[$style.toolbarBtn, $style.iconBtn]"
|
|
23
|
+
title="缩小 (Ctrl + -)"
|
|
24
|
+
>
|
|
25
|
+
<img :src="icon__zoom_out" alt="缩小" :class="$style.btnIcon" />
|
|
26
|
+
</button>
|
|
27
|
+
<button
|
|
28
|
+
@click="resetZoom"
|
|
29
|
+
:class="[$style.toolbarBtn, $style.iconBtn]"
|
|
30
|
+
title="重置缩放 (Ctrl + 0)"
|
|
31
|
+
>
|
|
32
|
+
<img :src="icon__zoom_reset" alt="重置" :class="$style.btnIcon" />
|
|
33
|
+
</button>
|
|
34
|
+
<button
|
|
35
|
+
@click="fitToScreen"
|
|
36
|
+
:class="[$style.toolbarBtn, $style.iconBtn]"
|
|
37
|
+
title="适应屏幕"
|
|
38
|
+
>
|
|
39
|
+
<img :src="icon__zoom_fit" alt="适应屏幕" :class="$style.btnIcon" />
|
|
40
|
+
</button>
|
|
41
|
+
<button
|
|
42
|
+
@click="toggleFullscreen"
|
|
43
|
+
:class="[$style.toolbarBtn, $style.iconBtn]"
|
|
44
|
+
title="全屏 (F11)"
|
|
45
|
+
>
|
|
46
|
+
<img
|
|
47
|
+
v-if="isFullscreen"
|
|
48
|
+
:src="icon__fullscreen_exit"
|
|
49
|
+
alt="退出全屏"
|
|
50
|
+
:class="$style.btnIcon"
|
|
51
|
+
/>
|
|
52
|
+
<img
|
|
53
|
+
v-else
|
|
54
|
+
:src="icon__fullscreen"
|
|
55
|
+
alt="全屏"
|
|
56
|
+
:class="$style.btnIcon"
|
|
57
|
+
/>
|
|
58
|
+
</button>
|
|
59
|
+
<span :class="$style.zoomLevel">{{ Math.round(scale * 100) }}%</span>
|
|
60
|
+
</div>
|
|
61
|
+
|
|
62
|
+
<!-- 图表容器 -->
|
|
63
|
+
<div
|
|
64
|
+
:class="[$style.mermaidContainer, { [$style.fullscreen]: isFullscreen }]"
|
|
65
|
+
@click="handleContainerClick"
|
|
66
|
+
>
|
|
67
|
+
<div v-if="loading" :class="$style.mermaidLoading">
|
|
68
|
+
<div :class="$style.spinner"></div>
|
|
69
|
+
<p>加载图表中...</p>
|
|
70
|
+
</div>
|
|
71
|
+
<div v-else-if="error" :class="$style.mermaidError">
|
|
72
|
+
<span :class="$style.errorIcon">⚠️</span>
|
|
73
|
+
<p>{{ error }}</p>
|
|
74
|
+
</div>
|
|
75
|
+
<div
|
|
76
|
+
v-show="!loading && !error"
|
|
77
|
+
ref="diagramRef"
|
|
78
|
+
:class="[$style.mermaidDiagram, props.class]"
|
|
79
|
+
:style="{
|
|
80
|
+
transform: `scale(${scale})`,
|
|
81
|
+
transformOrigin: 'center center',
|
|
82
|
+
}"
|
|
83
|
+
@wheel.prevent="handleWheel"
|
|
84
|
+
></div>
|
|
85
|
+
</div>
|
|
86
|
+
</div>
|
|
87
|
+
</template>
|
|
88
|
+
|
|
89
|
+
<script setup lang="ts">
|
|
90
|
+
import mermaid from 'mermaid'
|
|
91
|
+
import { nextTick, onMounted, onBeforeUnmount, ref, watch } from 'vue'
|
|
92
|
+
import {
|
|
93
|
+
icon__zoom_in,
|
|
94
|
+
icon__zoom_out,
|
|
95
|
+
icon__zoom_reset,
|
|
96
|
+
icon__zoom_fit,
|
|
97
|
+
icon__fullscreen,
|
|
98
|
+
icon__fullscreen_exit,
|
|
99
|
+
} from '../../assets/icons'
|
|
100
|
+
|
|
101
|
+
const props = defineProps({
|
|
102
|
+
graph: {
|
|
103
|
+
type: String,
|
|
104
|
+
required: true,
|
|
105
|
+
},
|
|
106
|
+
id: {
|
|
107
|
+
type: String,
|
|
108
|
+
required: true,
|
|
109
|
+
},
|
|
110
|
+
class: {
|
|
111
|
+
type: String,
|
|
112
|
+
default: 'mermaid',
|
|
113
|
+
},
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
const mermaidRef = ref<HTMLElement | null>(null)
|
|
117
|
+
const diagramRef = ref<HTMLElement | null>(null)
|
|
118
|
+
const loading = ref(true)
|
|
119
|
+
const error = ref<string | null>(null)
|
|
120
|
+
const scale = ref(1)
|
|
121
|
+
const isFullscreen = ref(false)
|
|
122
|
+
const showToolbar = ref(false)
|
|
123
|
+
|
|
124
|
+
// ===================================
|
|
125
|
+
// #region 工具栏显示控制
|
|
126
|
+
// ===================================
|
|
127
|
+
let hideTimer: ReturnType<typeof setTimeout> | null = null
|
|
128
|
+
|
|
129
|
+
function handleContainerClick() {
|
|
130
|
+
// 移动端点击切换工具栏显示
|
|
131
|
+
if (window.innerWidth <= 768) {
|
|
132
|
+
showToolbar.value = !showToolbar.value
|
|
133
|
+
|
|
134
|
+
// 3秒后自动隐藏
|
|
135
|
+
if (showToolbar.value) {
|
|
136
|
+
if (hideTimer) clearTimeout(hideTimer)
|
|
137
|
+
hideTimer = setTimeout(() => {
|
|
138
|
+
showToolbar.value = false
|
|
139
|
+
}, 3000)
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
// #endregion
|
|
144
|
+
|
|
145
|
+
// ===================================
|
|
146
|
+
// #region 缩放控制
|
|
147
|
+
// ===================================
|
|
148
|
+
const MIN_SCALE = 0.1
|
|
149
|
+
const MAX_SCALE = 5
|
|
150
|
+
const SCALE_STEP = 0.1
|
|
151
|
+
|
|
152
|
+
function zoomIn() {
|
|
153
|
+
scale.value = Math.min(scale.value + SCALE_STEP, MAX_SCALE)
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function zoomOut() {
|
|
157
|
+
scale.value = Math.max(scale.value - SCALE_STEP, MIN_SCALE)
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function resetZoom() {
|
|
161
|
+
scale.value = 1
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function fitToScreen() {
|
|
165
|
+
if (!diagramRef.value || !mermaidRef.value) return
|
|
166
|
+
|
|
167
|
+
const svg = diagramRef.value.querySelector('svg')
|
|
168
|
+
if (!svg) return
|
|
169
|
+
|
|
170
|
+
const containerRect = mermaidRef.value.getBoundingClientRect()
|
|
171
|
+
const svgRect = svg.getBoundingClientRect()
|
|
172
|
+
|
|
173
|
+
const scaleX = (containerRect.width * 0.9) / svgRect.width
|
|
174
|
+
const scaleY = (containerRect.height * 0.9) / svgRect.height
|
|
175
|
+
|
|
176
|
+
scale.value = Math.min(scaleX, scaleY, 1)
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function handleWheel(e: WheelEvent) {
|
|
180
|
+
if (e.ctrlKey || e.metaKey) {
|
|
181
|
+
e.preventDefault()
|
|
182
|
+
const delta = e.deltaY > 0 ? -SCALE_STEP : SCALE_STEP
|
|
183
|
+
scale.value = Math.max(MIN_SCALE, Math.min(MAX_SCALE, scale.value + delta))
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
// #endregion
|
|
187
|
+
|
|
188
|
+
// ===================================
|
|
189
|
+
// #region 全屏控制
|
|
190
|
+
// ===================================
|
|
191
|
+
function toggleFullscreen() {
|
|
192
|
+
if (!mermaidRef.value) return
|
|
193
|
+
|
|
194
|
+
if (!isFullscreen.value) {
|
|
195
|
+
if (mermaidRef.value.requestFullscreen) {
|
|
196
|
+
mermaidRef.value.requestFullscreen()
|
|
197
|
+
}
|
|
198
|
+
} else {
|
|
199
|
+
if (document.exitFullscreen) {
|
|
200
|
+
document.exitFullscreen()
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function handleFullscreenChange() {
|
|
206
|
+
isFullscreen.value = !!document.fullscreenElement
|
|
207
|
+
}
|
|
208
|
+
// #endregion
|
|
209
|
+
|
|
210
|
+
// ===================================
|
|
211
|
+
// #region 键盘快捷键
|
|
212
|
+
// ===================================
|
|
213
|
+
function handleKeydown(e: KeyboardEvent) {
|
|
214
|
+
if (e.ctrlKey || e.metaKey) {
|
|
215
|
+
if (e.key === '=' || e.key === '+') {
|
|
216
|
+
e.preventDefault()
|
|
217
|
+
zoomIn()
|
|
218
|
+
} else if (e.key === '-' || e.key === '_') {
|
|
219
|
+
e.preventDefault()
|
|
220
|
+
zoomOut()
|
|
221
|
+
} else if (e.key === '0') {
|
|
222
|
+
e.preventDefault()
|
|
223
|
+
resetZoom()
|
|
224
|
+
}
|
|
225
|
+
} else if (e.key === 'F11') {
|
|
226
|
+
e.preventDefault()
|
|
227
|
+
toggleFullscreen()
|
|
228
|
+
} else if (e.key === 'Escape' && isFullscreen.value) {
|
|
229
|
+
toggleFullscreen()
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
// #endregion
|
|
233
|
+
|
|
234
|
+
// ===================================
|
|
235
|
+
// #region Mermaid 渲染
|
|
236
|
+
// ===================================
|
|
237
|
+
// 获取当前主题
|
|
238
|
+
const getCurrentTheme = () => {
|
|
239
|
+
return document.documentElement.classList.contains('dark')
|
|
240
|
+
? 'dark'
|
|
241
|
+
: 'default'
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// 初始化 Mermaid
|
|
245
|
+
const initMermaid = async () => {
|
|
246
|
+
try {
|
|
247
|
+
mermaid.initialize({
|
|
248
|
+
startOnLoad: false,
|
|
249
|
+
theme: getCurrentTheme(),
|
|
250
|
+
securityLevel: 'loose',
|
|
251
|
+
fontFamily: 'inherit',
|
|
252
|
+
})
|
|
253
|
+
} catch (err) {
|
|
254
|
+
console.error('Mermaid initialization error:', err)
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// 渲染图表
|
|
259
|
+
const renderDiagram = async () => {
|
|
260
|
+
// 等待下一个 DOM 更新周期
|
|
261
|
+
await nextTick()
|
|
262
|
+
|
|
263
|
+
if (!diagramRef.value) {
|
|
264
|
+
// 再等待一小段时间确保 DOM 完全渲染
|
|
265
|
+
await new Promise((resolve) => setTimeout(resolve, 0))
|
|
266
|
+
if (!diagramRef.value) {
|
|
267
|
+
// console.warn('diagramRef is still null')
|
|
268
|
+
return
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
try {
|
|
273
|
+
loading.value = true
|
|
274
|
+
error.value = null
|
|
275
|
+
|
|
276
|
+
// 每次渲染前都重新初始化主题
|
|
277
|
+
mermaid.initialize({
|
|
278
|
+
startOnLoad: false,
|
|
279
|
+
theme: getCurrentTheme(),
|
|
280
|
+
securityLevel: 'loose',
|
|
281
|
+
fontFamily: 'inherit',
|
|
282
|
+
})
|
|
283
|
+
|
|
284
|
+
const { svg, bindFunctions } = await mermaid.render(
|
|
285
|
+
props.id,
|
|
286
|
+
decodeURIComponent(props.graph)
|
|
287
|
+
)
|
|
288
|
+
|
|
289
|
+
diagramRef.value.innerHTML = svg
|
|
290
|
+
|
|
291
|
+
// 绑定交互函数(如果有)
|
|
292
|
+
if (bindFunctions) {
|
|
293
|
+
bindFunctions(diagramRef.value)
|
|
294
|
+
}
|
|
295
|
+
} catch (err) {
|
|
296
|
+
error.value = `Failed to render diagram: ${err.message}`
|
|
297
|
+
console.error('Mermaid render error:', err)
|
|
298
|
+
} finally {
|
|
299
|
+
loading.value = false
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
// 监听主题变化
|
|
304
|
+
const observeTheme = () => {
|
|
305
|
+
const observer = new MutationObserver((mutations) => {
|
|
306
|
+
mutations.forEach((mutation) => {
|
|
307
|
+
if (
|
|
308
|
+
mutation.type === 'attributes' &&
|
|
309
|
+
mutation.attributeName === 'class'
|
|
310
|
+
) {
|
|
311
|
+
// 检查是否是主题变化
|
|
312
|
+
const isDark = document.documentElement.classList.contains('dark')
|
|
313
|
+
const wasDark = mutation.oldValue?.includes('dark')
|
|
314
|
+
|
|
315
|
+
// 只有当主题实际发生变化时才重新渲染
|
|
316
|
+
if ((isDark && !wasDark) || (!isDark && wasDark)) {
|
|
317
|
+
renderDiagram()
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
})
|
|
321
|
+
})
|
|
322
|
+
|
|
323
|
+
observer.observe(document.documentElement, {
|
|
324
|
+
attributes: true,
|
|
325
|
+
attributeFilter: ['class'],
|
|
326
|
+
attributeOldValue: true,
|
|
327
|
+
})
|
|
328
|
+
|
|
329
|
+
return observer
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
onMounted(async () => {
|
|
333
|
+
await initMermaid()
|
|
334
|
+
await renderDiagram()
|
|
335
|
+
|
|
336
|
+
// 观察主题变化
|
|
337
|
+
const themeObserver = observeTheme()
|
|
338
|
+
|
|
339
|
+
// 监听键盘事件
|
|
340
|
+
document.addEventListener('keydown', handleKeydown)
|
|
341
|
+
|
|
342
|
+
// 监听全屏变化
|
|
343
|
+
document.addEventListener('fullscreenchange', handleFullscreenChange)
|
|
344
|
+
|
|
345
|
+
// 清理函数
|
|
346
|
+
onBeforeUnmount(() => {
|
|
347
|
+
themeObserver.disconnect()
|
|
348
|
+
document.removeEventListener('keydown', handleKeydown)
|
|
349
|
+
document.removeEventListener('fullscreenchange', handleFullscreenChange)
|
|
350
|
+
})
|
|
351
|
+
})
|
|
352
|
+
|
|
353
|
+
// 当图表内容变化时重新渲染并重置缩放
|
|
354
|
+
watch(
|
|
355
|
+
() => props.graph,
|
|
356
|
+
() => {
|
|
357
|
+
renderDiagram()
|
|
358
|
+
resetZoom()
|
|
359
|
+
}
|
|
360
|
+
)
|
|
361
|
+
// #endregion
|
|
362
|
+
</script>
|
|
363
|
+
|
|
364
|
+
<style module src="./Mermaid.module.scss"></style>
|