UNPKG

react-signify

Version:

A JS library for predictable and maintainable global state management

3 lines (2 loc) 12 kB
"use strict";var e=require("react/jsx-runtime"),t=require("react");function r(e,t,r,n){if("a"===r&&!n)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof t?e!==t||!n:!t.has(e))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===r?n:"a"===r?n.call(e):n?n.value:t.get(e)}function n(e,t,r,n,s){if("m"===n)throw new TypeError("Private method is not writable");if("a"===n&&!s)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof t?e!==t||!s:!t.has(e))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===n?s.call(e,r):s?s.value=r:t.set(e,r),r}"function"==typeof SuppressedError&&SuppressedError,function(e,t){void 0===t&&(t={});var r=t.insertAt;if("undefined"!=typeof document){var n=document.head||document.getElementsByTagName("head")[0],s=document.createElement("style");s.type="text/css","top"===r&&n.firstChild?n.insertBefore(s,n.firstChild):n.appendChild(s),s.styleSheet?s.styleSheet.cssText=e:s.appendChild(document.createTextNode(e))}}(".signify_popup {\r\n font-size: 12px;\r\n width: 300px;\r\n height: 300px;\r\n background-color: white;\r\n box-shadow: 0 2px 10px rgba(0, 0, 0, 0.5);\r\n position: fixed;\r\n display: block;\r\n top: 50%;\r\n left: 50%;\r\n transform: translate(-50%, -50%);\r\n box-sizing: border-box;\r\n border-radius: 10px;\r\n overflow: hidden;\r\n}\r\n.signify_popup_header {\r\n cursor: move;\r\n display: flex;\r\n align-items: center;\r\n padding: 5px 20px;\r\n gap: 10px;\r\n font-size: 16px;\r\n color: white;\r\n user-select: none;\r\n}\r\n.signify_popup_header_button {\r\n cursor: pointer;\r\n}\r\n.signify_popup_header_label {\r\n margin-right: auto;\r\n overflow: hidden;\r\n text-overflow: ellipsis;\r\n text-wrap: nowrap;\r\n}\r\n.signify_popup_resizer {\r\n width: 20px;\r\n height: 20px;\r\n background-color: rgba(0, 0, 0, 0.1);\r\n position: absolute;\r\n bottom: 0;\r\n right: 0;\r\n cursor: se-resize;\r\n border-radius: 10px 0px 10px;\r\n}\r\n.signify_popup_json_viewer {\r\n margin: 0;\r\n height: calc(100% - 32px);\r\n overflow: auto;\r\n padding: 10px 20px;\r\n white-space: pre;\r\n box-sizing: border-box;\r\n}\r\n.signify_popup_json_viewer::-webkit-scrollbar {\r\n width: 8px;\r\n height: 8px;\r\n}\r\n.signify_popup_json_viewer::-webkit-scrollbar-thumb {\r\n background: gray;\r\n}\r\n.signify_popup_json_viewer::-webkit-scrollbar-track {\r\n background: #fff;\r\n}\r\n.signify_popup_json_key {\r\n color: brown;\r\n}\r\n.signify_popup_json_string {\r\n color: green;\r\n}\r\n.signify_popup_json_number {\r\n color: blue;\r\n}\r\n.signify_popup_json_boolean {\r\n color: purple;\r\n}\r\n.signify_popup_json_null {\r\n color: gray;\r\n}\r\n");let s=100;const o=r=>t.memo((({name:n,pick:o=e=>e,color:i})=>{const a=t.useRef(null);let l=`rs-${n}`,u=!1,c=!1,p=0,f=0,h=0;t.useLayoutEffect((()=>{var e;if(a.current){const{x:t,y:r,h:n,w:s}=JSON.parse(null!==(e=function(e){let t=e+"=",r=decodeURIComponent(document.cookie).split(";");for(let e=0;e<r.length;e++){let n=r[e];for(;" "==n.charAt(0);)n=n.substring(1);if(0==n.indexOf(t))return n.substring(t.length,n.length)}return null}(l))&&void 0!==e?e:"{}");t&&(a.current.style.left=t),r&&(a.current.style.top=r),s&&(a.current.style.width=s),n&&(a.current.style.height=n)}}),[]),t.useEffect((()=>{const e=e=>{if(u&&a.current&&(a.current.style.left=e.clientX-p+"px",a.current.style.top=e.clientY-f+"px"),c&&a.current){const t=a.current.getBoundingClientRect(),r=e.clientX-t.left,n=e.clientY-t.top;r>100&&(a.current.style.width=`${r+10}px`),n>100&&(a.current.style.height=`${n+10}px`)}},t=()=>{u=!1,c=!1,document.body.style.cursor="default",a.current&&function(e,t,r=30){const n=new Date;n.setTime(n.getTime()+24*r*60*60*1e3);let s="expires="+n.toUTCString();document.cookie=e+"="+t+";"+s+";path=/"}(l,JSON.stringify({x:a.current.style.left,y:a.current.style.top,w:a.current.style.width,h:a.current.style.height}))};return document.addEventListener("mousemove",e),document.addEventListener("mouseup",t),()=>{document.removeEventListener("mousemove",e),document.removeEventListener("mouseup",t)}}),[]);const d=t.useCallback((({clientX:e,clientY:t})=>{var r,n;a.current&&(u=!0,a.current.style.zIndex=""+s++,p=e-(null===(r=a.current)||void 0===r?void 0:r.offsetLeft),f=t-(null===(n=a.current)||void 0===n?void 0:n.offsetTop))}),[]),y=t.useCallback((e=>{c=!0,document.body.style.cursor="se-resize",e.preventDefault()}),[]),g=t.useCallback((e=>()=>{a.current&&(a.current.style.fontSize||(a.current.style.fontSize="12px"),a.current.style.fontSize=Number(a.current.style.fontSize.replace("px",""))+(e?2:-2)+"px")}),[]),v=t.useCallback((e=>("string"!=typeof e&&(e=JSON.stringify(e,void 0,2)),(e=e.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;")).replace(/("(\\u[a-zA-Z0-9]{4}|\ $^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g,(function(e){let t="number";return/^"/.test(e)?t=/:$/.test(e)?"key":"string":/true|false/.test(e)?t="boolean":/null/.test(e)&&(t="null"),'<span class="signify_popup_json_'+t+'">'+e+"</span>"})))),[]);return e.jsxs("div",{ref:a,className:"signify_popup",children:[e.jsxs("div",{style:{backgroundColor:null!=i?i:`rgb(${Math.floor(128*Math.random())}, ${Math.floor(128*Math.random())}, ${Math.floor(128*Math.random())})`},className:"signify_popup_header",onMouseDown:d,children:[e.jsx("label",{className:"signify_popup_header_label",children:e.jsx(r,{children:()=>e.jsxs(e.Fragment,{children:[n," - ",++h]})})}),e.jsx("span",{className:"signify_popup_header_button",onClick:g(!0),dangerouslySetInnerHTML:{__html:"&bigtriangleup;"}}),e.jsx("span",{className:"signify_popup_header_button",onClick:g(!1),dangerouslySetInnerHTML:{__html:"&bigtriangledown;"}})]}),e.jsx(r,{children:t=>e.jsx("pre",{className:"signify_popup_json_viewer",dangerouslySetInnerHTML:{__html:v(JSON.stringify(o(t),null,2))}})}),e.jsx("div",{onMouseDown:y,className:"signify_popup_resizer"})]})}),(()=>!0)),i={LocalStorage:()=>localStorage,SesionStorage:()=>sessionStorage},a=(e,t)=>{var r;(null==t?void 0:t.key)&&i[null!==(r=null==t?void 0:t.type)&&void 0!==r?r:"LocalStorage"]().setItem(t.key,JSON.stringify(e))},l=e=>{if("object"!=typeof e||null===e)return e;const t=new WeakMap,r=new Map;r.set(Date,(e=>new Date(e))),r.set(Map,((e,t)=>new Map(Array.from(e.entries()).map((([e,r])=>[t(e),t(r)]))))),r.set(Set,((e,t)=>new Set(Array.from(e).map((e=>t(e)))))),r.set(RegExp,(e=>new RegExp(e.source,e.flags))),r.set(Error,((e,t)=>{const r=new Error(e.message);return e.stack&&(r.stack=e.stack),e.cause&&(r.cause=t(e.cause)),r})),r.set(URL,(e=>new URL(e.toString()))),r.set(Blob,(e=>e.slice(0,e.size,e.type))),r.set(File,(e=>new File([e.slice(0,e.size)],e.name,{type:e.type,lastModified:e.lastModified}))),r.set(FileList,((e,t)=>{const r=Array.from(e).map((e=>t(e)));return Object.defineProperty(r,"item",{value:e=>r[e],enumerable:!1})}));const n=e=>{if("object"!=typeof e||null===e)return e;if(t.has(e))return t.get(e);if(Array.isArray(e)){const r=[];t.set(e,r);for(let t=0;t<e.length;t++)r[t]=n(e[t]);return r}if(ArrayBuffer.isView(e))return(s=e)instanceof Buffer?Buffer.from(s):new s.constructor(s.buffer.slice(),s.byteOffset,s.length);var s;if(e.constructor!==Object&&r.has(e.constructor)){const s=r.get(e.constructor)(e,n);return"object"==typeof s&&null!==s&&t.set(e,s),s}const o=Object.create(Object.getPrototypeOf(e));t.set(e,o);for(const t of Object.keys(e))o[t]=n(e[t]);for(const t of Object.getOwnPropertyNames(e))if(!Object.prototype.propertyIsEnumerable.call(e,t)){const r=Object.getOwnPropertyDescriptor(e,t);r&&(void 0!==r.value&&(r.value=n(r.value)),Object.defineProperty(o,t,r))}for(const t of Object.getOwnPropertySymbols(e)){const r=Object.getOwnPropertyDescriptor(e,t);r&&(void 0!==r.value&&(r.value=n(r.value)),Object.defineProperty(o,t,r))}return o};return n(e)},u=(e,t)=>{if(e===t)return!0;if(e&&t&&"object"==typeof e&&"object"==typeof t){if(e.constructor!==t.constructor)return!1;var r,n,s;if(Array.isArray(e)){if((r=e.length)!=t.length)return!1;for(n=r;0!=n--;)if(!u(e[n],t[n]))return!1;return!0}if(e.constructor===RegExp)return e.source===t.source&&e.flags===t.flags;if(e.valueOf!==Object.prototype.valueOf)return e.valueOf()===t.valueOf();if(e.toString!==Object.prototype.toString)return e.toString()===t.toString();if((r=(s=Object.keys(e)).length)!==Object.keys(t).length)return!1;for(n=r;0!=n--;)if(!Object.prototype.hasOwnProperty.call(t,s[n]))return!1;for(n=r;0!=n--;){var o=s[n];if(!u(e[o],t[o]))return!1}return!0}return e!=e&&t!=t},c=e=>(r,n)=>{t.useLayoutEffect((()=>(e.add(r),()=>{e.delete(r)})),null!=n?n:[])},p=(e,r)=>n=>{const s=t.useState({})[1],o=t.useRef((()=>{let e=null==n?void 0:n(r());return()=>{if(n){let t=n(r());if(u(e,t))return;e=t}s({})}})());return t.useLayoutEffect((()=>(e.add(o.current),()=>{e.delete(o.current)})),[]),n?n(r()):r()},f=e=>t.createElement((()=>e())),h=e=>({children:t})=>t(e()),d=e=>t.memo(h(e),(()=>!0));var y,g,v,m,b,w,_,x,j,k,S;class O{constructor(e,t){if(y.set(this,!0),g.set(this,void 0),v.set(this,void 0),m.set(this,void 0),b.set(this,new Set),w.set(this,new Set),_.set(this,void 0),x.set(this,void 0),j.set(this,void 0),k.set(this,((e=!0)=>{!r(this,y,"f")||r(this,j,"f")&&!r(this,j,"f").call(this,this.value)||r(this,b,"f").forEach((e=>e(this.value))),e&&r(this,w,"f").forEach((e=>e(this.value)))})),S.set(this,(e=>{var t,s;void 0!==e&&n(this,v,e,"f"),a(this.value,null===(t=r(this,m,"f"))||void 0===t?void 0:t.cache),null===(s=r(this,_,"f"))||void 0===s||s.call(this,this.value),r(this,k,"f").call(this)})),this.set=e=>{let t;if("function"==typeof e){let n={value:l(r(this,v,"f"))};e(n),t=n.value}else t=e;u(this.value,t)||r(this,x,"f")&&!r(this,x,"f").call(this,this.value,t)||r(this,S,"f").call(this,t)},this.stop=()=>{n(this,y,!1,"f")},this.resume=()=>{n(this,y,!0,"f"),r(this,k,"f").call(this,!1)},this.reset=()=>{r(this,S,"f").call(this,l(r(this,g,"f")))},this.conditionUpdating=e=>n(this,x,e,"f"),this.conditionRendering=e=>n(this,j,e,"f"),this.use=p(r(this,b,"f"),(()=>this.value)),this.watch=c(r(this,w,"f")),this.html=f(this.use),this.Wrap=h(this.use),this.HardWrap=d(this.use),this.slice=e=>{let t,n=e(this.value),s=!0;const i=new Set,a=new Set,l=(r=!0)=>{const o=e(this.value);!s||t&&!t(o)||(n=o,i.forEach((e=>e(o)))),r&&a.forEach((e=>e(o)))},y=p(i,(()=>n)),g={value:n,use:y,watch:c(a),html:f(y),Wrap:h(y),HardWrap:d(y),stop:()=>s=!1,resume:()=>{s=!0,l(!1)},conditionRendering:e=>t=e,DevTool:o(d(p(a,(()=>e(this.value)))))};return r(this,b,"f").add((()=>{u(e(this.value),n)||l()})),Object.defineProperty(g,"value",{get:()=>n,enumerable:!1,configurable:!1}),g},this.DevTool=o(d(p(r(this,w,"f"),(()=>this.value)))),n(this,g,e,"f"),n(this,v,((e,t)=>{var r;if(null==t?void 0:t.key){if("undefined"==typeof window)throw new Error("The cache feature is not recommended for Server-Side Rendering. Please remove the cache properties from the Signify variable.");const n=null!==(r=null==t?void 0:t.type)&&void 0!==r?r:"LocalStorage",s=i[n]().getItem(t.key);if(s)return JSON.parse(s);i[n]().setItem(t.key,JSON.stringify(e))}return e})(l(e),null==t?void 0:t.cache),"f"),n(this,m,t,"f"),null==t?void 0:t.syncKey){const{post:e,sync:s}=(({key:e,cb:t})=>{const r=`bc_${e}`,n=new BroadcastChannel(r);return n.onmessage=e=>t(e.data),{post:e=>{n.postMessage(e)},sync:e=>{const t=new BroadcastChannel("bcs");t.onmessage=t=>r===t.data&&n.postMessage(e()),t.postMessage(r)}}})({key:t.syncKey,cb:e=>{var t;n(this,v,e,"f"),a(this.value,null===(t=r(this,m,"f"))||void 0===t?void 0:t.cache),r(this,k,"f").call(this)}});n(this,_,e,"f"),s((()=>this.value))}}get value(){return r(this,v,"f")}}y=new WeakMap,g=new WeakMap,v=new WeakMap,m=new WeakMap,b=new WeakMap,w=new WeakMap,_=new WeakMap,x=new WeakMap,j=new WeakMap,k=new WeakMap,S=new WeakMap,exports.signify=(e,t)=>new O(e,t); //# sourceMappingURL=index.js.map