PromptCommands.svelte 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <script lang="ts">
  2. import { prompts } from '$lib/stores';
  3. import { findWordIndices } from '$lib/utils';
  4. import { tick, getContext } from 'svelte';
  5. import { toast } from 'svelte-sonner';
  6. const i18n = getContext('i18n');
  7. export let prompt = '';
  8. let selectedCommandIdx = 0;
  9. let filteredPromptCommands = [];
  10. $: filteredPromptCommands = $prompts
  11. .filter((p) => p.command.includes(prompt))
  12. .sort((a, b) => a.title.localeCompare(b.title));
  13. $: if (prompt) {
  14. selectedCommandIdx = 0;
  15. }
  16. export const selectUp = () => {
  17. selectedCommandIdx = Math.max(0, selectedCommandIdx - 1);
  18. };
  19. export const selectDown = () => {
  20. selectedCommandIdx = Math.min(selectedCommandIdx + 1, filteredPromptCommands.length - 1);
  21. };
  22. const confirmCommand = async (command) => {
  23. let text = command.content;
  24. if (command.content.includes('{{CLIPBOARD}}')) {
  25. const clipboardText = await navigator.clipboard.readText().catch((err) => {
  26. toast.error($i18n.t('Failed to read clipboard contents'));
  27. return '{{CLIPBOARD}}';
  28. });
  29. text = command.content.replaceAll('{{CLIPBOARD}}', clipboardText);
  30. }
  31. prompt = text;
  32. const chatInputElement = document.getElementById('chat-textarea');
  33. await tick();
  34. chatInputElement.style.height = '';
  35. chatInputElement.style.height = Math.min(chatInputElement.scrollHeight, 200) + 'px';
  36. chatInputElement?.focus();
  37. await tick();
  38. const words = findWordIndices(prompt);
  39. if (words.length > 0) {
  40. const word = words.at(0);
  41. chatInputElement.setSelectionRange(word?.startIndex, word.endIndex + 1);
  42. }
  43. };
  44. </script>
  45. {#if filteredPromptCommands.length > 0}
  46. <div class="md:px-2 mb-3 text-left w-full absolute bottom-0 left-0 right-0">
  47. <div class="flex w-full px-2">
  48. <div class=" bg-gray-100 dark:bg-gray-700 w-10 rounded-l-xl text-center">
  49. <div class=" text-lg font-semibold mt-2">/</div>
  50. </div>
  51. <div class="max-h-60 flex flex-col w-full rounded-r-xl bg-white">
  52. <div class="m-1 overflow-y-auto p-1 rounded-r-xl space-y-0.5">
  53. {#each filteredPromptCommands as command, commandIdx}
  54. <button
  55. class=" px-3 py-1.5 rounded-xl w-full text-left {commandIdx === selectedCommandIdx
  56. ? ' bg-gray-100 selected-command-option-button'
  57. : ''}"
  58. type="button"
  59. on:click={() => {
  60. confirmCommand(command);
  61. }}
  62. on:mousemove={() => {
  63. selectedCommandIdx = commandIdx;
  64. }}
  65. on:focus={() => {}}
  66. >
  67. <div class=" font-medium text-black">
  68. {command.command}
  69. </div>
  70. <div class=" text-xs text-gray-600">
  71. {command.title}
  72. </div>
  73. </button>
  74. {/each}
  75. </div>
  76. <div
  77. class=" px-2 pb-1 text-xs text-gray-600 bg-white rounded-br-xl flex items-center space-x-1"
  78. >
  79. <div>
  80. <svg
  81. xmlns="http://www.w3.org/2000/svg"
  82. fill="none"
  83. viewBox="0 0 24 24"
  84. stroke-width="1.5"
  85. stroke="currentColor"
  86. class="w-3 h-3"
  87. >
  88. <path
  89. stroke-linecap="round"
  90. stroke-linejoin="round"
  91. 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"
  92. />
  93. </svg>
  94. </div>
  95. <div class="line-clamp-1">
  96. {$i18n.t(
  97. 'Tip: Update multiple variable slots consecutively by pressing the tab key in the chat input after each replacement.'
  98. )}
  99. </div>
  100. </div>
  101. </div>
  102. </div>
  103. </div>
  104. {/if}