/* globals jQuery */ /** * ext-imagelib.js * * @license MIT * * @copyright 2010 Alexis Deveria * */ export default { name: 'imagelib', async init ({decode64, importLocale, dropXMLInternalSubset}) { const imagelibStrings = await importLocale(); const svgEditor = this; const $ = jQuery; const {uiStrings, canvas: svgCanvas, curConfig: {allowedImageLibOrigins}} = svgEditor; function closeBrowser () { $('#imgbrowse_holder').hide(); } function importImage (url) { const newImage = svgCanvas.addSVGElementFromJson({ element: 'image', attr: { x: 0, y: 0, width: 0, height: 0, id: svgCanvas.getNextId(), style: 'pointer-events:inherit' } }); svgCanvas.clearSelection(); svgCanvas.addToSelection([newImage]); svgCanvas.setImageURL(url); } const pending = {}; let mode = 's'; let multiArr = []; let transferStopped = false; let preview, submit; // Receive `postMessage` data window.addEventListener('message', function ({origin, data: response}) { if (!response || typeof response !== 'string') { // Do nothing return; } try { // Todo: This block can be removed (and the above check changed to // insist on an object) if embedAPI moves away from a string to // an object (if IE9 support not needed) response = JSON.parse(response); if (response.namespace !== 'imagelib') { return; } if (!allowedImageLibOrigins.includes('*') && !allowedImageLibOrigins.includes(origin)) { console.log(`Origin ${origin} not whitelisted for posting to ${window.origin}`); return; } } catch (e) { return; } const hasName = 'name' in response; const hasHref = 'href' in response; if (!hasName && transferStopped) { transferStopped = false; return; } let id; if (hasHref) { id = response.href; response = response.data; } // Hide possible transfer dialog box $('#dialog_box').hide(); let entry, curMeta, svgStr, imgStr; const type = hasName ? 'meta' : response.charAt(0); switch (type) { case 'meta': { // Metadata transferStopped = false; curMeta = response; // Should be safe to add dynamic property as passed metadata pending[curMeta.id] = curMeta; // lgtm [js/remote-property-injection] const name = (curMeta.name || 'file'); const message = uiStrings.notification.retrieving.replace('%s', name); if (mode !== 'm') { $.process_cancel(message, function () { transferStopped = true; // Should a message be sent back to the frame? $('#dialog_box').hide(); }); } else { entry = $('