/* globals jQuery */ /* * ext-imagelib.js * * Licensed under the MIT License * * Copyright(c) 2010 Alexis Deveria * */ import svgEditor from '../svg-editor.js'; svgEditor.addExtension('imagelib', function ({decode64}) { const $ = jQuery; const {uiStrings, canvas: svgCanvas} = svgEditor; $.extend(uiStrings, { imagelib: { select_lib: 'Select an image library', show_list: 'Show library list', import_single: 'Import single', import_multi: 'Import multiple', open: 'Open as new document' } }); const modularVersion = !('svgEditor' in window) || !window.svgEditor || window.svgEditor.modules !== false; const imgLibs = [ { name: 'Demo library (local)', url: svgEditor.curConfig.extIconsPath + 'imagelib/index' + (modularVersion ? '-es' : '') + '.html', description: 'Demonstration library for SVG-edit on this server' }, { name: 'IAN Symbol Libraries', url: 'https://ian.umces.edu/symbols/catalog/svgedit/album_chooser.php', description: 'Free library of illustrations' }, { name: 'Openclipart', url: 'https://openclipart.org/svgedit', description: 'Share and Use Images. Over 50,000 Public Domain SVG Images and Growing.' } ]; 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; window.addEventListener('message', function (evt) { // Receive `postMessage` data let response = evt.data; 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; } } 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; pending[curMeta.id] = curMeta; 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 = $('