Something went wrong. Try again.
A free, open-source, self-hosted place to write a novel.
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879<script lang="ts"> import { untrack } from 'svelte'; import RichEditor from '#lib/components/RichEditor.svelte'; import { putDocument } from '#lib/save-client.ts'; import type { SaveInput } from '#lib/save-client.ts';
/** * When two copies of the note disagree, the words from the browser go into a * note version before the saved copy replaces them, so either side of the * conflict stays recoverable. */ let { noteId, title, initialBody, initialWords, initialRevision, onsaved }: { noteId: string; title: string; initialBody: unknown; initialWords: number; /** The revision the note was loaded at, which the first save must name. */ initialRevision: number; /** Reports each confirmed save, so the list can reopen the note on its newest words. */ onsaved?: (saved: { id: string; body: unknown; text: string; revision: number }) => void; } = $props();
/* * The note this editor was mounted for: it flushes its last words as it * unmounts, when the `noteId` prop already reads another note or none. */ const id = untrack(() => noteId);
let body: ReturnType<typeof RichEditor> | undefined = $state();
/** * True when this mounted editor belongs to `targetId` and still holds an * unedited empty document, so a delayed template response can safely remount it. */ export function canReplaceWithTemplate(targetId: string): boolean { return id === targetId && (body?.isCleanEmpty() ?? false); }
async function save(input: SaveInput) { const ack = await putDocument( `/api/notes/${id}`, input, 'This note is no longer here. Your words are still on screen.' );
onsaved?.({ id, body: input.body, text: input.text, revision: ack.revision });
return ack; }
async function setAside(words: { body: unknown; text: string }) { const response = await fetch(`/api/notes/${id}/aside`, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(words) }); if (!response.ok) throw new Error('The words were not set aside.'); }</script>
<RichEditor bind:this={body} backupId={`note:${id}`} {title} {initialBody} {initialWords} {initialRevision} placeholder="What does the story need you to remember?" {save} {setAside}/>