Browse Source

feat: note editor

Timothy Jaeryang Baek 1 day ago
parent
commit
1d82a1c8c4

+ 157 - 0
src/lib/components/notes/NoteEditor.svelte

@@ -0,0 +1,157 @@
+<script lang="ts">
+	import { getContext, onMount } from 'svelte';
+	const i18n = getContext('i18n');
+
+	import { toast } from 'svelte-sonner';
+	import { getNoteById } from '$lib/apis/notes';
+
+	import RichTextInput from '../common/RichTextInput.svelte';
+	import Spinner from '../common/Spinner.svelte';
+	import Sparkles from '../icons/Sparkles.svelte';
+	import SparklesSolid from '../icons/SparklesSolid.svelte';
+	import Mic from '../icons/Mic.svelte';
+	import VoiceRecording from '../chat/MessageInput/VoiceRecording.svelte';
+	import Tooltip from '../common/Tooltip.svelte';
+	import { showSidebar } from '$lib/stores';
+
+	export let id: null | string = null;
+
+	let title = '';
+	let data = {
+		content: '',
+		files: []
+	};
+	let meta = null;
+	let accessControl = null;
+
+	let voiceInput = false;
+	let loading = false;
+
+	const init = async () => {
+		loading = true;
+		const res = await getNoteById(localStorage.token, id).catch((error) => {
+			toast.error(`${error}`);
+			return null;
+		});
+
+		if (res) {
+			title = res.title;
+			data = res.data;
+			meta = res.meta;
+			accessControl = res.access_control;
+		}
+
+		loading = false;
+	};
+
+	$: if (id) {
+		init();
+	}
+</script>
+
+<div class="relative flex-1 w-full h-full flex justify-center">
+	{#if loading}
+		<div class=" absolute top-0 bottom-0 left-0 right-0 flex">
+			<div class="m-auto">
+				<Spinner />
+			</div>
+		</div>
+	{/if}
+
+	<div class=" w-full flex flex-col gap-2 {loading ? 'opacity-20' : ''}">
+		<div class="shrink-0 w-full flex justify-between items-center px-4.5">
+			<div class="w-full">
+				<input
+					class="w-full text-2xl font-medium bg-transparent outline-hidden"
+					type="text"
+					bind:value={title}
+					placeholder={$i18n.t('Title')}
+					required
+				/>
+			</div>
+		</div>
+
+		<div class=" flex-1 w-full h-full overflow-auto px-4.5">
+			<RichTextInput
+				className="input-prose-sm"
+				bind:value={data.content}
+				placeholder={$i18n.t('Write something...')}
+				preserveBreaks={true}
+			/>
+		</div>
+	</div>
+</div>
+
+<div class="absolute bottom-0 left-0 right-0 p-5 max-w-full flex justify-end">
+	<div
+		class="flex gap-0.5 justify-end w-full {$showSidebar
+			? 'md:max-w-[calc(100%-260px)]'
+			: ''} max-w-full"
+	>
+		{#if voiceInput}
+			<div class="flex-1 w-full">
+				<VoiceRecording
+					bind:recording={voiceInput}
+					className="p-1 w-full max-w-full"
+					on:cancel={() => {
+						voiceInput = false;
+					}}
+					on:confirm={(e) => {
+						const { text, filename } = e.detail;
+
+						// url is hostname + /cache/audio/transcription/ + filename
+						const url = `${window.location.origin}/cache/audio/transcription/${filename}`;
+
+						// Open in new tab
+
+						if (content.trim() !== '') {
+							content = `${content}\n\n${text}\n\nRecording: ${url}\n\n`;
+						} else {
+							content = `${content}${text}\n\nRecording: ${url}\n\n`;
+						}
+
+						voiceInput = false;
+					}}
+				/>
+			</div>
+		{:else}
+			<Tooltip content={$i18n.t('Record')}>
+				<button
+					class="cursor-pointer p-2.5 flex rounded-full border border-gray-50 dark:border-none dark:bg-gray-850 hover:bg-gray-50 dark:hover:bg-gray-800 transition shadow-xl"
+					type="button"
+					on:click={async () => {
+						try {
+							let stream = await navigator.mediaDevices
+								.getUserMedia({ audio: true })
+								.catch(function (err) {
+									toast.error(
+										$i18n.t(`Permission denied when accessing microphone: {{error}}`, {
+											error: err
+										})
+									);
+									return null;
+								});
+
+							if (stream) {
+								voiceInput = true;
+								const tracks = stream.getTracks();
+								tracks.forEach((track) => track.stop());
+							}
+							stream = null;
+						} catch {
+							toast.error($i18n.t('Permission denied when accessing microphone'));
+						}
+					}}
+				>
+					<Mic className="size-4.5" />
+				</button>
+			</Tooltip>
+		{/if}
+
+		<!-- <button
+            class="cursor-pointer p-2.5 flex rounded-full hover:bg-gray-100 dark:hover:bg-gray-850 transition shadow-xl"
+        >
+            <SparklesSolid className="size-4" />
+        </button> -->
+	</div>
+</div>

+ 56 - 54
src/lib/components/notes/Notes.svelte

@@ -95,68 +95,70 @@
 		</div>
 	</DeleteConfirmDialog>
 
-	{#if Object.keys(notes).length > 0}
-		{#each Object.keys(notes) as timeRange}
-			<div class="w-full text-xs text-gray-500 dark:text-gray-500 font-medium pb-2">
-				{$i18n.t(timeRange)}
-			</div>
+	<div class="px-4.5">
+		{#if Object.keys(notes).length > 0}
+			{#each Object.keys(notes) as timeRange}
+				<div class="w-full text-xs text-gray-500 dark:text-gray-500 font-medium pb-2">
+					{$i18n.t(timeRange)}
+				</div>
 
-			<div class="mb-5 gap-2 grid @lg:grid-cols-2 @2xl:grid-cols-3">
-				{#each notes[timeRange] as note, idx (note.id)}
-					<div
-						class=" flex space-x-4 cursor-pointer w-full px-4 py-3.5 bg-gray-50 dark:bg-gray-850 dark:hover:bg-white/5 hover:bg-black/5 rounded-xl transition"
-					>
-						<div class=" flex flex-1 space-x-4 cursor-pointer w-full">
-							<a href={`/notes/${note.id}`} class="w-full -translate-y-0.5">
-								<div class=" flex-1 flex items-center gap-2 self-center">
-									<div class=" font-semibold line-clamp-1 capitalize">{note.title}</div>
-								</div>
-
-								<div class=" text-xs text-gray-500 dark:text-gray-500 line-clamp-2 pb-2">
-									{#if note.data?.content}
-										{note.data?.content}
-									{:else}
-										{$i18n.t('No content')}
-									{/if}
-								</div>
-
-								<div class=" text-xs px-0.5 w-full flex justify-between items-center">
-									<div>
-										{dayjs(note.updated_at / 1000000).fromNow()}
+				<div class="mb-5 gap-2 grid @lg:grid-cols-2 @2xl:grid-cols-3">
+					{#each notes[timeRange] as note, idx (note.id)}
+						<div
+							class=" flex space-x-4 cursor-pointer w-full px-4 py-3.5 bg-gray-50 dark:bg-gray-850 dark:hover:bg-white/5 hover:bg-black/5 rounded-xl transition"
+						>
+							<div class=" flex flex-1 space-x-4 cursor-pointer w-full">
+								<a href={`/notes/${note.id}`} class="w-full -translate-y-0.5">
+									<div class=" flex-1 flex items-center gap-2 self-center">
+										<div class=" font-semibold line-clamp-1 capitalize">{note.title}</div>
 									</div>
-									<Tooltip
-										content={note?.user?.email ?? $i18n.t('Deleted User')}
-										className="flex shrink-0"
-										placement="top-start"
-									>
-										<div class="shrink-0 text-gray-500">
-											{$i18n.t('By {{name}}', {
-												name: capitalizeFirstLetter(
-													note?.user?.name ?? note?.user?.email ?? $i18n.t('Deleted User')
-												)
-											})}
+
+									<div class=" text-xs text-gray-500 dark:text-gray-500 line-clamp-2 pb-2">
+										{#if note.data?.content}
+											{note.data?.content}
+										{:else}
+											{$i18n.t('No content')}
+										{/if}
+									</div>
+
+									<div class=" text-xs px-0.5 w-full flex justify-between items-center">
+										<div>
+											{dayjs(note.updated_at / 1000000).fromNow()}
 										</div>
-									</Tooltip>
-								</div>
-							</a>
+										<Tooltip
+											content={note?.user?.email ?? $i18n.t('Deleted User')}
+											className="flex shrink-0"
+											placement="top-start"
+										>
+											<div class="shrink-0 text-gray-500">
+												{$i18n.t('By {{name}}', {
+													name: capitalizeFirstLetter(
+														note?.user?.name ?? note?.user?.email ?? $i18n.t('Deleted User')
+													)
+												})}
+											</div>
+										</Tooltip>
+									</div>
+								</a>
+							</div>
 						</div>
-					</div>
-				{/each}
-			</div>
-		{/each}
-	{:else}
-		<div class="w-full h-full flex flex-col items-center justify-center">
-			<div class="pb-20 text-center">
-				<div class=" text-xl font-medium text-gray-400 dark:text-gray-600">
-					{$i18n.t('No Notes')}
+					{/each}
 				</div>
+			{/each}
+		{:else}
+			<div class="w-full h-full flex flex-col items-center justify-center">
+				<div class="pb-20 text-center">
+					<div class=" text-xl font-medium text-gray-400 dark:text-gray-600">
+						{$i18n.t('No Notes')}
+					</div>
 
-				<div class="mt-1 text-sm text-gray-300 dark:text-gray-700">
-					{$i18n.t('Create your first note by clicking on the plus button below.')}
+					<div class="mt-1 text-sm text-gray-300 dark:text-gray-700">
+						{$i18n.t('Create your first note by clicking on the plus button below.')}
+					</div>
 				</div>
 			</div>
-		</div>
-	{/if}
+		{/if}
+	</div>
 
 	<div class="absolute bottom-0 left-0 right-0 p-5 max-w-full flex justify-end">
 		<div class="flex gap-0.5 justify-end w-full">

+ 1 - 1
src/routes/(app)/notes/+layout.svelte

@@ -85,7 +85,7 @@
 		</div>
 	</nav>
 
-	<div class=" pb-1 px-[18px] flex-1 max-h-full overflow-y-auto @container">
+	<div class=" pb-1 flex-1 max-h-full overflow-y-auto @container">
 		<slot />
 	</div>
 </div>

+ 2 - 1
src/routes/(app)/notes/[id]/+page.svelte

@@ -1,5 +1,6 @@
 <script lang="ts">
 	import { page } from '$app/stores';
+	import NoteEditor from '$lib/components/notes/NoteEditor.svelte';
 </script>
 
-{$page.params.id}
+<NoteEditor id={$page.params.id} />