Fixed issue 487: foreignObject extension breaks connect two objects tool and made sure both extensions work together

git-svn-id: http://svg-edit.googlecode.com/svn/trunk@1409 eee81c28-f429-11dd-99c0-75d572ba1ddd
master
Alexis Deveria 2010-02-18 15:42:10 +00:00
parent 8e0bf800bf
commit 78c9ae4ad7
3 changed files with 73 additions and 74 deletions

View File

@ -72,74 +72,48 @@ $(function() {
setPoint(elem, 1, (pt_end.x + pt_start.x)/2, (pt_end.y + pt_start.y)/2); setPoint(elem, 1, (pt_end.x + pt_start.x)/2, (pt_end.y + pt_start.y)/2);
} }
} }
function findConnectors() { function findConnectors() {
// Check if selected elements have connections
var elems = selElems; var elems = selElems;
var i = elems.length;
var connectors = $(svgcontent).find(conn_sel); var connectors = $(svgcontent).find(conn_sel);
if(!connectors.length) return;
connections = []; connections = [];
var sel = ':not(a,g,svg,'+conn_sel+')';
var all_els = [];
// Get children from groups
while(i--) {
var elem = elems[i];
if(!elem) continue;
// Get all children that cannot contain children
var solid_elems = $(elem).find(sel);
// Include self if okay
if($(elem).filter(sel).length) {
solid_elems.push(elem);
}
$.merge(all_els, solid_elems);
}
i = all_els.length;
if(i > 1) { // Loop through connectors to see if one is connected to the element
// Multiselected, so unselect connector connectors.each(function() {
svgCanvas.removeFromSelection($(conn_sel).toArray()); var start = $(this).data("c_start");
} var end = $(this).data("c_end");
// Loop through selected elements
while(i--) {
var elem = all_els[i];
if(!elem) continue;
// Element was deleted, so delete connector var parts = [getElem(start), getElem(end)];
var was_deleted = !elem.parentNode; for(var i=0; i<2; i++) {
var c_elem = parts[i];
// Skip connector var add_this = false;
if($(elem).data('c_start')) continue; // The connected element might be part of a selected group
$(c_elem).parents().each(function() {
// Loop through connectors to see if one is connected to the element if($.inArray(this, elems) !== -1) {
connectors.each(function() { // Pretend this element is selected
var start = $(this).data("c_start"); add_this = true;
var end = $(this).data("c_end");
// Connector found for this element
if(start == elem.id || end == elem.id) {
if(was_deleted) {
$(this).remove();
return;
} }
var bb = svgCanvas.getStrokedBBox([elem]); });
if(!c_elem || !c_elem.parentNode) {
$(this).remove();
continue;
}
if($.inArray(c_elem, elems) !== -1 || add_this) {
var bb = svgCanvas.getStrokedBBox([c_elem]);
connections.push({ connections.push({
elem: elem, elem: c_elem,
connector: this, connector: this,
is_start: start == elem.id, is_start: (i === 0),
start_x: bb.x, start_x: bb.x,
start_y: bb.y start_y: bb.y
}); });
} }
}); }
} });
} }
function updateConnectors() { function updateConnectors() {
// Updates connector lines based on selected elements // Updates connector lines based on selected elements
// Is not used on mousemove, as it runs getStrokedBBox every time, // Is not used on mousemove, as it runs getStrokedBBox every time,
@ -300,7 +274,6 @@ $(function() {
}, },
mouseDown: function(opts) { mouseDown: function(opts) {
var e = opts.event; var e = opts.event;
start_x = opts.start_x, start_x = opts.start_x,
start_y = opts.start_y; start_y = opts.start_y;
var mode = svgCanvas.getMode(); var mode = svgCanvas.getMode();
@ -315,8 +288,10 @@ $(function() {
if($.inArray(svgcontent, parents) != -1) { if($.inArray(svgcontent, parents) != -1) {
// Connectable element // Connectable element
start_elem = mouse_target; // If child of foreignObject, use parent
var fo = $(mouse_target).closest("foreignObject");
start_elem = fo.length ? fo[0] : mouse_target;
// Get center of source element // Get center of source element
var bb = svgCanvas.getStrokedBBox([start_elem]); var bb = svgCanvas.getStrokedBBox([start_elem]);
@ -422,10 +397,13 @@ $(function() {
var zoom = svgCanvas.getZoom(); var zoom = svgCanvas.getZoom();
var e = opts.event, var e = opts.event,
x = opts.mouse_x/zoom, x = opts.mouse_x/zoom,
y = opts.mouse_y/zoom; y = opts.mouse_y/zoom,
mouse_target = e.target;
if(svgCanvas.getMode() == "connector") { if(svgCanvas.getMode() == "connector") {
if(e.target.parentNode.parentNode != svgcontent) { var fo = $(mouse_target).closest("foreignObject");
if(fo.length) mouse_target = fo[0];
if(mouse_target.parentNode.parentNode != svgcontent) {
// Not a valid target element, so remove line // Not a valid target element, so remove line
$(cur_line).remove(); $(cur_line).remove();
started = false; started = false;
@ -434,7 +412,7 @@ $(function() {
element: null, element: null,
started: started started: started
} }
} else if(e.target == start_elem) { } else if(mouse_target == start_elem) {
// Start line through click // Start line through click
started = true; started = true;
return { return {
@ -444,7 +422,8 @@ $(function() {
} }
} else { } else {
// Valid end element // Valid end element
end_elem = e.target; end_elem = mouse_target;
var start_id = start_elem.id, end_id = end_elem.id; var start_id = start_elem.id, end_id = end_elem.id;
var conn_str = start_id + " " + end_id; var conn_str = start_id + " " + end_id;
var alt_str = end_id + " " + start_id; var alt_str = end_id + " " + start_id;

View File

@ -63,7 +63,6 @@ $(function() {
var newDoc = Utils.text2xml('<svg xmlns="'+svgns+'" xmlns:xlink="'+xlinkns+'">'+xmlString+'</svg>'); var newDoc = Utils.text2xml('<svg xmlns="'+svgns+'" xmlns:xlink="'+xlinkns+'">'+xmlString+'</svg>');
// run it through our sanitizer to remove anything we do not support // run it through our sanitizer to remove anything we do not support
S.sanitizeSvg(newDoc.documentElement); S.sanitizeSvg(newDoc.documentElement);
elt.parentNode.replaceChild(svgdoc.importNode(newDoc.documentElement.firstChild, true), elt); elt.parentNode.replaceChild(svgdoc.importNode(newDoc.documentElement.firstChild, true), elt);
S.call("changed", [elt]); S.call("changed", [elt]);
svgCanvas.clearSelection(); svgCanvas.clearSelection();

View File

@ -2653,6 +2653,7 @@ function BatchCommand(text) {
}; };
var hasMatrixTransform = function(tlist) { var hasMatrixTransform = function(tlist) {
if(!tlist) return false;
var num = tlist.numberOfItems; var num = tlist.numberOfItems;
while (num--) { while (num--) {
var xform = tlist.getItem(num); var xform = tlist.getItem(num);
@ -3089,11 +3090,13 @@ function BatchCommand(text) {
start_x: start_x, start_x: start_x,
start_y: start_y, start_y: start_y,
selectedElements: selectedElements selectedElements: selectedElements
}); }, true);
if(ext_result) { $.each(ext_result, function(i, r) {
started = ext_result.started; if(r && r.started) {
} started = true;
}
});
}; };
// in this function we do not record any state changes yet (but we do update // in this function we do not record any state changes yet (but we do update
@ -3610,13 +3613,15 @@ function BatchCommand(text) {
event: evt, event: evt,
mouse_x: mouse_x, mouse_x: mouse_x,
mouse_y: mouse_y mouse_y: mouse_y
}); }, true);
if(ext_result) { $.each(ext_result, function(i, r) {
keep = ext_result.keep; if(r) {
element = ext_result.element; keep = r.keep || keep;
started = ext_result.started; element = r.element;
} started = r.started || started;
}
});
if (!keep && element != null) { if (!keep && element != null) {
element.parentNode.removeChild(element); element.parentNode.removeChild(element);
@ -6613,7 +6618,19 @@ function BatchCommand(text) {
ret.y += parseFloat(selected.getAttribute('y')); ret.y += parseFloat(selected.getAttribute('y'));
} else { } else {
try { ret = selected.getBBox(); } try { ret = selected.getBBox(); }
catch(e) { ret = null; } catch(e) {
// Check if element is child of a foreignObject
var fo = $(selected).closest("foreignObject");
if(fo.length) {
try {
ret = fo[0].getBBox();
} catch(e) {
ret = null;
}
} else {
ret = null;
}
}
} }
// get the bounding box from the DOM (which is in that element's coordinate system) // get the bounding box from the DOM (which is in that element's coordinate system)
@ -7337,6 +7354,7 @@ function BatchCommand(text) {
// re-creating the getCheckedBBox() function? shouldn't we make this a function // re-creating the getCheckedBBox() function? shouldn't we make this a function
// at the 'canvas' level // at the 'canvas' level
var getCheckedBBox = function(elem) { var getCheckedBBox = function(elem) {
try { try {
// TODO: Fix issue with rotated groups. Currently they work // TODO: Fix issue with rotated groups. Currently they work
// fine in FF, but not in other browsers (same problem mentioned // fine in FF, but not in other browsers (same problem mentioned
@ -7410,7 +7428,10 @@ function BatchCommand(text) {
} }
return bb; return bb;
} catch(e) { return null; } } catch(e) {
console.log(elem, e);
return null;
}
} }
var full_bb; var full_bb;