Prompts.svelte 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. const location = await getUserPosition();
  63. text = text.replaceAll('{{USER_LOCATION}}', String(location));
  64. }
  65. if (command.content.includes('{{USER_NAME}}')) {
  66. console.log($user);
  67. const name = $user.name || 'User';
  68. text = text.replaceAll('{{USER_NAME}}', name);
  69. }
  70. if (command.content.includes('{{USER_LANGUAGE}}')) {
  71. const language = localStorage.getItem('locale') || 'en-US';
  72. text = text.replaceAll('{{USER_LANGUAGE}}', language);
  73. }
  74. if (command.content.includes('{{CURRENT_DATE}}')) {
  75. const date = getFormattedDate();
  76. text = text.replaceAll('{{CURRENT_DATE}}', date);
  77. }
  78. if (command.content.includes('{{CURRENT_TIME}}')) {
  79. const time = getFormattedTime();
  80. text = text.replaceAll('{{CURRENT_TIME}}', time);
  81. }
  82. if (command.content.includes('{{CURRENT_DATETIME}}')) {
  83. const dateTime = getCurrentDateTime();
  84. text = text.replaceAll('{{CURRENT_DATETIME}}', dateTime);
  85. }
  86. if (command.content.includes('{{CURRENT_TIMEZONE}}')) {
  87. const timezone = getUserTimezone();
  88. text = text.replaceAll('{{CURRENT_TIMEZONE}}', timezone);
  89. }
  90. if (command.content.includes('{{CURRENT_WEEKDAY}}')) {
  91. const weekday = getWeekday();
  92. text = text.replaceAll('{{CURRENT_WEEKDAY}}', weekday);
  93. }
  94. prompt = text;
  95. const chatInputContainerElement = document.getElementById('chat-input-container');
  96. const chatInputElement = document.getElementById('chat-input');
  97. await tick();
  98. if (chatInputContainerElement) {
  99. chatInputContainerElement.style.height = '';
  100. chatInputContainerElement.style.height =
  101. Math.min(chatInputContainerElement.scrollHeight, 200) + 'px';
  102. }
  103. await tick();
  104. if (chatInputElement) {
  105. chatInputElement.focus();
  106. chatInputElement.dispatchEvent(new Event('input'));
  107. }
  108. };
  109. </script>
  110. {#if filteredPrompts.length > 0}
  111. <div
  112. id="commands-container"
  113. class="px-2 mb-2 text-left w-full absolute bottom-0 left-0 right-0 z-10"
  114. >
  115. <div class="flex w-full rounded-xl border border-gray-50 dark:border-gray-850">
  116. <div
  117. class="max-h-60 flex flex-col w-full rounded-xl bg-white dark:bg-gray-900 dark:text-gray-100"
  118. >
  119. <div class="m-1 overflow-y-auto p-1 space-y-0.5 scrollbar-hidden">
  120. {#each filteredPrompts as prompt, promptIdx}
  121. <button
  122. class=" px-3 py-1.5 rounded-xl w-full text-left {promptIdx === selectedPromptIdx
  123. ? ' bg-gray-50 dark:bg-gray-850 selected-command-option-button'
  124. : ''}"
  125. type="button"
  126. on:click={() => {
  127. confirmPrompt(prompt);
  128. }}
  129. on:mousemove={() => {
  130. selectedPromptIdx = promptIdx;
  131. }}
  132. on:focus={() => {}}
  133. >
  134. <div class=" font-medium text-black dark:text-gray-100">
  135. {prompt.command}
  136. </div>
  137. <div class=" text-xs text-gray-600 dark:text-gray-100">
  138. {prompt.title}
  139. </div>
  140. </button>
  141. {/each}
  142. </div>
  143. <div
  144. 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"
  145. >
  146. <div>
  147. <svg
  148. xmlns="http://www.w3.org/2000/svg"
  149. fill="none"
  150. viewBox="0 0 24 24"
  151. stroke-width="1.5"
  152. stroke="currentColor"
  153. class="w-3 h-3"
  154. >
  155. <path
  156. stroke-linecap="round"
  157. stroke-linejoin="round"
  158. 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"
  159. />
  160. </svg>
  161. </div>
  162. <div class="line-clamp-1">
  163. {$i18n.t(
  164. 'Tip: Update multiple variable slots consecutively by pressing the tab key in the chat input after each replacement.'
  165. )}
  166. </div>
  167. </div>
  168. </div>
  169. </div>
  170. </div>
  171. {/if}