SearchInput.svelte 5.5 KB

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