index.vue 8.7 KB

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