@sanity/visual-editing
Version:
[](https://npm-stat.com/charts.html?package=@sanity/visual-editing) [](https://
2 lines (1 loc) • 19.3 kB
JavaScript
;var e=require("react-compiler-runtime"),t=require("react"),n=require("use-effect-event"),o=require("./context.cjs"),r=require("./mutations.cjs"),i=require("./transformSanityNodeData.cjs"),a=require("@vercel/stega");const s=[];for(let e=0;e<256;++e)s.push((e+256).toString(16).slice(1));let c;const u=new Uint8Array(16);function l(){if(!c){if(typeof crypto>"u"||!crypto.getRandomValues)throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");c=crypto.getRandomValues.bind(crypto)}return c(u)}var d={randomUUID:typeof crypto<"u"&&crypto.randomUUID&&crypto.randomUUID.bind(crypto)};function y(e,t,n){if(d.randomUUID&&!t&&!e)return d.randomUUID();const o=(e=e||{}).random||(e.rng||l)();return o[6]=15&o[6]|64,o[8]=63&o[8]|128,function(e,t=0){return(s[e[t+0]]+s[e[t+1]]+s[e[t+2]]+s[e[t+3]]+"-"+s[e[t+4]]+s[e[t+5]]+"-"+s[e[t+6]]+s[e[t+7]]+"-"+s[e[t+8]]+s[e[t+9]]+"-"+s[e[t+10]]+s[e[t+11]]+s[e[t+12]]+s[e[t+13]]+s[e[t+14]]+s[e[t+15]]).toLowerCase()}(o)}function m(e){const t=e.getBoundingClientRect();return{x:t.x+scrollX,y:t.y+scrollY,w:t.width,h:t.height}}function p(e,t,n){return"x"===n?{x:e.x+t,y:e.y,w:e.w-2*t,h:e.h}:{x:e.x,y:e.y+t,w:e.w,h:e.h-2*t}}function f(e,t){const{x1:n,y1:o,x2:r,y2:i}=e,{x1:a,y1:s,x2:c,y2:u}=t;if(n===r&&o===i||a===c&&s===u)return!1;const l=(u-s)*(r-n)-(c-a)*(i-o);if(0===l)return!1;const d=((c-a)*(o-s)-(u-s)*(n-a))/l,y=((r-n)*(o-s)-(i-o)*(n-a))/l;if(d<0||d>1||y<0||y>1)return!1;return{x:n+d*(r-n),y:o+d*(i-o)}}function g(e,t){return e.x===t.x&&e.y===t.y&&e.w===t.w&&e.h===t.h}function h(e,t){const n=[{x1:t.x,y1:t.y,x2:t.x+t.w,y2:t.y},{x1:t.x+t.w,y1:t.y,x2:t.x+t.w,y2:t.y+t.h},{x1:t.x+t.w,y1:t.y+t.h,x2:t.x,y2:t.y+t.h},{x1:t.x,y1:t.y+t.h,x2:t.x,y2:t.y}],o=[];for(let t=0;t<n.length;t++){const r=f(e,n[t]);if(r){let e=!1;for(let t=0;t<o.length;t++)o[t].x===r.x&&o[t].y===r.y&&(e=!0);e||o.push(r)}}return 0!==o.length&&o.sort(((t,n)=>v(t,{x:e.x1,y:e.y1})-v(n,{x:e.x1,y:e.y1})))}function v(e,t){const n=e.x-t.x,o=e.y-t.y;return Math.sqrt(n*n+o*o)}function w(e,t,n){const o={x:e.x1,y:e.y1};if(t.some((e=>function(e,t){const n=e.x>=t.x&&e.x<=t.x+t.w,o=e.y>=t.y&&e.y<=t.y+t.h;return n&&o}(o,p(e,Math.min(e.w,e.h)/10,"horizontal"===n?"x":"y")))))return null;let r,i;for(const a of t){const t=h(e,p(a,Math.min(a.w,a.h)/10,"horizontal"===n?"x":"y"));if(t){const e=t[0];r?v(o,e)<v(o,r)&&(r=e,i=a):(r=e,i=a)}}return i||null}function x(e,t,n){const{x:o,y:r,w:i,h:a}=e,{x:s,y:c}=n;return{x:s+(o-s)*t,y:c+(r-c)*t,w:i*t,h:a*t}}function E(e){const t=Math.max(0,Math.min(...e.map((e=>e.y)))),n=Math.min(document.body.scrollHeight,Math.max(...e.map((e=>e.y+e.h))));return{min:t,max:n,height:n-t}}function b(e,t){return t.find((t=>g(m(t.elements.element),e)))?.sanity}function L(e,t,n){return Object.values(t).every((e=>null===e))?null:"horizontal"===n?{left:t.left?{rect:t.left,sanity:b(t.left,e)}:null,right:t.right?{rect:t.right,sanity:b(t.right,e)}:null}:{top:t.top?{rect:t.top,sanity:b(t.top,e)}:null,bottom:t.bottom?{rect:t.bottom,sanity:b(t.bottom,e)}:null}}function M(e){const t=document.body.getBoundingClientRect();return{x:Math.max(t.x,Math.min(e.clientX,t.x+t.width)),y:e.clientY+window.scrollY}}function S(e,t,n){const o=m(t),r=[...t.querySelectorAll(":where(h1, h2, h3, h4, p, a, img, span, button):not(:has(*))")];e.x<=o.x&&(e.x=o.x),e.x>=o.x+o.w&&(e.x=o.x+o.w),e.y>=o.y+o.h&&(e.y=o.y+o.h),e.y<=o.y&&(e.y=o.y);const i=r.map((e=>{const t=x(m(e),n,{x:o.x,y:o.y});return{x:t.x-o.x,y:t.y-o.y,w:t.w,h:t.h,tagName:e.tagName}}));return{offsetX:(o.x-e.x)*n,offsetY:(o.y-e.y)*n,w:o.w*n,h:o.h*n,maxWidth:o.w*n*.75,childRects:i}}function T(e){const t=function(e){const t=Math.max(0,Math.min(...e.map((e=>e.x)))),n=Math.min(document.body.offsetWidth,Math.max(...e.map((e=>e.x+e.w))));return{min:t,max:n,width:n-t}}(e),n=E(e),o=t.min>8&&t.min+t.width<=window.innerWidth-8,r=n.min>8&&n.min+n.height<=document.body.scrollHeight-8,i=o&&r;return{x:i?t.min-8:t.min,y:i?n.min-8:n.min,w:i?t.width+16:t.width,h:i?n.height+16:n.height}}async function k(e,t,n,o,r,i){return new Promise((a=>{const s=window.getComputedStyle(t).transform;if(1===new DOMMatrix(s).a)return;const c=n-window.innerHeight,u=scrollY;(e-=window.innerHeight/2)<0&&(e=0),t.addEventListener("transitionend",(()=>{t.style.transition="none",t.style.transform="none",scrollTo({top:e,behavior:"instant"}),setTimeout((()=>{o({type:"overlay/dragEndMinimapTransition"}),o({type:"overlay/dragToggleMinimap",display:!1})}),2*r),a()}),{once:!0}),o({type:"overlay/dragStartMinimapTransition"}),t.style.transform=`translateY(${Math.max(u-e,-c+u)}px) scale(1)`,i&&(document.body.style.overflow=i.body.overflow,document.body.style.height=i.body.height,document.documentElement.style.overflow=i.documentElement.overflow,document.documentElement.style.height=i.documentElement.height)}))}let I=!1,A={x:0,y:0},C={x:0,y:0},R=typeof document>"u"?0:document.documentElement.scrollHeight,U=null;function D(e){const{mouseEvent:t,element:n,overlayGroup:o,handler:r,target:i,onSequenceStart:a,onSequenceEnd:s}=e;if(0!==t.button)return;window.focus();const c=150;let u=o.map((e=>m(e.elements.element)));const l=n.getAttribute("data-sanity-drag-flow")||((b=u).some((e=>b.filter((t=>!g(e,t))).some((t=>e.y===t.y))))?"horizontal":"vertical"),d=n.getAttribute("data-sanity-drag-group"),y=!!n.getAttribute("data-sanity-drag-minimap-disable"),p=!!n.getAttribute("data-sanity-drag-prevent-default"),f=n.getAttribute("data-unstable_sanity-drag-document-height"),h=n.getAttribute("data-unstable_sanity-drag-group-height");var b;let D=null;const N=M(t),G=document.body,{minYScaled:O,scaleFactor:q}=function(e,t){let n=t||E(e).height;n+=200;const o=n>window.innerHeight?window.innerHeight/n:1,r=e.map((e=>x(e,o,{x:window.innerWidth/2,y:0}))),{min:i}=E(r);return{scaleFactor:o,minYScaled:i-100*o}}(u,h?~~h:null);let P=!1,_=!1,Y=!0;I||(U={body:{overflow:window.getComputedStyle(document.body).overflow,height:window.getComputedStyle(document.body).height},documentElement:{overflow:window.getComputedStyle(document.documentElement).overflow,height:window.getComputedStyle(document.documentElement).height}},R=f?~~f:document.documentElement.scrollHeight);const H=setInterval((()=>{u=o.map((e=>m(e.elements.element)))}),c),j=()=>{if(q>=1)return;const e=S(C,n,q);r({type:"overlay/dragUpdateSkeleton",skeleton:e}),r({type:"overlay/dragToggleMinimapPrompt",display:!1}),r({type:"overlay/dragToggleMinimap",display:!0}),I=!0,async function(e,t,n,o,r){return new Promise((i=>{e.addEventListener("transitionend",(()=>{setTimeout((()=>{o({type:"overlay/dragEndMinimapTransition"})}),2*r),i()}),{once:!0}),o({type:"overlay/dragStartMinimapTransition"}),o({type:"overlay/dragToggleMinimap",display:!0}),document.body.style.overflow="hidden",document.body.style.height="100%",document.documentElement.style.overflow="initial",document.documentElement.style.height="100%",setTimeout((()=>{e.style.transformOrigin="50% 0px",e.style.transition="transform 150ms ease",e.style.transform=`translate3d(0px, ${-n+scrollY}px, 0px) scale(${t})`}),25)}))}(G,q,O,r,c).then((()=>{setTimeout((()=>{r({type:"overlay/dragUpdateGroupRect",groupRect:T(u)})}),300)}))},z=e=>{Math.abs(e.deltaY)>=10&&q<1&&!I&&!_&&!y&&Y&&(r({type:"overlay/dragToggleMinimapPrompt",display:!0}),_=!0),e.shiftKey&&!I&&!y&&(window.dispatchEvent(new CustomEvent("unstable_sanity/dragApplyMinimap")),setTimeout((()=>{j()}),50))},V=e=>{if(e.preventDefault(),C=M(e),A=function(e){const t=document.body,n=window.getComputedStyle(t).transform;if("none"===n)return{x:e.x,y:e.y};const o=new DOMMatrix(n).inverse(),r=new DOMPoint(e.x,e.y).matrixTransform(o);return{x:r.x,y:r.y}}(C),Math.abs(v(C,N))<4)return;if(!P){const e=T(u),t=S(C,n,1);r({type:"overlay/dragStart",flow:l}),r({type:"overlay/dragUpdateSkeleton",skeleton:t}),r({type:"overlay/dragUpdateGroupRect",groupRect:e}),P=!0,a()}r({type:"overlay/dragUpdateCursorPosition",x:C.x,y:C.y}),e.shiftKey&&!I&&!y&&(window.dispatchEvent(new CustomEvent("unstable_sanity/dragApplyMinimap")),setTimeout((()=>{j()}),50));const t=function(e,t,n){if("horizontal"===n){const o={x1:e.x,y1:e.y,x2:e.x-1e8,y2:e.y},r={x1:e.x,y1:e.y,x2:e.x+1e8,y2:e.y};return{left:w(o,t,n),right:w(r,t,n)}}{const o={x1:e.x,y1:e.y,x2:e.x,y2:e.y-1e8},r={x1:e.x,y1:e.y,x2:e.x,y2:e.y+1e8};return{top:w(o,t,n),bottom:w(r,t,n)}}}(C,u,l);JSON.stringify(D)!==JSON.stringify(t)&&(D=t,r({type:"overlay/dragUpdateInsertPosition",insertPosition:L(o,D,l)}))},W=()=>{Y=!1,r({type:"overlay/dragEnd",target:i,insertPosition:D?L(o,D,l):null,dragGroup:d,flow:l,preventInsertDefault:p}),_&&r({type:"overlay/dragToggleMinimapPrompt",display:!1}),I||(clearInterval(H),s(),J(),B()),$()},X=e=>{if("Shift"===e.key&&I){I=!1;const e=S(C,n,1/q);r({type:"overlay/dragUpdateSkeleton",skeleton:e}),window.dispatchEvent(new CustomEvent("unstable_sanity/dragResetMinimap")),setTimeout((()=>{k(A.y,G,R,r,c,U)}),50),r({type:"overlay/dragUpdateGroupRect",groupRect:null}),Y||(clearInterval(H),$(),J(),B(),s())}},F=()=>{r({type:"overlay/dragUpdateGroupRect",groupRect:null}),window.dispatchEvent(new CustomEvent("unstable_sanity/dragResetMinimap")),setTimeout((()=>{k(A.y,G,R,r,c,U).then((()=>{I=!1}))}),50),clearInterval(H),$(),J(),B(),s()},$=()=>{window.removeEventListener("mousemove",V),window.removeEventListener("wheel",z),window.removeEventListener("mouseup",W)},B=()=>{window.removeEventListener("keyup",X)},J=()=>{window.removeEventListener("blur",F)};window.addEventListener("blur",F),window.addEventListener("keyup",X),window.addEventListener("wheel",z),window.addEventListener("mousemove",V),window.addEventListener("mouseup",W)}const N=e=>e instanceof HTMLElement||e instanceof SVGElement;function G(e){const{display:t}=window.getComputedStyle(e);if("inline"!==t)return e;const n=e.parentElement;return n?G(n):null}const O=e=>e&&N(e)?e.dataset?.sanityOverlayElement?e:O(e.parentElement):null;function q(e,t=!1){return n=e,a.VERCEL_STEGA_REGEX.lastIndex=0,a.VERCEL_STEGA_REGEX.test(n)?function(e,t=!1){try{const n=a.vercelStegaDecode(e);return n&&"sanity.io"===n.origin?(t&&(n.href=n.href?.replace(".alt","")),n):null}catch(t){return console.error("Failed to decode stega for string: ",e,"with the original error: ",t),null}}(e,t):null;var n}const P=e=>e.nodeType===Node.ELEMENT_NODE,_=e=>"IMG"===e.tagName,Y=e=>"TIME"===e.tagName,H=e=>"SVG"===e.tagName.toUpperCase();function j(e){return"path"in e}function z(e,t){let n=e.split("."),o=t.split(".");const r=Math.min(n.length,o.length);return n=n.slice(0,r).reverse(),o=o.slice(0,r).reverse(),n.reduce(((e,t,n)=>t===o[n]?[...e,t]:[]),[]).reverse().join(".")}function V(e){if(!e.length||!e.map((e=>j(e))).every(((e,t,n)=>e===n[0])))return;if(!j(e[0]))return e[0];const t=e.filter(j);let n=e[0];const o=["projectId","dataset","id","baseUrl","workspace","tool"];for(let e=1;e<t.length;e++){const r=t[e];if(o.some((e=>r[e]!==n?.[e]))){n=void 0;break}n.path=z(n.path,r.path)}return n}function W(e){const t=[];function n(e,n){const o=i.d(n);if(!o)return;const r=G(e);r&&t.push({elements:{element:e,measureElement:r},sanity:o})}if(e)for(const o of e.childNodes){const{nodeType:e,parentElement:r,textContent:i}=o;if(P(o)&&void 0!==o.dataset?.sanityEditTarget){const e=W(o).map((({sanity:e})=>e));if(!e.map((e=>j(e))).every(((e,t,n)=>e===n[0])))continue;const n=V(e);n&&t.push({elements:{element:o,measureElement:o},sanity:n})}else if(e===Node.TEXT_NODE&&r&&i){const e=q(i);if(!e)continue;n(r,e)}else if(P(o)){if("SCRIPT"===o.tagName||"SANITY-VISUAL-EDITING"===o.tagName)continue;if(o.dataset?.sanity)n(o,o.dataset.sanity);else if(o.dataset?.sanityEditInfo)n(o,o.dataset.sanityEditInfo);else{if(_(o)){const e=q(o.alt,!0);if(!e)continue;n(o,e);continue}if(Y(o)){const e=q(o.dateTime,!0);if(!e)continue;n(o,e)}else if(H(o)){if(!o.ariaLabel)continue;const e=q(o.ariaLabel,!0);if(!e)continue;n(o,e)}}t.push(...W(o))}}return t}function X(e){const t=e.lastIndexOf(".");return e.substring(t,e.length).includes("[")}function F(e){if(!X(e))return null;const t=e.split(".");return t[t.length-1]=t[t.length-1].replace(/\[.*?\]/g,"[]"),t.join(".")}function $(e,t){return!(!X(e.path)||!X(t.path))&&F(e.path)===F(t.path)}function B(e,t,n,o){if(!e.getAttribute("data-sanity")||e.getAttribute("data-sanity-drag-disable")||!t||!j(t)||!X(t.path))return null;const r=e.getAttribute("data-sanity-drag-group"),i=[...n].reduce(((e,n)=>{const i=o.get(n),a=n.getAttribute("data-sanity-drag-disable"),s=n.getAttribute("data-sanity-drag-group"),c=null!==n.getAttribute("data-sanity"),u=null===r||r===s;return i&&!a&&j(i.sanity)&&$(t,i.sanity)&&u&&c&&e.push(i),e}),[]);return i.length<=1?null:i}const J=t.createContext(null);exports.S=J,exports.c=function({handler:e,overlayElement:t,inFrame:n,optimisticActorReady:o}){let r=!1;const i=new Map,a=new WeakMap,s=new Set,c=new WeakMap,u=new WeakMap,l=n;let d,p,f,g=!1,h=[];const v=()=>h[h.length-1];function w(e,t){e.removeEventListener("click",t.click,{capture:!0}),e.removeEventListener("contextmenu",t.contextmenu,{capture:!0}),e.removeEventListener("mousemove",t.mousemove,{capture:!0}),e.removeEventListener("mousedown",t.mousedown,{capture:!0}),e.removeEventListener("mouseenter",t.mouseenter),e.removeEventListener("mouseleave",t.mouseleave)}function x({id:t,elements:n,handlers:o}){const{element:r,measureElement:i}=n;(function(e,t){e.addEventListener("click",t.click,{capture:!0}),e.addEventListener("contextmenu",t.contextmenu,{capture:!0}),e.addEventListener("mousemove",t.mousemove,{once:!0,capture:!0}),e.addEventListener("mousedown",t.mousedown,{capture:!0})})(r,o),d.observe(i),e({type:"element/activate",id:t})}function E({id:t,elements:n,handlers:o}){const{element:r,measureElement:i}=n;w(r,o),d.unobserve(i),h=h.filter((e=>e!==r)),e({type:"element/deactivate",id:t})}function b(t){if(n&&o)for(const n of h){if(t===n){const n=a.get(t)?.sanity;if(!n||!j(n))return;if(B(t,n,s,a)){const n=t.style.cursor;n&&u.set(t,n),e({type:"overlay/setCursor",element:t,cursor:"move"});continue}}L(n)}}function L(t){const n=u.get(t);e({type:"overlay/setCursor",element:t,cursor:n})}function M({elements:u,sanity:d}){const{element:f,measureElement:w}=u,E={click(t){const n=t.target;if(f===v()&&f.contains(n)){l&&(t.preventDefault(),t.stopPropagation());const n=a.get(f)?.sanity;n&&!g&&e({type:"element/click",id:M,sanity:n})}},contextmenu(t){if(!("path"in d&&n&&o&&d.path.split(".").pop()?.includes("[_key==")))return;const r=t.target;f===v()&&f.contains(r)&&(l&&(t.preventDefault(),t.stopPropagation()),e({type:"element/contextmenu",id:M,position:{x:t.clientX,y:t.clientY},sanity:d}))},mousedown(t){if(t.preventDefault(),t.currentTarget!==h.at(-1)||f.getAttribute("data-sanity-drag-disable")||!n||!o)return;const r=a.get(f)?.sanity;if(!r||!j(r)||!X(r.path))return;const i=B(f,d,s,a);i&&D({element:f,handler:e,mouseEvent:t,overlayGroup:i,target:r,onSequenceStart:()=>{g=!0},onSequenceEnd:()=>{setTimeout((()=>{g=!1}),250)}})},mousemove(e){E.mouseenter(e);const t=e.currentTarget;t&&(t.addEventListener("mouseenter",E.mouseenter),t.addEventListener("mouseleave",E.mouseleave))},mouseenter(){document.querySelector("vercel-live-feedback")&&f.closest("[data-vercel-edit-info]")||f.closest("[data-vercel-edit-target]")||(h.push(f),e({type:"element/mouseenter",id:M,rect:m(f)}),b(f))},mouseleave(n){function o(){h.pop();const t=v();if(e({type:"element/mouseleave",id:M}),t){b(t);const n=a.get(t);n&&e({type:"element/mouseenter",id:n.id,rect:m(t)})}L(f)}const{relatedTarget:r}=n,i=O(r),s=t.contains(i);if(N(i)&&s)return function e(t){const n=r=>{const{relatedTarget:i}=r;O(i)?i&&N(i)&&(t.removeEventListener("mouseleave",n),e(i)):(t.removeEventListener("mouseleave",n),o())};t.addEventListener("mouseleave",n)}(i);o()}},M=y(),S={id:M,elements:u,sanity:d,handlers:E};s.add(f),c.set(w,f),i.set(M,f),a.set(f,S),p?.observe(f),e({type:"element/register",id:M,element:f,rect:m(f),sanity:d,dragDisabled:!!f.getAttribute("data-sanity-drag-disable")}),r&&x(S)}function S({elements:t,sanity:n}){const{element:o}=t,r=a.get(o);r&&(a.set(o,{...r,sanity:n}),e({type:"element/update",id:r.id,rect:m(o),sanity:n}))}function T(e){const t=W(e);for(const e of t){const{element:t}=e.elements;a.has(t)?S(e):M(e)}}function k(t){const n=a.get(t);if(n){const{id:o,handlers:r}=n;w(t,r),d.unobserve(t),a.delete(t),s.delete(t),i.delete(o),e({type:"element/unregister",id:o})}}function I(e){let n=!1;for(const o of e){const{target:e,type:r}=o,i="characterData"===r?e.parentElement:e;i===t||t.contains(i)||(n=!0,N(i)&&T({childNodes:[i]}))}if(n)for(const e of s)e.isConnected||k(e)}function A(t){const n=a.get(t);n&&e({type:"element/updateRect",id:n.id,rect:m(t)})}function C(e){for(const t of e){const e=t.target;if(N(e)){const t=c.get(e);if(!t)return;A(t)}}}function R(e){if(r)for(const t of e){const{target:e}=t,n=N(e)&&a.get(e);n&&(t.isIntersecting?x(n):E(n))}}function U(t){const n=O(t.target);n?"capture"===n.dataset.sanityOverlayElement&&(t.preventDefault(),t.stopPropagation()):(h=[],e({type:"overlay/blur"}))}function G(){for(const e of s)A(e)}function q(t){"Escape"===t.key&&(h=[],e({type:"overlay/blur"}))}function P(e){const{target:t}=e;if(t!==window.document&&N(t))for(const e of s)t.contains(e)&&A(e)}function _(){r||(p=new IntersectionObserver(R,{threshold:.3}),s.forEach((e=>p.observe(e))),e({type:"overlay/activate"}),r=!0)}function Y(){r&&(p?.disconnect(),s.forEach((e=>{const t=a.get(e);t&&E(t)})),e({type:"overlay/deactivate"}),r=!1)}return window.document.fonts.ready.then((()=>{for(const e of s)A(e)})),window.addEventListener("click",U),window.addEventListener("contextmenu",U),window.addEventListener("keydown",q),window.addEventListener("resize",G),window.addEventListener("scroll",P,{capture:!0,passive:!0}),d=new ResizeObserver(C),f=new MutationObserver(I),f.observe(document.body,{attributes:!0,characterData:!0,childList:!0,subtree:!0}),T(document.body),_(),{activate:_,deactivate:Y,destroy:function(){window.removeEventListener("click",U),window.removeEventListener("contextmenu",U),window.removeEventListener("keydown",q),window.removeEventListener("resize",G),window.removeEventListener("scroll",P),f.disconnect(),d.disconnect(),s.forEach((e=>{k(e)})),i.clear(),s.clear(),h=[],Y()}}},exports.s=$,exports.u=function(i,a){const s=e.c(16),[c,u]=t.useState(!0),[l,d]=t.useState(i),[y,m]=t.useState(null),[p,f]=t.useState(i),g=r.b();let h;s[0]!==a?(h=(e,t)=>(Array.isArray(a)?a:[a]).reduce(((t,n)=>n(t,{document:e.document,id:r.c(e.id),originalId:e.id,type:e.type})),t),s[0]=a,s[1]=h):h=s[1];const v=n.useEffectEvent(h);let w;s[2]!==i?(w=()=>f(i),s[2]=i,s[3]=w):w=s[3];const x=n.useEffectEvent(w);let E,b,L,M;return s[4]!==g||s[5]!==v||s[6]!==x?(E=()=>{if(o.i(g))return;let e;const n=g.on("rebased.local",(t=>{const n={document:t.document,id:t.id,originalId:r.c(t.id),type:"mutate"};d((e=>v(n,e))),m(n),x(),u(!1),clearTimeout(e)})),i=g.on("pristine",(()=>{e=setTimeout((()=>{t.startTransition((()=>u(!0)))}),15e3)}));return()=>{n.unsubscribe(),i.unsubscribe()}},b=[g,v,x],s[4]=g,s[5]=v,s[6]=x,s[7]=E,s[8]=b):(E=s[7],b=s[8]),t.useEffect(E,b),s[9]!==y||s[10]!==p||s[11]!==i||s[12]!==c||s[13]!==v?(L=()=>{if(!c){if(!y)throw new Error("No last event found when syncing passthrough");p!==i&&t.startTransition((()=>{d(v(y,i)),f(i)}))}},M=[y,p,i,c,v],s[9]=y,s[10]=p,s[11]=i,s[12]=c,s[13]=v,s[14]=L,s[15]=M):(L=s[14],M=s[15]),t.useEffect(L,M),c?i:l};//# sourceMappingURL=SharedStateContext.cjs.map