@tnotesjs/core 0.1.23 → 0.1.24

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tnotesjs/core",
3
- "version": "0.1.23",
3
+ "version": "0.1.24",
4
4
  "description": "TNotes 知识库核心框架 —— 基于 VitePress 的笔记管理系统",
5
5
  "type": "module",
6
6
  "packageManager": "pnpm@10.17.1",
@@ -1,5 +1,6 @@
1
1
  <template>
2
2
  <div
3
+ v-if="isSidebarAvailable"
3
4
  class="sidebar-resize-handle"
4
5
  :class="{
5
6
  'is-hidden': hidden,
@@ -30,7 +31,8 @@
30
31
  </template>
31
32
 
32
33
  <script setup lang="ts">
33
- import { computed, onBeforeUnmount, onMounted, ref } from 'vue'
34
+ import { computed, nextTick, onBeforeUnmount, onMounted, ref, watch } from 'vue'
35
+ import { useRoute } from 'vitepress'
34
36
 
35
37
  import { useSidebarLayout } from './composables/useSidebarLayout'
36
38
  import { icon__next, icon__prev } from '../../assets/icons'
@@ -46,9 +48,12 @@ const {
46
48
  saveSidebarWidth,
47
49
  } = useSidebarLayout()
48
50
 
51
+ const route = useRoute()
49
52
  const isDragging = ref(false)
50
53
  const isContentFullscreen = ref(false)
54
+ const isSidebarAvailable = ref(false)
51
55
  const shortcutText = ref('Ctrl + Alt + ,')
56
+ const WIDE_LAYOUT_MIN_WIDTH = 1440
52
57
  let fullscreenObserver: MutationObserver | null = null
53
58
 
54
59
  const toggleIcon = computed(() => (hidden.value ? icon__next : icon__prev))
@@ -62,6 +67,7 @@ const toggleTitle = computed(
62
67
  onMounted(() => {
63
68
  initSidebarLayout()
64
69
  shortcutText.value = getShortcutText()
70
+ updateSidebarAvailability()
65
71
  updateContentFullscreen()
66
72
  fullscreenObserver = new MutationObserver(updateContentFullscreen)
67
73
  fullscreenObserver.observe(document.documentElement, {
@@ -85,13 +91,13 @@ function startResize(event: MouseEvent) {
85
91
  document.body.classList.add('is-sidebar-resizing')
86
92
  window.addEventListener('mousemove', resize)
87
93
  window.addEventListener('mouseup', stopResize)
88
- setSidebarWidth(event.clientX)
94
+ setSidebarWidth(getSidebarWidthFromClientX(event.clientX))
89
95
  }
90
96
 
91
97
  function resize(event: MouseEvent) {
92
98
  if (!isDragging.value) return
93
99
 
94
- setSidebarWidth(event.clientX)
100
+ setSidebarWidth(getSidebarWidthFromClientX(event.clientX))
95
101
  }
96
102
 
97
103
  function stopResize() {
@@ -105,17 +111,60 @@ function stopResize() {
105
111
  }
106
112
 
107
113
  function handleShortcut(event: KeyboardEvent) {
108
- if (!isToggleShortcut(event) || isEditableTarget(event.target)) return
114
+ if (
115
+ !isSidebarAvailable.value ||
116
+ !isToggleShortcut(event) ||
117
+ isEditableTarget(event.target)
118
+ ) {
119
+ return
120
+ }
109
121
 
110
122
  event.preventDefault()
111
123
  toggleSidebar()
112
124
  }
113
125
 
126
+ watch(
127
+ () => route.path,
128
+ async () => {
129
+ await nextTick()
130
+ updateSidebarAvailability()
131
+ updateContentFullscreen()
132
+ },
133
+ )
134
+
135
+ function updateSidebarAvailability() {
136
+ if (typeof document === 'undefined') return
137
+
138
+ isSidebarAvailable.value = Boolean(
139
+ document.querySelector('.VPContent.has-sidebar') &&
140
+ document.querySelector('.VPSidebar'),
141
+ )
142
+ }
143
+
114
144
  function updateContentFullscreen() {
115
145
  isContentFullscreen.value =
116
146
  document.documentElement.classList.contains('content-fullscreen')
117
147
  }
118
148
 
149
+ function getSidebarWidthFromClientX(clientX: number): number {
150
+ return clientX - getSidebarLayoutOffset()
151
+ }
152
+
153
+ function getSidebarLayoutOffset(): number {
154
+ if (typeof window === 'undefined' || typeof document === 'undefined') return 0
155
+ if (window.innerWidth < WIDE_LAYOUT_MIN_WIDTH) return 0
156
+
157
+ const layoutMaxWidth = Number.parseFloat(
158
+ window
159
+ .getComputedStyle(document.documentElement)
160
+ .getPropertyValue('--vp-layout-max-width'),
161
+ )
162
+
163
+ if (!Number.isFinite(layoutMaxWidth)) return 0
164
+
165
+ return Math.max(0, (window.innerWidth - layoutMaxWidth) / 2)
166
+ }
167
+
119
168
  function isToggleShortcut(event: KeyboardEvent): boolean {
120
169
  return (
121
170
  (event.ctrlKey || event.metaKey) && event.altKey && event.code === 'Comma'
@@ -308,4 +357,12 @@ function isEditableTarget(target: EventTarget | null): boolean {
308
357
  display: none;
309
358
  }
310
359
  }
360
+
361
+ @media (min-width: 1440px) {
362
+ .sidebar-resize-handle {
363
+ left: calc(
364
+ (100vw - var(--vp-layout-max-width)) / 2 + var(--tn-sidebar-width, 260px) - 5px
365
+ );
366
+ }
367
+ }
311
368
  </style>
@@ -11,12 +11,9 @@
11
11
  --vp-sidebar-width: var(--tn-sidebar-width);
12
12
  }
13
13
 
14
- // 侧边栏样式重写
14
+ // 保留 VitePress 对侧边栏宽屏居中的 left/width/padding-left 计算。
15
15
  .VPSidebar {
16
- width: var(--tn-sidebar-layout-width, var(--tn-sidebar-width)) !important;
17
16
  padding-top: var(--vp-nav-height) !important;
18
- padding-left: 1rem !important;
19
- padding-right: 1rem !important;
20
17
  padding-bottom: 1rem !important;
21
18
  }
22
19
 
@@ -33,16 +30,20 @@
33
30
  transition: transform 0.25s ease, opacity 0.25s ease;
34
31
  }
35
32
 
36
- .hide-sidebar .VPNavBarTitle {
33
+ .hide-sidebar .VPNavBar.has-sidebar .VPNavBarTitle {
37
34
  display: none !important;
38
35
  }
39
36
 
40
- .hide-sidebar .VPNavBar {
37
+ .hide-sidebar .VPNavBar.has-sidebar {
41
38
  background-color: var(--vp-nav-bg-color) !important;
42
39
  }
43
40
 
41
+ .hide-sidebar .VPNavBar.has-sidebar .divider {
42
+ padding-left: 0 !important;
43
+ }
44
+
44
45
  /* 主体内容往左补齐,与导航栏标题左对齐 */
45
- .hide-sidebar .VPContent {
46
+ .hide-sidebar .VPContent.has-sidebar {
46
47
  margin-left: 0 !important;
47
48
  padding-left: calc((100vw - var(--vp-layout-max-width)) / 2) !important;
48
49
  }