PromptCommands.svelte 3.3 KB

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