diff --git a/public/svg-edit/editor/embedapi.html b/public/svg-edit/editor/embedapi.html new file mode 100644 index 00000000..3db03640 --- /dev/null +++ b/public/svg-edit/editor/embedapi.html @@ -0,0 +1,56 @@ + + + + + + + + + + + + + +
+ + + + diff --git a/public/svg-edit/editor/extensions/ext-imagelib.js b/public/svg-edit/editor/extensions/ext-imagelib.js index 49df1729..642f0071 100644 --- a/public/svg-edit/editor/extensions/ext-imagelib.js +++ b/public/svg-edit/editor/extensions/ext-imagelib.js @@ -44,30 +44,79 @@ svgEditor.addExtension("imagelib", function() { svgCanvas.setImageURL(url); } + var mode = 's'; + var multi_arr = []; + var cur_meta; + var tranfer_stopped = false; + var pending = {}; + window.addEventListener("message", function(evt) { // Receive postMessage data var response = evt.data; if(!response) { - $.alert('No data was given', closeBrowser); + // Do nothing return; } var char1 = response.charAt(0); + var svg_str; + var img_str; + + if(char1 != "{" && tranfer_stopped) { + tranfer_stopped = false; + return; + } + + if(char1 == '|') { + var secondpos = response.indexOf('|', 1); + var id = response.substr(1, secondpos-1); + response = response.substr(secondpos+1); + char1 = response.charAt(0); + + } + + + // Hide possible transfer dialog box + $('#dialog_box').hide(); + switch (char1) { + case '{': + // Metadata + tranfer_stopped = false; + var cur_meta = JSON.parse(response); + + pending[cur_meta.id] = cur_meta; + + var message = 'Retrieving "' + (cur_meta.name || 'file') + '"...'; + + if(mode != 'm') { + $.process_cancel(message, function() { + tranfer_stopped = true; + // Should a message be sent back to the frame? + + $('#dialog_box').hide(); + }); + } else { + var entry = $('
' + message + '
').data('id', cur_meta.id); + preview.append(entry); + cur_meta.entry = entry; + } + + return; case '<': - svgCanvas.importSvgString(response); + svg_str = true; 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)); + response = svgCanvas.Utils.decode64(src); + svg_str = true; break; } else if(response.indexOf('data:image/') === 0) { - - importImage(response); + img_str = true; break; } // Else fall through @@ -78,14 +127,134 @@ svgEditor.addExtension("imagelib", function() { // Assume it's raw image data // importImage(str); - $.alert('Unexpected data was returned: ' + response, closeBrowser); + // Don't give warning as postMessage may have been used by something else + if(mode !== 'm') { + closeBrowser(); + } else { + pending[id].entry.remove(); + } +// $.alert('Unexpected data was returned: ' + response, function() { +// if(mode !== 'm') { +// closeBrowser(); +// } else { +// pending[id].entry.remove(); +// } +// }); return; } - closeBrowser(); - + switch (mode) { + case 's': + // Import one + if(svg_str) { + svgCanvas.importSvgString(response); + } else if(img_str) { + importImage(response); + } + closeBrowser(); + break; + case 'm': + // Import multiple + multi_arr.push([(svg_str ? 'svg' : 'img'), response]); + var cur_meta = pending[id]; + if(svg_str) { + if(cur_meta && cur_meta.name) { + var title = cur_meta.name; + } else { + // Try to find a title + var xml = new DOMParser().parseFromString(response, 'text/xml').documentElement; + var title = $(xml).children('title').first().text() || '(SVG #' + response.length + ')'; + } + if(cur_meta) { + preview.children().each(function() { + if($(this).data('id') == id) { + if(cur_meta.preview_url) { + $(this).html('' + title); + } else { + $(this).text(title); + } + } + }); + } else { + preview.append('
'+title+'
'); + } + } else { + if(cur_meta && cur_meta.preview_url) { + var title = cur_meta.name || ''; + } + if(cur_meta && cur_meta.preview_url) { + var entry = '' + title; + } else { + var entry = ''; + } + + if(cur_meta) { + preview.children().each(function() { + if($(this).data('id') == id) { + $(this).html(entry); + } + }); + } else { + preview.append($('
').append(entry)); + } + + } + break; + case 'o': + // Open + if(!svg_str) break; + svgEditor.openPrep(function(ok) { + if(!ok) return; + svgCanvas.clear(); + svgCanvas.setSvgString(response); + // updateCanvas(); + }); + closeBrowser(); + break; + } }, true); + var preview; + + function toggleMulti(show) { + var submit; + + $('#lib_framewrap, #imglib_opts').css({right: (show ? 200 : 10)}); + if(!preview) { + preview = $('
').css({ + position: 'absolute', + top: 45, + right: 10, + width: 180, + bottom: 45, + background: '#fff', + overflow: 'auto' + }).insertAfter('#lib_framewrap'); + + submit = $('').appendTo('#imgbrowse').click(function() { + $.each(multi_arr, function(i) { + var type = this[0]; + var data = this[1]; + if(type == 'svg') { + svgCanvas.importSvgString(data); + } else { + importImage(data); + } + svgCanvas.moveSelectedElements(i*20, i*20, false); + }); + preview.empty(); + multi_arr = []; + $('#imgbrowse_holder').hide(); + }).css({ + position: 'absolute', + bottom: 10, + right: -10 + }); + + } + + preview.toggle(show); + } function showBrowser() { var browser = $('#imgbrowse'); @@ -97,9 +266,14 @@ svgEditor.addExtension("imagelib", function() { var all_libs = 'Select an image library'; var lib_opts = $('