UNPKG

@preprio/prepr-nextjs

Version:

Next.js package for Prepr CMS preview functionality with advanced debugging and visual editing capabilities

2 lines 4.17 kB
import {a,b}from'../chunk-E7ATRJ2F.js';import {clsx}from'clsx';import {twMerge}from'tailwind-merge';var E={DECODE_FAILED:"STEGA_DECODE_FAILED",INVALID_FORMAT:"STEGA_INVALID_FORMAT",DOM_MANIPULATION_FAILED:"DOM_MANIPULATION_FAILED",CONTEXT_NOT_FOUND:"CONTEXT_NOT_FOUND"};function p(n,t,e,r){return {type:n,context:t,message:e.message,timestamp:new Date().toISOString(),stack:e.stack,additionalData:r}}function D(n,t,e){let r=p(E.DECODE_FAILED,t,n,e);return console.error("Stega Error:",r),process.env.NODE_ENV,r}function s(n,t){let e=p(E.DOM_MANIPULATION_FAILED,t,n);return console.error("DOM Error:",e),e}function x(n){let t=new Error(`${n} must be used within its provider`),e=p(E.CONTEXT_NOT_FOUND,n,t);throw console.error("Context Error:",e),t}var T=class{static createElement(t,e){try{let r=document.createElement(t);return r.className=e,r}catch(r){throw s(r,"createElement"),r}}static appendToBody(t){try{document.body.appendChild(t);}catch(e){throw s(e,"appendToBody"),e}}static removeFromBody(t){try{t.parentNode&&t.parentNode.removeChild(t);}catch(e){throw s(e,"removeFromBody"),e}}static setElementStyles(t,e){try{Object.entries(e).forEach(([r,o])=>{t.style.setProperty(r,o);});}catch(r){throw s(r,"setElementStyles"),r}}static getElementRect(t){try{return t.getBoundingClientRect()}catch(e){throw s(e,"getElementRect"),e}}static isElementInViewport(t){try{let e=this.getElementRect(t);return e.top>=0&&e.left>=0&&e.bottom<=(window.innerHeight||document.documentElement.clientHeight)&&e.right<=(window.innerWidth||document.documentElement.clientWidth)}catch(e){return s(e,"isElementInViewport"),false}}static calculateDistance(t,e,r,o){return Math.sqrt(Math.pow(r-t,2)+Math.pow(o-e,2))}static findClosestElement(t,e,r){try{let o=null,i=1/0;return r.forEach(c=>{let a=this.getElementRect(c),m=this.calculateDistance(t,e,a.left+a.width/2,a.top+a.height/2);m<i&&(i=m,o=c);}),o}catch(o){return s(o,"findClosestElement"),null}}static addEventListener(t,e,r,o){try{t.addEventListener(e,r,o);}catch(i){throw s(i,"addEventListener"),i}}static removeEventListener(t,e,r,o){try{t.removeEventListener(e,r,o);}catch(i){throw s(i,"removeEventListener"),i}}};var d=class n{constructor(t){this.options=a({prefix:"[Prepr]"},t);}isEnabled(){var t,e;return this.options.enabled!==void 0?this.options.enabled:(e=(t=l==null?void 0:l.options)==null?void 0:t.enabled)!=null?e:false}log(t,...e){if(!this.isEnabled())return;let r=this.options.prefix;console.log(`${r} ${t}`,...e);}warn(t,...e){if(!this.isEnabled())return;let r=this.options.prefix;console.warn(`${r} ${t}`,...e);}error(t,...e){if(!this.isEnabled())return;let r=this.options.prefix;console.error(`${r} ${t}`,...e);}scope(t){return new n(b(a({},this.options),{prefix:`${this.options.prefix}[${t}]`}))}},l=null;function L(n=false){l=new d({enabled:n});}function g(){return l||(l=new d({enabled:false})),l}function O(n,...t){g().log(n,...t);}function A(n,...t){g().warn(n,...t);}function M(n,...t){g().error(n,...t);}function I(n){return new d({prefix:`[Prepr][${n}]`})}function F(n,t){let e=null,r=0,o=(...i)=>{let c=Date.now(),a=c-r;a>=t?(n(...i),r=c):(e&&clearTimeout(e),e=setTimeout(()=>{n(...i),r=Date.now(),e=null;},t-a));};return o.cancel=()=>{e&&(clearTimeout(e),e=null);},o}function C(n,t){let e=null,r=(...o)=>{e&&clearTimeout(e),e=setTimeout(()=>{n(...o),e=null;},t);};return r.cancel=()=>{e&&(clearTimeout(e),e=null);},r}function S(n,t=1e3){let e=null,r=0;return ()=>{let o=Date.now();return (!e||o-r>t)&&(e=document.querySelectorAll(n),r=o),e}}function V(...n){return twMerge(clsx(n))}function k(n,t){if(typeof window!="undefined"){let e=a({name:"prepr_preview_bar",event:n},t);window.dispatchEvent(new CustomEvent("prepr_preview_bar",{detail:e})),window.parent&&window.parent!==window&&window.parent.postMessage(e,"*");}}export{T as DOMService,E as StegaError,V as cn,S as createElementCache,p as createErrorInfo,I as createScopedLogger,C as debounce,M as debugError,O as debugLog,A as debugWarn,g as getDebugLogger,x as handleContextError,s as handleDOMError,D as handleStegaError,L as initDebugLogger,k as sendPreprEvent,F as throttle};//# sourceMappingURL=index.js.map //# sourceMappingURL=index.js.map