diff --git a/src/lib/components/notes/Notes.svelte b/src/lib/components/notes/Notes.svelte index 43d7bb6e36..63ec4dcd82 100644 --- a/src/lib/components/notes/Notes.svelte +++ b/src/lib/components/notes/Notes.svelte @@ -36,7 +36,7 @@ import { createNewNote, deleteNoteById, getNotes } from '$lib/apis/notes'; import { capitalizeFirstLetter, copyToClipboard, getTimeRange } from '$lib/utils'; - import { downloadPdf } from './utils'; + import { downloadPdf, createNoteHandler } from './utils'; import EllipsisHorizontal from '../icons/EllipsisHorizontal.svelte'; import DeleteConfirmDialog from '$lib/components/common/ConfirmDialog.svelte'; @@ -393,7 +393,11 @@ class="cursor-pointer p-2.5 flex rounded-full border border-gray-50 bg-white dark:border-none dark:bg-gray-850 hover:bg-gray-50 dark:hover:bg-gray-800 transition shadow-xl" type="button" on:click={async () => { - createNoteHandler(); + const res = await createNoteHandler(dayjs().format('YYYY-MM-DD')); + + if (res) { + goto(`/notes/${res.id}`); + } }} > diff --git a/src/lib/components/notes/utils.ts b/src/lib/components/notes/utils.ts index 164f98619b..5d398ebaf2 100644 --- a/src/lib/components/notes/utils.ts +++ b/src/lib/components/notes/utils.ts @@ -1,4 +1,7 @@ import DOMPurify from 'dompurify'; +import { toast } from 'svelte-sonner'; + +import { createNewNote } from '$lib/apis/notes'; export const downloadPdf = async (note) => { const [{ default: jsPDF }, { default: html2canvas }] = await Promise.all([ @@ -103,3 +106,27 @@ export const downloadPdf = async (note) => { pdf.save(`${note.title}.pdf`); }; + +export const createNoteHandler = async (title: string, content?: string) => { + // $i18n.t('New Note'), + const res = await createNewNote(localStorage.token, { + // YYYY-MM-DD + title: title, + data: { + content: { + json: null, + html: content ?? '', + md: content ?? '' + } + }, + meta: null, + access_control: {} + }).catch((error) => { + toast.error(`${error}`); + return null; + }); + + if (res) { + return res; + } +}; diff --git a/src/routes/(app)/notes/+page.svelte b/src/routes/(app)/notes/+page.svelte index 759f16e018..f23ba5e6da 100644 --- a/src/routes/(app)/notes/+page.svelte +++ b/src/routes/(app)/notes/+page.svelte @@ -8,7 +8,7 @@ import { goto } from '$app/navigation'; import { page } from '$app/stores'; - import { createNoteHandler } from './utils'; + import { createNoteHandler } from '$lib/components/notes/utils'; import UserMenu from '$lib/components/layout/Sidebar/UserMenu.svelte'; import Notes from '$lib/components/notes/Notes.svelte'; diff --git a/src/routes/(app)/notes/[id]/+page.svelte b/src/routes/(app)/notes/[id]/+page.svelte index 76e3497f6c..d17fb60a2d 100644 --- a/src/routes/(app)/notes/[id]/+page.svelte +++ b/src/routes/(app)/notes/[id]/+page.svelte @@ -6,7 +6,7 @@ import { showSidebar } from '$lib/stores'; import dayjs from '$lib/dayjs'; - import { createNoteHandler } from '../utils'; + import { createNoteHandler } from '$lib/components/notes/utils'; import NoteEditor from '$lib/components/notes/NoteEditor.svelte'; diff --git a/src/routes/(app)/notes/new/+page.svelte b/src/routes/(app)/notes/new/+page.svelte index 94e8042295..417ace1cd0 100644 --- a/src/routes/(app)/notes/new/+page.svelte +++ b/src/routes/(app)/notes/new/+page.svelte @@ -5,7 +5,7 @@ import { page } from '$app/stores'; import dayjs from '$lib/dayjs'; - import { createNoteHandler } from '../utils'; + import { createNoteHandler } from '$lib/components/notes/utils'; onMount(async () => { const title = $page.url.searchParams.get('title') ?? dayjs().format('YYYY-MM-DD'); diff --git a/src/routes/(app)/notes/utils.ts b/src/routes/(app)/notes/utils.ts deleted file mode 100644 index d963daf822..0000000000 --- a/src/routes/(app)/notes/utils.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { createNewNote } from '$lib/apis/notes'; -import { toast } from 'svelte-sonner'; - -export const createNoteHandler = async (title: string, content?: string) => { - // $i18n.t('New Note'), - const res = await createNewNote(localStorage.token, { - // YYYY-MM-DD - title: title, - data: { - content: { - json: null, - html: content ?? '', - md: content ?? '' - } - }, - meta: null, - access_control: {} - }).catch((error) => { - toast.error(`${error}`); - return null; - }); - - if (res) { - return res; - } -};