Page editor: save in place, unsaved-changes guard, safer block list
- 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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KgF3Kf47LjPGHKQHszpUAv
This commit is contained in:
parent
1eb58c71ba
commit
cfe4e2062d
4 changed files with 303 additions and 188 deletions
|
|
@ -1,64 +1,115 @@
|
|||
<script lang="ts">
|
||||
import type { ContentBlock, EmailBlock } from '$lib/types';
|
||||
import { blockLabels } from '$lib/admin/blockDefaults';
|
||||
|
||||
let { blocks = [], labels = blockLabels, selectedId = '', onSelect = (id: string) => {}, onDelete = (id: string) => {}, onMove = (from: number, to: number) => {} } = $props<{
|
||||
blocks: (ContentBlock | EmailBlock)[];
|
||||
labels?: Record<string, string>;
|
||||
selectedId?: string;
|
||||
onSelect?: (id: string) => void;
|
||||
onDelete?: (id: string) => void;
|
||||
onMove?: (from: number, to: number) => void;
|
||||
}>();
|
||||
|
||||
let dragIndex = $state<number | null>(null);
|
||||
let holdingId = $state('');
|
||||
let holdTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
|
||||
function startHold(id: string) {
|
||||
holdingId = id;
|
||||
holdTimer = setTimeout(() => {
|
||||
const idx = blocks.findIndex((b: ContentBlock | EmailBlock) => b.id === id);
|
||||
onDelete(id);
|
||||
onSelect(blocks[idx + 1]?.id || blocks[idx - 1]?.id || '');
|
||||
holdingId = '';
|
||||
}, 1000);
|
||||
}
|
||||
|
||||
function endHold() {
|
||||
if (holdTimer) clearTimeout(holdTimer);
|
||||
holdingId = '';
|
||||
}
|
||||
</script>
|
||||
|
||||
<div class="space-y-3">
|
||||
{#each blocks as block, i (block.id)}
|
||||
<div
|
||||
role="listitem"
|
||||
class="rounded-box border bg-base-100 p-3 shadow-sm cursor-pointer"
|
||||
class:border-primary={selectedId === block.id}
|
||||
draggable="true"
|
||||
onclick={() => onSelect(block.id)}
|
||||
ondragstart={() => (dragIndex = i)}
|
||||
ondragover={(e) => e.preventDefault()}
|
||||
ondrop={() => { if (dragIndex !== null && dragIndex !== i) onMove(dragIndex, i); dragIndex = null; }}
|
||||
>
|
||||
<div class="flex items-center gap-2">
|
||||
<span class="cursor-grab text-xl text-base-content/40">☰</span>
|
||||
<div class="flex-1 font-semibold">{labels[block.type] ?? block.type}</div>
|
||||
<button
|
||||
class="btn btn-xs btn-error btn-outline relative overflow-hidden"
|
||||
class:btn-disabled={holdingId && holdingId !== block.id}
|
||||
class:delete-holding={holdingId === block.id}
|
||||
type="button"
|
||||
onpointerdown={() => startHold(block.id)}
|
||||
onpointerup={endHold}
|
||||
onpointerleave={endHold}
|
||||
>
|
||||
<span class="delete-fill" class:animating={holdingId === block.id}></span>
|
||||
<span class="relative z-10 text-error">Delete</span>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
<script lang="ts">
|
||||
import { onDestroy } from 'svelte';
|
||||
import type { ContentBlock, EmailBlock } from '$lib/types';
|
||||
import { blockLabels } from '$lib/admin/blockDefaults';
|
||||
|
||||
let { blocks = [], labels = blockLabels, selectedId = '', onSelect = (id: string) => {}, onDelete = (id: string) => {}, onMove = (from: number, to: number) => {} } = $props<{
|
||||
blocks: (ContentBlock | EmailBlock)[];
|
||||
labels?: Record<string, string>;
|
||||
selectedId?: string;
|
||||
onSelect?: (id: string) => void;
|
||||
onDelete?: (id: string) => void;
|
||||
onMove?: (from: number, to: number) => void;
|
||||
}>();
|
||||
|
||||
let dragIndex = $state<number | null>(null);
|
||||
let dropIndex = $state<number | null>(null);
|
||||
let holdingId = $state('');
|
||||
let holdTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
|
||||
// Delete is press-and-hold (1s) so a stray click can't remove a block.
|
||||
// The parent picks the next selection.
|
||||
function startHold(id: string) {
|
||||
endHold();
|
||||
holdingId = id;
|
||||
holdTimer = setTimeout(() => {
|
||||
holdTimer = null;
|
||||
holdingId = '';
|
||||
onDelete(id);
|
||||
}, 1000);
|
||||
}
|
||||
|
||||
function endHold() {
|
||||
if (holdTimer) clearTimeout(holdTimer);
|
||||
holdTimer = null;
|
||||
holdingId = '';
|
||||
}
|
||||
|
||||
onDestroy(endHold);
|
||||
|
||||
function drop(index: number) {
|
||||
if (dragIndex !== null && dragIndex !== index) onMove(dragIndex, index);
|
||||
dragIndex = null;
|
||||
dropIndex = null;
|
||||
}
|
||||
|
||||
function moveBy(index: number, delta: number) {
|
||||
const to = index + delta;
|
||||
if (to >= 0 && to < blocks.length) onMove(index, to);
|
||||
}
|
||||
</script>
|
||||
|
||||
<ul class="space-y-2">
|
||||
{#each blocks as block, i (block.id)}
|
||||
<li
|
||||
class="flex items-center gap-2 rounded-box border bg-base-100 p-2 pl-3 shadow-sm transition-colors {selectedId === block.id ? 'border-primary ring-1 ring-primary' : 'border-base-300 hover:border-base-content/30'}"
|
||||
class:opacity-50={dragIndex === i}
|
||||
class:border-dashed={dropIndex === i && dragIndex !== i}
|
||||
draggable="true"
|
||||
ondragstart={(e) => {
|
||||
dragIndex = i;
|
||||
// Firefox won't start a drag without data.
|
||||
e.dataTransfer?.setData('text/plain', block.id);
|
||||
if (e.dataTransfer) e.dataTransfer.effectAllowed = 'move';
|
||||
}}
|
||||
ondragover={(e) => {
|
||||
e.preventDefault();
|
||||
dropIndex = i;
|
||||
}}
|
||||
ondragleave={() => dropIndex === i && (dropIndex = null)}
|
||||
ondrop={(e) => {
|
||||
e.preventDefault();
|
||||
drop(i);
|
||||
}}
|
||||
ondragend={() => {
|
||||
dragIndex = null;
|
||||
dropIndex = null;
|
||||
}}
|
||||
>
|
||||
<span class="cursor-grab select-none text-lg text-base-content/40" aria-hidden="true" title="Drag to reorder">⠿</span>
|
||||
<button
|
||||
type="button"
|
||||
class="flex-1 truncate py-1 text-left font-semibold"
|
||||
aria-current={selectedId === block.id ? 'true' : undefined}
|
||||
onclick={() => onSelect(block.id)}
|
||||
onkeydown={(e) => {
|
||||
// Keyboard reordering: Alt + ↑/↓
|
||||
if (e.altKey && (e.key === 'ArrowUp' || e.key === 'ArrowDown')) {
|
||||
e.preventDefault();
|
||||
moveBy(i, e.key === 'ArrowUp' ? -1 : 1);
|
||||
}
|
||||
}}
|
||||
>
|
||||
{labels[block.type] ?? block.type}
|
||||
</button>
|
||||
<button
|
||||
class="btn btn-xs btn-error btn-outline relative overflow-hidden"
|
||||
class:btn-disabled={holdingId && holdingId !== block.id}
|
||||
class:delete-holding={holdingId === block.id}
|
||||
type="button"
|
||||
title="Hold to delete"
|
||||
onpointerdown={() => startHold(block.id)}
|
||||
onpointerup={endHold}
|
||||
onpointerleave={endHold}
|
||||
onpointercancel={endHold}
|
||||
onkeydown={(e) => (e.key === 'Enter' || e.key === ' ') && !e.repeat && startHold(block.id)}
|
||||
onkeyup={endHold}
|
||||
>
|
||||
<span class="delete-fill" class:animating={holdingId === block.id}></span>
|
||||
<span class="relative z-10 text-error">Delete</span>
|
||||
</button>
|
||||
</li>
|
||||
{:else}
|
||||
<li class="rounded-box border border-dashed border-base-300 p-4 text-center text-sm text-base-content/60">No blocks yet — use “Add block”.</li>
|
||||
{/each}
|
||||
</ul>
|
||||
|
|
|
|||
15
app-admin/src/lib/admin/unsaved.svelte.ts
Normal file
15
app-admin/src/lib/admin/unsaved.svelte.ts
Normal file
|
|
@ -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();
|
||||
});
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue