payload-live-preview 2.2.0 → 2.2.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 +32 -18
- package/dist/adapters/astro/index.js +1 -1
- package/dist/adapters/astro/index.js.map +1 -1
- package/dist/adapters/astro/middleware-entry.js +1 -1
- package/dist/adapters/astro/middleware-entry.js.map +1 -1
- package/dist/adapters/nextjs/index.js +1 -1
- package/dist/adapters/nextjs/index.js.map +1 -1
- package/dist/adapters/nuxt/index.js +1 -1
- package/dist/adapters/nuxt/index.js.map +1 -1
- package/dist/adapters/react/index.d.ts +2 -2
- package/dist/adapters/react/index.js +1 -1
- package/dist/adapters/react/index.js.map +1 -1
- package/dist/adapters/sveltekit/index.js +1 -1
- package/dist/adapters/sveltekit/index.js.map +1 -1
- package/dist/adapters/vue/index.d.ts +2 -2
- package/dist/adapters/vue/index.js +1 -1
- package/dist/adapters/vue/index.js.map +1 -1
- package/dist/annotate.d.ts +22 -15
- package/dist/annotate.js +1 -1
- package/dist/annotate.js.map +1 -1
- package/dist/client.cjs +1 -1
- package/dist/client.cjs.map +1 -1
- package/dist/client.d.cts +112 -112
- package/dist/client.d.ts +112 -112
- package/dist/client.js +1 -1
- package/dist/client.js.map +1 -1
- package/dist/codegen-cli.js +1 -1
- package/dist/codegen-cli.js.map +1 -1
- package/dist/codegen.cjs +1 -1
- package/dist/codegen.cjs.map +1 -1
- package/dist/codegen.d.cts +14 -16
- package/dist/codegen.d.ts +14 -16
- package/dist/codegen.js +1 -1
- package/dist/codegen.js.map +1 -1
- package/dist/core.cjs +1 -1
- package/dist/core.cjs.map +1 -1
- package/dist/core.d.cts +176 -170
- package/dist/core.d.ts +176 -170
- package/dist/core.js +1 -1
- package/dist/core.js.map +1 -1
- package/dist/doctor-cli.d.ts +1 -1
- package/dist/doctor-cli.js +1 -1
- package/dist/doctor-cli.js.map +1 -1
- package/dist/doctor.d.ts +2 -2
- package/dist/doctor.js +1 -1
- package/dist/doctor.js.map +1 -1
- package/dist/{document-session-DWi9H3Qk.d.ts → document-session-Bkd181PV.d.ts} +3 -2
- package/dist/fragment.cjs.map +1 -1
- package/dist/fragment.d.cts +1 -1
- package/dist/fragment.d.ts +1 -1
- package/dist/fragment.js.map +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +181 -175
- package/dist/index.d.ts +181 -175
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/lean.cjs +1 -1
- package/dist/lean.cjs.map +1 -1
- package/dist/lean.js +1 -1
- package/dist/lean.js.map +1 -1
- package/dist/lexical.cjs +1 -1
- package/dist/lexical.cjs.map +1 -1
- package/dist/lexical.js +1 -1
- package/dist/lexical.js.map +1 -1
- package/dist/plugins.cjs +1 -1
- package/dist/plugins.cjs.map +1 -1
- package/dist/plugins.d.cts +1 -1
- package/dist/plugins.d.ts +1 -1
- package/dist/plugins.js +1 -1
- package/dist/plugins.js.map +1 -1
- package/dist/{probe-DpMZ4fOS.d.ts → probe-D-IHg9dF.d.ts} +1 -1
- package/dist/structural.cjs +2 -2
- package/dist/structural.cjs.map +1 -1
- package/dist/structural.d.cts +27 -27
- package/dist/structural.d.ts +27 -27
- package/dist/structural.js +2 -2
- package/dist/structural.js.map +1 -1
- package/package.json +7 -6
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { D as DocumentSessionOptions, a as DocumentSnapshot } from '../../document-session-
|
|
2
|
-
export { b as DocumentStatus } from '../../document-session-
|
|
1
|
+
import { D as DocumentSessionOptions, a as DocumentSnapshot } from '../../document-session-Bkd181PV.js';
|
|
2
|
+
export { b as DocumentStatus } from '../../document-session-Bkd181PV.js';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* `<LivePreviewRouteRefresh />` — lends the runtime the host router's own
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
'use client';
|
|
2
|
-
import{useTransition as e,useRef as t,useEffect as i,useSyncExternalStore as n}from"react";function r(e){return("object"==typeof e&&null!==e||"function"==typeof e)&&"function"==typeof e.then&&(Promise.resolve(e).catch(()=>{}),!0)}function s(){}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 r of e){const e=u(r,t,i);e&&n.push(e)}return t.active.delete(e),n}function u(e,t,i){if(o(t),"object"!=typeof e||null===e)return;const n=e,r=n.name,s=n.type;if("string"!=typeof r||0===r.length)return;if("string"!=typeof s||0===s.length)return;const c=l(n.fields,t,i+1),u=function(e,t,i){if("object"==typeof e&&null!==e&&!Array.isArray(e)){a(e,t,i);const n=[];for(const r in e){if(!Object.hasOwn(e,r))continue;o(t);const s=Reflect.get(e,r);"object"!=typeof s||null===s||["__proto__","constructor","prototype"].includes(r)||n.push({...s,slug:r,fields:l(s.fields,t,i+1)})}return t.active.delete(e),n}if(!Array.isArray(e))return[];a(e,t,i);const n=[];for(const r of e){if(o(t),"object"!=typeof r||null===r)continue;const e=r,s=e.slug;if("string"!=typeof s||0===s.length)continue;const a=l(e.fields,t,i+1);n.push({...e,slug:s,fields:a})}return t.active.delete(e),n}(n.blocks,t,i+1),d={...n,name:r,type:s},h=n.relationTo;if(Array.isArray(h)){const e=[];for(const i of h)o(t),e.push(i);d.relationTo=e}return c.length>0?d.fields=c:delete d.fields,u.length>0?d.blocks=u:delete d.blocks,d}function d(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"],g=1e4,v=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 R(e){return Number.isFinite(e)?String(e):w(e,512)?e:m()}function A(e,t){return Object.hasOwn(e,t)?Reflect.get(e,t):void 0}function L(e,t,i){Object.defineProperty(e,t,{value:i,writable:!0,configurable:!0,enumerable:!0})}function O(e){let t=e.length;for(;t>0&&47===e.charCodeAt(t-1);)t-=1;return e.slice(0,t)}function T(e){const t=/* @__PURE__ */new WeakSet;let i=0,n=0;return function e(r,s){if(++i>g||s>64)return m();if("string"==typeof r)return n+=r.length,n>v&&m(),r;if(null==r||"boolean"==typeof r)return r;if("number"==typeof r)return Number.isFinite(r)?r:m();if(!Array.isArray(r)&&!f(r))return m();if(Array.isArray(r)){r.length>g&&m();for(let e=0;e<r.length;e+=1)Object.hasOwn(r,e)||m()}if(t.has(r))return m();t.add(r);const o=Array.isArray(r)?[]:{};for(const t of Object.keys(r))L(o,t,e(Reflect.get(r,t),s+1));return t.delete(r),o}(e,0)}function k(e,t,i,n,r,s){if(!Number.isSafeInteger(r)||r<0||r>64)return m();void 0!==s&&("string"!=typeof s||s.length>128||b(s))&&m();const o=T(e);if(!f(o))return m();const a=/* @__PURE__ */new Map,c=/* @__PURE__ */new Set;let l=0;const u=/* @__PURE__ */new WeakSet,d=e=>{(++l>g||e>64)&&m()};function h(e,t,i,n){if(null==n||""===n)return;const s=f(n)?A(n,"id"):n,o=R(s);if(0===r)return void L(e,t,s);const l=JSON.stringify([i,o]);c.add(l),c.size>1e3&&m();const u=a.get(i)??[];u.push({owner:e,key:t,id:o}),a.set(i,u)}function p(e,t,i){void 0!==i.hasMany&&"boolean"!=typeof i.hasMany&&m();const n=i.relationTo,r=A(e,t);if(Array.isArray(n)){0===n.length&&m();const e=f(r)?A(r,"relationTo"):void 0;let t;for(const i of n)d(0),S(i)||m(),i===e&&(t=i);if(null==r||""===r)return;f(r)&&void 0!==t||m(),h(r,"value",t,A(r,"value"))}else S(n)||m(),h(e,t,n,r)}function v(e,t){if(d(t),Array.isArray(e)||f(e)){if(f(e)&&Object.hasOwn(e,"relationTo")&&Object.hasOwn(e,"value")){const t=A(e,"relationTo");return S(t)||m(),void h(e,"value",t,A(e,"value"))}for(const i of Object.keys(e))v(Reflect.get(e,i),t+1)}}!function e(t,i,n){d(n),u.has(t)&&m(),u.add(t);for(const r of t){d(n),f(r)&&w(r.name,512)&&!y.has(r.name)||m();const t=A(i,r.name);if("relationship"===r.type||"upload"===r.type)if(r.hasMany)if(null==t||Array.isArray(t)||m(),Array.isArray(t))for(let e=0;e<t.length;e+=1)p(t,e,r);else p(i,r.name,r);else p(i,r.name,r);else if("group"===r.type||"tabs"===r.type)null==t||f(t)||m(),f(t)&&r.fields&&e(r.fields,t,n+1);else if("row"===r.type||"collapsible"===r.type)r.fields&&e(r.fields,i,n+1);else if("array"===r.type||"blocks"===r.type){if(0===t?L(i,r.name,[]):null==t||Array.isArray(t)||m(),Array.isArray(t))for(const i of t)if(f(i)||m(),"array"===r.type&&r.fields)e(r.fields,i,n+1);else if("blocks"===r.type){const t=A(i,"blockType"),s=r.blocks?.find(e=>(d(n),e.slug===t));void 0===s&&m(),e(s.fields,i,n+1)}}else"richText"===r.type&&v(t,n+1)}u.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 P=`${O(i)}${O(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],u=new URLSearchParams({depth:String(r),limit:String(o.length)});void 0!==s&&u.set("locale",s),u.set(a?"where[id][equals]":"where[id][in]",a?t:o.join(","));const d=`${P}${encodeURIComponent(e)}?${u}`;if(d.length>8192){0===c.length&&m();break}new URL(d).origin!==U.origin&&m(),c.push(t),n+=1,l=d}(0===c.length||k.length>=64)&&m();const u=new Set(c);k.push({url:l,ids:u,slots:t.filter(e=>u.has(e.id))})}}return{doc:o,requests:k}}async function U(e,t,i){let n,r=!1,s=!1;const o=/* @__PURE__ */Symbol();let a;const c=new Promise(e=>{a=()=>{e(o)}}),l=()=>{i()&&!t.aborted||m()},u=()=>{if(void 0!==n&&!r){r=!0;try{Promise.resolve(n.cancel()).catch(()=>{})}catch{}}},d=()=>{a?.(),u()};t.addEventListener("abort",d,{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 r=i();n=r,l();const a=[];let u=0,d=0;for(;;){const e=Promise.resolve().then(()=>{l();const e=r.read.bind(r);return l(),e()}),t=await Promise.race([e,c]);if(l(),t===o&&m(),t.done)break;u+=t.value.byteLength,(++d>1e4||u>v)&&m(),a.push(t.value)}s=!0;const h=new Uint8Array(u);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",d),s||u();try{n?.releaseLock()}catch{}}}function P(e,t){const i=T(t);f(i)||m();const n=A(i,"docs");(!Array.isArray(n)||n.length>20)&&m();const r=/* @__PURE__ */new Map;for(const t of n){f(t)||m();const i=R(A(t,"id"));e.ids.has(i)&&!r.has(i)||m(),r.set(i,t)}r.size!==e.ids.size&&m();for(const t of e.slots)L(t.owner,t.key,r.get(t.id))}function j(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 q(e){return j(e,128)&&!e.includes("?")&&!e.includes("#")}function C(e){return"number"==typeof e?Number.isFinite(e):"string"==typeof e&&j(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??s,(...e)=>{try{r(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 q(t)?{path:`globals/${encodeURIComponent(t)}`,field:"globalType",value:t}:null;const i=e.collectionSlug??"",n=e.data.id;return q(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,r=!1;const s=()=>this.attempt!==t;try{if(i?.abort(),s())return{status:"superseded"};const t=e.schema;if(r=void 0!==t,void 0!==t){const i=k(e.data,t,this.serverURL,this.apiRoute,this.depth,e.locale);if(s())return{status:"superseded"};if(0===i.requests.length)return{status:"merged",doc:i.doc};const r=this.fetchFn??("function"==typeof fetch?fetch:void 0);if(s())return{status:"superseded"};if(void 0===r)return{status:"unavailable"};n=new AbortController,this.inflight=n;for(const e of i.requests){const t=await r(e.url,{method:"GET",credentials:"include",redirect:"error",signal:n.signal});if(s())return{status:"superseded"};const i=t.ok;if(s())return{status:"superseded"};if(!i){const e=t.status;return s()?{status:"superseded"}:(n.abort(),s()?{status:"superseded"}:(this.log("merge HTTP",e),s()?{status:"superseded"}:{status:"unavailable"}))}const o=await U(t,n.signal,()=>!s());if(s())return{status:"superseded"};if(P(e,o),s())return{status:"superseded"}}return{status:"merged",doc:i.doc}}const o=this.endpointOf(e);if(s())return{status:"superseded"};if(null===o)return{status:"unavailable"};const a=this.fetchFn??("function"==typeof fetch?fetch:void 0);if(s())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(s())return{status:"superseded"};n=new AbortController,this.inflight=n;const l=`${this.serverURL}${this.apiRoute}/${o.path}`,u=await a(l,{method:"POST",credentials:"include",headers:{"Content-Type":"application/json","X-Payload-HTTP-Method-Override":"GET"},body:c,signal:n.signal});if(s())return{status:"superseded"};const d=u.ok;if(s())return{status:"superseded"};if(!d){const e=u.status;return s()?{status:"superseded"}:(this.log("merge HTTP",e,l),s()?{status:"superseded"}:{status:"unavailable"})}const h=u.json.bind(u);if(s())return{status:"superseded"};const f=await h();if(s())return{status:"superseded"};if(null===f||"object"!=typeof f||Array.isArray(f))return this.log("merge invalid",l),s()?{status:"superseded"}:{status:"unavailable"};const p=f,g=p[o.field],v=Object.hasOwn(p,o.field)&&("id"===o.field?!C(g)||String(g)!==o.value:g!==o.value);return s()?{status:"superseded"}:v?(this.log("merge invalid",l),s()?{status:"superseded"}:{status:"unavailable"}):{status:"merged",doc:p}}catch(e){return s()||r&&(n?.abort(),s())?{status:"superseded"}:(this.log("merge exception",r?new Error("legacy population could not be resolved"):e),s()?{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 r=this.generation;try{this.receive(n,r,e)}catch{let e;try{e=n.origin}catch{e=""}this.reportInvalid("shape",e,r)}};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 r=e.data;var s;if("object"==typeof(s=r)&&null!==s&&"type"in s&&"string"==typeof s.type)switch(r.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)}(r)?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}(r),n,t):void this.reportInvalid("shape",n,t);case"payload-document-event":return function(e){const t=e;return d(t.action,e=>"updated"===e||"created"===e||"deleted"===e)&&d(t.slug,e=>"string"==typeof e)&&d(t.id,e=>"string"==typeof e||Number.isFinite(e))}(r)?void this.invokeHandler(t,this.handlers.onDocumentEvent,r,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}(r);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 r=n?{generation:i,revision:this.revision+=1}:void 0,s=this.handlers.validateToken;if(!this.isCurrentGeneration(i))return;if(void 0===s)return void this.commitUpdate(e,t,i,r);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:r,verdict:void 0,next:void 0};let a;this.enqueue(o);try{a=s(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 r={};this.latestAcceptedAttempt=r;const s=function(e){const t={...e};delete t.previewToken;try{return structuredClone(t)}catch{return null}}(e);if(this.latestAcceptedAttempt!==r||!this.isCurrentGeneration(i))return;this.lastAccepted=null===s?void 0:{generation:i,message:s,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{r(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 F(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 M=class{#t;#i;#n;#r;#s=/* @__PURE__ */new Set;#o;#a;#c=0;#l=[];#u=!1;#d;#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.#r=t.target,this.#o={data:e,isLoading:!0,status:"idle",error:void 0,revision:0},this.#a=this.#o}subscribe=e=>(this.#s.add(e),this.#u||this.#p(),()=>{this.#s.delete(e),0===this.#s.size&&this.#g()});getSnapshot=()=>this.#a;getServerSnapshot=()=>this.#o;#p(){"undefined"==typeof window&&void 0===this.#r||(this.#u=!0,this.#i.attach(this.#r??window),E.sendReady(this.#v(),this.#n.enumerate()))}#g(){this.#u&&(this.#u=!1,this.#c+=1,this.#i.detach(),this.#t.destroy())}#v(){const e=this.#r??("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.#u&&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.#d=e.fieldSchemaJSON);const n=this.#d;void 0!==e.locale&&(this.#h=e.locale);const r=void 0===n?e.locale:this.#h;if(this.#m({...this.#a,isLoading:!0}),!this.#y(i))return;this.#l=[];const s=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!==r?{locale:r}:{}});this.#y(i)&&"superseded"!==s.status&&("unavailable"!==s.status?this.#m({data:s.doc,isLoading:!1,status:"live",error:void 0,revision:i}):this.#m({...this.#a,isLoading:!1,status:"unavailable",error:new Error(`live preview: ${F(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.#s])e()}}},G="__livePreviewRouteRefresh";function registerRouteRefresh(e){const t="undefined"==typeof window?void 0:window;return void 0===t?()=>{}:(t[G]=e,()=>{t[G]===e&&(t[G]=void 0)})}function z(e){const t=e.current;e.current=[];for(const e of t)e()}function _({refresh:n}){const[r,s]=e(),o=t([]);return i(()=>{r||z(o)}),i(()=>{const e=registerRouteRefresh(()=>new Promise(e=>{o.current.push(e),s(()=>{n()})}));return()=>{e(),z(o)}},[n,s]),null}function J(e){const i=function(e){const{initialData:i,serverURL:n,apiRoute:r,depth:s,allowedOrigins:o,eventSourcePolicy:a,enableReferrerDetection:c,enableLocalhostMatching:l,fetchFn:u,target:d}=e,h=[n,r??"",String(s??""),(o??[]).join(" "),a??"",String(c??""),String(l??"")].join(" | "),f=t(null);return f.current?.key===h&&f.current.target===d||(f.current={key:h,target:d,session:new M(i,{serverURL:n,...void 0!==r?{apiRoute:r}:{},...void 0!==s?{depth:s}:{},...void 0!==o?{allowedOrigins:o}:{},...void 0!==a?{eventSourcePolicy:a}:{},...void 0!==c?{enableReferrerDetection:c}:{},...void 0!==l?{enableLocalhostMatching:l}:{},...void 0!==u?{fetchFn:u}:{},...void 0!==d?{target:d}:{}})}),f.current.session}(e);return n(i.subscribe,i.getSnapshot,i.getServerSnapshot)}export{_ as LivePreviewRouteRefresh,registerRouteRefresh,J as useLivePreviewDocument};
|
|
2
|
+
import{useTransition as e,useRef as t,useEffect as i,useSyncExternalStore as n}from"react";function r(e){return("object"==typeof e&&null!==e||"function"==typeof e)&&"function"==typeof e.then&&(Promise.resolve(e).catch(()=>{}),!0)}function s(){}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 r of e){const e=u(r,t,i);e&&n.push(e)}return t.active.delete(e),n}function u(e,t,i){if(o(t),"object"!=typeof e||null===e)return;const n=e,r=n.name,s=n.type;if("string"!=typeof r||0===r.length)return;if("string"!=typeof s||0===s.length)return;const c=l(n.fields,t,i+1),u=function(e,t,i){if("object"==typeof e&&null!==e&&!Array.isArray(e)){a(e,t,i);const n=[];for(const r in e){if(!Object.hasOwn(e,r))continue;o(t);const s=Reflect.get(e,r);"object"!=typeof s||null===s||["__proto__","constructor","prototype"].includes(r)||n.push({...s,slug:r,fields:l(s.fields,t,i+1)})}return t.active.delete(e),n}if(!Array.isArray(e))return[];a(e,t,i);const n=[];for(const r of e){if(o(t),"object"!=typeof r||null===r)continue;const e=r,s=e.slug;if("string"!=typeof s||0===s.length)continue;const a=l(e.fields,t,i+1);n.push({...e,slug:s,fields:a})}return t.active.delete(e),n}(n.blocks,t,i+1),d={...n,name:r,type:s},h=n.relationTo;if(Array.isArray(h)){const e=[];for(const i of h)o(t),e.push(i);d.relationTo=e}return c.length>0?d.fields=c:delete d.fields,u.length>0?d.blocks=u:delete d.blocks,d}function d(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"],g=1e4,v=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 R(e){return Number.isFinite(e)?String(e):w(e,512)?e:m()}function A(e,t){return Object.hasOwn(e,t)?Reflect.get(e,t):void 0}function L(e,t,i){Object.defineProperty(e,t,{value:i,writable:!0,configurable:!0,enumerable:!0})}function O(e){let t=e.length;for(;t>0&&47===e.charCodeAt(t-1);)t-=1;return e.slice(0,t)}function T(e){const t=/* @__PURE__ */new WeakSet;let i=0,n=0;return function e(r,s){if(++i>g||s>64)return m();if("string"==typeof r)return n+=r.length,n>v&&m(),r;if(null==r||"boolean"==typeof r)return r;if("number"==typeof r)return Number.isFinite(r)?r:m();if(!Array.isArray(r)&&!f(r))return m();if(Array.isArray(r)){r.length>g&&m();for(let e=0;e<r.length;e+=1)Object.hasOwn(r,e)||m()}if(t.has(r))return m();t.add(r);const o=Array.isArray(r)?[]:{};for(const t of Object.keys(r))L(o,t,e(Reflect.get(r,t),s+1));return t.delete(r),o}(e,0)}function k(e,t,i,n,r,s){if(!Number.isSafeInteger(r)||r<0||r>64)return m();void 0!==s&&("string"!=typeof s||s.length>128||b(s))&&m();const o=T(e);if(!f(o))return m();const a=/* @__PURE__ */new Map,c=/* @__PURE__ */new Set;let l=0;const u=/* @__PURE__ */new WeakSet,d=e=>{(++l>g||e>64)&&m()};function h(e,t,i,n){if(null==n||""===n)return;const s=f(n)?A(n,"id"):n,o=R(s);if(0===r)return void L(e,t,s);const l=JSON.stringify([i,o]);c.add(l),c.size>1e3&&m();const u=a.get(i)??[];u.push({owner:e,key:t,id:o}),a.set(i,u)}function p(e,t,i){void 0!==i.hasMany&&"boolean"!=typeof i.hasMany&&m();const n=i.relationTo,r=A(e,t);if(Array.isArray(n)){0===n.length&&m();const e=f(r)?A(r,"relationTo"):void 0;let t;for(const i of n)d(0),S(i)||m(),i===e&&(t=i);if(null==r||""===r)return;f(r)&&void 0!==t||m(),h(r,"value",t,A(r,"value"))}else S(n)||m(),h(e,t,n,r)}function v(e,t){if(d(t),Array.isArray(e)||f(e)){if(f(e)&&Object.hasOwn(e,"relationTo")&&Object.hasOwn(e,"value")){const t=A(e,"relationTo");return S(t)||m(),void h(e,"value",t,A(e,"value"))}for(const i of Object.keys(e))v(Reflect.get(e,i),t+1)}}!function e(t,i,n){d(n),u.has(t)&&m(),u.add(t);for(const r of t){d(n),f(r)&&w(r.name,512)&&!y.has(r.name)||m();const t=A(i,r.name);if("relationship"===r.type||"upload"===r.type)if(r.hasMany)if(null==t||Array.isArray(t)||m(),Array.isArray(t))for(let e=0;e<t.length;e+=1)p(t,e,r);else p(i,r.name,r);else p(i,r.name,r);else if("group"===r.type||"tabs"===r.type)null==t||f(t)||m(),f(t)&&r.fields&&e(r.fields,t,n+1);else if("row"===r.type||"collapsible"===r.type)r.fields&&e(r.fields,i,n+1);else if("array"===r.type||"blocks"===r.type){if(0===t?L(i,r.name,[]):null==t||Array.isArray(t)||m(),Array.isArray(t))for(const i of t)if(f(i)||m(),"array"===r.type&&r.fields)e(r.fields,i,n+1);else if("blocks"===r.type){const t=A(i,"blockType"),s=r.blocks?.find(e=>(d(n),e.slug===t));void 0===s&&m(),e(s.fields,i,n+1)}}else"richText"===r.type&&v(t,n+1)}u.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 P=`${O(i)}${O(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],u=new URLSearchParams({depth:String(r),limit:String(o.length)});void 0!==s&&u.set("locale",s),u.set(a?"where[id][equals]":"where[id][in]",a?t:o.join(","));const d=`${P}${encodeURIComponent(e)}?${u}`;if(d.length>8192){0===c.length&&m();break}new URL(d).origin!==U.origin&&m(),c.push(t),n+=1,l=d}(0===c.length||k.length>=64)&&m();const u=new Set(c);k.push({url:l,ids:u,slots:t.filter(e=>u.has(e.id))})}}return{doc:o,requests:k}}async function U(e,t,i){let n,r=!1,s=!1;const o=/* @__PURE__ */Symbol();let a;const c=new Promise(e=>{a=()=>{e(o)}}),l=()=>{i()&&!t.aborted||m()},u=()=>{if(void 0!==n&&!r){r=!0;try{Promise.resolve(n.cancel()).catch(()=>{})}catch{}}},d=()=>{a?.(),u()};t.addEventListener("abort",d,{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 r=i();n=r,l();const a=[];let u=0,d=0;for(;;){const e=Promise.resolve().then(()=>{l();const e=r.read.bind(r);return l(),e()}),t=await Promise.race([e,c]);if(l(),t===o&&m(),t.done)break;u+=t.value.byteLength,(++d>1e4||u>v)&&m(),a.push(t.value)}s=!0;const h=new Uint8Array(u);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",d),s||u();try{n?.releaseLock()}catch{}}}function P(e,t){const i=T(t);f(i)||m();const n=A(i,"docs");(!Array.isArray(n)||n.length>20)&&m();const r=/* @__PURE__ */new Map;for(const t of n){f(t)||m();const i=R(A(t,"id"));e.ids.has(i)&&!r.has(i)||m(),r.set(i,t)}r.size!==e.ids.size&&m();for(const t of e.slots)L(t.owner,t.key,r.get(t.id))}function j(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 q(e){return j(e,128)&&!e.includes("?")&&!e.includes("#")}function C(e){return"number"==typeof e?Number.isFinite(e):"string"==typeof e&&j(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??s,(...e)=>{try{r(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 q(t)?{path:`globals/${encodeURIComponent(t)}`,field:"globalType",value:t}:null;const i=e.collectionSlug??"",n=e.data.id;return q(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,r=!1;const s=()=>this.attempt!==t;try{if(i?.abort(),s())return{status:"superseded"};const t=e.schema;if(r=void 0!==t,void 0!==t){const i=k(e.data,t,this.serverURL,this.apiRoute,this.depth,e.locale);if(s())return{status:"superseded"};if(0===i.requests.length)return{status:"merged",doc:i.doc};const r=this.fetchFn??("function"==typeof fetch?fetch:void 0);if(s())return{status:"superseded"};if(void 0===r)return{status:"unavailable"};n=new AbortController,this.inflight=n;for(const e of i.requests){const t=await r(e.url,{method:"GET",credentials:"include",redirect:"error",signal:n.signal});if(s())return{status:"superseded"};const i=t.ok;if(s())return{status:"superseded"};if(!i){const e=t.status;return s()?{status:"superseded"}:(n.abort(),s()?{status:"superseded"}:(this.log("merge HTTP",e),s()?{status:"superseded"}:{status:"unavailable"}))}const o=await U(t,n.signal,()=>!s());if(s())return{status:"superseded"};if(P(e,o),s())return{status:"superseded"}}return{status:"merged",doc:i.doc}}const o=this.endpointOf(e);if(s())return{status:"superseded"};if(null===o)return{status:"unavailable"};const a=this.fetchFn??("function"==typeof fetch?fetch:void 0);if(s())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(s())return{status:"superseded"};n=new AbortController,this.inflight=n;const l=`${this.serverURL}${this.apiRoute}/${o.path}`,u=await a(l,{method:"POST",credentials:"include",headers:{"Content-Type":"application/json","X-Payload-HTTP-Method-Override":"GET"},body:c,signal:n.signal});if(s())return{status:"superseded"};const d=u.ok;if(s())return{status:"superseded"};if(!d){const e=u.status;return s()?{status:"superseded"}:(this.log("merge HTTP",e,l),s()?{status:"superseded"}:{status:"unavailable"})}const h=u.json.bind(u);if(s())return{status:"superseded"};const f=await h();if(s())return{status:"superseded"};if(null===f||"object"!=typeof f||Array.isArray(f))return this.log("merge invalid",l),s()?{status:"superseded"}:{status:"unavailable"};const p=f,g=p[o.field],v=Object.hasOwn(p,o.field)&&("id"===o.field?!C(g)||String(g)!==o.value:g!==o.value);return s()?{status:"superseded"}:v?(this.log("merge invalid",l),s()?{status:"superseded"}:{status:"unavailable"}):{status:"merged",doc:p}}catch(e){return s()||r&&(n?.abort(),s())?{status:"superseded"}:(this.log("merge exception",r?new Error("legacy population could not be resolved"):e),s()?{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 r=this.generation;try{this.receive(n,r,e)}catch{let e;try{e=n.origin}catch{e=""}this.reportInvalid("shape",e,r)}};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 r=e.data;var s;if("object"==typeof(s=r)&&null!==s&&"type"in s&&"string"==typeof s.type)switch(r.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)}(r)?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}(r),n,t):void this.reportInvalid("shape",n,t);case"payload-document-event":return function(e){const t=e;return d(t.action,e=>"updated"===e||"created"===e||"deleted"===e)&&d(t.slug,e=>"string"==typeof e)&&d(t.id,e=>"string"==typeof e||Number.isFinite(e))}(r)?void this.invokeHandler(t,this.handlers.onDocumentEvent,r,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}(r);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 r=n?{generation:i,revision:this.revision+=1}:void 0,s=this.handlers.validateToken;if(!this.isCurrentGeneration(i))return;if(void 0===s)return void this.commitUpdate(e,t,i,r);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:r,verdict:void 0,next:void 0};let a;this.enqueue(o);try{a=s(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 r={};this.latestAcceptedAttempt=r;const s=function(e){const t={...e};delete t.previewToken;try{return structuredClone(t)}catch{return null}}(e);if(this.latestAcceptedAttempt!==r||!this.isCurrentGeneration(i))return;this.lastAccepted=null===s?void 0:{generation:i,message:s,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{r(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];function D(){if("undefined"==typeof location)return;const e=location.ancestorOrigins;return e?.[0]}var OriginDetector=class{#e;constructor(e={}){const t=/* @__PURE__ */new Set;for(const i of e.additionalOrigins??[]){const e=F(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,D()]}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]);0===this.#e[0].size&&void 0!==this.#e[1]&&e.add(this.#e[1]);const t=this.#e[5];if(this.#e[2]&&void 0!==t)H.test(t)&&e.add(t);else if(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 F(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 M(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 G=class{#t;#i;#n;#r;#s=/* @__PURE__ */new Set;#o;#a;#c=0;#l=[];#u=!1;#d;#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.#r=t.target,this.#o={data:e,isLoading:!0,status:"idle",error:void 0,revision:0},this.#a=this.#o}subscribe=e=>(this.#s.add(e),this.#u||this.#p(),()=>{this.#s.delete(e),0===this.#s.size&&this.#g()});getSnapshot=()=>this.#a;getServerSnapshot=()=>this.#o;#p(){"undefined"==typeof window&&void 0===this.#r||(this.#u=!0,this.#i.attach(this.#r??window),E.sendReady(this.#v(),this.#n.enumerate()))}#g(){this.#u&&(this.#u=!1,this.#c+=1,this.#i.detach(),this.#t.destroy())}#v(){const e=this.#r??("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.#u&&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.#d=e.fieldSchemaJSON);const n=this.#d;void 0!==e.locale&&(this.#h=e.locale);const r=void 0===n?e.locale:this.#h;if(this.#m({...this.#a,isLoading:!0}),!this.#y(i))return;this.#l=[];const s=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!==r?{locale:r}:{}});this.#y(i)&&"superseded"!==s.status&&("unavailable"!==s.status?this.#m({data:s.doc,isLoading:!1,status:"live",error:void 0,revision:i}):this.#m({...this.#a,isLoading:!1,status:"unavailable",error:new Error(`live preview: ${M(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.#s])e()}}},z="__livePreviewRouteRefresh";function registerRouteRefresh(e){const t="undefined"==typeof window?void 0:window;return void 0===t?()=>{}:(t[z]=e,()=>{t[z]===e&&(t[z]=void 0)})}function _(e){const t=e.current;e.current=[];for(const e of t)e()}function J({refresh:n}){const[r,s]=e(),o=t([]);return i(()=>{r||_(o)}),i(()=>{const e=registerRouteRefresh(()=>new Promise(e=>{o.current.push(e),s(()=>{n()})}));return()=>{e(),_(o)}},[n,s]),null}function V(e){const i=function(e){const{initialData:i,serverURL:n,apiRoute:r,depth:s,allowedOrigins:o,eventSourcePolicy:a,enableReferrerDetection:c,enableLocalhostMatching:l,fetchFn:u,target:d}=e,h=[n,r??"",String(s??""),(o??[]).join(" "),a??"",String(c??""),String(l??"")].join(" | "),f=t(null);return f.current?.key===h&&f.current.target===d||(f.current={key:h,target:d,session:new G(i,{serverURL:n,...void 0!==r?{apiRoute:r}:{},...void 0!==s?{depth:s}:{},...void 0!==o?{allowedOrigins:o}:{},...void 0!==a?{eventSourcePolicy:a}:{},...void 0!==c?{enableReferrerDetection:c}:{},...void 0!==l?{enableLocalhostMatching:l}:{},...void 0!==u?{fetchFn:u}:{},...void 0!==d?{target:d}:{}})}),f.current.session}(e);return n(i.subscribe,i.getSnapshot,i.getServerSnapshot)}export{J as LivePreviewRouteRefresh,registerRouteRefresh,V as useLivePreviewDocument};
|
|
3
3
|
//# sourceMappingURL=index.js.map
|