@widgetic/embed-sdk 2.1.2 → 2.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -0
- package/dist/browser/chunks/ABTestManager.js +1 -1
- package/dist/browser/chunks/advanced.js +1 -1
- package/dist/browser/chunks/api.js +1 -1
- package/dist/browser/chunks/core.js +1 -1
- package/dist/browser/chunks/display.js +1 -1
- package/dist/browser/chunks/renderers.js +1 -1
- package/dist/browser/chunks/scanner.js +1 -1
- package/dist/browser/chunks/security.js +1 -1
- package/dist/browser/chunks/workflows.js +1 -1
- package/dist/browser/embed-bootstrap.js +1 -1
- package/dist/browser/embed-sdk.js +1 -1
- package/dist/esm/chunks/ABTestManager.js +1 -1
- package/dist/esm/chunks/advanced.js +1 -1
- package/dist/esm/chunks/api.js +1 -1
- package/dist/esm/chunks/core.js +1 -1
- package/dist/esm/chunks/display.js +1 -1
- package/dist/esm/chunks/renderers.js +1 -1
- package/dist/esm/chunks/scanner.js +1 -1
- package/dist/esm/chunks/security.js +1 -1
- package/dist/esm/chunks/workflows.js +1 -1
- package/dist/esm/embed-bootstrap.js +1 -1
- package/dist/esm/embed-sdk.js +1 -1
- package/dist/esm/types/core/auth-detector.d.ts +2 -0
- package/dist/esm/types/core/constants.d.ts +1 -1
- package/dist/esm/types/core/version.d.ts +1 -0
- package/dist/esm/types/utils/autoscale-request.d.ts +11 -0
- package/dist/esm/types/utils/editor-session-access.d.ts +15 -0
- package/dist/esm/types/utils/editor-session-response.d.ts +21 -0
- package/dist/esm/types/utils/embed-autoscale.d.ts +139 -0
- package/dist/esm/types/utils/gateway-request.d.ts +29 -0
- package/dist/esm/types/utils/hydrate-published-host.d.ts +8 -2
- package/dist/esm/types/utils/mount-direct-widget.d.ts +28 -0
- package/dist/esm/types/utils/public-embed-lookup.d.ts +8 -2
- package/dist/esm/types/utils/published-host-bridge.d.ts +26 -5
- package/dist/esm/types/utils/workflow-shared-contract.d.ts +7 -0
- package/dist/esm/types/workflows/workflow-stage.d.ts +1 -1
- package/dist/umd/embed-bootstrap.js +1 -1
- package/dist/umd/embed-sdk.js +1 -1
- package/package.json +5 -4
|
@@ -1 +1 @@
|
|
|
1
|
-
import{_ as t}from"./vendor.js";import{C as e,o as i,b as o,l as n,d as s,p as r}from"./core.js";import{getMessenger as a}from"./messaging.js";const d=t=>null!==t&&"object"==typeof t&&!Array.isArray(t)&&"[object Object]"===Object.prototype.toString.call(t);function c(t){if(Array.isArray(t))return t.map(c);if(!d(t))return t;const e={};for(const[i,o]of Object.entries(t)){e[i.replace(/[_-](\w)/g,((t,e)=>e?e.toUpperCase():""))]=Array.isArray(o)?o.map(c):d(o)?c(o):o}return e}function l(t){if(Array.isArray(t))return t.map(l);if(!d(t))return t;const e={};for(const[i,o]of Object.entries(t)){e[i.replace(/([a-z0-9])([A-Z])/g,"$1_$2").replace(/[\s-]+/g,"_").toLowerCase()]=Array.isArray(o)?o.map(l):d(o)?l(o):o}return e}class h{constructor(){this.sessionCache=null,this.sessionCacheExpiry=0,this.ownershipCache=new Map;const t=r(),n=new e({basePath:t.basePath,apiKey:t.apiKey,credentials:"include"});this.authApi=new i(n),this.compositionsApi=new o(n)}checkAuthentication(){return t(this,void 0,void 0,(function*(){try{if(this.sessionCache&&Date.now()<this.sessionCacheExpiry)return{isAuthenticated:!0,session:this.sessionCache,user:this.sessionCache.user,canEdit:!0};const t=c(yield this.authApi.getSession());return t&&t.accessToken&&t.user?(this.sessionCache=t,this.sessionCacheExpiry=Date.now()+3e5,n.info("User authenticated for editing",{userId:t.user.id,email:t.user.email}),{isAuthenticated:!0,session:t,user:t.user,canEdit:!0}):{isAuthenticated:!1,canEdit:!1}}catch(t){return n.debug("Authentication check failed",{error:t}),t instanceof Response&&t.status,{isAuthenticated:!1,canEdit:!1}}}))}checkCompositionOwnership(e){return t(this,void 0,void 0,(function*(){var t;try{const i=this.ownershipCache.get(e);if(i)return i;const o=yield this.checkAuthentication();if(!o.isAuthenticated||!(null===(t=o.session)||void 0===t?void 0:t.user)){const t={isOwner:!1,canEdit:!1,error:"User not authenticated"};return this.ownershipCache.set(e,t),t}const s=c(yield this.compositionsApi.getCompositionById({id:e}));if(!s){const t={isOwner:!1,canEdit:!1,error:"Composition not found"};return this.ownershipCache.set(e,t),t}const r=s.creatorId===o.session.user.id,a={isOwner:r,canEdit:r,composition:s,error:r?void 0:"User does not own this composition"};return this.ownershipCache.set(e,a),setTimeout((()=>{this.ownershipCache.delete(e)}),12e4),n.debug("Composition ownership checked",{compositionId:e,isOwner:r,userId:o.session.user.id,compositionCreatorId:s.creatorId}),a}catch(t){n.warn("Failed to check composition ownership",{compositionId:e,error:t});const i={isOwner:!1,canEdit:!1,error:t instanceof Error?t.message:"Unknown error"};return this.ownershipCache.set(e,i),i}}))}canEditComposition(e){return t(this,void 0,void 0,(function*(){return(yield this.checkCompositionOwnership(e)).canEdit}))}getCurrentUser(){return t(this,void 0,void 0,(function*(){return(yield this.checkAuthentication()).user||null}))}clearCache(){this.sessionCache=null,this.sessionCacheExpiry=0,this.ownershipCache.clear(),n.debug("Authentication cache cleared")}checkMultipleCompositions(e){return t(this,void 0,void 0,(function*(){const i=new Map;return(yield this.checkAuthentication()).isAuthenticated?(yield Promise.all(e.map((e=>t(this,void 0,void 0,(function*(){const t=yield this.checkCompositionOwnership(e);i.set(e,t)}))))),i):(e.forEach((t=>{i.set(t,{isOwner:!1,canEdit:!1,error:"User not authenticated"})})),i)}))}isEditingEnabled(){return s.ENABLE_EDITING}}let u=null;function p(){return u||(u=new h),u}class g{constructor(t={}){this.host=null,this.container=null,this.content=null,this.destroyed=!1,this.options=Object.assign({target:"widgetic-portal",className:"",style:"",clickThrough:!1,zIndex:99999999},t),this.createPortal()}createPortal(){this.host=this.findOrCreateHost(),this.container=document.createElement("div"),this.container.className=(this.options.className?this.options.className+" ":"")+"widgetic-portal-contents",this.container.style.cssText=`\n position: absolute;\n top: 0;\n left: 0;\n width: 100%;\n height: 100%;\n z-index: ${this.options.zIndex};\n pointer-events: ${this.options.clickThrough?"none":"auto"};\n ${this.options.style}\n `,this.options.clickThrough?this.container.style.pointerEvents="none":this.container.style.pointerEvents="auto",this.host.appendChild(this.container),n.debug("Portal created",{target:this.options.target,host:this.host,container:this.container})}findOrCreateHost(){let t=null;if(this.options.target instanceof HTMLElement)return this.options.target;if("string"==typeof this.options.target){const e=`.${this.options.target}`;t=document.querySelector(e)}return t||(t=document.createElement("div"),t.classList.add("string"==typeof this.options.target?this.options.target:"widgetic-portal"),document.body.appendChild(t),n.debug("Portal host created",{target:this.options.target,host:t})),t}mount(t){this.destroyed?n.warn("Cannot mount content to destroyed portal"):this.container?(this.content&&this.unmount(),t.style.pointerEvents="auto",this.content=t,this.container.appendChild(t),n.debug("Content mounted to portal",{content:t})):n.error("Portal container not available")}unmount(){this.content&&this.container&&this.container.contains(this.content)&&(this.container.removeChild(this.content),this.content=null,n.debug("Content unmounted from portal"))}destroy(){if(!this.destroyed){if(this.unmount(),this.container&&this.host&&this.host.contains(this.container)&&this.host.removeChild(this.container),this.host&&0===this.host.childNodes.length&&this.host.parentNode){const t="string"==typeof this.options.target?this.options.target:"widgetic-portal";this.host.classList.contains(t)&&this.host.parentNode.removeChild(this.host)}this.destroyed=!0,this.host=null,this.container=null,this.content=null,n.debug("Portal destroyed")}}getContainer(){return this.container}isDestroyed(){return this.destroyed}updateOptions(t){this.destroyed?n.warn("Cannot update options on destroyed portal"):(this.options=Object.assign(Object.assign({},this.options),t),this.container&&(void 0!==t.className&&(this.container.className=(t.className?t.className+" ":"")+"widgetic-portal-contents"),void 0!==t.style&&(this.container.style.cssText=`\n position: absolute;\n top: 0;\n left: 0;\n width: 100%;\n height: 100%;\n z-index: ${this.options.zIndex};\n pointer-events: ${this.options.clickThrough,"none"};\n ${this.options.style}\n `),void 0!==t.clickThrough&&(this.container.style.pointerEvents=(t.clickThrough,"none"))),n.debug("Portal options updated",{options:this.options}))}}function y(t){return new g(t)}class m{constructor(t={}){this.config=t,this.loadPromise=null,this.sdkInstance=null,this.isLoading=!1,this.loadTimeout=3e4,this.config=Object.assign({basePath:t.basePath||this.getUISdkBaseUrl(),version:t.version||"latest",debug:t.debug||!1},t)}getUISdkBaseUrl(){return"development"===s.NODE_ENV?s.CLIENT_URL:`${s.PRODUCTION_URL}/ui-sdk`}loadUISdk(){return t(this,void 0,void 0,(function*(){if(this.sdkInstance)return this.sdkInstance;if(this.loadPromise)return this.loadPromise;this.isLoading=!0,this.loadPromise=this.performLoad();try{return this.sdkInstance=yield this.loadPromise,n.info("UI SDK loaded successfully",{basePath:this.config.basePath,version:this.config.version}),this.sdkInstance}catch(t){throw n.error("Failed to load UI SDK",{error:t,config:this.config}),this.loadPromise=null,t}finally{this.isLoading=!1}}))}performLoad(){return t(this,void 0,void 0,(function*(){return new Promise(((t,e)=>{const i=setTimeout((()=>{e(new Error("UI SDK load timeout"))}),this.loadTimeout);try{if(window.WidgeticUI)return clearTimeout(i),void t(window.WidgeticUI);const o=document.createElement("script");o.src=`${this.config.basePath}/widgetic-ui.js`,o.async=!0,o.crossOrigin="anonymous",o.onload=()=>{clearTimeout(i),window.WidgeticUI?(n.debug("UI SDK script loaded from external source"),t(window.WidgeticUI)):e(new Error("UI SDK script loaded but WidgeticUI not available"))},o.onerror=()=>{clearTimeout(i),e(new Error("Failed to load UI SDK script"))},document.head.appendChild(o),n.debug("Loading UI SDK",{src:o.src})}catch(t){clearTimeout(i),e(t)}}))}))}isLoaded(){return null!==this.sdkInstance}isLoadingState(){return this.isLoading}getInstance(){return this.sdkInstance}initializeUISdk(e){return t(this,void 0,void 0,(function*(){const t=yield this.loadUISdk();return yield t.init(Object.assign(Object.assign({},this.config),e)),n.info("UI SDK initialized",{config:Object.assign(Object.assign({},this.config),e)}),t}))}cleanup(){return t(this,void 0,void 0,(function*(){if(this.sdkInstance){try{yield this.sdkInstance.destroy()}catch(t){n.warn("Error during UI SDK cleanup",{error:t})}this.sdkInstance=null}this.loadPromise=null,this.isLoading=!1,window.WidgeticUI&&delete window.WidgeticUI,n.debug("UI SDK cleanup complete")}))}preload(){return t(this,void 0,void 0,(function*(){try{yield this.loadUISdk(),n.debug("UI SDK preloaded successfully")}catch(t){n.warn("UI SDK preload failed",{error:t})}}))}}let v=null;function f(t){return v||(v=new m(t)),v}class w{constructor(t={}){this.options=t,this.sessions=new Map,this.defaultOptions={showEditButton:!0,editButtonText:"✏️ Edit",editButtonPosition:"top-right",enableClickToEdit:!0,autoDetectAuth:!0},this.options=Object.assign(Object.assign({},this.defaultOptions),t)}addOverlay(e,i){return t(this,void 0,void 0,(function*(){try{if(!this.options.autoDetectAuth)return void(yield this.createOverlay(e,i,!0));const t=p(),o=yield t.checkCompositionOwnership(i);o.canEdit?(yield this.createOverlay(e,i,!0),n.debug("Editor overlay added for owned composition",{compositionId:i})):n.debug("Editor overlay not added - user cannot edit",{compositionId:i,reason:o.error})}catch(t){n.warn("Failed to add editor overlay",{compositionId:i,error:t})}}))}createOverlay(e,i,o){return t(this,void 0,void 0,(function*(){if(this.sessions.has(i))return;const t=document.createElement("div");if(t.className="widgetic-editor-overlay",t.style.cssText=`\n position: absolute;\n top: 0;\n left: 0;\n right: 0;\n bottom: 0;\n z-index: 10000;\n pointer-events: none;\n ${this.getOverlayStyles()}\n `,this.options.showEditButton&&o){const e=this.createEditButton(i);t.appendChild(e)}this.options.enableClickToEdit&&o&&this.addClickToEdit(t,i);const s=e.parentElement;if(s){"static"===window.getComputedStyle(s).position&&(s.style.position="relative"),s.appendChild(t)}else e.style.position="relative",e.appendChild(t);const r={compositionId:i,isOpen:!1,overlay:t};this.sessions.set(i,r),n.debug("Editor overlay created",{compositionId:i})}))}createEditButton(e){const i=document.createElement("button");return i.className="widgetic-edit-button",i.textContent=this.options.editButtonText||"✏️ Edit",i.style.cssText=`\n position: absolute;\n ${this.getButtonPositionStyles()}\n background: #007bff;\n color: white;\n border: none;\n border-radius: 4px;\n padding: 8px 12px;\n font-size: 12px;\n font-weight: 500;\n cursor: pointer;\n pointer-events: auto;\n box-shadow: 0 2px 4px rgba(0,0,0,0.1);\n transition: all 0.2s ease;\n z-index: 10001;\n ${this.getCustomButtonStyles()}\n `,i.addEventListener("mouseenter",(()=>{i.style.background="#0056b3",i.style.transform="scale(1.05)"})),i.addEventListener("mouseleave",(()=>{i.style.background="#007bff",i.style.transform="scale(1)"})),i.addEventListener("click",(i=>t(this,void 0,void 0,(function*(){i.preventDefault(),i.stopPropagation(),yield this.openEditor(e)})))),i}getButtonPositionStyles(){const t="10px";switch(this.options.editButtonPosition||"top-right"){case"top-left":return`top: ${t}; left: ${t};`;case"top-right":default:return`top: ${t}; right: ${t};`;case"bottom-left":return`bottom: ${t}; left: ${t};`;case"bottom-right":return`bottom: ${t}; right: ${t};`}}getCustomButtonStyles(){return this.options.editButtonStyle?Object.entries(this.options.editButtonStyle).map((([t,e])=>`${this.camelToKebab(t)}: ${e};`)).join(" "):""}getOverlayStyles(){return this.options.overlayStyle?Object.entries(this.options.overlayStyle).map((([t,e])=>`${this.camelToKebab(t)}: ${e};`)).join(" "):""}camelToKebab(t){return t.replace(/([a-z0-9])([A-Z])/g,"$1-$2").toLowerCase()}addClickToEdit(e,i){e.style.cursor="pointer",e.style.pointerEvents="auto",e.addEventListener("click",(o=>t(this,void 0,void 0,(function*(){o.target===e&&(o.preventDefault(),o.stopPropagation(),yield this.openEditor(i))})))),e.addEventListener("mouseenter",(()=>{e.style.backgroundColor="rgba(0, 123, 255, 0.1)",e.style.border="2px dashed #007bff"})),e.addEventListener("mouseleave",(()=>{e.style.backgroundColor="transparent",e.style.border="none"}))}createEditorContainer(){const t=new g({target:"widgetic-editor-portal",className:"widgetic-editor-modal",zIndex:1e5,clickThrough:!1});const e=document.createElement("div");e.className="widgetic-editor-modal-backdrop",e.style.cssText="\n position: fixed;\n top: 0;\n left: 0;\n width: 100vw;\n height: 100vh;\n background: rgba(0, 0, 0, 0.8);\n display: flex;\n align-items: center;\n justify-content: center;\n ";const i=document.createElement("div");return i.className="widgetic-editor-content",i.style.cssText="\n width: 90vw;\n height: 90vh;\n max-width: 1200px;\n max-height: 800px;\n background: white;\n border-radius: 8px;\n overflow: hidden;\n position: relative;\n ",e.appendChild(i),t.mount(e),{container:e,portal:t}}openEditor(e){return t(this,void 0,void 0,(function*(){try{const t=this.sessions.get(e);if(!t)return void n.warn("No editor session found",{compositionId:e});if(t.isOpen)return void n.debug("Editor already open",{compositionId:e});n.info("Opening composition editor",{compositionId:e});const i=f(),o=yield i.initializeUISdk(),{container:s,portal:r}=this.createEditorContainer(),d=yield o.mountCompositionEditor(s.querySelector(".widgetic-editor-content"),e,{onSave:t=>this.handleEditorSave(e,t),onClose:()=>this.closeEditor(e),onPreview:t=>this.handleEditorPreview(e,t)}),c=a().createWidgetController(e);if(t.isOpen=!0,t.editorContainer=s,t.editorInstance=d,t.widgetController=c,t.portal=r,t.overlay){const e=t.overlay.querySelector(".widgetic-edit-button");e&&(e.style.display="none")}n.info("Composition editor opened successfully",{compositionId:e})}catch(t){n.error("Failed to open editor",{compositionId:e,error:t}),this.showError("Failed to open editor. Please try again.",e)}}))}handleEditorSave(e,i){return t(this,void 0,void 0,(function*(){try{n.info("Saving composition changes",{compositionId:e,data:i});const t=this.sessions.get(e);(null==t?void 0:t.widgetController)&&(yield t.widgetController.updateConfig(i),yield t.widgetController.refresh()),yield this.closeEditor(e),n.info("Composition saved successfully",{compositionId:e})}catch(t){n.error("Failed to save composition",{compositionId:e,error:t}),this.showError("Failed to save changes. Please try again.",e)}}))}handleEditorPreview(e,i){return t(this,void 0,void 0,(function*(){try{const t=this.sessions.get(e);(null==t?void 0:t.widgetController)&&(yield t.widgetController.callFunction("preview",i))}catch(t){n.warn("Preview update failed",{compositionId:e,error:t})}}))}closeEditor(e){return t(this,void 0,void 0,(function*(){const t=this.sessions.get(e);if(t&&t.isOpen)try{if(t.editorInstance&&t.editorContainer){const e=f().getInstance();e&&(yield e.unmountCompositionEditor(t.editorContainer))}if(t.portal&&t.portal.destroy(),t.overlay){const e=t.overlay.querySelector(".widgetic-edit-button");e&&(e.style.display="block")}t.isOpen=!1,t.editorContainer=void 0,t.editorInstance=void 0,t.portal=void 0,n.info("Editor closed",{compositionId:e})}catch(t){n.error("Error closing editor",{compositionId:e,error:t})}}))}showError(t,e){const i=document.createElement("div");i.style.cssText="\n position: fixed;\n top: 20px;\n right: 20px;\n background: #dc3545;\n color: white;\n padding: 12px 16px;\n border-radius: 4px;\n z-index: 100001;\n font-size: 14px;\n max-width: 300px;\n ",i.textContent=t,document.body.appendChild(i),setTimeout((()=>{i.parentNode&&i.parentNode.removeChild(i)}),5e3)}removeOverlay(t){const e=this.sessions.get(t);e&&(e.isOpen&&this.closeEditor(t),e.overlay&&e.overlay.parentNode&&e.overlay.parentNode.removeChild(e.overlay),e.portal&&e.portal.destroy(),this.sessions.delete(t),n.debug("Editor overlay removed",{compositionId:t}))}isEditorOpen(t){var e;return(null===(e=this.sessions.get(t))||void 0===e?void 0:e.isOpen)||!1}getActiveSessions(){return Array.from(this.sessions.keys())}cleanup(){for(const t of this.sessions.keys())this.removeOverlay(t);n.debug("Editor overlay cleanup complete")}}let b=null;function E(t){return b||(b=new w(t)),b}var C=Object.freeze({__proto__:null,EditorOverlay:w,getEditorOverlay:E,resetEditorOverlay:function(){b&&(b.cleanup(),b=null)}});export{E as a,f as b,y as c,c as d,C as e,p as g,l as t};
|
|
1
|
+
import{_ as t}from"./vendor.js";import{n as e,C as i,o,b as n,l as s,d as r,p as a,a as d}from"./core.js";import{getMessenger as c}from"./messaging.js";const l=t=>null!==t&&"object"==typeof t&&!Array.isArray(t)&&"[object Object]"===Object.prototype.toString.call(t);function h(t){if(Array.isArray(t))return t.map(h);if(!l(t))return t;const e={};for(const[i,o]of Object.entries(t)){e[i.replace(/[_-](\w)/g,((t,e)=>e?e.toUpperCase():""))]=Array.isArray(o)?o.map(h):l(o)?h(o):o}return e}function u(t){if(Array.isArray(t))return t.map(u);if(!l(t))return t;const e={};for(const[i,o]of Object.entries(t)){e[i.replace(/([a-z0-9])([A-Z])/g,"$1_$2").replace(/[\s-]+/g,"_").toLowerCase()]=Array.isArray(o)?o.map(u):l(o)?u(o):o}return e}function p(t){if(!t)return"";try{return new URL(t).hostname.toLowerCase()}catch(t){return""}}function g(t){return null!==t&&"object"==typeof t&&!Array.isArray(t)}function y(t){return g(t)&&"boolean"==typeof t.success&&"data"in t?t.data:t}class m{constructor(){var t;this.sessionCache=null,this.sessionCacheExpiry=0,this.ownershipCache=new Map;const e=a({basePath:d()});this.apiUrl=null!==(t=e.basePath)&&void 0!==t?t:"";const s=new i({basePath:e.basePath,apiKey:e.apiKey,credentials:"include"});this.authApi=new o(s),this.compositionsApi=new n(s)}checkAuthentication(){return t(this,void 0,void 0,(function*(){var t,i;try{const o="undefined"!=typeof window&&null!==(i=null===(t=window.location)||void 0===t?void 0:t.hostname)&&void 0!==i?i:"";if(!function(t){var i,o,n;const s=p(t.apiUrl),r=p(null!==(i=t.productionApiUrl)&&void 0!==i?i:e.PROD_API_URL);if(!s||!r||s!==r)return!0;const a=String(null!==(o=t.pageHostname)&&void 0!==o?o:"").trim().toLowerCase();if(!a)return!1;if(function(t){const e=t.replace(/^\[|\]$/g,"");return"localhost"===e||e.endsWith(".localhost")||"127.0.0.1"===e||"::1"===e}(a))return!0;const d=p(null!==(n=t.widgeticSiteUrl)&&void 0!==n?n:e.PROD_CLIENT_URL);return!d||a===d||a.endsWith(`.${d}`)}({pageHostname:o,apiUrl:this.apiUrl}))return s.debug("Editor session check skipped: this website cannot read the API's answer",{pageHostname:o}),{isAuthenticated:!1,canEdit:!1};if(this.sessionCache&&Date.now()<this.sessionCacheExpiry)return{isAuthenticated:!0,session:this.sessionCache,user:this.sessionCache.user,canEdit:!0};const n=yield this.authApi.getSessionRaw(),r=function(t){const e=y(t);if(!g(e))return null;const i=h(e),o=i.user;return"string"!=typeof i.accessToken||""===i.accessToken?null:g(o)&&"string"==typeof o.id&&""!==o.id?i:null}(yield n.raw.json().catch((()=>null)));return r?(this.sessionCache=r,this.sessionCacheExpiry=Date.now()+3e5,s.info("User authenticated for editing",{userId:r.user.id,email:r.user.email}),{isAuthenticated:!0,session:r,user:r.user,canEdit:!0}):{isAuthenticated:!1,canEdit:!1}}catch(t){return s.debug("Authentication check failed",{error:t}),t instanceof Response&&t.status,{isAuthenticated:!1,canEdit:!1}}}))}checkCompositionOwnership(e){return t(this,void 0,void 0,(function*(){var t;try{const i=this.ownershipCache.get(e);if(i)return i;const o=yield this.checkAuthentication();if(!o.isAuthenticated||!(null===(t=o.session)||void 0===t?void 0:t.user)){const t={isOwner:!1,canEdit:!1,error:"User not authenticated"};return this.ownershipCache.set(e,t),t}const n=h(y(yield this.compositionsApi.getCompositionById({id:e})));if(!n){const t={isOwner:!1,canEdit:!1,error:"Composition not found"};return this.ownershipCache.set(e,t),t}const r=n.creatorId===o.session.user.id,a={isOwner:r,canEdit:r,composition:n,error:r?void 0:"User does not own this composition"};return this.ownershipCache.set(e,a),setTimeout((()=>{this.ownershipCache.delete(e)}),12e4),s.debug("Composition ownership checked",{compositionId:e,isOwner:r,userId:o.session.user.id,compositionCreatorId:n.creatorId}),a}catch(t){s.warn("Failed to check composition ownership",{compositionId:e,error:t});const i={isOwner:!1,canEdit:!1,error:t instanceof Error?t.message:"Unknown error"};return this.ownershipCache.set(e,i),i}}))}canEditComposition(e){return t(this,void 0,void 0,(function*(){return(yield this.checkCompositionOwnership(e)).canEdit}))}getCurrentUser(){return t(this,void 0,void 0,(function*(){return(yield this.checkAuthentication()).user||null}))}clearCache(){this.sessionCache=null,this.sessionCacheExpiry=0,this.ownershipCache.clear(),s.debug("Authentication cache cleared")}checkMultipleCompositions(e){return t(this,void 0,void 0,(function*(){const i=new Map;return(yield this.checkAuthentication()).isAuthenticated?(yield Promise.all(e.map((e=>t(this,void 0,void 0,(function*(){const t=yield this.checkCompositionOwnership(e);i.set(e,t)}))))),i):(e.forEach((t=>{i.set(t,{isOwner:!1,canEdit:!1,error:"User not authenticated"})})),i)}))}isEditingEnabled(){return r.ENABLE_EDITING}}let v=null;function f(){return v||(v=new m),v}class w{constructor(t={}){this.host=null,this.container=null,this.content=null,this.destroyed=!1,this.options=Object.assign({target:"widgetic-portal",className:"",style:"",clickThrough:!1,zIndex:99999999},t),this.createPortal()}createPortal(){this.host=this.findOrCreateHost(),this.container=document.createElement("div"),this.container.className=(this.options.className?this.options.className+" ":"")+"widgetic-portal-contents",this.container.style.cssText=`\n position: absolute;\n top: 0;\n left: 0;\n width: 100%;\n height: 100%;\n z-index: ${this.options.zIndex};\n pointer-events: ${this.options.clickThrough?"none":"auto"};\n ${this.options.style}\n `,this.options.clickThrough?this.container.style.pointerEvents="none":this.container.style.pointerEvents="auto",this.host.appendChild(this.container),s.debug("Portal created",{target:this.options.target,host:this.host,container:this.container})}findOrCreateHost(){let t=null;if(this.options.target instanceof HTMLElement)return this.options.target;if("string"==typeof this.options.target){const e=`.${this.options.target}`;t=document.querySelector(e)}return t||(t=document.createElement("div"),t.classList.add("string"==typeof this.options.target?this.options.target:"widgetic-portal"),document.body.appendChild(t),s.debug("Portal host created",{target:this.options.target,host:t})),t}mount(t){this.destroyed?s.warn("Cannot mount content to destroyed portal"):this.container?(this.content&&this.unmount(),t.style.pointerEvents="auto",this.content=t,this.container.appendChild(t),s.debug("Content mounted to portal",{content:t})):s.error("Portal container not available")}unmount(){this.content&&this.container&&this.container.contains(this.content)&&(this.container.removeChild(this.content),this.content=null,s.debug("Content unmounted from portal"))}destroy(){if(!this.destroyed){if(this.unmount(),this.container&&this.host&&this.host.contains(this.container)&&this.host.removeChild(this.container),this.host&&0===this.host.childNodes.length&&this.host.parentNode){const t="string"==typeof this.options.target?this.options.target:"widgetic-portal";this.host.classList.contains(t)&&this.host.parentNode.removeChild(this.host)}this.destroyed=!0,this.host=null,this.container=null,this.content=null,s.debug("Portal destroyed")}}getContainer(){return this.container}isDestroyed(){return this.destroyed}updateOptions(t){this.destroyed?s.warn("Cannot update options on destroyed portal"):(this.options=Object.assign(Object.assign({},this.options),t),this.container&&(void 0!==t.className&&(this.container.className=(t.className?t.className+" ":"")+"widgetic-portal-contents"),void 0!==t.style&&(this.container.style.cssText=`\n position: absolute;\n top: 0;\n left: 0;\n width: 100%;\n height: 100%;\n z-index: ${this.options.zIndex};\n pointer-events: ${this.options.clickThrough,"none"};\n ${this.options.style}\n `),void 0!==t.clickThrough&&(this.container.style.pointerEvents=(t.clickThrough,"none"))),s.debug("Portal options updated",{options:this.options}))}}function b(t){return new w(t)}class E{constructor(t={}){this.config=t,this.loadPromise=null,this.sdkInstance=null,this.isLoading=!1,this.loadTimeout=3e4,this.config=Object.assign({basePath:t.basePath||this.getUISdkBaseUrl(),version:t.version||"latest",debug:t.debug||!1},t)}getUISdkBaseUrl(){return"development"===r.NODE_ENV?r.CLIENT_URL:`${r.PRODUCTION_URL}/ui-sdk`}loadUISdk(){return t(this,void 0,void 0,(function*(){if(this.sdkInstance)return this.sdkInstance;if(this.loadPromise)return this.loadPromise;this.isLoading=!0,this.loadPromise=this.performLoad();try{return this.sdkInstance=yield this.loadPromise,s.info("UI SDK loaded successfully",{basePath:this.config.basePath,version:this.config.version}),this.sdkInstance}catch(t){throw s.error("Failed to load UI SDK",{error:t,config:this.config}),this.loadPromise=null,t}finally{this.isLoading=!1}}))}performLoad(){return t(this,void 0,void 0,(function*(){return new Promise(((t,e)=>{const i=setTimeout((()=>{e(new Error("UI SDK load timeout"))}),this.loadTimeout);try{if(window.WidgeticUI)return clearTimeout(i),void t(window.WidgeticUI);const o=document.createElement("script");o.src=`${this.config.basePath}/widgetic-ui.js`,o.async=!0,o.crossOrigin="anonymous",o.onload=()=>{clearTimeout(i),window.WidgeticUI?(s.debug("UI SDK script loaded from external source"),t(window.WidgeticUI)):e(new Error("UI SDK script loaded but WidgeticUI not available"))},o.onerror=()=>{clearTimeout(i),e(new Error("Failed to load UI SDK script"))},document.head.appendChild(o),s.debug("Loading UI SDK",{src:o.src})}catch(t){clearTimeout(i),e(t)}}))}))}isLoaded(){return null!==this.sdkInstance}isLoadingState(){return this.isLoading}getInstance(){return this.sdkInstance}initializeUISdk(e){return t(this,void 0,void 0,(function*(){const t=yield this.loadUISdk();return yield t.init(Object.assign(Object.assign({},this.config),e)),s.info("UI SDK initialized",{config:Object.assign(Object.assign({},this.config),e)}),t}))}cleanup(){return t(this,void 0,void 0,(function*(){if(this.sdkInstance){try{yield this.sdkInstance.destroy()}catch(t){s.warn("Error during UI SDK cleanup",{error:t})}this.sdkInstance=null}this.loadPromise=null,this.isLoading=!1,window.WidgeticUI&&delete window.WidgeticUI,s.debug("UI SDK cleanup complete")}))}preload(){return t(this,void 0,void 0,(function*(){try{yield this.loadUISdk(),s.debug("UI SDK preloaded successfully")}catch(t){s.warn("UI SDK preload failed",{error:t})}}))}}let C=null;function I(t){return C||(C=new E(t)),C}class k{constructor(t={}){this.options=t,this.sessions=new Map,this.defaultOptions={showEditButton:!0,editButtonText:"✏️ Edit",editButtonPosition:"top-right",enableClickToEdit:!0,autoDetectAuth:!0},this.options=Object.assign(Object.assign({},this.defaultOptions),t)}addOverlay(e,i){return t(this,void 0,void 0,(function*(){try{if(!this.options.autoDetectAuth)return void(yield this.createOverlay(e,i,!0));const t=f(),o=yield t.checkCompositionOwnership(i);o.canEdit?(yield this.createOverlay(e,i,!0),s.debug("Editor overlay added for owned composition",{compositionId:i})):s.debug("Editor overlay not added - user cannot edit",{compositionId:i,reason:o.error})}catch(t){s.warn("Failed to add editor overlay",{compositionId:i,error:t})}}))}createOverlay(e,i,o){return t(this,void 0,void 0,(function*(){if(this.sessions.has(i))return;const t=document.createElement("div");if(t.className="widgetic-editor-overlay",t.style.cssText=`\n position: absolute;\n top: 0;\n left: 0;\n right: 0;\n bottom: 0;\n z-index: 10000;\n pointer-events: none;\n ${this.getOverlayStyles()}\n `,this.options.showEditButton&&o){const e=this.createEditButton(i);t.appendChild(e)}this.options.enableClickToEdit&&o&&this.addClickToEdit(t,i);const n=e.parentElement;if(n){"static"===window.getComputedStyle(n).position&&(n.style.position="relative"),n.appendChild(t)}else e.style.position="relative",e.appendChild(t);const r={compositionId:i,isOpen:!1,overlay:t};this.sessions.set(i,r),s.debug("Editor overlay created",{compositionId:i})}))}createEditButton(e){const i=document.createElement("button");return i.className="widgetic-edit-button",i.textContent=this.options.editButtonText||"✏️ Edit",i.style.cssText=`\n position: absolute;\n ${this.getButtonPositionStyles()}\n background: #007bff;\n color: white;\n border: none;\n border-radius: 4px;\n padding: 8px 12px;\n font-size: 12px;\n font-weight: 500;\n cursor: pointer;\n pointer-events: auto;\n box-shadow: 0 2px 4px rgba(0,0,0,0.1);\n transition: all 0.2s ease;\n z-index: 10001;\n ${this.getCustomButtonStyles()}\n `,i.addEventListener("mouseenter",(()=>{i.style.background="#0056b3",i.style.transform="scale(1.05)"})),i.addEventListener("mouseleave",(()=>{i.style.background="#007bff",i.style.transform="scale(1)"})),i.addEventListener("click",(i=>t(this,void 0,void 0,(function*(){i.preventDefault(),i.stopPropagation(),yield this.openEditor(e)})))),i}getButtonPositionStyles(){const t="10px";switch(this.options.editButtonPosition||"top-right"){case"top-left":return`top: ${t}; left: ${t};`;case"top-right":default:return`top: ${t}; right: ${t};`;case"bottom-left":return`bottom: ${t}; left: ${t};`;case"bottom-right":return`bottom: ${t}; right: ${t};`}}getCustomButtonStyles(){return this.options.editButtonStyle?Object.entries(this.options.editButtonStyle).map((([t,e])=>`${this.camelToKebab(t)}: ${e};`)).join(" "):""}getOverlayStyles(){return this.options.overlayStyle?Object.entries(this.options.overlayStyle).map((([t,e])=>`${this.camelToKebab(t)}: ${e};`)).join(" "):""}camelToKebab(t){return t.replace(/([a-z0-9])([A-Z])/g,"$1-$2").toLowerCase()}addClickToEdit(e,i){e.style.cursor="pointer",e.style.pointerEvents="auto",e.addEventListener("click",(o=>t(this,void 0,void 0,(function*(){o.target===e&&(o.preventDefault(),o.stopPropagation(),yield this.openEditor(i))})))),e.addEventListener("mouseenter",(()=>{e.style.backgroundColor="rgba(0, 123, 255, 0.1)",e.style.border="2px dashed #007bff"})),e.addEventListener("mouseleave",(()=>{e.style.backgroundColor="transparent",e.style.border="none"}))}createEditorContainer(){const t=new w({target:"widgetic-editor-portal",className:"widgetic-editor-modal",zIndex:1e5,clickThrough:!1});const e=document.createElement("div");e.className="widgetic-editor-modal-backdrop",e.style.cssText="\n position: fixed;\n top: 0;\n left: 0;\n width: 100vw;\n height: 100vh;\n background: rgba(0, 0, 0, 0.8);\n display: flex;\n align-items: center;\n justify-content: center;\n ";const i=document.createElement("div");return i.className="widgetic-editor-content",i.style.cssText="\n width: 90vw;\n height: 90vh;\n max-width: 1200px;\n max-height: 800px;\n background: white;\n border-radius: 8px;\n overflow: hidden;\n position: relative;\n ",e.appendChild(i),t.mount(e),{container:e,portal:t}}openEditor(e){return t(this,void 0,void 0,(function*(){try{const t=this.sessions.get(e);if(!t)return void s.warn("No editor session found",{compositionId:e});if(t.isOpen)return void s.debug("Editor already open",{compositionId:e});s.info("Opening composition editor",{compositionId:e});const i=I(),o=yield i.initializeUISdk(),{container:n,portal:r}=this.createEditorContainer(),a=yield o.mountCompositionEditor(n.querySelector(".widgetic-editor-content"),e,{onSave:t=>this.handleEditorSave(e,t),onClose:()=>this.closeEditor(e),onPreview:t=>this.handleEditorPreview(e,t)}),d=c().createWidgetController(e);if(t.isOpen=!0,t.editorContainer=n,t.editorInstance=a,t.widgetController=d,t.portal=r,t.overlay){const e=t.overlay.querySelector(".widgetic-edit-button");e&&(e.style.display="none")}s.info("Composition editor opened successfully",{compositionId:e})}catch(t){s.error("Failed to open editor",{compositionId:e,error:t}),this.showError("Failed to open editor. Please try again.",e)}}))}handleEditorSave(e,i){return t(this,void 0,void 0,(function*(){try{s.info("Saving composition changes",{compositionId:e,data:i});const t=this.sessions.get(e);(null==t?void 0:t.widgetController)&&(yield t.widgetController.updateConfig(i),yield t.widgetController.refresh()),yield this.closeEditor(e),s.info("Composition saved successfully",{compositionId:e})}catch(t){s.error("Failed to save composition",{compositionId:e,error:t}),this.showError("Failed to save changes. Please try again.",e)}}))}handleEditorPreview(e,i){return t(this,void 0,void 0,(function*(){try{const t=this.sessions.get(e);(null==t?void 0:t.widgetController)&&(yield t.widgetController.callFunction("preview",i))}catch(t){s.warn("Preview update failed",{compositionId:e,error:t})}}))}closeEditor(e){return t(this,void 0,void 0,(function*(){const t=this.sessions.get(e);if(t&&t.isOpen)try{if(t.editorInstance&&t.editorContainer){const e=I().getInstance();e&&(yield e.unmountCompositionEditor(t.editorContainer))}if(t.portal&&t.portal.destroy(),t.overlay){const e=t.overlay.querySelector(".widgetic-edit-button");e&&(e.style.display="block")}t.isOpen=!1,t.editorContainer=void 0,t.editorInstance=void 0,t.portal=void 0,s.info("Editor closed",{compositionId:e})}catch(t){s.error("Error closing editor",{compositionId:e,error:t})}}))}showError(t,e){const i=document.createElement("div");i.style.cssText="\n position: fixed;\n top: 20px;\n right: 20px;\n background: #dc3545;\n color: white;\n padding: 12px 16px;\n border-radius: 4px;\n z-index: 100001;\n font-size: 14px;\n max-width: 300px;\n ",i.textContent=t,document.body.appendChild(i),setTimeout((()=>{i.parentNode&&i.parentNode.removeChild(i)}),5e3)}removeOverlay(t){const e=this.sessions.get(t);e&&(e.isOpen&&this.closeEditor(t),e.overlay&&e.overlay.parentNode&&e.overlay.parentNode.removeChild(e.overlay),e.portal&&e.portal.destroy(),this.sessions.delete(t),s.debug("Editor overlay removed",{compositionId:t}))}isEditorOpen(t){var e;return(null===(e=this.sessions.get(t))||void 0===e?void 0:e.isOpen)||!1}getActiveSessions(){return Array.from(this.sessions.keys())}cleanup(){for(const t of this.sessions.keys())this.removeOverlay(t);s.debug("Editor overlay cleanup complete")}}let O=null;function x(t){return O||(O=new k(t)),O}var U=Object.freeze({__proto__:null,EditorOverlay:k,getEditorOverlay:x,resetEditorOverlay:function(){O&&(O.cleanup(),O=null)}});export{x as a,I as b,b as c,u as d,U as e,f as g,h as t};
|