@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,282 +1,282 @@
1
- <template>
2
- <!-- #region img preview -->
3
- <div
4
- v-show="preview.visible"
5
- :class="$style.tnPreview"
6
- @click.self="closePreview"
7
- @wheel.prevent="onWheel"
8
- >
9
- <!-- 左侧切换按钮 -->
10
- <button
11
- v-if="preview.images.length > 1"
12
- :class="[$style.nav, $style.navLeft]"
13
- @click.stop="prevImage"
14
- >
15
- <img :src="icon__prev" alt="prev" />
16
- </button>
17
- <!-- 右侧切换按钮 -->
18
- <button
19
- v-if="preview.images.length > 1"
20
- :class="[$style.nav, $style.navRight]"
21
- @click.stop="nextImage"
22
- >
23
- <img :src="icon__next" alt="next" />
24
- </button>
25
-
26
- <div :class="$style.toolbar">
27
- <button @click.stop="zoomOut" title="缩小">
28
- <img :src="icon__zoom_out" alt="zoom out" />
29
- </button>
30
- <button @click.stop="resetTransform" title="还原">
31
- <img :src="icon__restore" alt="restore" />
32
- </button>
33
- <button @click.stop="zoomIn" title="放大">
34
- <img :src="icon__zoom_in" alt="zoom in" />
35
- </button>
36
- <button :class="$style.close" @click.stop="closePreview" title="关闭">
37
- <img :src="icon__close" alt="close" />
38
- </button>
39
- </div>
40
-
41
- <img
42
- ref="previewImg"
43
- :class="[$style.img, { [$style.dragging]: isDragging }]"
44
- :src="preview.src"
45
- :style="previewStyle"
46
- @pointerdown="onPointerDown"
47
- draggable="false"
48
- />
49
-
50
- <!-- 底部页码指示器 -->
51
- <div v-if="preview.images.length > 1" :class="$style.counter">
52
- {{ preview.index + 1 }} / {{ preview.images.length }}
53
- </div>
54
- </div>
55
- <!-- #endregion -->
56
- </template>
57
-
58
- <script setup>
1
+ <template>
2
+ <!-- #region img preview -->
3
+ <div
4
+ v-show="preview.visible"
5
+ :class="$style.tnPreview"
6
+ @click.self="closePreview"
7
+ @wheel.prevent="onWheel"
8
+ >
9
+ <!-- 左侧切换按钮 -->
10
+ <button
11
+ v-if="preview.images.length > 1"
12
+ :class="[$style.nav, $style.navLeft]"
13
+ @click.stop="prevImage"
14
+ >
15
+ <img :src="icon__prev" alt="prev" />
16
+ </button>
17
+ <!-- 右侧切换按钮 -->
18
+ <button
19
+ v-if="preview.images.length > 1"
20
+ :class="[$style.nav, $style.navRight]"
21
+ @click.stop="nextImage"
22
+ >
23
+ <img :src="icon__next" alt="next" />
24
+ </button>
25
+
26
+ <div :class="$style.toolbar">
27
+ <button @click.stop="zoomOut" title="缩小">
28
+ <img :src="icon__zoom_out" alt="zoom out" />
29
+ </button>
30
+ <button @click.stop="resetTransform" title="还原">
31
+ <img :src="icon__restore" alt="restore" />
32
+ </button>
33
+ <button @click.stop="zoomIn" title="放大">
34
+ <img :src="icon__zoom_in" alt="zoom in" />
35
+ </button>
36
+ <button :class="$style.close" @click.stop="closePreview" title="关闭">
37
+ <img :src="icon__close" alt="close" />
38
+ </button>
39
+ </div>
40
+
41
+ <img
42
+ ref="previewImg"
43
+ :class="[$style.img, { [$style.dragging]: isDragging }]"
44
+ :src="preview.src"
45
+ :style="previewStyle"
46
+ @pointerdown="onPointerDown"
47
+ draggable="false"
48
+ />
49
+
50
+ <!-- 底部页码指示器 -->
51
+ <div v-if="preview.images.length > 1" :class="$style.counter">
52
+ {{ preview.index + 1 }} / {{ preview.images.length }}
53
+ </div>
54
+ </div>
55
+ <!-- #endregion -->
56
+ </template>
57
+
58
+ <script setup>
59
59
  import { ref, computed, onMounted, onBeforeUnmount } from 'vue'
60
-
61
- import {
62
- icon__close,
63
- icon__next,
64
- icon__prev,
65
- icon__restore,
66
- icon__zoom_in,
67
- icon__zoom_out,
68
- } from '../../assets/icons'
69
-
70
-
71
- const preview = ref({
72
- visible: false,
73
- src: '',
74
- scale: 1,
75
- tx: 0,
76
- ty: 0,
77
- images: [], // 当前文档里的所有图片 src
78
- index: -1, // 当前打开的索引
79
- })
80
-
81
- const previewImg = ref(null)
82
-
83
- const isDragging = ref(false)
84
-
85
- const previewStyle = computed(() => ({
86
- transform: `translate(${preview.value.tx}px, ${preview.value.ty}px) scale(${preview.value.scale})`,
87
- cursor: 'grab',
88
- }))
89
-
90
- function openPreview(src) {
91
- // 收集当前文档区所有 img,排除:
92
- // 1. data-preview="false" 的图片
93
- // 2. .tn-preview-ignore 容器内的图片
94
- // 3. 组件工具栏按钮内的图标 (button > img)
95
- // 4. 导航按钮内的图标
96
- const imgs = Array.from(document.querySelectorAll('.vp-doc img'))
97
- .filter((img) => {
98
- // 排除明确标记不预览的
99
- if (
100
- img.dataset?.preview === 'false' ||
101
- img.closest('.tn-preview-ignore')
102
- ) {
103
- return false
104
- }
105
-
106
- // 排除按钮内的图标 (工具栏图标)
107
- if (img.closest('button')) {
108
- return false
109
- }
110
-
111
- // 排除小尺寸图标 (通常是图标而非内容图片)
112
- if (img.naturalWidth < 50 && img.naturalHeight < 50) {
113
- return false
114
- }
115
-
116
- return true
117
- })
118
- .map((img) => img.currentSrc || img.src)
119
-
120
- preview.value.images = imgs
121
- preview.value.index = imgs.indexOf(src)
122
-
123
- preview.value.visible = true
124
- preview.value.src = src
125
- preview.value.scale = 1
126
- preview.value.tx = 0
127
- preview.value.ty = 0
128
- // 锁滚动(可选)
129
- document.documentElement.style.overflow = 'hidden'
130
- }
131
-
132
- function prevImage() {
133
- if (preview.value.images.length <= 1) return
134
- preview.value.index =
135
- (preview.value.index - 1 + preview.value.images.length) %
136
- preview.value.images.length
137
- preview.value.src = preview.value.images[preview.value.index]
138
- resetTransform()
139
- }
140
-
141
- function nextImage() {
142
- if (preview.value.images.length <= 1) return
143
- preview.value.index = (preview.value.index + 1) % preview.value.images.length
144
- preview.value.src = preview.value.images[preview.value.index]
145
- resetTransform()
146
- }
147
-
148
- function closePreview() {
149
- preview.value.visible = false
150
- document.documentElement.style.overflow = ''
151
- }
152
-
153
- function zoomIn() {
154
- preview.value.scale = Math.min(3, preview.value.scale + 0.1)
155
- }
156
-
157
- function zoomOut() {
158
- preview.value.scale = Math.max(0.2, preview.value.scale - 0.1)
159
- }
160
-
161
- function resetTransform() {
162
- preview.value.scale = 1
163
- preview.value.tx = 0
164
- preview.value.ty = 0
165
- }
166
-
167
- // 滚轮缩放(以视口中心为参考,简单好用)
168
- function onWheel(e) {
169
- const delta = -e.deltaY
170
- const factor = delta > 0 ? 1.1 : 1 / 1.1
171
- const next = Math.min(6, Math.max(0.2, preview.value.scale * factor))
172
- preview.value.scale = next
173
- }
174
-
175
- // 拖拽(Pointer Events,兼容鼠标/触屏)
176
- let dragging = false
177
- let startX = 0
178
- let startY = 0
179
- let baseX = 0
180
- let baseY = 0
181
-
182
- function onPointerDown(e) {
183
- dragging = true
184
- isDragging.value = true
185
- e.target.setPointerCapture?.(e.pointerId)
186
- startX = e.clientX
187
- startY = e.clientY
188
- baseX = preview.value.tx
189
- baseY = preview.value.ty
190
- window.addEventListener('pointermove', onPointerMove)
191
- window.addEventListener('pointerup', onPointerUp, { once: true })
192
- }
193
-
194
- function onPointerMove(e) {
195
- if (!dragging) return
196
- preview.value.tx = baseX + (e.clientX - startX)
197
- preview.value.ty = baseY + (e.clientY - startY)
198
- }
199
-
200
- function onPointerUp() {
201
- dragging = false
202
- isDragging.value = false
203
- window.removeEventListener('pointermove', onPointerMove)
204
- }
205
-
206
- let downX = 0
207
- function onPointerDownSwiper(e) {
208
- downX = e.clientX
209
- }
210
-
211
- // 事件委托:拦截正文区域的 <img> 点击
212
- function onDocClick(e) {
213
- if (preview.value.visible) return // 预览层打开时忽略底下点击
214
- const target = e.target
215
- if (!target) return
216
-
217
- // 只处理正文:VitePress 主文档区常见容器:.vp-doc / .VPDoc .main
218
- const img = target.closest?.('img')
219
- if (!img) return
220
- const inDoc = img.closest('.main')
221
- if (!inDoc) return
222
-
223
- // 判断是否在 swiper 中切换图片
224
- const inSwiper = img.closest('.swiper-container')
225
- if (inSwiper) {
226
- const diffX = Math.abs(e.clientX - downX)
227
- // 只要水平位移超过阈值,就认为是滑动,而不是点击
228
- if (diffX > 5) {
229
- return
230
- }
231
- }
232
-
233
- // 明确排除:带 data-preview="false" 或 .tn-preview-ignore 的图片
234
- if (img.dataset?.preview === 'false' || img.closest('.tn-preview-ignore'))
235
- return
236
-
237
- // 排除按钮内的图标 (组件工具栏)
238
- if (img.closest('button')) return
239
-
240
- // 排除小尺寸图标
241
- if (img.naturalWidth < 50 && img.naturalHeight < 50) return
242
-
243
- // !禁止图片包含超链接
244
- // TODO
245
- // 做法 1:包含超链接的图片不支持 preview;
246
- // 做法 2:如果图片包了链接,阻止跳转,支持 preview;【当前的方案】
247
- // 做法 3:维护一个白名单,对特定类型的图片进行特殊处理;
248
- const a = img.closest('a')
249
- if (a) {
250
- e.preventDefault()
251
- e.stopPropagation()
252
- }
253
-
254
- const src = img.currentSrc || img.src
255
- if (src) openPreview(src)
256
- }
257
-
258
- // 键盘 ESC 关闭
259
- function onKeydown(e) {
260
- if (!preview.value.visible) return
261
- if (e.key === 'Escape') closePreview()
262
- else if (e.key === 'ArrowLeft') prevImage()
263
- else if (e.key === 'ArrowRight') nextImage()
264
- else if (e.key === 'Enter') toggleZoom()
265
- else if (e.key === 'ArrowUp') zoomIn()
266
- else if (e.key === 'ArrowDown') zoomOut()
267
- }
268
-
269
- onMounted(() => {
270
- document.addEventListener('click', onDocClick, true)
271
- document.addEventListener('keydown', onKeydown)
272
- document.addEventListener('pointerdown', onPointerDownSwiper, true)
273
- })
274
-
275
- onBeforeUnmount(() => {
276
- document.removeEventListener('click', onDocClick, true)
277
- document.removeEventListener('keydown', onKeydown)
278
- document.removeEventListener('pointerdown', onPointerDownSwiper, true)
279
- })
280
- </script>
281
-
282
- <style module src="./ImagePreview.module.scss"></style>
60
+
61
+ import {
62
+ icon__close,
63
+ icon__next,
64
+ icon__prev,
65
+ icon__restore,
66
+ icon__zoom_in,
67
+ icon__zoom_out,
68
+ } from '../../assets/icons'
69
+
70
+
71
+ const preview = ref({
72
+ visible: false,
73
+ src: '',
74
+ scale: 1,
75
+ tx: 0,
76
+ ty: 0,
77
+ images: [], // 当前文档里的所有图片 src
78
+ index: -1, // 当前打开的索引
79
+ })
80
+
81
+ const previewImg = ref(null)
82
+
83
+ const isDragging = ref(false)
84
+
85
+ const previewStyle = computed(() => ({
86
+ transform: `translate(${preview.value.tx}px, ${preview.value.ty}px) scale(${preview.value.scale})`,
87
+ cursor: 'grab',
88
+ }))
89
+
90
+ function openPreview(src) {
91
+ // 收集当前文档区所有 img,排除:
92
+ // 1. data-preview="false" 的图片
93
+ // 2. .tn-preview-ignore 容器内的图片
94
+ // 3. 组件工具栏按钮内的图标 (button > img)
95
+ // 4. 导航按钮内的图标
96
+ const imgs = Array.from(document.querySelectorAll('.vp-doc img'))
97
+ .filter((img) => {
98
+ // 排除明确标记不预览的
99
+ if (
100
+ img.dataset?.preview === 'false' ||
101
+ img.closest('.tn-preview-ignore')
102
+ ) {
103
+ return false
104
+ }
105
+
106
+ // 排除按钮内的图标 (工具栏图标)
107
+ if (img.closest('button')) {
108
+ return false
109
+ }
110
+
111
+ // 排除小尺寸图标 (通常是图标而非内容图片)
112
+ if (img.naturalWidth < 50 && img.naturalHeight < 50) {
113
+ return false
114
+ }
115
+
116
+ return true
117
+ })
118
+ .map((img) => img.currentSrc || img.src)
119
+
120
+ preview.value.images = imgs
121
+ preview.value.index = imgs.indexOf(src)
122
+
123
+ preview.value.visible = true
124
+ preview.value.src = src
125
+ preview.value.scale = 1
126
+ preview.value.tx = 0
127
+ preview.value.ty = 0
128
+ // 锁滚动(可选)
129
+ document.documentElement.style.overflow = 'hidden'
130
+ }
131
+
132
+ function prevImage() {
133
+ if (preview.value.images.length <= 1) return
134
+ preview.value.index =
135
+ (preview.value.index - 1 + preview.value.images.length) %
136
+ preview.value.images.length
137
+ preview.value.src = preview.value.images[preview.value.index]
138
+ resetTransform()
139
+ }
140
+
141
+ function nextImage() {
142
+ if (preview.value.images.length <= 1) return
143
+ preview.value.index = (preview.value.index + 1) % preview.value.images.length
144
+ preview.value.src = preview.value.images[preview.value.index]
145
+ resetTransform()
146
+ }
147
+
148
+ function closePreview() {
149
+ preview.value.visible = false
150
+ document.documentElement.style.overflow = ''
151
+ }
152
+
153
+ function zoomIn() {
154
+ preview.value.scale = Math.min(3, preview.value.scale + 0.1)
155
+ }
156
+
157
+ function zoomOut() {
158
+ preview.value.scale = Math.max(0.2, preview.value.scale - 0.1)
159
+ }
160
+
161
+ function resetTransform() {
162
+ preview.value.scale = 1
163
+ preview.value.tx = 0
164
+ preview.value.ty = 0
165
+ }
166
+
167
+ // 滚轮缩放(以视口中心为参考,简单好用)
168
+ function onWheel(e) {
169
+ const delta = -e.deltaY
170
+ const factor = delta > 0 ? 1.1 : 1 / 1.1
171
+ const next = Math.min(6, Math.max(0.2, preview.value.scale * factor))
172
+ preview.value.scale = next
173
+ }
174
+
175
+ // 拖拽(Pointer Events,兼容鼠标/触屏)
176
+ let dragging = false
177
+ let startX = 0
178
+ let startY = 0
179
+ let baseX = 0
180
+ let baseY = 0
181
+
182
+ function onPointerDown(e) {
183
+ dragging = true
184
+ isDragging.value = true
185
+ e.target.setPointerCapture?.(e.pointerId)
186
+ startX = e.clientX
187
+ startY = e.clientY
188
+ baseX = preview.value.tx
189
+ baseY = preview.value.ty
190
+ window.addEventListener('pointermove', onPointerMove)
191
+ window.addEventListener('pointerup', onPointerUp, { once: true })
192
+ }
193
+
194
+ function onPointerMove(e) {
195
+ if (!dragging) return
196
+ preview.value.tx = baseX + (e.clientX - startX)
197
+ preview.value.ty = baseY + (e.clientY - startY)
198
+ }
199
+
200
+ function onPointerUp() {
201
+ dragging = false
202
+ isDragging.value = false
203
+ window.removeEventListener('pointermove', onPointerMove)
204
+ }
205
+
206
+ let downX = 0
207
+ function onPointerDownSwiper(e) {
208
+ downX = e.clientX
209
+ }
210
+
211
+ // 事件委托:拦截正文区域的 <img> 点击
212
+ function onDocClick(e) {
213
+ if (preview.value.visible) return // 预览层打开时忽略底下点击
214
+ const target = e.target
215
+ if (!target) return
216
+
217
+ // 只处理正文:VitePress 主文档区常见容器:.vp-doc / .VPDoc .main
218
+ const img = target.closest?.('img')
219
+ if (!img) return
220
+ const inDoc = img.closest('.main')
221
+ if (!inDoc) return
222
+
223
+ // 判断是否在 swiper 中切换图片
224
+ const inSwiper = img.closest('.swiper-container')
225
+ if (inSwiper) {
226
+ const diffX = Math.abs(e.clientX - downX)
227
+ // 只要水平位移超过阈值,就认为是滑动,而不是点击
228
+ if (diffX > 5) {
229
+ return
230
+ }
231
+ }
232
+
233
+ // 明确排除:带 data-preview="false" 或 .tn-preview-ignore 的图片
234
+ if (img.dataset?.preview === 'false' || img.closest('.tn-preview-ignore'))
235
+ return
236
+
237
+ // 排除按钮内的图标 (组件工具栏)
238
+ if (img.closest('button')) return
239
+
240
+ // 排除小尺寸图标
241
+ if (img.naturalWidth < 50 && img.naturalHeight < 50) return
242
+
243
+ // !禁止图片包含超链接
244
+ // TODO
245
+ // 做法 1:包含超链接的图片不支持 preview;
246
+ // 做法 2:如果图片包了链接,阻止跳转,支持 preview;【当前的方案】
247
+ // 做法 3:维护一个白名单,对特定类型的图片进行特殊处理;
248
+ const a = img.closest('a')
249
+ if (a) {
250
+ e.preventDefault()
251
+ e.stopPropagation()
252
+ }
253
+
254
+ const src = img.currentSrc || img.src
255
+ if (src) openPreview(src)
256
+ }
257
+
258
+ // 键盘 ESC 关闭
259
+ function onKeydown(e) {
260
+ if (!preview.value.visible) return
261
+ if (e.key === 'Escape') closePreview()
262
+ else if (e.key === 'ArrowLeft') prevImage()
263
+ else if (e.key === 'ArrowRight') nextImage()
264
+ else if (e.key === 'Enter') toggleZoom()
265
+ else if (e.key === 'ArrowUp') zoomIn()
266
+ else if (e.key === 'ArrowDown') zoomOut()
267
+ }
268
+
269
+ onMounted(() => {
270
+ document.addEventListener('click', onDocClick, true)
271
+ document.addEventListener('keydown', onKeydown)
272
+ document.addEventListener('pointerdown', onPointerDownSwiper, true)
273
+ })
274
+
275
+ onBeforeUnmount(() => {
276
+ document.removeEventListener('click', onDocClick, true)
277
+ document.removeEventListener('keydown', onKeydown)
278
+ document.removeEventListener('pointerdown', onPointerDownSwiper, true)
279
+ })
280
+ </script>
281
+
282
+ <style module src="./ImagePreview.module.scss"></style>