PromptEditor.svelte 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <script lang="ts">
  2. import { onMount, tick, getContext } from 'svelte';
  3. import Textarea from '$lib/components/common/Textarea.svelte';
  4. import { toast } from 'svelte-sonner';
  5. import Tooltip from '$lib/components/common/Tooltip.svelte';
  6. export let onSubmit: Function;
  7. export let edit = false;
  8. export let prompt = null;
  9. const i18n = getContext('i18n');
  10. let loading = false;
  11. let title = '';
  12. let command = '';
  13. let content = '';
  14. $: if (!edit) {
  15. command = title !== '' ? `${title.replace(/\s+/g, '-').toLowerCase()}` : '';
  16. }
  17. const submitHandler = async () => {
  18. loading = true;
  19. if (validateCommandString(command)) {
  20. await onSubmit({
  21. title,
  22. command,
  23. content
  24. });
  25. } else {
  26. toast.error(
  27. $i18n.t('Only alphanumeric characters and hyphens are allowed in the command string.')
  28. );
  29. }
  30. loading = false;
  31. };
  32. const validateCommandString = (inputString) => {
  33. // Regular expression to match only alphanumeric characters and hyphen
  34. const regex = /^[a-zA-Z0-9-]+$/;
  35. // Test the input string against the regular expression
  36. return regex.test(inputString);
  37. };
  38. onMount(async () => {
  39. if (prompt) {
  40. title = prompt.title;
  41. await tick();
  42. command = prompt.command.at(0) === '/' ? prompt.command.slice(1) : prompt.command;
  43. content = prompt.content;
  44. }
  45. });
  46. </script>
  47. <form
  48. class="flex flex-col max-w-4xl mx-auto mt-4 mb-10 pb-10"
  49. on:submit|preventDefault={() => {
  50. submitHandler();
  51. }}
  52. >
  53. <div class="my-2">
  54. <Tooltip
  55. content={`${$i18n.t('Only alphanumeric characters and hyphens are allowed')} - ${$i18n.t(
  56. 'Activate this command by typing "/{{COMMAND}}" to chat input.',
  57. {
  58. COMMAND: command
  59. }
  60. )}`}
  61. placement="bottom-start"
  62. >
  63. <div class="flex flex-col w-full">
  64. <div>
  65. <input
  66. class="text-2xl font-semibold w-full bg-transparent outline-none"
  67. placeholder={$i18n.t('Title')}
  68. bind:value={title}
  69. required
  70. />
  71. </div>
  72. <div class="flex gap-0.5 items-center text-xs text-gray-500">
  73. <div class="">/</div>
  74. <input
  75. class=" w-full bg-transparent outline-none"
  76. placeholder={$i18n.t('Command')}
  77. bind:value={command}
  78. required
  79. disabled={edit}
  80. />
  81. </div>
  82. </div>
  83. </Tooltip>
  84. </div>
  85. <div class="my-2">
  86. <div class="flex w-full justify-between">
  87. <div class=" self-center text-sm font-semibold">{$i18n.t('Prompt Content')}</div>
  88. </div>
  89. <div class="mt-2">
  90. <div>
  91. <Textarea
  92. className="text-sm w-full bg-transparent outline-none overflow-y-hidden resize-none"
  93. placeholder={$i18n.t('Write a summary in 50 words that summarizes [topic or keyword].')}
  94. bind:value={content}
  95. rows={6}
  96. required
  97. />
  98. </div>
  99. <div class="text-xs text-gray-400 dark:text-gray-500">
  100. ⓘ {$i18n.t('Format your variables using brackets like this:')}&nbsp;<span
  101. class=" text-gray-600 dark:text-gray-300 font-medium"
  102. >{'{{'}{$i18n.t('variable')}{'}}'}</span
  103. >.
  104. {$i18n.t('Make sure to enclose them with')}
  105. <span class=" text-gray-600 dark:text-gray-300 font-medium">{'{{'}</span>
  106. {$i18n.t('and')}
  107. <span class=" text-gray-600 dark:text-gray-300 font-medium">{'}}'}</span>.
  108. </div>
  109. <div class="text-xs text-gray-400 dark:text-gray-500">
  110. {$i18n.t('Utilize')}<span class=" text-gray-600 dark:text-gray-300 font-medium">
  111. {` {{CLIPBOARD}}`}</span
  112. >
  113. {$i18n.t('variable to have them replaced with clipboard content.')}
  114. </div>
  115. </div>
  116. </div>
  117. <div class="my-4 flex justify-end">
  118. <button
  119. class=" text-sm px-3 py-2 transition rounded-lg {loading
  120. ? ' cursor-not-allowed bg-white hover:bg-gray-100 text-black'
  121. : ' bg-white hover:bg-gray-100 text-black'} flex w-full justify-center"
  122. type="submit"
  123. disabled={loading}
  124. >
  125. <div class=" self-center font-medium">{$i18n.t('Save & Create')}</div>
  126. {#if loading}
  127. <div class="ml-1.5 self-center">
  128. <svg
  129. class=" w-4 h-4"
  130. viewBox="0 0 24 24"
  131. fill="currentColor"
  132. xmlns="http://www.w3.org/2000/svg"
  133. ><style>
  134. .spinner_ajPY {
  135. transform-origin: center;
  136. animation: spinner_AtaB 0.75s infinite linear;
  137. }
  138. @keyframes spinner_AtaB {
  139. 100% {
  140. transform: rotate(360deg);
  141. }
  142. }
  143. </style><path
  144. d="M12,1A11,11,0,1,0,23,12,11,11,0,0,0,12,1Zm0,19a8,8,0,1,1,8-8A8,8,0,0,1,12,20Z"
  145. opacity=".25"
  146. /><path
  147. d="M10.14,1.16a11,11,0,0,0-9,8.92A1.59,1.59,0,0,0,2.46,12,1.52,1.52,0,0,0,4.11,10.7a8,8,0,0,1,6.66-6.61A1.42,1.42,0,0,0,12,2.69h0A1.57,1.57,0,0,0,10.14,1.16Z"
  148. class="spinner_ajPY"
  149. /></svg
  150. >
  151. </div>
  152. {/if}
  153. </button>
  154. </div>
  155. </form>