index.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <script lang="ts">
  2. export default {
  3. name: "Search",
  4. };
  5. </script>
  6. <script setup lang="ts">
  7. import { ref, computed, watch, shallowRef, markRaw, nextTick, onMounted } from "vue";
  8. import Mark from 'mark.js/src/vanilla.js'
  9. import localSearchIndex from '@localSearchIndex'
  10. import MiniSearch, { type SearchResult } from 'minisearch'
  11. import { Search } from "@element-plus/icons-vue";
  12. import { watchDebounced, useFocus, computedAsync } from "@vueuse/core";
  13. import { useI18n } from "vue-i18n";
  14. import { useData } from 'vitepress'
  15. import { LRUCache } from 'vitepress/dist/client/theme-default/support/lru'
  16. import { pathToFile } from 'vitepress/dist/client/app/utils'
  17. import { escapeRegExp } from 'vitepress/dist/client/shared'
  18. import _ from "lodash";
  19. const { t } = useI18n();
  20. /* Search */
  21. const searchIndexData = shallowRef(localSearchIndex)
  22. // hmr
  23. if (import.meta.hot) {
  24. import.meta.hot.accept('/@localSearchIndex', (m) => {
  25. if (m) {
  26. searchIndexData.value = m.default
  27. }
  28. })
  29. }
  30. interface Result {
  31. title: string
  32. titles: string[]
  33. text?: string
  34. }
  35. const vitePressData = useData()
  36. const { localeIndex, theme } = vitePressData
  37. const searchIndex = computedAsync(async () =>
  38. markRaw(
  39. MiniSearch.loadJSON<Result>(
  40. (await searchIndexData.value[localeIndex.value]?.())?.default,
  41. {
  42. fields: ['title', 'titles', 'text'],
  43. storeFields: ['title', 'titles'],
  44. searchOptions: {
  45. fuzzy: 0.2,
  46. prefix: true,
  47. boost: { title: 4, text: 2, titles: 1 },
  48. ...(theme.value.search?.provider === 'local' &&
  49. theme.value.search.options?.miniSearch?.searchOptions)
  50. },
  51. ...(theme.value.search?.provider === 'local' &&
  52. theme.value.search.options?.miniSearch?.options)
  53. }
  54. )
  55. )
  56. )
  57. const filterText = ref("");
  58. const input$ = ref();
  59. const { focused } = useFocus(computed(() => input$.value?.input));
  60. onMounted(() => {
  61. focused.value = true
  62. })
  63. const results = shallowRef<(SearchResult & Result)[]>([])
  64. const loading = ref(false);
  65. const suggestionVisible = computed(() => {
  66. // TEST
  67. // return true
  68. const isValidData = results.value.length > 0;
  69. return !!( focused.value && (isValidData || loading.value || filterText.value) );
  70. });
  71. const resultsEl = shallowRef<HTMLElement>()
  72. const mark = computedAsync(async () => {
  73. if (!resultsEl.value) return
  74. return markRaw(new Mark(resultsEl.value))
  75. }, null)
  76. function formMarkRegex(terms: Set<string>) {
  77. return new RegExp(
  78. [...terms]
  79. .sort((a, b) => b.length - a.length)
  80. .map((term) => `(${escapeRegExp(term)})`)
  81. .join('|'),
  82. 'gi'
  83. )
  84. }
  85. watch(() => filterText.value, () => {
  86. if (!results.value.length) {
  87. loading.value = true
  88. }
  89. })
  90. watchDebounced(
  91. () => [searchIndex.value, filterText.value] as const,
  92. async ([index, filterTextValue], old, onCleanup) => {
  93. let canceled = false
  94. onCleanup(() => {
  95. canceled = true
  96. })
  97. if (!index) return
  98. // Search
  99. results.value = index
  100. .search(filterTextValue)
  101. .slice(0, 16) as (SearchResult & Result)[]
  102. const terms = new Set<string>()
  103. results.value = results.value.map((r) => {
  104. for (const term in r.match) {
  105. terms.add(term)
  106. }
  107. return r
  108. })
  109. loading.value = false
  110. await nextTick()
  111. if (canceled) return
  112. await new Promise((r) => {
  113. mark.value?.unmark({
  114. done: () => {
  115. mark.value?.markRegExp(formMarkRegex(terms), { done: r })
  116. }
  117. })
  118. })
  119. // const excerpts = el.value?.querySelectorAll('.result .excerpt') ?? []
  120. // for (const excerpt of excerpts) {
  121. // excerpt
  122. // .querySelector('mark[data-markjs="true"]')
  123. // ?.scrollIntoView({ block: 'center' })
  124. // }
  125. // FIXME: without this whole page scrolls to the bottom
  126. // resultsEl.value?.firstElementChild?.scrollIntoView({ block: 'start' })
  127. },
  128. { debounce: 200, immediate: true }
  129. );
  130. const searchRecommend = (e) => {
  131. filterText.value = e.target.innerText
  132. input$.value.focus()
  133. }
  134. </script>
  135. <template>
  136. <div class="search-container">
  137. <el-popover
  138. :visible="suggestionVisible"
  139. :show-arrow="false"
  140. :offset="0"
  141. :teleported="false"
  142. width="100%"
  143. >
  144. <template #reference>
  145. <div class="search-trigger" :class="{ 'has-content': suggestionVisible }">
  146. <el-input
  147. :ref="(el) => (input$ = el)"
  148. v-model="filterText"
  149. clearable
  150. :prefix-icon="Search"
  151. :placeholder="t('请输入关键词,如:课程、协同、AI')"
  152. ></el-input>
  153. </div>
  154. </template>
  155. <div class="search-content">
  156. <template v-if="loading">
  157. <el-skeleton animated />
  158. </template>
  159. <template v-else-if="results.length">
  160. <ul ref="resultsEl" class="results">
  161. <li v-for="(p, index) in results" :key="index">
  162. <a
  163. :href="p.id"
  164. class="result"
  165. :aria-label="[...p.titles, p.title].join(' > ')"
  166. >
  167. <div>
  168. <div class="titles">
  169. <span class="title-icon">#</span>
  170. <span v-for="(t, index) in p.titles" :key="index" class="title">
  171. <span class="text" v-html="t" />
  172. <span class="vpi-chevron-right local-search-icon" />
  173. </span>
  174. <span class="title main">
  175. <span class="text" v-html="p.title" />
  176. </span>
  177. </div>
  178. </div>
  179. </a>
  180. </li>
  181. </ul>
  182. </template>
  183. <template v-else-if="filterText">
  184. <el-empty :image-size="80">
  185. <template #description>
  186. <span
  187. >无法找到相关结果 <strong>"{{ filterText }}"</strong>
  188. </span>
  189. </template>
  190. </el-empty>
  191. </template>
  192. </div>
  193. </el-popover>
  194. <div class="search-recommend">
  195. <span> 搜索推荐: </span>
  196. <span class="recommend" @click="searchRecommend">课程</span>
  197. <span class="recommend" @click="searchRecommend">协同</span>
  198. <span class="recommend" @click="searchRecommend">项目</span>
  199. <span class="recommend" @click="searchRecommend">登录</span>
  200. <span class="recommend" @click="searchRecommend">AI助手</span>
  201. </div>
  202. </div>
  203. </template>
  204. <i18n locale="zh-HK">
  205. {
  206. "请输入关键词,如:课程、协同、AI": "TODO",
  207. }
  208. </i18n>
  209. <style lang="scss" scoped>
  210. .search-container {
  211. width: 514px;
  212. margin: auto;
  213. position: relative;
  214. .search-trigger {
  215. border: 1px solid #aeccfe;
  216. padding: 1px;
  217. width: 100%;
  218. height: 52px;
  219. border-radius: 26px;
  220. display: flex;
  221. align-items: center;
  222. padding: 0 10px;
  223. overflow: hidden;
  224. transition: all 0.2s;
  225. :deep(.el-input) {
  226. .el-input__wrapper {
  227. box-shadow: none;
  228. }
  229. }
  230. &:has(input:focus) {
  231. border: none;
  232. box-shadow: var(--el-box-shadow-light);
  233. }
  234. &.has-content {
  235. border-bottom: 1px solid #e2eeff;
  236. border-radius: 26px 26px 0 0;
  237. }
  238. }
  239. :deep(.el-popover) {
  240. border-radius: 0 0 26px 26px;
  241. border: none;
  242. clip-path: inset(0px -10px -10px -10px);
  243. padding: 0;
  244. overflow: hidden;
  245. max-height: 300px;
  246. overflow-y: scroll;
  247. }
  248. .search-recommend {
  249. display: flex;
  250. align-items: center;
  251. gap: 8px;
  252. padding: 0 20px;
  253. margin-top: 8px;
  254. span {
  255. color: #41506dcc;
  256. font-size: 12px;
  257. font-weight: 600;
  258. line-height: 20px;
  259. }
  260. .recommend {
  261. cursor: pointer;
  262. }
  263. }
  264. .search-content {
  265. .el-skeleton {
  266. padding: 10px 20px;
  267. }
  268. .results {
  269. display: flex;
  270. flex-direction: column;
  271. overflow-x: hidden;
  272. overflow-y: auto;
  273. overscroll-behavior: contain;
  274. list-style-type: none;
  275. padding: 0 0 5px;
  276. margin: 0;
  277. li {
  278. margin: 0;
  279. }
  280. .result {
  281. display: flex;
  282. align-items: center;
  283. gap: 8px;
  284. border-radius: 4px;
  285. transition: none;
  286. line-height: 1rem;
  287. outline: none;
  288. min-height: 40px;
  289. padding: 5px 20px;
  290. color: #333;
  291. :deep(mark) {
  292. color: #3681fc;
  293. background: transparent;
  294. }
  295. &:hover {
  296. background-color: #ecf5ff;
  297. }
  298. .titles {
  299. display: flex;
  300. flex-wrap: wrap;
  301. gap: 4px;
  302. position: relative;
  303. z-index: 1001;
  304. padding: 2px 0;
  305. }
  306. .title {
  307. display: flex;
  308. align-items: center;
  309. gap: 4px;
  310. }
  311. .title.main {
  312. font-weight: 500;
  313. }
  314. .title-icon {
  315. opacity: 0.5;
  316. font-weight: 500;
  317. color: var(--vp-c-brand-1);
  318. }
  319. .title svg {
  320. opacity: 0.5;
  321. }
  322. }
  323. }
  324. }
  325. }
  326. </style>