@tnotesjs/core 0.1.15 → 0.1.16

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