Something went wrong. Try again.
A free, open-source, self-hosted place to write a novel.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401<script lang="ts"> import { enhance } from '$app/forms'; import { tick } from 'svelte'; import Swatch from '#lib/components/Swatch.svelte';
/** * The title-page facts of one project: what the library card shows, and what * the Word and EPUB exports print as the author. * * The card is the only place these values are read, so this is a dialog and not * a surface of its own. */ type Project = { id: string; title: string; subtitle: string | null; description: string | null; author: string | null; accent: string; };
let { project, accents, onclose }: { project: Project; accents: readonly { value: string; label: string }[]; onclose: () => void; } = $props();
let modal: HTMLDivElement | undefined = $state(); let titleField: HTMLInputElement | undefined = $state(); let saving = $state(false); let error = $state<string | null>(null);
/* * Focus starts on the title and returns to whatever opened the dialog. Read the * previous element once on mount: at destroy it is the dialog itself. */ $effect(() => { const previous = document.activeElement as HTMLElement | null;
void tick().then(() => titleField?.select());
return () => previous?.focus(); });
function close() { onclose(); }
/** Modal: Tab cycles inside the dialog rather than typing behind it. */ function onkeydown(event: KeyboardEvent) { if (event.key === 'Escape') { event.preventDefault(); close(); return; }
if (event.key !== 'Tab' || !modal) return;
const items = Array.from( modal.querySelectorAll<HTMLElement>( 'a[href], button:not([disabled]), input, select, textarea, [tabindex]:not([tabindex="-1"])' ) ).filter((element) => element.offsetParent !== null);
if (items.length === 0) return;
const first = items[0]; const last = items[items.length - 1];
if (event.shiftKey && document.activeElement === first) { event.preventDefault(); last.focus(); return; }
if (!event.shiftKey && document.activeElement === last) { event.preventDefault(); first.focus(); } }</script>
<svelte:window {onkeydown} />
<div class="backdrop" role="presentation" onclick={close}> <!-- svelte-ignore a11y_click_events_have_key_events --> <div class="modal" bind:this={modal} role="dialog" aria-modal="true" aria-labelledby="details-heading" tabindex="-1" onclick={(event) => event.stopPropagation()} > <div class="modal-header"> <h2 id="details-heading">Project details</h2> <button class="close-btn tap-area" type="button" aria-label="Close details" onclick={close}> × </button> </div>
<form method="POST" action="?/details" use:enhance={() => { saving = true; error = null;
return async ({ result, update }) => { if (result.type === 'success') { // The refresh carries the new values to the card before the dialog // closes, and `reset: false` keeps the typed words if it fails. await update({ reset: false }); close(); return; }
saving = false;
if (result.type === 'failure') { // The page's notice reads `message`, so value refusals travel under // `details` and are not said twice. A guard refusal has only `message`, // and the notice behind the dialog cannot be read, so it shows here. const data = result.data as { details?: unknown; message?: unknown } | undefined; const said = typeof data?.details === 'string' ? data.details : data?.message; error = typeof said === 'string' ? said : 'The details did not save.'; return; }
if (result.type === 'error') { error = 'The details did not save. Check the connection and try again.'; } }; }} > <input type="hidden" name="id" value={project.id} />
<div class="modal-body"> <!-- The maxlengths match the caps `updateProjectDetails` enforces. --> <div class="field-row"> <label class="field-label" for="details-title">Title</label> <input class="field" id="details-title" name="title" type="text" bind:this={titleField} value={project.title} maxlength="120" required /> </div>
<div class="field-row"> <label class="field-label" for="details-subtitle">Subtitle</label> <input class="field" id="details-subtitle" name="subtitle" type="text" value={project.subtitle ?? ''} maxlength="160" placeholder="Saltmarsh, book one" /> <p class="field-hint">A line under the title on the library card.</p> </div>
<div class="field-row"> <label class="field-label" for="details-author">Author</label> <input class="field" id="details-author" name="author" type="text" value={project.author ?? ''} maxlength="120" placeholder="The name you publish under" /> <p class="field-hint">Printed on the Word manuscript and the EPUB.</p> </div>
<div class="field-row"> <label class="field-label" for="details-description">Description</label> <textarea class="field" id="details-description" name="description" rows="3" maxlength="600" value={project.description ?? ''}></textarea> <p class="field-hint">A sentence or two about the project, shown on its card.</p> </div>
<fieldset class="accents"> <legend class="field-label">Card colour</legend> {#each accents as accent (accent.value)} <label class="accent"> <input type="radio" name="accent" value={accent.value} checked={project.accent === accent.value} /> <Swatch accent={accent.value} size={9} /> <span>{accent.label}</span> </label> {/each} </fieldset>
{#if error} <p class="field-error" role="alert">{error}</p> {/if} </div>
<div class="modal-footer"> <button class="btn btn-ghost" type="button" onclick={close}>Cancel</button> <button class="btn btn-primary" type="submit" disabled={saving}> {saving ? 'Saving…' : 'Save details'} </button> </div> </form> </div></div>
<style> .backdrop { position: fixed; inset: 0; z-index: var(--fw-z-modal); display: grid; place-items: center; padding: var(--dr-space-md); background: var(--fw-backdrop); }
.modal { display: flex; flex-direction: column; width: 100%; max-width: 34rem; max-height: calc(100dvh - 2rem); overflow-y: auto; background: var(--dr-surface-elevated); border: 2px solid var(--dr-border-color-container); border-radius: var(--dr-radius); box-shadow: 6px 6px 0 0 var(--dr-shadow-color), var(--dr-shadow-inner-highlight); }
.modal-header { display: flex; align-items: center; justify-content: space-between; position: sticky; top: 0; z-index: 1; padding: var(--dr-space-sm) var(--dr-space-md); border-bottom: 2px solid var(--dr-border-color-container); background: var(--dr-surface-base); }
h2 { margin: 0; font-family: var(--dr-font-heading); font-size: var(--dr-text-sm); letter-spacing: 0.08em; text-transform: uppercase; color: var(--dr-text-primary); }
.close-btn { display: grid; place-items: center; width: 24px; height: 24px; border: 1px solid var(--dr-border-color); background: var(--dr-surface-elevated); color: var(--dr-text-secondary); font-family: var(--dr-font-mono); font-size: var(--dr-text-sm); line-height: 1; cursor: pointer; transition: background-color var(--dr-transition-fast), color var(--dr-transition-fast); }
.close-btn:hover { background: var(--dr-surface-highlight); color: var(--dr-text-primary); }
.close-btn:focus-visible { outline: var(--dr-focus-ring); outline-offset: 1px; }
.modal-body { display: flex; flex-direction: column; gap: var(--dr-space-sm); padding: var(--dr-space-md); }
.field-row { display: flex; flex-direction: column; }
.field-hint { margin-top: 0.25rem; }
.accents { display: flex; flex-wrap: wrap; gap: var(--dr-space-sm); margin: var(--dr-space-xs) 0 0; padding: 0; border: 0; }
.accents legend { padding: 0; margin-bottom: 0.35rem; }
.accent { display: inline-flex; align-items: center; gap: 0.4rem; padding: 0.35rem 0.55rem; border: 1px solid var(--dr-border-color); background: var(--dr-surface-base); color: var(--dr-text-secondary); font-size: var(--dr-text-sm); cursor: pointer; }
.accent input { position: absolute; opacity: 0; pointer-events: none; }
.accent:has(input:focus-visible) { outline: var(--dr-focus-ring); outline-offset: 2px; }
/* A chosen colour reads as a key that is down, the same as a selected tab. */ .accent:has(input:checked) { border-color: var(--dr-border-color-hover); background: var(--dr-surface-highlight); color: var(--dr-text-primary); }
.field-error { margin: 0; }
.modal-footer { display: flex; align-items: center; justify-content: flex-end; position: sticky; bottom: 0; z-index: 1; gap: var(--dr-space-sm); padding: var(--dr-space-sm) var(--dr-space-md); border-top: 2px solid var(--dr-border-color-container); background: var(--dr-surface-base); }
@media (max-width: 40rem) { .backdrop { align-items: end; padding: 0; }
.modal { max-width: none; max-height: 92dvh; border-bottom: 0; border-radius: var(--dr-radius) var(--dr-radius) 0 0; } }
@media (pointer: coarse) { .accent { min-height: 44px; } }</style>