@tnotesjs/core 0.0.5 → 0.0.7

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 (113) hide show
  1. package/README.md +84 -84
  2. package/dist/vitepress/config/index.js +4 -1
  3. package/package.json +1 -1
  4. package/types/config.ts +61 -61
  5. package/types/index.ts +11 -11
  6. package/types/note.ts +33 -33
  7. package/vitepress/assets/icons/icon__check.svg +3 -3
  8. package/vitepress/assets/icons/icon__clipboard.svg +7 -7
  9. package/vitepress/assets/icons/icon__close.svg +1 -1
  10. package/vitepress/assets/icons/icon__collapse.svg +1 -1
  11. package/vitepress/assets/icons/icon__confirm.svg +1 -1
  12. package/vitepress/assets/icons/icon__copy.svg +4 -4
  13. package/vitepress/assets/icons/icon__focus.svg +1 -1
  14. package/vitepress/assets/icons/icon__fold.svg +3 -3
  15. package/vitepress/assets/icons/icon__folder.svg +1 -1
  16. package/vitepress/assets/icons/icon__fullscreen.svg +1 -1
  17. package/vitepress/assets/icons/icon__fullscreen_exit.svg +1 -1
  18. package/vitepress/assets/icons/icon__github.svg +3 -3
  19. package/vitepress/assets/icons/icon__mindmap.svg +1 -1
  20. package/vitepress/assets/icons/icon__next.svg +1 -1
  21. package/vitepress/assets/icons/icon__number_gray.svg +1 -1
  22. package/vitepress/assets/icons/icon__number_purple.svg +1 -1
  23. package/vitepress/assets/icons/icon__prev.svg +1 -1
  24. package/vitepress/assets/icons/icon__restore.svg +1 -1
  25. package/vitepress/assets/icons/icon__rotate.svg +4 -4
  26. package/vitepress/assets/icons/icon__search.svg +1 -1
  27. package/vitepress/assets/icons/icon__sidebar_collapsed.svg +1 -1
  28. package/vitepress/assets/icons/icon__sidebar_opened.svg +1 -1
  29. package/vitepress/assets/icons/icon__totop.svg +5 -5
  30. package/vitepress/assets/icons/icon__vscode.svg +5 -5
  31. package/vitepress/assets/icons/icon__zoom_fit.svg +1 -1
  32. package/vitepress/assets/icons/icon__zoom_in.svg +1 -1
  33. package/vitepress/assets/icons/icon__zoom_out.svg +1 -1
  34. package/vitepress/assets/icons/icon__zoom_reset.svg +1 -1
  35. package/vitepress/assets/icons/index.ts +38 -38
  36. package/vitepress/components/BilibiliOutsidePlayer/BilibiliOutsidePlayer.vue +20 -20
  37. package/vitepress/components/CodeBlockFullscreen/CodeBlockFullscreen.vue +373 -373
  38. package/vitepress/components/CodeBlockFullscreen/index.ts +115 -115
  39. package/vitepress/components/CodeBlockFullscreen/styles.css +64 -64
  40. package/vitepress/components/Discussions/Discussions.module.scss +32 -32
  41. package/vitepress/components/Discussions/Discussions.vue +211 -211
  42. package/vitepress/components/EnWordList/EnWordList.module.scss +124 -124
  43. package/vitepress/components/EnWordList/EnWordList.vue +543 -543
  44. package/vitepress/components/EnWordList/RightClickMenu.module.scss +22 -22
  45. package/vitepress/components/EnWordList/RightClickMenu.vue +66 -66
  46. package/vitepress/components/Footprints/Footprints.module.scss +93 -93
  47. package/vitepress/components/Footprints/Footprints.vue +377 -377
  48. package/vitepress/components/Layout/AboutModal.module.scss +233 -233
  49. package/vitepress/components/Layout/AboutModal.vue +105 -105
  50. package/vitepress/components/Layout/AboutPanel.vue +266 -266
  51. package/vitepress/components/Layout/ContentCollapse.vue +603 -603
  52. package/vitepress/components/Layout/CustomSidebar.vue +605 -605
  53. package/vitepress/components/Layout/DocBeforeControls.vue +149 -139
  54. package/vitepress/components/Layout/DocFooter.vue +233 -233
  55. package/vitepress/components/Layout/ImagePreview.module.scss +201 -201
  56. package/vitepress/components/Layout/ImagePreview.vue +281 -281
  57. package/vitepress/components/Layout/Layout.module.scss +661 -661
  58. package/vitepress/components/Layout/Layout.vue +542 -542
  59. package/vitepress/components/Layout/NoteStatus.vue +140 -140
  60. package/vitepress/components/Layout/SidebarItems.vue +263 -263
  61. package/vitepress/components/Layout/SidebarNavBefore.vue +92 -92
  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 +34 -34
  65. package/vitepress/components/Layout/ToggleSidebar.module.scss +11 -11
  66. package/vitepress/components/Layout/ToggleSidebar.vue +35 -35
  67. package/vitepress/components/Layout/composables/useCollapseControl.ts +88 -88
  68. package/vitepress/components/Layout/composables/useNoteConfig.ts +121 -121
  69. package/vitepress/components/Layout/composables/useNoteSave.ts +173 -173
  70. package/vitepress/components/Layout/composables/useNoteValidation.ts +85 -85
  71. package/vitepress/components/Layout/composables/useRedirect.ts +110 -110
  72. package/vitepress/components/Layout/composables/useVSCodeIntegration.ts +85 -85
  73. package/vitepress/components/Layout/homeReadme.data.ts +124 -124
  74. package/vitepress/components/LoadingPage/LoadingPage.vue +192 -192
  75. package/vitepress/components/MarkMap/MarkMap.module.scss +159 -159
  76. package/vitepress/components/MarkMap/MarkMap.vue +404 -404
  77. package/vitepress/components/Mermaid/Mermaid.module.scss +275 -275
  78. package/vitepress/components/Mermaid/Mermaid.vue +364 -364
  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 +433 -433
  83. package/vitepress/components/Settings/Settings.vue +306 -306
  84. package/vitepress/components/SidebarCard/MindMapView.vue +483 -483
  85. package/vitepress/components/SidebarCard/NotesTrendChart.vue +108 -108
  86. package/vitepress/components/SidebarCard/SidebarCard.vue +948 -948
  87. package/vitepress/components/Tooltip/Tooltip.vue +70 -70
  88. package/vitepress/components/constants.ts +91 -91
  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 +160 -141
  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 +470 -470
  101. package/vitepress/plugins/getNoteByConfigIdPlugin.ts +107 -107
  102. package/vitepress/plugins/renameNotePlugin.ts +60 -60
  103. package/vitepress/plugins/updateConfigPlugin.ts +63 -63
  104. package/vitepress/theme/index.ts +95 -95
  105. package/vitepress/theme/styles/base.scss +50 -50
  106. package/vitepress/theme/styles/components/404.scss +31 -31
  107. package/vitepress/theme/styles/components/collapse.scss +175 -175
  108. package/vitepress/theme/styles/components/markmap.scss +101 -101
  109. package/vitepress/theme/styles/components/swiper.scss +255 -255
  110. package/vitepress/theme/styles/index.scss +25 -25
  111. package/vitepress/theme/styles/layout.scss +62 -62
  112. package/vitepress/theme/styles/utilities.scss +39 -39
  113. package/vitepress/theme/styles/vitepress-override.scss +25 -25
@@ -1,281 +1,281 @@
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
-
68
- 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>
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
+
68
+ 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>