Prompts.svelte 5.2 KB

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