feat(search): allow force disabling detailed view

pull/2234/head
Divyansh Singh 2 years ago
parent fe1f58b700
commit 40f1d1b6f6

@ -18,7 +18,9 @@ import {
ref, ref,
shallowRef, shallowRef,
watch, watch,
type Ref type Ref,
computed,
watchEffect
} from 'vue' } from 'vue'
import type { ModalTranslations } from '../../../../types/local-search' import type { ModalTranslations } from '../../../../types/local-search'
import { pathToFile, withBase } from '../../app/utils' import { pathToFile, withBase } from '../../app/utils'
@ -54,7 +56,7 @@ interface Result {
text?: string text?: string
} }
const { localeIndex } = useData() const { localeIndex, theme } = useData()
const searchIndex = computedAsync(async () => const searchIndex = computedAsync(async () =>
markRaw( markRaw(
@ -80,6 +82,19 @@ const showDetailedList = useLocalStorage(
false false
) )
const disableDetailedView = computed(() => {
return (
theme.value.search?.provider === 'local' &&
theme.value.search.options?.disableDetailedView === true
)
})
watchEffect(() => {
if (disableDetailedView.value) {
showDetailedList.value = false
}
})
const results: Ref<(SearchResult & Result)[]> = shallowRef([]) const results: Ref<(SearchResult & Result)[]> = shallowRef([])
const contents = shallowRef(new Map<string, Map<string, string>>()) const contents = shallowRef(new Map<string, Map<string, string>>())
@ -158,21 +173,16 @@ debouncedWatch(
title = title.replace(reg, `<mark>$&</mark>`) title = title.replace(reg, `<mark>$&</mark>`)
} }
if (match.includes('titles')) { if (match.includes('titles')) {
titles = titles.flatMap((t) => titles = titles
t ? [t.replace(reg, `<mark>$&</mark>`)] : [] .map((t) => t?.replace(reg, `<mark>$&</mark>`))
) .filter(Boolean)
} }
if (showDetailedListValue && match.includes('text')) { if (showDetailedListValue && match.includes('text')) {
text = text.replace(reg, `<mark>$&</mark>`) text = text.replace(reg, `<mark>$&</mark>`)
} }
} }
return { return { ...r, title, titles, text }
...r,
title,
titles,
text
}
}) })
contents.value = c contents.value = c
@ -272,9 +282,6 @@ onKeyStroke('Escape', () => {
}) })
// Translations // Translations
const { theme } = useData()
const defaultTranslations: { modal: ModalTranslations } = { const defaultTranslations: { modal: ModalTranslations } = {
modal: { modal: {
displayDetails: 'Display detailed list', displayDetails: 'Display detailed list',
@ -364,10 +371,9 @@ useEventListener('popstate', (event) => {
/> />
<div class="search-actions"> <div class="search-actions">
<button <button
v-if="!disableDetailedView"
class="toggle-layout-button" class="toggle-layout-button"
:class="{ :class="{ 'detailed-list': showDetailedList }"
'detailed-list': showDetailedList
}"
:title="$t('modal.displayDetails')" :title="$t('modal.displayDetails')"
@click="showDetailedList = !showDetailedList" @click="showDetailedList = !showDetailedList"
> >
@ -746,6 +752,7 @@ useEventListener('popstate', (event) => {
background-color: var(--vp-c-highlight-bg); background-color: var(--vp-c-highlight-bg);
color: var(--vp-c-highlight-text); color: var(--vp-c-highlight-text);
border-radius: 2px; border-radius: 2px;
padding: 0 4px;
} }
.excerpt :deep(.vp-code-group) .tabs { .excerpt :deep(.vp-code-group) .tabs {

@ -98,7 +98,7 @@
--vp-c-mute-dark: #e3e3e5; --vp-c-mute-dark: #e3e3e5;
--vp-c-mute-darker: #d7d7d9; --vp-c-mute-darker: #d7d7d9;
--vp-c-highlight-bg: var(--vp-c-yellow-lighter); --vp-c-highlight-bg: var(--vp-c-green-lighter);
--vp-c-highlight-text: var(--vp-c-black); --vp-c-highlight-text: var(--vp-c-black);
} }

@ -293,6 +293,11 @@ export namespace DefaultTheme {
// local search -------------------------------------------------------------- // local search --------------------------------------------------------------
export interface LocalSearchOptions { export interface LocalSearchOptions {
/**
* @default false
*/
disableDetailedView?: boolean
translations?: LocalSearchTranslations translations?: LocalSearchTranslations
locales?: Record<string, Partial<Omit<LocalSearchOptions, 'locales'>>> locales?: Record<string, Partial<Omit<LocalSearchOptions, 'locales'>>>
} }

Loading…
Cancel
Save