payload-live-preview 2.0.5 → 2.1.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 (116) hide show
  1. package/README.md +143 -39
  2. package/dist/adapters/astro/FragmentBridge.astro +25 -0
  3. package/dist/adapters/astro/PreviewBoundary.astro +2 -4
  4. package/dist/adapters/astro/RichText.astro +26 -5
  5. package/dist/adapters/astro/index.d.ts +41 -9
  6. package/dist/adapters/astro/index.js +1 -1
  7. package/dist/adapters/astro/index.js.map +1 -1
  8. package/dist/adapters/astro/middleware-entry.d.ts +3 -3
  9. package/dist/adapters/astro/middleware-entry.js +1 -1
  10. package/dist/adapters/astro/middleware-entry.js.map +1 -1
  11. package/dist/adapters/nextjs/index.d.ts +17 -12
  12. package/dist/adapters/nextjs/index.js +1 -1
  13. package/dist/adapters/nextjs/index.js.map +1 -1
  14. package/dist/adapters/nuxt/index.d.ts +11 -6
  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 +64 -13
  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 +11 -7
  21. package/dist/adapters/react/index.js +1 -1
  22. package/dist/adapters/react/index.js.map +1 -1
  23. package/dist/adapters/sveltekit/index.d.ts +10 -5
  24. package/dist/adapters/sveltekit/index.js +1 -1
  25. package/dist/adapters/sveltekit/index.js.map +1 -1
  26. package/dist/adapters/vue/index.d.ts +10 -7
  27. package/dist/adapters/vue/index.js +1 -1
  28. package/dist/adapters/vue/index.js.map +1 -1
  29. package/dist/annotate.d.ts +15 -2
  30. package/dist/annotate.js.map +1 -1
  31. package/dist/client.cjs +1 -1
  32. package/dist/client.cjs.map +1 -1
  33. package/dist/client.d.cts +367 -266
  34. package/dist/client.d.ts +367 -266
  35. package/dist/client.js +1 -1
  36. package/dist/client.js.map +1 -1
  37. package/dist/codegen-cli.js.map +1 -1
  38. package/dist/codegen.cjs.map +1 -1
  39. package/dist/codegen.d.cts +7 -1
  40. package/dist/codegen.d.ts +7 -1
  41. package/dist/codegen.js.map +1 -1
  42. package/dist/core.cjs +1 -1
  43. package/dist/core.cjs.map +1 -1
  44. package/dist/core.d.cts +544 -392
  45. package/dist/core.d.ts +544 -392
  46. package/dist/core.js +1 -1
  47. package/dist/core.js.map +1 -1
  48. package/dist/doctor-cli.d.ts +1 -1
  49. package/dist/doctor-cli.js +1 -1
  50. package/dist/doctor-cli.js.map +1 -1
  51. package/dist/doctor.d.ts +2 -2
  52. package/dist/doctor.js +1 -1
  53. package/dist/doctor.js.map +1 -1
  54. package/dist/{document-session-BfBfLVpg.d.ts → document-session-DWi9H3Qk.d.ts} +7 -0
  55. package/dist/{fragment-endpoint-DATKU183.d.ts → fragment-endpoint-B2qcRw5T.d.ts} +22 -7
  56. package/dist/fragment.cjs +1 -1
  57. package/dist/fragment.cjs.map +1 -1
  58. package/dist/fragment.d.cts +37 -16
  59. package/dist/fragment.d.ts +37 -16
  60. package/dist/fragment.js +1 -1
  61. package/dist/fragment.js.map +1 -1
  62. package/dist/index.cjs +1 -1
  63. package/dist/index.cjs.map +1 -1
  64. package/dist/index.d.cts +598 -400
  65. package/dist/index.d.ts +598 -400
  66. package/dist/index.js +1 -1
  67. package/dist/index.js.map +1 -1
  68. package/dist/lean.cjs +1 -1
  69. package/dist/lean.cjs.map +1 -1
  70. package/dist/lean.d.cts +5 -2
  71. package/dist/lean.d.ts +5 -2
  72. package/dist/lean.js +1 -1
  73. package/dist/lean.js.map +1 -1
  74. package/dist/lexical.cjs +1 -1
  75. package/dist/lexical.cjs.map +1 -1
  76. package/dist/lexical.d.cts +32 -11
  77. package/dist/lexical.d.ts +32 -11
  78. package/dist/lexical.js +1 -1
  79. package/dist/lexical.js.map +1 -1
  80. package/dist/{locals-CNUGYbA_.d.ts → locals-B1mF3aTb.d.ts} +1 -1
  81. package/dist/{middleware-CWObDO37.d.ts → middleware-BGobLN9P.d.ts} +11 -5
  82. package/dist/migrate.js +1 -1
  83. package/dist/migrate.js.map +1 -1
  84. package/dist/{options-uHKrDM2U.d.ts → options-Cm8fC7dj.d.ts} +31 -1
  85. package/dist/payload.cjs +1 -1
  86. package/dist/payload.cjs.map +1 -1
  87. package/dist/payload.d.cts +18 -2
  88. package/dist/payload.d.ts +18 -2
  89. package/dist/payload.js +1 -1
  90. package/dist/payload.js.map +1 -1
  91. package/dist/plugin.cjs +2 -0
  92. package/dist/plugin.cjs.map +1 -0
  93. package/dist/plugin.d.cts +30 -0
  94. package/dist/plugin.d.ts +30 -0
  95. package/dist/plugin.js +2 -0
  96. package/dist/plugin.js.map +1 -0
  97. package/dist/plugins.cjs +1 -1
  98. package/dist/plugins.cjs.map +1 -1
  99. package/dist/plugins.d.cts +102 -29
  100. package/dist/plugins.d.ts +102 -29
  101. package/dist/plugins.js +1 -1
  102. package/dist/plugins.js.map +1 -1
  103. package/dist/{probe-Bzm4fYFL.d.ts → probe-DGPDsnbj.d.ts} +6 -2
  104. package/dist/server.cjs +1 -1
  105. package/dist/server.cjs.map +1 -1
  106. package/dist/server.d.cts +43 -4
  107. package/dist/server.d.ts +43 -4
  108. package/dist/server.js +1 -1
  109. package/dist/server.js.map +1 -1
  110. package/dist/structural.cjs +2 -2
  111. package/dist/structural.cjs.map +1 -1
  112. package/dist/structural.d.cts +61 -13
  113. package/dist/structural.d.ts +61 -13
  114. package/dist/structural.js +2 -2
  115. package/dist/structural.js.map +1 -1
  116. package/package.json +35 -4
@@ -1,6 +1,6 @@
1
1
  import { ComputedRef } from 'vue';
2
- import { b as DocumentStatus, D as DocumentSessionOptions } from '../../document-session-BfBfLVpg.js';
3
- export { a as DocumentSnapshot } from '../../document-session-BfBfLVpg.js';
2
+ import { b as DocumentStatus, D as DocumentSessionOptions } from '../../document-session-DWi9H3Qk.js';
3
+ export { a as DocumentSnapshot } from '../../document-session-DWi9H3Qk.js';
4
4
 
5
5
  /**
6
6
  * The Vue composable: Payload's `useLivePreview` shape, with this package's
@@ -9,15 +9,16 @@ export { a as DocumentSnapshot } from '../../document-session-BfBfLVpg.js';
9
9
  * session that belongs to this call alone (ADR 0002).
10
10
  *
11
11
  * The same `DocumentSession` the React hook uses; only the reactivity is Vue's.
12
- * It touches no DOM: a composable re-renders your components, which is the
13
- * other side of the trade the runtime makes (docs/vue.md).
12
+ * It touches no DOM. Vue preserves component and DOM state while type, key and
13
+ * position stay stable; a remount or replacement loses the state that node or
14
+ * component held (docs/vue.md).
14
15
  *
15
16
  * `vue` is an optional peer. This entry and the Nuxt fragment renderer are the
16
17
  * only places that need it.
17
18
  */
18
19
 
19
20
  interface UseLivePreviewDocumentOptions<T> extends DocumentSessionOptions {
20
- /** The document the page was rendered from; returned until an update merges. */
21
+ /** The document this session starts from; create a new composable instance to switch documents. */
21
22
  readonly initialData: T;
22
23
  }
23
24
  /** One ref per field of the snapshot, so a template can bind them individually. */
@@ -29,6 +30,8 @@ interface LivePreviewDocumentRefs<T> {
29
30
  readonly status: ComputedRef<DocumentStatus>;
30
31
  /** Why the last update did not merge; `undefined` unless `status` is `unavailable`. */
31
32
  readonly error: ComputedRef<Error | undefined>;
33
+ /** Which of the session's messages `data` came from; compare it after `nextTick` to know it was painted. */
34
+ readonly revision: ComputedRef<number>;
32
35
  }
33
36
  /**
34
37
  * Subscribe to the admin's updates and return the merged document as refs.
@@ -36,8 +39,8 @@ interface LivePreviewDocumentRefs<T> {
36
39
  * ```vue
37
40
  * <script setup lang="ts">
38
41
  * const { data, status } = useLivePreviewDocument<Page>({
39
- * serverURL: import.meta.env.PUBLIC_PAYLOAD_URL,
40
- * allowedOrigins: [import.meta.env.PUBLIC_PAYLOAD_URL],
42
+ * serverURL: import.meta.env.VITE_PAYLOAD_URL,
43
+ * allowedOrigins: [import.meta.env.VITE_PAYLOAD_ADMIN_ORIGIN],
41
44
  * initialData: page,
42
45
  * depth: 1,
43
46
  * });
@@ -1,2 +1,2 @@
1
- import{getCurrentScope as t,shallowRef as e,onScopeDispose as i,computed as n}from"vue";function s(t){return("object"==typeof t&&null!==t||"function"==typeof t)&&"function"==typeof t.then&&(Promise.resolve(t).catch(()=>{}),!0)}function r(){}function o(t,e){return t.length>0&&t.length<=e&&"."!==t&&".."!==t&&!t.includes("/")&&!t.includes("\\")&&!function(t){for(const e of t){const t=e.codePointAt(0)??0;if(t<=31||127===t)return!0}return!1}(t)}function a(t){return o(t,128)&&!t.includes("?")&&!t.includes("#")}var h=class{serverURL;apiRoute;depth;fetchFn;log;inflight=null;attempt=0;destroyDepth=0;constructor(t){this.serverURL=function(t){let e=t.length;for(;t.endsWith("/",e);)e-=1;return t.slice(0,e)}(t.serverURL);const e=t.apiRoute??"/api";var i;this.apiRoute=e.startsWith("/")?e:`/${e}`,this.depth=t.depth??1,this.fetchFn=t.fetchFn,this.log=(i=t.log??r,(...t)=>{try{s(i(...t))}catch{}})}canMerge(t){return null!==this.endpointOf(t)}endpointOf(t){try{const i=t.globalSlug;if(i)return a(i)?`globals/${encodeURIComponent(i)}`:null;const n=t.collectionSlug??"",s=t.data.id;return a(n)&&("number"==typeof(e=s)?Number.isFinite(e):"string"==typeof e&&o(e,512))?`${encodeURIComponent(n)}/${encodeURIComponent(String(s))}`:null}catch{return null}var e}async merge(t){if(this.destroyDepth>0)return{status:"superseded"};const e=this.attempt+=1,i=this.inflight;this.inflight=null;let n=null;const s=()=>this.attempt!==e;try{if(i?.abort(),s())return{status:"superseded"};const e=this.endpointOf(t);if(null===e)return s()?{status:"superseded"}:{status:"unavailable"};const r=this.fetchFn??("function"==typeof fetch?fetch:void 0);if(void 0===r)return{status:"unavailable"};n=new AbortController,this.inflight=n;const o=`${this.serverURL}${this.apiRoute}/${e}`,a=await r(o,{method:"POST",credentials:"include",headers:{"Content-Type":"application/json","X-Payload-HTTP-Method-Override":"GET"},body:JSON.stringify({data:t.data,depth:this.depth,flattenLocales:!1,locale:t.locale}),signal:n.signal});if(s())return{status:"superseded"};if(!a.ok)return this.log("merge HTTP",a.status,o),{status:"unavailable"};const h=await a.json();return s()?{status:"superseded"}:null===h||"object"!=typeof h||Array.isArray(h)?(this.log("merge invalid",o),{status:"unavailable"}):{status:"merged",doc:h}}catch(t){return s()?{status:"superseded"}:(this.log("merge exception",t),{status:"unavailable"})}finally{this.inflight===n&&(this.inflight=null)}}destroy(){this.attempt+=1;const t=this.inflight;this.inflight=null,this.destroyDepth+=1;try{t?.abort()}finally{this.destroyDepth-=1}}};function d(t){if(!Array.isArray(t))return[];const e=[];for(const i of t){const t=l(i);t&&e.push(t)}return e}function l(t){if("object"!=typeof t||null===t)return;const e=t,i=e.name,n=e.type;if("string"!=typeof i||0===i.length)return;if("string"!=typeof n||0===n.length)return;const s=d(e.fields),r=function(t){if(!Array.isArray(t))return[];const e=[];for(const i of t){if("object"!=typeof i||null===i)continue;const t=i,n=t.slug;if("string"!=typeof n||0===n.length)continue;const s=d(t.fields);e.push({...t,slug:n,fields:s})}return e}(e.blocks),o={...e,name:i,type:n};return s.length>0?o.fields=s:delete o.fields,r.length>0?o.blocks=r:delete o.blocks,o}function u(t,e){return void 0===t||e(t)}function c(t,e){return null==t||e(t)}function f(t){if("object"!=typeof t||null===t)return!1;const e=Object.getPrototypeOf(t);return null===e||null===Object.getPrototypeOf(e)}var p=["data","fieldSchemaJSON","globalSlug","collectionSlug","locale","ready","previewToken","protocolVersion"],g=class{matcher;handlers;boundListener=void 0;attachedTarget=void 0;generation=0;revision=0;queueHead=void 0;queueTail=void 0;constructor(t,e){this.matcher=t,this.handlers=e}attach(t=window){if(void 0!==this.attachedTarget)return;let e=!1;const i=n=>{if(!e||this.boundListener!==i)return;const s=this.generation;try{this.receive(n,s,t)}catch{let t;try{t=n.origin}catch{t=""}this.reportInvalid("shape",t,s)}};this.generation+=1,this.resetQueue(),this.attachedTarget=t,this.boundListener=i;try{t.addEventListener("message",i)}catch(e){this.boundListener===i&&(this.attachedTarget=void 0,this.boundListener=void 0,this.resetQueue());try{t.removeEventListener("message",i)}catch{}throw e}if(this.boundListener===i)e=!0;else try{t.removeEventListener("message",i)}catch{}}detach(){const t=this.attachedTarget,e=this.boundListener;void 0!==t&&void 0!==e&&(this.attachedTarget=void 0,this.boundListener=void 0,this.resetQueue(),t.removeEventListener("message",e))}advanceGeneration(){return void 0!==this.attachedTarget&&(this.generation+=1,this.resetQueue(),!0)}static sendReady(t,e){const i={type:"payload-live-preview",ready:!0,protocolVersion:4};for(const n of t)for(const t of e)try{n.postMessage(i,t)}catch{}}receive(t,e,i){const n=t.origin;if(!this.matchesOrigin(n,e))return void this.reportInvalid("origin",n,e);if(!this.matchesSource(t,i))return void this.reportInvalid("source",n,e);const s=t.data;var r;if("object"==typeof(r=s)&&null!==r&&"type"in r&&"string"==typeof r.type)switch(s.type){case"payload-live-preview":return function(t){const e=t;return!(void 0!==e.data&&null!==e.data&&!f(e.data))&&c(e.fieldSchemaJSON,t=>Array.isArray(t))&&c(e.globalSlug,t=>"string"==typeof t)&&c(e.collectionSlug,t=>"string"==typeof t)&&c(e.locale,t=>"string"==typeof t)&&c(e.ready,t=>"boolean"==typeof t)&&c(e.previewToken,t=>"string"==typeof t)&&c(e.protocolVersion,t=>"number"==typeof t)}(s)?void this.dispatchUpdate(function(t){const e={...t};for(const t of p)null===Reflect.get(e,t)&&Reflect.deleteProperty(e,t);if(!Array.isArray(e.fieldSchemaJSON))return e;let i;try{i=d(e.fieldSchemaJSON)}catch{i=[]}return{...e,fieldSchemaJSON:i}}(s),n,e):void this.reportInvalid("shape",n,e);case"payload-document-event":return function(t){const e=t;return u(e.action,t=>"updated"===t||"created"===t||"deleted"===t)&&u(e.slug,t=>"string"==typeof t)&&u(e.id,t=>"string"==typeof t||Number.isFinite(t))}(s)?void this.invokeHandler(e,this.handlers.onDocumentEvent,s,n):void this.reportInvalid("shape",n,e);case"payload-live-preview-focus":{const t=function(t){const e=t.field;return"string"==typeof e&&e.length>0?e:void 0}(s);return void 0===t?void this.reportInvalid("shape",n,e):void this.invokeHandler(e,this.handlers.onFocusField,t,n)}default:this.reportInvalid("type",n,e)}else this.reportInvalid("shape",n,e)}dispatchUpdate(t,e,i){const n=f(t.data);if(!this.isCurrentGeneration(i))return;const s=n?{generation:i,revision:this.revision+=1}:void 0,r=this.handlers.validateToken;if(!this.isCurrentGeneration(i))return;if(void 0===r)return void this.commitUpdate(t,e,i,s);if(!0===t.ready&&void 0===t.data)return void this.commitUpdate(t,e,i,void 0);const o={generation:i,message:t,origin:e,messageRevision:s,verdict:void 0,next:void 0};let a;this.enqueue(o);try{a=r(t.previewToken,e)}catch{return void this.settleValidation(o,!1)}"boolean"!=typeof a?Promise.resolve(a).then(t=>{this.settleValidation(o,!0===t)},()=>{this.settleValidation(o,!1)}):this.settleValidation(o,a)}settleValidation(t,e){t.verdict=e,this.drainQueue(t.generation)}drainQueue(t){for(;this.isCurrentGeneration(t);){const e=this.queueHead;if(void 0===e)return;if(this.matchesOrigin(e.origin,t)){if(void 0===e.verdict)return;this.dequeue(e)&&(e.verdict?this.commitUpdate(e.message,e.origin,e.generation,e.messageRevision):this.reportInvalid("token",e.origin,e.generation))}else{if(!this.dequeue(e))continue;this.reportInvalid("origin",e.origin,e.generation)}}}commitUpdate(t,e,i,n){void 0===n?this.invokeHandler(i,this.handlers.onUpdate,t,e):this.invokeHandler(i,this.handlers.onUpdate,t,e,n)}reportInvalid(t,e,i){this.invokeHandler(i,this.handlers.onInvalid,t,e)}matchesSource(t,e){if("parent-or-opener"!==this.handlers.sourcePolicy)return!0;try{const i=t.source;if("object"!=typeof i||null===i)return!1;const n=e.parent;return n!==e&&i===n||i===e.opener}catch{return!1}}matchesOrigin(t,e){try{return!0===(this.isCurrentGeneration(e)&&this.matcher(t))&&this.isCurrentGeneration(e)}catch{return!1}}invokeHandler(t,e,...i){if(void 0!==e&&this.isCurrentGeneration(t))try{s(e(...i))}catch{}}isCurrentGeneration(t){return void 0!==this.attachedTarget&&this.generation===t}enqueue(t){void 0===this.queueTail?this.queueHead=t:this.queueTail.next=t,this.queueTail=t}dequeue(t){return this.queueHead===t&&(this.queueHead=t.next,void 0===this.queueHead&&(this.queueTail=void 0),!0)}resetQueue(){this.queueHead=void 0,this.queueTail=void 0}};var v=/^https?:\/\/(localhost|127\.0\.0\.1)(?::\d+)?$/i,y=/^https?:\/\/[^/?#\s]+$/i,m=[3e3,3001,3333,4e3,4321,5e3,5173,5174,8e3,8080,8888,9e3],OriginDetector=class{#t;constructor(t={}){const e=/* @__PURE__ */new Set;for(const i of t.additionalOrigins??[]){const t=b(i);void 0!==t&&e.add(t)}let i;if(t.enableReferrerDetection??1){const e=t.referrer??("undefined"!=typeof document?document.referrer:"");if(e.length>0)try{const t=new URL(e).origin;t&&"null"!==t&&(i=t)}catch{}}const n=(t.enableLocalhostMatching??!0)&&(t.forceDevMode??function isDevMode(){const t=function(){if("undefined"==typeof process)return;const t=process.env.NODE_ENV;return"string"==typeof t?t:void 0}();if(void 0!==t)return"production"!==t;if("undefined"!=typeof window){const t=window.location.hostname;return"localhost"===t||"127.0.0.1"===t}return!1}());this.#t=[e,i,n,void 0!==i,void 0]}matches(t){return 0!==t.length&&"null"!==t&&(void 0!==this.#t[4]?t===this.#t[4]:!!this.#t[0].has(t)||0===this.#t[0].size&&void 0!==this.#t[1]&&t===this.#t[1]||!(!this.#t[2]||!v.test(t)))}lockOrigin(t){return!!this.matches(t)&&(this.#t[4]=t,!0)}unlockOrigin(){const t=this.#t[4];return this.#t[4]=void 0,t}get lockedOrigin(){return this.#t[4]}enumerate(){if(void 0!==this.#t[4])return[this.#t[4]];const t=new Set(this.#t[0]);if(0===this.#t[0].size&&void 0!==this.#t[1]&&t.add(this.#t[1]),this.#t[2])for(const e of m)t.add(`http://localhost:${String(e)}`),t.add(`http://127.0.0.1:${String(e)}`);return[...t]}get referrerWasAvailable(){return this.#t[3]}get isProductionUnconfigured(){return!(!function isInIframe(){if("undefined"==typeof window)return!1;try{return window.self!==window.top}catch{return!0}}()||this.#t[0].size>0||this.#t[2]||void 0!==this.#t[1])}get isReferrerOnlyTrust(){return!(this.#t[0].size>0||this.#t[2]||void 0===this.#t[1])}};function b(t){const e=t.trim();if(0!==e.length)try{const t=new URL(e);if("http:"!==t.protocol&&"https:"!==t.protocol)return;if(!y.test(t.origin))return;return t.origin}catch{return}}function w(t){const[e,i]=t;return"merge HTTP"===e?`the server answered ${String(i)}`:"merge invalid"===e?"the server answered with something other than a document":"merge exception"===e?i instanceof Error?i.message:"the request failed":"the update could not be merged"}var S=class{#e;#i;#n;#s;#r=/* @__PURE__ */new Set;#o;#a;#h=0;#d=[];#l=!1;constructor(t,e){this.#n=new OriginDetector({...void 0!==e.allowedOrigins?{additionalOrigins:e.allowedOrigins}:{},enableReferrerDetection:e.enableReferrerDetection??!1,...void 0!==e.enableLocalhostMatching?{enableLocalhostMatching:e.enableLocalhostMatching}:{}}),this.#e=new h({serverURL:e.serverURL,...void 0!==e.apiRoute?{apiRoute:e.apiRoute}:{},...void 0!==e.depth?{depth:e.depth}:{},...void 0!==e.fetchFn?{fetchFn:e.fetchFn}:{},log:(...t)=>{this.#d=t}}),this.#i=new g(t=>this.#n.matches(t),{onUpdate:t=>{this.#u(t)},onDocumentEvent:()=>{},sourcePolicy:e.eventSourcePolicy??"parent-or-opener"}),this.#s=e.target,this.#o={data:t,isLoading:!0,status:"idle",error:void 0},this.#a=this.#o}subscribe=t=>(this.#r.add(t),this.#l||this.#c(),()=>{this.#r.delete(t),0===this.#r.size&&this.#f()});getSnapshot=()=>this.#a;getServerSnapshot=()=>this.#o;#c(){"undefined"==typeof window&&void 0===this.#s||(this.#l=!0,this.#i.attach(this.#s??window),g.sendReady(this.#p(),this.#n.enumerate()))}#f(){this.#l&&(this.#l=!1,this.#i.detach(),this.#e.destroy())}#p(){const t=this.#s??("undefined"==typeof window?void 0:window);if(void 0===t)return[];const e=[];t.parent!==t&&e.push(t.parent);const i=t.opener;return null!=i&&i!==t&&e.push(i),e}async#u(t){const e=t.data;if(void 0===e)return;const i=this.#h+=1;this.#g({...this.#a,isLoading:!0}),this.#d=[];const n=await this.#e.merge({data:e,...void 0!==t.collectionSlug?{collectionSlug:t.collectionSlug}:{},...void 0!==t.globalSlug?{globalSlug:t.globalSlug}:{},...void 0!==t.locale?{locale:t.locale}:{}});i===this.#h&&"superseded"!==n.status&&("unavailable"!==n.status?this.#g({data:n.doc,isLoading:!1,status:"live",error:void 0}):this.#g({...this.#a,isLoading:!1,status:"unavailable",error:new Error(`live preview: ${w(this.#d)}`)}))}#g(t){const e=this.#a;if(e.data!==t.data||e.isLoading!==t.isLoading||e.status!==t.status||e.error!==t.error){this.#a=t;for(const t of[...this.#r])t()}}};function L(s){if(void 0===t())throw new Error("payload-live-preview: useLivePreviewDocument() needs an effect scope — call it from setup() or inside effectScope(), so its window listener and any request in flight are released with the component.");const{initialData:r,...o}=s,a=new S(r,o),h=e(a.getSnapshot());return i(a.subscribe(()=>{h.value=a.getSnapshot()})),{data:n(()=>h.value.data),isLoading:n(()=>h.value.isLoading),status:n(()=>h.value.status),error:n(()=>h.value.error)}}export{L as useLivePreviewDocument};
1
+ import{getCurrentScope as e,shallowRef as t,onScopeDispose as i,computed as n}from"vue";function s(e){return("object"==typeof e&&null!==e||"function"==typeof e)&&"function"==typeof e.then&&(Promise.resolve(e).catch(()=>{}),!0)}function r(){}function o(e){if(++e.nodes>1e4)throw new Error("invalid or oversized field schema")}function a(e,t,i){if(o(t),i>64||t.active.has(e))throw new Error("invalid or oversized field schema");t.active.add(e)}function c(e){try{return l(e,{nodes:0,active:/* @__PURE__ */new WeakSet},0)}catch{return[]}}function l(e,t,i){if(!Array.isArray(e))return[];a(e,t,i);const n=[];for(const s of e){const e=d(s,t,i);e&&n.push(e)}return t.active.delete(e),n}function d(e,t,i){if(o(t),"object"!=typeof e||null===e)return;const n=e,s=n.name,r=n.type;if("string"!=typeof s||0===s.length)return;if("string"!=typeof r||0===r.length)return;const c=l(n.fields,t,i+1),d=function(e,t,i){if("object"==typeof e&&null!==e&&!Array.isArray(e)){a(e,t,i);const n=[];for(const s in e){if(!Object.hasOwn(e,s))continue;o(t);const r=Reflect.get(e,s);"object"!=typeof r||null===r||["__proto__","constructor","prototype"].includes(s)||n.push({...r,slug:s,fields:l(r.fields,t,i+1)})}return t.active.delete(e),n}if(!Array.isArray(e))return[];a(e,t,i);const n=[];for(const s of e){if(o(t),"object"!=typeof s||null===s)continue;const e=s,r=e.slug;if("string"!=typeof r||0===r.length)continue;const a=l(e.fields,t,i+1);n.push({...e,slug:r,fields:a})}return t.active.delete(e),n}(n.blocks,t,i+1),u={...n,name:s,type:r},h=n.relationTo;if(Array.isArray(h)){const e=[];for(const i of h)o(t),e.push(i);u.relationTo=e}return c.length>0?u.fields=c:delete u.fields,d.length>0?u.blocks=d:delete u.blocks,u}function u(e,t){return void 0===e||t(e)}function h(e,t){return null==e||t(e)}function f(e){if("object"!=typeof e||null===e)return!1;const t=Object.getPrototypeOf(e);return null===t||null===Object.getPrototypeOf(t)}var p=["data","fieldSchemaJSON","globalSlug","collectionSlug","locale","ready","previewToken","protocolVersion"],v=1e4,g=1048576,y=/* @__PURE__ */new Set(["__proto__","constructor","prototype"]);function m(){throw new Error("invalid or oversized legacy population")}function b(e){for(let t=0;t<e.length;t+=1){const i=e.charCodeAt(t);if(i<=31||127===i)return!0}return!1}function w(e,t){if("string"!=typeof e||0===e.length||e.length>t||"."===e||".."===e)return!1;if(e.includes("/")||e.includes("\\")||b(e))return!1;try{return encodeURIComponent(e),!0}catch{return!1}}function S(e){return w(e,128)&&!/[?#%]/u.test(e)&&!y.has(e)}function A(e){return Number.isFinite(e)?String(e):w(e,512)?e:m()}function L(e,t){return Object.hasOwn(e,t)?Reflect.get(e,t):void 0}function O(e,t,i){Object.defineProperty(e,t,{value:i,writable:!0,configurable:!0,enumerable:!0})}function T(e){let t=e.length;for(;t>0&&47===e.charCodeAt(t-1);)t-=1;return e.slice(0,t)}function R(e){const t=/* @__PURE__ */new WeakSet;let i=0,n=0;return function e(s,r){if(++i>v||r>64)return m();if("string"==typeof s)return n+=s.length,n>g&&m(),s;if(null==s||"boolean"==typeof s)return s;if("number"==typeof s)return Number.isFinite(s)?s:m();if(!Array.isArray(s)&&!f(s))return m();if(Array.isArray(s)){s.length>v&&m();for(let e=0;e<s.length;e+=1)Object.hasOwn(s,e)||m()}if(t.has(s))return m();t.add(s);const o=Array.isArray(s)?[]:{};for(const t of Object.keys(s))O(o,t,e(Reflect.get(s,t),r+1));return t.delete(s),o}(e,0)}function k(e,t,i,n,s,r){if(!Number.isSafeInteger(s)||s<0||s>64)return m();void 0!==r&&("string"!=typeof r||r.length>128||b(r))&&m();const o=R(e);if(!f(o))return m();const a=/* @__PURE__ */new Map,c=/* @__PURE__ */new Set;let l=0;const d=/* @__PURE__ */new WeakSet,u=e=>{(++l>v||e>64)&&m()};function h(e,t,i,n){if(null==n||""===n)return;const r=f(n)?L(n,"id"):n,o=A(r);if(0===s)return void O(e,t,r);const l=JSON.stringify([i,o]);c.add(l),c.size>1e3&&m();const d=a.get(i)??[];d.push({owner:e,key:t,id:o}),a.set(i,d)}function p(e,t,i){void 0!==i.hasMany&&"boolean"!=typeof i.hasMany&&m();const n=i.relationTo,s=L(e,t);if(Array.isArray(n)){0===n.length&&m();const e=f(s)?L(s,"relationTo"):void 0;let t;for(const i of n)u(0),S(i)||m(),i===e&&(t=i);if(null==s||""===s)return;f(s)&&void 0!==t||m(),h(s,"value",t,L(s,"value"))}else S(n)||m(),h(e,t,n,s)}function g(e,t){if(u(t),Array.isArray(e)||f(e)){if(f(e)&&Object.hasOwn(e,"relationTo")&&Object.hasOwn(e,"value")){const t=L(e,"relationTo");return S(t)||m(),void h(e,"value",t,L(e,"value"))}for(const i of Object.keys(e))g(Reflect.get(e,i),t+1)}}!function e(t,i,n){u(n),d.has(t)&&m(),d.add(t);for(const s of t){u(n),f(s)&&w(s.name,512)&&!y.has(s.name)||m();const t=L(i,s.name);if("relationship"===s.type||"upload"===s.type)if(s.hasMany)if(null==t||Array.isArray(t)||m(),Array.isArray(t))for(let e=0;e<t.length;e+=1)p(t,e,s);else p(i,s.name,s);else p(i,s.name,s);else if("group"===s.type||"tabs"===s.type)null==t||f(t)||m(),f(t)&&s.fields&&e(s.fields,t,n+1);else if("row"===s.type||"collapsible"===s.type)s.fields&&e(s.fields,i,n+1);else if("array"===s.type||"blocks"===s.type){if(0===t?O(i,s.name,[]):null==t||Array.isArray(t)||m(),Array.isArray(t))for(const i of t)if(f(i)||m(),"array"===s.type&&s.fields)e(s.fields,i,n+1);else if("blocks"===s.type){const t=L(i,"blockType"),r=s.blocks?.find(e=>(u(n),e.slug===t));void 0===r&&m(),e(r.fields,i,n+1)}}else"richText"===s.type&&g(t,n+1)}d.delete(t)}(t,o,0),a.size>25&&m();const k=[];if(0===a.size)return{doc:o,requests:k};const U=new URL(i);(!["http:","https:"].includes(U.protocol)||U.username||U.password||U.search||U.hash)&&m(),(!n.startsWith("/")||n.includes("\\")||/[%?#]/u.test(n)||b(n))&&m(),n.split("/").some(e=>"."===e||".."===e)&&m();const q=`${T(i)}${T(n)}/`;for(const[e,t]of a){const i=[...new Set(t.map(e=>e.id))];let n=0;for(;n<i.length;){const o=i[n];void 0===o&&m();const a=o.includes(","),c=[];let l="";for(;n<i.length&&c.length<20;){const t=i[n];if(void 0===t||c.length>0&&(a||t.includes(",")))break;const o=[...c,t],d=new URLSearchParams({depth:String(s),limit:String(o.length)});void 0!==r&&d.set("locale",r),d.set(a?"where[id][equals]":"where[id][in]",a?t:o.join(","));const u=`${q}${encodeURIComponent(e)}?${d}`;if(u.length>8192){0===c.length&&m();break}new URL(u).origin!==U.origin&&m(),c.push(t),n+=1,l=u}(0===c.length||k.length>=64)&&m();const d=new Set(c);k.push({url:l,ids:d,slots:t.filter(e=>d.has(e.id))})}}return{doc:o,requests:k}}async function U(e,t,i){let n,s=!1,r=!1;const o=/* @__PURE__ */Symbol();let a;const c=new Promise(e=>{a=()=>{e(o)}}),l=()=>{i()&&!t.aborted||m()},d=()=>{if(void 0!==n&&!s){s=!0;try{Promise.resolve(n.cancel()).catch(()=>{})}catch{}}},u=()=>{a?.(),d()};t.addEventListener("abort",u,{once:!0});try{l();const t=e.body??null;if(l(),null===t){const t=e.json.bind(e);l();const i=await Promise.race([Promise.resolve().then(()=>(l(),t())),c]);return l(),i===o&&m(),i}const i=t.getReader.bind(t);l();const s=i();n=s,l();const a=[];let d=0,u=0;for(;;){const e=Promise.resolve().then(()=>{l();const e=s.read.bind(s);return l(),e()}),t=await Promise.race([e,c]);if(l(),t===o&&m(),t.done)break;d+=t.value.byteLength,(++u>1e4||d>g)&&m(),a.push(t.value)}r=!0;const h=new Uint8Array(d);let f=0;for(const e of a)h.set(e,f),f+=e.byteLength;try{return JSON.parse((new TextDecoder).decode(h))}catch{return m()}}finally{t.removeEventListener("abort",u),r||d();try{n?.releaseLock()}catch{}}}function q(e,t){const i=R(t);f(i)||m();const n=L(i,"docs");(!Array.isArray(n)||n.length>20)&&m();const s=/* @__PURE__ */new Map;for(const t of n){f(t)||m();const i=A(L(t,"id"));e.ids.has(i)&&!s.has(i)||m(),s.set(i,t)}s.size!==e.ids.size&&m();for(const t of e.slots)O(t.owner,t.key,s.get(t.id))}function P(e,t){return e.length>0&&e.length<=t&&"."!==e&&".."!==e&&!e.includes("/")&&!e.includes("\\")&&!function(e){for(const t of e){const e=t.codePointAt(0)??0;if(e<=31||127===e)return!0}return!1}(e)}function j(e){return P(e,128)&&!e.includes("?")&&!e.includes("#")}function C(e){return"number"==typeof e?Number.isFinite(e):"string"==typeof e&&P(e,512)}var I=class{serverURL;apiRoute;depth;fetchFn;log;inflight=null;attempt=0;destroyDepth=0;constructor(e){this.serverURL=function(e){let t=e.length;for(;e.endsWith("/",t);)t-=1;return e.slice(0,t)}(e.serverURL);const t=e.apiRoute??"/api";var i;this.apiRoute=t.startsWith("/")?t:`/${t}`,this.depth=e.depth??1,this.fetchFn=e.fetchFn,this.log=(i=e.log??r,(...e)=>{try{s(i(...e))}catch{}})}canMerge(e){try{const t=e.schema;return void 0!==t?(k(e.data,t,this.serverURL,this.apiRoute,this.depth,e.locale),!0):null!==this.endpointOf(e)}catch{return!1}}endpointOf(e){try{const t=e.globalSlug;if(t)return j(t)?{path:`globals/${encodeURIComponent(t)}`,field:"globalType",value:t}:null;const i=e.collectionSlug??"",n=e.data.id;return j(i)&&C(n)?{path:`${encodeURIComponent(i)}/${encodeURIComponent(String(n))}`,field:"id",value:String(n)}:null}catch{return null}}async merge(e){if(this.destroyDepth>0)return{status:"superseded"};const t=this.attempt+=1,i=this.inflight;this.inflight=null;let n=null,s=!1;const r=()=>this.attempt!==t;try{if(i?.abort(),r())return{status:"superseded"};const t=e.schema;if(s=void 0!==t,void 0!==t){const i=k(e.data,t,this.serverURL,this.apiRoute,this.depth,e.locale);if(r())return{status:"superseded"};if(0===i.requests.length)return{status:"merged",doc:i.doc};const s=this.fetchFn??("function"==typeof fetch?fetch:void 0);if(r())return{status:"superseded"};if(void 0===s)return{status:"unavailable"};n=new AbortController,this.inflight=n;for(const e of i.requests){const t=await s(e.url,{method:"GET",credentials:"include",redirect:"error",signal:n.signal});if(r())return{status:"superseded"};const i=t.ok;if(r())return{status:"superseded"};if(!i){const e=t.status;return r()?{status:"superseded"}:(n.abort(),r()?{status:"superseded"}:(this.log("merge HTTP",e),r()?{status:"superseded"}:{status:"unavailable"}))}const o=await U(t,n.signal,()=>!r());if(r())return{status:"superseded"};if(q(e,o),r())return{status:"superseded"}}return{status:"merged",doc:i.doc}}const o=this.endpointOf(e);if(r())return{status:"superseded"};if(null===o)return{status:"unavailable"};const a=this.fetchFn??("function"==typeof fetch?fetch:void 0);if(r())return{status:"superseded"};if(void 0===a)return{status:"unavailable"};const c=JSON.stringify({data:e.data,depth:this.depth,flattenLocales:!1,locale:e.locale});if(r())return{status:"superseded"};n=new AbortController,this.inflight=n;const l=`${this.serverURL}${this.apiRoute}/${o.path}`,d=await a(l,{method:"POST",credentials:"include",headers:{"Content-Type":"application/json","X-Payload-HTTP-Method-Override":"GET"},body:c,signal:n.signal});if(r())return{status:"superseded"};const u=d.ok;if(r())return{status:"superseded"};if(!u){const e=d.status;return r()?{status:"superseded"}:(this.log("merge HTTP",e,l),r()?{status:"superseded"}:{status:"unavailable"})}const h=d.json.bind(d);if(r())return{status:"superseded"};const f=await h();if(r())return{status:"superseded"};if(null===f||"object"!=typeof f||Array.isArray(f))return this.log("merge invalid",l),r()?{status:"superseded"}:{status:"unavailable"};const p=f,v=p[o.field],g=Object.hasOwn(p,o.field)&&("id"===o.field?!C(v)||String(v)!==o.value:v!==o.value);return r()?{status:"superseded"}:g?(this.log("merge invalid",l),r()?{status:"superseded"}:{status:"unavailable"}):{status:"merged",doc:p}}catch(e){return r()||s&&(n?.abort(),r())?{status:"superseded"}:(this.log("merge exception",s?new Error("legacy population could not be resolved"):e),r()?{status:"superseded"}:{status:"unavailable"})}finally{this.inflight===n&&(this.inflight=null)}}destroy(){this.attempt+=1;const e=this.inflight;this.inflight=null,this.destroyDepth+=1;try{e?.abort()}finally{this.destroyDepth-=1}}},E=class{matcher;handlers;boundListener=void 0;attachedTarget=void 0;generation=0;revision=0;queueHead=void 0;queueTail=void 0;lastAccepted=void 0;replayedGeneration=0;latestAcceptedAttempt=void 0;constructor(e,t){this.matcher=e,this.handlers=t}attach(e=window){if(void 0!==this.attachedTarget)return;let t=!1;const i=n=>{if(!t||this.boundListener!==i)return;const s=this.generation;try{this.receive(n,s,e)}catch{let e;try{e=n.origin}catch{e=""}this.reportInvalid("shape",e,s)}};this.generation+=1,this.resetQueue(),this.attachedTarget=e,this.boundListener=i;try{e.addEventListener("message",i)}catch(t){this.boundListener===i&&(this.attachedTarget=void 0,this.boundListener=void 0,this.resetQueue());try{e.removeEventListener("message",i)}catch{}throw t}if(this.boundListener===i)t=!0;else try{e.removeEventListener("message",i)}catch{}}detach(){const e=this.attachedTarget,t=this.boundListener;void 0!==e&&void 0!==t&&(this.attachedTarget=void 0,this.boundListener=void 0,this.resetQueue(),e.removeEventListener("message",t))}advanceGeneration(){return void 0!==this.attachedTarget&&(this.generation+=1,this.resetQueue(),!0)}replayLastAccepted(){const e=this.lastAccepted,t=this.generation,i=this.handlers.onReplay;if(void 0===i||void 0===e||e.generation>=t||this.replayedGeneration===t)return!1;if(!this.matchesOrigin(e.origin,t)||this.lastAccepted!==e)return!1;this.replayedGeneration=t;const n={generation:t,revision:this.revision+=1};return this.invokeHandler(t,i,structuredClone(e.message),e.origin,n),!0}forgetLastAccepted(){this.lastAccepted=void 0,this.replayedGeneration=0,this.latestAcceptedAttempt=void 0}static sendReady(e,t){const i={type:"payload-live-preview",ready:!0,protocolVersion:4};for(const n of e)for(const e of t)try{n.postMessage(i,e)}catch{}}receive(e,t,i){const n=e.origin;if(!this.matchesOrigin(n,t))return void this.reportInvalid("origin",n,t);if(!this.matchesSource(e,i))return void this.reportInvalid("source",n,t);const s=e.data;var r;if("object"==typeof(r=s)&&null!==r&&"type"in r&&"string"==typeof r.type)switch(s.type){case"payload-live-preview":return function(e){const t=e;return!(void 0!==t.data&&null!==t.data&&!f(t.data))&&h(t.fieldSchemaJSON,e=>Array.isArray(e))&&h(t.globalSlug,e=>"string"==typeof e)&&h(t.collectionSlug,e=>"string"==typeof e)&&h(t.locale,e=>"string"==typeof e)&&h(t.ready,e=>"boolean"==typeof e)&&h(t.previewToken,e=>"string"==typeof e)&&h(t.protocolVersion,e=>"number"==typeof e)}(s)?void this.dispatchUpdate(function(e){const t={...e};for(const e of p)null===Reflect.get(t,e)&&Reflect.deleteProperty(t,e);return Array.isArray(t.fieldSchemaJSON)?{...t,fieldSchemaJSON:c(t.fieldSchemaJSON)}:t}(s),n,t):void this.reportInvalid("shape",n,t);case"payload-document-event":return function(e){const t=e;return u(t.action,e=>"updated"===e||"created"===e||"deleted"===e)&&u(t.slug,e=>"string"==typeof e)&&u(t.id,e=>"string"==typeof e||Number.isFinite(e))}(s)?void this.invokeHandler(t,this.handlers.onDocumentEvent,s,n):void this.reportInvalid("shape",n,t);case"payload-live-preview-focus":{const e=function(e){const t=e.field;return"string"==typeof t&&t.length>0?t:void 0}(s);return void 0===e?void this.reportInvalid("shape",n,t):void this.invokeHandler(t,this.handlers.onFocusField,e,n)}default:this.reportInvalid("type",n,t)}else this.reportInvalid("shape",n,t)}dispatchUpdate(e,t,i){const n=f(e.data);if(!this.isCurrentGeneration(i))return;const s=n?{generation:i,revision:this.revision+=1}:void 0,r=this.handlers.validateToken;if(!this.isCurrentGeneration(i))return;if(void 0===r)return void this.commitUpdate(e,t,i,s);if(!0===e.ready&&void 0===e.data)return void this.commitUpdate(e,t,i,void 0);const o={generation:i,message:e,origin:t,messageRevision:s,verdict:void 0,next:void 0};let a;this.enqueue(o);try{a=r(e.previewToken,t)}catch{return void this.settleValidation(o,!1)}"boolean"!=typeof a?Promise.resolve(a).then(e=>{this.settleValidation(o,!0===e)},()=>{this.settleValidation(o,!1)}):this.settleValidation(o,a)}settleValidation(e,t){e.verdict=t,this.drainQueue(e.generation)}drainQueue(e){for(;this.isCurrentGeneration(e);){const t=this.queueHead;if(void 0===t)return;if(this.matchesOrigin(t.origin,e)){if(void 0===t.verdict)return;this.dequeue(t)&&(t.verdict?this.commitUpdate(t.message,t.origin,t.generation,t.messageRevision):this.reportInvalid("token",t.origin,t.generation))}else{if(!this.dequeue(t))continue;this.reportInvalid("origin",t.origin,t.generation)}}}commitUpdate(e,t,i,n){if(void 0===n)this.invokeHandler(i,this.handlers.onUpdate,e,t);else{const s={};this.latestAcceptedAttempt=s;const r=function(e){const t={...e};delete t.previewToken;try{return structuredClone(t)}catch{return null}}(e);if(this.latestAcceptedAttempt!==s||!this.isCurrentGeneration(i))return;this.lastAccepted=null===r?void 0:{generation:i,message:r,origin:t},this.invokeHandler(i,this.handlers.onUpdate,e,t,n)}}reportInvalid(e,t,i){this.invokeHandler(i,this.handlers.onInvalid,e,t)}matchesSource(e,t){if("parent-or-opener"!==this.handlers.sourcePolicy)return!0;try{const i=e.source;if("object"!=typeof i||null===i)return!1;const n=t.parent;return n!==t&&i===n||i===t.opener}catch{return!1}}matchesOrigin(e,t){try{return!0===(this.isCurrentGeneration(t)&&this.matcher(e))&&this.isCurrentGeneration(t)}catch{return!1}}invokeHandler(e,t,...i){if(void 0!==t&&this.isCurrentGeneration(e))try{s(t(...i))}catch{}}isCurrentGeneration(e){return void 0!==this.attachedTarget&&this.generation===e}enqueue(e){void 0===this.queueTail?this.queueHead=e:this.queueTail.next=e,this.queueTail=e}dequeue(e){return this.queueHead===e&&(this.queueHead=e.next,void 0===this.queueHead&&(this.queueTail=void 0),!0)}resetQueue(){this.queueHead=void 0,this.queueTail=void 0}};var H=/^https?:\/\/(localhost|127\.0\.0\.1)(?::\d+)?$/i,$=/^https?:\/\/[^/?#\s]+$/i,N=[3e3,3001,3333,4e3,4321,5e3,5173,5174,8e3,8080,8888,9e3],OriginDetector=class{#e;constructor(e={}){const t=/* @__PURE__ */new Set;for(const i of e.additionalOrigins??[]){const e=D(i);void 0!==e&&t.add(e)}let i;if(e.enableReferrerDetection??1){const t=e.referrer??("undefined"!=typeof document?document.referrer:"");if(t.length>0)try{const e=new URL(t).origin;e&&"null"!==e&&(i=e)}catch{}}const n=(e.enableLocalhostMatching??!0)&&(e.forceDevMode??function isDevMode(){const e=function(){if("undefined"==typeof process)return;const e=process.env.NODE_ENV;return"string"==typeof e?e:void 0}();if(void 0!==e)return"production"!==e;if("undefined"!=typeof window){const e=window.location.hostname;return"localhost"===e||"127.0.0.1"===e}return!1}());this.#e=[t,i,n,void 0!==i,void 0]}matches(e){return 0!==e.length&&"null"!==e&&(void 0!==this.#e[4]?e===this.#e[4]:!!this.#e[0].has(e)||0===this.#e[0].size&&void 0!==this.#e[1]&&e===this.#e[1]||!(!this.#e[2]||!H.test(e)))}lockOrigin(e){return!!this.matches(e)&&(this.#e[4]=e,!0)}unlockOrigin(){const e=this.#e[4];return this.#e[4]=void 0,e}get lockedOrigin(){return this.#e[4]}enumerate(){if(void 0!==this.#e[4])return[this.#e[4]];const e=new Set(this.#e[0]);if(0===this.#e[0].size&&void 0!==this.#e[1]&&e.add(this.#e[1]),this.#e[2])for(const t of N)e.add(`http://localhost:${String(t)}`),e.add(`http://127.0.0.1:${String(t)}`);return[...e]}get referrerWasAvailable(){return this.#e[3]}get isProductionUnconfigured(){return!(!function isInIframe(){if("undefined"==typeof window)return!1;try{return window.self!==window.top}catch{return!0}}()||this.#e[0].size>0||this.#e[2]||void 0!==this.#e[1])}get isReferrerOnlyTrust(){return!(this.#e[0].size>0||this.#e[2]||void 0===this.#e[1])}};function D(e){const t=e.trim();if(0!==t.length)try{const e=new URL(t);if("http:"!==e.protocol&&"https:"!==e.protocol)return;if(!$.test(e.origin))return;return e.origin}catch{return}}function G(e){const[t,i]=e;return"merge HTTP"===t?`the server answered ${String(i)}`:"merge invalid"===t?"the server answered with something other than a document":"merge exception"===t?i instanceof Error?i.message:"the request failed":"the update could not be merged"}var F=class{#t;#i;#n;#s;#r=/* @__PURE__ */new Set;#o;#a;#c=0;#l=[];#d=!1;#u;#h;constructor(e,t){this.#n=new OriginDetector({...void 0!==t.allowedOrigins?{additionalOrigins:t.allowedOrigins}:{},enableReferrerDetection:t.enableReferrerDetection??!1,...void 0!==t.enableLocalhostMatching?{enableLocalhostMatching:t.enableLocalhostMatching}:{}}),this.#t=new I({serverURL:t.serverURL,...void 0!==t.apiRoute?{apiRoute:t.apiRoute}:{},...void 0!==t.depth?{depth:t.depth}:{},...void 0!==t.fetchFn?{fetchFn:t.fetchFn}:{},log:(...e)=>{this.#l=e}}),this.#i=new E(e=>this.#n.matches(e),{onUpdate:e=>{this.#f(e)},onDocumentEvent:()=>{},sourcePolicy:t.eventSourcePolicy??"parent-or-opener"}),this.#s=t.target,this.#o={data:e,isLoading:!0,status:"idle",error:void 0,revision:0},this.#a=this.#o}subscribe=e=>(this.#r.add(e),this.#d||this.#p(),()=>{this.#r.delete(e),0===this.#r.size&&this.#v()});getSnapshot=()=>this.#a;getServerSnapshot=()=>this.#o;#p(){"undefined"==typeof window&&void 0===this.#s||(this.#d=!0,this.#i.attach(this.#s??window),E.sendReady(this.#g(),this.#n.enumerate()))}#v(){this.#d&&(this.#d=!1,this.#c+=1,this.#i.detach(),this.#t.destroy())}#g(){const e=this.#s??("undefined"==typeof window?void 0:window);if(void 0===e)return[];const t=[];e.parent!==e&&t.push(e.parent);const i=e.opener;return null!=i&&i!==e&&t.push(i),t}#y(e){return this.#d&&e===this.#c}async#f(e){const t=e.data;if(void 0===t)return;const i=this.#c+=1;void 0!==e.fieldSchemaJSON&&(this.#u=e.fieldSchemaJSON);const n=this.#u;void 0!==e.locale&&(this.#h=e.locale);const s=void 0===n?e.locale:this.#h;if(this.#m({...this.#a,isLoading:!0}),!this.#y(i))return;this.#l=[];const r=await this.#t.merge({data:t,...void 0===n?{}:{schema:n},...void 0!==e.collectionSlug?{collectionSlug:e.collectionSlug}:{},...void 0!==e.globalSlug?{globalSlug:e.globalSlug}:{},...void 0!==s?{locale:s}:{}});this.#y(i)&&"superseded"!==r.status&&("unavailable"!==r.status?this.#m({data:r.doc,isLoading:!1,status:"live",error:void 0,revision:i}):this.#m({...this.#a,isLoading:!1,status:"unavailable",error:new Error(`live preview: ${G(this.#l)}`)}))}#m(e){const t=this.#a;if(t.data!==e.data||t.isLoading!==e.isLoading||t.status!==e.status||t.error!==e.error||t.revision!==e.revision){this.#a=e;for(const e of[...this.#r])e()}}};function M(s){if(void 0===e())throw new Error("payload-live-preview: useLivePreviewDocument() needs an effect scope — call it from setup() or inside effectScope(), so its window listener and any request in flight are released with the component.");const{initialData:r,...o}=s,a=new F(r,o),c=t(a.getSnapshot());return i(a.subscribe(()=>{c.value=a.getSnapshot()})),{data:n(()=>c.value.data),isLoading:n(()=>c.value.isLoading),status:n(()=>c.value.status),error:n(()=>c.value.error),revision:n(()=>c.value.revision)}}export{M as useLivePreviewDocument};
2
2
  //# sourceMappingURL=index.js.map