Notes.svelte 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <script>
  2. import { getContext } from 'svelte';
  3. const i18n = getContext('i18n');
  4. import RichTextInput from '../common/RichTextInput.svelte';
  5. import Spinner from '../common/Spinner.svelte';
  6. import Sparkles from '../icons/Sparkles.svelte';
  7. import SparklesSolid from '../icons/SparklesSolid.svelte';
  8. import Mic from '../icons/Mic.svelte';
  9. import VoiceRecording from '../chat/MessageInput/VoiceRecording.svelte';
  10. import Tooltip from '../common/Tooltip.svelte';
  11. import { toast } from 'svelte-sonner';
  12. let name = '';
  13. let content = '';
  14. let voiceInput = false;
  15. let loading = false;
  16. </script>
  17. <div class="relative flex-1 w-full h-full flex justify-center overflow-auto px-5">
  18. {#if loading}
  19. <div class=" absolute top-0 bottom-0 left-0 right-0 flex">
  20. <div class="m-auto">
  21. <Spinner />
  22. </div>
  23. </div>
  24. {/if}
  25. <div class=" w-full flex flex-col gap-2 {loading ? 'opacity-20' : ''}">
  26. <div class="flex-shrink-0 w-full flex justify-between items-center">
  27. <div class="w-full">
  28. <input
  29. class="w-full text-2xl font-medium bg-transparent outline-none"
  30. type="text"
  31. bind:value={name}
  32. placeholder={$i18n.t('Title')}
  33. required
  34. />
  35. </div>
  36. </div>
  37. <div class=" flex-1 w-full h-full">
  38. <RichTextInput
  39. className=" input-prose-sm"
  40. bind:value={content}
  41. placeholder={$i18n.t('Write something...')}
  42. />
  43. </div>
  44. </div>
  45. <div class="absolute bottom-0 left-0 right-0 p-5 max-w-full flex justify-end">
  46. <div class="flex gap-0.5 justify-end w-full">
  47. {#if voiceInput}
  48. <div class="flex-1 w-full">
  49. <VoiceRecording
  50. bind:recording={voiceInput}
  51. className="p-1 w-full max-w-full"
  52. on:cancel={() => {
  53. voiceInput = false;
  54. }}
  55. on:confirm={(e) => {
  56. const { text, filename } = e.detail;
  57. // url is hostname + /cache/audio/transcription/ + filename
  58. const url = `${window.location.origin}/cache/audio/transcription/${filename}`;
  59. // Open in new tab
  60. if (content.trim() !== '') {
  61. content = `${content}\n\n${text}\n\nRecording: ${url}\n\n`;
  62. } else {
  63. content = `${content}${text}\n\nRecording: ${url}\n\n`;
  64. }
  65. voiceInput = false;
  66. }}
  67. />
  68. </div>
  69. {:else}
  70. <Tooltip content={$i18n.t('Voice Input')}>
  71. <button
  72. class="cursor-pointer p-2.5 flex rounded-full hover:bg-gray-100 dark:hover:bg-gray-850 transition shadow-xl"
  73. type="button"
  74. on:click={async () => {
  75. try {
  76. let stream = await navigator.mediaDevices
  77. .getUserMedia({ audio: true })
  78. .catch(function (err) {
  79. toast.error(
  80. $i18n.t(`Permission denied when accessing microphone: {{error}}`, {
  81. error: err
  82. })
  83. );
  84. return null;
  85. });
  86. if (stream) {
  87. voiceInput = true;
  88. const tracks = stream.getTracks();
  89. tracks.forEach((track) => track.stop());
  90. }
  91. stream = null;
  92. } catch {
  93. toast.error($i18n.t('Permission denied when accessing microphone'));
  94. }
  95. }}
  96. >
  97. <Mic className="size-4" />
  98. </button>
  99. </Tooltip>
  100. {/if}
  101. <!-- <button
  102. class="cursor-pointer p-2.5 flex rounded-full hover:bg-gray-100 dark:hover:bg-gray-850 transition shadow-xl"
  103. >
  104. <SparklesSolid className="size-4" />
  105. </button> -->
  106. </div>
  107. </div>
  108. </div>