+page.svelte (1610B)
1 <script lang="ts"> 2 import ListingEditor from '../../lib/components/ListingEditor.svelte'; 3 import { page } from '$app/state'; 4 import { canonicalLocalId } from '../../lib/private-handles.ts'; 5 import type { ListingEditorRoute } from '../../lib/publishing/listing-editor.ts'; 6 // A history return can restore the sell page under its shallow draft URL. 7 // Admit that original ID on mount; the first shallow save never swaps the 8 // mounted editor's resource or DOM while the user is typing. 9 const resumed = canonicalLocalId(page.shallow?.params?.draftId); 10 const route: ListingEditorRoute = resumed 11 ? { mode: 'draft', draftId: resumed } 12 : { mode: 'new' }; 13 import { goto, beforeNavigate } from '$app/navigation'; 14 import { draftHref } from '../../lib/routes.ts'; 15 import { internalHref } from '../../lib/navigation-url.ts'; 16 let pendingCurrent: (() => boolean) | undefined; 17 beforeNavigate((navigation) => { 18 if (navigation.shallow && pendingCurrent && !pendingCurrent()) 19 navigation.cancel(); 20 }); 21 async function firstSave(id: string, current: () => boolean) { 22 if (!current()) return; 23 pendingCurrent = current; 24 try { 25 const href = internalHref(draftHref(id)); 26 if (href !== undefined) 27 await goto(href, { shallow: true, replace: true, reset: false }); 28 else throw Error('Unavailable draft route'); 29 } finally { 30 if (pendingCurrent === current) pendingCurrent = undefined; 31 } 32 } 33 async function savedClose() { 34 await goto('/selling'); 35 } 36 </script> 37 38 <ListingEditor {route} onfirstsave={firstSave} onsavedclose={savedClose} />