SearchInput.svelte 5.7 KB

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