Applied patch by bencurthoys to fix embed api

git-svn-id: http://svg-edit.googlecode.com/svn/trunk@1734 eee81c28-f429-11dd-99c0-75d572ba1ddd
master
Alexis Deveria 2010-09-20 18:55:10 +00:00
parent b79afbde87
commit fb653705d6
5 changed files with 160 additions and 123 deletions

56
editor/embedapi.html Normal file
View File

@ -0,0 +1,56 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title></title>
</head>
<body>
<script type="text/javascript" src="embedapi.js"></script>
<script type="text/javascript">
var svgCanvas = null;
function init_embed() {
var frame = document.getElementById('svgedit');
svgCanvas = new embedded_svg_edit(frame);
// Hide main button, as we will be controlling new/load/save etc from the host document
var doc;
doc = frame.contentDocument;
if (!doc)
{
doc = frame.contentWindow.document;
}
var mainButton = doc.getElementById('main_button');
mainButton.style.display = 'none';
}
function handleSvgData(data, error) {
if (error)
{
alert('error ' + error);
}
else
{
alert('Congratulations. Your SVG string is back in the host page, do with it what you will\n\n' + data);
}
}
function loadSvg() {
var svgexample = '<svg width="640" height="480" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg"><g><title>Layer 1</title><rect stroke-width="5" stroke="#000000" fill="#FF0000" id="svg_1" height="35" width="51" y="35" x="32"/><ellipse ry="15" rx="24" stroke-width="5" stroke="#000000" fill="#0000ff" id="svg_2" cy="60" cx="66"/></g></svg>';
svgCanvas.setSvgString(svgexample);
}
function saveSvg() {
svgCanvas.getSvgString()(handleSvgData);
}
</script>
<button onclick="loadSvg();">Load example</button>
<button onclick="saveSvg();">Save data</button>
<br/>
<iframe src="svg-editor.html" width="900px" height="600px" id="svgedit" onload="init_embed()"></iframe>
</body>
</html>

View File

@ -55,11 +55,7 @@ svgEditor.addExtension("imagelib", function() {
var response = evt.data;
if(!response) {
$.alert('No data was given', function() {
if(mode !== 'm') {
closeBrowser();
}
});
// Do nothing
return;
}
@ -131,13 +127,19 @@ svgEditor.addExtension("imagelib", function() {
// Assume it's raw image data
// importImage(str);
$.alert('Unexpected data was returned: ' + response, function() {
if(mode !== 'm') {
closeBrowser();
} else {
pending[id].entry.remove();
}
});
// 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;
}

View File

@ -10,7 +10,8 @@
<link rel="stylesheet" href="svg-editor.css" type="text/css"/>
<link rel="stylesheet" href="spinbtn/JQuerySpinBtn.css" type="text/css"/>
<!-- Development version of script tags: -->
<script type="text/javascript" src="jquery.js"></script>
<!-- <script type="text/javascript" src="jquery.js"></script> -->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.js"></script>
<script type="text/javascript" src="js-hotkeys/jquery.hotkeys.min.js"></script>
<script type="text/javascript" src="jgraduate/jquery.jgraduate.js"></script>
<script type="text/javascript" src="svgicons/jquery.svgicons.js"></script>
@ -24,7 +25,7 @@
<!-- <script type="text/javascript" src="extensions/ext-helloworld.js"></script> -->
<!-- Release version of script tags: >
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="js-hotkeys/jquery.hotkeys-0.7.9.js"></script>
<script type="text/javascript" src="jquerybbq/jquery.bbq.min.js"></script>
<script type="text/javascript" src="jgraduate/jquery.jgraduate.min.js"></script>

View File

@ -3992,48 +3992,47 @@
// var lang = ('lang' in curPrefs) ? curPrefs.lang : null;
Editor.putLocale(null, good_langs);
// Not sure what this was being used for...commented out until known.
// The "message" event listener was interfering with image lib responder
// try{
// json_encode = function(obj){
// //simple partial JSON encoder implementation
// if(window.JSON && JSON.stringify) return JSON.stringify(obj);
// var enc = arguments.callee; //for purposes of recursion
// if(typeof obj == "boolean" || typeof obj == "number"){
// return obj+'' //should work...
// }else if(typeof obj == "string"){
// //a large portion of this is stolen from Douglas Crockford's json2.js
// return '"'+
// obj.replace(
// /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g
// , function (a) {
// return '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
// })
// +'"'; //note that this isn't quite as purtyful as the usualness
// }else if(obj.length){ //simple hackish test for arrayish-ness
// for(var i = 0; i < obj.length; i++){
// obj[i] = enc(obj[i]); //encode every sub-thingy on top
// }
// return "["+obj.join(",")+"]";
// }else{
// var pairs = []; //pairs will be stored here
// for(var k in obj){ //loop through thingys
// pairs.push(enc(k)+":"+enc(obj[k])); //key: value
// }
// return "{"+pairs.join(",")+"}" //wrap in the braces
// }
// }
// window.addEventListener("message", function(e){
// var cbid = parseInt(e.data.substr(0, e.data.indexOf(";")));
// try{
// e.source.postMessage("SVGe"+cbid+";"+json_encode(eval(e.data)), e.origin);
// }catch(err){
// e.source.postMessage("SVGe"+cbid+";error:"+err.message, e.origin);
// }
// }, false)
// }catch(err){
// window.embed_error = err;
// }
// Callback handler for embedapi.js
try{
json_encode = function(obj){
//simple partial JSON encoder implementation
if(window.JSON && JSON.stringify) return JSON.stringify(obj);
var enc = arguments.callee; //for purposes of recursion
if(typeof obj == "boolean" || typeof obj == "number"){
return obj+'' //should work...
}else if(typeof obj == "string"){
//a large portion of this is stolen from Douglas Crockford's json2.js
return '"'+
obj.replace(
/[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g
, function (a) {
return '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
})
+'"'; //note that this isn't quite as purtyful as the usualness
}else if(obj.length){ //simple hackish test for arrayish-ness
for(var i = 0; i < obj.length; i++){
obj[i] = enc(obj[i]); //encode every sub-thingy on top
}
return "["+obj.join(",")+"]";
}else{
var pairs = []; //pairs will be stored here
for(var k in obj){ //loop through thingys
pairs.push(enc(k)+":"+enc(obj[k])); //key: value
}
return "{"+pairs.join(",")+"}" //wrap in the braces
}
}
window.addEventListener("message", function(e){
var cbid = parseInt(e.data.substr(0, e.data.indexOf(";")));
try{
e.source.postMessage("SVGe"+cbid+";"+json_encode(eval(e.data)), "*");
}catch(err){
e.source.postMessage("SVGe"+cbid+";error:"+err.message, "*");
}
}, false)
}catch(err){
window.embed_error = err;
}

View File

@ -208,26 +208,6 @@ $(function() {
}
data_loaded = true;
}
// Clean source SVGs (mostly for Inkscape files)
// TODO: Find a way to do this without crashing Safari (when converting to IMG)
$(svgdoc).find('metadata').remove().end()
.find('*').each(function(i, el) {
if(el.nodeName.indexOf(':') != -1) {
$(el).remove();
}
var attrs = $.extend(false, el.attributes, {});
for(i in attrs) {
var attr = attrs[i];
var fullattr = attr.prefix?attr.prefix + ':' + attr.localName:'';
if(attr.prefix) {
el.removeAttribute(attr.localName); // for Opera
el.removeAttribute(fullattr); // for Webkit
}
if(fullattr == 'xlink:href') {
el.setAttribute('xlink:href', attr.nodeValue);
}
}
});
elems = $(svgdoc.firstChild).children(); //.getElementsByTagName('foreignContent');
@ -253,36 +233,36 @@ $(function() {
}
}
var setIcon = function(target, icon, id, setID) {
if(isOpera) icon.css('visibility','hidden');
if(opts.replace) {
if(setID) icon.attr('id',id);
var cl = target.attr('class');
if(cl) icon.attr('class','svg_icon '+cl);
target.replaceWith(icon);
} else {
target.append(icon);
}
if(isOpera) {
setTimeout(function() {
icon.removeAttr('style');
},1);
}
}
var addIcon = function(icon, id) {
if(opts.id_match === undefined || opts.id_match !== false) {
setIcon(holder, icon, id, true);
}
svg_icons[id] = icon;
}
function makeIcons(toImage, fallback) {
if(icons_made) return;
if(opts.no_img) toImage = false;
var holder;
var setIcon = function(target, icon, id, setID) {
if(isOpera) icon.css('visibility','hidden');
if(opts.replace) {
if(setID) icon.attr('id',id);
var cl = target.attr('class');
if(cl) icon.attr('class','svg_icon '+cl);
target.replaceWith(icon);
} else {
target.append(icon);
}
if(isOpera) {
setTimeout(function() {
icon.removeAttr('style');
},1);
}
}
var addIcon = function(icon, id) {
if(opts.id_match === undefined || opts.id_match !== false) {
setIcon(holder, icon, id, true);
}
svg_icons[id] = icon;
}
if(toImage) {
var temp_holder = $(document.createElement('div'));
temp_holder.hide().appendTo('body');
@ -303,13 +283,13 @@ $(function() {
addIcon(icon, id);
});
} else {
$.each(elems, function(i, elem) {
var id = elem.getAttribute('id');
if(id == 'svg_eof') return;
var len = elems.length;
for(var i = 0; i < len; i++) {
var elem = elems[i];
var id = elem.id;
if(id === 'svg_eof') break;
holder = $('#' + id);
if(elem.getElementsByTagNameNS) {
var svg = elem.getElementsByTagNameNS(svgns, 'svg')[0];
}
var svg = elem.getElementsByTagNameNS(svgns, 'svg')[0];
var svgroot = document.createElementNS(svgns, "svg");
svgroot.setAttributeNS(svgns, 'viewBox', [0,0,icon_w,icon_h].join(' '));
@ -324,17 +304,16 @@ $(function() {
svg.setAttribute('viewBox', [0,0,w,h].join(' '));
}
$(svgroot).attr({
"xmlns": svgns,
"width": icon_w,
"height": icon_h,
"xmlns:xlink": xlinkns,
"class": 'svg_icon'
});
// Not using jQuery to be a bit faster
svgroot.setAttribute('xmlns', svgns);
svgroot.setAttribute('width', icon_w);
svgroot.setAttribute('height', icon_h);
svgroot.setAttribute("xmlns:xlink", xlinkns);
svgroot.setAttribute("class", 'svg_icon');
// Without cloning, Firefox will make another GET request.
// With cloning, causes issue in Opera/Win/Non-EN
if(!isOpera) svg = svg.cloneNode(true);
// if(!isOpera) svg = svg.cloneNode(true);
svgroot.appendChild(svg);
@ -350,7 +329,8 @@ $(function() {
var icon = fixIDs($(svgroot), i);
}
addIcon(icon, id);
});
}
}
if(opts.placement) {
@ -372,7 +352,6 @@ $(function() {
icons_made = true;
if(opts.callback) opts.callback(svg_icons);
}
fixIDs = function(svg_el, svg_num, force) {
@ -385,18 +364,18 @@ $(function() {
if(isOpera) no_dupes = false; // Opera didn't clone svg_el, so not reliable
// if(!force && no_dupes) return;
var new_id = 'x' + id + svg_num + i;
$(this).attr('id', new_id);
this.id = new_id;
svg_el.find('[fill="url(#' + id + ')"]').each(function() {
$(this).attr('fill', 'url(#' + new_id + ')');
this.setAttribute('fill', 'url(#' + new_id + ')');
}).end().find('[stroke="url(#' + id + ')"]').each(function() {
$(this).attr('stroke', 'url(#' + new_id + ')');
this.setAttribute('stroke', 'url(#' + new_id + ')');
}).end().find('use').each(function() {
if(this.getAttribute('xlink:href') == '#' + id) {
this.setAttributeNS(xlinkns,'href','#' + new_id);
}
}).end().find('[filter="url(#' + id + ')"]').each(function() {
$(this).attr('filter', 'url(#' + new_id + ')');
this.setAttribute('filter', 'url(#' + new_id + ')');
});
});
return svg_el;