Prompts.svelte 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <script lang="ts">
  2. import { prompts, user } from '$lib/stores';
  3. import {
  4. findWordIndices,
  5. getUserPosition,
  6. getFormattedDate,
  7. getFormattedTime,
  8. getCurrentDateTime,
  9. getUserTimezone,
  10. getWeekday
  11. } from '$lib/utils';
  12. import { tick, getContext } from 'svelte';
  13. import { toast } from 'svelte-sonner';
  14. const i18n = getContext('i18n');
  15. export let files;
  16. export let prompt = '';
  17. export let command = '';
  18. let selectedPromptIdx = 0;
  19. let filteredPrompts = [];
  20. $: filteredPrompts = $prompts
  21. .filter((p) => p.command.toLowerCase().includes(command.toLowerCase()))
  22. .sort((a, b) => a.title.localeCompare(b.title));
  23. $: if (command) {
  24. selectedPromptIdx = 0;
  25. }
  26. export const selectUp = () => {
  27. selectedPromptIdx = Math.max(0, selectedPromptIdx - 1);
  28. };
  29. export const selectDown = () => {
  30. selectedPromptIdx = Math.min(selectedPromptIdx + 1, filteredPrompts.length - 1);
  31. };
  32. const confirmPrompt = async (command) => {
  33. let text = command.content;
  34. if (command.content.includes('{{CLIPBOARD}}')) {
  35. const clipboardText = await navigator.clipboard.readText().catch((err) => {
  36. toast.error($i18n.t('Failed to read clipboard contents'));
  37. return '{{CLIPBOARD}}';
  38. });
  39. const clipboardItems = await navigator.clipboard.read();
  40. let imageUrl = null;
  41. for (const item of clipboardItems) {
  42. // Check for known image types
  43. for (const type of item.types) {
  44. if (type.startsWith('image/')) {
  45. const blob = await item.getType(type);
  46. imageUrl = URL.createObjectURL(blob);
  47. }
  48. }
  49. }
  50. if (imageUrl) {
  51. files = [
  52. ...files,
  53. {
  54. type: 'image',
  55. url: imageUrl
  56. }
  57. ];
  58. }
  59. text = text.replaceAll('{{CLIPBOARD}}', clipboardText);
  60. }
  61. if (command.content.includes('{{USER_LOCATION}}')) {
  62. let location;
  63. try {
  64. location = await getUserPosition();
  65. } catch (error) {
  66. toast.error($i18n.t('Location access not allowed'));
  67. location = 'LOCATION_UNKNOWN';
  68. }
  69. text = text.replaceAll('{{USER_LOCATION}}', String(location));
  70. }
  71. if (command.content.includes('{{USER_NAME}}')) {
  72. console.log($user);
  73. const name = $user.name || 'User';
  74. text = text.replaceAll('{{USER_NAME}}', name);
  75. }
  76. if (command.content.includes('{{USER_LANGUAGE}}')) {
  77. const language = localStorage.getItem('locale') || 'en-US';
  78. text = text.replaceAll('{{USER_LANGUAGE}}', language);
  79. }
  80. if (command.content.includes('{{CURRENT_DATE}}')) {
  81. const date = getFormattedDate();
  82. text = text.replaceAll('{{CURRENT_DATE}}', date);
  83. }
  84. if (command.content.includes('{{CURRENT_TIME}}')) {
  85. const time = getFormattedTime();
  86. text = text.replaceAll('{{CURRENT_TIME}}', time);
  87. }
  88. if (command.content.includes('{{CURRENT_DATETIME}}')) {
  89. const dateTime = getCurrentDateTime();
  90. text = text.replaceAll('{{CURRENT_DATETIME}}', dateTime);
  91. }
  92. if (command.content.includes('{{CURRENT_TIMEZONE}}')) {
  93. const timezone = getUserTimezone();
  94. text = text.replaceAll('{{CURRENT_TIMEZONE}}', timezone);
  95. }
  96. if (command.content.includes('{{CURRENT_WEEKDAY}}')) {
  97. const weekday = getWeekday();
  98. text = text.replaceAll('{{CURRENT_WEEKDAY}}', weekday);
  99. }
  100. prompt = text;
  101. const chatInputContainerElement = document.getElementById('chat-input-container');
  102. const chatInputElement = document.getElementById('chat-input');
  103. await tick();
  104. if (chatInputContainerElement) {
  105. chatInputContainerElement.style.height = '';
  106. chatInputContainerElement.style.height =
  107. Math.min(chatInputContainerElement.scrollHeight, 200) + 'px';
  108. }
  109. await tick();
  110. if (chatInputElement) {
  111. chatInputElement.focus();
  112. chatInputElement.dispatchEvent(new Event('input'));
  113. }
  114. };
  115. </script>
  116. {#if filteredPrompts.length > 0}
  117. <div
  118. id="commands-container"
  119. class="px-2 mb-2 text-left w-full absolute bottom-0 left-0 right-0 z-10"
  120. >
  121. <div class="flex w-full rounded-xl border border-gray-100 dark:border-gray-850">
  122. <div
  123. class="max-h-60 flex flex-col w-full rounded-xl bg-white dark:bg-gray-900 dark:text-gray-100"
  124. >
  125. <div class="m-1 overflow-y-auto p-1 space-y-0.5 scrollbar-hidden">
  126. {#each filteredPrompts as prompt, promptIdx}
  127. <button
  128. class=" px-3 py-1.5 rounded-xl w-full text-left {promptIdx === selectedPromptIdx
  129. ? ' bg-gray-50 dark:bg-gray-850 selected-command-option-button'
  130. : ''}"
  131. type="button"
  132. on:click={() => {
  133. confirmPrompt(prompt);
  134. }}
  135. on:mousemove={() => {
  136. selectedPromptIdx = promptIdx;
  137. }}
  138. on:focus={() => {}}
  139. >
  140. <div class=" font-medium text-black dark:text-gray-100">
  141. {prompt.command}
  142. </div>
  143. <div class=" text-xs text-gray-600 dark:text-gray-100">
  144. {prompt.title}
  145. </div>
  146. </button>
  147. {/each}
  148. </div>
  149. <div
  150. class=" px-2 pt-0.5 pb-1 text-xs text-gray-600 dark:text-gray-100 bg-white dark:bg-gray-900 rounded-b-xl flex items-center space-x-1"
  151. >
  152. <div>
  153. <svg
  154. xmlns="http://www.w3.org/2000/svg"
  155. fill="none"
  156. viewBox="0 0 24 24"
  157. stroke-width="1.5"
  158. stroke="currentColor"
  159. class="w-3 h-3"
  160. >
  161. <path
  162. stroke-linecap="round"
  163. stroke-linejoin="round"
  164. d="m11.25 11.25.041-.02a.75.75 0 0 1 1.063.852l-.708 2.836a.75.75 0 0 0 1.063.853l.041-.021M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Zm-9-3.75h.008v.008H12V8.25Z"
  165. />
  166. </svg>
  167. </div>
  168. <div class="line-clamp-1">
  169. {$i18n.t(
  170. 'Tip: Update multiple variable slots consecutively by pressing the tab key in the chat input after each replacement.'
  171. )}
  172. </div>
  173. </div>
  174. </div>
  175. </div>
  176. </div>
  177. {/if}