@tnotesjs/core 0.1.18 → 0.1.20

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 (114) hide show
  1. package/README.md +87 -87
  2. package/package.json +1 -1
  3. package/types/config.ts +61 -61
  4. package/types/index.ts +11 -11
  5. package/types/note.ts +33 -33
  6. package/vitepress/assets/icons/icon__check.svg +3 -3
  7. package/vitepress/assets/icons/icon__clipboard.svg +7 -7
  8. package/vitepress/assets/icons/icon__close.svg +1 -1
  9. package/vitepress/assets/icons/icon__collapse.svg +1 -1
  10. package/vitepress/assets/icons/icon__confirm.svg +1 -1
  11. package/vitepress/assets/icons/icon__copy.svg +4 -4
  12. package/vitepress/assets/icons/icon__focus.svg +1 -1
  13. package/vitepress/assets/icons/icon__fold.svg +3 -3
  14. package/vitepress/assets/icons/icon__folder.svg +1 -1
  15. package/vitepress/assets/icons/icon__fullscreen.svg +1 -1
  16. package/vitepress/assets/icons/icon__fullscreen_exit.svg +1 -1
  17. package/vitepress/assets/icons/icon__github.svg +3 -3
  18. package/vitepress/assets/icons/icon__mindmap.svg +1 -1
  19. package/vitepress/assets/icons/icon__next.svg +1 -1
  20. package/vitepress/assets/icons/icon__number_gray.svg +1 -1
  21. package/vitepress/assets/icons/icon__number_purple.svg +1 -1
  22. package/vitepress/assets/icons/icon__prev.svg +1 -1
  23. package/vitepress/assets/icons/icon__restore.svg +1 -1
  24. package/vitepress/assets/icons/icon__rotate.svg +4 -4
  25. package/vitepress/assets/icons/icon__search.svg +1 -1
  26. package/vitepress/assets/icons/icon__sidebar_collapsed.svg +1 -1
  27. package/vitepress/assets/icons/icon__sidebar_opened.svg +1 -1
  28. package/vitepress/assets/icons/icon__totop.svg +5 -5
  29. package/vitepress/assets/icons/icon__vscode.svg +5 -5
  30. package/vitepress/assets/icons/icon__zoom_fit.svg +1 -1
  31. package/vitepress/assets/icons/icon__zoom_in.svg +1 -1
  32. package/vitepress/assets/icons/icon__zoom_out.svg +1 -1
  33. package/vitepress/assets/icons/icon__zoom_reset.svg +1 -1
  34. package/vitepress/assets/icons/index.ts +39 -39
  35. package/vitepress/components/BilibiliOutsidePlayer/BilibiliOutsidePlayer.vue +20 -20
  36. package/vitepress/components/CodeBlockFullscreen/CodeBlockFullscreen.vue +369 -369
  37. package/vitepress/components/CodeBlockFullscreen/index.ts +213 -213
  38. package/vitepress/components/CodeBlockFullscreen/styles.css +126 -126
  39. package/vitepress/components/Discussions/Discussions.module.scss +32 -32
  40. package/vitepress/components/Discussions/Discussions.vue +211 -211
  41. package/vitepress/components/EnWordList/EnWordList.module.scss +124 -124
  42. package/vitepress/components/EnWordList/EnWordList.vue +543 -543
  43. package/vitepress/components/EnWordList/RightClickMenu.module.scss +22 -22
  44. package/vitepress/components/EnWordList/RightClickMenu.vue +66 -66
  45. package/vitepress/components/Footprints/Footprints.module.scss +93 -93
  46. package/vitepress/components/Footprints/Footprints.vue +377 -377
  47. package/vitepress/components/Layout/AboutModal.module.scss +233 -233
  48. package/vitepress/components/Layout/AboutModal.vue +105 -105
  49. package/vitepress/components/Layout/AboutPanel.vue +270 -270
  50. package/vitepress/components/Layout/ContentCollapse.vue +603 -603
  51. package/vitepress/components/Layout/CustomSidebar.vue +817 -817
  52. package/vitepress/components/Layout/DocBeforeControls.vue +149 -149
  53. package/vitepress/components/Layout/DocFooter.vue +233 -233
  54. package/vitepress/components/Layout/ImagePreview.module.scss +201 -201
  55. package/vitepress/components/Layout/ImagePreview.vue +281 -281
  56. package/vitepress/components/Layout/Layout.module.scss +661 -661
  57. package/vitepress/components/Layout/Layout.vue +621 -621
  58. package/vitepress/components/Layout/NoteStatus.vue +140 -140
  59. package/vitepress/components/Layout/SidebarItems.vue +1083 -1083
  60. package/vitepress/components/Layout/SidebarNavBefore.vue +171 -171
  61. package/vitepress/components/Layout/SidebarResizeHandle.vue +319 -319
  62. package/vitepress/components/Layout/Swiper.vue +167 -167
  63. package/vitepress/components/Layout/ToggleFullContent.module.scss +11 -11
  64. package/vitepress/components/Layout/ToggleFullContent.vue +35 -35
  65. package/vitepress/components/Layout/ToggleSidebar.module.scss +11 -11
  66. package/vitepress/components/Layout/ToggleSidebar.vue +26 -26
  67. package/vitepress/components/Layout/composables/useCollapseControl.ts +88 -88
  68. package/vitepress/components/Layout/composables/useNoteConfig.ts +119 -119
  69. package/vitepress/components/Layout/composables/useNoteSave.ts +178 -178
  70. package/vitepress/components/Layout/composables/useNoteValidation.ts +85 -85
  71. package/vitepress/components/Layout/composables/useRedirect.ts +113 -113
  72. package/vitepress/components/Layout/composables/useSidebarLayout.ts +122 -122
  73. package/vitepress/components/Layout/composables/useVSCodeIntegration.ts +85 -85
  74. package/vitepress/components/Layout/homeReadme.data.ts +124 -124
  75. package/vitepress/components/MarkMap/MarkMap.module.scss +159 -159
  76. package/vitepress/components/MarkMap/MarkMap.vue +405 -405
  77. package/vitepress/components/Mermaid/Mermaid.module.scss +275 -275
  78. package/vitepress/components/Mermaid/Mermaid.vue +365 -365
  79. package/vitepress/components/NotesTable/NotesTable.module.scss +77 -77
  80. package/vitepress/components/NotesTable/NotesTable.vue +98 -98
  81. package/vitepress/components/NotesTable/README.md +67 -67
  82. package/vitepress/components/Settings/Settings.module.scss +375 -375
  83. package/vitepress/components/Settings/Settings.vue +334 -334
  84. package/vitepress/components/SidebarCard/MindMapView.vue +484 -484
  85. package/vitepress/components/SidebarCard/NotesTrendChart.vue +108 -108
  86. package/vitepress/components/SidebarCard/SidebarCard.vue +946 -946
  87. package/vitepress/components/Tooltip/Tooltip.vue +70 -70
  88. package/vitepress/components/constants.ts +109 -109
  89. package/vitepress/components/notesConfig.data.ts +73 -73
  90. package/vitepress/components/sidebar.data.ts +59 -59
  91. package/vitepress/components/tnotes-config.data.ts +21 -21
  92. package/vitepress/components/utils.ts +26 -26
  93. package/vitepress/config/index.ts +169 -169
  94. package/vitepress/configs/constants.ts +26 -26
  95. package/vitepress/configs/head.config.ts +25 -25
  96. package/vitepress/configs/index.ts +9 -9
  97. package/vitepress/configs/markdown-it.d.ts +23 -23
  98. package/vitepress/configs/markdown.config.ts +366 -366
  99. package/vitepress/configs/theme.config.ts +108 -108
  100. package/vitepress/plugins/buildProgressPlugin.ts +434 -434
  101. package/vitepress/plugins/getNoteByConfigIdPlugin.ts +107 -107
  102. package/vitepress/plugins/renameNotePlugin.ts +68 -68
  103. package/vitepress/plugins/sidebarStructurePlugin.ts +260 -260
  104. package/vitepress/plugins/updateConfigPlugin.ts +63 -63
  105. package/vitepress/theme/index.ts +137 -137
  106. package/vitepress/theme/styles/base.scss +64 -59
  107. package/vitepress/theme/styles/components/404.scss +31 -31
  108. package/vitepress/theme/styles/components/collapse.scss +172 -185
  109. package/vitepress/theme/styles/components/markmap.scss +101 -101
  110. package/vitepress/theme/styles/components/swiper.scss +255 -255
  111. package/vitepress/theme/styles/index.scss +25 -25
  112. package/vitepress/theme/styles/layout.scss +78 -78
  113. package/vitepress/theme/styles/utilities.scss +39 -39
  114. package/vitepress/theme/styles/vitepress-override.scss +25 -25
@@ -1,405 +1,405 @@
1
- <script setup lang="ts">
2
- import { scaleOrdinal, schemePastel2, schemeSet3, schemeTableau10 } from 'd3'
3
- import { Transformer } from 'markmap-lib'
4
- import { Toolbar } from 'markmap-toolbar'
5
- import 'markmap-toolbar/dist/style.css'
6
- import { Markmap, IMarkmapOptions } from 'markmap-view'
7
- import { nextTick, onBeforeUnmount, onMounted, ref, watch } from 'vue'
8
-
9
- import { icon__fullscreen, icon__fullscreen_exit, icon__confirm } from '../../assets/icons'
10
- import { MARKMAP_THEME_KEY, MARKMAP_EXPAND_LEVEL_KEY } from '../constants'
11
-
12
- // doc: https://github.com/markmap/markmap/blob/205367a24603dc187f67da1658940c6cade20dce/packages/markmap-view/src/constants.ts#L15
13
-
14
- const props = defineProps({
15
- content: { type: String, default: '' },
16
- duration: { type: Number, default: 100 },
17
- spacingVertical: { type: Number, default: 10 },
18
- spacingHorizontal: { type: Number, default: 20 },
19
- nodeMinHeight: { type: Number, default: 24 },
20
- initialExpandLevel: { type: Number, default: 5 },
21
- })
22
-
23
- const svgRef = ref<SVGSVGElement | null>(null)
24
- const containerRef = ref<HTMLDivElement | null>(null)
25
-
26
- let markmapInstance: Markmap | null = null
27
- let observer: MutationObserver | null = null
28
- let toolbarEl: HTMLElement | null = null
29
-
30
- // 从 localStorage 读取配置,如果没有则使用 props 或默认值
31
- const getInitialExpandLevel = () => {
32
- if (typeof window !== 'undefined') {
33
- const saved = localStorage.getItem(MARKMAP_EXPAND_LEVEL_KEY)
34
- if (saved) return parseInt(saved)
35
- }
36
- return props.initialExpandLevel
37
- }
38
-
39
- const getThemeColorFn = () => {
40
- if (typeof window !== 'undefined') {
41
- const theme = localStorage.getItem(MARKMAP_THEME_KEY) || 'default'
42
- switch (theme) {
43
- case 'colorful':
44
- return scaleOrdinal(schemeTableau10)
45
- case 'dark':
46
- return scaleOrdinal(schemeSet3)
47
- default:
48
- return scaleOrdinal(schemePastel2)
49
- }
50
- }
51
- return scaleOrdinal(schemePastel2)
52
- }
53
-
54
- // 可由配置/props 初始化,也可以通过工具栏改动
55
- const expandLevel = ref(getInitialExpandLevel())
56
- const transformer = new Transformer()
57
- const isFullscreen = ref(false)
58
-
59
- // 保存工具栏中层级输入框的引用,方便同步显示与绑定事件
60
- let toolbarLevelInput: HTMLInputElement | null = null
61
-
62
- function renderMarkmap(content: string, level = expandLevel.value) {
63
- if (!svgRef.value) return
64
-
65
- nextTick().then(() => {
66
- if (markmapInstance) {
67
- try {
68
- markmapInstance.destroy()
69
- } catch {}
70
- markmapInstance = null
71
- }
72
-
73
- if (!content.trim()) {
74
- svgRef.value!.innerHTML = '<text x="20" y="30" fill="#999">空内容</text>'
75
- return
76
- }
77
-
78
- try {
79
- const { root } = transformer.transform(content)
80
- const colorFn = getThemeColorFn()
81
- const options: Partial<IMarkmapOptions> = {
82
- // autoFit 会自动调整 scale 和 position 来适配当前容器大小,在阅读大量节点内容的时候,会放大某块区域阅读,每次展开节点或者收起节点,都会自动触发 autoFit,导致阅读体验不佳,因此不启用。
83
- // autoFit: true,
84
- initialExpandLevel: level,
85
- duration: props.duration,
86
- nodeMinHeight: props.nodeMinHeight,
87
- spacingVertical: props.spacingVertical,
88
- spacingHorizontal: props.spacingHorizontal,
89
- maxInitialScale: 2,
90
- maxWidth: 400,
91
- // 使用配置的主题颜色
92
- color: (node): string => colorFn(`${node.state?.path || ''}`),
93
- }
94
-
95
- markmapInstance = Markmap.create(svgRef.value!, options, root)
96
-
97
- setTimeout(() => {
98
- try {
99
- markmapInstance?.fit() // 确保居中
100
- } catch (e) {
101
- console.warn('fit failed', e)
102
- }
103
- }, 0)
104
-
105
- initToolbar()
106
- setupObserver()
107
- } catch (error: any) {
108
- console.error('Markmap render error:', error)
109
- svgRef.value!.innerHTML = `<text x="20" y="30" fill="red">Markmap 错误: ${error.message}</text>`
110
- }
111
- })
112
- }
113
-
114
- function initToolbar() {
115
- if (!markmapInstance || !containerRef.value) return
116
-
117
- // 移除现有的工具栏
118
- if (toolbarEl) {
119
- toolbarEl.remove()
120
- toolbarEl = null
121
- toolbarLevelInput = null
122
- }
123
-
124
- // 创建新工具栏
125
- const { el } = Toolbar.create(markmapInstance)
126
- toolbarEl = el
127
- toolbarEl.style.position = 'absolute'
128
- toolbarEl.style.top = '1rem'
129
- toolbarEl.style.right = '.5rem'
130
- toolbarEl.style.scale = '.8'
131
- const brand = toolbarEl.querySelector('.mm-toolbar-brand')
132
- if (brand) toolbarEl.removeChild(brand)
133
- containerRef.value.appendChild(toolbarEl)
134
-
135
- // 添加自定义全屏按钮
136
- addFullscreenButton(toolbarEl)
137
-
138
- // 在工具栏中添加更新按钮 + 层级输入(并支持 Enter 键触发)
139
- addUpdateButton(toolbarEl)
140
- }
141
-
142
- function addFullscreenButton(toolbar: HTMLElement) {
143
- const fullscreenBtn = document.createElement('div')
144
- fullscreenBtn.className = 'mm-toolbar-item'
145
- fullscreenBtn.title = isFullscreen.value ? '退出全屏' : '全屏'
146
- fullscreenBtn.innerHTML = isFullscreen.value
147
- ? `<img src="${icon__fullscreen_exit}" alt="退出全屏" style="width:18px;height:18px;display:block" />`
148
- : `<img src="${icon__fullscreen}" alt="全屏" style="width:18px;height:18px;display:block" />`
149
- fullscreenBtn.addEventListener('click', toggleFullscreen)
150
- toolbar.appendChild(fullscreenBtn)
151
- }
152
-
153
- function addUpdateButton(toolbar: HTMLElement) {
154
- // 创建更新按钮容器
155
- const updateContainer = document.createElement('div')
156
- updateContainer.style.display = 'flex'
157
- updateContainer.style.alignItems = 'center'
158
- updateContainer.style.gap = '8px'
159
- updateContainer.style.marginRight = '5px'
160
-
161
- // 创建输入框(确保具有 id/name,且样式能在工具栏中清晰可见)
162
- const levelInput = document.createElement('input')
163
- levelInput.type = 'number'
164
- levelInput.id = 'markmap-expand-level'
165
- levelInput.name = 'markmap-expand-level'
166
- levelInput.min = '1'
167
- levelInput.max = '100'
168
- levelInput.value = expandLevel.value.toString()
169
- levelInput.style.width = '2.4rem'
170
- levelInput.style.height = '1.6rem'
171
- levelInput.style.padding = '2px 6px'
172
- levelInput.style.fontSize = '12px'
173
- levelInput.style.lineHeight = '1.2'
174
- levelInput.style.textAlign = 'center'
175
- levelInput.style.boxSizing = 'border-box'
176
- levelInput.style.borderBottom = '.5px solid var(--vp-c-tip-1)'
177
- // levelInput.style.borderRadius = '4px'
178
- // levelInput.style.background = 'var(--vp-c-bg)'
179
- levelInput.style.color = 'var(--vp-c-tip-1)'
180
- levelInput.title = '展开层级'
181
- levelInput.setAttribute('aria-label', 'markmap-expand-level')
182
-
183
- // 保存引用,供外部(props 改变)同步输入值
184
- toolbarLevelInput = levelInput
185
-
186
- // 实时限制输入范围(1-100)
187
- levelInput.addEventListener('input', (e) => {
188
- const input = e.target as HTMLInputElement
189
- let value = parseInt(input.value)
190
-
191
- // 如果输入为空或非数字,不做处理
192
- if (input.value === '' || isNaN(value)) return
193
-
194
- // 限制范围:1-100
195
- if (value < 1) {
196
- input.value = '1'
197
- value = 1
198
- } else if (value > 100) {
199
- input.value = '100'
200
- value = 100
201
- }
202
- })
203
-
204
- // 当输入框值变化时仅更新内部 expandLevel(不自动渲染)
205
- levelInput.addEventListener('change', (e) => {
206
- const value = parseInt((e.target as HTMLInputElement).value)
207
- if (!isNaN(value) && value >= 1 && value <= 100) {
208
- expandLevel.value = value
209
- } else {
210
- // 回退到之前的值(避免非法输入)
211
- levelInput.value = expandLevel.value.toString()
212
- }
213
- })
214
-
215
- // 按键监听:按 Enter 时触发更新(相当于点击确认按钮)
216
- levelInput.addEventListener('keydown', (e) => {
217
- if (e.key === 'Enter') {
218
- // 同步输入框值到 expandLevel(防止未触发 change)
219
- const val = parseInt((e.target as HTMLInputElement).value)
220
- if (!isNaN(val) && val >= 1 && val <= 100) {
221
- expandLevel.value = val
222
- } else {
223
- // 如果超出范围,重置为有效值
224
- levelInput.value = expandLevel.value.toString()
225
- }
226
- onUpdateClick()
227
- }
228
- })
229
-
230
- // 创建按钮
231
- const updateBtn = document.createElement('button')
232
- updateBtn.type = 'button'
233
- updateBtn.title = '确定层级并更新'
234
- updateBtn.innerHTML = `<img src="${icon__confirm}" alt="确定" style="width:16px;height:16px;display:block" />`
235
- updateBtn.addEventListener('click', onUpdateClick)
236
-
237
- updateContainer.appendChild(levelInput)
238
- updateContainer.appendChild(updateBtn)
239
-
240
- // 将更新按钮插入到工具栏开头
241
- toolbar.insertBefore(updateContainer, toolbar.firstChild)
242
- }
243
-
244
- // 切换全屏(保持原来的实现)
245
- function toggleFullscreen() {
246
- if (!containerRef.value) return
247
-
248
- if (!isFullscreen.value) {
249
- // 进入全屏
250
- if (containerRef.value.requestFullscreen) {
251
- containerRef.value.requestFullscreen().catch((err) => {
252
- console.error('全屏请求失败:', err)
253
- })
254
- } else if ((containerRef.value as any).webkitRequestFullscreen) {
255
- ;(containerRef.value as any).webkitRequestFullscreen()
256
- } else if ((containerRef.value as any).msRequestFullscreen) {
257
- ;(containerRef.value as any).msRequestFullscreen()
258
- }
259
- } else {
260
- // 退出全屏
261
- if (document.exitFullscreen) {
262
- document.exitFullscreen()
263
- } else if ((document as any).webkitExitFullscreen) {
264
- ;(document as any).webkitExitFullscreen()
265
- } else if ((document as any).msExitFullscreen) {
266
- ;(document as any).msExitFullscreen()
267
- }
268
- }
269
- }
270
-
271
- // 监听全屏状态变化
272
- function handleFullscreenChange() {
273
- isFullscreen.value = !!(
274
- document.fullscreenElement ||
275
- (document as any).webkitFullscreenElement ||
276
- (document as any).msFullscreenElement
277
- )
278
-
279
- // 更新工具栏中的全屏按钮(若存在)
280
- if (toolbarEl) {
281
- const fullscreenBtn = toolbarEl.querySelector(
282
- '.mm-toolbar-item:last-child'
283
- ) as HTMLButtonElement
284
- if (fullscreenBtn) {
285
- fullscreenBtn.title = isFullscreen.value
286
- ? '退出全屏(Exit Fullscreen)'
287
- : '全屏(Fullscreen)'
288
- fullscreenBtn.innerHTML = isFullscreen.value
289
- ? `<img src="${icon__fullscreen_exit}" alt="退出全屏" style="width:18px;height:18px;display:block" />`
290
- : `<img src="${icon__fullscreen}" alt="全屏" style="width:18px;height:18px;display:block" />`
291
- }
292
- }
293
-
294
- // 全屏模式下调整SVG高度
295
- if (svgRef.value) {
296
- if (isFullscreen.value) {
297
- svgRef.value.style.height = 'calc(100vh - 100px)'
298
- } else {
299
- svgRef.value.style.height = '400px'
300
- }
301
-
302
- // 确保居中 - 无论进入还是退出全屏都执行居中
303
- setTimeout(() => {
304
- if (markmapInstance) {
305
- try {
306
- markmapInstance.fit()
307
- } catch (e) {
308
- console.warn('居中失败', e)
309
- }
310
- }
311
- }, 300)
312
- }
313
- }
314
-
315
- function setupObserver() {
316
- if (!svgRef.value) return
317
- if (observer !== null) {
318
- observer.disconnect()
319
- }
320
- observer = new MutationObserver(() => {
321
- // DOM 变动后处理(保留扩展点)
322
- })
323
- observer.observe(svgRef.value, {
324
- childList: true,
325
- subtree: true,
326
- attributes: true,
327
- })
328
- }
329
-
330
- // 只监听内容变化,expandLevel 改动不自动渲染(点击/Enter 确认渲染)
331
- watch(
332
- () => props.content,
333
- (newVal) => {
334
- renderMarkmap(decodeURIComponent(newVal || ''))
335
- }
336
- )
337
-
338
- // 新增:当外部传入的 initialExpandLevel 改变时,组件应同步并重新渲染
339
- watch(
340
- () => props.initialExpandLevel,
341
- (newVal) => {
342
- if (typeof newVal === 'number' && !isNaN(newVal)) {
343
- expandLevel.value = newVal
344
- // 更新输入框显示(如果已创建)
345
- if (toolbarLevelInput) toolbarLevelInput.value = newVal.toString()
346
- // 重新渲染使用新的层级(保持对外部 prop 改动的即时响应)
347
- renderMarkmap(decodeURIComponent(props.content || ''), newVal)
348
- }
349
- }
350
- )
351
-
352
- // 同步:当内部 expandLevel 变更时(例如通过输入框 change 事件),更新工具栏输入显示
353
- watch(expandLevel, (v) => {
354
- if (toolbarLevelInput) {
355
- const asStr = (v || 0).toString()
356
- if (toolbarLevelInput.value !== asStr) {
357
- toolbarLevelInput.value = asStr
358
- }
359
- }
360
- })
361
-
362
- // 点击更新按钮才用当前 expandLevel 渲染
363
- function onUpdateClick() {
364
- renderMarkmap(decodeURIComponent(props.content || ''), expandLevel.value)
365
- }
366
-
367
- onMounted(() => {
368
- renderMarkmap(decodeURIComponent(props.content || ''))
369
-
370
- // 添加全屏事件监听
371
- document.addEventListener('fullscreenchange', handleFullscreenChange)
372
- document.addEventListener('webkitfullscreenchange', handleFullscreenChange)
373
- document.addEventListener('MSFullscreenChange', handleFullscreenChange)
374
- })
375
-
376
- onBeforeUnmount(() => {
377
- if (markmapInstance) {
378
- try {
379
- markmapInstance.destroy()
380
- } catch {}
381
- markmapInstance = null
382
- }
383
- if (observer !== null) {
384
- observer.disconnect()
385
- observer = null
386
- }
387
-
388
- // 移除全屏事件监听
389
- document.removeEventListener('fullscreenchange', handleFullscreenChange)
390
- document.removeEventListener('webkitfullscreenchange', handleFullscreenChange)
391
- document.removeEventListener('MSFullscreenChange', handleFullscreenChange)
392
- })
393
- </script>
394
-
395
- <template>
396
- <div
397
- :class="$style.markmapContainer"
398
- ref="containerRef"
399
- style="position: relative"
400
- >
401
- <svg ref="svgRef" style="width: 100%; height: 400px"></svg>
402
- </div>
403
- </template>
404
-
405
- <style module src="./MarkMap.module.scss"></style>
1
+ <script setup lang="ts">
2
+ import { scaleOrdinal, schemePastel2, schemeSet3, schemeTableau10 } from 'd3'
3
+ import { Transformer } from 'markmap-lib'
4
+ import { Toolbar } from 'markmap-toolbar'
5
+ import 'markmap-toolbar/dist/style.css'
6
+ import { Markmap, IMarkmapOptions } from 'markmap-view'
7
+ import { nextTick, onBeforeUnmount, onMounted, ref, watch } from 'vue'
8
+
9
+ import { icon__fullscreen, icon__fullscreen_exit, icon__confirm } from '../../assets/icons'
10
+ import { MARKMAP_THEME_KEY, MARKMAP_EXPAND_LEVEL_KEY } from '../constants'
11
+
12
+ // doc: https://github.com/markmap/markmap/blob/205367a24603dc187f67da1658940c6cade20dce/packages/markmap-view/src/constants.ts#L15
13
+
14
+ const props = defineProps({
15
+ content: { type: String, default: '' },
16
+ duration: { type: Number, default: 100 },
17
+ spacingVertical: { type: Number, default: 10 },
18
+ spacingHorizontal: { type: Number, default: 20 },
19
+ nodeMinHeight: { type: Number, default: 24 },
20
+ initialExpandLevel: { type: Number, default: 5 },
21
+ })
22
+
23
+ const svgRef = ref<SVGSVGElement | null>(null)
24
+ const containerRef = ref<HTMLDivElement | null>(null)
25
+
26
+ let markmapInstance: Markmap | null = null
27
+ let observer: MutationObserver | null = null
28
+ let toolbarEl: HTMLElement | null = null
29
+
30
+ // 从 localStorage 读取配置,如果没有则使用 props 或默认值
31
+ const getInitialExpandLevel = () => {
32
+ if (typeof window !== 'undefined') {
33
+ const saved = localStorage.getItem(MARKMAP_EXPAND_LEVEL_KEY)
34
+ if (saved) return parseInt(saved)
35
+ }
36
+ return props.initialExpandLevel
37
+ }
38
+
39
+ const getThemeColorFn = () => {
40
+ if (typeof window !== 'undefined') {
41
+ const theme = localStorage.getItem(MARKMAP_THEME_KEY) || 'default'
42
+ switch (theme) {
43
+ case 'colorful':
44
+ return scaleOrdinal(schemeTableau10)
45
+ case 'dark':
46
+ return scaleOrdinal(schemeSet3)
47
+ default:
48
+ return scaleOrdinal(schemePastel2)
49
+ }
50
+ }
51
+ return scaleOrdinal(schemePastel2)
52
+ }
53
+
54
+ // 可由配置/props 初始化,也可以通过工具栏改动
55
+ const expandLevel = ref(getInitialExpandLevel())
56
+ const transformer = new Transformer()
57
+ const isFullscreen = ref(false)
58
+
59
+ // 保存工具栏中层级输入框的引用,方便同步显示与绑定事件
60
+ let toolbarLevelInput: HTMLInputElement | null = null
61
+
62
+ function renderMarkmap(content: string, level = expandLevel.value) {
63
+ if (!svgRef.value) return
64
+
65
+ nextTick().then(() => {
66
+ if (markmapInstance) {
67
+ try {
68
+ markmapInstance.destroy()
69
+ } catch {}
70
+ markmapInstance = null
71
+ }
72
+
73
+ if (!content.trim()) {
74
+ svgRef.value!.innerHTML = '<text x="20" y="30" fill="#999">空内容</text>'
75
+ return
76
+ }
77
+
78
+ try {
79
+ const { root } = transformer.transform(content)
80
+ const colorFn = getThemeColorFn()
81
+ const options: Partial<IMarkmapOptions> = {
82
+ // autoFit 会自动调整 scale 和 position 来适配当前容器大小,在阅读大量节点内容的时候,会放大某块区域阅读,每次展开节点或者收起节点,都会自动触发 autoFit,导致阅读体验不佳,因此不启用。
83
+ // autoFit: true,
84
+ initialExpandLevel: level,
85
+ duration: props.duration,
86
+ nodeMinHeight: props.nodeMinHeight,
87
+ spacingVertical: props.spacingVertical,
88
+ spacingHorizontal: props.spacingHorizontal,
89
+ maxInitialScale: 2,
90
+ maxWidth: 400,
91
+ // 使用配置的主题颜色
92
+ color: (node): string => colorFn(`${node.state?.path || ''}`),
93
+ }
94
+
95
+ markmapInstance = Markmap.create(svgRef.value!, options, root)
96
+
97
+ setTimeout(() => {
98
+ try {
99
+ markmapInstance?.fit() // 确保居中
100
+ } catch (e) {
101
+ console.warn('fit failed', e)
102
+ }
103
+ }, 0)
104
+
105
+ initToolbar()
106
+ setupObserver()
107
+ } catch (error: any) {
108
+ console.error('Markmap render error:', error)
109
+ svgRef.value!.innerHTML = `<text x="20" y="30" fill="red">Markmap 错误: ${error.message}</text>`
110
+ }
111
+ })
112
+ }
113
+
114
+ function initToolbar() {
115
+ if (!markmapInstance || !containerRef.value) return
116
+
117
+ // 移除现有的工具栏
118
+ if (toolbarEl) {
119
+ toolbarEl.remove()
120
+ toolbarEl = null
121
+ toolbarLevelInput = null
122
+ }
123
+
124
+ // 创建新工具栏
125
+ const { el } = Toolbar.create(markmapInstance)
126
+ toolbarEl = el
127
+ toolbarEl.style.position = 'absolute'
128
+ toolbarEl.style.top = '1rem'
129
+ toolbarEl.style.right = '.5rem'
130
+ toolbarEl.style.scale = '.8'
131
+ const brand = toolbarEl.querySelector('.mm-toolbar-brand')
132
+ if (brand) toolbarEl.removeChild(brand)
133
+ containerRef.value.appendChild(toolbarEl)
134
+
135
+ // 添加自定义全屏按钮
136
+ addFullscreenButton(toolbarEl)
137
+
138
+ // 在工具栏中添加更新按钮 + 层级输入(并支持 Enter 键触发)
139
+ addUpdateButton(toolbarEl)
140
+ }
141
+
142
+ function addFullscreenButton(toolbar: HTMLElement) {
143
+ const fullscreenBtn = document.createElement('div')
144
+ fullscreenBtn.className = 'mm-toolbar-item'
145
+ fullscreenBtn.title = isFullscreen.value ? '退出全屏' : '全屏'
146
+ fullscreenBtn.innerHTML = isFullscreen.value
147
+ ? `<img src="${icon__fullscreen_exit}" alt="退出全屏" style="width:18px;height:18px;display:block" />`
148
+ : `<img src="${icon__fullscreen}" alt="全屏" style="width:18px;height:18px;display:block" />`
149
+ fullscreenBtn.addEventListener('click', toggleFullscreen)
150
+ toolbar.appendChild(fullscreenBtn)
151
+ }
152
+
153
+ function addUpdateButton(toolbar: HTMLElement) {
154
+ // 创建更新按钮容器
155
+ const updateContainer = document.createElement('div')
156
+ updateContainer.style.display = 'flex'
157
+ updateContainer.style.alignItems = 'center'
158
+ updateContainer.style.gap = '8px'
159
+ updateContainer.style.marginRight = '5px'
160
+
161
+ // 创建输入框(确保具有 id/name,且样式能在工具栏中清晰可见)
162
+ const levelInput = document.createElement('input')
163
+ levelInput.type = 'number'
164
+ levelInput.id = 'markmap-expand-level'
165
+ levelInput.name = 'markmap-expand-level'
166
+ levelInput.min = '1'
167
+ levelInput.max = '100'
168
+ levelInput.value = expandLevel.value.toString()
169
+ levelInput.style.width = '2.4rem'
170
+ levelInput.style.height = '1.6rem'
171
+ levelInput.style.padding = '2px 6px'
172
+ levelInput.style.fontSize = '12px'
173
+ levelInput.style.lineHeight = '1.2'
174
+ levelInput.style.textAlign = 'center'
175
+ levelInput.style.boxSizing = 'border-box'
176
+ levelInput.style.borderBottom = '.5px solid var(--vp-c-tip-1)'
177
+ // levelInput.style.borderRadius = '4px'
178
+ // levelInput.style.background = 'var(--vp-c-bg)'
179
+ levelInput.style.color = 'var(--vp-c-tip-1)'
180
+ levelInput.title = '展开层级'
181
+ levelInput.setAttribute('aria-label', 'markmap-expand-level')
182
+
183
+ // 保存引用,供外部(props 改变)同步输入值
184
+ toolbarLevelInput = levelInput
185
+
186
+ // 实时限制输入范围(1-100)
187
+ levelInput.addEventListener('input', (e) => {
188
+ const input = e.target as HTMLInputElement
189
+ let value = parseInt(input.value)
190
+
191
+ // 如果输入为空或非数字,不做处理
192
+ if (input.value === '' || isNaN(value)) return
193
+
194
+ // 限制范围:1-100
195
+ if (value < 1) {
196
+ input.value = '1'
197
+ value = 1
198
+ } else if (value > 100) {
199
+ input.value = '100'
200
+ value = 100
201
+ }
202
+ })
203
+
204
+ // 当输入框值变化时仅更新内部 expandLevel(不自动渲染)
205
+ levelInput.addEventListener('change', (e) => {
206
+ const value = parseInt((e.target as HTMLInputElement).value)
207
+ if (!isNaN(value) && value >= 1 && value <= 100) {
208
+ expandLevel.value = value
209
+ } else {
210
+ // 回退到之前的值(避免非法输入)
211
+ levelInput.value = expandLevel.value.toString()
212
+ }
213
+ })
214
+
215
+ // 按键监听:按 Enter 时触发更新(相当于点击确认按钮)
216
+ levelInput.addEventListener('keydown', (e) => {
217
+ if (e.key === 'Enter') {
218
+ // 同步输入框值到 expandLevel(防止未触发 change)
219
+ const val = parseInt((e.target as HTMLInputElement).value)
220
+ if (!isNaN(val) && val >= 1 && val <= 100) {
221
+ expandLevel.value = val
222
+ } else {
223
+ // 如果超出范围,重置为有效值
224
+ levelInput.value = expandLevel.value.toString()
225
+ }
226
+ onUpdateClick()
227
+ }
228
+ })
229
+
230
+ // 创建按钮
231
+ const updateBtn = document.createElement('button')
232
+ updateBtn.type = 'button'
233
+ updateBtn.title = '确定层级并更新'
234
+ updateBtn.innerHTML = `<img src="${icon__confirm}" alt="确定" style="width:16px;height:16px;display:block" />`
235
+ updateBtn.addEventListener('click', onUpdateClick)
236
+
237
+ updateContainer.appendChild(levelInput)
238
+ updateContainer.appendChild(updateBtn)
239
+
240
+ // 将更新按钮插入到工具栏开头
241
+ toolbar.insertBefore(updateContainer, toolbar.firstChild)
242
+ }
243
+
244
+ // 切换全屏(保持原来的实现)
245
+ function toggleFullscreen() {
246
+ if (!containerRef.value) return
247
+
248
+ if (!isFullscreen.value) {
249
+ // 进入全屏
250
+ if (containerRef.value.requestFullscreen) {
251
+ containerRef.value.requestFullscreen().catch((err) => {
252
+ console.error('全屏请求失败:', err)
253
+ })
254
+ } else if ((containerRef.value as any).webkitRequestFullscreen) {
255
+ ;(containerRef.value as any).webkitRequestFullscreen()
256
+ } else if ((containerRef.value as any).msRequestFullscreen) {
257
+ ;(containerRef.value as any).msRequestFullscreen()
258
+ }
259
+ } else {
260
+ // 退出全屏
261
+ if (document.exitFullscreen) {
262
+ document.exitFullscreen()
263
+ } else if ((document as any).webkitExitFullscreen) {
264
+ ;(document as any).webkitExitFullscreen()
265
+ } else if ((document as any).msExitFullscreen) {
266
+ ;(document as any).msExitFullscreen()
267
+ }
268
+ }
269
+ }
270
+
271
+ // 监听全屏状态变化
272
+ function handleFullscreenChange() {
273
+ isFullscreen.value = !!(
274
+ document.fullscreenElement ||
275
+ (document as any).webkitFullscreenElement ||
276
+ (document as any).msFullscreenElement
277
+ )
278
+
279
+ // 更新工具栏中的全屏按钮(若存在)
280
+ if (toolbarEl) {
281
+ const fullscreenBtn = toolbarEl.querySelector(
282
+ '.mm-toolbar-item:last-child'
283
+ ) as HTMLButtonElement
284
+ if (fullscreenBtn) {
285
+ fullscreenBtn.title = isFullscreen.value
286
+ ? '退出全屏(Exit Fullscreen)'
287
+ : '全屏(Fullscreen)'
288
+ fullscreenBtn.innerHTML = isFullscreen.value
289
+ ? `<img src="${icon__fullscreen_exit}" alt="退出全屏" style="width:18px;height:18px;display:block" />`
290
+ : `<img src="${icon__fullscreen}" alt="全屏" style="width:18px;height:18px;display:block" />`
291
+ }
292
+ }
293
+
294
+ // 全屏模式下调整SVG高度
295
+ if (svgRef.value) {
296
+ if (isFullscreen.value) {
297
+ svgRef.value.style.height = 'calc(100vh - 100px)'
298
+ } else {
299
+ svgRef.value.style.height = '400px'
300
+ }
301
+
302
+ // 确保居中 - 无论进入还是退出全屏都执行居中
303
+ setTimeout(() => {
304
+ if (markmapInstance) {
305
+ try {
306
+ markmapInstance.fit()
307
+ } catch (e) {
308
+ console.warn('居中失败', e)
309
+ }
310
+ }
311
+ }, 300)
312
+ }
313
+ }
314
+
315
+ function setupObserver() {
316
+ if (!svgRef.value) return
317
+ if (observer !== null) {
318
+ observer.disconnect()
319
+ }
320
+ observer = new MutationObserver(() => {
321
+ // DOM 变动后处理(保留扩展点)
322
+ })
323
+ observer.observe(svgRef.value, {
324
+ childList: true,
325
+ subtree: true,
326
+ attributes: true,
327
+ })
328
+ }
329
+
330
+ // 只监听内容变化,expandLevel 改动不自动渲染(点击/Enter 确认渲染)
331
+ watch(
332
+ () => props.content,
333
+ (newVal) => {
334
+ renderMarkmap(decodeURIComponent(newVal || ''))
335
+ }
336
+ )
337
+
338
+ // 新增:当外部传入的 initialExpandLevel 改变时,组件应同步并重新渲染
339
+ watch(
340
+ () => props.initialExpandLevel,
341
+ (newVal) => {
342
+ if (typeof newVal === 'number' && !isNaN(newVal)) {
343
+ expandLevel.value = newVal
344
+ // 更新输入框显示(如果已创建)
345
+ if (toolbarLevelInput) toolbarLevelInput.value = newVal.toString()
346
+ // 重新渲染使用新的层级(保持对外部 prop 改动的即时响应)
347
+ renderMarkmap(decodeURIComponent(props.content || ''), newVal)
348
+ }
349
+ }
350
+ )
351
+
352
+ // 同步:当内部 expandLevel 变更时(例如通过输入框 change 事件),更新工具栏输入显示
353
+ watch(expandLevel, (v) => {
354
+ if (toolbarLevelInput) {
355
+ const asStr = (v || 0).toString()
356
+ if (toolbarLevelInput.value !== asStr) {
357
+ toolbarLevelInput.value = asStr
358
+ }
359
+ }
360
+ })
361
+
362
+ // 点击更新按钮才用当前 expandLevel 渲染
363
+ function onUpdateClick() {
364
+ renderMarkmap(decodeURIComponent(props.content || ''), expandLevel.value)
365
+ }
366
+
367
+ onMounted(() => {
368
+ renderMarkmap(decodeURIComponent(props.content || ''))
369
+
370
+ // 添加全屏事件监听
371
+ document.addEventListener('fullscreenchange', handleFullscreenChange)
372
+ document.addEventListener('webkitfullscreenchange', handleFullscreenChange)
373
+ document.addEventListener('MSFullscreenChange', handleFullscreenChange)
374
+ })
375
+
376
+ onBeforeUnmount(() => {
377
+ if (markmapInstance) {
378
+ try {
379
+ markmapInstance.destroy()
380
+ } catch {}
381
+ markmapInstance = null
382
+ }
383
+ if (observer !== null) {
384
+ observer.disconnect()
385
+ observer = null
386
+ }
387
+
388
+ // 移除全屏事件监听
389
+ document.removeEventListener('fullscreenchange', handleFullscreenChange)
390
+ document.removeEventListener('webkitfullscreenchange', handleFullscreenChange)
391
+ document.removeEventListener('MSFullscreenChange', handleFullscreenChange)
392
+ })
393
+ </script>
394
+
395
+ <template>
396
+ <div
397
+ :class="$style.markmapContainer"
398
+ ref="containerRef"
399
+ style="position: relative"
400
+ >
401
+ <svg ref="svgRef" style="width: 100%; height: 400px"></svg>
402
+ </div>
403
+ </template>
404
+
405
+ <style module src="./MarkMap.module.scss"></style>