/* * ext-imagelib.js * * Licensed under the Apache License, Version 2 * * Copyright(c) 2010 Alexis Deveria * */ svgEditor.addExtension("imagelib", function() { var img_libs = [{ name: 'Demo library (local)', url: 'extensions/imagelib/index.html', description: 'Demonstration library for SVG-edit on this server' }, { name: 'IAN Symbol Libraries', url: 'http://ian.umces.edu/symbols/catalog/svgedit/album_chooser.php', description: 'Free library of 2300+ illustrations' } ]; var xlinkns = "http://www.w3.org/1999/xlink"; function closeBrowser() { $('#imgbrowse_holder').hide(); } function importImage(url) { var 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); } window.addEventListener("message", function(evt) { // Receive postMessage data var response = evt.data; if(!response) { $.alert('No data was given', closeBrowser); return; } var char1 = response.charAt(0); switch (char1) { case '<': svgCanvas.importSvgString(response); break; case 'd': if(response.indexOf('data:image/svg+xml') === 0) { var pre = 'data:image/svg+xml;base64,'; var src = response.substring(pre.length); svgCanvas.importSvgString(svgCanvas.Utils.decode64(src)); break; } else if(response.indexOf('data:image/') === 0) { importImage(response); break; } // Else fall through default: // TODO: See if there's a way to base64 encode the binary data stream // var str = 'data:;base64,' + svgCanvas.Utils.encode64(response, true); // Assume it's raw image data // importImage(str); $.alert('Unexpected data was returned: ' + response, closeBrowser); return; } closeBrowser(); }, true); function showBrowser() { var browser = $('#imgbrowse'); if(!browser.length) { $('
\
').insertAfter('#svg_docprops'); browser = $('#imgbrowse'); var all_libs = 'Select an image library'; var lib_opts = $('