feat(admin): add runes-based autosave to EssayForm
- Add createAutoSaveStore for edit mode - Add updatedAt tracking for conflict detection - Add hasLoaded flag to prevent autosave on initial load - Prime autosave after initial data loads - Add AutoSaveStatus indicator in header - Move draft recovery from inline to prominent banner - Only save draft on autosave failure (not every change) - Smart navigation guard (only blocks if unsaved) - Add beforeunload warning (only if unsaved changes) - Add keyboard shortcut (Cmd/Ctrl+S) - Add proper cleanup on unmount - Update clearDraft calls in restore/dismiss functions - Fix $derived syntax (use $derived.by for draftTimeText) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
32b4d16f9a
commit
c49ce5cbb5
1 changed files with 223 additions and 55 deletions
|
|
@ -1,12 +1,14 @@
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { goto } from '$app/navigation'
|
import { goto, beforeNavigate } from '$app/navigation'
|
||||||
import AdminPage from './AdminPage.svelte'
|
import AdminPage from './AdminPage.svelte'
|
||||||
import AdminSegmentedControl from './AdminSegmentedControl.svelte'
|
import AdminSegmentedControl from './AdminSegmentedControl.svelte'
|
||||||
import Editor from './Editor.svelte'
|
import Editor from './Editor.svelte'
|
||||||
import Button from './Button.svelte'
|
import Button from './Button.svelte'
|
||||||
import Input from './Input.svelte'
|
import Input from './Input.svelte'
|
||||||
import { toast } from '$lib/stores/toast'
|
import { toast } from '$lib/stores/toast'
|
||||||
import { makeDraftKey, saveDraft, loadDraft, clearDraft, timeAgo } from '$lib/admin/draftStore'
|
import { makeDraftKey, saveDraft, loadDraft, clearDraft, timeAgo } from '$lib/admin/draftStore'
|
||||||
|
import { createAutoSaveStore } from '$lib/admin/autoSave.svelte'
|
||||||
|
import AutoSaveStatus from './AutoSaveStatus.svelte'
|
||||||
import type { JSONContent } from '@tiptap/core'
|
import type { JSONContent } from '@tiptap/core'
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
|
|
@ -17,6 +19,7 @@ import { makeDraftKey, saveDraft, loadDraft, clearDraft, timeAgo } from '$lib/ad
|
||||||
content: JSONContent
|
content: JSONContent
|
||||||
tags: string[]
|
tags: string[]
|
||||||
status: 'draft' | 'published'
|
status: 'draft' | 'published'
|
||||||
|
updatedAt?: string
|
||||||
}
|
}
|
||||||
mode: 'create' | 'edit'
|
mode: 'create' | 'edit'
|
||||||
}
|
}
|
||||||
|
|
@ -25,9 +28,11 @@ import { makeDraftKey, saveDraft, loadDraft, clearDraft, timeAgo } from '$lib/ad
|
||||||
|
|
||||||
// State
|
// State
|
||||||
let isLoading = $state(false)
|
let isLoading = $state(false)
|
||||||
|
let hasLoaded = $state(mode === 'create') // Create mode loads immediately
|
||||||
let isSaving = $state(false)
|
let isSaving = $state(false)
|
||||||
let activeTab = $state('metadata')
|
let activeTab = $state('metadata')
|
||||||
let showPublishMenu = $state(false)
|
let showPublishMenu = $state(false)
|
||||||
|
let updatedAt = $state<string | undefined>(initialData?.updatedAt)
|
||||||
|
|
||||||
// Form data
|
// Form data
|
||||||
let title = $state(initialData?.title || '')
|
let title = $state(initialData?.title || '')
|
||||||
|
|
@ -38,14 +43,14 @@ import { makeDraftKey, saveDraft, loadDraft, clearDraft, timeAgo } from '$lib/ad
|
||||||
let tagInput = $state('')
|
let tagInput = $state('')
|
||||||
|
|
||||||
// Ref to the editor component
|
// Ref to the editor component
|
||||||
let editorRef: any
|
let editorRef: any
|
||||||
|
|
||||||
// Draft backup
|
// Draft backup
|
||||||
const draftKey = $derived(makeDraftKey('post', postId ?? 'new'))
|
const draftKey = $derived(makeDraftKey('post', postId ?? 'new'))
|
||||||
let showDraftPrompt = $state(false)
|
let showDraftPrompt = $state(false)
|
||||||
let draftTimestamp = $state<number | null>(null)
|
let draftTimestamp = $state<number | null>(null)
|
||||||
let timeTicker = $state(0)
|
let timeTicker = $state(0)
|
||||||
const draftTimeText = $derived(() => (draftTimestamp ? (timeTicker, timeAgo(draftTimestamp)) : null))
|
const draftTimeText = $derived.by(() => (draftTimestamp ? (timeTicker, timeAgo(draftTimestamp)) : null))
|
||||||
|
|
||||||
function buildPayload() {
|
function buildPayload() {
|
||||||
return {
|
return {
|
||||||
|
|
@ -54,10 +59,35 @@ function buildPayload() {
|
||||||
type: 'essay',
|
type: 'essay',
|
||||||
status,
|
status,
|
||||||
content,
|
content,
|
||||||
tags
|
tags,
|
||||||
|
updatedAt
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Autosave store (edit mode only)
|
||||||
|
let autoSave = mode === 'edit' && postId
|
||||||
|
? createAutoSaveStore({
|
||||||
|
debounceMs: 2000,
|
||||||
|
getPayload: () => (hasLoaded ? buildPayload() : null),
|
||||||
|
save: async (payload, { signal }) => {
|
||||||
|
const response = await fetch(`/api/posts/${postId}`, {
|
||||||
|
method: 'PUT',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify(payload),
|
||||||
|
credentials: 'same-origin',
|
||||||
|
signal
|
||||||
|
})
|
||||||
|
if (!response.ok) throw new Error('Failed to save')
|
||||||
|
return await response.json()
|
||||||
|
},
|
||||||
|
onSaved: (saved: any, { prime }) => {
|
||||||
|
updatedAt = saved.updatedAt
|
||||||
|
prime(buildPayload())
|
||||||
|
if (draftKey) clearDraft(draftKey)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
: null
|
||||||
|
|
||||||
const tabOptions = [
|
const tabOptions = [
|
||||||
{ value: 'metadata', label: 'Metadata' },
|
{ value: 'metadata', label: 'Metadata' },
|
||||||
{ value: 'content', label: 'Content' }
|
{ value: 'content', label: 'Content' }
|
||||||
|
|
@ -73,11 +103,31 @@ $effect(() => {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// Save draft when key fields change
|
// Prime autosave on initial load (edit mode only)
|
||||||
$effect(() => {
|
$effect(() => {
|
||||||
title; slug; status; content; tags
|
if (mode === 'edit' && initialData && !hasLoaded && autoSave) {
|
||||||
|
autoSave.prime(buildPayload())
|
||||||
|
hasLoaded = true
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// Trigger autosave when form data changes
|
||||||
|
$effect(() => {
|
||||||
|
title; slug; status; content; tags; activeTab
|
||||||
|
if (hasLoaded && autoSave) {
|
||||||
|
autoSave.schedule()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// Save draft only when autosave fails
|
||||||
|
$effect(() => {
|
||||||
|
if (hasLoaded && autoSave) {
|
||||||
|
const saveStatus = autoSave.status
|
||||||
|
if (saveStatus === 'error' || saveStatus === 'offline') {
|
||||||
saveDraft(draftKey, buildPayload())
|
saveDraft(draftKey, buildPayload())
|
||||||
})
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
// Show restore prompt if a draft exists
|
// Show restore prompt if a draft exists
|
||||||
$effect(() => {
|
$effect(() => {
|
||||||
|
|
@ -88,7 +138,7 @@ $effect(() => {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
function restoreDraft() {
|
function restoreDraft() {
|
||||||
const draft = loadDraft<any>(draftKey)
|
const draft = loadDraft<any>(draftKey)
|
||||||
if (!draft) return
|
if (!draft) return
|
||||||
const p = draft.payload
|
const p = draft.payload
|
||||||
|
|
@ -98,19 +148,76 @@ function restoreDraft() {
|
||||||
content = p.content ?? content
|
content = p.content ?? content
|
||||||
tags = p.tags ?? tags
|
tags = p.tags ?? tags
|
||||||
showDraftPrompt = false
|
showDraftPrompt = false
|
||||||
}
|
clearDraft(draftKey)
|
||||||
|
}
|
||||||
|
|
||||||
function dismissDraft() {
|
function dismissDraft() {
|
||||||
showDraftPrompt = false
|
showDraftPrompt = false
|
||||||
}
|
clearDraft(draftKey)
|
||||||
|
}
|
||||||
|
|
||||||
// Auto-update draft time text every minute when prompt visible
|
// Auto-update draft time text every minute when prompt visible
|
||||||
$effect(() => {
|
$effect(() => {
|
||||||
if (showDraftPrompt) {
|
if (showDraftPrompt) {
|
||||||
const id = setInterval(() => (timeTicker = timeTicker + 1), 60000)
|
const id = setInterval(() => (timeTicker = timeTicker + 1), 60000)
|
||||||
return () => clearInterval(id)
|
return () => clearInterval(id)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Navigation guard: flush autosave before navigating away (only if unsaved)
|
||||||
|
beforeNavigate(async (navigation) => {
|
||||||
|
if (hasLoaded && autoSave) {
|
||||||
|
if (autoSave.status === 'saved') {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
navigation.cancel()
|
||||||
|
try {
|
||||||
|
await autoSave.flush()
|
||||||
|
navigation.retry()
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Autosave flush failed:', error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// Warn before closing browser tab/window if there are unsaved changes
|
||||||
|
$effect(() => {
|
||||||
|
if (!hasLoaded || !autoSave) return
|
||||||
|
|
||||||
|
function handleBeforeUnload(event: BeforeUnloadEvent) {
|
||||||
|
if (autoSave!.status !== 'saved') {
|
||||||
|
event.preventDefault()
|
||||||
|
event.returnValue = ''
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
window.addEventListener('beforeunload', handleBeforeUnload)
|
||||||
|
return () => window.removeEventListener('beforeunload', handleBeforeUnload)
|
||||||
|
})
|
||||||
|
|
||||||
|
// Keyboard shortcut: Cmd/Ctrl+S to save immediately
|
||||||
|
$effect(() => {
|
||||||
|
if (!hasLoaded || !autoSave) return
|
||||||
|
|
||||||
|
function handleKeydown(e: KeyboardEvent) {
|
||||||
|
if ((e.metaKey || e.ctrlKey) && e.key.toLowerCase() === 's') {
|
||||||
|
e.preventDefault()
|
||||||
|
autoSave!.flush().catch((error) => {
|
||||||
|
console.error('Autosave flush failed:', error)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
document.addEventListener('keydown', handleKeydown)
|
||||||
|
return () => document.removeEventListener('keydown', handleKeydown)
|
||||||
|
})
|
||||||
|
|
||||||
|
// Cleanup autosave on unmount
|
||||||
|
$effect(() => {
|
||||||
|
if (autoSave) {
|
||||||
|
return () => autoSave.destroy()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
function addTag() {
|
function addTag() {
|
||||||
if (tagInput && !tags.includes(tagInput)) {
|
if (tagInput && !tags.includes(tagInput)) {
|
||||||
|
|
@ -294,16 +401,26 @@ $effect(() => {
|
||||||
</div>
|
</div>
|
||||||
{/if}
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
{#if showDraftPrompt}
|
{#if mode === 'edit' && autoSave}
|
||||||
<div class="draft-prompt">
|
<AutoSaveStatus status={autoSave.status} error={autoSave.lastError} />
|
||||||
Unsaved draft found{#if draftTimeText} (saved {draftTimeText}){/if}.
|
|
||||||
<button class="link" onclick={restoreDraft}>Restore</button>
|
|
||||||
<button class="link" onclick={dismissDraft}>Dismiss</button>
|
|
||||||
</div>
|
|
||||||
{/if}
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
</header>
|
</header>
|
||||||
|
|
||||||
|
{#if showDraftPrompt}
|
||||||
|
<div class="draft-banner">
|
||||||
|
<div class="draft-banner-content">
|
||||||
|
<span class="draft-banner-text">
|
||||||
|
Unsaved draft found{#if draftTimeText} (saved {draftTimeText}){/if}.
|
||||||
|
</span>
|
||||||
|
<div class="draft-banner-actions">
|
||||||
|
<button class="draft-banner-button" onclick={restoreDraft}>Restore</button>
|
||||||
|
<button class="draft-banner-button dismiss" onclick={dismissDraft}>Dismiss</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
|
|
||||||
<div class="admin-container">
|
<div class="admin-container">
|
||||||
{#if error}
|
{#if error}
|
||||||
<div class="error-message">{error}</div>
|
<div class="error-message">{error}</div>
|
||||||
|
|
@ -430,18 +547,69 @@ $effect(() => {
|
||||||
display: flex;
|
display: flex;
|
||||||
}
|
}
|
||||||
|
|
||||||
.draft-prompt {
|
.draft-banner {
|
||||||
margin-left: $unit-2x;
|
background: $blue-95;
|
||||||
color: $gray-40;
|
border-bottom: 1px solid $blue-80;
|
||||||
font-size: 0.75rem;
|
padding: $unit-2x $unit-5x;
|
||||||
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
|
align-items: center;
|
||||||
|
animation: slideDown 0.2s ease-out;
|
||||||
|
|
||||||
.link {
|
@keyframes slideDown {
|
||||||
background: none;
|
from {
|
||||||
|
opacity: 0;
|
||||||
|
transform: translateY(-10px);
|
||||||
|
}
|
||||||
|
to {
|
||||||
|
opacity: 1;
|
||||||
|
transform: translateY(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
.draft-banner-content {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: space-between;
|
||||||
|
gap: $unit-3x;
|
||||||
|
width: 100%;
|
||||||
|
max-width: 1200px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.draft-banner-text {
|
||||||
|
color: $blue-20;
|
||||||
|
font-size: $font-size-small;
|
||||||
|
font-weight: $font-weight-med;
|
||||||
|
}
|
||||||
|
|
||||||
|
.draft-banner-actions {
|
||||||
|
display: flex;
|
||||||
|
gap: $unit-2x;
|
||||||
|
}
|
||||||
|
|
||||||
|
.draft-banner-button {
|
||||||
|
background: $blue-50;
|
||||||
border: none;
|
border: none;
|
||||||
color: $gray-20;
|
color: $white;
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
margin-left: $unit;
|
padding: $unit-half $unit-2x;
|
||||||
padding: 0;
|
border-radius: $corner-radius-sm;
|
||||||
|
font-size: $font-size-small;
|
||||||
|
font-weight: $font-weight-med;
|
||||||
|
transition: background $transition-fast;
|
||||||
|
|
||||||
|
&:hover {
|
||||||
|
background: $blue-40;
|
||||||
|
}
|
||||||
|
|
||||||
|
&.dismiss {
|
||||||
|
background: transparent;
|
||||||
|
color: $blue-30;
|
||||||
|
|
||||||
|
&:hover {
|
||||||
|
background: $blue-90;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue