@sanity/visual-editing
Version:
[](https://npm-stat.com/charts.html?package=@sanity/visual-editing) [](https://
2 lines (1 loc) • 5.53 kB
JavaScript
;var t=require("react-compiler-runtime"),e=require("@sanity/mutate"),n=require("./context.cjs"),r=require("./transformSanityNodeData.cjs"),o=require("react");function s(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var i=s(require("get-random-values-esm"));const u=/_key\s*==\s*['"](.*)['"]/,c=/^\d*:\d*$/;function a(t){return"number"==typeof t||"string"==typeof t&&/^\[\d+\]$/.test(t)}function f(t){return"string"==typeof t?u.test(t.trim()):"object"==typeof t&&"_key"in t}const p=/[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]|(?=(?:\.|\[\])(?:\.|\[\]|$))/g,d=/_key\s*==\s*['"](.*)['"]/;function y(t,e,n){const r="string"==typeof e?function(t){if("string"!=typeof t)throw new Error("Path is not a string");const e=t.match(p);if(!e)throw new Error("Invalid path string");return e.map(h)}(e):e;if(!Array.isArray(r))throw new Error("Path must be an array or a string");let o=t;for(let t=0;t<r.length;t++){const e=r[t];if(a(e)){if(!Array.isArray(o))return n;o=o[e]}if(f(e)){if(!Array.isArray(o))return n;o=o.find((t=>t._key===e._key))}if("string"==typeof e&&(o="object"==typeof o&&null!==o?o[e]:void 0),typeof o>"u")return n}return o}function h(t){return a(t)?function(t){return Number(t.replace(/[^\d]/g,""))}(t):f(t)?function(t){return{_key:t.match(d)[1]}}(t):function(t){if("string"==typeof t&&c.test(t))return!0;if(!Array.isArray(t)||2!==t.length)return!1;const[e,n]=t;return!("number"!=typeof e&&""!==e||"number"!=typeof n&&""!==n)}(t)?function(t){const[e,n]=t.split(":").map((t=>""===t?t:Number(t)));return[e,n]}(t):t}function l(t){return t.startsWith(r.D)}function m(t){return l(t)?t:r.D+t}function g(t){return l(t)?t.slice(r.D.length):t}function b(){return o.useSyncExternalStore(k,x,w)}function w(){return n.e}function x(){return n.a}function k(t){return n.l.add(t),()=>n.l.delete(t)}function S(t,e){let n;return(...r)=>{clearTimeout(n),n=setTimeout((()=>{t.apply(t,r)}),e)}}function _(t,e){const r=window.self!==window.top||window.opener;if(n.i(e)||!r)throw new Error("The `useDocuments` hook cannot be used in this context");const o=m(t),s=g(t),i=e.getSnapshot().context?.documents,u=i?.[o],c=i?.[s],a=u||c;if(!a)throw new Error(`Document "${t}" not found`);const f=u.getSnapshot().context?.local||c.getSnapshot().context?.local,p=new Promise((t=>{if(f)t(f);else{const e=a.on("ready",(n=>{const{snapshot:r}=n;t(r||null),e.unsubscribe()}))}}));return{draftDoc:u,draftId:o,getSnapshot:()=>p,publishedDoc:c,publishedId:s,get snapshot(){if(!f)throw new Error(`Snapshot for document "${t}" not found`);return f}}}function I(t,e){return()=>{const{draftDoc:n}=_(t,e);n.send({type:"submit"})}}function D(t,e){return n=>{const{snapshot:r}=_(t,e);return n?y(r,n):r}}function E(t,e){const{getSnapshot:n}=_(t,e);return n}function v(t,n){return async(r,o)=>{const s=_(t,n),{draftDoc:i,draftId:u,getSnapshot:c,publishedId:a}=s,{commit:f=!0}=o||{},p=await("function"==typeof r?r({draftId:u,publishedId:a,get snapshot(){return s.snapshot},getSnapshot:c}):r),d=await c();if(!d)throw new Error(`Snapshot for document "${t}" not found`);i.send({type:"mutate",mutations:[e.createIfNotExists({...d,_id:u}),e.patch(u,p)]}),f&&("object"==typeof f&&"debounce"in f?S((()=>i.send({type:"submit"})),f.debounce)():i.send({type:"submit"}))}}const A=(()=>{let t;return()=>{if(t)return t;t=[];for(let e=0;e<256;++e)t[e]=(e+256).toString(16).slice(1);return t}})();function j(t){const e=A();return function(t=16){const e=new Uint8Array(t);return i.default(e),e}(t).reduce(((t,n)=>t+e[n]),"").slice(0,t)}function O(t){const e="string"==typeof t?t:t.path,n=e.lastIndexOf("."),r=e.substring(n+1,e.length);if(!r.indexOf("["))throw new Error("Invalid path: not an array");const o=e.lastIndexOf("["),s=e.substring(0,o);let i,u;if(r.includes("_key")){const t=r.indexOf('"')+1,e=r.indexOf('"',t);i=r.substring(t,e),u=!0}else{const t=r.indexOf("[")+1,e=r.indexOf("]",t);i=r.substring(t,e),u=!1}if(!s||!i)throw new Error("Invalid path");return{path:s,key:i,hasExplicitKey:u}}exports.a=function(t,n,r){const{path:o,key:s}=O(t),i=j();return[e.at(o,e.insert([{_type:n,_key:i}],r,{_key:s}))]},exports.b=b,exports.c=g,exports.d=y,exports.e=async function(t,n,r){if(!t.type)throw new Error("Node type is missing");const{path:o,key:s}=O(t),i=await n.getSnapshot(),u=y(i,o),c=y(i,t.path),a=u.findIndex((t=>t._key===s));let f=-1,p="before";if("first"===r){if(0===a)return[];f=0,p="before"}else if("last"===r){if(a===u.length-1)return[];f=-1,p="after"}else if("next"===r){if(a===u.length-1)return[];f=a,p="after"}else if("previous"===r){if(0===a)return[];f=a-1,p="before"}return[e.at(o,e.truncate(a,a+1)),e.at(o,e.insert(c,p,f))]},exports.f=function(t,n){const{path:r,key:o}=O(t),s=y(n,r).findIndex((t=>t._key===o));return[e.at(r,e.truncate(s,s+1))]},exports.g=O,exports.h=function(t,n,r="after"){const{path:o,key:s}=O(t),i={...y(n,t.path),_key:j()};return[e.at(o,e.insert(i,r,{_key:s}))]},exports.i=function(){const t=b();let e;return e=!n.i(t),e},exports.j=m,exports.u=function(){const e=t.c(7),n=b();let r;e[0]!==n?(r=t=>({id:t,commit:I(t,n),get:D(t,n),getSnapshot:E(t,n),patch:v(t,n)}),e[0]=n,e[1]=r):r=e[1];const o=r;let s;e[2]!==n?(s=(t,e,r)=>{const{draftDoc:o}=_(t,n),{commit:s}=r||{},i=void 0===s||s;o.send({type:"mutate",mutations:e}),i&&("object"==typeof i&&"debounce"in i?S((()=>o.send({type:"submit"})),i.debounce)():o.send({type:"submit"}))},e[2]=n,e[3]=s):s=e[3];const i=s;let u;return e[4]!==o||e[5]!==i?(u={getDocument:o,mutateDocument:i},e[4]=o,e[5]=i,e[6]=u):u=e[6],u};//# sourceMappingURL=mutations.cjs.map