SearchInput.svelte 5.6 KB

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