Fix another bug with Issue 40: group, rotate, move, undo, redo: now works

git-svn-id: http://svg-edit.googlecode.com/svn/trunk@605 eee81c28-f429-11dd-99c0-75d572ba1ddd
master
Jeff Schiller 2009-09-06 03:51:16 +00:00
parent e1382ac70a
commit f70cdf5043
1 changed files with 37 additions and 11 deletions

View File

@ -42,6 +42,7 @@ function ChangeElementCommand(elem, attrs, text) {
} }
this.apply = function() { this.apply = function() {
var bChangedTransform = false;
for( attr in this.newValues ) { for( attr in this.newValues ) {
if (this.newValues[attr]) { if (this.newValues[attr]) {
if (attr == "#text") this.elem.textContent = this.newValues[attr]; if (attr == "#text") this.elem.textContent = this.newValues[attr];
@ -51,9 +52,10 @@ function ChangeElementCommand(elem, attrs, text) {
if (attr != "#text") this.elem.textContent = ""; if (attr != "#text") this.elem.textContent = "";
else this.elem.removeAttribute(attr); else this.elem.removeAttribute(attr);
} }
if (attr == "transform") { bChangedTransform = true; }
} }
// relocate rotational transform, if necessary // relocate rotational transform, if necessary
if($.inArray("transform", this.newValues) == -1) { if(!bChangedTransform) {
var angle = canvas.getRotationAngle(elem); var angle = canvas.getRotationAngle(elem);
if (angle) { if (angle) {
var bbox = elem.getBBox(); var bbox = elem.getBBox();
@ -69,6 +71,7 @@ function ChangeElementCommand(elem, attrs, text) {
}; };
this.unapply = function() { this.unapply = function() {
var bChangedTransform = false;
for( attr in this.oldValues ) { for( attr in this.oldValues ) {
if (this.oldValues[attr]) { if (this.oldValues[attr]) {
if (attr == "#text") this.elem.textContent = this.oldValues[attr]; if (attr == "#text") this.elem.textContent = this.oldValues[attr];
@ -78,9 +81,10 @@ function ChangeElementCommand(elem, attrs, text) {
if (attr == "#text") this.elem.textContent = ""; if (attr == "#text") this.elem.textContent = "";
else this.elem.removeAttribute(attr); else this.elem.removeAttribute(attr);
} }
if (attr == "transform") { bChangedTransform = true; }
} }
// relocate rotational transform, if necessary // relocate rotational transform, if necessary
if($.inArray("transform", this.oldValues) == -1) { if(!bChangedTransform) {
var angle = canvas.getRotationAngle(elem); var angle = canvas.getRotationAngle(elem);
if (angle) { if (angle) {
var bbox = elem.getBBox(); var bbox = elem.getBBox();
@ -130,7 +134,7 @@ function RemoveElementCommand(elem, parent, text) {
function MoveElementCommand(elem, oldNextSibling, oldParent, text) { function MoveElementCommand(elem, oldNextSibling, oldParent, text) {
this.elem = elem; this.elem = elem;
this.text = text ? ("Move " + elem.tagName + " to " + text) : ("Move " + elem.tagName + "top/bottom"); this.text = text ? ("Move " + elem.tagName + " to " + text) : ("Move " + elem.tagName);
this.oldNextSibling = oldNextSibling; this.oldNextSibling = oldNextSibling;
this.oldParent = oldParent; this.oldParent = oldParent;
this.newNextSibling = elem.nextSibling; this.newNextSibling = elem.nextSibling;
@ -884,8 +888,10 @@ function BatchCommand(text) {
var box = canvas.getBBox(selected); var box = canvas.getBBox(selected);
// if we have not moved/resized, then immediately leave // if we have not moved/resized, then immediately leave
if (box.x == selectedBBox.x && box.y == selectedBBox.y && var xform = selected.getAttribute("transform");
box.width == selectedBBox.width && box.height == selectedBBox.height) { if ( (!xform || xform == "") && box.x == selectedBBox.x && box.y == selectedBBox.y &&
box.width == selectedBBox.width && box.height == selectedBBox.height)
{
return null; return null;
} }
@ -900,7 +906,7 @@ function BatchCommand(text) {
var scalew = function(w) {return parseInt(w*selectedBBox.width/box.width);} var scalew = function(w) {return parseInt(w*selectedBBox.width/box.width);}
var scaleh = function(h) {return parseInt(h*selectedBBox.height/box.height);} var scaleh = function(h) {return parseInt(h*selectedBBox.height/box.height);}
var changes = {}; var batchCmd = new BatchCommand("Transform");
// if there was a rotation transform, re-set it, otherwise empty out the transform attribute // if there was a rotation transform, re-set it, otherwise empty out the transform attribute
var angle = canvas.getRotationAngle(selected); var angle = canvas.getRotationAngle(selected);
@ -959,6 +965,11 @@ function BatchCommand(text) {
var rotate = ["rotate(", angle, " ", cx, ",", cy, ")"].join(''); var rotate = ["rotate(", angle, " ", cx, ",", cy, ")"].join('');
selected.setAttribute("transform", rotate); selected.setAttribute("transform", rotate);
// if we were rotated, store just the old rotation (not other transforms) on the
// undo stack
var changes = {};
changes["transform"] = ["rotate(", angle, " ", tr_x, ",", tr_y, ")"].join('');
batchCmd.addSubCommand(new ChangeElementCommand(selected, changes));
if(pointGripContainer) { if(pointGripContainer) {
pointGripContainer.setAttribute("transform", rotate); pointGripContainer.setAttribute("transform", rotate);
} }
@ -977,8 +988,6 @@ function BatchCommand(text) {
// if it's a group, transfer the transform attribute to each child element // if it's a group, transfer the transform attribute to each child element
// and recursively call recalculateDimensions() // and recursively call recalculateDimensions()
if (selected.tagName == "g") { if (selected.tagName == "g") {
var batchCmd = new BatchCommand("Transform Group");
var xform = selected.getAttribute("transform");
var children = selected.childNodes; var children = selected.childNodes;
var i = children.length; var i = children.length;
while (i--) { while (i--) {
@ -998,6 +1007,8 @@ function BatchCommand(text) {
return batchCmd; return batchCmd;
} }
var changes = {};
switch (selected.tagName) switch (selected.tagName)
{ {
case "g": case "g":
@ -1185,8 +1196,9 @@ function BatchCommand(text) {
break; break;
} }
if (changes) { if (changes) {
return new ChangeElementCommand(selected, changes); batchCmd.addSubCommand(new ChangeElementCommand(selected, changes));
} }
return batchCmd;
}; };
// public events // public events
@ -3004,6 +3016,8 @@ function BatchCommand(text) {
canvas.addToSelection([g]); canvas.addToSelection([g]);
}; };
// TODO: if the group has a rotational transform on it, transfer that transform
// to each child element and add the change commands to the batch command
this.ungroupSelectedElement = function() { this.ungroupSelectedElement = function() {
var g = selectedElements[0]; var g = selectedElements[0];
if (g.tagName == "g") { if (g.tagName == "g") {
@ -3011,6 +3025,7 @@ function BatchCommand(text) {
var parent = g.parentNode; var parent = g.parentNode;
var anchor = g.previousSibling; var anchor = g.previousSibling;
var children = new Array(g.childNodes.length); var children = new Array(g.childNodes.length);
var xform = g.getAttribute("transform");
var i = 0; var i = 0;
while (g.firstChild) { while (g.firstChild) {
var elem = g.firstChild; var elem = g.firstChild;
@ -3018,6 +3033,17 @@ function BatchCommand(text) {
var oldParent = elem.parentNode; var oldParent = elem.parentNode;
children[i++] = elem = parent.insertBefore(elem, anchor); children[i++] = elem = parent.insertBefore(elem, anchor);
batchCmd.addSubCommand(new MoveElementCommand(elem, oldNextSibling, oldParent)); batchCmd.addSubCommand(new MoveElementCommand(elem, oldNextSibling, oldParent));
if (xform) {
elem.setAttribute("transform", xform);
batchCmd.addSubCommand(recalculateDimensions(elem, elem.getBBox()));
}
}
// remove transform and make it undo-able
if (xform) {
var changes = {};
changes['transform'] = xform;
batchCmd.addSubCommand(new ChangeElementCommand(g, changes));
} }
// remove the group from the selection // remove the group from the selection