index.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <script lang="ts">
  2. export default {
  3. name: "Search",
  4. };
  5. </script>
  6. <script setup lang="ts">
  7. import { ref, computed, watch, shallowRef } from "vue";
  8. import localSearchIndex from '@localSearchIndex'
  9. import { Search } from "@element-plus/icons-vue";
  10. import { watchDebounced, useFocus } from "@vueuse/core";
  11. import { useI18n } from "vue-i18n";
  12. import _ from "lodash";
  13. /* Search */
  14. const searchIndexData = shallowRef(localSearchIndex)
  15. // hmr
  16. if (import.meta.hot) {
  17. import.meta.hot.accept('/@localSearchIndex', (m) => {
  18. if (m) {
  19. searchIndexData.value = m.default
  20. }
  21. })
  22. }
  23. const { t } = useI18n();
  24. const input = ref("");
  25. const input$ = ref();
  26. const { focused } = useFocus(computed(() => input$.value?.input));
  27. const suggestions = ref<unknown[]>([]);
  28. const loading = ref(false);
  29. const suggestionVisible = computed(() => {
  30. // TEST
  31. // return true
  32. const isValidData = suggestions.value.length > 0;
  33. return focused.value && (isValidData || loading.value);
  34. });
  35. watch(
  36. () => input.value,
  37. (val) => {
  38. loading.value = !!val;
  39. if (!val) {
  40. suggestions.value = [];
  41. }
  42. }
  43. );
  44. const fetchSuggestions = (mock) => {
  45. return new Promise((resolve, reject) => {
  46. setTimeout(() => {
  47. // MOCK
  48. // TODO should we use local search and gpt search together?
  49. resolve(["vue", "react", mock]);
  50. }, 1000);
  51. });
  52. };
  53. watchDebounced(
  54. () => input.value,
  55. async (taggedInput) => {
  56. if (!taggedInput) {
  57. return;
  58. }
  59. const result = await fetchSuggestions(taggedInput);
  60. if (taggedInput === input.value) {
  61. suggestions.value = result as unknown[];
  62. loading.value = false;
  63. }
  64. },
  65. { debounce: 500 }
  66. );
  67. </script>
  68. <template>
  69. <div class="search-container">
  70. <el-popover
  71. :visible="suggestionVisible"
  72. :show-arrow="false"
  73. :offset="0"
  74. :teleported="false"
  75. width="100%"
  76. >
  77. <template #reference>
  78. <div class="search-trigger" :class="{ 'has-content': suggestionVisible }">
  79. <el-input
  80. :ref="(el) => (input$ = el)"
  81. v-model="input"
  82. clearable
  83. :prefix-icon="Search"
  84. :placeholder="t('请输入关键词,如:课程、协同、AI')"
  85. ></el-input>
  86. </div>
  87. </template>
  88. <div class="search-content">
  89. <template v-if="loading">
  90. <span>loading</span>
  91. </template>
  92. <template v-else>
  93. <ul>
  94. <li v-for="(suggest, _index) in suggestions" :key="_index">{{ suggest }}</li>
  95. </ul>
  96. </template>
  97. </div>
  98. </el-popover>
  99. </div>
  100. </template>
  101. <i18n locale="zh-HK">
  102. {
  103. "请输入关键词,如:课程、协同、AI": "TODO",
  104. }
  105. </i18n>
  106. <style lang="scss" scoped>
  107. .search-container {
  108. width: 514px;
  109. margin: auto;
  110. position: relative;
  111. .search-trigger {
  112. border: 1px solid #aeccfe;
  113. padding: 1px;
  114. width: 100%;
  115. height: 52px;
  116. border-radius: 26px;
  117. display: flex;
  118. align-items: center;
  119. padding: 0 10px;
  120. overflow: hidden;
  121. transition: all 0.2s;
  122. :deep(.el-input) {
  123. .el-input__wrapper {
  124. box-shadow: none;
  125. }
  126. }
  127. &:has(input:focus) {
  128. border: none;
  129. box-shadow: var(--el-box-shadow-light);
  130. }
  131. &.has-content {
  132. border-bottom: 1px solid #e2eeff;
  133. border-radius: 26px 26px 0 0;
  134. }
  135. }
  136. :deep(.el-popover) {
  137. border-radius: 0 0 26px 26px;
  138. border: none;
  139. clip-path: inset(0px -10px -10px -10px);
  140. }
  141. .search-content {
  142. }
  143. }
  144. </style>