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() {
282282const matchExact = computed(() => searchMode.value === 'exact')
283283const urlSearchMode = computed(() => searchMode.value === 'url')
284284
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
285336// Mobile settings popup
286337const showSettingsPopup = ref(false)
287338const settingsButtonRef = ref<HTMLButtonElement>()
@@ -1506,6 +1557,7 @@ function getSearchResultHref(item: SearchResult & Result) {
15061557}
15071558
15081559function 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))
15091561 window.dispatchEvent(
15101562 new CustomEvent('search-nav', { detail: { query: filterText.value } })
15111563 )
@@ -1545,6 +1597,7 @@ onKeyStroke('Enter', (e) => {
15451597 }
15461598
15471599 if (selectedPackage) {
1600 saveToHistory(filterText.value, searchMode.value, [getPageLabel(getPageKey(String(selectedPackage.id))), ...selectedPackage.titles, selectedPackage.title].filter(Boolean), getSearchResultHref(selectedPackage))
15481601 window.dispatchEvent(
15491602 new CustomEvent('search-nav', {
15501603 detail: { query: filterText.value }
@@ -1555,7 +1608,8 @@ onKeyStroke('Enter', (e) => {
15551608 }
15561609})
15571610
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))
15591613 window.dispatchEvent(
15601614 new CustomEvent('search-nav', {
15611615 detail: { query: filterText.value }
@@ -1933,9 +1987,9 @@ function onMouseMove(e: MouseEvent) {
19331987 <AnimatePresence>
19341988 <component
19351989 :is="searchMotionDiv"
1936 v-if="urlSearchMode
1990 v-if="!showHistory && (urlSearchMode
19371991 ? (!filterText || !urlMatches.length)
1938 : (!filterText || !filteredResults.length)"
1992 : (!filterText || !filteredResults.length))"
19391993 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"
19401994 v-bind="emptyStateMotion"
19411995 >
@@ -1949,6 +2003,60 @@ function onMouseMove(e: MouseEvent) {
19492003 </component>
19502004 </AnimatePresence>
19512005
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
19522060 <!-- URL search results -->
19532061 <template v-if="urlSearchMode">
19542062 <AnimatePresence>
@@ -2047,7 +2155,7 @@ function onMouseMove(e: MouseEvent) {
20472155 @mouseenter="!disableMouseOver &&
20482156 (selectedIndex = index + 1)"
20492157 @focusin="selectedIndex = index + 1"
2050 @click="onResultClick"
2158 @click="onResultClick(p)"
20512159 :data-index="index + 1"
20522160 >
20532161 <div>
@@ -2238,7 +2346,7 @@ function onMouseMove(e: MouseEvent) {
22382346 :style="settingsPopupStyle"
22392347 >
22402348 <div class="settings-section">
2241 <div class="settings-section-label">Search Mode</div>
2349 <div class="settings-section-label">Search</div>
22422350 <div class="settings-options">
22432351 <button
22442352 type="button"
@@ -2246,7 +2354,6 @@ function onMouseMove(e: MouseEvent) {
22462354 :class="{ active: searchMode === 'exact' }"
22472355 @click="searchMode = 'exact'"
22482356 >
2249 <Regex :size="18" stroke-width="1.25" />
22502357 <span>Exact</span>
22512358 </button>
22522359 <button
@@ -2255,7 +2362,6 @@ function onMouseMove(e: MouseEvent) {
22552362 :class="{ active: searchMode === 'fuzzy' }"
22562363 @click="searchMode = 'fuzzy'"
22572364 >
2258 <LocateOff :size="18" stroke-width="1.25" />
22592365 <span>Fuzzy</span>
22602366 </button>
22612367 <button
@@ -2264,7 +2370,6 @@ function onMouseMove(e: MouseEvent) {
22642370 :class="{ active: searchMode === 'url' }"
22652371 @click="searchMode = 'url'"
22662372 >
2267 <Globe :size="18" stroke-width="1.25" />
22682373 <span>URL</span>
22692374 </button>
22702375 </div>
@@ -2273,7 +2378,7 @@ function onMouseMove(e: MouseEvent) {
22732378 v-if="!disableDetailedView && searchMode !== 'url'"
22742379 class="settings-section"
22752380 >
2276 <div class="settings-section-label">View Mode</div>
2381 <div class="settings-section-label">View</div>
22772382 <div class="settings-options">
22782383 <button
22792384 type="button"
@@ -2281,7 +2386,6 @@ function onMouseMove(e: MouseEvent) {
22812386 :class="{ active: showDetailedList }"
22822387 @click="showDetailedList = true"
22832388 >
2284 <TextAlignStart :size="18" stroke-width="1.25" />
22852389 <span>Detail</span>
22862390 </button>
22872391 <button
@@ -2290,11 +2394,31 @@ function onMouseMove(e: MouseEvent) {
22902394 :class="{ active: !showDetailedList }"
22912395 @click="showDetailedList = false"
22922396 >
2293 <List :size="18" stroke-width="1.25" />
22942397 <span>List</span>
22952398 </button>
22962399 </div>
22972400 </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>
22982422 </div>
22992423 </Transition>
23002424 </Teleport>
@@ -2336,9 +2460,9 @@ function onMouseMove(e: MouseEvent) {
23362460 flex-direction: column;
23372461 gap: 16px;
23382462 background: var(--vp-local-search-bg);
2339 width: min(100vw - 60px, 900px);
2463 width: min(100vw - 60px, 700px);
23402464 height: 100%;
2341 max-height: min(100vh - 128px, 900px);
2465 max-height: min(100vh - 128px, 680px);
23422466 border-radius: 6px;
23432467}
23442468
@@ -2882,19 +3006,7 @@ svg {
28823006
28833007.search-mode-group,
28843008.view-group {
2885 display: flex;
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 }
3009 display: none;
28983010}
28993011
29003012.mode-btn {
@@ -3011,34 +3123,27 @@ svg {
30113123 background: transparent;
30123124}
30133125
3014/* Settings toggle button — mobile only */
3126/* Settings toggle button — always visible */
30153127.settings-toggle-btn {
3016 display: none;
3017}
3018
3019@media (max-width: 767px) {
3020 .settings-toggle-btn {
3021 display: flex;
3022 border: none;
3023 border-radius: 5px;
3024 color: var(--vp-c-text-1);
3025 padding: 8px;
3026 }
3128 display: flex;
3129 border: none;
3130 border-radius: 5px;
3131 color: var(--vp-c-text-1);
3132 padding: 8px;
30273133}
30283134
30293135/* Settings popup card */
30303136.search-settings-popup {
30313137 position: fixed;
30323138 z-index: 200;
3033 background: var(--vp-c-bg-elv);
3139 background: var(--vp-c-bg);
30343140 border: 1px solid var(--vp-c-divider);
3035 border-radius: 12px;
3036 padding: 12px;
3037 box-shadow: 0 8px 32px rgba(0, 0, 0, 0.18);
3141 border-radius: 10px;
3142 padding: 10px;
3143 box-shadow: 0 4px 20px rgba(0, 0, 0, 0.25);
30383144 display: flex;
30393145 flex-direction: column;
3040 gap: 10px;
3041 min-width: 175px;
3146 min-width: 196px;
30423147}
30433148
30443149.settings-section {
@@ -3047,50 +3152,52 @@ svg {
30473152 gap: 6px;
30483153}
30493154
3155.settings-section + .settings-section {
3156 border-top: 1px solid var(--vp-c-divider);
3157 margin-top: 8px;
3158 padding-top: 8px;
3159}
3160
30503161.settings-section-label {
3051 font-size: 0.7rem;
3162 font-size: 11px;
30523163 font-weight: 600;
30533164 text-transform: uppercase;
30543165 letter-spacing: 0.06em;
3055 color: var(--vp-c-text-3);
3056 padding: 0 4px;
3166 color: var(--vp-c-text-3, var(--vp-c-text-2));
3167 margin-bottom: 2px;
30573168}
30583169
30593170.settings-options {
30603171 display: flex;
3061 flex-direction: column;
3062 border: 1px solid var(--vp-c-divider);
3063 border-radius: 8px;
3064 overflow: hidden;
3172 gap: 4px;
30653173}
30663174
30673175.settings-option {
30683176 display: flex;
30693177 align-items: center;
3070 gap: 8px;
3071 padding: 9px 12px;
3178 justify-content: center;
3179 gap: 5px;
3180 padding: 5px 6px;
30723181 background: transparent;
30733182 border: none;
3183 border-radius: 5px;
30743184 cursor: pointer;
30753185 color: var(--vp-c-text-2);
3076 font-size: 0.88rem;
3077 text-align: left;
3186 font-size: 14px;
3187 font-weight: 500;
30783188 transition: background-color 0.15s, color 0.15s;
3079 width: 100%;
3080}
3081
3082.settings-option + .settings-option {
3083 border-top: 1px solid var(--vp-c-divider);
3189 flex: 1;
30843190}
30853191
30863192.settings-option:hover {
3087 background: var(--vp-c-default-soft);
30883193 color: var(--vp-c-text-1);
3194 background: var(--vp-c-bg-soft);
30893195}
30903196
30913197.settings-option.active {
30923198 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;
30943201}
30953202
30963203.settings-popup-enter-active,
@@ -3104,4 +3211,149 @@ svg {
31043211 opacity: 0;
31053212 transform: scale(0.92) translateY(-4px);
31063213}
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}
31073359</style>
\ No newline at end of file