SearchInput.svelte 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 mt-0.5 mb-2 flex justify-center space-x-2 relative" 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. id="search-input"
  79. class="w-full rounded-r-xl py-1.5 pl-2.5 pr-4 text-sm bg-transparent dark:text-gray-300 outline-none"
  80. placeholder={placeholder ? placeholder : $i18n.t('Search')}
  81. bind:value
  82. on:input={() => {
  83. dispatch('input');
  84. }}
  85. on:focus={() => {
  86. focused = true;
  87. }}
  88. on:keydown={(e) => {
  89. if (e.key === 'Enter') {
  90. if (filteredTags.length > 0) {
  91. const tagElement = document.getElementById(`search-tag-${selectedIdx}`);
  92. tagElement.click();
  93. return;
  94. }
  95. if (filteredOptions.length > 0) {
  96. const optionElement = document.getElementById(`search-option-${selectedIdx}`);
  97. optionElement.click();
  98. return;
  99. }
  100. }
  101. if (e.key === 'ArrowUp') {
  102. e.preventDefault();
  103. selectedIdx = Math.max(0, selectedIdx - 1);
  104. } else if (e.key === 'ArrowDown') {
  105. e.preventDefault();
  106. if (filteredTags.length > 0) {
  107. selectedIdx = Math.min(selectedIdx + 1, filteredTags.length - 1);
  108. } else {
  109. selectedIdx = Math.min(selectedIdx + 1, filteredOptions.length - 1);
  110. }
  111. } else {
  112. // if the user types something, reset to the top selection.
  113. selectedIdx = 0;
  114. }
  115. }}
  116. />
  117. </div>
  118. {#if focused && (filteredOptions.length > 0 || filteredTags.length > 0)}
  119. <!-- svelte-ignore a11y-no-static-element-interactions -->
  120. <div
  121. 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"
  122. in:fade={{ duration: 50 }}
  123. on:mouseenter={() => {
  124. selectedIdx = null;
  125. }}
  126. on:mouseleave={() => {
  127. selectedIdx = 0;
  128. }}
  129. >
  130. <div class="px-2 py-2 text-xs group">
  131. {#if filteredTags.length > 0}
  132. <div class="px-1 font-medium dark:text-gray-300 text-gray-700 mb-1">Tags</div>
  133. <div class="">
  134. {#each filteredTags as tag, tagIdx}
  135. <button
  136. class=" px-1.5 py-0.5 flex gap-1 hover:bg-gray-100 dark:hover:bg-gray-900 w-full rounded {selectedIdx ===
  137. tagIdx
  138. ? 'bg-gray-100 dark:bg-gray-900'
  139. : ''}"
  140. id="search-tag-{tagIdx}"
  141. on:click|stopPropagation={async () => {
  142. const words = value.split(' ');
  143. words.pop();
  144. words.push(`tag:${tag.id} `);
  145. value = words.join(' ');
  146. dispatch('input');
  147. }}
  148. >
  149. <div class="dark:text-gray-300 text-gray-700 font-medium">{tag.name}</div>
  150. <div class=" text-gray-500 line-clamp-1">
  151. {tag.id}
  152. </div>
  153. </button>
  154. {/each}
  155. </div>
  156. {:else if filteredOptions.length > 0}
  157. <div class="px-1 font-medium dark:text-gray-300 text-gray-700 mb-1">Search options</div>
  158. <div class="">
  159. {#each filteredOptions as option, optionIdx}
  160. <button
  161. class=" px-1.5 py-0.5 flex gap-1 hover:bg-gray-100 dark:hover:bg-gray-900 w-full rounded {selectedIdx ===
  162. optionIdx
  163. ? 'bg-gray-100 dark:bg-gray-900'
  164. : ''}"
  165. id="search-option-{optionIdx}"
  166. on:click|stopPropagation={async () => {
  167. const words = value.split(' ');
  168. words.pop();
  169. words.push('tag:');
  170. value = words.join(' ');
  171. dispatch('input');
  172. }}
  173. >
  174. <div class="dark:text-gray-300 text-gray-700 font-medium">{option.name}</div>
  175. <div class=" text-gray-500 line-clamp-1">
  176. {option.description}
  177. </div>
  178. </button>
  179. {/each}
  180. </div>
  181. {/if}
  182. </div>
  183. </div>
  184. {/if}
  185. </div>