@sanity/preview-kit-compat
Advanced tools
Comparing version 1.0.1 to 1.0.2
# Changelog | ||
## [1.0.2](https://github.com/sanity-io/visual-editing/compare/preview-kit-compat-v1.0.1...preview-kit-compat-v1.0.2) (2023-11-29) | ||
### Bug Fixes | ||
* **deps:** update dependency @sanity/client to ^6.9.0 ([#434](https://github.com/sanity-io/visual-editing/issues/434)) ([c7c8ec5](https://github.com/sanity-io/visual-editing/commit/c7c8ec59e6503060eb60800bac4a494e39c1595c)) | ||
* **deps:** update dependency @sanity/client to ^6.9.1 ([#437](https://github.com/sanity-io/visual-editing/issues/437)) ([6974b0d](https://github.com/sanity-io/visual-editing/commit/6974b0d6fabd9911af40f6a1de645473b47df19d)) | ||
* only fire documents in use when in frame ([6faf032](https://github.com/sanity-io/visual-editing/commit/6faf032e85aff969b1defafd7ddcf55668ebfd8b)) | ||
## [1.0.1](https://github.com/sanity-io/visual-editing/compare/preview-kit-compat-v1.0.0...preview-kit-compat-v1.0.1) (2023-11-16) | ||
@@ -4,0 +13,0 @@ |
@@ -1,1 +0,1 @@ | ||
import{useState as n,useEffect as e}from"react";let t;const o=new Uint8Array(16);function i(){if(!t&&(t="undefined"!=typeof crypto&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto),!t))throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");return t(o)}const a=[];for(let n=0;n<256;++n)a.push((n+256).toString(16).slice(1));var s={randomUUID:"undefined"!=typeof crypto&&crypto.randomUUID&&crypto.randomUUID.bind(crypto)};function c(n,e,t){if(s.randomUUID&&!e&&!n)return s.randomUUID();const o=(n=n||{}).random||(n.rng||i)();if(o[6]=15&o[6]|64,o[8]=63&o[8]|128,e){t=t||0;for(let n=0;n<16;++n)e[t+n]=o[n];return e}return function(n,e=0){return a[n[e+0]]+a[n[e+1]]+a[n[e+2]]+a[n[e+3]]+"-"+a[n[e+4]]+a[n[e+5]]+"-"+a[n[e+6]]+a[n[e+7]]+"-"+a[n[e+8]]+a[n[e+9]]+"-"+a[n[e+10]]+a[n[e+11]]+a[n[e+12]]+a[n[e+13]]+a[n[e+14]]+a[n[e+15]]}(o)}const r=1e3,d=3e3,f=["channel/disconnect","channel/response"],u=["handshake/syn","handshake/syn-ack","handshake/ack"],p=n=>Boolean(n)&&"object"==typeof n&&!Array.isArray(n),h=n=>f.some((e=>e===n)),l=n=>u.some((e=>e===n)),g=n=>"channel/heartbeat"===n;function m(n){const e=window.self!==window.top,t=n.connections.map((n=>({buffer:[],config:n,heartbeat:null,id:null,status:"fresh"})));function o(n){n.heartbeat&&window.clearInterval(n.heartbeat)}function i(e,t){const i=e.status;i!==t&&(e.status=t,n.onStatusUpdate?.(t,i,e),"connected"===t&&(function(n){const e=[...n.buffer];n.buffer.splice(0,n.buffer.length),e.forEach((({type:e,data:t})=>{a(n,e,t)}))}(e),function(n){if(o(n),n.config.heartbeat){const e="number"==typeof n.config.heartbeat?n.config.heartbeat:d;n.heartbeat=window.setInterval((()=>{u("channel/heartbeat",void 0,[n])}),e)}}(e)),"disconnected"===t&&o(e))}function a(e,o,i){return new Promise(((a,s)=>{const d={id:c(),type:o,connectionId:e.id,from:n.id,to:e.config.id,data:i},f=h(o),u=l(o),p=g(o),m=t.find((n=>n.id===e.id&&"connected"===n.status));if(f||u||p||m){if(!f||p){const t=setTimeout((()=>{d.connectionId===e.id?s({reason:`Received no response to message '${d.id}' on client '${n.id}'`,msg:d,connection:e}):a(d.id)}),r),o=n=>{const{data:e}=n;"channel/response"===e.type&&e.data?.responseTo&&e.data.responseTo===d.id&&(window.removeEventListener("message",o,!1),clearTimeout(t),a(d.id))};window.addEventListener("message",o,!1)}try{const n=u?"*":e.config.targetOrigin;return e.config.target.postMessage(d,{targetOrigin:n}),void((f||u)&&a(d.id))}catch(t){console.error("Failed to postMessage",t,{msg:d,connection:e}),s({reason:`Failed to postMessage '${d.id}' on client '${n.id}'`,msg:d,connection:e})}}"fresh"!==e.status&&"connecting"!==e.status||(e.buffer.push({type:o,data:i}),a(d.id)),s({reason:`Will not send message '${d.id}' on client '${n.id}'`,msg:d,connection:e})}))}function s(n,e,t){return Promise.allSettled(n.map((n=>a(n,e,t))))}function f(e){const o=function(e){const{source:o,origin:i,data:a}=e;if(p(a))return t.find((e=>n.id===a.to&&e.config.id===a.from&&e.config.target===o&&(e.config.targetOrigin===i||"*"===e.config.targetOrigin)&&(e.id===a.connectionId||l(a.type))))}(e);if(!o)return;const{data:s}=e;if(l(s.type))!function(n,e){if("handshake/syn"===e.data.type){const t=e.data.data?.id||n.id;n.id=t,a(n,"handshake/syn-ack",{id:t})}if("handshake/syn-ack"===e.data.type){const t=e.data.data?.id||n.id;n.id=t,i(n,"connected"),a(n,"handshake/ack",{id:t})}if("handshake/ack"===e.data.type){const t=e.data.data?.id||n.id;n.id=t,i(n,"connected")}}(o,e);else if("channel/disconnect"===s.type)i(o,"disconnected");else if("channel/response"===s.type);else{const e=[s.type,s.data];n.handler(...e),a(o,"channel/response",{responseTo:s.id})}}async function u(n,e,o){const a=await s(o||t,n,e);return a.forEach((n=>{if("rejected"===n.status){const e=t.find((e=>"connected"===e.status&&e.id===n.reason.connection.id));e&&i(e,"unhealthy")}})),a}return function(){window.addEventListener("message",f,!1);const n=t.filter((n=>["disconnected","fresh","unhealthy"].includes(n.status)));Promise.all(n.map((n=>(i(n,"connecting"),a(n,"handshake/syn",{id:c()})))))}(),{disconnect:function(){window.removeEventListener("message",f,!1);const n=t.filter((({status:n})=>"connecting"===n||"connected"===n));n.length&&(s(n,"channel/disconnect"),n.forEach((n=>{i(n,"disconnected")})))},inFrame:e,send:u}}function y(t,o,i,a){const[s,c]=n(),[r,d]=n(!1);e((()=>{const n=new URL(o||"/",location.origin).origin,e=m({id:"preview-kit",onStatusUpdate(n){"connected"===n?d(!0):"disconnected"!==n&&"unhealthy"!==n||d(!1)},connections:[{target:parent,targetOrigin:n,id:"presentation"}],handler:()=>{}}),t=setTimeout((()=>c(e)),0);return()=>{clearTimeout(t),e.disconnect(),c(void 0)}}),[o,a,i]);const f=JSON.stringify(Array.from(t.keys()));e((()=>{"[]"!==f&&s&&r&&s.send("preview-kit/documents",{projectId:i,dataset:a,perspective:"previewDrafts",documents:Array.from(t.values())})}),[f,s,r,a,t,i])}export{y as useDocumentsInUse};//# sourceMappingURL=index.js.map | ||
import{useState as n,useEffect as e}from"react";let t;const o=new Uint8Array(16);function i(){if(!t&&(t="undefined"!=typeof crypto&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto),!t))throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");return t(o)}const a=[];for(let n=0;n<256;++n)a.push((n+256).toString(16).slice(1));var s={randomUUID:"undefined"!=typeof crypto&&crypto.randomUUID&&crypto.randomUUID.bind(crypto)};function c(n,e,t){if(s.randomUUID&&!e&&!n)return s.randomUUID();const o=(n=n||{}).random||(n.rng||i)();if(o[6]=15&o[6]|64,o[8]=63&o[8]|128,e){t=t||0;for(let n=0;n<16;++n)e[t+n]=o[n];return e}return function(n,e=0){return a[n[e+0]]+a[n[e+1]]+a[n[e+2]]+a[n[e+3]]+"-"+a[n[e+4]]+a[n[e+5]]+"-"+a[n[e+6]]+a[n[e+7]]+"-"+a[n[e+8]]+a[n[e+9]]+"-"+a[n[e+10]]+a[n[e+11]]+a[n[e+12]]+a[n[e+13]]+a[n[e+14]]+a[n[e+15]]}(o)}const r=1e3,d=3e3,f=["channel/disconnect","channel/response"],u=["handshake/syn","handshake/syn-ack","handshake/ack"],p=n=>Boolean(n)&&"object"==typeof n&&!Array.isArray(n),h=n=>f.some((e=>e===n)),l=n=>u.some((e=>e===n)),g=n=>"channel/heartbeat"===n;function m(n){const e=window.self!==window.top,t=n.connections.map((n=>({buffer:[],config:n,heartbeat:null,id:null,status:"fresh"})));function o(n){n.heartbeat&&window.clearInterval(n.heartbeat)}function i(e,t){const i=e.status;i!==t&&(e.status=t,n.onStatusUpdate?.(t,i,e),"connected"===t&&(function(n){const e=[...n.buffer];n.buffer.splice(0,n.buffer.length),e.forEach((({type:e,data:t})=>{a(n,e,t)}))}(e),function(n){if(o(n),n.config.heartbeat){const e="number"==typeof n.config.heartbeat?n.config.heartbeat:d;n.heartbeat=window.setInterval((()=>{u("channel/heartbeat",void 0,[n])}),e)}}(e)),"disconnected"===t&&o(e))}function a(e,o,i){return new Promise(((a,s)=>{const d={id:c(),type:o,connectionId:e.id,from:n.id,to:e.config.id,data:i},f=h(o),u=l(o),p=g(o),m=t.find((n=>n.id===e.id&&"connected"===n.status));if(f||u||p||m){if(!f||p){const t=setTimeout((()=>{d.connectionId===e.id?s({reason:`Received no response to message '${d.id}' on client '${n.id}'`,msg:d,connection:e}):a(d.id)}),r),o=n=>{const{data:e}=n;"channel/response"===e.type&&e.data?.responseTo&&e.data.responseTo===d.id&&(window.removeEventListener("message",o,!1),clearTimeout(t),a(d.id))};window.addEventListener("message",o,!1)}try{const n=u?"*":e.config.targetOrigin;return e.config.target.postMessage(d,{targetOrigin:n}),void((f||u)&&a(d.id))}catch(t){console.error("Failed to postMessage",t,{msg:d,connection:e}),s({reason:`Failed to postMessage '${d.id}' on client '${n.id}'`,msg:d,connection:e})}}"fresh"!==e.status&&"connecting"!==e.status||(e.buffer.push({type:o,data:i}),a(d.id)),s({reason:`Will not send message '${d.id}' on client '${n.id}'`,msg:d,connection:e})}))}function s(n,e,t){return Promise.allSettled(n.map((n=>a(n,e,t))))}function f(e){const o=function(e){const{source:o,origin:i,data:a}=e;if(p(a))return t.find((e=>n.id===a.to&&e.config.id===a.from&&e.config.target===o&&(e.config.targetOrigin===i||"*"===e.config.targetOrigin)&&(e.id===a.connectionId||l(a.type))))}(e);if(!o)return;const{data:s}=e;if(l(s.type))!function(n,e){if("handshake/syn"===e.data.type){const t=e.data.data?.id||n.id;n.id=t,a(n,"handshake/syn-ack",{id:t})}if("handshake/syn-ack"===e.data.type){const t=e.data.data?.id||n.id;n.id=t,i(n,"connected"),a(n,"handshake/ack",{id:t})}if("handshake/ack"===e.data.type){const t=e.data.data?.id||n.id;n.id=t,i(n,"connected")}}(o,e);else if("channel/disconnect"===s.type)i(o,"disconnected");else if("channel/response"===s.type);else{const e=[s.type,s.data];n.handler(...e),a(o,"channel/response",{responseTo:s.id})}}async function u(n,e,o){const a=await s(o||t,n,e);return a.forEach((n=>{if("rejected"===n.status){const e=t.find((e=>"connected"===e.status&&e.id===n.reason.connection.id));e&&i(e,"unhealthy")}})),a}return function(){window.addEventListener("message",f,!1);const n=t.filter((n=>["disconnected","fresh","unhealthy"].includes(n.status)));Promise.all(n.map((n=>(i(n,"connecting"),a(n,"handshake/syn",{id:c()})))))}(),{disconnect:function(){window.removeEventListener("message",f,!1);const n=t.filter((({status:n})=>"connecting"===n||"connected"===n));n.length&&(s(n,"channel/disconnect"),n.forEach((n=>{i(n,"disconnected")})))},inFrame:e,send:u}}function y(t,o,i,a){const[s,c]=n(),[r,d]=n(!1);e((()=>{if(window.self===window.top)return;const n=new URL(o||"/",location.origin).origin,e=m({id:"preview-kit",onStatusUpdate(n){"connected"===n?d(!0):"disconnected"!==n&&"unhealthy"!==n||d(!1)},connections:[{target:parent,targetOrigin:n,id:"presentation"}],handler:()=>{}}),t=setTimeout((()=>c(e)),0);return()=>{clearTimeout(t),e.disconnect(),c(void 0)}}),[o,a,i]);const f=JSON.stringify(Array.from(t.keys()));e((()=>{"[]"!==f&&s&&r&&s.send("preview-kit/documents",{projectId:i,dataset:a,perspective:"previewDrafts",documents:Array.from(t.values())})}),[f,s,r,a,t,i])}export{y as useDocumentsInUse};//# sourceMappingURL=index.js.map |
{ | ||
"name": "@sanity/preview-kit-compat", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"homepage": "https://github.com/sanity-io/visual-editing/tree/main/packages/preview-kit-compat#readme", | ||
@@ -105,8 +105,8 @@ "bugs": { | ||
"devDependencies": { | ||
"@sanity/client": "^6.8.6", | ||
"@sanity/pkg-utils": "^3.2.3", | ||
"@types/react": "^18.2.37", | ||
"@typescript-eslint/eslint-plugin": "^6.11.0", | ||
"@typescript-eslint/parser": "^6.11.0", | ||
"eslint": "^8.53.0", | ||
"@sanity/client": "^6.9.1", | ||
"@sanity/pkg-utils": "^3.2.5", | ||
"@types/react": "^18.2.39", | ||
"@typescript-eslint/eslint-plugin": "^6.13.1", | ||
"@typescript-eslint/parser": "^6.13.1", | ||
"eslint": "^8.54.0", | ||
"eslint-config-prettier": "^9.0.0", | ||
@@ -116,3 +116,3 @@ "eslint-plugin-prettier": "^5.0.1", | ||
"react": "^18.2.0", | ||
"typescript": "^5.2.2", | ||
"typescript": "^5.3.2", | ||
"channels": "0.0.0", | ||
@@ -122,3 +122,3 @@ "visual-editing-helpers": "0.0.0" | ||
"peerDependencies": { | ||
"@sanity/client": "^6.8.6", | ||
"@sanity/client": "^6.9.1", | ||
"react": "^18.2.0" | ||
@@ -125,0 +125,0 @@ }, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
66282