SearchInput.svelte 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <script lang="ts">
  2. import { getAllTags } from '$lib/apis/chats';
  3. import { tags } from '$lib/stores';
  4. import { getContext, createEventDispatcher, onMount, onDestroy, tick } from 'svelte';
  5. import { fade } from 'svelte/transition';
  6. const dispatch = createEventDispatcher();
  7. const i18n = getContext('i18n');
  8. export let placeholder = '';
  9. export let value = '';
  10. let selectedIdx = 0;
  11. let lastWord = '';
  12. $: lastWord = value ? value.split(' ').at(-1) : value;
  13. let options = [
  14. {
  15. name: 'tag:',
  16. description: $i18n.t('search for tags')
  17. }
  18. ];
  19. let focused = false;
  20. let loading = false;
  21. let filteredOptions = options;
  22. $: filteredOptions = options.filter((option) => {
  23. return option.name.startsWith(lastWord);
  24. });
  25. let filteredTags = [];
  26. $: filteredTags = lastWord.startsWith('tag:')
  27. ? [
  28. ...$tags,
  29. {
  30. id: 'none',
  31. name: $i18n.t('Untagged')
  32. }
  33. ].filter((tag) => {
  34. const tagName = lastWord.slice(4);
  35. if (tagName) {
  36. const tagId = tagName.replace(' ', '_').toLowerCase();
  37. if (tag.id !== tagId) {
  38. return tag.id.startsWith(tagId);
  39. } else {
  40. return false;
  41. }
  42. } else {
  43. return true;
  44. }
  45. })
  46. : [];
  47. const initTags = async () => {
  48. loading = true;
  49. await tags.set(await getAllTags(localStorage.token));
  50. loading = false;
  51. };
  52. const documentClickHandler = (e) => {
  53. const searchContainer = document.getElementById('search-container');
  54. const chatSearch = document.getElementById('chat-search');
  55. if (!searchContainer.contains(e.target) && !chatSearch.contains(e.target)) {
  56. if (e.target.id.startsWith('search-tag-') || e.target.id.startsWith('search-option-')) {
  57. return;
  58. }
  59. focused = false;
  60. }
  61. };
  62. onMount(() => {
  63. document.addEventListener('click', documentClickHandler);
  64. });
  65. onDestroy(() => {
  66. document.removeEventListener('click', documentClickHandler);
  67. });
  68. </script>
  69. <div class="px-1 mb-1 flex justify-center space-x-2 relative z-10" id="search-container">
  70. <div class="flex w-full rounded-xl" id="chat-search">
  71. <div class="self-center pl-3 py-2 rounded-l-xl bg-transparent">
  72. <svg
  73. xmlns="http://www.w3.org/2000/svg"
  74. viewBox="0 0 20 20"
  75. fill="currentColor"
  76. class="w-4 h-4"
  77. >
  78. <path
  79. fill-rule="evenodd"
  80. d="M9 3.5a5.5 5.5 0 100 11 5.5 5.5 0 000-11zM2 9a7 7 0 1112.452 4.391l3.328 3.329a.75.75 0 11-1.06 1.06l-3.329-3.328A7 7 0 012 9z"
  81. clip-rule="evenodd"
  82. />
  83. </svg>
  84. </div>
  85. <input
  86. class="w-full rounded-r-xl py-1.5 pl-2.5 pr-4 text-sm bg-transparent dark:text-gray-300 outline-hidden"
  87. placeholder={placeholder ? placeholder : $i18n.t('Search')}
  88. bind:value
  89. on:input={() => {
  90. dispatch('input');
  91. }}
  92. on:focus={() => {
  93. focused = true;
  94. initTags();
  95. }}
  96. on:keydown={(e) => {
  97. if (e.key === 'Enter') {
  98. if (filteredTags.length > 0) {
  99. const tagElement = document.getElementById(`search-tag-${selectedIdx}`);
  100. tagElement.click();
  101. return;
  102. }
  103. if (filteredOptions.length > 0) {
  104. const optionElement = document.getElementById(`search-option-${selectedIdx}`);
  105. optionElement.click();
  106. return;
  107. }
  108. }
  109. if (e.key === 'ArrowUp') {
  110. e.preventDefault();
  111. selectedIdx = Math.max(0, selectedIdx - 1);
  112. } else if (e.key === 'ArrowDown') {
  113. e.preventDefault();
  114. if (filteredTags.length > 0) {
  115. selectedIdx = Math.min(selectedIdx + 1, filteredTags.length - 1);
  116. } else {
  117. selectedIdx = Math.min(selectedIdx + 1, filteredOptions.length - 1);
  118. }
  119. } else {
  120. // if the user types something, reset to the top selection.
  121. selectedIdx = 0;
  122. }
  123. }}
  124. />
  125. </div>
  126. {#if focused && (filteredOptions.length > 0 || filteredTags.length > 0)}
  127. <!-- svelte-ignore a11y-no-static-element-interactions -->
  128. <div
  129. class="absolute top-0 mt-8 left-0 right-1 border dark:border-gray-900 bg-gray-50 dark:bg-gray-950 rounded-lg z-10 shadow-lg"
  130. in:fade={{ duration: 50 }}
  131. on:mouseenter={() => {
  132. selectedIdx = null;
  133. }}
  134. on:mouseleave={() => {
  135. selectedIdx = 0;
  136. }}
  137. >
  138. <div class="px-2 py-2 text-xs group">
  139. {#if filteredTags.length > 0}
  140. <div class="px-1 font-medium dark:text-gray-300 text-gray-700 mb-1">Tags</div>
  141. <div class="max-h-60 overflow-auto">
  142. {#each filteredTags as tag, tagIdx}
  143. <button
  144. class=" px-1.5 py-0.5 flex gap-1 hover:bg-gray-100 dark:hover:bg-gray-900 w-full rounded {selectedIdx ===
  145. tagIdx
  146. ? 'bg-gray-100 dark:bg-gray-900'
  147. : ''}"
  148. id="search-tag-{tagIdx}"
  149. on:click|stopPropagation={async () => {
  150. const words = value.split(' ');
  151. words.pop();
  152. words.push(`tag:${tag.id} `);
  153. value = words.join(' ');
  154. dispatch('input');
  155. }}
  156. >
  157. <div class="dark:text-gray-300 text-gray-700 font-medium line-clamp-1 shrink-0">
  158. {tag.name}
  159. </div>
  160. <div class=" text-gray-500 line-clamp-1">
  161. {tag.id}
  162. </div>
  163. </button>
  164. {/each}
  165. </div>
  166. {:else if filteredOptions.length > 0}
  167. <div class="px-1 font-medium dark:text-gray-300 text-gray-700 mb-1">
  168. {$i18n.t('Search options')}
  169. </div>
  170. <div class=" max-h-60 overflow-auto">
  171. {#each filteredOptions as option, optionIdx}
  172. <button
  173. class=" px-1.5 py-0.5 flex gap-1 hover:bg-gray-100 dark:hover:bg-gray-900 w-full rounded {selectedIdx ===
  174. optionIdx
  175. ? 'bg-gray-100 dark:bg-gray-900'
  176. : ''}"
  177. id="search-option-{optionIdx}"
  178. on:click|stopPropagation={async () => {
  179. const words = value.split(' ');
  180. words.pop();
  181. words.push('tag:');
  182. value = words.join(' ');
  183. dispatch('input');
  184. }}
  185. >
  186. <div class="dark:text-gray-300 text-gray-700 font-medium">{option.name}</div>
  187. <div class=" text-gray-500 line-clamp-1">
  188. {option.description}
  189. </div>
  190. </button>
  191. {/each}
  192. </div>
  193. {/if}
  194. </div>
  195. </div>
  196. {/if}
  197. </div>