index.vue 8.8 KB

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