From cfe4e2062ddb48cd7b12db67b25dc58ff46fa7ec Mon Sep 17 00:00:00 2001 From: agent Date: Tue, 6 Oct 2026 10:46:40 +0000 Subject: [PATCH] Page editor: save in place, unsaved-changes guard, safer block list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Saving no longer reloads the page: a validation error (empty title, duplicate slug) used to re-render from the server and silently discard every unsaved block edit. Shows saving/saved/error feedback like the email editor, an "Unsaved changes" badge, and warns before leaving. - Status shown in the header; on a published page "Save draft" was misleading (changes go live immediately), so it now says so and the button reads "Save". Publish is a named submit (no DOM mutation). - Block list: selected block is visible again (Tailwind v4's default border colour is currentColor, so every item had a heavy black border); drag & drop works in Firefox (needs dataTransfer data) with a drop indicator; Alt+↑/↓ keyboard reorder; hold-to-delete works from the keyboard, is cancelled on pointercancel/unmount, and no longer selects the wrong neighbour afterwards. - Shared warnOnUnsavedChanges helper; the email editor uses it too (confirm() is ignored on tab close — cancel() triggers the browser prompt). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01KgF3Kf47LjPGHKQHszpUAv --- app-admin/src/lib/admin/BlockList.svelte | 179 +++++++----- app-admin/src/lib/admin/unsaved.svelte.ts | 15 + .../src/routes/emails/[key]/+page.svelte | 37 ++- .../src/routes/pages/[slug]/+page.svelte | 260 +++++++++++------- 4 files changed, 303 insertions(+), 188 deletions(-) create mode 100644 app-admin/src/lib/admin/unsaved.svelte.ts diff --git a/app-admin/src/lib/admin/BlockList.svelte b/app-admin/src/lib/admin/BlockList.svelte index 761a929..3054347 100644 --- a/app-admin/src/lib/admin/BlockList.svelte +++ b/app-admin/src/lib/admin/BlockList.svelte @@ -1,64 +1,115 @@ - - -
- {#each blocks as block, i (block.id)} -
onSelect(block.id)} - ondragstart={() => (dragIndex = i)} - ondragover={(e) => e.preventDefault()} - ondrop={() => { if (dragIndex !== null && dragIndex !== i) onMove(dragIndex, i); dragIndex = null; }} - > -
- ☰ -
{labels[block.type] ?? block.type}
- -
-
- {/each} -
+ + + diff --git a/app-admin/src/lib/admin/unsaved.svelte.ts b/app-admin/src/lib/admin/unsaved.svelte.ts new file mode 100644 index 0000000..6841fbd --- /dev/null +++ b/app-admin/src/lib/admin/unsaved.svelte.ts @@ -0,0 +1,15 @@ +import { beforeNavigate } from '$app/navigation'; + +/** + * Warn before leaving a page with unsaved edits. Call during component init. + * Form submissions are allowed through (they are the save). + */ +export function warnOnUnsavedChanges(isDirty: () => boolean) { + beforeNavigate(({ cancel, type }) => { + if (!isDirty() || type === 'form') return; + // Closing the tab/reloading: browsers only allow their own prompt, + // which `cancel()` triggers. confirm() is ignored there. + if (type === 'leave') return cancel(); + if (!confirm('You have unsaved changes. Leave anyway?')) cancel(); + }); +} diff --git a/app-admin/src/routes/emails/[key]/+page.svelte b/app-admin/src/routes/emails/[key]/+page.svelte index b429335..2cad426 100644 --- a/app-admin/src/routes/emails/[key]/+page.svelte +++ b/app-admin/src/routes/emails/[key]/+page.svelte @@ -1,9 +1,9 @@ - -Edit {page.title} | CMS - -
- - - - -
-
-

Edit page

-

Manage blocks, draft previews, and publishing.

-
-
- - - -
-
- - {#if form?.message}
{form.message}
{/if} - -
- - -
- -
- - -
-
- -{#if showPreview} -
-
-
-
- -
- -
-
-{/if} + + +Edit {title || 'page'} | CMS + + showPreview && e.key === 'Escape' && (showPreview = false)} /> + +
{ + saving = submitter?.getAttribute('value') === 'publish' ? 'publish' : 'save'; + return async ({ result, update }) => { + await update({ reset: false }); + saving = ''; + if (result.type === 'success' || result.type === 'redirect') { + // Adopt what the server stored (it normalises the slug). + slug = data.page.slug; + title = data.page.title; + savedSnapshot = serialize(); + } + }; + }} +> + + + +
+
+ ← Pages +
+

Edit page

+ {published ? 'Published' : 'Draft'} +
+

+ {published ? 'This page is live — saved changes appear on the site immediately.' : 'Not visible on the site until you publish it.'} +

+
+
+ {#if dirty}Unsaved changes{/if} + + + {#if !published} + + {/if} +
+
+ + {#if form?.message} +
{form.message}
+ {/if} + +
+ + +
+ +
+ + +
+
+ +{#if showPreview} + +{/if}