index.vue 8.6 KB

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