Commit e23a78cbf6

e23a78cbf64a7a2d96a0f4efb76797b7d5967c56

parent: afea2abb81

Unsigned

Duck <57977673+woducku@users.noreply.github.com> · 2026-06-26 17:17 UTC

Add search history

Layout: unified · split

docs/.vitepress/theme/components/Search.vue +314 −62
@@ -282,6 +282,57 @@ function cycleSearchMode() {
282const matchExact = computed(() => searchMode.value === 'exact') 282const matchExact = computed(() => searchMode.value === 'exact')
283const urlSearchMode = computed(() => searchMode.value === 'url') 283const urlSearchMode = computed(() => searchMode.value === 'url')
284 284
285// Search history
286interface SearchHistoryEntry {
287 query: string
288 mode: SearchMode
289 path?: string[]
290 href?: string
291}
292
293const MAX_HISTORY = 12
294
295const saveHistoryEnabled = useLocalStorage('vitepress:local-search-save-history', false)
296
297const searchHistory = useLocalStorage<SearchHistoryEntry[]>(
298 'vitepress:local-search-history',
299 []
300)
301
302const showHistory = computed(
303 () => !filterText.value && searchHistory.value.length > 0
304)
305
306function saveToHistory(query: string, mode: SearchMode, path?: string[], href?: string) {
307 const trimmed = query.trim()
308 if (!trimmed || !saveHistoryEnabled.value) return
309 const pathKey = (path ?? []).join('\n')
310 searchHistory.value = [
311 { query: trimmed, mode, path, href },
312 ...searchHistory.value.filter(
313 (h) => !(h.query === trimmed && h.mode === mode && (h.path ?? []).join('\n') === pathKey)
314 )
315 ].slice(0, MAX_HISTORY)
316}
317
318function removeFromHistory(index: number) {
319 searchHistory.value = searchHistory.value.filter((_, i) => i !== index)
320}
321
322function applyHistory(entry: SearchHistoryEntry) {
323 if (entry.href) {
324 window.dispatchEvent(
325 new CustomEvent('search-nav', { detail: { query: entry.query } })
326 )
327 router.go(entry.href)
328 showSearch.value = false
329 return
330 }
331 searchMode.value = entry.mode
332 filterText.value = entry.query
333 nextTick(() => focusSearchInput(false))
334}
335
285// Mobile settings popup 336// Mobile settings popup
286const showSettingsPopup = ref(false) 337const showSettingsPopup = ref(false)
287const settingsButtonRef = ref<HTMLButtonElement>() 338const settingsButtonRef = ref<HTMLButtonElement>()
@@ -1506,6 +1557,7 @@ function getSearchResultHref(item: SearchResult & Result) {
1506} 1557}
1507 1558
1508function navigateToUrlResult(item: UrlResult) { 1559function navigateToUrlResult(item: UrlResult) {
1560 saveToHistory(filterText.value, searchMode.value, [getPageLabel(item.pageId), ...item.titles, item.linkText].filter(Boolean), buildResultHref(item.pageId, item.tabs, item.anchor))
1509 window.dispatchEvent( 1561 window.dispatchEvent(
1510 new CustomEvent('search-nav', { detail: { query: filterText.value } }) 1562 new CustomEvent('search-nav', { detail: { query: filterText.value } })
1511 ) 1563 )
@@ -1545,6 +1597,7 @@ onKeyStroke('Enter', (e) => {
1545 } 1597 }
1546 1598
1547 if (selectedPackage) { 1599 if (selectedPackage) {
1600 saveToHistory(filterText.value, searchMode.value, [getPageLabel(getPageKey(String(selectedPackage.id))), ...selectedPackage.titles, selectedPackage.title].filter(Boolean), getSearchResultHref(selectedPackage))
1548 window.dispatchEvent( 1601 window.dispatchEvent(
1549 new CustomEvent('search-nav', { 1602 new CustomEvent('search-nav', {
1550 detail: { query: filterText.value } 1603 detail: { query: filterText.value }
@@ -1555,7 +1608,8 @@ onKeyStroke('Enter', (e) => {
1555 } 1608 }
1556}) 1609})
1557 1610
1558function onResultClick() { 1611function onResultClick(item: SearchResult & Result) {
1612 saveToHistory(filterText.value, searchMode.value, [getPageLabel(getPageKey(String(item.id))), ...item.titles, item.title].filter(Boolean), getSearchResultHref(item))
1559 window.dispatchEvent( 1613 window.dispatchEvent(
1560 new CustomEvent('search-nav', { 1614 new CustomEvent('search-nav', {
1561 detail: { query: filterText.value } 1615 detail: { query: filterText.value }
@@ -1933,9 +1987,9 @@ function onMouseMove(e: MouseEvent) {
1933 <AnimatePresence> 1987 <AnimatePresence>
1934 <component 1988 <component
1935 :is="searchMotionDiv" 1989 :is="searchMotionDiv"
1936 v-if="urlSearchMode 1990 v-if="!showHistory && (urlSearchMode
1937 ? (!filterText || !urlMatches.length) 1991 ? (!filterText || !urlMatches.length)
1938 : (!filterText || !filteredResults.length)" 1992 : (!filterText || !filteredResults.length))"
1939 class="flex flex-1 flex-col justify-center items-center w-full min-h-full gap-2 font-medium text-sm text-center text-gray-500 dark:text-gray-300 m-auto opacity-90" 1993 class="flex flex-1 flex-col justify-center items-center w-full min-h-full gap-2 font-medium text-sm text-center text-gray-500 dark:text-gray-300 m-auto opacity-90"
1940 v-bind="emptyStateMotion" 1994 v-bind="emptyStateMotion"
1941 > 1995 >
@@ -1949,6 +2003,60 @@ function onMouseMove(e: MouseEvent) {
1949 </component> 2003 </component>
1950 </AnimatePresence> 2004 </AnimatePresence>
1951 2005
2006 <!-- Search history -->
2007 <AnimatePresence>
2008 <component
2009 :is="searchMotionDiv"
2010 v-if="showHistory"
2011 class="history-list"
2012 v-bind="emptyStateMotion"
2013 >
2014 <div class="history-header">
2015 <span>Recent searches</span>
2016 <button
2017 class="history-clear-all"
2018 title="Clear all history"
2019 @click.stop="searchHistory = []"
2020 >
2021 <X :size="14" stroke-width="1.5" />
2022 </button>
2023 </div>
2024 <ul class="history-items">
2025 <li
2026 v-for="(entry, index) in searchHistory"
2027 :key="index"
2028 class="history-item"
2029 @click="applyHistory(entry)"
2030 >
2031 <div class="history-main">
2032 <span class="history-query">{{ entry.query }}</span>
2033 <template v-if="entry.path?.length">
2034 <ArrowRight :size="13" stroke-width="1.5" class="history-arrow" />
2035 <template v-if="entry.path.length > 1">
2036 <span class="history-path-start">{{ entry.path.slice(0, -1).join(' › ') }}</span>
2037 <span class="history-path-sep">›</span>
2038 </template>
2039 <span class="history-path-end" :class="{ 'history-path-only': entry.path.length === 1 }">{{ entry.path[entry.path.length - 1] }}</span>
2040 </template>
2041 </div>
2042 <component
2043 :is="entry.mode === 'exact' ? Regex : entry.mode === 'fuzzy' ? LocateOff : Globe"
2044 :size="14"
2045 stroke-width="1.25"
2046 class="history-mode-icon"
2047 />
2048 <button
2049 class="history-remove"
2050 :title="'Remove \'' + entry.query + '\' from history'"
2051 @click.stop="removeFromHistory(index)"
2052 >
2053 <X :size="14" stroke-width="1.5" />
2054 </button>
2055 </li>
2056 </ul>
2057 </component>
2058 </AnimatePresence>
2059
1952 <!-- URL search results --> 2060 <!-- URL search results -->
1953 <template v-if="urlSearchMode"> 2061 <template v-if="urlSearchMode">
1954 <AnimatePresence> 2062 <AnimatePresence>
@@ -2047,7 +2155,7 @@ function onMouseMove(e: MouseEvent) {
2047 @mouseenter="!disableMouseOver && 2155 @mouseenter="!disableMouseOver &&
2048 (selectedIndex = index + 1)" 2156 (selectedIndex = index + 1)"
2049 @focusin="selectedIndex = index + 1" 2157 @focusin="selectedIndex = index + 1"
2050 @click="onResultClick" 2158 @click="onResultClick(p)"
2051 :data-index="index + 1" 2159 :data-index="index + 1"
2052 > 2160 >
2053 <div> 2161 <div>
@@ -2238,7 +2346,7 @@ function onMouseMove(e: MouseEvent) {
2238 :style="settingsPopupStyle" 2346 :style="settingsPopupStyle"
2239 > 2347 >
2240 <div class="settings-section"> 2348 <div class="settings-section">
2241 <div class="settings-section-label">Search Mode</div> 2349 <div class="settings-section-label">Search</div>
2242 <div class="settings-options"> 2350 <div class="settings-options">
2243 <button 2351 <button
2244 type="button" 2352 type="button"
@@ -2246,7 +2354,6 @@ function onMouseMove(e: MouseEvent) {
2246 :class="{ active: searchMode === 'exact' }" 2354 :class="{ active: searchMode === 'exact' }"
2247 @click="searchMode = 'exact'" 2355 @click="searchMode = 'exact'"
2248 > 2356 >
2249 <Regex :size="18" stroke-width="1.25" />
2250 <span>Exact</span> 2357 <span>Exact</span>
2251 </button> 2358 </button>
2252 <button 2359 <button
@@ -2255,7 +2362,6 @@ function onMouseMove(e: MouseEvent) {
2255 :class="{ active: searchMode === 'fuzzy' }" 2362 :class="{ active: searchMode === 'fuzzy' }"
2256 @click="searchMode = 'fuzzy'" 2363 @click="searchMode = 'fuzzy'"
2257 > 2364 >
2258 <LocateOff :size="18" stroke-width="1.25" />
2259 <span>Fuzzy</span> 2365 <span>Fuzzy</span>
2260 </button> 2366 </button>
2261 <button 2367 <button
@@ -2264,7 +2370,6 @@ function onMouseMove(e: MouseEvent) {
2264 :class="{ active: searchMode === 'url' }" 2370 :class="{ active: searchMode === 'url' }"
2265 @click="searchMode = 'url'" 2371 @click="searchMode = 'url'"
2266 > 2372 >
2267 <Globe :size="18" stroke-width="1.25" />
2268 <span>URL</span> 2373 <span>URL</span>
2269 </button> 2374 </button>
2270 </div> 2375 </div>
@@ -2273,7 +2378,7 @@ function onMouseMove(e: MouseEvent) {
2273 v-if="!disableDetailedView && searchMode !== 'url'" 2378 v-if="!disableDetailedView && searchMode !== 'url'"
2274 class="settings-section" 2379 class="settings-section"
2275 > 2380 >
2276 <div class="settings-section-label">View Mode</div> 2381 <div class="settings-section-label">View</div>
2277 <div class="settings-options"> 2382 <div class="settings-options">
2278 <button 2383 <button
2279 type="button" 2384 type="button"
@@ -2281,7 +2386,6 @@ function onMouseMove(e: MouseEvent) {
2281 :class="{ active: showDetailedList }" 2386 :class="{ active: showDetailedList }"
2282 @click="showDetailedList = true" 2387 @click="showDetailedList = true"
2283 > 2388 >
2284 <TextAlignStart :size="18" stroke-width="1.25" />
2285 <span>Detail</span> 2389 <span>Detail</span>
2286 </button> 2390 </button>
2287 <button 2391 <button
@@ -2290,11 +2394,31 @@ function onMouseMove(e: MouseEvent) {
2290 :class="{ active: !showDetailedList }" 2394 :class="{ active: !showDetailedList }"
2291 @click="showDetailedList = false" 2395 @click="showDetailedList = false"
2292 > 2396 >
2293 <List :size="18" stroke-width="1.25" />
2294 <span>List</span> 2397 <span>List</span>
2295 </button> 2398 </button>
2296 </div> 2399 </div>
2297 </div> 2400 </div>
2401 <div class="settings-section">
2402 <div class="settings-section-label">History</div>
2403 <div class="settings-options">
2404 <button
2405 type="button"
2406 class="settings-option"
2407 :class="{ active: saveHistoryEnabled }"
2408 @click="saveHistoryEnabled = true"
2409 >
2410 <span>On</span>
2411 </button>
2412 <button
2413 type="button"
2414 class="settings-option"
2415 :class="{ active: !saveHistoryEnabled }"
2416 @click="saveHistoryEnabled = false"
2417 >
2418 <span>Off</span>
2419 </button>
2420 </div>
2421 </div>
2298 </div> 2422 </div>
2299 </Transition> 2423 </Transition>
2300 </Teleport> 2424 </Teleport>
@@ -2336,9 +2460,9 @@ function onMouseMove(e: MouseEvent) {
2336 flex-direction: column; 2460 flex-direction: column;
2337 gap: 16px; 2461 gap: 16px;
2338 background: var(--vp-local-search-bg); 2462 background: var(--vp-local-search-bg);
2339 width: min(100vw - 60px, 900px); 2463 width: min(100vw - 60px, 700px);
2340 height: 100%; 2464 height: 100%;
2341 max-height: min(100vh - 128px, 900px); 2465 max-height: min(100vh - 128px, 680px);
2342 border-radius: 6px; 2466 border-radius: 6px;
2343} 2467}
2344 2468
@@ -2882,19 +3006,7 @@ svg {
2882 3006
2883.search-mode-group, 3007.search-mode-group,
2884.view-group { 3008.view-group {
2885 display: flex; 3009 display: none;
2886 align-items: center;
2887 border: 1px solid var(--vp-c-divider);
2888 border-radius: 5px;
2889 overflow: hidden;
2890}
2891
2892/* Mobile: hide toolbar groups entirely — settings popup handles them */
2893@media (max-width: 767px) {
2894 .search-mode-group,
2895 .view-group {
2896 display: none;
2897 }
2898} 3010}
2899 3011
2900.mode-btn { 3012.mode-btn {
@@ -3011,34 +3123,27 @@ svg {
3011 background: transparent; 3123 background: transparent;
3012} 3124}
3013 3125
3014/* Settings toggle button — mobile only */ 3126/* Settings toggle button — always visible */
3015.settings-toggle-btn { 3127.settings-toggle-btn {
3016 display: none; 3128 display: flex;
3017} 3129 border: none;
3018 3130 border-radius: 5px;
3019@media (max-width: 767px) { 3131 color: var(--vp-c-text-1);
3020 .settings-toggle-btn { 3132 padding: 8px;
3021 display: flex;
3022 border: none;
3023 border-radius: 5px;
3024 color: var(--vp-c-text-1);
3025 padding: 8px;
3026 }
3027} 3133}
3028 3134
3029/* Settings popup card */ 3135/* Settings popup card */
3030.search-settings-popup { 3136.search-settings-popup {
3031 position: fixed; 3137 position: fixed;
3032 z-index: 200; 3138 z-index: 200;
3033 background: var(--vp-c-bg-elv); 3139 background: var(--vp-c-bg);
3034 border: 1px solid var(--vp-c-divider); 3140 border: 1px solid var(--vp-c-divider);
3035 border-radius: 12px; 3141 border-radius: 10px;
3036 padding: 12px; 3142 padding: 10px;
3037 box-shadow: 0 8px 32px rgba(0, 0, 0, 0.18); 3143 box-shadow: 0 4px 20px rgba(0, 0, 0, 0.25);
3038 display: flex; 3144 display: flex;
3039 flex-direction: column; 3145 flex-direction: column;
3040 gap: 10px; 3146 min-width: 196px;
3041 min-width: 175px;
3042} 3147}
3043 3148
3044.settings-section { 3149.settings-section {
@@ -3047,50 +3152,52 @@ svg {
3047 gap: 6px; 3152 gap: 6px;
3048} 3153}
3049 3154
3155.settings-section + .settings-section {
3156 border-top: 1px solid var(--vp-c-divider);
3157 margin-top: 8px;
3158 padding-top: 8px;
3159}
3160
3050.settings-section-label { 3161.settings-section-label {
3051 font-size: 0.7rem; 3162 font-size: 11px;
3052 font-weight: 600; 3163 font-weight: 600;
3053 text-transform: uppercase; 3164 text-transform: uppercase;
3054 letter-spacing: 0.06em; 3165 letter-spacing: 0.06em;
3055 color: var(--vp-c-text-3); 3166 color: var(--vp-c-text-3, var(--vp-c-text-2));
3056 padding: 0 4px; 3167 margin-bottom: 2px;
3057} 3168}
3058 3169
3059.settings-options { 3170.settings-options {
3060 display: flex; 3171 display: flex;
3061 flex-direction: column; 3172 gap: 4px;
3062 border: 1px solid var(--vp-c-divider);
3063 border-radius: 8px;
3064 overflow: hidden;
3065} 3173}
3066 3174
3067.settings-option { 3175.settings-option {
3068 display: flex; 3176 display: flex;
3069 align-items: center; 3177 align-items: center;
3070 gap: 8px; 3178 justify-content: center;
3071 padding: 9px 12px; 3179 gap: 5px;
3180 padding: 5px 6px;
3072 background: transparent; 3181 background: transparent;
3073 border: none; 3182 border: none;
3183 border-radius: 5px;
3074 cursor: pointer; 3184 cursor: pointer;
3075 color: var(--vp-c-text-2); 3185 color: var(--vp-c-text-2);
3076 font-size: 0.88rem; 3186 font-size: 14px;
3077 text-align: left; 3187 font-weight: 500;
3078 transition: background-color 0.15s, color 0.15s; 3188 transition: background-color 0.15s, color 0.15s;
3079 width: 100%; 3189 flex: 1;
3080}
3081
3082.settings-option + .settings-option {
3083 border-top: 1px solid var(--vp-c-divider);
3084} 3190}
3085 3191
3086.settings-option:hover { 3192.settings-option:hover {
3087 background: var(--vp-c-default-soft);
3088 color: var(--vp-c-text-1); 3193 color: var(--vp-c-text-1);
3194 background: var(--vp-c-bg-soft);
3089} 3195}
3090 3196
3091.settings-option.active { 3197.settings-option.active {
3092 color: var(--vp-c-brand-1); 3198 color: var(--vp-c-brand-1);
3093 background: color-mix(in srgb, var(--vp-c-brand-1) 10%, transparent); 3199 background: var(--vp-c-brand-soft);
3200 font-weight: 600;
3094} 3201}
3095 3202
3096.settings-popup-enter-active, 3203.settings-popup-enter-active,
@@ -3104,4 +3211,149 @@ svg {
3104 opacity: 0; 3211 opacity: 0;
3105 transform: scale(0.92) translateY(-4px); 3212 transform: scale(0.92) translateY(-4px);
3106} 3213}
3214
3215.history-list {
3216 display: flex;
3217 flex-direction: column;
3218 width: 100%;
3219 padding: 4px 0;
3220}
3221
3222.history-header {
3223 font-size: 0.7rem;
3224 font-weight: 600;
3225 text-transform: uppercase;
3226 letter-spacing: 0.06em;
3227 color: var(--vp-c-text-3);
3228 padding: 4px 12px 8px;
3229 display: flex;
3230 align-items: center;
3231 justify-content: space-between;
3232}
3233
3234.history-clear-all {
3235 display: flex;
3236 align-items: center;
3237 justify-content: center;
3238 flex-shrink: 0;
3239 padding: 0;
3240 border: none;
3241 background: transparent;
3242 color: var(--vp-c-text-3);
3243 cursor: pointer;
3244 transition: color 0.15s;
3245}
3246
3247.history-clear-all:hover {
3248 color: var(--vp-c-text-1);
3249}
3250
3251.history-items {
3252 display: flex;
3253 flex-direction: column;
3254 list-style: none;
3255 margin: 0;
3256 padding: 0;
3257}
3258
3259.history-item {
3260 display: flex;
3261 align-items: center;
3262 gap: 8px;
3263 padding: 8px 12px;
3264 cursor: pointer;
3265 border-radius: 4px;
3266 transition: background-color 0.15s;
3267}
3268
3269.history-item:hover {
3270 background: var(--vp-c-default-soft);
3271}
3272
3273.history-main {
3274 flex: 1;
3275 min-width: 0;
3276 display: flex;
3277 align-items: center;
3278 gap: 6px;
3279 overflow: hidden;
3280}
3281
3282.history-query {
3283 flex-shrink: 0;
3284 font-size: 0.9rem;
3285 color: var(--vp-c-text-1);
3286 white-space: nowrap;
3287}
3288
3289.history-arrow {
3290 flex-shrink: 0;
3291 opacity: 0.5;
3292 color: var(--vp-c-text-3);
3293}
3294
3295.history-path-start {
3296 min-width: 0;
3297 overflow: hidden;
3298 text-overflow: ellipsis;
3299 white-space: nowrap;
3300 font-size: 0.78rem;
3301 font-weight: 500;
3302 color: var(--vp-c-text-3);
3303}
3304
3305.history-path-sep {
3306 flex-shrink: 0;
3307 font-size: 0.78rem;
3308 color: var(--vp-c-text-3);
3309}
3310
3311.history-path-end {
3312 flex-shrink: 0;
3313 white-space: nowrap;
3314 font-size: 0.78rem;
3315 font-weight: 500;
3316 color: var(--vp-c-text-3);
3317}
3318
3319.history-path-only {
3320 flex-shrink: 1;
3321 min-width: 0;
3322 overflow: hidden;
3323 text-overflow: ellipsis;
3324}
3325
3326.history-mode-icon {
3327 flex-shrink: 0;
3328 color: var(--vp-c-text-3);
3329 opacity: 0.7;
3330}
3331
3332.history-remove {
3333 display: flex;
3334 align-items: center;
3335 justify-content: center;
3336 flex-shrink: 0;
3337 padding: 0;
3338 border: none;
3339 background: transparent;
3340 color: var(--vp-c-text-3);
3341 cursor: pointer;
3342 opacity: 0;
3343 transition: opacity 0.15s, color 0.15s;
3344}
3345
3346.history-item:hover .history-remove {
3347 opacity: 1;
3348}
3349
3350.history-remove:hover {
3351 color: var(--vp-c-text-1);
3352}
3353
3354@media (any-pointer: coarse) {
3355 .history-remove {
3356 opacity: 1;
3357 }
3358}
3107</style>\ No newline at end of file 3359</style>\ No newline at end of file