diff --git a/editor/extensions/ext-imagelib.js b/editor/extensions/ext-imagelib.js index f1f97d58..bfdae794 100644 --- a/editor/extensions/ext-imagelib.js +++ b/editor/extensions/ext-imagelib.js @@ -75,8 +75,38 @@ export default { let transferStopped = false; let preview, submit; - // Receive `postMessage` data - window.addEventListener('message', async function ({origin, data: response}) { // eslint-disable-line no-shadow + /** + * @typedef {PlainObject} ImageLibMessage + * @property {"imagelib"} namespace Required to distinguish from any other messages of app. + * @property {string} href Set to same value as previous `ImageLibMetaMessage` `id`. + * @property {string} data The response (as an SVG string or URL) + */ + + /** + * @typedef {PlainObject} ImageLibMetaMessage + * @property {"imagelib"} namespace Required to distinguish from any other messages of app. + * @property {string} name If the subsequent response is an SVG string or if `preview_url` + * is present, will be used as the title for the preview image. When an + * SVG string is present, will default to the first ``'s contents or + * "(SVG #<Length of response>)" if none is present. Otherwise, if `preview_url` + * is present, will default to the empty string. + * @property {string} id Identifier (the expected `href` for a subsequent response message); + * used for ensuring the subsequent response can be tied to this `ImageLibMetaMessage` object. + * @property {string} [preview_url] Used to set an image source along with the + * name/title. If the subsequent response is an SVG string and there is no `preview_url`, + * the default will just be to show the name/title. If the response is not an SVG + * string, the default will be to show that response (i.e., the URL). + * @property {string} entry Set automatically with div holding retrieving message (until ready to delete) + * @todo Should use a separate Map instead of `entry` + */ + + /** + * @param {PlainObject} cfg + * @param {string} cfg.origin + * @param {ImageLibMetaMessage|ImageLibMessage|string} cfg.data String is deprecated when parsed to JSON `ImageLibMessage` + * @returns {void} + */ + async function onMessage ({origin, data: response}) { // eslint-disable-line no-shadow if (!response || !['string', 'object'].includes(typeof response)) { // Do nothing return; @@ -283,7 +313,10 @@ export default { break; } } - }, true); + } + + // Receive `postMessage` data + window.addEventListener('message', onMessage, true); /** * @param {boolean} show