@tnotesjs/core 0.3.0 → 0.4.1

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.
@@ -1,618 +0,0 @@
1
- <!--
2
- vitepress/components/MarkMap/MarkMap.vue
3
- -->
4
-
5
- <script setup lang="ts">
6
- import { scaleOrdinal, schemePastel2, schemeSet3, schemeTableau10 } from 'd3'
7
- import { Transformer } from 'markmap-lib'
8
- import { Toolbar } from 'markmap-toolbar'
9
- import 'markmap-toolbar/dist/style.css'
10
- import { Markmap, IMarkmapOptions } from 'markmap-view'
11
- import { onContentUpdated } from 'vitepress'
12
- import { nextTick, onBeforeUnmount, onMounted, ref, watch } from 'vue'
13
-
14
- import { icon__fullscreen, icon__fullscreen_exit, icon__confirm } from '../../assets/icons'
15
- import { MARKMAP_THEME_KEY } from '../constants'
16
-
17
- // doc: https://github.com/markmap/markmap/blob/205367a24603dc187f67da1658940c6cade20dce/packages/markmap-view/src/constants.ts#L15
18
-
19
- const DEFAULT_EXPAND_LEVEL = 3
20
-
21
- const props = defineProps({
22
- content: { type: String, default: '' },
23
- duration: { type: Number, default: 100 },
24
- spacingVertical: { type: Number, default: 10 },
25
- spacingHorizontal: { type: Number, default: 20 },
26
- nodeMinHeight: { type: Number, default: 24 },
27
- initialExpandLevel: { type: Number, default: DEFAULT_EXPAND_LEVEL },
28
- })
29
-
30
- const svgRef = ref<SVGSVGElement | null>(null)
31
- const containerRef = ref<HTMLDivElement | null>(null)
32
-
33
- let markmapInstance: Markmap | null = null
34
- let observer: MutationObserver | null = null
35
- let toolbarEl: HTMLElement | null = null
36
- let toolbarReinitTimer: ReturnType<typeof setTimeout> | null = null
37
- let darkClassObserver: MutationObserver | null = null
38
-
39
- const isMarkmapDark = ref(false)
40
-
41
- const getThemeColorFn = () => {
42
- if (typeof window !== 'undefined') {
43
- if (document.documentElement.classList.contains('markmap-dark')) {
44
- return scaleOrdinal(schemeSet3)
45
- }
46
- const theme = localStorage.getItem(MARKMAP_THEME_KEY) || 'default'
47
- switch (theme) {
48
- case 'colorful':
49
- return scaleOrdinal(schemeTableau10)
50
- case 'dark':
51
- return scaleOrdinal(schemeSet3)
52
- default:
53
- return scaleOrdinal(schemePastel2)
54
- }
55
- }
56
- return scaleOrdinal(schemePastel2)
57
- }
58
-
59
- // 可由 props 初始化,也可以通过工具栏改动
60
- const expandLevel = ref(props.initialExpandLevel)
61
- const transformer = new Transformer()
62
- const isFullscreen = ref(false)
63
-
64
- // 保存工具栏中层级输入框的引用,方便同步显示与绑定事件
65
- let toolbarLevelInput: HTMLInputElement | null = null
66
-
67
- function renderMarkmap(content: string, level = expandLevel.value) {
68
- if (!svgRef.value) return
69
-
70
- nextTick().then(() => {
71
- if (markmapInstance) {
72
- try {
73
- markmapInstance.destroy()
74
- } catch {}
75
- markmapInstance = null
76
- }
77
-
78
- if (!content.trim()) {
79
- svgRef.value!.innerHTML = '<text x="20" y="30" fill="#999">空内容</text>'
80
- return
81
- }
82
-
83
- try {
84
- const { root } = transformer.transform(content)
85
- const colorFn = getThemeColorFn()
86
- const options: Partial<IMarkmapOptions> = {
87
- // autoFit 会自动调整 scale 和 position 来适配当前容器大小,在阅读大量节点内容的时候,会放大某块区域阅读,每次展开节点或者收起节点,都会自动触发 autoFit,导致阅读体验不佳,因此不启用。
88
- // autoFit: true,
89
- initialExpandLevel: level,
90
- duration: props.duration,
91
- nodeMinHeight: props.nodeMinHeight,
92
- spacingVertical: props.spacingVertical,
93
- spacingHorizontal: props.spacingHorizontal,
94
- maxInitialScale: 2,
95
- maxWidth: 400,
96
- // 使用配置的主题颜色
97
- color: (node): string => colorFn(`${node.state?.path || ''}`),
98
- }
99
-
100
- markmapInstance = Markmap.create(svgRef.value!, options, root)
101
-
102
- setTimeout(() => {
103
- try {
104
- markmapInstance?.fit() // 确保居中
105
- } catch (e) {
106
- console.warn('fit failed', e)
107
- }
108
- }, 0)
109
-
110
- initToolbar()
111
- setupObserver()
112
- } catch (error: any) {
113
- console.error('Markmap render error:', error)
114
- svgRef.value!.innerHTML = `<text x="20" y="30" fill="red">Markmap 错误: ${error.message}</text>`
115
- }
116
- })
117
- }
118
-
119
- function initToolbar() {
120
- if (!markmapInstance || !containerRef.value) return
121
-
122
- // 移除现有的工具栏
123
- if (toolbarEl) {
124
- toolbarEl.remove()
125
- toolbarEl = null
126
- toolbarLevelInput = null
127
- }
128
-
129
- // 创建新工具栏
130
- const { el } = Toolbar.create(markmapInstance)
131
- toolbarEl = el
132
- toolbarEl.style.position = 'absolute'
133
- toolbarEl.style.top = '1rem'
134
- toolbarEl.style.right = '.5rem'
135
- toolbarEl.style.zIndex = '10'
136
- toolbarEl.style.scale = '.8'
137
- const brand = toolbarEl.querySelector('.mm-toolbar-brand')
138
- if (brand) toolbarEl.removeChild(brand)
139
- containerRef.value.appendChild(toolbarEl)
140
-
141
- // 添加自定义全屏按钮
142
- addFullscreenButton(toolbarEl)
143
-
144
- // 在工具栏中添加更新按钮 + 层级输入(并支持 Enter 键触发)
145
- addUpdateButton(toolbarEl)
146
- }
147
-
148
- function addFullscreenButton(toolbar: HTMLElement) {
149
- const fullscreenBtn = document.createElement('div')
150
- fullscreenBtn.className = 'mm-toolbar-item'
151
- fullscreenBtn.title = isFullscreen.value ? '退出全屏' : '全屏'
152
- fullscreenBtn.innerHTML = isFullscreen.value
153
- ? `<img src="${icon__fullscreen_exit}" alt="退出全屏" style="width:18px;height:18px;display:block" />`
154
- : `<img src="${icon__fullscreen}" alt="全屏" style="width:18px;height:18px;display:block" />`
155
- fullscreenBtn.addEventListener('click', toggleFullscreen)
156
- toolbar.appendChild(fullscreenBtn)
157
- }
158
-
159
- function addUpdateButton(toolbar: HTMLElement) {
160
- // 创建更新按钮容器
161
- const updateContainer = document.createElement('div')
162
- updateContainer.style.display = 'flex'
163
- updateContainer.style.alignItems = 'center'
164
- updateContainer.style.gap = '8px'
165
- updateContainer.style.marginRight = '5px'
166
-
167
- // 创建输入框(确保具有 id/name,且样式能在工具栏中清晰可见)
168
- const levelInput = document.createElement('input')
169
- levelInput.type = 'number'
170
- levelInput.id = 'markmap-expand-level'
171
- levelInput.name = 'markmap-expand-level'
172
- levelInput.min = '1'
173
- levelInput.max = '100'
174
- levelInput.value = expandLevel.value.toString()
175
- levelInput.style.width = '2.4rem'
176
- levelInput.style.height = '1.6rem'
177
- levelInput.style.padding = '2px 6px'
178
- levelInput.style.fontSize = '12px'
179
- levelInput.style.lineHeight = '1.2'
180
- levelInput.style.textAlign = 'center'
181
- levelInput.style.boxSizing = 'border-box'
182
- levelInput.style.borderBottom = '.5px solid var(--vp-c-tip-1)'
183
- // levelInput.style.borderRadius = '4px'
184
- // levelInput.style.background = 'var(--vp-c-bg)'
185
- levelInput.style.color = 'var(--vp-c-tip-1)'
186
- levelInput.title = '展开层级'
187
-
188
- // 保存引用,供外部(props 改变)同步输入值
189
- toolbarLevelInput = levelInput
190
-
191
- // 实时限制输入范围(1-100)
192
- levelInput.addEventListener('input', (e) => {
193
- const input = e.target as HTMLInputElement
194
- let value = parseInt(input.value)
195
-
196
- // 如果输入为空或非数字,不做处理
197
- if (input.value === '' || isNaN(value)) return
198
-
199
- // 限制范围:1-100
200
- if (value < 1) {
201
- input.value = '1'
202
- value = 1
203
- } else if (value > 100) {
204
- input.value = '100'
205
- value = 100
206
- }
207
- })
208
-
209
- // 当输入框值变化时仅更新内部 expandLevel(不自动渲染)
210
- levelInput.addEventListener('change', (e) => {
211
- const value = parseInt((e.target as HTMLInputElement).value)
212
- if (!isNaN(value) && value >= 1 && value <= 100) {
213
- expandLevel.value = value
214
- } else {
215
- // 回退到之前的值(避免非法输入)
216
- levelInput.value = expandLevel.value.toString()
217
- }
218
- })
219
-
220
- // 按键监听:按 Enter 时触发更新(相当于点击确认按钮)
221
- levelInput.addEventListener('keydown', (e) => {
222
- if (e.key === 'Enter') {
223
- // 同步输入框值到 expandLevel(防止未触发 change)
224
- const val = parseInt((e.target as HTMLInputElement).value)
225
- if (!isNaN(val) && val >= 1 && val <= 100) {
226
- expandLevel.value = val
227
- } else {
228
- // 如果超出范围,重置为有效值
229
- levelInput.value = expandLevel.value.toString()
230
- }
231
- onUpdateClick()
232
- }
233
- })
234
-
235
- // 创建按钮
236
- const updateBtn = document.createElement('button')
237
- updateBtn.type = 'button'
238
- updateBtn.title = '确定层级并更新'
239
- updateBtn.innerHTML = `<img src="${icon__confirm}" alt="确定" style="width:16px;height:16px;display:block" />`
240
- updateBtn.addEventListener('click', onUpdateClick)
241
-
242
- updateContainer.appendChild(levelInput)
243
- updateContainer.appendChild(updateBtn)
244
-
245
- // 将更新按钮插入到工具栏开头
246
- toolbar.insertBefore(updateContainer, toolbar.firstChild)
247
- }
248
-
249
- // 切换全屏(保持原来的实现)
250
- function toggleFullscreen() {
251
- if (!containerRef.value) return
252
-
253
- if (!isFullscreen.value) {
254
- // 进入全屏
255
- if (containerRef.value.requestFullscreen) {
256
- containerRef.value.requestFullscreen().catch((err) => {
257
- console.error('全屏请求失败:', err)
258
- })
259
- } else if ((containerRef.value as any).webkitRequestFullscreen) {
260
- ;(containerRef.value as any).webkitRequestFullscreen()
261
- } else if ((containerRef.value as any).msRequestFullscreen) {
262
- ;(containerRef.value as any).msRequestFullscreen()
263
- }
264
- } else {
265
- // 退出全屏
266
- if (document.exitFullscreen) {
267
- document.exitFullscreen()
268
- } else if ((document as any).webkitExitFullscreen) {
269
- ;(document as any).webkitExitFullscreen()
270
- } else if ((document as any).msExitFullscreen) {
271
- ;(document as any).msExitFullscreen()
272
- }
273
- }
274
- }
275
-
276
- // 监听全屏状态变化
277
- function handleFullscreenChange() {
278
- isFullscreen.value = !!(
279
- document.fullscreenElement ||
280
- (document as any).webkitFullscreenElement ||
281
- (document as any).msFullscreenElement
282
- )
283
-
284
- // 更新工具栏中的全屏按钮(若存在)
285
- if (toolbarEl) {
286
- const fullscreenBtn = toolbarEl.querySelector(
287
- '.mm-toolbar-item:last-child'
288
- ) as HTMLButtonElement
289
- if (fullscreenBtn) {
290
- fullscreenBtn.title = isFullscreen.value
291
- ? '退出全屏(Exit Fullscreen)'
292
- : '全屏(Fullscreen)'
293
- fullscreenBtn.innerHTML = isFullscreen.value
294
- ? `<img src="${icon__fullscreen_exit}" alt="退出全屏" style="width:18px;height:18px;display:block" />`
295
- : `<img src="${icon__fullscreen}" alt="全屏" style="width:18px;height:18px;display:block" />`
296
- }
297
- }
298
-
299
- // 全屏模式下调整SVG高度
300
- if (svgRef.value) {
301
- if (isFullscreen.value) {
302
- svgRef.value.style.height = 'calc(100vh - 100px)'
303
- } else {
304
- svgRef.value.style.height = '400px'
305
- }
306
-
307
- // 确保居中 - 无论进入还是退出全屏都执行居中
308
- setTimeout(() => {
309
- if (markmapInstance) {
310
- try {
311
- markmapInstance.fit()
312
- } catch (e) {
313
- console.warn('居中失败', e)
314
- }
315
- }
316
- }, 300)
317
- }
318
- }
319
-
320
- function setupObserver() {
321
- if (!svgRef.value) return
322
- if (observer !== null) {
323
- observer.disconnect()
324
- }
325
- observer = new MutationObserver(() => {
326
- // DOM 变动后处理(保留扩展点)
327
- })
328
- observer.observe(svgRef.value, {
329
- childList: true,
330
- subtree: true,
331
- attributes: true,
332
- })
333
- }
334
-
335
- // 只监听内容变化,expandLevel 改动不自动渲染(点击/Enter 确认渲染)
336
- watch(
337
- () => props.content,
338
- (newVal) => {
339
- renderMarkmap(decodeURIComponent(newVal || ''))
340
- }
341
- )
342
-
343
- // 新增:当外部传入的 initialExpandLevel 改变时,组件应同步并重新渲染
344
- watch(
345
- () => props.initialExpandLevel,
346
- (newVal) => {
347
- if (typeof newVal === 'number' && !isNaN(newVal)) {
348
- expandLevel.value = newVal
349
- // 更新输入框显示(如果已创建)
350
- if (toolbarLevelInput) toolbarLevelInput.value = newVal.toString()
351
- // 重新渲染使用新的层级(保持对外部 prop 改动的即时响应)
352
- renderMarkmap(decodeURIComponent(props.content || ''), newVal)
353
- }
354
- }
355
- )
356
-
357
- // 同步:当内部 expandLevel 变更时(例如通过输入框 change 事件),更新工具栏输入显示
358
- watch(expandLevel, (v) => {
359
- if (toolbarLevelInput) {
360
- const asStr = (v || 0).toString()
361
- if (toolbarLevelInput.value !== asStr) {
362
- toolbarLevelInput.value = asStr
363
- }
364
- }
365
- })
366
-
367
- // 点击更新按钮才用当前 expandLevel 渲染
368
- function onUpdateClick() {
369
- renderMarkmap(decodeURIComponent(props.content || ''), expandLevel.value)
370
- }
371
-
372
- function syncMarkmapDarkState(reRender = false) {
373
- const dark = document.documentElement.classList.contains('markmap-dark')
374
- const changed = isMarkmapDark.value !== dark
375
- isMarkmapDark.value = dark
376
- if (typeof window !== 'undefined') {
377
- localStorage.setItem(MARKMAP_THEME_KEY, dark ? 'dark' : 'default')
378
- }
379
- if (reRender && changed && markmapInstance && props.content) {
380
- renderMarkmap(decodeURIComponent(props.content || ''), expandLevel.value)
381
- }
382
- }
383
-
384
- function ensureToolbarAfterContentUpdate() {
385
- if (toolbarReinitTimer) clearTimeout(toolbarReinitTimer)
386
- // ContentCollapse.onContentUpdated 会在 ~150ms 后重组 DOM,稍晚再补挂工具栏
387
- toolbarReinitTimer = setTimeout(() => {
388
- toolbarReinitTimer = null
389
- if (!containerRef.value || !markmapInstance) return
390
- if (!containerRef.value.querySelector('.mm-toolbar')) {
391
- initToolbar()
392
- }
393
- }, 350)
394
- }
395
-
396
- onMounted(() => {
397
- if (typeof window !== 'undefined') {
398
- if (localStorage.getItem(MARKMAP_THEME_KEY) === 'dark') {
399
- document.documentElement.classList.add('markmap-dark')
400
- }
401
- syncMarkmapDarkState(false)
402
- darkClassObserver = new MutationObserver(() => syncMarkmapDarkState(true))
403
- darkClassObserver.observe(document.documentElement, {
404
- attributes: true,
405
- attributeFilter: ['class'],
406
- })
407
- }
408
-
409
- renderMarkmap(decodeURIComponent(props.content || ''))
410
-
411
- // 添加全屏事件监听
412
- document.addEventListener('fullscreenchange', handleFullscreenChange)
413
- document.addEventListener('webkitfullscreenchange', handleFullscreenChange)
414
- document.addEventListener('MSFullscreenChange', handleFullscreenChange)
415
- })
416
-
417
- onContentUpdated(() => {
418
- ensureToolbarAfterContentUpdate()
419
- })
420
-
421
- onBeforeUnmount(() => {
422
- if (darkClassObserver) {
423
- darkClassObserver.disconnect()
424
- darkClassObserver = null
425
- }
426
- if (toolbarReinitTimer) {
427
- clearTimeout(toolbarReinitTimer)
428
- toolbarReinitTimer = null
429
- }
430
- if (toolbarEl) {
431
- toolbarEl.remove()
432
- toolbarEl = null
433
- toolbarLevelInput = null
434
- }
435
- if (markmapInstance) {
436
- try {
437
- markmapInstance.destroy()
438
- } catch {}
439
- markmapInstance = null
440
- }
441
- if (observer !== null) {
442
- observer.disconnect()
443
- observer = null
444
- }
445
-
446
- // 移除全屏事件监听
447
- document.removeEventListener('fullscreenchange', handleFullscreenChange)
448
- document.removeEventListener('webkitfullscreenchange', handleFullscreenChange)
449
- document.removeEventListener('MSFullscreenChange', handleFullscreenChange)
450
- })
451
- </script>
452
-
453
- <template>
454
- <div
455
- class="markmapContainer"
456
- :class="{ 'is-markmap-dark': isMarkmapDark }"
457
- ref="containerRef"
458
- style="position: relative"
459
- >
460
- <svg ref="svgRef" style="width: 100%; height: 400px"></svg>
461
- </div>
462
- </template>
463
-
464
- <style scoped lang="scss">
465
- /**
466
- * MarkMap 组件样式
467
- * 全局样式(.mm-toolbar, .mm-toolbar-item 等)已移至 global-components.css
468
- */
469
-
470
- .markmapContainer {
471
- border-radius: 8px;
472
- padding: 1rem;
473
- overflow: auto;
474
- position: relative;
475
- margin: 1.5rem 0;
476
- transition: all 0.3s ease;
477
- background-color: #fff;
478
- box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
479
-
480
- &:hover {
481
- box-shadow: 0 4px 20px rgba(0, 0, 0, 0.12);
482
- }
483
-
484
- svg {
485
- min-width: 100%;
486
- display: block;
487
- transition: height 0.3s ease;
488
-
489
- text:empty {
490
- animation: pulse 1.5s ease-in-out infinite;
491
- }
492
- }
493
- }
494
-
495
- /* dark background */
496
- .markmapContainer.is-markmap-dark {
497
- background-color: #1d1d1d;
498
- box-shadow: 0 2px 12px rgba(0, 0, 0, 0.3);
499
-
500
- &:hover {
501
- box-shadow: 0 4px 20px rgba(0, 0, 0, 0.4);
502
- }
503
- }
504
-
505
- /* 按钮组样式 */
506
- .btnGroup {
507
- color: var(--vp-c-brand-1);
508
-
509
- input {
510
- display: inline-block;
511
- width: 2rem;
512
- text-align: center;
513
- border: 1px solid var(--vp-c-divider);
514
- border-radius: 4px;
515
- padding: 2px 4px;
516
- background: var(--vp-c-bg);
517
- color: var(--vp-c-text-1);
518
- transition: all 0.2s ease;
519
-
520
- &:focus {
521
- outline: none;
522
- border-color: var(--vp-c-brand-1);
523
- box-shadow: 0 0 0 2px rgba(var(--vp-c-brand-1), 0.1);
524
- }
525
-
526
- &:hover {
527
- border-color: var(--vp-c-brand-2);
528
- }
529
- }
530
-
531
- button {
532
- outline: none;
533
- border: none;
534
- cursor: pointer;
535
- padding: 4px 8px;
536
- border-radius: 4px;
537
- background-color: #f0f0f0;
538
- transition: all 0.2s ease;
539
- font-weight: 500;
540
-
541
- &:hover {
542
- background-color: #e0e0e0;
543
- transform: translateY(-1px);
544
- }
545
-
546
- &:active {
547
- transform: translateY(0);
548
- }
549
- }
550
- }
551
-
552
- :global(.markmap-dark) .btnGroup {
553
- button {
554
- background-color: #333;
555
- color: #fff;
556
-
557
- &:hover {
558
- background-color: #444;
559
- }
560
- }
561
-
562
- input {
563
- background-color: #2a2a2a;
564
- border-color: #444;
565
- color: #fff;
566
-
567
- &:focus {
568
- border-color: var(--vp-c-brand-1);
569
- }
570
- }
571
- }
572
-
573
- /* 全屏样式 */
574
- .markmapContainer:fullscreen,
575
- .markmapContainer:-ms-fullscreen,
576
- .markmapContainer:-webkit-full-screen {
577
- width: 100%;
578
- height: 100%;
579
- padding: 20px;
580
- background: white;
581
- display: flex;
582
- flex-direction: column;
583
- justify-content: center;
584
- box-shadow: none;
585
- }
586
-
587
- .markmapContainer.is-markmap-dark:fullscreen,
588
- .markmapContainer.is-markmap-dark:-ms-fullscreen,
589
- .markmapContainer.is-markmap-dark:-webkit-full-screen {
590
- background: #1d1d1d;
591
- }
592
-
593
- /* 响应式设计 */
594
- @media (max-width: 768px) {
595
- .markmapContainer {
596
- padding: 0.75rem;
597
- margin: 1rem 0;
598
- }
599
- }
600
-
601
- @media (max-width: 480px) {
602
- .markmapContainer {
603
- padding: 0.5rem;
604
- margin: 0.75rem 0;
605
- }
606
- }
607
-
608
- /* 加载动画 */
609
- @keyframes pulse {
610
- 0%,
611
- 100% {
612
- opacity: 1;
613
- }
614
- 50% {
615
- opacity: 0.5;
616
- }
617
- }
618
- </style>