@tnotesjs/core 0.0.6 → 0.0.8

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 (116) hide show
  1. package/README.md +87 -84
  2. package/dist/{chunk-JDVN2QYW.js → chunk-JUMVJKWV.js} +615 -614
  3. package/dist/cli/index.js +125 -125
  4. package/dist/vitepress/config/index.js +184 -210
  5. package/package.json +15 -3
  6. package/types/config.ts +61 -61
  7. package/types/index.ts +11 -11
  8. package/types/note.ts +33 -33
  9. package/types/shims.d.ts +3 -4
  10. package/vitepress/assets/icons/icon__check.svg +3 -3
  11. package/vitepress/assets/icons/icon__clipboard.svg +7 -7
  12. package/vitepress/assets/icons/icon__close.svg +1 -1
  13. package/vitepress/assets/icons/icon__collapse.svg +1 -1
  14. package/vitepress/assets/icons/icon__confirm.svg +1 -1
  15. package/vitepress/assets/icons/icon__copy.svg +4 -4
  16. package/vitepress/assets/icons/icon__focus.svg +1 -1
  17. package/vitepress/assets/icons/icon__fold.svg +3 -3
  18. package/vitepress/assets/icons/icon__folder.svg +1 -1
  19. package/vitepress/assets/icons/icon__fullscreen.svg +1 -1
  20. package/vitepress/assets/icons/icon__fullscreen_exit.svg +1 -1
  21. package/vitepress/assets/icons/icon__github.svg +3 -3
  22. package/vitepress/assets/icons/icon__mindmap.svg +1 -1
  23. package/vitepress/assets/icons/icon__next.svg +1 -1
  24. package/vitepress/assets/icons/icon__number_gray.svg +1 -1
  25. package/vitepress/assets/icons/icon__number_purple.svg +1 -1
  26. package/vitepress/assets/icons/icon__prev.svg +1 -1
  27. package/vitepress/assets/icons/icon__restore.svg +1 -1
  28. package/vitepress/assets/icons/icon__rotate.svg +4 -4
  29. package/vitepress/assets/icons/icon__search.svg +1 -1
  30. package/vitepress/assets/icons/icon__sidebar_collapsed.svg +1 -1
  31. package/vitepress/assets/icons/icon__sidebar_opened.svg +1 -1
  32. package/vitepress/assets/icons/icon__totop.svg +5 -5
  33. package/vitepress/assets/icons/icon__vscode.svg +5 -5
  34. package/vitepress/assets/icons/icon__zoom_fit.svg +1 -1
  35. package/vitepress/assets/icons/icon__zoom_in.svg +1 -1
  36. package/vitepress/assets/icons/icon__zoom_out.svg +1 -1
  37. package/vitepress/assets/icons/icon__zoom_reset.svg +1 -1
  38. package/vitepress/assets/icons/index.ts +38 -38
  39. package/vitepress/components/BilibiliOutsidePlayer/BilibiliOutsidePlayer.vue +20 -20
  40. package/vitepress/components/CodeBlockFullscreen/CodeBlockFullscreen.vue +373 -372
  41. package/vitepress/components/CodeBlockFullscreen/index.ts +115 -114
  42. package/vitepress/components/CodeBlockFullscreen/styles.css +64 -64
  43. package/vitepress/components/Discussions/Discussions.module.scss +32 -32
  44. package/vitepress/components/Discussions/Discussions.vue +211 -210
  45. package/vitepress/components/EnWordList/EnWordList.module.scss +124 -124
  46. package/vitepress/components/EnWordList/EnWordList.vue +543 -542
  47. package/vitepress/components/EnWordList/RightClickMenu.module.scss +22 -22
  48. package/vitepress/components/EnWordList/RightClickMenu.vue +66 -66
  49. package/vitepress/components/Footprints/Footprints.module.scss +93 -93
  50. package/vitepress/components/Footprints/Footprints.vue +377 -377
  51. package/vitepress/components/Layout/AboutModal.module.scss +233 -233
  52. package/vitepress/components/Layout/AboutModal.vue +105 -105
  53. package/vitepress/components/Layout/AboutPanel.vue +270 -265
  54. package/vitepress/components/Layout/ContentCollapse.vue +603 -602
  55. package/vitepress/components/Layout/CustomSidebar.vue +608 -605
  56. package/vitepress/components/Layout/DocBeforeControls.vue +149 -139
  57. package/vitepress/components/Layout/DocFooter.vue +233 -232
  58. package/vitepress/components/Layout/ImagePreview.module.scss +201 -201
  59. package/vitepress/components/Layout/ImagePreview.vue +281 -280
  60. package/vitepress/components/Layout/Layout.module.scss +661 -661
  61. package/vitepress/components/Layout/Layout.vue +539 -542
  62. package/vitepress/components/Layout/NoteStatus.vue +140 -139
  63. package/vitepress/components/Layout/SidebarItems.vue +267 -262
  64. package/vitepress/components/Layout/SidebarNavBefore.vue +92 -92
  65. package/vitepress/components/Layout/Swiper.vue +167 -167
  66. package/vitepress/components/Layout/ToggleFullContent.module.scss +11 -11
  67. package/vitepress/components/Layout/ToggleFullContent.vue +35 -34
  68. package/vitepress/components/Layout/ToggleSidebar.module.scss +11 -11
  69. package/vitepress/components/Layout/ToggleSidebar.vue +36 -35
  70. package/vitepress/components/Layout/composables/useCollapseControl.ts +88 -88
  71. package/vitepress/components/Layout/composables/useNoteConfig.ts +119 -121
  72. package/vitepress/components/Layout/composables/useNoteSave.ts +173 -172
  73. package/vitepress/components/Layout/composables/useNoteValidation.ts +85 -85
  74. package/vitepress/components/Layout/composables/useRedirect.ts +113 -110
  75. package/vitepress/components/Layout/composables/useVSCodeIntegration.ts +86 -83
  76. package/vitepress/components/Layout/homeReadme.data.ts +124 -124
  77. package/vitepress/components/LoadingPage/LoadingPage.vue +192 -192
  78. package/vitepress/components/MarkMap/MarkMap.module.scss +159 -159
  79. package/vitepress/components/MarkMap/MarkMap.vue +405 -404
  80. package/vitepress/components/Mermaid/Mermaid.module.scss +275 -275
  81. package/vitepress/components/Mermaid/Mermaid.vue +365 -363
  82. package/vitepress/components/NotesTable/NotesTable.module.scss +77 -77
  83. package/vitepress/components/NotesTable/NotesTable.vue +98 -97
  84. package/vitepress/components/NotesTable/README.md +67 -67
  85. package/vitepress/components/Settings/Settings.module.scss +433 -433
  86. package/vitepress/components/Settings/Settings.vue +306 -305
  87. package/vitepress/components/SidebarCard/MindMapView.vue +484 -483
  88. package/vitepress/components/SidebarCard/NotesTrendChart.vue +108 -108
  89. package/vitepress/components/SidebarCard/SidebarCard.vue +946 -948
  90. package/vitepress/components/Tooltip/Tooltip.vue +70 -70
  91. package/vitepress/components/constants.ts +91 -91
  92. package/vitepress/components/notesConfig.data.ts +73 -73
  93. package/vitepress/components/sidebar.data.ts +59 -59
  94. package/vitepress/components/tnotes-config.data.ts +21 -21
  95. package/vitepress/components/utils.ts +26 -26
  96. package/vitepress/config/index.ts +160 -146
  97. package/vitepress/configs/constants.ts +26 -26
  98. package/vitepress/configs/head.config.ts +25 -24
  99. package/vitepress/configs/index.ts +9 -9
  100. package/vitepress/configs/markdown-it.d.ts +23 -20
  101. package/vitepress/configs/markdown.config.ts +366 -365
  102. package/vitepress/configs/theme.config.ts +108 -108
  103. package/vitepress/plugins/buildProgressPlugin.ts +434 -470
  104. package/vitepress/plugins/getNoteByConfigIdPlugin.ts +107 -106
  105. package/vitepress/plugins/renameNotePlugin.ts +60 -59
  106. package/vitepress/plugins/updateConfigPlugin.ts +63 -62
  107. package/vitepress/theme/index.ts +95 -93
  108. package/vitepress/theme/styles/base.scss +50 -50
  109. package/vitepress/theme/styles/components/404.scss +31 -31
  110. package/vitepress/theme/styles/components/collapse.scss +175 -175
  111. package/vitepress/theme/styles/components/markmap.scss +101 -101
  112. package/vitepress/theme/styles/components/swiper.scss +255 -255
  113. package/vitepress/theme/styles/index.scss +25 -25
  114. package/vitepress/theme/styles/layout.scss +62 -62
  115. package/vitepress/theme/styles/utilities.scss +39 -39
  116. package/vitepress/theme/styles/vitepress-override.scss +25 -25
@@ -1,281 +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>
59
- import {
60
- icon__close,
61
- icon__next,
62
- icon__prev,
63
- icon__restore,
64
- icon__zoom_in,
65
- icon__zoom_out,
66
- } from '../../assets/icons'
67
-
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>
68
59
  import { ref, computed, onMounted, onBeforeUnmount } from 'vue'
69
-
70
- const preview = ref({
71
- visible: false,
72
- src: '',
73
- scale: 1,
74
- tx: 0,
75
- ty: 0,
76
- images: [], // 当前文档里的所有图片 src
77
- index: -1, // 当前打开的索引
78
- })
79
-
80
- const previewImg = ref(null)
81
-
82
- const isDragging = ref(false)
83
-
84
- const previewStyle = computed(() => ({
85
- transform: `translate(${preview.value.tx}px, ${preview.value.ty}px) scale(${preview.value.scale})`,
86
- cursor: 'grab',
87
- }))
88
-
89
- function openPreview(src) {
90
- // 收集当前文档区所有 img,排除:
91
- // 1. data-preview="false" 的图片
92
- // 2. .tn-preview-ignore 容器内的图片
93
- // 3. 组件工具栏按钮内的图标 (button > img)
94
- // 4. 导航按钮内的图标
95
- const imgs = Array.from(document.querySelectorAll('.vp-doc img'))
96
- .filter((img) => {
97
- // 排除明确标记不预览的
98
- if (
99
- img.dataset?.preview === 'false' ||
100
- img.closest('.tn-preview-ignore')
101
- ) {
102
- return false
103
- }
104
-
105
- // 排除按钮内的图标 (工具栏图标)
106
- if (img.closest('button')) {
107
- return false
108
- }
109
-
110
- // 排除小尺寸图标 (通常是图标而非内容图片)
111
- if (img.naturalWidth < 50 && img.naturalHeight < 50) {
112
- return false
113
- }
114
-
115
- return true
116
- })
117
- .map((img) => img.currentSrc || img.src)
118
-
119
- preview.value.images = imgs
120
- preview.value.index = imgs.indexOf(src)
121
-
122
- preview.value.visible = true
123
- preview.value.src = src
124
- preview.value.scale = 1
125
- preview.value.tx = 0
126
- preview.value.ty = 0
127
- // 锁滚动(可选)
128
- document.documentElement.style.overflow = 'hidden'
129
- }
130
-
131
- function prevImage() {
132
- if (preview.value.images.length <= 1) return
133
- preview.value.index =
134
- (preview.value.index - 1 + preview.value.images.length) %
135
- preview.value.images.length
136
- preview.value.src = preview.value.images[preview.value.index]
137
- resetTransform()
138
- }
139
-
140
- function nextImage() {
141
- if (preview.value.images.length <= 1) return
142
- preview.value.index = (preview.value.index + 1) % preview.value.images.length
143
- preview.value.src = preview.value.images[preview.value.index]
144
- resetTransform()
145
- }
146
-
147
- function closePreview() {
148
- preview.value.visible = false
149
- document.documentElement.style.overflow = ''
150
- }
151
-
152
- function zoomIn() {
153
- preview.value.scale = Math.min(3, preview.value.scale + 0.1)
154
- }
155
-
156
- function zoomOut() {
157
- preview.value.scale = Math.max(0.2, preview.value.scale - 0.1)
158
- }
159
-
160
- function resetTransform() {
161
- preview.value.scale = 1
162
- preview.value.tx = 0
163
- preview.value.ty = 0
164
- }
165
-
166
- // 滚轮缩放(以视口中心为参考,简单好用)
167
- function onWheel(e) {
168
- const delta = -e.deltaY
169
- const factor = delta > 0 ? 1.1 : 1 / 1.1
170
- const next = Math.min(6, Math.max(0.2, preview.value.scale * factor))
171
- preview.value.scale = next
172
- }
173
-
174
- // 拖拽(Pointer Events,兼容鼠标/触屏)
175
- let dragging = false
176
- let startX = 0
177
- let startY = 0
178
- let baseX = 0
179
- let baseY = 0
180
-
181
- function onPointerDown(e) {
182
- dragging = true
183
- isDragging.value = true
184
- e.target.setPointerCapture?.(e.pointerId)
185
- startX = e.clientX
186
- startY = e.clientY
187
- baseX = preview.value.tx
188
- baseY = preview.value.ty
189
- window.addEventListener('pointermove', onPointerMove)
190
- window.addEventListener('pointerup', onPointerUp, { once: true })
191
- }
192
-
193
- function onPointerMove(e) {
194
- if (!dragging) return
195
- preview.value.tx = baseX + (e.clientX - startX)
196
- preview.value.ty = baseY + (e.clientY - startY)
197
- }
198
-
199
- function onPointerUp() {
200
- dragging = false
201
- isDragging.value = false
202
- window.removeEventListener('pointermove', onPointerMove)
203
- }
204
-
205
- let downX = 0
206
- function onPointerDownSwiper(e) {
207
- downX = e.clientX
208
- }
209
-
210
- // 事件委托:拦截正文区域的 <img> 点击
211
- function onDocClick(e) {
212
- if (preview.value.visible) return // 预览层打开时忽略底下点击
213
- const target = e.target
214
- if (!target) return
215
-
216
- // 只处理正文:VitePress 主文档区常见容器:.vp-doc / .VPDoc .main
217
- const img = target.closest?.('img')
218
- if (!img) return
219
- const inDoc = img.closest('.main')
220
- if (!inDoc) return
221
-
222
- // 判断是否在 swiper 中切换图片
223
- const inSwiper = img.closest('.swiper-container')
224
- if (inSwiper) {
225
- const diffX = Math.abs(e.clientX - downX)
226
- // 只要水平位移超过阈值,就认为是滑动,而不是点击
227
- if (diffX > 5) {
228
- return
229
- }
230
- }
231
-
232
- // 明确排除:带 data-preview="false" 或 .tn-preview-ignore 的图片
233
- if (img.dataset?.preview === 'false' || img.closest('.tn-preview-ignore'))
234
- return
235
-
236
- // 排除按钮内的图标 (组件工具栏)
237
- if (img.closest('button')) return
238
-
239
- // 排除小尺寸图标
240
- if (img.naturalWidth < 50 && img.naturalHeight < 50) return
241
-
242
- // !禁止图片包含超链接
243
- // TODO
244
- // 做法 1:包含超链接的图片不支持 preview;
245
- // 做法 2:如果图片包了链接,阻止跳转,支持 preview;【当前的方案】
246
- // 做法 3:维护一个白名单,对特定类型的图片进行特殊处理;
247
- const a = img.closest('a')
248
- if (a) {
249
- e.preventDefault()
250
- e.stopPropagation()
251
- }
252
-
253
- const src = img.currentSrc || img.src
254
- if (src) openPreview(src)
255
- }
256
-
257
- // 键盘 ESC 关闭
258
- function onKeydown(e) {
259
- if (!preview.value.visible) return
260
- if (e.key === 'Escape') closePreview()
261
- else if (e.key === 'ArrowLeft') prevImage()
262
- else if (e.key === 'ArrowRight') nextImage()
263
- else if (e.key === 'Enter') toggleZoom()
264
- else if (e.key === 'ArrowUp') zoomIn()
265
- else if (e.key === 'ArrowDown') zoomOut()
266
- }
267
-
268
- onMounted(() => {
269
- document.addEventListener('click', onDocClick, true)
270
- document.addEventListener('keydown', onKeydown)
271
- document.addEventListener('pointerdown', onPointerDownSwiper, true)
272
- })
273
-
274
- onBeforeUnmount(() => {
275
- document.removeEventListener('click', onDocClick, true)
276
- document.removeEventListener('keydown', onKeydown)
277
- document.removeEventListener('pointerdown', onPointerDownSwiper, true)
278
- })
279
- </script>
280
-
281
- <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>