@classic-homes/offline-svelte 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +355 -0
- package/dist/__tests__/setup.d.ts +1 -0
- package/dist/__tests__/setup.js +18 -0
- package/dist/__tests__/useFileStorage.test.d.ts +1 -0
- package/dist/__tests__/useFileStorage.test.js +139 -0
- package/dist/__tests__/useOfflineCache.test.d.ts +1 -0
- package/dist/__tests__/useOfflineCache.test.js +119 -0
- package/dist/__tests__/usePersistedForm.test.d.ts +1 -0
- package/dist/__tests__/usePersistedForm.test.js +150 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.js +36 -0
- package/dist/useDraftManager.svelte.d.ts +77 -0
- package/dist/useDraftManager.svelte.js +189 -0
- package/dist/useDraftSync.svelte.d.ts +108 -0
- package/dist/useDraftSync.svelte.js +265 -0
- package/dist/useFileStorage.svelte.d.ts +64 -0
- package/dist/useFileStorage.svelte.js +195 -0
- package/dist/useOfflineCache.svelte.d.ts +83 -0
- package/dist/useOfflineCache.svelte.js +229 -0
- package/dist/usePersistedForm.svelte.d.ts +129 -0
- package/dist/usePersistedForm.svelte.js +451 -0
- package/package.json +85 -0
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for usePersistedForm composable
|
|
3
|
+
*
|
|
4
|
+
* Note: Svelte 5 runes ($state, $effect) require the Svelte compiler context.
|
|
5
|
+
* These tests focus on the DraftStorageService used by the composable.
|
|
6
|
+
*/
|
|
7
|
+
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
|
|
8
|
+
import { initOfflineStorage } from '@classic-homes/offline-core';
|
|
9
|
+
describe('DraftStorageService integration (used by usePersistedForm)', () => {
|
|
10
|
+
let storage;
|
|
11
|
+
beforeEach(async () => {
|
|
12
|
+
storage = await initOfflineStorage({
|
|
13
|
+
preferredAdapters: ['memory'],
|
|
14
|
+
});
|
|
15
|
+
});
|
|
16
|
+
afterEach(async () => {
|
|
17
|
+
await storage.cleanup.runCleanup();
|
|
18
|
+
await storage.dispose();
|
|
19
|
+
});
|
|
20
|
+
describe('draft persistence', () => {
|
|
21
|
+
it('should save and load drafts', async () => {
|
|
22
|
+
const formData = { name: 'John', email: 'john@example.com' };
|
|
23
|
+
await storage.drafts.saveDraft('contact-form', formData);
|
|
24
|
+
const result = await storage.drafts.loadDraft('contact-form');
|
|
25
|
+
expect(result.restored).toBe(true);
|
|
26
|
+
expect(result.draft).not.toBeNull();
|
|
27
|
+
expect(result.draft.data).toEqual(formData);
|
|
28
|
+
});
|
|
29
|
+
it('should return empty result for non-existent draft', async () => {
|
|
30
|
+
const result = await storage.drafts.loadDraft('non-existent');
|
|
31
|
+
expect(result.restored).toBe(false);
|
|
32
|
+
expect(result.draft).toBeNull();
|
|
33
|
+
});
|
|
34
|
+
it('should exclude specified fields', async () => {
|
|
35
|
+
const formData = {
|
|
36
|
+
name: 'John',
|
|
37
|
+
password: 'secret123',
|
|
38
|
+
confirmPassword: 'secret123',
|
|
39
|
+
};
|
|
40
|
+
await storage.drafts.saveDraft('signup', formData, {}, {
|
|
41
|
+
excludeFields: ['password', 'confirmPassword'],
|
|
42
|
+
});
|
|
43
|
+
const result = await storage.drafts.loadDraft('signup');
|
|
44
|
+
expect(result.draft.data.name).toBe('John');
|
|
45
|
+
expect(result.draft.data.password).toBeUndefined();
|
|
46
|
+
expect(result.draft.data.confirmPassword).toBeUndefined();
|
|
47
|
+
});
|
|
48
|
+
it('should save draft with file references', async () => {
|
|
49
|
+
const formData = { title: 'Document' };
|
|
50
|
+
const fileRefs = {
|
|
51
|
+
attachments: ['file:1', 'file:2'],
|
|
52
|
+
photo: ['file:3'],
|
|
53
|
+
};
|
|
54
|
+
await storage.drafts.saveDraft('upload-form', formData, fileRefs);
|
|
55
|
+
const result = await storage.drafts.loadDraft('upload-form');
|
|
56
|
+
expect(result.draft.fileRefs).toEqual(fileRefs);
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
describe('draft clearing', () => {
|
|
60
|
+
it('should clear a draft', async () => {
|
|
61
|
+
await storage.drafts.saveDraft('form1', { value: 1 });
|
|
62
|
+
await storage.drafts.clearDraft('form1');
|
|
63
|
+
const result = await storage.drafts.loadDraft('form1');
|
|
64
|
+
expect(result.draft).toBeNull();
|
|
65
|
+
});
|
|
66
|
+
it('should clear all drafts', async () => {
|
|
67
|
+
await storage.drafts.saveDraft('form1', { v: 1 });
|
|
68
|
+
await storage.drafts.saveDraft('form2', { v: 2 });
|
|
69
|
+
const deleted = await storage.drafts.clearAllDrafts();
|
|
70
|
+
expect(deleted).toBe(2);
|
|
71
|
+
expect(await storage.drafts.hasDraft('form1')).toBe(false);
|
|
72
|
+
expect(await storage.drafts.hasDraft('form2')).toBe(false);
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
describe('draft existence check', () => {
|
|
76
|
+
it('should return true for existing draft', async () => {
|
|
77
|
+
await storage.drafts.saveDraft('form1', { value: 1 });
|
|
78
|
+
expect(await storage.drafts.hasDraft('form1')).toBe(true);
|
|
79
|
+
});
|
|
80
|
+
it('should return false for non-existent draft', async () => {
|
|
81
|
+
expect(await storage.drafts.hasDraft('non-existent')).toBe(false);
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
describe('draft listing', () => {
|
|
85
|
+
it('should list all draft form keys', async () => {
|
|
86
|
+
await storage.drafts.saveDraft('form1', { v: 1 });
|
|
87
|
+
await storage.drafts.saveDraft('form2', { v: 2 });
|
|
88
|
+
await storage.drafts.saveDraft('form3', { v: 3 });
|
|
89
|
+
const keys = await storage.drafts.listDraftFormKeys();
|
|
90
|
+
expect(keys).toHaveLength(3);
|
|
91
|
+
expect(keys).toContain('form1');
|
|
92
|
+
expect(keys).toContain('form2');
|
|
93
|
+
expect(keys).toContain('form3');
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
describe('draft info', () => {
|
|
97
|
+
it('should return draft info for existing draft', async () => {
|
|
98
|
+
vi.useFakeTimers();
|
|
99
|
+
const now = Date.now();
|
|
100
|
+
vi.setSystemTime(now);
|
|
101
|
+
await storage.drafts.saveDraft('form1', { value: 1 }, { field1: ['file:1'] }, { expirationMs: 10000 });
|
|
102
|
+
const info = await storage.drafts.getDraftInfo('form1');
|
|
103
|
+
expect(info.exists).toBe(true);
|
|
104
|
+
expect(info.timestamp).toBe(now);
|
|
105
|
+
expect(info.expiresAt).toBe(now + 10000);
|
|
106
|
+
expect(info.hasFiles).toBe(true);
|
|
107
|
+
vi.useRealTimers();
|
|
108
|
+
});
|
|
109
|
+
it('should return hasFiles false when no files', async () => {
|
|
110
|
+
await storage.drafts.saveDraft('form1', { value: 1 });
|
|
111
|
+
const info = await storage.drafts.getDraftInfo('form1');
|
|
112
|
+
expect(info.hasFiles).toBe(false);
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
describe('draft merging', () => {
|
|
116
|
+
it('should merge data into existing draft', async () => {
|
|
117
|
+
await storage.drafts.saveDraft('form1', { name: 'John', email: 'old@example.com' });
|
|
118
|
+
await storage.drafts.mergeDraft('form1', { email: 'new@example.com', phone: '123' });
|
|
119
|
+
const result = await storage.drafts.loadDraft('form1');
|
|
120
|
+
expect(result.draft.data).toEqual({
|
|
121
|
+
name: 'John',
|
|
122
|
+
email: 'new@example.com',
|
|
123
|
+
phone: '123',
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
it('should preserve file references when merging', async () => {
|
|
127
|
+
await storage.drafts.saveDraft('form1', { name: 'John' }, { docs: ['file:1'] });
|
|
128
|
+
await storage.drafts.mergeDraft('form1', { email: 'john@example.com' });
|
|
129
|
+
const result = await storage.drafts.loadDraft('form1');
|
|
130
|
+
expect(result.draft.fileRefs.docs).toEqual(['file:1']);
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
describe('file reference updates', () => {
|
|
134
|
+
it('should update file references', async () => {
|
|
135
|
+
await storage.drafts.saveDraft('form1', { title: 'Doc' }, { files: ['file:1'] });
|
|
136
|
+
const updated = await storage.drafts.updateFileRefs('form1', 'files', [
|
|
137
|
+
'file:1',
|
|
138
|
+
'file:2',
|
|
139
|
+
'file:3',
|
|
140
|
+
]);
|
|
141
|
+
expect(updated).toBe(true);
|
|
142
|
+
const result = await storage.drafts.loadDraft('form1');
|
|
143
|
+
expect(result.draft.fileRefs.files).toEqual(['file:1', 'file:2', 'file:3']);
|
|
144
|
+
});
|
|
145
|
+
it('should return false when draft does not exist', async () => {
|
|
146
|
+
const updated = await storage.drafts.updateFileRefs('non-existent', 'files', ['file:1']);
|
|
147
|
+
expect(updated).toBe(false);
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
});
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @classic-homes/offline-svelte
|
|
3
|
+
*
|
|
4
|
+
* Svelte bindings for offline persistence and caching.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```svelte
|
|
8
|
+
* <script lang="ts">
|
|
9
|
+
* import { initOfflineStorage } from '@classic-homes/offline-core';
|
|
10
|
+
* import { useFileStorage, usePersistedForm, useOfflineCache } from '@classic-homes/offline-svelte';
|
|
11
|
+
*
|
|
12
|
+
* // Initialize at app startup (e.g., in +layout.svelte)
|
|
13
|
+
* onMount(async () => {
|
|
14
|
+
* await initOfflineStorage();
|
|
15
|
+
* });
|
|
16
|
+
*
|
|
17
|
+
* // Use in components
|
|
18
|
+
* const files = useFileStorage({
|
|
19
|
+
* formKey: 'contact',
|
|
20
|
+
* fieldName: 'documents',
|
|
21
|
+
* });
|
|
22
|
+
* </script>
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export { useFileStorage, type UseFileStorageOptions, type UseFileStorageReturn, } from './useFileStorage.svelte.js';
|
|
26
|
+
export { usePersistedForm, type UsePersistedFormOptions, type UsePersistedFormReturn, type FieldErrors, } from './usePersistedForm.svelte.js';
|
|
27
|
+
export { useOfflineCache, invalidateCacheByTag, invalidateAllCache, type UseOfflineCacheOptions, type UseOfflineCacheReturn, } from './useOfflineCache.svelte.js';
|
|
28
|
+
export { useDraftManager, type UseDraftManagerOptions, type UseDraftManagerReturn, } from './useDraftManager.svelte.js';
|
|
29
|
+
export { useDraftSync, type UseDraftSyncOptions, type UseDraftSyncReturn, } from './useDraftSync.svelte.js';
|
|
30
|
+
export { initOfflineStorage, getOfflineStorage, isOfflineStorageInitialized, getOfflineStorageOrNull, type OfflineStorage, type OfflineStorageOptions, } from '@classic-homes/offline-core';
|
|
31
|
+
export type { DraftMetadata, StoredDraftV2, DraftSyncStatus, CreateDraftOptions, ListDraftsOptions, SyncAdapter, SyncConfig, SyncResult, DraftConflict, ConflictResolution, DraftPayload, RemoteDraft, } from '@classic-homes/offline-core';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @classic-homes/offline-svelte
|
|
3
|
+
*
|
|
4
|
+
* Svelte bindings for offline persistence and caching.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```svelte
|
|
8
|
+
* <script lang="ts">
|
|
9
|
+
* import { initOfflineStorage } from '@classic-homes/offline-core';
|
|
10
|
+
* import { useFileStorage, usePersistedForm, useOfflineCache } from '@classic-homes/offline-svelte';
|
|
11
|
+
*
|
|
12
|
+
* // Initialize at app startup (e.g., in +layout.svelte)
|
|
13
|
+
* onMount(async () => {
|
|
14
|
+
* await initOfflineStorage();
|
|
15
|
+
* });
|
|
16
|
+
*
|
|
17
|
+
* // Use in components
|
|
18
|
+
* const files = useFileStorage({
|
|
19
|
+
* formKey: 'contact',
|
|
20
|
+
* fieldName: 'documents',
|
|
21
|
+
* });
|
|
22
|
+
* </script>
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
// File storage composable
|
|
26
|
+
export { useFileStorage, } from './useFileStorage.svelte.js';
|
|
27
|
+
// Form persistence composable
|
|
28
|
+
export { usePersistedForm, } from './usePersistedForm.svelte.js';
|
|
29
|
+
// Cache composable
|
|
30
|
+
export { useOfflineCache, invalidateCacheByTag, invalidateAllCache, } from './useOfflineCache.svelte.js';
|
|
31
|
+
// Draft manager composable
|
|
32
|
+
export { useDraftManager, } from './useDraftManager.svelte.js';
|
|
33
|
+
// Draft sync composable
|
|
34
|
+
export { useDraftSync, } from './useDraftSync.svelte.js';
|
|
35
|
+
// Re-export core initialization for convenience
|
|
36
|
+
export { initOfflineStorage, getOfflineStorage, isOfflineStorageInitialized, getOfflineStorageOrNull, } from '@classic-homes/offline-core';
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* useDraftManager - Svelte composable for multi-draft management
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```svelte
|
|
6
|
+
* <script lang="ts">
|
|
7
|
+
* import { useDraftManager } from '@classic-homes/offline-svelte';
|
|
8
|
+
*
|
|
9
|
+
* const manager = useDraftManager({ formKey: 'contact-form' });
|
|
10
|
+
*
|
|
11
|
+
* async function handleNewDraft() {
|
|
12
|
+
* const draft = await manager.createDraft({ name: 'New Contact' });
|
|
13
|
+
* manager.selectDraft(draft.id);
|
|
14
|
+
* }
|
|
15
|
+
* </script>
|
|
16
|
+
*
|
|
17
|
+
* <ul>
|
|
18
|
+
* {#each manager.drafts as draft}
|
|
19
|
+
* <li class:selected={draft.id === manager.selectedDraftId}>
|
|
20
|
+
* <button onclick={() => manager.selectDraft(draft.id)}>
|
|
21
|
+
* {draft.name}
|
|
22
|
+
* </button>
|
|
23
|
+
* <button onclick={() => manager.deleteDraft(draft.id)}>Delete</button>
|
|
24
|
+
* </li>
|
|
25
|
+
* {/each}
|
|
26
|
+
* </ul>
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
import { type DraftMetadata, type StoredDraftV2, type CreateDraftOptions } from '@classic-homes/offline-core';
|
|
30
|
+
/**
|
|
31
|
+
* Options for useDraftManager
|
|
32
|
+
*/
|
|
33
|
+
export interface UseDraftManagerOptions {
|
|
34
|
+
/** Filter drafts by form key */
|
|
35
|
+
formKey?: string;
|
|
36
|
+
/** Auto-load drafts on initialization */
|
|
37
|
+
autoLoad?: boolean;
|
|
38
|
+
/** Sort field for draft list */
|
|
39
|
+
sortBy?: 'createdAt' | 'updatedAt' | 'name';
|
|
40
|
+
/** Sort direction */
|
|
41
|
+
sortOrder?: 'asc' | 'desc';
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Return type for useDraftManager
|
|
45
|
+
*/
|
|
46
|
+
export interface UseDraftManagerReturn {
|
|
47
|
+
/** List of draft metadata */
|
|
48
|
+
readonly drafts: DraftMetadata[];
|
|
49
|
+
/** Whether drafts are being loaded */
|
|
50
|
+
readonly isLoading: boolean;
|
|
51
|
+
/** Current error if any */
|
|
52
|
+
readonly error: string | null;
|
|
53
|
+
/** Currently selected draft ID */
|
|
54
|
+
readonly selectedDraftId: string | null;
|
|
55
|
+
/** Number of drafts */
|
|
56
|
+
readonly count: number;
|
|
57
|
+
/** Create a new draft */
|
|
58
|
+
createDraft: (options?: CreateDraftOptions) => Promise<DraftMetadata>;
|
|
59
|
+
/** Load a draft by ID */
|
|
60
|
+
loadDraft: <T = Record<string, unknown>>(draftId: string) => Promise<StoredDraftV2<T> | null>;
|
|
61
|
+
/** Rename a draft */
|
|
62
|
+
renameDraft: (draftId: string, name: string) => Promise<void>;
|
|
63
|
+
/** Delete a draft */
|
|
64
|
+
deleteDraft: (draftId: string) => Promise<void>;
|
|
65
|
+
/** Duplicate a draft */
|
|
66
|
+
duplicateDraft: (draftId: string, newName?: string) => Promise<DraftMetadata | null>;
|
|
67
|
+
/** Select a draft */
|
|
68
|
+
selectDraft: (draftId: string | null) => void;
|
|
69
|
+
/** Refresh the draft list */
|
|
70
|
+
refresh: () => Promise<void>;
|
|
71
|
+
/** Clear all drafts for the form key */
|
|
72
|
+
clearAll: () => Promise<void>;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Create a draft manager composable
|
|
76
|
+
*/
|
|
77
|
+
export declare function useDraftManager(options?: UseDraftManagerOptions): UseDraftManagerReturn;
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* useDraftManager - Svelte composable for multi-draft management
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```svelte
|
|
6
|
+
* <script lang="ts">
|
|
7
|
+
* import { useDraftManager } from '@classic-homes/offline-svelte';
|
|
8
|
+
*
|
|
9
|
+
* const manager = useDraftManager({ formKey: 'contact-form' });
|
|
10
|
+
*
|
|
11
|
+
* async function handleNewDraft() {
|
|
12
|
+
* const draft = await manager.createDraft({ name: 'New Contact' });
|
|
13
|
+
* manager.selectDraft(draft.id);
|
|
14
|
+
* }
|
|
15
|
+
* </script>
|
|
16
|
+
*
|
|
17
|
+
* <ul>
|
|
18
|
+
* {#each manager.drafts as draft}
|
|
19
|
+
* <li class:selected={draft.id === manager.selectedDraftId}>
|
|
20
|
+
* <button onclick={() => manager.selectDraft(draft.id)}>
|
|
21
|
+
* {draft.name}
|
|
22
|
+
* </button>
|
|
23
|
+
* <button onclick={() => manager.deleteDraft(draft.id)}>Delete</button>
|
|
24
|
+
* </li>
|
|
25
|
+
* {/each}
|
|
26
|
+
* </ul>
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
import { getOfflineStorage, isOfflineStorageInitialized, } from '@classic-homes/offline-core';
|
|
30
|
+
/**
|
|
31
|
+
* Create a draft manager composable
|
|
32
|
+
*/
|
|
33
|
+
export function useDraftManager(options = {}) {
|
|
34
|
+
const { formKey, autoLoad = true, sortBy = 'updatedAt', sortOrder = 'desc' } = options;
|
|
35
|
+
// Reactive state
|
|
36
|
+
let drafts = $state([]);
|
|
37
|
+
let isLoading = $state(false);
|
|
38
|
+
let error = $state(null);
|
|
39
|
+
let selectedDraftId = $state(null);
|
|
40
|
+
// Derived state
|
|
41
|
+
const count = $derived(drafts.length);
|
|
42
|
+
/**
|
|
43
|
+
* Load drafts from storage
|
|
44
|
+
*/
|
|
45
|
+
async function loadDrafts() {
|
|
46
|
+
if (!isOfflineStorageInitialized()) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
isLoading = true;
|
|
50
|
+
error = null;
|
|
51
|
+
try {
|
|
52
|
+
const storage = getOfflineStorage();
|
|
53
|
+
const listOptions = {
|
|
54
|
+
formKey,
|
|
55
|
+
sortBy,
|
|
56
|
+
sortOrder,
|
|
57
|
+
};
|
|
58
|
+
drafts = await storage.draftManager.listDrafts(listOptions);
|
|
59
|
+
}
|
|
60
|
+
catch (err) {
|
|
61
|
+
error = err instanceof Error ? err.message : 'Failed to load drafts';
|
|
62
|
+
console.error('[useDraftManager] Failed to load drafts:', err);
|
|
63
|
+
}
|
|
64
|
+
finally {
|
|
65
|
+
isLoading = false;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Create a new draft
|
|
70
|
+
*/
|
|
71
|
+
async function createDraft(createOptions = {}) {
|
|
72
|
+
if (!isOfflineStorageInitialized()) {
|
|
73
|
+
throw new Error('Offline storage not initialized');
|
|
74
|
+
}
|
|
75
|
+
const storage = getOfflineStorage();
|
|
76
|
+
const key = formKey || 'default';
|
|
77
|
+
const metadata = await storage.draftManager.createDraft(key, createOptions);
|
|
78
|
+
// Refresh the list
|
|
79
|
+
await loadDrafts();
|
|
80
|
+
return metadata;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Load a draft by ID
|
|
84
|
+
*/
|
|
85
|
+
async function loadDraft(draftId) {
|
|
86
|
+
if (!isOfflineStorageInitialized()) {
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
const storage = getOfflineStorage();
|
|
90
|
+
return storage.draftManager.getDraft(draftId);
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Rename a draft
|
|
94
|
+
*/
|
|
95
|
+
async function renameDraft(draftId, name) {
|
|
96
|
+
if (!isOfflineStorageInitialized()) {
|
|
97
|
+
throw new Error('Offline storage not initialized');
|
|
98
|
+
}
|
|
99
|
+
const storage = getOfflineStorage();
|
|
100
|
+
await storage.draftManager.updateDraftMetadata(draftId, { name });
|
|
101
|
+
// Update local state
|
|
102
|
+
drafts = drafts.map((d) => (d.id === draftId ? { ...d, name } : d));
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Delete a draft
|
|
106
|
+
*/
|
|
107
|
+
async function deleteDraft(draftId) {
|
|
108
|
+
if (!isOfflineStorageInitialized()) {
|
|
109
|
+
throw new Error('Offline storage not initialized');
|
|
110
|
+
}
|
|
111
|
+
const storage = getOfflineStorage();
|
|
112
|
+
await storage.draftManager.deleteDraft(draftId, true);
|
|
113
|
+
// Update local state
|
|
114
|
+
drafts = drafts.filter((d) => d.id !== draftId);
|
|
115
|
+
// Clear selection if deleted
|
|
116
|
+
if (selectedDraftId === draftId) {
|
|
117
|
+
selectedDraftId = null;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Duplicate a draft
|
|
122
|
+
*/
|
|
123
|
+
async function duplicateDraft(draftId, newName) {
|
|
124
|
+
if (!isOfflineStorageInitialized()) {
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
const storage = getOfflineStorage();
|
|
128
|
+
const metadata = await storage.draftManager.duplicateDraft(draftId, newName);
|
|
129
|
+
if (metadata) {
|
|
130
|
+
await loadDrafts();
|
|
131
|
+
}
|
|
132
|
+
return metadata;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Select a draft
|
|
136
|
+
*/
|
|
137
|
+
function selectDraft(draftId) {
|
|
138
|
+
selectedDraftId = draftId;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Refresh the draft list
|
|
142
|
+
*/
|
|
143
|
+
async function refresh() {
|
|
144
|
+
await loadDrafts();
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Clear all drafts for the form key
|
|
148
|
+
*/
|
|
149
|
+
async function clearAll() {
|
|
150
|
+
if (!isOfflineStorageInitialized() || !formKey) {
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
const storage = getOfflineStorage();
|
|
154
|
+
await storage.draftManager.deleteDraftsByFormKey(formKey);
|
|
155
|
+
drafts = [];
|
|
156
|
+
selectedDraftId = null;
|
|
157
|
+
}
|
|
158
|
+
// Auto-load drafts on initialization
|
|
159
|
+
$effect(() => {
|
|
160
|
+
if (autoLoad) {
|
|
161
|
+
void loadDrafts();
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
return {
|
|
165
|
+
get drafts() {
|
|
166
|
+
return drafts;
|
|
167
|
+
},
|
|
168
|
+
get isLoading() {
|
|
169
|
+
return isLoading;
|
|
170
|
+
},
|
|
171
|
+
get error() {
|
|
172
|
+
return error;
|
|
173
|
+
},
|
|
174
|
+
get selectedDraftId() {
|
|
175
|
+
return selectedDraftId;
|
|
176
|
+
},
|
|
177
|
+
get count() {
|
|
178
|
+
return count;
|
|
179
|
+
},
|
|
180
|
+
createDraft,
|
|
181
|
+
loadDraft,
|
|
182
|
+
renameDraft,
|
|
183
|
+
deleteDraft,
|
|
184
|
+
duplicateDraft,
|
|
185
|
+
selectDraft,
|
|
186
|
+
refresh,
|
|
187
|
+
clearAll,
|
|
188
|
+
};
|
|
189
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* useDraftSync - Svelte composable for cloud sync management
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```svelte
|
|
6
|
+
* <script lang="ts">
|
|
7
|
+
* import { useDraftSync } from '@classic-homes/offline-svelte';
|
|
8
|
+
*
|
|
9
|
+
* // Create a sync adapter (implement SyncAdapter interface)
|
|
10
|
+
* const adapter = createMySyncAdapter();
|
|
11
|
+
*
|
|
12
|
+
* const sync = useDraftSync({
|
|
13
|
+
* adapter,
|
|
14
|
+
* autoSync: true,
|
|
15
|
+
* });
|
|
16
|
+
* </script>
|
|
17
|
+
*
|
|
18
|
+
* <div>
|
|
19
|
+
* Status: {sync.isOnline ? 'Online' : 'Offline'}
|
|
20
|
+
* {#if sync.isSyncing}
|
|
21
|
+
* <span>Syncing...</span>
|
|
22
|
+
* {/if}
|
|
23
|
+
* {#if sync.pendingCount > 0}
|
|
24
|
+
* <span>{sync.pendingCount} pending</span>
|
|
25
|
+
* {/if}
|
|
26
|
+
* </div>
|
|
27
|
+
*
|
|
28
|
+
* {#if sync.conflicts.length > 0}
|
|
29
|
+
* <div>
|
|
30
|
+
* <h3>Conflicts</h3>
|
|
31
|
+
* {#each sync.conflicts as conflict}
|
|
32
|
+
* <div>
|
|
33
|
+
* <span>{conflict.localDraft.metadata.name}</span>
|
|
34
|
+
* <button onclick={() => sync.resolveConflict(conflict.localDraftId, 'keep-local')}>
|
|
35
|
+
* Keep Local
|
|
36
|
+
* </button>
|
|
37
|
+
* <button onclick={() => sync.resolveConflict(conflict.localDraftId, 'keep-remote')}>
|
|
38
|
+
* Keep Remote
|
|
39
|
+
* </button>
|
|
40
|
+
* </div>
|
|
41
|
+
* {/each}
|
|
42
|
+
* </div>
|
|
43
|
+
* {/if}
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
import { type SyncAdapter, type SyncResult, type DraftConflict, type ConflictResolution } from '@classic-homes/offline-core';
|
|
47
|
+
/**
|
|
48
|
+
* Options for useDraftSync
|
|
49
|
+
*/
|
|
50
|
+
export interface UseDraftSyncOptions {
|
|
51
|
+
/** Sync adapter implementation */
|
|
52
|
+
adapter: SyncAdapter;
|
|
53
|
+
/** Enable auto-sync on interval */
|
|
54
|
+
autoSync?: boolean;
|
|
55
|
+
/** Auto-sync interval in milliseconds (default: 30000) */
|
|
56
|
+
syncIntervalMs?: number;
|
|
57
|
+
/** Auto-sync when coming back online */
|
|
58
|
+
autoSyncOnReconnect?: boolean;
|
|
59
|
+
/** Callback when sync completes */
|
|
60
|
+
onSyncComplete?: (result: SyncResult) => void;
|
|
61
|
+
/** Callback when conflicts are detected */
|
|
62
|
+
onConflictDetected?: (conflicts: DraftConflict[]) => void;
|
|
63
|
+
/** Callback when online status changes */
|
|
64
|
+
onOnlineStatusChange?: (online: boolean) => void;
|
|
65
|
+
/** Callback for sync errors */
|
|
66
|
+
onSyncError?: (error: Error) => void;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Return type for useDraftSync
|
|
70
|
+
*/
|
|
71
|
+
export interface UseDraftSyncReturn {
|
|
72
|
+
/** Whether device is online */
|
|
73
|
+
readonly isOnline: boolean;
|
|
74
|
+
/** Whether sync is in progress */
|
|
75
|
+
readonly isSyncing: boolean;
|
|
76
|
+
/** Timestamp of last successful sync */
|
|
77
|
+
readonly lastSyncAt: number | null;
|
|
78
|
+
/** Current conflicts */
|
|
79
|
+
readonly conflicts: DraftConflict[];
|
|
80
|
+
/** Number of drafts pending sync */
|
|
81
|
+
readonly pendingCount: number;
|
|
82
|
+
/** Last sync result */
|
|
83
|
+
readonly lastResult: SyncResult | null;
|
|
84
|
+
/** Current error if any */
|
|
85
|
+
readonly error: string | null;
|
|
86
|
+
/** Whether auto-sync is active */
|
|
87
|
+
readonly isAutoSyncActive: boolean;
|
|
88
|
+
/** Sync all drafts */
|
|
89
|
+
syncAll: () => Promise<SyncResult>;
|
|
90
|
+
/** Sync a specific draft */
|
|
91
|
+
syncDraft: (draftId: string) => Promise<SyncResult>;
|
|
92
|
+
/** Force push a draft (overwrite remote) */
|
|
93
|
+
forcePush: (draftId: string) => Promise<void>;
|
|
94
|
+
/** Force pull a draft (overwrite local) */
|
|
95
|
+
forcePull: (draftId: string) => Promise<void>;
|
|
96
|
+
/** Resolve a conflict */
|
|
97
|
+
resolveConflict: (draftId: string, resolution: ConflictResolution) => Promise<void>;
|
|
98
|
+
/** Start auto-sync */
|
|
99
|
+
startAutoSync: () => void;
|
|
100
|
+
/** Stop auto-sync */
|
|
101
|
+
stopAutoSync: () => void;
|
|
102
|
+
/** Dispose the sync service */
|
|
103
|
+
dispose: () => void;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Create a draft sync composable
|
|
107
|
+
*/
|
|
108
|
+
export declare function useDraftSync(options: UseDraftSyncOptions): UseDraftSyncReturn;
|