AddTextContentModal.svelte 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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" 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 bind:value={content} placeholder={$i18n.t('Write something...')} />
  64. </div>
  65. </div>
  66. </div>
  67. <div
  68. class="flex flex-row items-center justify-end text-sm font-medium flex-shrink-0 mt-1 p-4 gap-1.5"
  69. >
  70. <div class="">
  71. {#if voiceInput}
  72. <div class=" max-w-full w-64">
  73. <VoiceRecording
  74. bind:recording={voiceInput}
  75. className="p-1"
  76. on:cancel={() => {
  77. voiceInput = false;
  78. }}
  79. on:confirm={(e) => {
  80. const response = e.detail;
  81. content = `${content}${response} `;
  82. voiceInput = false;
  83. }}
  84. />
  85. </div>
  86. {:else}
  87. <Tooltip content={$i18n.t('Voice Input')}>
  88. <button
  89. class=" p-2 bg-gray-50 text-gray-700 dark:bg-gray-700 dark:text-white transition rounded-full"
  90. type="button"
  91. on:click={async () => {
  92. try {
  93. let stream = await navigator.mediaDevices
  94. .getUserMedia({ audio: true })
  95. .catch(function (err) {
  96. toast.error(
  97. $i18n.t(`Permission denied when accessing microphone: {{error}}`, {
  98. error: err
  99. })
  100. );
  101. return null;
  102. });
  103. if (stream) {
  104. voiceInput = true;
  105. const tracks = stream.getTracks();
  106. tracks.forEach((track) => track.stop());
  107. }
  108. stream = null;
  109. } catch {
  110. toast.error($i18n.t('Permission denied when accessing microphone'));
  111. }
  112. }}
  113. >
  114. <Mic className="size-5" />
  115. </button>
  116. </Tooltip>
  117. {/if}
  118. </div>
  119. <div class=" flex-shrink-0">
  120. <Tooltip content={$i18n.t('Save')}>
  121. <button
  122. class=" px-3.5 py-2 bg-black text-white dark:bg-white dark:text-black transition rounded-full"
  123. type="submit"
  124. >
  125. {$i18n.t('Save')}
  126. </button>
  127. </Tooltip>
  128. </div>
  129. </div>
  130. </form>
  131. </div>
  132. </Modal>
  133. <style>
  134. input::-webkit-outer-spin-button,
  135. input::-webkit-inner-spin-button {
  136. /* display: none; <- Crashes Chrome on hover */
  137. -webkit-appearance: none;
  138. margin: 0; /* <-- Apparently some margin are still there even though it's hidden */
  139. }
  140. .tabs::-webkit-scrollbar {
  141. display: none; /* for Chrome, Safari and Opera */
  142. }
  143. .tabs {
  144. -ms-overflow-style: none; /* IE and Edge */
  145. scrollbar-width: none; /* Firefox */
  146. }
  147. input[type='number'] {
  148. -moz-appearance: textfield; /* Firefox */
  149. }
  150. </style>