@iss-ai/window-message-bus 0.0.1 → 0.0.2
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/lib/index.cjs.js +1 -1
- package/lib/index.esm.js +1 -1
- package/lib/index.min.js +1 -1
- package/lib/index.umd.js +1 -1
- package/package.json +1 -1
package/lib/index.cjs.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var t=function(e,o){return t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var o in e)Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o])},t(e,o)};function e(e,o){if("function"!=typeof o&&null!==o)throw new TypeError("Class extends value "+String(o)+" is not a constructor or null");function n(){this.constructor=e}t(e,o),e.prototype=null===o?Object.create(o):(n.prototype=o.prototype,new n)}var o=function(){return o=Object.assign||function(t){for(var e,o=1,n=arguments.length;o<n;o++)for(var r in e=arguments[o])Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r]);return t},o.apply(this,arguments)};function n(t,e,o,n){return new(o||(o=Promise))((function(e,r){function i(t){try{a(n.next(t))}catch(t){r(t)}}function s(t){try{a(n.throw(t))}catch(t){r(t)}}function a(t){var n;t.done?e(t.value):(n=t.value,n instanceof o?n:new o((function(t){t(n)}))).then(i,s)}a((n=n.apply(t,[])).next())}))}function r(t,e){var o,n,r,i={label:0,sent:function(){if(1&r[0])throw r[1];return r[1]},trys:[],ops:[]},s=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return s.next=a(0),s.throw=a(1),s.return=a(2),"function"==typeof Symbol&&(s[Symbol.iterator]=function(){return this}),s;function a(a){return function(c){return function(a){if(o)throw new TypeError("Generator is already executing.");for(;s&&(s=0,a[0]&&(i=0)),i;)try{if(o=1,n&&(r=2&a[0]?n.return:a[0]?n.throw||((r=n.return)&&r.call(n),0):n.next)&&!(r=r.call(n,a[1])).done)return r;switch(n=0,r&&(a=[2&a[0],r.value]),a[0]){case 0:case 1:r=a;break;case 4:return i.label++,{value:a[1],done:!1};case 5:i.label++,n=a[1],a=[0];continue;case 7:a=i.ops.pop(),i.trys.pop();continue;default:if(!((r=(r=i.trys).length>0&&r[r.length-1])||6!==a[0]&&2!==a[0])){i=0;continue}if(3===a[0]&&(!r||a[1]>r[0]&&a[1]<r[3])){i.label=a[1];break}if(6===a[0]&&i.label<r[1]){i.label=r[1],r=a;break}if(r&&i.label<r[2]){i.label=r[2],i.ops.push(a);break}r[2]&&i.ops.pop(),i.trys.pop();continue}a=e.call(t,i)}catch(t){a=[6,t],n=0}finally{o=r=0}if(5&a[0])throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}([a,c])}}}function i(t,e,o){if(2===arguments.length)for(var n,r=0,i=e.length;r<i;r++)!n&&r in e||(n||(n=Array.prototype.slice.call(e,0,r)),n[r]=e[r]);return t.concat(n||Array.prototype.slice.call(e))}"function"==typeof SuppressedError&&SuppressedError,"function"==typeof SuppressedError&&SuppressedError;var s=function(){function t(t){var e,o;void 0===t&&(t={}),this._events=new Map,this._debug=null!==(e=t.debug)&&void 0!==e&&e,this._debugPrefix=null!==(o=t.debugPrefix)&&void 0!==o?o:"[SampleEventBus]"}return t.prototype.log=function(t){for(var e=[],o=1;o<arguments.length;o++)e[o-1]=arguments[o];this._debug&&console.log.apply(console,i(["".concat((new Date).toLocaleString(),":").concat(this._debugPrefix,": ").concat(t)],e,!1))},t.prototype.enableDebug=function(t){return this._debug=!0,t&&(this._debugPrefix=t),this.log("Debug mode enabled"),this},t.prototype.disableDebug=function(){return this.log("Debug mode disabled"),this._debug=!1,this},t.prototype.isDebugEnabled=function(){return this._debug},t.prototype.on=function(t,e){if("function"!=typeof e)throw new TypeError("Handler must be a function");var o=this._events.get(t);return o?Array.isArray(o)?(o.push(e),this.log("Added listener #".concat(o.length,' for "').concat(t,'"'))):(this._events.set(t,[o,e]),this.log('Converted to multi-listener for "'.concat(t,'", count: 2'))):(this._events.set(t,e),this.log('Registered first listener for "'.concat(t,'"'))),this},t.prototype.emit=function(t){for(var e=[],o=1;o<arguments.length;o++)e[o-1]=arguments[o];return n(this,0,void 0,(function(){var o,s,a=this;return r(this,(function(c){switch(c.label){case 0:return(o=this._events.get(t))?(s=Array.isArray(o)?o:[o],this.log.apply(this,i(['Emitting "'.concat(t,'" to ').concat(s.length," listener(s)")],e,!1)),[4,Promise.all(s.map((function(o,i){return n(a,0,void 0,(function(){var n;return r(this,(function(r){switch(r.label){case 0:return r.trys.push([0,2,,3]),this.log("Calling listener #".concat(i+1,' for "').concat(t,'"')),[4,o.apply(void 0,e)];case 1:return r.sent(),this.log("Listener #".concat(i+1,' for "').concat(t,'" completed')),[3,3];case 2:return n=r.sent(),console.error("".concat(this._debugPrefix," Error in listener #").concat(i+1,' for "').concat(t,'":'),n),[3,3];case 3:return[2]}}))}))})))]):(this.log('No listeners for "'.concat(t,'", skipping')),[2]);case 1:return c.sent(),this.log('All listeners for "'.concat(t,'" finished')),[2]}}))}))},t.prototype.off=function(t,e){var o=this._events.get(t);if(!o)return this.log('No listeners to remove for "'.concat(t,'"')),this;if(!e){var n=Array.isArray(o)?o.length:1;return this._events.delete(t),this.log("Removed all ".concat(n,' listener(s) for "').concat(t,'"')),this}if("function"==typeof o)o===e?(this._events.delete(t),this.log('Removed only listener for "'.concat(t,'"'))):this.log('Handler not found for "'.concat(t,'"'));else{var r=o.indexOf(e);r>-1?(o.splice(r,1),this.log("Removed listener #".concat(r+1,' for "').concat(t,'", ').concat(o.length," remaining")),0===o.length?(this._events.delete(t),this.log('No more listeners for "'.concat(t,'"'))):1===o.length&&(this._events.set(t,o[0]),this.log('Converted back to single listener for "'.concat(t,'"')))):this.log("Handler not found in ".concat(o.length,' listeners for "').concat(t,'"'))}return this},t.prototype.once=function(t,e){var o=this;if("function"!=typeof e)throw new TypeError("Handler must be a function");this.log('Registering once listener for "'.concat(t,'"'));var i=function(){for(var s=[],a=0;a<arguments.length;a++)s[a]=arguments[a];return n(o,0,void 0,(function(){return r(this,(function(o){switch(o.label){case 0:return this.log('Once listener triggered for "'.concat(t,'", removing')),this.off(t,i),[4,e.apply(void 0,s)];case 1:return o.sent(),[2]}}))}))};return this.on(t,i)},t.prototype.has=function(t){var e=this._events.has(t);return this.log('Checking "'.concat(t,'": ').concat(e?"has listeners":"no listeners")),e},t.prototype.listenerCount=function(t){var e=this._events.get(t),o=e?Array.isArray(e)?e.length:1:0;return this.log('Listener count for "'.concat(t,'": ').concat(o)),o},t.prototype.clear=function(){var t=this._events.size;this._events.clear(),this.log("Cleared all ".concat(t," event(s)"))},t}();function a(){return"".concat(Date.now(),"-").concat(Math.random().toString(36).slice(2,9))}var c=function(t){function o(e){void 0===e&&(e={});var o=t.call(this,{debug:e.debug,debugPrefix:e.debugPrefix||"[WindowMessageBus]"})||this;return o._sourceId=e.sourceId||"wmb-".concat(a().slice(0,8)),o._allowedOrigins=e.allowedOrigins||["*"],o._serialize=e.serialize||JSON.stringify,o._deserialize=e.deserialize||JSON.parse,o._messageHandler=o._handleMessage.bind(o),o._attachMessageListener(),o}return e(o,t),o.prototype._attachMessageListener=function(){"undefined"!=typeof window&&(window.addEventListener("message",this._messageHandler,!1),this.log("Attached to window message event"))},o.prototype._detachMessageListener=function(){"undefined"!=typeof window&&(window.removeEventListener("message",this._messageHandler,!1),this.log("Detached from window message event"))},o.prototype._isAllowedOrigin=function(t){return!!this._allowedOrigins.includes("*")||this._allowedOrigins.includes(t)},o.prototype._handleMessage=function(e){try{var o=e.data,n=e.origin,r=e.source,i=o;if("string"==typeof o)try{i=this._deserialize(o)}catch(t){return void this.log("Failed to deserialize message data:",t)}if(!i||"object"!=typeof i||!("type"in i))return;var s=i;if(r&&!this._isAllowedOrigin(n))return void this.log("Blocked message from disallowed origin: ".concat(n));if(s.sourceId===this._sourceId)return void this.log("Ignored own message: ".concat(s.type));this.log("Received message: ".concat(s.type," from ").concat(n),s.data),t.prototype.emit.call(this,s.type,s.data,{origin:n,source:r,messageId:s.messageId,sourceId:s.sourceId})}catch(t){this.log("Error handling message:",t)}},o.prototype._getTargetWindow=function(t){return"undefined"==typeof window?null:"parent"===t?window.parent:"self"===t?window:"top"===t?window.top:t},o.prototype.buildMessage=function(t,e){return{type:t,data:e,messageId:a(),sourceId:this._sourceId}},o.prototype.emitTo=function(t,e){for(var o,n=[],r=2;r<arguments.length;r++)n[r-2]=arguments[r];var i=this._getTargetWindow(t);if(i){var s=this.buildMessage(e,1===n.length?n[0]:n),a=this._serialize(s);try{var c="*";"string"==typeof t&&("parent"===t&&window.parent.location?c=window.parent.location.origin:"top"===t&&(null===(o=window.top)||void 0===o?void 0:o.location)&&(c=window.top.location.origin)),i.postMessage(a,c),this.log("Sent message: ".concat(e," to ").concat(t),s)}catch(t){throw this.log("Failed to send message: ".concat(e),t),new Error("Failed to post message: ".concat(t))}}else this.log('Cannot send message: no target window for "'.concat(t,'"'))},o.prototype.emitToChildren=function(t){for(var e=[],o=1;o<arguments.length;o++)e[o-1]=arguments[o];if("undefined"!=typeof window&&window.frames)for(var n=this.buildMessage(t,1===e.length?e[0]:e),r=this._serialize(n),i=0;i<window.frames.length;i++){var s=window.frames[i];if(s&&s!==window)try{s.postMessage(r,"*"),this.log("Broadcasted message: ".concat(t," to frame[").concat(i,"]"),n)}catch(t){this.log("Failed to broadcast to frame[".concat(i,"]"),t)}}},o.prototype.getSourceId=function(){return this._sourceId},o.prototype.updateAllowedOrigins=function(t){this._allowedOrigins=t,this.log("Updated allowed origins: ".concat(t.join(", ")))},o.prototype.addAllowedOrigin=function(t){this._allowedOrigins.includes(t)||(this._allowedOrigins.push(t),this.log("Added allowed origin: ".concat(t)))},o.prototype.removeAllowedOrigin=function(t){var e=this._allowedOrigins.indexOf(t);e>-1&&(this._allowedOrigins.splice(e,1),this.log("Removed allowed origin: ".concat(t)))},o.prototype.destroy=function(){this._detachMessageListener(),this.clear(),this.log("WindowMessageBus destroyed")},o}(s);var l=function(t){function n(e){void 0===e&&(e={});var o=t.call(this,{debug:e.debug,sourceId:e.sourceId,allowedOrigins:e.allowedOrigins})||this;return"undefined"!=typeof window?o._defaultTarget=window.self!==window.top?"parent":"self":o._defaultTarget="self",o}return e(n,t),n.prototype.getDefaultTarget=function(){return this._defaultTarget},n.prototype.isReady=function(t,e){void 0===t&&(t={});var n=e||this._defaultTarget;this.emitTo(n,"isReady",o({sourceId:this.getSourceId(),timestamp:Date.now()},t))},n.prototype.onReady=function(t){return this._createUnsubscribe("onReady",t)},n.prototype.setData=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"setData",{data:t,timestamp:Date.now()})},n.prototype.onSetData=function(t){return this._createUnsubscribe("onSetData",(function(e){t(null==e?void 0:e.data)}))},n.prototype.setConfig=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"setConfig",{config:t,timestamp:Date.now()})},n.prototype.onSetConfig=function(t){return this._createUnsubscribe("onSetConfig",(function(e){t(null==e?void 0:e.config)}))},n.prototype.change=function(t,e){var n=e||this._defaultTarget;this.emitTo(n,"change",o(o({},t),{timestamp:t.timestamp||Date.now()}))},n.prototype.onChange=function(t){return this._createUnsubscribe("onChange",t)},n.prototype.save=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"save",{options:t,timestamp:Date.now()})},n.prototype.onSave=function(t){return this._createUnsubscribe("onSave",(function(e){t(null==e?void 0:e.options)}))},n.prototype.exportSVG=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"exportSVG",{options:t,timestamp:Date.now()})},n.prototype.onExportSVG=function(t){return this._createUnsubscribe("onExportSVG",(function(e){t(null==e?void 0:e.options)}))},n.prototype.exportPNG=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"exportPNG",{options:t,timestamp:Date.now()})},n.prototype.onExportPNG=function(t){return this._createUnsubscribe("onExportPNG",(function(e){t(null==e?void 0:e.options)}))},n.prototype.exportData=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"exportData",{options:t,timestamp:Date.now()})},n.prototype.onExportData=function(t){return this._createUnsubscribe("onExportData",(function(e){t(null==e?void 0:e.options)}))},n.prototype.getData=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"getData",{options:t,timestamp:Date.now()})},n.prototype.onGetData=function(t){return this._createUnsubscribe("onGetData",(function(e){t(null==e?void 0:e.options)}))},n.prototype.thumnail=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"thumnail",{options:t,timestamp:Date.now()})},n.prototype.onThumnail=function(t){return this._createUnsubscribe("onThumnail",(function(e){t(null==e?void 0:e.options)}))},n.prototype.preview=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"preview",{options:t,timestamp:Date.now()})},n.prototype.onPreview=function(t){return this._createUnsubscribe("onPreview",(function(e){t(null==e?void 0:e.options)}))},n.prototype._createUnsubscribe=function(t,e){var o=e;this.on(t,o);var n=!1,r=o;return this.on(t,(function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];if(!n)return r.apply(this,t)})),function(){n=!0}},n.prototype.destroy=function(){t.prototype.destroy.call(this),this.isDebugEnabled()&&console.log("".concat((new Date).toLocaleString(),":[EditorMessageBus]: EditorMessageBus destroyed"))},n}(c);exports.EditorMessageBus=l,exports.WindowMessageBus=c,exports.createEditorMessageBus=function(t){return new l(t)},exports.createWindowMessageBus=function(t){return new c(t)};
|
|
1
|
+
"use strict";var t=function(e,o){return t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var o in e)Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o])},t(e,o)};function e(e,o){if("function"!=typeof o&&null!==o)throw new TypeError("Class extends value "+String(o)+" is not a constructor or null");function n(){this.constructor=e}t(e,o),e.prototype=null===o?Object.create(o):(n.prototype=o.prototype,new n)}var o=function(){return o=Object.assign||function(t){for(var e,o=1,n=arguments.length;o<n;o++)for(var r in e=arguments[o])Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r]);return t},o.apply(this,arguments)};function n(t,e,o,n){return new(o||(o=Promise))((function(e,r){function i(t){try{a(n.next(t))}catch(t){r(t)}}function s(t){try{a(n.throw(t))}catch(t){r(t)}}function a(t){var n;t.done?e(t.value):(n=t.value,n instanceof o?n:new o((function(t){t(n)}))).then(i,s)}a((n=n.apply(t,[])).next())}))}function r(t,e){var o,n,r,i={label:0,sent:function(){if(1&r[0])throw r[1];return r[1]},trys:[],ops:[]},s=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return s.next=a(0),s.throw=a(1),s.return=a(2),"function"==typeof Symbol&&(s[Symbol.iterator]=function(){return this}),s;function a(a){return function(c){return function(a){if(o)throw new TypeError("Generator is already executing.");for(;s&&(s=0,a[0]&&(i=0)),i;)try{if(o=1,n&&(r=2&a[0]?n.return:a[0]?n.throw||((r=n.return)&&r.call(n),0):n.next)&&!(r=r.call(n,a[1])).done)return r;switch(n=0,r&&(a=[2&a[0],r.value]),a[0]){case 0:case 1:r=a;break;case 4:return i.label++,{value:a[1],done:!1};case 5:i.label++,n=a[1],a=[0];continue;case 7:a=i.ops.pop(),i.trys.pop();continue;default:if(!((r=(r=i.trys).length>0&&r[r.length-1])||6!==a[0]&&2!==a[0])){i=0;continue}if(3===a[0]&&(!r||a[1]>r[0]&&a[1]<r[3])){i.label=a[1];break}if(6===a[0]&&i.label<r[1]){i.label=r[1],r=a;break}if(r&&i.label<r[2]){i.label=r[2],i.ops.push(a);break}r[2]&&i.ops.pop(),i.trys.pop();continue}a=e.call(t,i)}catch(t){a=[6,t],n=0}finally{o=r=0}if(5&a[0])throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}([a,c])}}}function i(t,e,o){if(2===arguments.length)for(var n,r=0,i=e.length;r<i;r++)!n&&r in e||(n||(n=Array.prototype.slice.call(e,0,r)),n[r]=e[r]);return t.concat(n||Array.prototype.slice.call(e))}"function"==typeof SuppressedError&&SuppressedError,"function"==typeof SuppressedError&&SuppressedError;var s=function(){function t(t){var e,o;void 0===t&&(t={}),this._events=new Map,this._debug=null!==(e=t.debug)&&void 0!==e&&e,this._debugPrefix=null!==(o=t.debugPrefix)&&void 0!==o?o:"[SampleEventBus]"}return t.prototype.log=function(t){for(var e=[],o=1;o<arguments.length;o++)e[o-1]=arguments[o];this._debug&&console.log.apply(console,i(["".concat((new Date).toLocaleString(),":").concat(this._debugPrefix,": ").concat(t)],e,!1))},t.prototype.enableDebug=function(t){return this._debug=!0,t&&(this._debugPrefix=t),this.log("Debug mode enabled"),this},t.prototype.disableDebug=function(){return this.log("Debug mode disabled"),this._debug=!1,this},t.prototype.isDebugEnabled=function(){return this._debug},t.prototype.on=function(t,e){if("function"!=typeof e)throw new TypeError("Handler must be a function");var o=this._events.get(t);return o?Array.isArray(o)?(o.push(e),this.log("Added listener #".concat(o.length,' for "').concat(t,'"'))):(this._events.set(t,[o,e]),this.log('Converted to multi-listener for "'.concat(t,'", count: 2'))):(this._events.set(t,e),this.log('Registered first listener for "'.concat(t,'"'))),this},t.prototype.emit=function(t){for(var e=[],o=1;o<arguments.length;o++)e[o-1]=arguments[o];return n(this,0,void 0,(function(){var o,s,a=this;return r(this,(function(c){switch(c.label){case 0:return(o=this._events.get(t))?(s=Array.isArray(o)?o:[o],this.log.apply(this,i(['Emitting "'.concat(t,'" to ').concat(s.length," listener(s)")],e,!1)),[4,Promise.all(s.map((function(o,i){return n(a,0,void 0,(function(){var n;return r(this,(function(r){switch(r.label){case 0:return r.trys.push([0,2,,3]),this.log("Calling listener #".concat(i+1,' for "').concat(t,'"')),[4,o.apply(void 0,e)];case 1:return r.sent(),this.log("Listener #".concat(i+1,' for "').concat(t,'" completed')),[3,3];case 2:return n=r.sent(),console.error("".concat(this._debugPrefix," Error in listener #").concat(i+1,' for "').concat(t,'":'),n),[3,3];case 3:return[2]}}))}))})))]):(this.log('No listeners for "'.concat(t,'", skipping')),[2]);case 1:return c.sent(),this.log('All listeners for "'.concat(t,'" finished')),[2]}}))}))},t.prototype.off=function(t,e){var o=this._events.get(t);if(!o)return this.log('No listeners to remove for "'.concat(t,'"')),this;if(!e){var n=Array.isArray(o)?o.length:1;return this._events.delete(t),this.log("Removed all ".concat(n,' listener(s) for "').concat(t,'"')),this}if("function"==typeof o)o===e?(this._events.delete(t),this.log('Removed only listener for "'.concat(t,'"'))):this.log('Handler not found for "'.concat(t,'"'));else{var r=o.indexOf(e);r>-1?(o.splice(r,1),this.log("Removed listener #".concat(r+1,' for "').concat(t,'", ').concat(o.length," remaining")),0===o.length?(this._events.delete(t),this.log('No more listeners for "'.concat(t,'"'))):1===o.length&&(this._events.set(t,o[0]),this.log('Converted back to single listener for "'.concat(t,'"')))):this.log("Handler not found in ".concat(o.length,' listeners for "').concat(t,'"'))}return this},t.prototype.once=function(t,e){var o=this;if("function"!=typeof e)throw new TypeError("Handler must be a function");this.log('Registering once listener for "'.concat(t,'"'));var i=function(){for(var s=[],a=0;a<arguments.length;a++)s[a]=arguments[a];return n(o,0,void 0,(function(){return r(this,(function(o){switch(o.label){case 0:return this.log('Once listener triggered for "'.concat(t,'", removing')),this.off(t,i),[4,e.apply(void 0,s)];case 1:return o.sent(),[2]}}))}))};return this.on(t,i)},t.prototype.has=function(t){var e=this._events.has(t);return this.log('Checking "'.concat(t,'": ').concat(e?"has listeners":"no listeners")),e},t.prototype.listenerCount=function(t){var e=this._events.get(t),o=e?Array.isArray(e)?e.length:1:0;return this.log('Listener count for "'.concat(t,'": ').concat(o)),o},t.prototype.clear=function(){var t=this._events.size;this._events.clear(),this.log("Cleared all ".concat(t," event(s)"))},t}();function a(){return"".concat(Date.now(),"-").concat(Math.random().toString(36).slice(2,9))}var c=function(t){function o(e){void 0===e&&(e={});var o=t.call(this,{debug:e.debug,debugPrefix:e.debugPrefix||"[WindowMessageBus]"})||this;return o._sourceId=e.sourceId||"wmb-".concat(a().slice(0,8)),o._allowedOrigins=e.allowedOrigins||["*"],o._serialize=e.serialize||JSON.stringify,o._deserialize=e.deserialize||JSON.parse,o._messageHandler=o._handleMessage.bind(o),o._attachMessageListener(),o}return e(o,t),o.prototype._attachMessageListener=function(){"undefined"!=typeof window&&(window.addEventListener("message",this._messageHandler,!1),this.log("Attached to window message event"))},o.prototype._detachMessageListener=function(){"undefined"!=typeof window&&(window.removeEventListener("message",this._messageHandler,!1),this.log("Detached from window message event"))},o.prototype._isAllowedOrigin=function(t){return!!this._allowedOrigins.includes("*")||this._allowedOrigins.includes(t)},o.prototype._handleMessage=function(e){try{var o=e.data,n=e.origin,r=e.source,i=o;if("string"==typeof o)try{i=this._deserialize(o)}catch(t){return void this.log("Failed to deserialize message data:",t)}if(!i||"object"!=typeof i||!("type"in i))return;var s=i;if(r&&!this._isAllowedOrigin(n))return void this.log("Blocked message from disallowed origin: ".concat(n));if(s.sourceId===this._sourceId)return void this.log("Ignored own message: ".concat(s.type));this.log("Received message: ".concat(s.type," from ").concat(n),s.data),t.prototype.emit.call(this,s.type,s.data,{origin:n,source:r,messageId:s.messageId,sourceId:s.sourceId})}catch(t){this.log("Error handling message:",t)}},o.prototype._getTargetWindow=function(t){return"undefined"==typeof window?null:"parent"===t?window.parent:"self"===t?window:"top"===t?window.top:t},o.prototype.buildMessage=function(t,e){return{type:t,data:e,messageId:a(),sourceId:this._sourceId}},o.prototype.emitTo=function(t,e){for(var o=[],n=2;n<arguments.length;n++)o[n-2]=arguments[n];var r=this._getTargetWindow(t);if(r){var i=this.buildMessage(e,1===o.length?o[0]:o),s=this._serialize(i);try{r.postMessage(s,"*"),this.log("Sent message: ".concat(e," to ").concat(t),i)}catch(t){throw this.log("Failed to send message: ".concat(e),t),new Error("Failed to post message: ".concat(t))}}else this.log('Cannot send message: no target window for "'.concat(t,'"'))},o.prototype.emitToChildren=function(t){for(var e=[],o=1;o<arguments.length;o++)e[o-1]=arguments[o];if("undefined"!=typeof window&&window.frames)for(var n=this.buildMessage(t,1===e.length?e[0]:e),r=this._serialize(n),i=0;i<window.frames.length;i++){var s=window.frames[i];if(s&&s!==window)try{s.postMessage(r,"*"),this.log("Broadcasted message: ".concat(t," to frame[").concat(i,"]"),n)}catch(t){this.log("Failed to broadcast to frame[".concat(i,"]"),t)}}},o.prototype.getSourceId=function(){return this._sourceId},o.prototype.updateAllowedOrigins=function(t){this._allowedOrigins=t,this.log("Updated allowed origins: ".concat(t.join(", ")))},o.prototype.addAllowedOrigin=function(t){this._allowedOrigins.includes(t)||(this._allowedOrigins.push(t),this.log("Added allowed origin: ".concat(t)))},o.prototype.removeAllowedOrigin=function(t){var e=this._allowedOrigins.indexOf(t);e>-1&&(this._allowedOrigins.splice(e,1),this.log("Removed allowed origin: ".concat(t)))},o.prototype.destroy=function(){this._detachMessageListener(),this.clear(),this.log("WindowMessageBus destroyed")},o}(s);var u=function(t){function n(e){void 0===e&&(e={});var o=t.call(this,{debug:e.debug,sourceId:e.sourceId,allowedOrigins:e.allowedOrigins})||this;return"undefined"!=typeof window?o._defaultTarget=window.self!==window.top?"parent":"self":o._defaultTarget="self",o}return e(n,t),n.prototype.getDefaultTarget=function(){return this._defaultTarget},n.prototype.isReady=function(t,e){void 0===t&&(t={});var n=e||this._defaultTarget;this.emitTo(n,"isReady",o({sourceId:this.getSourceId(),timestamp:Date.now()},t))},n.prototype.onReady=function(t){return this._createUnsubscribe("onReady",t)},n.prototype.setData=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"setData",{data:t,timestamp:Date.now()})},n.prototype.onSetData=function(t){return this._createUnsubscribe("onSetData",(function(e){t(null==e?void 0:e.data)}))},n.prototype.setConfig=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"setConfig",{config:t,timestamp:Date.now()})},n.prototype.onSetConfig=function(t){return this._createUnsubscribe("onSetConfig",(function(e){t(null==e?void 0:e.config)}))},n.prototype.change=function(t,e){var n=e||this._defaultTarget;this.emitTo(n,"change",o(o({},t),{timestamp:t.timestamp||Date.now()}))},n.prototype.onChange=function(t){return this._createUnsubscribe("onChange",t)},n.prototype.save=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"save",{options:t,timestamp:Date.now()})},n.prototype.onSave=function(t){return this._createUnsubscribe("onSave",(function(e){t(null==e?void 0:e.options)}))},n.prototype.exportSVG=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"exportSVG",{options:t,timestamp:Date.now()})},n.prototype.onExportSVG=function(t){return this._createUnsubscribe("onExportSVG",(function(e){t(null==e?void 0:e.options)}))},n.prototype.exportPNG=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"exportPNG",{options:t,timestamp:Date.now()})},n.prototype.onExportPNG=function(t){return this._createUnsubscribe("onExportPNG",(function(e){t(null==e?void 0:e.options)}))},n.prototype.exportData=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"exportData",{options:t,timestamp:Date.now()})},n.prototype.onExportData=function(t){return this._createUnsubscribe("onExportData",(function(e){t(null==e?void 0:e.options)}))},n.prototype.getData=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"getData",{options:t,timestamp:Date.now()})},n.prototype.onGetData=function(t){return this._createUnsubscribe("onGetData",(function(e){t(null==e?void 0:e.options)}))},n.prototype.thumnail=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"thumnail",{options:t,timestamp:Date.now()})},n.prototype.onThumnail=function(t){return this._createUnsubscribe("onThumnail",(function(e){t(null==e?void 0:e.options)}))},n.prototype.preview=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"preview",{options:t,timestamp:Date.now()})},n.prototype.onPreview=function(t){return this._createUnsubscribe("onPreview",(function(e){t(null==e?void 0:e.options)}))},n.prototype._createUnsubscribe=function(t,e){var o=e;this.on(t,o);var n=!1,r=o;return this.on(t,(function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];if(!n)return r.apply(this,t)})),function(){n=!0}},n.prototype.destroy=function(){t.prototype.destroy.call(this),this.isDebugEnabled()&&console.log("".concat((new Date).toLocaleString(),":[EditorMessageBus]: EditorMessageBus destroyed"))},n}(c);exports.EditorMessageBus=u,exports.WindowMessageBus=c,exports.createEditorMessageBus=function(t){return new u(t)},exports.createWindowMessageBus=function(t){return new c(t)};
|
package/lib/index.esm.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var t=function(e,o){return t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var o in e)Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o])},t(e,o)};function e(e,o){if("function"!=typeof o&&null!==o)throw new TypeError("Class extends value "+String(o)+" is not a constructor or null");function n(){this.constructor=e}t(e,o),e.prototype=null===o?Object.create(o):(n.prototype=o.prototype,new n)}var o=function(){return o=Object.assign||function(t){for(var e,o=1,n=arguments.length;o<n;o++)for(var r in e=arguments[o])Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r]);return t},o.apply(this,arguments)};function n(t,e,o,n){return new(o||(o=Promise))((function(e,r){function i(t){try{a(n.next(t))}catch(t){r(t)}}function s(t){try{a(n.throw(t))}catch(t){r(t)}}function a(t){var n;t.done?e(t.value):(n=t.value,n instanceof o?n:new o((function(t){t(n)}))).then(i,s)}a((n=n.apply(t,[])).next())}))}function r(t,e){var o,n,r,i={label:0,sent:function(){if(1&r[0])throw r[1];return r[1]},trys:[],ops:[]},s=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return s.next=a(0),s.throw=a(1),s.return=a(2),"function"==typeof Symbol&&(s[Symbol.iterator]=function(){return this}),s;function a(a){return function(c){return function(a){if(o)throw new TypeError("Generator is already executing.");for(;s&&(s=0,a[0]&&(i=0)),i;)try{if(o=1,n&&(r=2&a[0]?n.return:a[0]?n.throw||((r=n.return)&&r.call(n),0):n.next)&&!(r=r.call(n,a[1])).done)return r;switch(n=0,r&&(a=[2&a[0],r.value]),a[0]){case 0:case 1:r=a;break;case 4:return i.label++,{value:a[1],done:!1};case 5:i.label++,n=a[1],a=[0];continue;case 7:a=i.ops.pop(),i.trys.pop();continue;default:if(!((r=(r=i.trys).length>0&&r[r.length-1])||6!==a[0]&&2!==a[0])){i=0;continue}if(3===a[0]&&(!r||a[1]>r[0]&&a[1]<r[3])){i.label=a[1];break}if(6===a[0]&&i.label<r[1]){i.label=r[1],r=a;break}if(r&&i.label<r[2]){i.label=r[2],i.ops.push(a);break}r[2]&&i.ops.pop(),i.trys.pop();continue}a=e.call(t,i)}catch(t){a=[6,t],n=0}finally{o=r=0}if(5&a[0])throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}([a,c])}}}function i(t,e,o){if(2===arguments.length)for(var n,r=0,i=e.length;r<i;r++)!n&&r in e||(n||(n=Array.prototype.slice.call(e,0,r)),n[r]=e[r]);return t.concat(n||Array.prototype.slice.call(e))}"function"==typeof SuppressedError&&SuppressedError,"function"==typeof SuppressedError&&SuppressedError;var s=function(){function t(t){var e,o;void 0===t&&(t={}),this._events=new Map,this._debug=null!==(e=t.debug)&&void 0!==e&&e,this._debugPrefix=null!==(o=t.debugPrefix)&&void 0!==o?o:"[SampleEventBus]"}return t.prototype.log=function(t){for(var e=[],o=1;o<arguments.length;o++)e[o-1]=arguments[o];this._debug&&console.log.apply(console,i(["".concat((new Date).toLocaleString(),":").concat(this._debugPrefix,": ").concat(t)],e,!1))},t.prototype.enableDebug=function(t){return this._debug=!0,t&&(this._debugPrefix=t),this.log("Debug mode enabled"),this},t.prototype.disableDebug=function(){return this.log("Debug mode disabled"),this._debug=!1,this},t.prototype.isDebugEnabled=function(){return this._debug},t.prototype.on=function(t,e){if("function"!=typeof e)throw new TypeError("Handler must be a function");var o=this._events.get(t);return o?Array.isArray(o)?(o.push(e),this.log("Added listener #".concat(o.length,' for "').concat(t,'"'))):(this._events.set(t,[o,e]),this.log('Converted to multi-listener for "'.concat(t,'", count: 2'))):(this._events.set(t,e),this.log('Registered first listener for "'.concat(t,'"'))),this},t.prototype.emit=function(t){for(var e=[],o=1;o<arguments.length;o++)e[o-1]=arguments[o];return n(this,0,void 0,(function(){var o,s,a=this;return r(this,(function(c){switch(c.label){case 0:return(o=this._events.get(t))?(s=Array.isArray(o)?o:[o],this.log.apply(this,i(['Emitting "'.concat(t,'" to ').concat(s.length," listener(s)")],e,!1)),[4,Promise.all(s.map((function(o,i){return n(a,0,void 0,(function(){var n;return r(this,(function(r){switch(r.label){case 0:return r.trys.push([0,2,,3]),this.log("Calling listener #".concat(i+1,' for "').concat(t,'"')),[4,o.apply(void 0,e)];case 1:return r.sent(),this.log("Listener #".concat(i+1,' for "').concat(t,'" completed')),[3,3];case 2:return n=r.sent(),console.error("".concat(this._debugPrefix," Error in listener #").concat(i+1,' for "').concat(t,'":'),n),[3,3];case 3:return[2]}}))}))})))]):(this.log('No listeners for "'.concat(t,'", skipping')),[2]);case 1:return c.sent(),this.log('All listeners for "'.concat(t,'" finished')),[2]}}))}))},t.prototype.off=function(t,e){var o=this._events.get(t);if(!o)return this.log('No listeners to remove for "'.concat(t,'"')),this;if(!e){var n=Array.isArray(o)?o.length:1;return this._events.delete(t),this.log("Removed all ".concat(n,' listener(s) for "').concat(t,'"')),this}if("function"==typeof o)o===e?(this._events.delete(t),this.log('Removed only listener for "'.concat(t,'"'))):this.log('Handler not found for "'.concat(t,'"'));else{var r=o.indexOf(e);r>-1?(o.splice(r,1),this.log("Removed listener #".concat(r+1,' for "').concat(t,'", ').concat(o.length," remaining")),0===o.length?(this._events.delete(t),this.log('No more listeners for "'.concat(t,'"'))):1===o.length&&(this._events.set(t,o[0]),this.log('Converted back to single listener for "'.concat(t,'"')))):this.log("Handler not found in ".concat(o.length,' listeners for "').concat(t,'"'))}return this},t.prototype.once=function(t,e){var o=this;if("function"!=typeof e)throw new TypeError("Handler must be a function");this.log('Registering once listener for "'.concat(t,'"'));var i=function(){for(var s=[],a=0;a<arguments.length;a++)s[a]=arguments[a];return n(o,0,void 0,(function(){return r(this,(function(o){switch(o.label){case 0:return this.log('Once listener triggered for "'.concat(t,'", removing')),this.off(t,i),[4,e.apply(void 0,s)];case 1:return o.sent(),[2]}}))}))};return this.on(t,i)},t.prototype.has=function(t){var e=this._events.has(t);return this.log('Checking "'.concat(t,'": ').concat(e?"has listeners":"no listeners")),e},t.prototype.listenerCount=function(t){var e=this._events.get(t),o=e?Array.isArray(e)?e.length:1:0;return this.log('Listener count for "'.concat(t,'": ').concat(o)),o},t.prototype.clear=function(){var t=this._events.size;this._events.clear(),this.log("Cleared all ".concat(t," event(s)"))},t}();function a(){return"".concat(Date.now(),"-").concat(Math.random().toString(36).slice(2,9))}var c=function(t){function o(e){void 0===e&&(e={});var o=t.call(this,{debug:e.debug,debugPrefix:e.debugPrefix||"[WindowMessageBus]"})||this;return o._sourceId=e.sourceId||"wmb-".concat(a().slice(0,8)),o._allowedOrigins=e.allowedOrigins||["*"],o._serialize=e.serialize||JSON.stringify,o._deserialize=e.deserialize||JSON.parse,o._messageHandler=o._handleMessage.bind(o),o._attachMessageListener(),o}return e(o,t),o.prototype._attachMessageListener=function(){"undefined"!=typeof window&&(window.addEventListener("message",this._messageHandler,!1),this.log("Attached to window message event"))},o.prototype._detachMessageListener=function(){"undefined"!=typeof window&&(window.removeEventListener("message",this._messageHandler,!1),this.log("Detached from window message event"))},o.prototype._isAllowedOrigin=function(t){return!!this._allowedOrigins.includes("*")||this._allowedOrigins.includes(t)},o.prototype._handleMessage=function(e){try{var o=e.data,n=e.origin,r=e.source,i=o;if("string"==typeof o)try{i=this._deserialize(o)}catch(t){return void this.log("Failed to deserialize message data:",t)}if(!i||"object"!=typeof i||!("type"in i))return;var s=i;if(r&&!this._isAllowedOrigin(n))return void this.log("Blocked message from disallowed origin: ".concat(n));if(s.sourceId===this._sourceId)return void this.log("Ignored own message: ".concat(s.type));this.log("Received message: ".concat(s.type," from ").concat(n),s.data),t.prototype.emit.call(this,s.type,s.data,{origin:n,source:r,messageId:s.messageId,sourceId:s.sourceId})}catch(t){this.log("Error handling message:",t)}},o.prototype._getTargetWindow=function(t){return"undefined"==typeof window?null:"parent"===t?window.parent:"self"===t?window:"top"===t?window.top:t},o.prototype.buildMessage=function(t,e){return{type:t,data:e,messageId:a(),sourceId:this._sourceId}},o.prototype.emitTo=function(t,e){for(var o,n=[],r=2;r<arguments.length;r++)n[r-2]=arguments[r];var i=this._getTargetWindow(t);if(i){var s=this.buildMessage(e,1===n.length?n[0]:n),a=this._serialize(s);try{var c="*";"string"==typeof t&&("parent"===t&&window.parent.location?c=window.parent.location.origin:"top"===t&&(null===(o=window.top)||void 0===o?void 0:o.location)&&(c=window.top.location.origin)),i.postMessage(a,c),this.log("Sent message: ".concat(e," to ").concat(t),s)}catch(t){throw this.log("Failed to send message: ".concat(e),t),new Error("Failed to post message: ".concat(t))}}else this.log('Cannot send message: no target window for "'.concat(t,'"'))},o.prototype.emitToChildren=function(t){for(var e=[],o=1;o<arguments.length;o++)e[o-1]=arguments[o];if("undefined"!=typeof window&&window.frames)for(var n=this.buildMessage(t,1===e.length?e[0]:e),r=this._serialize(n),i=0;i<window.frames.length;i++){var s=window.frames[i];if(s&&s!==window)try{s.postMessage(r,"*"),this.log("Broadcasted message: ".concat(t," to frame[").concat(i,"]"),n)}catch(t){this.log("Failed to broadcast to frame[".concat(i,"]"),t)}}},o.prototype.getSourceId=function(){return this._sourceId},o.prototype.updateAllowedOrigins=function(t){this._allowedOrigins=t,this.log("Updated allowed origins: ".concat(t.join(", ")))},o.prototype.addAllowedOrigin=function(t){this._allowedOrigins.includes(t)||(this._allowedOrigins.push(t),this.log("Added allowed origin: ".concat(t)))},o.prototype.removeAllowedOrigin=function(t){var e=this._allowedOrigins.indexOf(t);e>-1&&(this._allowedOrigins.splice(e,1),this.log("Removed allowed origin: ".concat(t)))},o.prototype.destroy=function(){this._detachMessageListener(),this.clear(),this.log("WindowMessageBus destroyed")},o}(s);function l(t){return new c(t)}var u=function(t){function n(e){void 0===e&&(e={});var o=t.call(this,{debug:e.debug,sourceId:e.sourceId,allowedOrigins:e.allowedOrigins})||this;return"undefined"!=typeof window?o._defaultTarget=window.self!==window.top?"parent":"self":o._defaultTarget="self",o}return e(n,t),n.prototype.getDefaultTarget=function(){return this._defaultTarget},n.prototype.isReady=function(t,e){void 0===t&&(t={});var n=e||this._defaultTarget;this.emitTo(n,"isReady",o({sourceId:this.getSourceId(),timestamp:Date.now()},t))},n.prototype.onReady=function(t){return this._createUnsubscribe("onReady",t)},n.prototype.setData=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"setData",{data:t,timestamp:Date.now()})},n.prototype.onSetData=function(t){return this._createUnsubscribe("onSetData",(function(e){t(null==e?void 0:e.data)}))},n.prototype.setConfig=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"setConfig",{config:t,timestamp:Date.now()})},n.prototype.onSetConfig=function(t){return this._createUnsubscribe("onSetConfig",(function(e){t(null==e?void 0:e.config)}))},n.prototype.change=function(t,e){var n=e||this._defaultTarget;this.emitTo(n,"change",o(o({},t),{timestamp:t.timestamp||Date.now()}))},n.prototype.onChange=function(t){return this._createUnsubscribe("onChange",t)},n.prototype.save=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"save",{options:t,timestamp:Date.now()})},n.prototype.onSave=function(t){return this._createUnsubscribe("onSave",(function(e){t(null==e?void 0:e.options)}))},n.prototype.exportSVG=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"exportSVG",{options:t,timestamp:Date.now()})},n.prototype.onExportSVG=function(t){return this._createUnsubscribe("onExportSVG",(function(e){t(null==e?void 0:e.options)}))},n.prototype.exportPNG=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"exportPNG",{options:t,timestamp:Date.now()})},n.prototype.onExportPNG=function(t){return this._createUnsubscribe("onExportPNG",(function(e){t(null==e?void 0:e.options)}))},n.prototype.exportData=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"exportData",{options:t,timestamp:Date.now()})},n.prototype.onExportData=function(t){return this._createUnsubscribe("onExportData",(function(e){t(null==e?void 0:e.options)}))},n.prototype.getData=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"getData",{options:t,timestamp:Date.now()})},n.prototype.onGetData=function(t){return this._createUnsubscribe("onGetData",(function(e){t(null==e?void 0:e.options)}))},n.prototype.thumnail=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"thumnail",{options:t,timestamp:Date.now()})},n.prototype.onThumnail=function(t){return this._createUnsubscribe("onThumnail",(function(e){t(null==e?void 0:e.options)}))},n.prototype.preview=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"preview",{options:t,timestamp:Date.now()})},n.prototype.onPreview=function(t){return this._createUnsubscribe("onPreview",(function(e){t(null==e?void 0:e.options)}))},n.prototype._createUnsubscribe=function(t,e){var o=e;this.on(t,o);var n=!1,r=o;return this.on(t,(function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];if(!n)return r.apply(this,t)})),function(){n=!0}},n.prototype.destroy=function(){t.prototype.destroy.call(this),this.isDebugEnabled()&&console.log("".concat((new Date).toLocaleString(),":[EditorMessageBus]: EditorMessageBus destroyed"))},n}(c);function p(t){return new u(t)}export{u as EditorMessageBus,c as WindowMessageBus,p as createEditorMessageBus,l as createWindowMessageBus};
|
|
1
|
+
var t=function(e,o){return t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var o in e)Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o])},t(e,o)};function e(e,o){if("function"!=typeof o&&null!==o)throw new TypeError("Class extends value "+String(o)+" is not a constructor or null");function n(){this.constructor=e}t(e,o),e.prototype=null===o?Object.create(o):(n.prototype=o.prototype,new n)}var o=function(){return o=Object.assign||function(t){for(var e,o=1,n=arguments.length;o<n;o++)for(var r in e=arguments[o])Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r]);return t},o.apply(this,arguments)};function n(t,e,o,n){return new(o||(o=Promise))((function(e,r){function i(t){try{a(n.next(t))}catch(t){r(t)}}function s(t){try{a(n.throw(t))}catch(t){r(t)}}function a(t){var n;t.done?e(t.value):(n=t.value,n instanceof o?n:new o((function(t){t(n)}))).then(i,s)}a((n=n.apply(t,[])).next())}))}function r(t,e){var o,n,r,i={label:0,sent:function(){if(1&r[0])throw r[1];return r[1]},trys:[],ops:[]},s=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return s.next=a(0),s.throw=a(1),s.return=a(2),"function"==typeof Symbol&&(s[Symbol.iterator]=function(){return this}),s;function a(a){return function(c){return function(a){if(o)throw new TypeError("Generator is already executing.");for(;s&&(s=0,a[0]&&(i=0)),i;)try{if(o=1,n&&(r=2&a[0]?n.return:a[0]?n.throw||((r=n.return)&&r.call(n),0):n.next)&&!(r=r.call(n,a[1])).done)return r;switch(n=0,r&&(a=[2&a[0],r.value]),a[0]){case 0:case 1:r=a;break;case 4:return i.label++,{value:a[1],done:!1};case 5:i.label++,n=a[1],a=[0];continue;case 7:a=i.ops.pop(),i.trys.pop();continue;default:if(!((r=(r=i.trys).length>0&&r[r.length-1])||6!==a[0]&&2!==a[0])){i=0;continue}if(3===a[0]&&(!r||a[1]>r[0]&&a[1]<r[3])){i.label=a[1];break}if(6===a[0]&&i.label<r[1]){i.label=r[1],r=a;break}if(r&&i.label<r[2]){i.label=r[2],i.ops.push(a);break}r[2]&&i.ops.pop(),i.trys.pop();continue}a=e.call(t,i)}catch(t){a=[6,t],n=0}finally{o=r=0}if(5&a[0])throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}([a,c])}}}function i(t,e,o){if(2===arguments.length)for(var n,r=0,i=e.length;r<i;r++)!n&&r in e||(n||(n=Array.prototype.slice.call(e,0,r)),n[r]=e[r]);return t.concat(n||Array.prototype.slice.call(e))}"function"==typeof SuppressedError&&SuppressedError,"function"==typeof SuppressedError&&SuppressedError;var s=function(){function t(t){var e,o;void 0===t&&(t={}),this._events=new Map,this._debug=null!==(e=t.debug)&&void 0!==e&&e,this._debugPrefix=null!==(o=t.debugPrefix)&&void 0!==o?o:"[SampleEventBus]"}return t.prototype.log=function(t){for(var e=[],o=1;o<arguments.length;o++)e[o-1]=arguments[o];this._debug&&console.log.apply(console,i(["".concat((new Date).toLocaleString(),":").concat(this._debugPrefix,": ").concat(t)],e,!1))},t.prototype.enableDebug=function(t){return this._debug=!0,t&&(this._debugPrefix=t),this.log("Debug mode enabled"),this},t.prototype.disableDebug=function(){return this.log("Debug mode disabled"),this._debug=!1,this},t.prototype.isDebugEnabled=function(){return this._debug},t.prototype.on=function(t,e){if("function"!=typeof e)throw new TypeError("Handler must be a function");var o=this._events.get(t);return o?Array.isArray(o)?(o.push(e),this.log("Added listener #".concat(o.length,' for "').concat(t,'"'))):(this._events.set(t,[o,e]),this.log('Converted to multi-listener for "'.concat(t,'", count: 2'))):(this._events.set(t,e),this.log('Registered first listener for "'.concat(t,'"'))),this},t.prototype.emit=function(t){for(var e=[],o=1;o<arguments.length;o++)e[o-1]=arguments[o];return n(this,0,void 0,(function(){var o,s,a=this;return r(this,(function(c){switch(c.label){case 0:return(o=this._events.get(t))?(s=Array.isArray(o)?o:[o],this.log.apply(this,i(['Emitting "'.concat(t,'" to ').concat(s.length," listener(s)")],e,!1)),[4,Promise.all(s.map((function(o,i){return n(a,0,void 0,(function(){var n;return r(this,(function(r){switch(r.label){case 0:return r.trys.push([0,2,,3]),this.log("Calling listener #".concat(i+1,' for "').concat(t,'"')),[4,o.apply(void 0,e)];case 1:return r.sent(),this.log("Listener #".concat(i+1,' for "').concat(t,'" completed')),[3,3];case 2:return n=r.sent(),console.error("".concat(this._debugPrefix," Error in listener #").concat(i+1,' for "').concat(t,'":'),n),[3,3];case 3:return[2]}}))}))})))]):(this.log('No listeners for "'.concat(t,'", skipping')),[2]);case 1:return c.sent(),this.log('All listeners for "'.concat(t,'" finished')),[2]}}))}))},t.prototype.off=function(t,e){var o=this._events.get(t);if(!o)return this.log('No listeners to remove for "'.concat(t,'"')),this;if(!e){var n=Array.isArray(o)?o.length:1;return this._events.delete(t),this.log("Removed all ".concat(n,' listener(s) for "').concat(t,'"')),this}if("function"==typeof o)o===e?(this._events.delete(t),this.log('Removed only listener for "'.concat(t,'"'))):this.log('Handler not found for "'.concat(t,'"'));else{var r=o.indexOf(e);r>-1?(o.splice(r,1),this.log("Removed listener #".concat(r+1,' for "').concat(t,'", ').concat(o.length," remaining")),0===o.length?(this._events.delete(t),this.log('No more listeners for "'.concat(t,'"'))):1===o.length&&(this._events.set(t,o[0]),this.log('Converted back to single listener for "'.concat(t,'"')))):this.log("Handler not found in ".concat(o.length,' listeners for "').concat(t,'"'))}return this},t.prototype.once=function(t,e){var o=this;if("function"!=typeof e)throw new TypeError("Handler must be a function");this.log('Registering once listener for "'.concat(t,'"'));var i=function(){for(var s=[],a=0;a<arguments.length;a++)s[a]=arguments[a];return n(o,0,void 0,(function(){return r(this,(function(o){switch(o.label){case 0:return this.log('Once listener triggered for "'.concat(t,'", removing')),this.off(t,i),[4,e.apply(void 0,s)];case 1:return o.sent(),[2]}}))}))};return this.on(t,i)},t.prototype.has=function(t){var e=this._events.has(t);return this.log('Checking "'.concat(t,'": ').concat(e?"has listeners":"no listeners")),e},t.prototype.listenerCount=function(t){var e=this._events.get(t),o=e?Array.isArray(e)?e.length:1:0;return this.log('Listener count for "'.concat(t,'": ').concat(o)),o},t.prototype.clear=function(){var t=this._events.size;this._events.clear(),this.log("Cleared all ".concat(t," event(s)"))},t}();function a(){return"".concat(Date.now(),"-").concat(Math.random().toString(36).slice(2,9))}var c=function(t){function o(e){void 0===e&&(e={});var o=t.call(this,{debug:e.debug,debugPrefix:e.debugPrefix||"[WindowMessageBus]"})||this;return o._sourceId=e.sourceId||"wmb-".concat(a().slice(0,8)),o._allowedOrigins=e.allowedOrigins||["*"],o._serialize=e.serialize||JSON.stringify,o._deserialize=e.deserialize||JSON.parse,o._messageHandler=o._handleMessage.bind(o),o._attachMessageListener(),o}return e(o,t),o.prototype._attachMessageListener=function(){"undefined"!=typeof window&&(window.addEventListener("message",this._messageHandler,!1),this.log("Attached to window message event"))},o.prototype._detachMessageListener=function(){"undefined"!=typeof window&&(window.removeEventListener("message",this._messageHandler,!1),this.log("Detached from window message event"))},o.prototype._isAllowedOrigin=function(t){return!!this._allowedOrigins.includes("*")||this._allowedOrigins.includes(t)},o.prototype._handleMessage=function(e){try{var o=e.data,n=e.origin,r=e.source,i=o;if("string"==typeof o)try{i=this._deserialize(o)}catch(t){return void this.log("Failed to deserialize message data:",t)}if(!i||"object"!=typeof i||!("type"in i))return;var s=i;if(r&&!this._isAllowedOrigin(n))return void this.log("Blocked message from disallowed origin: ".concat(n));if(s.sourceId===this._sourceId)return void this.log("Ignored own message: ".concat(s.type));this.log("Received message: ".concat(s.type," from ").concat(n),s.data),t.prototype.emit.call(this,s.type,s.data,{origin:n,source:r,messageId:s.messageId,sourceId:s.sourceId})}catch(t){this.log("Error handling message:",t)}},o.prototype._getTargetWindow=function(t){return"undefined"==typeof window?null:"parent"===t?window.parent:"self"===t?window:"top"===t?window.top:t},o.prototype.buildMessage=function(t,e){return{type:t,data:e,messageId:a(),sourceId:this._sourceId}},o.prototype.emitTo=function(t,e){for(var o=[],n=2;n<arguments.length;n++)o[n-2]=arguments[n];var r=this._getTargetWindow(t);if(r){var i=this.buildMessage(e,1===o.length?o[0]:o),s=this._serialize(i);try{r.postMessage(s,"*"),this.log("Sent message: ".concat(e," to ").concat(t),i)}catch(t){throw this.log("Failed to send message: ".concat(e),t),new Error("Failed to post message: ".concat(t))}}else this.log('Cannot send message: no target window for "'.concat(t,'"'))},o.prototype.emitToChildren=function(t){for(var e=[],o=1;o<arguments.length;o++)e[o-1]=arguments[o];if("undefined"!=typeof window&&window.frames)for(var n=this.buildMessage(t,1===e.length?e[0]:e),r=this._serialize(n),i=0;i<window.frames.length;i++){var s=window.frames[i];if(s&&s!==window)try{s.postMessage(r,"*"),this.log("Broadcasted message: ".concat(t," to frame[").concat(i,"]"),n)}catch(t){this.log("Failed to broadcast to frame[".concat(i,"]"),t)}}},o.prototype.getSourceId=function(){return this._sourceId},o.prototype.updateAllowedOrigins=function(t){this._allowedOrigins=t,this.log("Updated allowed origins: ".concat(t.join(", ")))},o.prototype.addAllowedOrigin=function(t){this._allowedOrigins.includes(t)||(this._allowedOrigins.push(t),this.log("Added allowed origin: ".concat(t)))},o.prototype.removeAllowedOrigin=function(t){var e=this._allowedOrigins.indexOf(t);e>-1&&(this._allowedOrigins.splice(e,1),this.log("Removed allowed origin: ".concat(t)))},o.prototype.destroy=function(){this._detachMessageListener(),this.clear(),this.log("WindowMessageBus destroyed")},o}(s);function l(t){return new c(t)}var u=function(t){function n(e){void 0===e&&(e={});var o=t.call(this,{debug:e.debug,sourceId:e.sourceId,allowedOrigins:e.allowedOrigins})||this;return"undefined"!=typeof window?o._defaultTarget=window.self!==window.top?"parent":"self":o._defaultTarget="self",o}return e(n,t),n.prototype.getDefaultTarget=function(){return this._defaultTarget},n.prototype.isReady=function(t,e){void 0===t&&(t={});var n=e||this._defaultTarget;this.emitTo(n,"isReady",o({sourceId:this.getSourceId(),timestamp:Date.now()},t))},n.prototype.onReady=function(t){return this._createUnsubscribe("onReady",t)},n.prototype.setData=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"setData",{data:t,timestamp:Date.now()})},n.prototype.onSetData=function(t){return this._createUnsubscribe("onSetData",(function(e){t(null==e?void 0:e.data)}))},n.prototype.setConfig=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"setConfig",{config:t,timestamp:Date.now()})},n.prototype.onSetConfig=function(t){return this._createUnsubscribe("onSetConfig",(function(e){t(null==e?void 0:e.config)}))},n.prototype.change=function(t,e){var n=e||this._defaultTarget;this.emitTo(n,"change",o(o({},t),{timestamp:t.timestamp||Date.now()}))},n.prototype.onChange=function(t){return this._createUnsubscribe("onChange",t)},n.prototype.save=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"save",{options:t,timestamp:Date.now()})},n.prototype.onSave=function(t){return this._createUnsubscribe("onSave",(function(e){t(null==e?void 0:e.options)}))},n.prototype.exportSVG=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"exportSVG",{options:t,timestamp:Date.now()})},n.prototype.onExportSVG=function(t){return this._createUnsubscribe("onExportSVG",(function(e){t(null==e?void 0:e.options)}))},n.prototype.exportPNG=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"exportPNG",{options:t,timestamp:Date.now()})},n.prototype.onExportPNG=function(t){return this._createUnsubscribe("onExportPNG",(function(e){t(null==e?void 0:e.options)}))},n.prototype.exportData=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"exportData",{options:t,timestamp:Date.now()})},n.prototype.onExportData=function(t){return this._createUnsubscribe("onExportData",(function(e){t(null==e?void 0:e.options)}))},n.prototype.getData=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"getData",{options:t,timestamp:Date.now()})},n.prototype.onGetData=function(t){return this._createUnsubscribe("onGetData",(function(e){t(null==e?void 0:e.options)}))},n.prototype.thumnail=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"thumnail",{options:t,timestamp:Date.now()})},n.prototype.onThumnail=function(t){return this._createUnsubscribe("onThumnail",(function(e){t(null==e?void 0:e.options)}))},n.prototype.preview=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"preview",{options:t,timestamp:Date.now()})},n.prototype.onPreview=function(t){return this._createUnsubscribe("onPreview",(function(e){t(null==e?void 0:e.options)}))},n.prototype._createUnsubscribe=function(t,e){var o=e;this.on(t,o);var n=!1,r=o;return this.on(t,(function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];if(!n)return r.apply(this,t)})),function(){n=!0}},n.prototype.destroy=function(){t.prototype.destroy.call(this),this.isDebugEnabled()&&console.log("".concat((new Date).toLocaleString(),":[EditorMessageBus]: EditorMessageBus destroyed"))},n}(c);function p(t){return new u(t)}export{u as EditorMessageBus,c as WindowMessageBus,p as createEditorMessageBus,l as createWindowMessageBus};
|
package/lib/index.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(t){"use strict";var e=function(t,o){return e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var o in e)Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o])},e(t,o)};function o(t,o){if("function"!=typeof o&&null!==o)throw new TypeError("Class extends value "+String(o)+" is not a constructor or null");function n(){this.constructor=t}e(t,o),t.prototype=null===o?Object.create(o):(n.prototype=o.prototype,new n)}var n=function(){return n=Object.assign||function(t){for(var e,o=1,n=arguments.length;o<n;o++)for(var r in e=arguments[o])Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r]);return t},n.apply(this,arguments)};function r(t,e,o,n){return new(o||(o=Promise))((function(e,r){function i(t){try{a(n.next(t))}catch(t){r(t)}}function s(t){try{a(n.throw(t))}catch(t){r(t)}}function a(t){var n;t.done?e(t.value):(n=t.value,n instanceof o?n:new o((function(t){t(n)}))).then(i,s)}a((n=n.apply(t,[])).next())}))}function i(t,e){var o,n,r,i={label:0,sent:function(){if(1&r[0])throw r[1];return r[1]},trys:[],ops:[]},s=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return s.next=a(0),s.throw=a(1),s.return=a(2),"function"==typeof Symbol&&(s[Symbol.iterator]=function(){return this}),s;function a(a){return function(c){return function(a){if(o)throw new TypeError("Generator is already executing.");for(;s&&(s=0,a[0]&&(i=0)),i;)try{if(o=1,n&&(r=2&a[0]?n.return:a[0]?n.throw||((r=n.return)&&r.call(n),0):n.next)&&!(r=r.call(n,a[1])).done)return r;switch(n=0,r&&(a=[2&a[0],r.value]),a[0]){case 0:case 1:r=a;break;case 4:return i.label++,{value:a[1],done:!1};case 5:i.label++,n=a[1],a=[0];continue;case 7:a=i.ops.pop(),i.trys.pop();continue;default:if(!((r=(r=i.trys).length>0&&r[r.length-1])||6!==a[0]&&2!==a[0])){i=0;continue}if(3===a[0]&&(!r||a[1]>r[0]&&a[1]<r[3])){i.label=a[1];break}if(6===a[0]&&i.label<r[1]){i.label=r[1],r=a;break}if(r&&i.label<r[2]){i.label=r[2],i.ops.push(a);break}r[2]&&i.ops.pop(),i.trys.pop();continue}a=e.call(t,i)}catch(t){a=[6,t],n=0}finally{o=r=0}if(5&a[0])throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}([a,c])}}}function s(t,e,o){if(2===arguments.length)for(var n,r=0,i=e.length;r<i;r++)!n&&r in e||(n||(n=Array.prototype.slice.call(e,0,r)),n[r]=e[r]);return t.concat(n||Array.prototype.slice.call(e))}"function"==typeof SuppressedError&&SuppressedError,"function"==typeof SuppressedError&&SuppressedError;var a=function(){function t(t){var e,o;void 0===t&&(t={}),this._events=new Map,this._debug=null!==(e=t.debug)&&void 0!==e&&e,this._debugPrefix=null!==(o=t.debugPrefix)&&void 0!==o?o:"[SampleEventBus]"}return t.prototype.log=function(t){for(var e=[],o=1;o<arguments.length;o++)e[o-1]=arguments[o];this._debug&&console.log.apply(console,s(["".concat((new Date).toLocaleString(),":").concat(this._debugPrefix,": ").concat(t)],e,!1))},t.prototype.enableDebug=function(t){return this._debug=!0,t&&(this._debugPrefix=t),this.log("Debug mode enabled"),this},t.prototype.disableDebug=function(){return this.log("Debug mode disabled"),this._debug=!1,this},t.prototype.isDebugEnabled=function(){return this._debug},t.prototype.on=function(t,e){if("function"!=typeof e)throw new TypeError("Handler must be a function");var o=this._events.get(t);return o?Array.isArray(o)?(o.push(e),this.log("Added listener #".concat(o.length,' for "').concat(t,'"'))):(this._events.set(t,[o,e]),this.log('Converted to multi-listener for "'.concat(t,'", count: 2'))):(this._events.set(t,e),this.log('Registered first listener for "'.concat(t,'"'))),this},t.prototype.emit=function(t){for(var e=[],o=1;o<arguments.length;o++)e[o-1]=arguments[o];return r(this,0,void 0,(function(){var o,n,a=this;return i(this,(function(c){switch(c.label){case 0:return(o=this._events.get(t))?(n=Array.isArray(o)?o:[o],this.log.apply(this,s(['Emitting "'.concat(t,'" to ').concat(n.length," listener(s)")],e,!1)),[4,Promise.all(n.map((function(o,n){return r(a,0,void 0,(function(){var r;return i(this,(function(i){switch(i.label){case 0:return i.trys.push([0,2,,3]),this.log("Calling listener #".concat(n+1,' for "').concat(t,'"')),[4,o.apply(void 0,e)];case 1:return i.sent(),this.log("Listener #".concat(n+1,' for "').concat(t,'" completed')),[3,3];case 2:return r=i.sent(),console.error("".concat(this._debugPrefix," Error in listener #").concat(n+1,' for "').concat(t,'":'),r),[3,3];case 3:return[2]}}))}))})))]):(this.log('No listeners for "'.concat(t,'", skipping')),[2]);case 1:return c.sent(),this.log('All listeners for "'.concat(t,'" finished')),[2]}}))}))},t.prototype.off=function(t,e){var o=this._events.get(t);if(!o)return this.log('No listeners to remove for "'.concat(t,'"')),this;if(!e){var n=Array.isArray(o)?o.length:1;return this._events.delete(t),this.log("Removed all ".concat(n,' listener(s) for "').concat(t,'"')),this}if("function"==typeof o)o===e?(this._events.delete(t),this.log('Removed only listener for "'.concat(t,'"'))):this.log('Handler not found for "'.concat(t,'"'));else{var r=o.indexOf(e);r>-1?(o.splice(r,1),this.log("Removed listener #".concat(r+1,' for "').concat(t,'", ').concat(o.length," remaining")),0===o.length?(this._events.delete(t),this.log('No more listeners for "'.concat(t,'"'))):1===o.length&&(this._events.set(t,o[0]),this.log('Converted back to single listener for "'.concat(t,'"')))):this.log("Handler not found in ".concat(o.length,' listeners for "').concat(t,'"'))}return this},t.prototype.once=function(t,e){var o=this;if("function"!=typeof e)throw new TypeError("Handler must be a function");this.log('Registering once listener for "'.concat(t,'"'));var n=function(){for(var s=[],a=0;a<arguments.length;a++)s[a]=arguments[a];return r(o,0,void 0,(function(){return i(this,(function(o){switch(o.label){case 0:return this.log('Once listener triggered for "'.concat(t,'", removing')),this.off(t,n),[4,e.apply(void 0,s)];case 1:return o.sent(),[2]}}))}))};return this.on(t,n)},t.prototype.has=function(t){var e=this._events.has(t);return this.log('Checking "'.concat(t,'": ').concat(e?"has listeners":"no listeners")),e},t.prototype.listenerCount=function(t){var e=this._events.get(t),o=e?Array.isArray(e)?e.length:1:0;return this.log('Listener count for "'.concat(t,'": ').concat(o)),o},t.prototype.clear=function(){var t=this._events.size;this._events.clear(),this.log("Cleared all ".concat(t," event(s)"))},t}();function c(){return"".concat(Date.now(),"-").concat(Math.random().toString(36).slice(2,9))}var l=function(t){function e(e){void 0===e&&(e={});var o=t.call(this,{debug:e.debug,debugPrefix:e.debugPrefix||"[WindowMessageBus]"})||this;return o._sourceId=e.sourceId||"wmb-".concat(c().slice(0,8)),o._allowedOrigins=e.allowedOrigins||["*"],o._serialize=e.serialize||JSON.stringify,o._deserialize=e.deserialize||JSON.parse,o._messageHandler=o._handleMessage.bind(o),o._attachMessageListener(),o}return o(e,t),e.prototype._attachMessageListener=function(){"undefined"!=typeof window&&(window.addEventListener("message",this._messageHandler,!1),this.log("Attached to window message event"))},e.prototype._detachMessageListener=function(){"undefined"!=typeof window&&(window.removeEventListener("message",this._messageHandler,!1),this.log("Detached from window message event"))},e.prototype._isAllowedOrigin=function(t){return!!this._allowedOrigins.includes("*")||this._allowedOrigins.includes(t)},e.prototype._handleMessage=function(e){try{var o=e.data,n=e.origin,r=e.source,i=o;if("string"==typeof o)try{i=this._deserialize(o)}catch(t){return void this.log("Failed to deserialize message data:",t)}if(!i||"object"!=typeof i||!("type"in i))return;var s=i;if(r&&!this._isAllowedOrigin(n))return void this.log("Blocked message from disallowed origin: ".concat(n));if(s.sourceId===this._sourceId)return void this.log("Ignored own message: ".concat(s.type));this.log("Received message: ".concat(s.type," from ").concat(n),s.data),t.prototype.emit.call(this,s.type,s.data,{origin:n,source:r,messageId:s.messageId,sourceId:s.sourceId})}catch(t){this.log("Error handling message:",t)}},e.prototype._getTargetWindow=function(t){return"undefined"==typeof window?null:"parent"===t?window.parent:"self"===t?window:"top"===t?window.top:t},e.prototype.buildMessage=function(t,e){return{type:t,data:e,messageId:c(),sourceId:this._sourceId}},e.prototype.emitTo=function(t,e){for(var o,n=[],r=2;r<arguments.length;r++)n[r-2]=arguments[r];var i=this._getTargetWindow(t);if(i){var s=this.buildMessage(e,1===n.length?n[0]:n),a=this._serialize(s);try{var c="*";"string"==typeof t&&("parent"===t&&window.parent.location?c=window.parent.location.origin:"top"===t&&(null===(o=window.top)||void 0===o?void 0:o.location)&&(c=window.top.location.origin)),i.postMessage(a,c),this.log("Sent message: ".concat(e," to ").concat(t),s)}catch(t){throw this.log("Failed to send message: ".concat(e),t),new Error("Failed to post message: ".concat(t))}}else this.log('Cannot send message: no target window for "'.concat(t,'"'))},e.prototype.emitToChildren=function(t){for(var e=[],o=1;o<arguments.length;o++)e[o-1]=arguments[o];if("undefined"!=typeof window&&window.frames)for(var n=this.buildMessage(t,1===e.length?e[0]:e),r=this._serialize(n),i=0;i<window.frames.length;i++){var s=window.frames[i];if(s&&s!==window)try{s.postMessage(r,"*"),this.log("Broadcasted message: ".concat(t," to frame[").concat(i,"]"),n)}catch(t){this.log("Failed to broadcast to frame[".concat(i,"]"),t)}}},e.prototype.getSourceId=function(){return this._sourceId},e.prototype.updateAllowedOrigins=function(t){this._allowedOrigins=t,this.log("Updated allowed origins: ".concat(t.join(", ")))},e.prototype.addAllowedOrigin=function(t){this._allowedOrigins.includes(t)||(this._allowedOrigins.push(t),this.log("Added allowed origin: ".concat(t)))},e.prototype.removeAllowedOrigin=function(t){var e=this._allowedOrigins.indexOf(t);e>-1&&(this._allowedOrigins.splice(e,1),this.log("Removed allowed origin: ".concat(t)))},e.prototype.destroy=function(){this._detachMessageListener(),this.clear(),this.log("WindowMessageBus destroyed")},e}(a);var u=function(t){function e(e){void 0===e&&(e={});var o=t.call(this,{debug:e.debug,sourceId:e.sourceId,allowedOrigins:e.allowedOrigins})||this;return"undefined"!=typeof window?o._defaultTarget=window.self!==window.top?"parent":"self":o._defaultTarget="self",o}return o(e,t),e.prototype.getDefaultTarget=function(){return this._defaultTarget},e.prototype.isReady=function(t,e){void 0===t&&(t={});var o=e||this._defaultTarget;this.emitTo(o,"isReady",n({sourceId:this.getSourceId(),timestamp:Date.now()},t))},e.prototype.onReady=function(t){return this._createUnsubscribe("onReady",t)},e.prototype.setData=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"setData",{data:t,timestamp:Date.now()})},e.prototype.onSetData=function(t){return this._createUnsubscribe("onSetData",(function(e){t(null==e?void 0:e.data)}))},e.prototype.setConfig=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"setConfig",{config:t,timestamp:Date.now()})},e.prototype.onSetConfig=function(t){return this._createUnsubscribe("onSetConfig",(function(e){t(null==e?void 0:e.config)}))},e.prototype.change=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"change",n(n({},t),{timestamp:t.timestamp||Date.now()}))},e.prototype.onChange=function(t){return this._createUnsubscribe("onChange",t)},e.prototype.save=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"save",{options:t,timestamp:Date.now()})},e.prototype.onSave=function(t){return this._createUnsubscribe("onSave",(function(e){t(null==e?void 0:e.options)}))},e.prototype.exportSVG=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"exportSVG",{options:t,timestamp:Date.now()})},e.prototype.onExportSVG=function(t){return this._createUnsubscribe("onExportSVG",(function(e){t(null==e?void 0:e.options)}))},e.prototype.exportPNG=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"exportPNG",{options:t,timestamp:Date.now()})},e.prototype.onExportPNG=function(t){return this._createUnsubscribe("onExportPNG",(function(e){t(null==e?void 0:e.options)}))},e.prototype.exportData=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"exportData",{options:t,timestamp:Date.now()})},e.prototype.onExportData=function(t){return this._createUnsubscribe("onExportData",(function(e){t(null==e?void 0:e.options)}))},e.prototype.getData=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"getData",{options:t,timestamp:Date.now()})},e.prototype.onGetData=function(t){return this._createUnsubscribe("onGetData",(function(e){t(null==e?void 0:e.options)}))},e.prototype.thumnail=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"thumnail",{options:t,timestamp:Date.now()})},e.prototype.onThumnail=function(t){return this._createUnsubscribe("onThumnail",(function(e){t(null==e?void 0:e.options)}))},e.prototype.preview=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"preview",{options:t,timestamp:Date.now()})},e.prototype.onPreview=function(t){return this._createUnsubscribe("onPreview",(function(e){t(null==e?void 0:e.options)}))},e.prototype._createUnsubscribe=function(t,e){var o=e;this.on(t,o);var n=!1,r=o;return this.on(t,(function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];if(!n)return r.apply(this,t)})),function(){n=!0}},e.prototype.destroy=function(){t.prototype.destroy.call(this),this.isDebugEnabled()&&console.log("".concat((new Date).toLocaleString(),":[EditorMessageBus]: EditorMessageBus destroyed"))},e}(l);t.EditorMessageBus=u,t.WindowMessageBus=l,t.createEditorMessageBus=function(t){return new u(t)},t.createWindowMessageBus=function(t){return new l(t)}}({});
|
|
1
|
+
!function(t){"use strict";var e=function(t,o){return e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var o in e)Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o])},e(t,o)};function o(t,o){if("function"!=typeof o&&null!==o)throw new TypeError("Class extends value "+String(o)+" is not a constructor or null");function n(){this.constructor=t}e(t,o),t.prototype=null===o?Object.create(o):(n.prototype=o.prototype,new n)}var n=function(){return n=Object.assign||function(t){for(var e,o=1,n=arguments.length;o<n;o++)for(var r in e=arguments[o])Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r]);return t},n.apply(this,arguments)};function r(t,e,o,n){return new(o||(o=Promise))((function(e,r){function i(t){try{a(n.next(t))}catch(t){r(t)}}function s(t){try{a(n.throw(t))}catch(t){r(t)}}function a(t){var n;t.done?e(t.value):(n=t.value,n instanceof o?n:new o((function(t){t(n)}))).then(i,s)}a((n=n.apply(t,[])).next())}))}function i(t,e){var o,n,r,i={label:0,sent:function(){if(1&r[0])throw r[1];return r[1]},trys:[],ops:[]},s=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return s.next=a(0),s.throw=a(1),s.return=a(2),"function"==typeof Symbol&&(s[Symbol.iterator]=function(){return this}),s;function a(a){return function(c){return function(a){if(o)throw new TypeError("Generator is already executing.");for(;s&&(s=0,a[0]&&(i=0)),i;)try{if(o=1,n&&(r=2&a[0]?n.return:a[0]?n.throw||((r=n.return)&&r.call(n),0):n.next)&&!(r=r.call(n,a[1])).done)return r;switch(n=0,r&&(a=[2&a[0],r.value]),a[0]){case 0:case 1:r=a;break;case 4:return i.label++,{value:a[1],done:!1};case 5:i.label++,n=a[1],a=[0];continue;case 7:a=i.ops.pop(),i.trys.pop();continue;default:if(!((r=(r=i.trys).length>0&&r[r.length-1])||6!==a[0]&&2!==a[0])){i=0;continue}if(3===a[0]&&(!r||a[1]>r[0]&&a[1]<r[3])){i.label=a[1];break}if(6===a[0]&&i.label<r[1]){i.label=r[1],r=a;break}if(r&&i.label<r[2]){i.label=r[2],i.ops.push(a);break}r[2]&&i.ops.pop(),i.trys.pop();continue}a=e.call(t,i)}catch(t){a=[6,t],n=0}finally{o=r=0}if(5&a[0])throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}([a,c])}}}function s(t,e,o){if(2===arguments.length)for(var n,r=0,i=e.length;r<i;r++)!n&&r in e||(n||(n=Array.prototype.slice.call(e,0,r)),n[r]=e[r]);return t.concat(n||Array.prototype.slice.call(e))}"function"==typeof SuppressedError&&SuppressedError,"function"==typeof SuppressedError&&SuppressedError;var a=function(){function t(t){var e,o;void 0===t&&(t={}),this._events=new Map,this._debug=null!==(e=t.debug)&&void 0!==e&&e,this._debugPrefix=null!==(o=t.debugPrefix)&&void 0!==o?o:"[SampleEventBus]"}return t.prototype.log=function(t){for(var e=[],o=1;o<arguments.length;o++)e[o-1]=arguments[o];this._debug&&console.log.apply(console,s(["".concat((new Date).toLocaleString(),":").concat(this._debugPrefix,": ").concat(t)],e,!1))},t.prototype.enableDebug=function(t){return this._debug=!0,t&&(this._debugPrefix=t),this.log("Debug mode enabled"),this},t.prototype.disableDebug=function(){return this.log("Debug mode disabled"),this._debug=!1,this},t.prototype.isDebugEnabled=function(){return this._debug},t.prototype.on=function(t,e){if("function"!=typeof e)throw new TypeError("Handler must be a function");var o=this._events.get(t);return o?Array.isArray(o)?(o.push(e),this.log("Added listener #".concat(o.length,' for "').concat(t,'"'))):(this._events.set(t,[o,e]),this.log('Converted to multi-listener for "'.concat(t,'", count: 2'))):(this._events.set(t,e),this.log('Registered first listener for "'.concat(t,'"'))),this},t.prototype.emit=function(t){for(var e=[],o=1;o<arguments.length;o++)e[o-1]=arguments[o];return r(this,0,void 0,(function(){var o,n,a=this;return i(this,(function(c){switch(c.label){case 0:return(o=this._events.get(t))?(n=Array.isArray(o)?o:[o],this.log.apply(this,s(['Emitting "'.concat(t,'" to ').concat(n.length," listener(s)")],e,!1)),[4,Promise.all(n.map((function(o,n){return r(a,0,void 0,(function(){var r;return i(this,(function(i){switch(i.label){case 0:return i.trys.push([0,2,,3]),this.log("Calling listener #".concat(n+1,' for "').concat(t,'"')),[4,o.apply(void 0,e)];case 1:return i.sent(),this.log("Listener #".concat(n+1,' for "').concat(t,'" completed')),[3,3];case 2:return r=i.sent(),console.error("".concat(this._debugPrefix," Error in listener #").concat(n+1,' for "').concat(t,'":'),r),[3,3];case 3:return[2]}}))}))})))]):(this.log('No listeners for "'.concat(t,'", skipping')),[2]);case 1:return c.sent(),this.log('All listeners for "'.concat(t,'" finished')),[2]}}))}))},t.prototype.off=function(t,e){var o=this._events.get(t);if(!o)return this.log('No listeners to remove for "'.concat(t,'"')),this;if(!e){var n=Array.isArray(o)?o.length:1;return this._events.delete(t),this.log("Removed all ".concat(n,' listener(s) for "').concat(t,'"')),this}if("function"==typeof o)o===e?(this._events.delete(t),this.log('Removed only listener for "'.concat(t,'"'))):this.log('Handler not found for "'.concat(t,'"'));else{var r=o.indexOf(e);r>-1?(o.splice(r,1),this.log("Removed listener #".concat(r+1,' for "').concat(t,'", ').concat(o.length," remaining")),0===o.length?(this._events.delete(t),this.log('No more listeners for "'.concat(t,'"'))):1===o.length&&(this._events.set(t,o[0]),this.log('Converted back to single listener for "'.concat(t,'"')))):this.log("Handler not found in ".concat(o.length,' listeners for "').concat(t,'"'))}return this},t.prototype.once=function(t,e){var o=this;if("function"!=typeof e)throw new TypeError("Handler must be a function");this.log('Registering once listener for "'.concat(t,'"'));var n=function(){for(var s=[],a=0;a<arguments.length;a++)s[a]=arguments[a];return r(o,0,void 0,(function(){return i(this,(function(o){switch(o.label){case 0:return this.log('Once listener triggered for "'.concat(t,'", removing')),this.off(t,n),[4,e.apply(void 0,s)];case 1:return o.sent(),[2]}}))}))};return this.on(t,n)},t.prototype.has=function(t){var e=this._events.has(t);return this.log('Checking "'.concat(t,'": ').concat(e?"has listeners":"no listeners")),e},t.prototype.listenerCount=function(t){var e=this._events.get(t),o=e?Array.isArray(e)?e.length:1:0;return this.log('Listener count for "'.concat(t,'": ').concat(o)),o},t.prototype.clear=function(){var t=this._events.size;this._events.clear(),this.log("Cleared all ".concat(t," event(s)"))},t}();function c(){return"".concat(Date.now(),"-").concat(Math.random().toString(36).slice(2,9))}var u=function(t){function e(e){void 0===e&&(e={});var o=t.call(this,{debug:e.debug,debugPrefix:e.debugPrefix||"[WindowMessageBus]"})||this;return o._sourceId=e.sourceId||"wmb-".concat(c().slice(0,8)),o._allowedOrigins=e.allowedOrigins||["*"],o._serialize=e.serialize||JSON.stringify,o._deserialize=e.deserialize||JSON.parse,o._messageHandler=o._handleMessage.bind(o),o._attachMessageListener(),o}return o(e,t),e.prototype._attachMessageListener=function(){"undefined"!=typeof window&&(window.addEventListener("message",this._messageHandler,!1),this.log("Attached to window message event"))},e.prototype._detachMessageListener=function(){"undefined"!=typeof window&&(window.removeEventListener("message",this._messageHandler,!1),this.log("Detached from window message event"))},e.prototype._isAllowedOrigin=function(t){return!!this._allowedOrigins.includes("*")||this._allowedOrigins.includes(t)},e.prototype._handleMessage=function(e){try{var o=e.data,n=e.origin,r=e.source,i=o;if("string"==typeof o)try{i=this._deserialize(o)}catch(t){return void this.log("Failed to deserialize message data:",t)}if(!i||"object"!=typeof i||!("type"in i))return;var s=i;if(r&&!this._isAllowedOrigin(n))return void this.log("Blocked message from disallowed origin: ".concat(n));if(s.sourceId===this._sourceId)return void this.log("Ignored own message: ".concat(s.type));this.log("Received message: ".concat(s.type," from ").concat(n),s.data),t.prototype.emit.call(this,s.type,s.data,{origin:n,source:r,messageId:s.messageId,sourceId:s.sourceId})}catch(t){this.log("Error handling message:",t)}},e.prototype._getTargetWindow=function(t){return"undefined"==typeof window?null:"parent"===t?window.parent:"self"===t?window:"top"===t?window.top:t},e.prototype.buildMessage=function(t,e){return{type:t,data:e,messageId:c(),sourceId:this._sourceId}},e.prototype.emitTo=function(t,e){for(var o=[],n=2;n<arguments.length;n++)o[n-2]=arguments[n];var r=this._getTargetWindow(t);if(r){var i=this.buildMessage(e,1===o.length?o[0]:o),s=this._serialize(i);try{r.postMessage(s,"*"),this.log("Sent message: ".concat(e," to ").concat(t),i)}catch(t){throw this.log("Failed to send message: ".concat(e),t),new Error("Failed to post message: ".concat(t))}}else this.log('Cannot send message: no target window for "'.concat(t,'"'))},e.prototype.emitToChildren=function(t){for(var e=[],o=1;o<arguments.length;o++)e[o-1]=arguments[o];if("undefined"!=typeof window&&window.frames)for(var n=this.buildMessage(t,1===e.length?e[0]:e),r=this._serialize(n),i=0;i<window.frames.length;i++){var s=window.frames[i];if(s&&s!==window)try{s.postMessage(r,"*"),this.log("Broadcasted message: ".concat(t," to frame[").concat(i,"]"),n)}catch(t){this.log("Failed to broadcast to frame[".concat(i,"]"),t)}}},e.prototype.getSourceId=function(){return this._sourceId},e.prototype.updateAllowedOrigins=function(t){this._allowedOrigins=t,this.log("Updated allowed origins: ".concat(t.join(", ")))},e.prototype.addAllowedOrigin=function(t){this._allowedOrigins.includes(t)||(this._allowedOrigins.push(t),this.log("Added allowed origin: ".concat(t)))},e.prototype.removeAllowedOrigin=function(t){var e=this._allowedOrigins.indexOf(t);e>-1&&(this._allowedOrigins.splice(e,1),this.log("Removed allowed origin: ".concat(t)))},e.prototype.destroy=function(){this._detachMessageListener(),this.clear(),this.log("WindowMessageBus destroyed")},e}(a);var l=function(t){function e(e){void 0===e&&(e={});var o=t.call(this,{debug:e.debug,sourceId:e.sourceId,allowedOrigins:e.allowedOrigins})||this;return"undefined"!=typeof window?o._defaultTarget=window.self!==window.top?"parent":"self":o._defaultTarget="self",o}return o(e,t),e.prototype.getDefaultTarget=function(){return this._defaultTarget},e.prototype.isReady=function(t,e){void 0===t&&(t={});var o=e||this._defaultTarget;this.emitTo(o,"isReady",n({sourceId:this.getSourceId(),timestamp:Date.now()},t))},e.prototype.onReady=function(t){return this._createUnsubscribe("onReady",t)},e.prototype.setData=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"setData",{data:t,timestamp:Date.now()})},e.prototype.onSetData=function(t){return this._createUnsubscribe("onSetData",(function(e){t(null==e?void 0:e.data)}))},e.prototype.setConfig=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"setConfig",{config:t,timestamp:Date.now()})},e.prototype.onSetConfig=function(t){return this._createUnsubscribe("onSetConfig",(function(e){t(null==e?void 0:e.config)}))},e.prototype.change=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"change",n(n({},t),{timestamp:t.timestamp||Date.now()}))},e.prototype.onChange=function(t){return this._createUnsubscribe("onChange",t)},e.prototype.save=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"save",{options:t,timestamp:Date.now()})},e.prototype.onSave=function(t){return this._createUnsubscribe("onSave",(function(e){t(null==e?void 0:e.options)}))},e.prototype.exportSVG=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"exportSVG",{options:t,timestamp:Date.now()})},e.prototype.onExportSVG=function(t){return this._createUnsubscribe("onExportSVG",(function(e){t(null==e?void 0:e.options)}))},e.prototype.exportPNG=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"exportPNG",{options:t,timestamp:Date.now()})},e.prototype.onExportPNG=function(t){return this._createUnsubscribe("onExportPNG",(function(e){t(null==e?void 0:e.options)}))},e.prototype.exportData=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"exportData",{options:t,timestamp:Date.now()})},e.prototype.onExportData=function(t){return this._createUnsubscribe("onExportData",(function(e){t(null==e?void 0:e.options)}))},e.prototype.getData=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"getData",{options:t,timestamp:Date.now()})},e.prototype.onGetData=function(t){return this._createUnsubscribe("onGetData",(function(e){t(null==e?void 0:e.options)}))},e.prototype.thumnail=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"thumnail",{options:t,timestamp:Date.now()})},e.prototype.onThumnail=function(t){return this._createUnsubscribe("onThumnail",(function(e){t(null==e?void 0:e.options)}))},e.prototype.preview=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"preview",{options:t,timestamp:Date.now()})},e.prototype.onPreview=function(t){return this._createUnsubscribe("onPreview",(function(e){t(null==e?void 0:e.options)}))},e.prototype._createUnsubscribe=function(t,e){var o=e;this.on(t,o);var n=!1,r=o;return this.on(t,(function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];if(!n)return r.apply(this,t)})),function(){n=!0}},e.prototype.destroy=function(){t.prototype.destroy.call(this),this.isDebugEnabled()&&console.log("".concat((new Date).toLocaleString(),":[EditorMessageBus]: EditorMessageBus destroyed"))},e}(u);t.EditorMessageBus=l,t.WindowMessageBus=u,t.createEditorMessageBus=function(t){return new l(t)},t.createWindowMessageBus=function(t){return new u(t)}}({});
|
package/lib/index.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).WindowMessageBus={})}(this,(function(t){"use strict";var e=function(t,o){return e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var o in e)Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o])},e(t,o)};function o(t,o){if("function"!=typeof o&&null!==o)throw new TypeError("Class extends value "+String(o)+" is not a constructor or null");function n(){this.constructor=t}e(t,o),t.prototype=null===o?Object.create(o):(n.prototype=o.prototype,new n)}var n=function(){return n=Object.assign||function(t){for(var e,o=1,n=arguments.length;o<n;o++)for(var i in e=arguments[o])Object.prototype.hasOwnProperty.call(e,i)&&(t[i]=e[i]);return t},n.apply(this,arguments)};function i(t,e,o,n){return new(o||(o=Promise))((function(e,i){function r(t){try{a(n.next(t))}catch(t){i(t)}}function s(t){try{a(n.throw(t))}catch(t){i(t)}}function a(t){var n;t.done?e(t.value):(n=t.value,n instanceof o?n:new o((function(t){t(n)}))).then(r,s)}a((n=n.apply(t,[])).next())}))}function r(t,e){var o,n,i,r={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]},s=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return s.next=a(0),s.throw=a(1),s.return=a(2),"function"==typeof Symbol&&(s[Symbol.iterator]=function(){return this}),s;function a(a){return function(c){return function(a){if(o)throw new TypeError("Generator is already executing.");for(;s&&(s=0,a[0]&&(r=0)),r;)try{if(o=1,n&&(i=2&a[0]?n.return:a[0]?n.throw||((i=n.return)&&i.call(n),0):n.next)&&!(i=i.call(n,a[1])).done)return i;switch(n=0,i&&(a=[2&a[0],i.value]),a[0]){case 0:case 1:i=a;break;case 4:return r.label++,{value:a[1],done:!1};case 5:r.label++,n=a[1],a=[0];continue;case 7:a=r.ops.pop(),r.trys.pop();continue;default:if(!((i=(i=r.trys).length>0&&i[i.length-1])||6!==a[0]&&2!==a[0])){r=0;continue}if(3===a[0]&&(!i||a[1]>i[0]&&a[1]<i[3])){r.label=a[1];break}if(6===a[0]&&r.label<i[1]){r.label=i[1],i=a;break}if(i&&r.label<i[2]){r.label=i[2],r.ops.push(a);break}i[2]&&r.ops.pop(),r.trys.pop();continue}a=e.call(t,r)}catch(t){a=[6,t],n=0}finally{o=i=0}if(5&a[0])throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}([a,c])}}}function s(t,e,o){if(2===arguments.length)for(var n,i=0,r=e.length;i<r;i++)!n&&i in e||(n||(n=Array.prototype.slice.call(e,0,i)),n[i]=e[i]);return t.concat(n||Array.prototype.slice.call(e))}"function"==typeof SuppressedError&&SuppressedError,"function"==typeof SuppressedError&&SuppressedError;var a=function(){function t(t){var e,o;void 0===t&&(t={}),this._events=new Map,this._debug=null!==(e=t.debug)&&void 0!==e&&e,this._debugPrefix=null!==(o=t.debugPrefix)&&void 0!==o?o:"[SampleEventBus]"}return t.prototype.log=function(t){for(var e=[],o=1;o<arguments.length;o++)e[o-1]=arguments[o];this._debug&&console.log.apply(console,s(["".concat((new Date).toLocaleString(),":").concat(this._debugPrefix,": ").concat(t)],e,!1))},t.prototype.enableDebug=function(t){return this._debug=!0,t&&(this._debugPrefix=t),this.log("Debug mode enabled"),this},t.prototype.disableDebug=function(){return this.log("Debug mode disabled"),this._debug=!1,this},t.prototype.isDebugEnabled=function(){return this._debug},t.prototype.on=function(t,e){if("function"!=typeof e)throw new TypeError("Handler must be a function");var o=this._events.get(t);return o?Array.isArray(o)?(o.push(e),this.log("Added listener #".concat(o.length,' for "').concat(t,'"'))):(this._events.set(t,[o,e]),this.log('Converted to multi-listener for "'.concat(t,'", count: 2'))):(this._events.set(t,e),this.log('Registered first listener for "'.concat(t,'"'))),this},t.prototype.emit=function(t){for(var e=[],o=1;o<arguments.length;o++)e[o-1]=arguments[o];return i(this,0,void 0,(function(){var o,n,a=this;return r(this,(function(c){switch(c.label){case 0:return(o=this._events.get(t))?(n=Array.isArray(o)?o:[o],this.log.apply(this,s(['Emitting "'.concat(t,'" to ').concat(n.length," listener(s)")],e,!1)),[4,Promise.all(n.map((function(o,n){return i(a,0,void 0,(function(){var i;return r(this,(function(r){switch(r.label){case 0:return r.trys.push([0,2,,3]),this.log("Calling listener #".concat(n+1,' for "').concat(t,'"')),[4,o.apply(void 0,e)];case 1:return r.sent(),this.log("Listener #".concat(n+1,' for "').concat(t,'" completed')),[3,3];case 2:return i=r.sent(),console.error("".concat(this._debugPrefix," Error in listener #").concat(n+1,' for "').concat(t,'":'),i),[3,3];case 3:return[2]}}))}))})))]):(this.log('No listeners for "'.concat(t,'", skipping')),[2]);case 1:return c.sent(),this.log('All listeners for "'.concat(t,'" finished')),[2]}}))}))},t.prototype.off=function(t,e){var o=this._events.get(t);if(!o)return this.log('No listeners to remove for "'.concat(t,'"')),this;if(!e){var n=Array.isArray(o)?o.length:1;return this._events.delete(t),this.log("Removed all ".concat(n,' listener(s) for "').concat(t,'"')),this}if("function"==typeof o)o===e?(this._events.delete(t),this.log('Removed only listener for "'.concat(t,'"'))):this.log('Handler not found for "'.concat(t,'"'));else{var i=o.indexOf(e);i>-1?(o.splice(i,1),this.log("Removed listener #".concat(i+1,' for "').concat(t,'", ').concat(o.length," remaining")),0===o.length?(this._events.delete(t),this.log('No more listeners for "'.concat(t,'"'))):1===o.length&&(this._events.set(t,o[0]),this.log('Converted back to single listener for "'.concat(t,'"')))):this.log("Handler not found in ".concat(o.length,' listeners for "').concat(t,'"'))}return this},t.prototype.once=function(t,e){var o=this;if("function"!=typeof e)throw new TypeError("Handler must be a function");this.log('Registering once listener for "'.concat(t,'"'));var n=function(){for(var s=[],a=0;a<arguments.length;a++)s[a]=arguments[a];return i(o,0,void 0,(function(){return r(this,(function(o){switch(o.label){case 0:return this.log('Once listener triggered for "'.concat(t,'", removing')),this.off(t,n),[4,e.apply(void 0,s)];case 1:return o.sent(),[2]}}))}))};return this.on(t,n)},t.prototype.has=function(t){var e=this._events.has(t);return this.log('Checking "'.concat(t,'": ').concat(e?"has listeners":"no listeners")),e},t.prototype.listenerCount=function(t){var e=this._events.get(t),o=e?Array.isArray(e)?e.length:1:0;return this.log('Listener count for "'.concat(t,'": ').concat(o)),o},t.prototype.clear=function(){var t=this._events.size;this._events.clear(),this.log("Cleared all ".concat(t," event(s)"))},t}();function c(){return"".concat(Date.now(),"-").concat(Math.random().toString(36).slice(2,9))}var l=function(t){function e(e){void 0===e&&(e={});var o=t.call(this,{debug:e.debug,debugPrefix:e.debugPrefix||"[WindowMessageBus]"})||this;return o._sourceId=e.sourceId||"wmb-".concat(c().slice(0,8)),o._allowedOrigins=e.allowedOrigins||["*"],o._serialize=e.serialize||JSON.stringify,o._deserialize=e.deserialize||JSON.parse,o._messageHandler=o._handleMessage.bind(o),o._attachMessageListener(),o}return o(e,t),e.prototype._attachMessageListener=function(){"undefined"!=typeof window&&(window.addEventListener("message",this._messageHandler,!1),this.log("Attached to window message event"))},e.prototype._detachMessageListener=function(){"undefined"!=typeof window&&(window.removeEventListener("message",this._messageHandler,!1),this.log("Detached from window message event"))},e.prototype._isAllowedOrigin=function(t){return!!this._allowedOrigins.includes("*")||this._allowedOrigins.includes(t)},e.prototype._handleMessage=function(e){try{var o=e.data,n=e.origin,i=e.source,r=o;if("string"==typeof o)try{r=this._deserialize(o)}catch(t){return void this.log("Failed to deserialize message data:",t)}if(!r||"object"!=typeof r||!("type"in r))return;var s=r;if(i&&!this._isAllowedOrigin(n))return void this.log("Blocked message from disallowed origin: ".concat(n));if(s.sourceId===this._sourceId)return void this.log("Ignored own message: ".concat(s.type));this.log("Received message: ".concat(s.type," from ").concat(n),s.data),t.prototype.emit.call(this,s.type,s.data,{origin:n,source:i,messageId:s.messageId,sourceId:s.sourceId})}catch(t){this.log("Error handling message:",t)}},e.prototype._getTargetWindow=function(t){return"undefined"==typeof window?null:"parent"===t?window.parent:"self"===t?window:"top"===t?window.top:t},e.prototype.buildMessage=function(t,e){return{type:t,data:e,messageId:c(),sourceId:this._sourceId}},e.prototype.emitTo=function(t,e){for(var o,n=[],i=2;i<arguments.length;i++)n[i-2]=arguments[i];var r=this._getTargetWindow(t);if(r){var s=this.buildMessage(e,1===n.length?n[0]:n),a=this._serialize(s);try{var c="*";"string"==typeof t&&("parent"===t&&window.parent.location?c=window.parent.location.origin:"top"===t&&(null===(o=window.top)||void 0===o?void 0:o.location)&&(c=window.top.location.origin)),r.postMessage(a,c),this.log("Sent message: ".concat(e," to ").concat(t),s)}catch(t){throw this.log("Failed to send message: ".concat(e),t),new Error("Failed to post message: ".concat(t))}}else this.log('Cannot send message: no target window for "'.concat(t,'"'))},e.prototype.emitToChildren=function(t){for(var e=[],o=1;o<arguments.length;o++)e[o-1]=arguments[o];if("undefined"!=typeof window&&window.frames)for(var n=this.buildMessage(t,1===e.length?e[0]:e),i=this._serialize(n),r=0;r<window.frames.length;r++){var s=window.frames[r];if(s&&s!==window)try{s.postMessage(i,"*"),this.log("Broadcasted message: ".concat(t," to frame[").concat(r,"]"),n)}catch(t){this.log("Failed to broadcast to frame[".concat(r,"]"),t)}}},e.prototype.getSourceId=function(){return this._sourceId},e.prototype.updateAllowedOrigins=function(t){this._allowedOrigins=t,this.log("Updated allowed origins: ".concat(t.join(", ")))},e.prototype.addAllowedOrigin=function(t){this._allowedOrigins.includes(t)||(this._allowedOrigins.push(t),this.log("Added allowed origin: ".concat(t)))},e.prototype.removeAllowedOrigin=function(t){var e=this._allowedOrigins.indexOf(t);e>-1&&(this._allowedOrigins.splice(e,1),this.log("Removed allowed origin: ".concat(t)))},e.prototype.destroy=function(){this._detachMessageListener(),this.clear(),this.log("WindowMessageBus destroyed")},e}(a);var u=function(t){function e(e){void 0===e&&(e={});var o=t.call(this,{debug:e.debug,sourceId:e.sourceId,allowedOrigins:e.allowedOrigins})||this;return"undefined"!=typeof window?o._defaultTarget=window.self!==window.top?"parent":"self":o._defaultTarget="self",o}return o(e,t),e.prototype.getDefaultTarget=function(){return this._defaultTarget},e.prototype.isReady=function(t,e){void 0===t&&(t={});var o=e||this._defaultTarget;this.emitTo(o,"isReady",n({sourceId:this.getSourceId(),timestamp:Date.now()},t))},e.prototype.onReady=function(t){return this._createUnsubscribe("onReady",t)},e.prototype.setData=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"setData",{data:t,timestamp:Date.now()})},e.prototype.onSetData=function(t){return this._createUnsubscribe("onSetData",(function(e){t(null==e?void 0:e.data)}))},e.prototype.setConfig=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"setConfig",{config:t,timestamp:Date.now()})},e.prototype.onSetConfig=function(t){return this._createUnsubscribe("onSetConfig",(function(e){t(null==e?void 0:e.config)}))},e.prototype.change=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"change",n(n({},t),{timestamp:t.timestamp||Date.now()}))},e.prototype.onChange=function(t){return this._createUnsubscribe("onChange",t)},e.prototype.save=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"save",{options:t,timestamp:Date.now()})},e.prototype.onSave=function(t){return this._createUnsubscribe("onSave",(function(e){t(null==e?void 0:e.options)}))},e.prototype.exportSVG=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"exportSVG",{options:t,timestamp:Date.now()})},e.prototype.onExportSVG=function(t){return this._createUnsubscribe("onExportSVG",(function(e){t(null==e?void 0:e.options)}))},e.prototype.exportPNG=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"exportPNG",{options:t,timestamp:Date.now()})},e.prototype.onExportPNG=function(t){return this._createUnsubscribe("onExportPNG",(function(e){t(null==e?void 0:e.options)}))},e.prototype.exportData=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"exportData",{options:t,timestamp:Date.now()})},e.prototype.onExportData=function(t){return this._createUnsubscribe("onExportData",(function(e){t(null==e?void 0:e.options)}))},e.prototype.getData=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"getData",{options:t,timestamp:Date.now()})},e.prototype.onGetData=function(t){return this._createUnsubscribe("onGetData",(function(e){t(null==e?void 0:e.options)}))},e.prototype.thumnail=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"thumnail",{options:t,timestamp:Date.now()})},e.prototype.onThumnail=function(t){return this._createUnsubscribe("onThumnail",(function(e){t(null==e?void 0:e.options)}))},e.prototype.preview=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"preview",{options:t,timestamp:Date.now()})},e.prototype.onPreview=function(t){return this._createUnsubscribe("onPreview",(function(e){t(null==e?void 0:e.options)}))},e.prototype._createUnsubscribe=function(t,e){var o=e;this.on(t,o);var n=!1,i=o;return this.on(t,(function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];if(!n)return i.apply(this,t)})),function(){n=!0}},e.prototype.destroy=function(){t.prototype.destroy.call(this),this.isDebugEnabled()&&console.log("".concat((new Date).toLocaleString(),":[EditorMessageBus]: EditorMessageBus destroyed"))},e}(l);t.EditorMessageBus=u,t.WindowMessageBus=l,t.createEditorMessageBus=function(t){return new u(t)},t.createWindowMessageBus=function(t){return new l(t)}}));
|
|
1
|
+
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).WindowMessageBus={})}(this,(function(t){"use strict";var e=function(t,o){return e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var o in e)Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o])},e(t,o)};function o(t,o){if("function"!=typeof o&&null!==o)throw new TypeError("Class extends value "+String(o)+" is not a constructor or null");function n(){this.constructor=t}e(t,o),t.prototype=null===o?Object.create(o):(n.prototype=o.prototype,new n)}var n=function(){return n=Object.assign||function(t){for(var e,o=1,n=arguments.length;o<n;o++)for(var r in e=arguments[o])Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r]);return t},n.apply(this,arguments)};function r(t,e,o,n){return new(o||(o=Promise))((function(e,r){function i(t){try{a(n.next(t))}catch(t){r(t)}}function s(t){try{a(n.throw(t))}catch(t){r(t)}}function a(t){var n;t.done?e(t.value):(n=t.value,n instanceof o?n:new o((function(t){t(n)}))).then(i,s)}a((n=n.apply(t,[])).next())}))}function i(t,e){var o,n,r,i={label:0,sent:function(){if(1&r[0])throw r[1];return r[1]},trys:[],ops:[]},s=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return s.next=a(0),s.throw=a(1),s.return=a(2),"function"==typeof Symbol&&(s[Symbol.iterator]=function(){return this}),s;function a(a){return function(c){return function(a){if(o)throw new TypeError("Generator is already executing.");for(;s&&(s=0,a[0]&&(i=0)),i;)try{if(o=1,n&&(r=2&a[0]?n.return:a[0]?n.throw||((r=n.return)&&r.call(n),0):n.next)&&!(r=r.call(n,a[1])).done)return r;switch(n=0,r&&(a=[2&a[0],r.value]),a[0]){case 0:case 1:r=a;break;case 4:return i.label++,{value:a[1],done:!1};case 5:i.label++,n=a[1],a=[0];continue;case 7:a=i.ops.pop(),i.trys.pop();continue;default:if(!((r=(r=i.trys).length>0&&r[r.length-1])||6!==a[0]&&2!==a[0])){i=0;continue}if(3===a[0]&&(!r||a[1]>r[0]&&a[1]<r[3])){i.label=a[1];break}if(6===a[0]&&i.label<r[1]){i.label=r[1],r=a;break}if(r&&i.label<r[2]){i.label=r[2],i.ops.push(a);break}r[2]&&i.ops.pop(),i.trys.pop();continue}a=e.call(t,i)}catch(t){a=[6,t],n=0}finally{o=r=0}if(5&a[0])throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}([a,c])}}}function s(t,e,o){if(2===arguments.length)for(var n,r=0,i=e.length;r<i;r++)!n&&r in e||(n||(n=Array.prototype.slice.call(e,0,r)),n[r]=e[r]);return t.concat(n||Array.prototype.slice.call(e))}"function"==typeof SuppressedError&&SuppressedError,"function"==typeof SuppressedError&&SuppressedError;var a=function(){function t(t){var e,o;void 0===t&&(t={}),this._events=new Map,this._debug=null!==(e=t.debug)&&void 0!==e&&e,this._debugPrefix=null!==(o=t.debugPrefix)&&void 0!==o?o:"[SampleEventBus]"}return t.prototype.log=function(t){for(var e=[],o=1;o<arguments.length;o++)e[o-1]=arguments[o];this._debug&&console.log.apply(console,s(["".concat((new Date).toLocaleString(),":").concat(this._debugPrefix,": ").concat(t)],e,!1))},t.prototype.enableDebug=function(t){return this._debug=!0,t&&(this._debugPrefix=t),this.log("Debug mode enabled"),this},t.prototype.disableDebug=function(){return this.log("Debug mode disabled"),this._debug=!1,this},t.prototype.isDebugEnabled=function(){return this._debug},t.prototype.on=function(t,e){if("function"!=typeof e)throw new TypeError("Handler must be a function");var o=this._events.get(t);return o?Array.isArray(o)?(o.push(e),this.log("Added listener #".concat(o.length,' for "').concat(t,'"'))):(this._events.set(t,[o,e]),this.log('Converted to multi-listener for "'.concat(t,'", count: 2'))):(this._events.set(t,e),this.log('Registered first listener for "'.concat(t,'"'))),this},t.prototype.emit=function(t){for(var e=[],o=1;o<arguments.length;o++)e[o-1]=arguments[o];return r(this,0,void 0,(function(){var o,n,a=this;return i(this,(function(c){switch(c.label){case 0:return(o=this._events.get(t))?(n=Array.isArray(o)?o:[o],this.log.apply(this,s(['Emitting "'.concat(t,'" to ').concat(n.length," listener(s)")],e,!1)),[4,Promise.all(n.map((function(o,n){return r(a,0,void 0,(function(){var r;return i(this,(function(i){switch(i.label){case 0:return i.trys.push([0,2,,3]),this.log("Calling listener #".concat(n+1,' for "').concat(t,'"')),[4,o.apply(void 0,e)];case 1:return i.sent(),this.log("Listener #".concat(n+1,' for "').concat(t,'" completed')),[3,3];case 2:return r=i.sent(),console.error("".concat(this._debugPrefix," Error in listener #").concat(n+1,' for "').concat(t,'":'),r),[3,3];case 3:return[2]}}))}))})))]):(this.log('No listeners for "'.concat(t,'", skipping')),[2]);case 1:return c.sent(),this.log('All listeners for "'.concat(t,'" finished')),[2]}}))}))},t.prototype.off=function(t,e){var o=this._events.get(t);if(!o)return this.log('No listeners to remove for "'.concat(t,'"')),this;if(!e){var n=Array.isArray(o)?o.length:1;return this._events.delete(t),this.log("Removed all ".concat(n,' listener(s) for "').concat(t,'"')),this}if("function"==typeof o)o===e?(this._events.delete(t),this.log('Removed only listener for "'.concat(t,'"'))):this.log('Handler not found for "'.concat(t,'"'));else{var r=o.indexOf(e);r>-1?(o.splice(r,1),this.log("Removed listener #".concat(r+1,' for "').concat(t,'", ').concat(o.length," remaining")),0===o.length?(this._events.delete(t),this.log('No more listeners for "'.concat(t,'"'))):1===o.length&&(this._events.set(t,o[0]),this.log('Converted back to single listener for "'.concat(t,'"')))):this.log("Handler not found in ".concat(o.length,' listeners for "').concat(t,'"'))}return this},t.prototype.once=function(t,e){var o=this;if("function"!=typeof e)throw new TypeError("Handler must be a function");this.log('Registering once listener for "'.concat(t,'"'));var n=function(){for(var s=[],a=0;a<arguments.length;a++)s[a]=arguments[a];return r(o,0,void 0,(function(){return i(this,(function(o){switch(o.label){case 0:return this.log('Once listener triggered for "'.concat(t,'", removing')),this.off(t,n),[4,e.apply(void 0,s)];case 1:return o.sent(),[2]}}))}))};return this.on(t,n)},t.prototype.has=function(t){var e=this._events.has(t);return this.log('Checking "'.concat(t,'": ').concat(e?"has listeners":"no listeners")),e},t.prototype.listenerCount=function(t){var e=this._events.get(t),o=e?Array.isArray(e)?e.length:1:0;return this.log('Listener count for "'.concat(t,'": ').concat(o)),o},t.prototype.clear=function(){var t=this._events.size;this._events.clear(),this.log("Cleared all ".concat(t," event(s)"))},t}();function c(){return"".concat(Date.now(),"-").concat(Math.random().toString(36).slice(2,9))}var u=function(t){function e(e){void 0===e&&(e={});var o=t.call(this,{debug:e.debug,debugPrefix:e.debugPrefix||"[WindowMessageBus]"})||this;return o._sourceId=e.sourceId||"wmb-".concat(c().slice(0,8)),o._allowedOrigins=e.allowedOrigins||["*"],o._serialize=e.serialize||JSON.stringify,o._deserialize=e.deserialize||JSON.parse,o._messageHandler=o._handleMessage.bind(o),o._attachMessageListener(),o}return o(e,t),e.prototype._attachMessageListener=function(){"undefined"!=typeof window&&(window.addEventListener("message",this._messageHandler,!1),this.log("Attached to window message event"))},e.prototype._detachMessageListener=function(){"undefined"!=typeof window&&(window.removeEventListener("message",this._messageHandler,!1),this.log("Detached from window message event"))},e.prototype._isAllowedOrigin=function(t){return!!this._allowedOrigins.includes("*")||this._allowedOrigins.includes(t)},e.prototype._handleMessage=function(e){try{var o=e.data,n=e.origin,r=e.source,i=o;if("string"==typeof o)try{i=this._deserialize(o)}catch(t){return void this.log("Failed to deserialize message data:",t)}if(!i||"object"!=typeof i||!("type"in i))return;var s=i;if(r&&!this._isAllowedOrigin(n))return void this.log("Blocked message from disallowed origin: ".concat(n));if(s.sourceId===this._sourceId)return void this.log("Ignored own message: ".concat(s.type));this.log("Received message: ".concat(s.type," from ").concat(n),s.data),t.prototype.emit.call(this,s.type,s.data,{origin:n,source:r,messageId:s.messageId,sourceId:s.sourceId})}catch(t){this.log("Error handling message:",t)}},e.prototype._getTargetWindow=function(t){return"undefined"==typeof window?null:"parent"===t?window.parent:"self"===t?window:"top"===t?window.top:t},e.prototype.buildMessage=function(t,e){return{type:t,data:e,messageId:c(),sourceId:this._sourceId}},e.prototype.emitTo=function(t,e){for(var o=[],n=2;n<arguments.length;n++)o[n-2]=arguments[n];var r=this._getTargetWindow(t);if(r){var i=this.buildMessage(e,1===o.length?o[0]:o),s=this._serialize(i);try{r.postMessage(s,"*"),this.log("Sent message: ".concat(e," to ").concat(t),i)}catch(t){throw this.log("Failed to send message: ".concat(e),t),new Error("Failed to post message: ".concat(t))}}else this.log('Cannot send message: no target window for "'.concat(t,'"'))},e.prototype.emitToChildren=function(t){for(var e=[],o=1;o<arguments.length;o++)e[o-1]=arguments[o];if("undefined"!=typeof window&&window.frames)for(var n=this.buildMessage(t,1===e.length?e[0]:e),r=this._serialize(n),i=0;i<window.frames.length;i++){var s=window.frames[i];if(s&&s!==window)try{s.postMessage(r,"*"),this.log("Broadcasted message: ".concat(t," to frame[").concat(i,"]"),n)}catch(t){this.log("Failed to broadcast to frame[".concat(i,"]"),t)}}},e.prototype.getSourceId=function(){return this._sourceId},e.prototype.updateAllowedOrigins=function(t){this._allowedOrigins=t,this.log("Updated allowed origins: ".concat(t.join(", ")))},e.prototype.addAllowedOrigin=function(t){this._allowedOrigins.includes(t)||(this._allowedOrigins.push(t),this.log("Added allowed origin: ".concat(t)))},e.prototype.removeAllowedOrigin=function(t){var e=this._allowedOrigins.indexOf(t);e>-1&&(this._allowedOrigins.splice(e,1),this.log("Removed allowed origin: ".concat(t)))},e.prototype.destroy=function(){this._detachMessageListener(),this.clear(),this.log("WindowMessageBus destroyed")},e}(a);var l=function(t){function e(e){void 0===e&&(e={});var o=t.call(this,{debug:e.debug,sourceId:e.sourceId,allowedOrigins:e.allowedOrigins})||this;return"undefined"!=typeof window?o._defaultTarget=window.self!==window.top?"parent":"self":o._defaultTarget="self",o}return o(e,t),e.prototype.getDefaultTarget=function(){return this._defaultTarget},e.prototype.isReady=function(t,e){void 0===t&&(t={});var o=e||this._defaultTarget;this.emitTo(o,"isReady",n({sourceId:this.getSourceId(),timestamp:Date.now()},t))},e.prototype.onReady=function(t){return this._createUnsubscribe("onReady",t)},e.prototype.setData=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"setData",{data:t,timestamp:Date.now()})},e.prototype.onSetData=function(t){return this._createUnsubscribe("onSetData",(function(e){t(null==e?void 0:e.data)}))},e.prototype.setConfig=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"setConfig",{config:t,timestamp:Date.now()})},e.prototype.onSetConfig=function(t){return this._createUnsubscribe("onSetConfig",(function(e){t(null==e?void 0:e.config)}))},e.prototype.change=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"change",n(n({},t),{timestamp:t.timestamp||Date.now()}))},e.prototype.onChange=function(t){return this._createUnsubscribe("onChange",t)},e.prototype.save=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"save",{options:t,timestamp:Date.now()})},e.prototype.onSave=function(t){return this._createUnsubscribe("onSave",(function(e){t(null==e?void 0:e.options)}))},e.prototype.exportSVG=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"exportSVG",{options:t,timestamp:Date.now()})},e.prototype.onExportSVG=function(t){return this._createUnsubscribe("onExportSVG",(function(e){t(null==e?void 0:e.options)}))},e.prototype.exportPNG=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"exportPNG",{options:t,timestamp:Date.now()})},e.prototype.onExportPNG=function(t){return this._createUnsubscribe("onExportPNG",(function(e){t(null==e?void 0:e.options)}))},e.prototype.exportData=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"exportData",{options:t,timestamp:Date.now()})},e.prototype.onExportData=function(t){return this._createUnsubscribe("onExportData",(function(e){t(null==e?void 0:e.options)}))},e.prototype.getData=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"getData",{options:t,timestamp:Date.now()})},e.prototype.onGetData=function(t){return this._createUnsubscribe("onGetData",(function(e){t(null==e?void 0:e.options)}))},e.prototype.thumnail=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"thumnail",{options:t,timestamp:Date.now()})},e.prototype.onThumnail=function(t){return this._createUnsubscribe("onThumnail",(function(e){t(null==e?void 0:e.options)}))},e.prototype.preview=function(t,e){var o=e||this._defaultTarget;this.emitTo(o,"preview",{options:t,timestamp:Date.now()})},e.prototype.onPreview=function(t){return this._createUnsubscribe("onPreview",(function(e){t(null==e?void 0:e.options)}))},e.prototype._createUnsubscribe=function(t,e){var o=e;this.on(t,o);var n=!1,r=o;return this.on(t,(function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];if(!n)return r.apply(this,t)})),function(){n=!0}},e.prototype.destroy=function(){t.prototype.destroy.call(this),this.isDebugEnabled()&&console.log("".concat((new Date).toLocaleString(),":[EditorMessageBus]: EditorMessageBus destroyed"))},e}(u);t.EditorMessageBus=l,t.WindowMessageBus=u,t.createEditorMessageBus=function(t){return new l(t)},t.createWindowMessageBus=function(t){return new u(t)}}));
|