payload-live-preview 2.3.2 → 2.4.0

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.
Files changed (60) hide show
  1. package/README.md +13 -46
  2. package/dist/adapters/astro/index.js +1 -1
  3. package/dist/adapters/astro/index.js.map +1 -1
  4. package/dist/adapters/astro/middleware-entry.js +1 -1
  5. package/dist/adapters/astro/middleware-entry.js.map +1 -1
  6. package/dist/adapters/nextjs/client.d.ts +28 -0
  7. package/dist/adapters/nextjs/client.js +3 -0
  8. package/dist/adapters/nextjs/client.js.map +1 -0
  9. package/dist/adapters/nextjs/index.d.ts +34 -12
  10. package/dist/adapters/nextjs/index.js +1 -1
  11. package/dist/adapters/nextjs/index.js.map +1 -1
  12. package/dist/adapters/nuxt/client.d.ts +29 -0
  13. package/dist/adapters/nuxt/client.js +2 -0
  14. package/dist/adapters/nuxt/client.js.map +1 -0
  15. package/dist/adapters/nuxt/index.js +1 -1
  16. package/dist/adapters/nuxt/index.js.map +1 -1
  17. package/dist/adapters/nuxt/module.d.ts +23 -3
  18. package/dist/adapters/nuxt/module.js +1 -1
  19. package/dist/adapters/nuxt/module.js.map +1 -1
  20. package/dist/adapters/react/index.d.ts +2 -2
  21. package/dist/adapters/react/index.js +1 -1
  22. package/dist/adapters/react/index.js.map +1 -1
  23. package/dist/adapters/sveltekit/client.d.ts +37 -0
  24. package/dist/adapters/sveltekit/client.js +2 -0
  25. package/dist/adapters/sveltekit/client.js.map +1 -0
  26. package/dist/adapters/sveltekit/index.js +1 -1
  27. package/dist/adapters/sveltekit/index.js.map +1 -1
  28. package/dist/adapters/vue/index.d.ts +2 -2
  29. package/dist/adapters/vue/index.js +1 -1
  30. package/dist/adapters/vue/index.js.map +1 -1
  31. package/dist/client.cjs +1 -1
  32. package/dist/client.cjs.map +1 -1
  33. package/dist/client.js +1 -1
  34. package/dist/client.js.map +1 -1
  35. package/dist/core.cjs +1 -1
  36. package/dist/core.cjs.map +1 -1
  37. package/dist/core.d.cts +7 -1
  38. package/dist/core.d.ts +7 -1
  39. package/dist/core.js +1 -1
  40. package/dist/core.js.map +1 -1
  41. package/dist/{document-session-BuYuO3ae.d.ts → document-session-oK92QjES.d.ts} +7 -1
  42. package/dist/index.cjs +1 -1
  43. package/dist/index.cjs.map +1 -1
  44. package/dist/index.js +1 -1
  45. package/dist/index.js.map +1 -1
  46. package/dist/lean.cjs +1 -1
  47. package/dist/lean.cjs.map +1 -1
  48. package/dist/lean.js +1 -1
  49. package/dist/lean.js.map +1 -1
  50. package/dist/lexical.cjs.map +1 -1
  51. package/dist/lexical.js.map +1 -1
  52. package/dist/plugins.cjs +1 -1
  53. package/dist/plugins.cjs.map +1 -1
  54. package/dist/plugins.js +1 -1
  55. package/dist/plugins.js.map +1 -1
  56. package/dist/server.cjs.map +1 -1
  57. package/dist/server.js.map +1 -1
  58. package/dist/structural.cjs.map +1 -1
  59. package/dist/structural.js.map +1 -1
  60. package/package.json +23 -1
@@ -0,0 +1,37 @@
1
+ /**
2
+ * `livePreviewNavigation()` — the SvelteKit glue a preview needed in its own
3
+ * layout before 2.4: it tells the runtime when a client navigation has
4
+ * committed and lends it `invalidateAll()` for route updates. Call it from the
5
+ * script of a layout that stays mounted across the preview routes.
6
+ *
7
+ * SvelteKit's two functions are passed in rather than imported: `$app/*` exists
8
+ * only inside a Kit build, and a package that imports it has to be compiled by
9
+ * the app as a Svelte library, which would change how Vite treats every other
10
+ * import of this package. `svelte` itself is an optional peer.
11
+ */
12
+ /** The part of SvelteKit's `AfterNavigate` this reads; Kit 2 has no `shallow`. */
13
+ interface CommittedNavigation {
14
+ readonly from: unknown;
15
+ readonly type?: string;
16
+ readonly shallow?: boolean;
17
+ }
18
+ /** SvelteKit's own `afterNavigate` and `invalidateAll` from `$app/navigation`. */
19
+ interface SvelteKitNavigation {
20
+ readonly afterNavigate: (callback: (navigation: CommittedNavigation) => void) => void;
21
+ readonly invalidateAll: () => Promise<void>;
22
+ }
23
+ /**
24
+ * Must run while the component initialises, as `afterNavigate` must.
25
+ *
26
+ * ```svelte
27
+ * <script lang="ts">
28
+ * import { afterNavigate, invalidateAll } from '$app/navigation';
29
+ * import { livePreviewNavigation } from 'payload-live-preview/sveltekit/client';
30
+ *
31
+ * livePreviewNavigation({ afterNavigate, invalidateAll });
32
+ * </script>
33
+ * ```
34
+ */
35
+ declare function livePreviewNavigation({ afterNavigate, invalidateAll }: SvelteKitNavigation): void;
36
+
37
+ export { type CommittedNavigation, type SvelteKitNavigation, livePreviewNavigation };
@@ -0,0 +1,2 @@
1
+ import{onMount as e,tick as t}from"svelte";var i="__livePreviewRouteRefresh";function o({afterNavigate:o,invalidateAll:n}){o(e=>{null!==e.from&&(!0===e.shallow&&"goto"===e.type||document.dispatchEvent(new Event("payload-live-preview:navigation")))}),e(()=>function registerRouteRefresh(e){const t="undefined"==typeof window?void 0:window;return void 0===t?()=>{}:(t[i]=e,()=>{t[i]===e&&(t[i]=void 0)})}(async()=>{await n(),await t()}))}export{o as livePreviewNavigation};
2
+ //# sourceMappingURL=client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.js","names":["SLOT","livePreviewNavigation","afterNavigate","invalidateAll","navigation","from","shallow","type","document","dispatchEvent","Event","onMount","registerRouteRefresh","refresh","target","window","async","tick"],"sources":["../../../src/core/navigation-lifecycle.ts","../../../src/core/route-refresh.ts","../../../src/adapters/sveltekit/client.ts"],"sourcesContent":["/**\n * Binds the document lifecycle the runtime cannot see: a back/forward-cache\n * restore re-runs no scripts, so a client that stayed attached comes back\n * bound to a frozen document and quietly stops updating; a soft navigation\n * replaces the content the binding cache describes. Both are silent failures.\n */\n\n/** The part of the client this owner drives. */\nexport interface NavigationLifecycleTarget {\n suspend(): boolean;\n resume(): boolean;\n /** Re-scan bindings after replacing markup without crossing a navigation boundary. */\n refreshCache(): void;\n /** Re-scan and replay the last accepted document after a committed router navigation. */\n refreshAfterNavigation?(): void;\n}\n\n/** Package-owned event dispatched after a framework router commit. @internal */\nexport const NAVIGATION_COMMIT_EVENT = 'payload-live-preview:navigation';\nconst ASTRO_INITIAL_PAGE_LOAD_EVENT = 'astro:page-load';\nconst ASTRO_AFTER_SWAP_EVENT = 'astro:after-swap';\n\nexport interface NavigationLifecycleOptions {\n /** Where `pagehide`/`pageshow` fire; they exist only on the window. Defaults to `window`. */\n readonly windowTarget?: EventTarget;\n /** Event target for soft-navigation events. Defaults to `document`. */\n readonly documentTarget?: EventTarget;\n /**\n * Events after which the DOM was replaced and the current editor state must\n * be replayed, e.g. `astro:page-load`. Empty by default: only the host knows\n * which its router fires.\n */\n readonly softNavigationEvents?: readonly string[];\n}\n\n/** Returns an unbind function; calling it twice is harmless. */\nexport function bindNavigationLifecycle(\n target: NavigationLifecycleTarget,\n options: NavigationLifecycleOptions = {},\n): () => void {\n const windowTarget = options.windowTarget ?? (globalThis as { window?: EventTarget }).window;\n const documentTarget =\n options.documentTarget ?? (globalThis as { document?: EventTarget }).document;\n const softEvents = [...new Set(options.softNavigationEvents ?? [])];\n\n const onPageHide = (): void => {\n target.suspend();\n };\n const onPageShow = (event: Event): void => {\n // Only a persisted restore needs reacquiring; an ordinary load already started.\n if ((event as Event & { readonly persisted?: boolean }).persisted === true) {\n target.resume();\n }\n };\n let astroNavigationArmed = false;\n const onAstroAfterSwap = (): void => {\n astroNavigationArmed = true;\n };\n const onSoftNavigation = (event: Event): void => {\n // Astro emits page-load for the initial document too. A client-router\n // commit is the page-load preceded by after-swap, including before the\n // browser's initial `load` when a subresource is slow.\n if (event.type === ASTRO_INITIAL_PAGE_LOAD_EVENT) {\n if (!astroNavigationArmed) return;\n astroNavigationArmed = false;\n }\n if (target.refreshAfterNavigation === undefined) target.refreshCache();\n else target.refreshAfterNavigation();\n };\n\n let unbound = false;\n\n windowTarget?.addEventListener('pagehide', onPageHide);\n windowTarget?.addEventListener('pageshow', onPageShow);\n for (const name of softEvents) documentTarget?.addEventListener(name, onSoftNavigation);\n const observesAstro = softEvents.includes(ASTRO_INITIAL_PAGE_LOAD_EVENT);\n if (observesAstro) documentTarget?.addEventListener(ASTRO_AFTER_SWAP_EVENT, onAstroAfterSwap);\n\n return (): void => {\n if (unbound) return;\n unbound = true;\n windowTarget?.removeEventListener('pagehide', onPageHide);\n windowTarget?.removeEventListener('pageshow', onPageShow);\n for (const name of softEvents) documentTarget?.removeEventListener(name, onSoftNavigation);\n if (observesAstro) {\n documentTarget?.removeEventListener(ASTRO_AFTER_SWAP_EVENT, onAstroAfterSwap);\n }\n };\n}\n","/**\n * The seam between a host framework's own router and the route strategy.\n *\n * The route strategy answers a revision by fetching the route and morphing it\n * into the living page. On a page whose DOM belongs to a reconciler that is\n * working against the framework, and it was seen to break once: `removeChild`\n * on `null` inside React's commit phase, over markup this package had patched.\n * A host that re-renders the route itself does the same job without the morph\n * and without the second HTML request, so it is given the chance to.\n *\n * Why a named slot on `window` and not a reference: the runtime is an inline\n * script that builds its strategy before any application code has hydrated, so\n * there is nothing to hand a component; `window.__livePreview`, the only other\n * public surface, is created later and not at all outside a preview; and the\n * two richer extension points — plugins and events — are reachable only through\n * the programmatic client, which a page using an adapter does not have. One\n * slot, written by whoever mounts and read at the moment of a refresh, is the\n * smallest thing that survives both orders of arrival.\n */\n\n/**\n * A host's own route refresh. It must settle once the fresh markup is\n * committed: the runtime re-applies the revision on top of it, and doing that\n * against the markup being replaced would write into nodes about to be dropped.\n * A function that returns nothing is taken as already committed; the route\n * strategy says once that it could not wait (LP0810), and 3.0 requires the\n * promise (ADR 0028).\n */\nexport type RouteRefresh = () => void | Promise<void>;\n\n/** Read late, so it does not matter whether the runtime or the component came first. */\nconst SLOT = '__livePreviewRouteRefresh';\n\ntype WindowSlot = Record<string, RouteRefresh | undefined>;\n\nfunction slot(): WindowSlot | undefined {\n return typeof window === 'undefined' ? undefined : (window as unknown as WindowSlot);\n}\n\n/**\n * Lend the runtime the host's route refresh; the return value undoes it, for a\n * component's cleanup. A later registration wins, and undoing one that has\n * already been replaced does nothing — two components on one page would\n * otherwise take the slot from each other on unmount.\n */\nexport function registerRouteRefresh(refresh: RouteRefresh): () => void {\n const target = slot();\n if (target === undefined) return () => undefined;\n target[SLOT] = refresh;\n return () => {\n // Cleared rather than deleted: the slot is read through `?.` anyway, and\n // removing the key buys nothing a lint rule would not have to be argued out of.\n if (target[SLOT] === refresh) target[SLOT] = undefined;\n };\n}\n\n/** The registered refresh, or `undefined` on a page that registered none. */\nexport function readRouteRefresh(): RouteRefresh | undefined {\n return slot()?.[SLOT];\n}\n","/**\n * `livePreviewNavigation()` — the SvelteKit glue a preview needed in its own\n * layout before 2.4: it tells the runtime when a client navigation has\n * committed and lends it `invalidateAll()` for route updates. Call it from the\n * script of a layout that stays mounted across the preview routes.\n *\n * SvelteKit's two functions are passed in rather than imported: `$app/*` exists\n * only inside a Kit build, and a package that imports it has to be compiled by\n * the app as a Svelte library, which would change how Vite treats every other\n * import of this package. `svelte` itself is an optional peer.\n */\n\nimport { onMount, tick } from 'svelte';\nimport { NAVIGATION_COMMIT_EVENT } from '@core/navigation-lifecycle';\nimport { registerRouteRefresh } from '@core/route-refresh';\n\n/** The part of SvelteKit's `AfterNavigate` this reads; Kit 2 has no `shallow`. */\nexport interface CommittedNavigation {\n readonly from: unknown;\n readonly type?: string;\n readonly shallow?: boolean;\n}\n\n/** SvelteKit's own `afterNavigate` and `invalidateAll` from `$app/navigation`. */\nexport interface SvelteKitNavigation {\n readonly afterNavigate: (callback: (navigation: CommittedNavigation) => void) => void;\n readonly invalidateAll: () => Promise<void>;\n}\n\n/**\n * Must run while the component initialises, as `afterNavigate` must.\n *\n * ```svelte\n * <script lang=\"ts\">\n * import { afterNavigate, invalidateAll } from '$app/navigation';\n * import { livePreviewNavigation } from 'payload-live-preview/sveltekit/client';\n *\n * livePreviewNavigation({ afterNavigate, invalidateAll });\n * </script>\n * ```\n */\nexport function livePreviewNavigation({ afterNavigate, invalidateAll }: SvelteKitNavigation): void {\n afterNavigate((navigation) => {\n // The mount call has no `from`: startup already applies the editor's document.\n if (navigation.from === null) return;\n // A shallow `goto` (SvelteKit 3) keeps the rendered route; it only sets page state.\n if (navigation.shallow === true && navigation.type === 'goto') return;\n document.dispatchEvent(new Event(NAVIGATION_COMMIT_EVENT));\n });\n // `invalidateAll()` settles the route's loads; `tick()` the DOM they render.\n // The returned cleanup gives the registration back when the layout unmounts.\n onMount(() =>\n registerRouteRefresh(async () => {\n await invalidateAll();\n await tick();\n }),\n );\n}\n"],"mappings":"2CAkBO,ICaDA,EAAO,4BCUN,SAASC,GAAsBC,cAAEA,EAAAC,cAAeA,IACrDD,EAAeE,IAEW,OAApBA,EAAWC,QAEY,IAAvBD,EAAWE,SAAwC,SAApBF,EAAWG,MAC9CC,SAASC,cAAc,IAAIC,MF7BQ,uCEiCrCC,EAAQ,IDNH,SAASC,qBAAqBC,GACnC,MAAMC,EAVmB,oBAAXC,YAAyB,EAAaA,OAWpD,YAAe,IAAXD,EAA6B,QACjCA,EAAOd,GAAQa,EACR,KAGDC,EAAOd,KAAUa,IAASC,EAAOd,QAAQ,IAEjD,CCFIY,CAAqBI,gBACbb,UACAc,MAGZ,Q","ignoreList":[]}