AddTextContentModal.svelte 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <script lang="ts">
  2. import { toast } from 'svelte-sonner';
  3. import dayjs from 'dayjs';
  4. import { onMount, getContext, createEventDispatcher } from 'svelte';
  5. const i18n = getContext('i18n');
  6. const dispatch = createEventDispatcher();
  7. import Modal from '$lib/components/common/Modal.svelte';
  8. import RichTextInput from '$lib/components/common/RichTextInput.svelte';
  9. import XMark from '$lib/components/icons/XMark.svelte';
  10. import Mic from '$lib/components/icons/Mic.svelte';
  11. import Tooltip from '$lib/components/common/Tooltip.svelte';
  12. import VoiceRecording from '$lib/components/chat/MessageInput/VoiceRecording.svelte';
  13. export let show = false;
  14. let name = 'Untitled';
  15. let content = '';
  16. let voiceInput = false;
  17. </script>
  18. <Modal size="full" containerClassName="" className="h-full bg-white dark:bg-gray-900" bind:show>
  19. <div class="absolute top-0 right-0 p-5">
  20. <button
  21. class="self-center dark:text-white"
  22. type="button"
  23. on:click={() => {
  24. show = false;
  25. }}
  26. >
  27. <XMark className="size-3.5" />
  28. </button>
  29. </div>
  30. <div class="flex flex-col md:flex-row w-full h-full md:space-x-4 dark:text-gray-200">
  31. <form
  32. class="flex flex-col w-full h-full"
  33. on:submit|preventDefault={() => {
  34. if (name.trim() === '' || content.trim() === '') {
  35. toast.error($i18n.t('Please fill in all fields.'));
  36. name = name.trim();
  37. content = content.trim();
  38. return;
  39. }
  40. dispatch('submit', {
  41. name,
  42. content
  43. });
  44. show = false;
  45. name = '';
  46. content = '';
  47. }}
  48. >
  49. <div class=" flex-1 w-full h-full flex justify-center overflow-auto px-5 py-4">
  50. <div class=" max-w-3xl py-2 md:py-10 w-full flex flex-col gap-2">
  51. <div class="flex-shrink-0 w-full flex justify-between items-center">
  52. <div class="w-full">
  53. <input
  54. class="w-full text-3xl font-semibold bg-transparent outline-none"
  55. type="text"
  56. bind:value={name}
  57. placeholder={$i18n.t('Title')}
  58. required
  59. />
  60. </div>
  61. </div>
  62. <div class=" flex-1 w-full h-full">
  63. <RichTextInput
  64. bind:value={content}
  65. placeholder={$i18n.t('Write something...')}
  66. preserveBreaks={true}
  67. />
  68. </div>
  69. </div>
  70. </div>
  71. <div
  72. class="flex flex-row items-center justify-end text-sm font-medium flex-shrink-0 mt-1 p-4 gap-1.5"
  73. >
  74. <div class="">
  75. {#if voiceInput}
  76. <div class=" max-w-full w-full">
  77. <VoiceRecording
  78. bind:recording={voiceInput}
  79. className="p-1"
  80. on:cancel={() => {
  81. voiceInput = false;
  82. }}
  83. on:confirm={(e) => {
  84. const { text, filename } = e.detail;
  85. content = `${content}${text} `;
  86. voiceInput = false;
  87. }}
  88. />
  89. </div>
  90. {:else}
  91. <Tooltip content={$i18n.t('Voice Input')}>
  92. <button
  93. class=" p-2 bg-gray-50 text-gray-700 dark:bg-gray-700 dark:text-white transition rounded-full"
  94. type="button"
  95. on:click={async () => {
  96. try {
  97. let stream = await navigator.mediaDevices
  98. .getUserMedia({ audio: true })
  99. .catch(function (err) {
  100. toast.error(
  101. $i18n.t(`Permission denied when accessing microphone: {{error}}`, {
  102. error: err
  103. })
  104. );
  105. return null;
  106. });
  107. if (stream) {
  108. voiceInput = true;
  109. const tracks = stream.getTracks();
  110. tracks.forEach((track) => track.stop());
  111. }
  112. stream = null;
  113. } catch {
  114. toast.error($i18n.t('Permission denied when accessing microphone'));
  115. }
  116. }}
  117. >
  118. <Mic className="size-5" />
  119. </button>
  120. </Tooltip>
  121. {/if}
  122. </div>
  123. <div class=" flex-shrink-0">
  124. <Tooltip content={$i18n.t('Save')}>
  125. <button
  126. class=" px-3.5 py-2 bg-black text-white dark:bg-white dark:text-black transition rounded-full"
  127. type="submit"
  128. >
  129. {$i18n.t('Save')}
  130. </button>
  131. </Tooltip>
  132. </div>
  133. </div>
  134. </form>
  135. </div>
  136. </Modal>
  137. <style>
  138. input::-webkit-outer-spin-button,
  139. input::-webkit-inner-spin-button {
  140. /* display: none; <- Crashes Chrome on hover */
  141. -webkit-appearance: none;
  142. margin: 0; /* <-- Apparently some margin are still there even though it's hidden */
  143. }
  144. .tabs::-webkit-scrollbar {
  145. display: none; /* for Chrome, Safari and Opera */
  146. }
  147. .tabs {
  148. -ms-overflow-style: none; /* IE and Edge */
  149. scrollbar-width: none; /* Firefox */
  150. }
  151. input[type='number'] {
  152. -moz-appearance: textfield; /* Firefox */
  153. }
  154. </style>