fixed pathtool bug, where browsers would delete path with pathseg error in console. Worked around this by copying 'item' object, but could possibly be optimised further by only copying required pars of object

master
ibrierley 2015-07-06 12:21:37 +02:00
parent 7a1273eb93
commit fbbd740c79
1 changed files with 40 additions and 20 deletions

View File

@ -15,7 +15,8 @@
// 3) math.js // 3) math.js
// 4) svgutils.js // 4) svgutils.js
(function() {'use strict'; (function() {
//'use strict';
if (!svgedit.path) { if (!svgedit.path) {
svgedit.path = {}; svgedit.path = {};
@ -139,7 +140,6 @@ svgedit.path.getPointFromGrip = function(pt, path) {
svgedit.path.addPointGrip = function(index, x, y) { svgedit.path.addPointGrip = function(index, x, y) {
// create the container of all the point grips // create the container of all the point grips
var pointGripContainer = svgedit.path.getGripContainer(); var pointGripContainer = svgedit.path.getGripContainer();
var pointGrip = svgedit.utilities.getElem('pathpointgrip_'+index); var pointGrip = svgedit.utilities.getElem('pathpointgrip_'+index);
// create it // create it
if (!pointGrip) { if (!pointGrip) {
@ -208,7 +208,6 @@ svgedit.path.addCtrlGrip = function(id) {
svgedit.path.getCtrlLine = function(id) { svgedit.path.getCtrlLine = function(id) {
var ctrlLine = svgedit.utilities.getElem('ctrlLine_'+id); var ctrlLine = svgedit.utilities.getElem('ctrlLine_'+id);
if (ctrlLine) {return ctrlLine;} if (ctrlLine) {return ctrlLine;}
ctrlLine = document.createElementNS(NS.SVG, 'line'); ctrlLine = document.createElementNS(NS.SVG, 'line');
svgedit.utilities.assignAttributes(ctrlLine, { svgedit.utilities.assignAttributes(ctrlLine, {
'id': 'ctrlLine_'+id, 'id': 'ctrlLine_'+id,
@ -251,7 +250,6 @@ svgedit.path.getControlPoints = function(seg) {
var i; var i;
for (i = 1; i < 3; i++) { for (i = 1; i < 3; i++) {
var id = index + 'c' + i; var id = index + 'c' + i;
var ctrlLine = cpt['c' + i + '_line'] = svgedit.path.getCtrlLine(id); var ctrlLine = cpt['c' + i + '_line'] = svgedit.path.getCtrlLine(id);
var pt = svgedit.path.getGripPt(seg, {x:item['x' + i], y:item['y' + i]}); var pt = svgedit.path.getGripPt(seg, {x:item['x' + i], y:item['y' + i]});
@ -283,6 +281,7 @@ svgedit.path.getControlPoints = function(seg) {
// This replaces the segment at the given index. Type is given as number. // This replaces the segment at the given index. Type is given as number.
svgedit.path.replacePathSeg = function(type, index, pts, elem) { svgedit.path.replacePathSeg = function(type, index, pts, elem) {
var path = elem || svgedit.path.path.elem; var path = elem || svgedit.path.path.elem;
var func = 'createSVGPathSeg' + pathFuncs[type]; var func = 'createSVGPathSeg' + pathFuncs[type];
var seg = path[func].apply(path, pts); var seg = path[func].apply(path, pts);
@ -308,6 +307,7 @@ svgedit.path.replacePathSeg = function(type, index, pts, elem) {
} }
}; };
svgedit.path.getSegSelector = function(seg, update) { svgedit.path.getSegSelector = function(seg, update) {
var index = seg.index; var index = seg.index;
var segLine = svgedit.utilities.getElem('segline_' + index); var segLine = svgedit.utilities.getElem('segline_' + index);
@ -345,7 +345,6 @@ svgedit.path.getSegSelector = function(seg, update) {
pts[i] = pt.x; pts[i] = pt.x;
pts[i+1] = pt.y; pts[i+1] = pt.y;
} }
svgedit.path.replacePathSeg(seg.type, 1, pts, segLine); svgedit.path.replacePathSeg(seg.type, 1, pts, segLine);
} }
return segLine; return segLine;
@ -477,12 +476,17 @@ svgedit.path.Segment.prototype.update = function(full) {
svgedit.path.Segment.prototype.move = function(dx, dy) { svgedit.path.Segment.prototype.move = function(dx, dy) {
var cur_pts, item = this.item; var cur_pts, item = this.item;
if (this.ctrlpts) { // fix for path tool dom breakage, amending item does bad things now, so we take a copy and use that. Can probably improve to just take a shallow copy of object
cur_pts = [item.x += dx, item.y += dy, var cloneItem = $.extend({}, item);
item.x1, item.y1, item.x2 += dx, item.y2 += dy];
if(this.ctrlpts) {
var cur_pts = [cloneItem.x += dx, cloneItem.y += dy,
cloneItem.x1, cloneItem.y1, cloneItem.x2 += dx, cloneItem.y2 += dy];
} else { } else {
cur_pts = [item.x += dx, item.y += dy]; var cur_pts = [cloneItem.x += dx, cloneItem.y += dy];
} }
//fix
svgedit.path.replacePathSeg(this.type, this.index, cur_pts); svgedit.path.replacePathSeg(this.type, this.index, cur_pts);
if (this.next && this.next.ctrlpts) { if (this.next && this.next.ctrlpts) {
@ -505,6 +509,9 @@ svgedit.path.Segment.prototype.move = function(dx, dy) {
if (this.next) {this.next.update(true);} if (this.next) {this.next.update(true);}
}; };
//HERE
svgedit.path.Segment.prototype.setLinked = function(num) { svgedit.path.Segment.prototype.setLinked = function(num) {
var seg, anum, pt; var seg, anum, pt;
if (num == 2) { if (num == 2) {
@ -520,13 +527,16 @@ svgedit.path.Segment.prototype.setLinked = function(num) {
} }
var item = seg.item; var item = seg.item;
// fix for path tool dom breakage, amending item does bad things now, so we take a copy and use that. Can probably improve to just take a shallow copy of object
var cloneItem = $.extend({}, item);
cloneItem['x' + anum ] = pt.x + (pt.x - this.item['x' + num]);
cloneItem['y' + anum ] = pt.y + (pt.y - this.item['y' + num]);
item['x' + anum] = pt.x + (pt.x - this.item['x' + num]); var pts = [cloneItem.x, cloneItem.y,
item['y' + anum] = pt.y + (pt.y - this.item['y' + num]); cloneItem.x1, cloneItem.y1,
cloneItem.x2, cloneItem.y2];
//end fix
var pts = [item.x, item.y,
item.x1, item.y1,
item.x2, item.y2];
svgedit.path.replacePathSeg(seg.type, seg.index, pts); svgedit.path.replacePathSeg(seg.type, seg.index, pts);
seg.update(true); seg.update(true);
@ -535,10 +545,16 @@ svgedit.path.Segment.prototype.setLinked = function(num) {
svgedit.path.Segment.prototype.moveCtrl = function(num, dx, dy) { svgedit.path.Segment.prototype.moveCtrl = function(num, dx, dy) {
var item = this.item; var item = this.item;
item['x' + num] += dx; // fix for path tool dom breakage, amending item does bad things now, so we take a copy and use that. Can probably improve to just take a shallow copy of object
item['y' + num] += dy; var cloneItem = $.extend({}, item);
cloneItem['x' + num] += dx;
cloneItem['y' + num] += dy;
var pts = [cloneItem.x,cloneItem.y,
cloneItem.x1,cloneItem.y1, cloneItem.x2,cloneItem.y2];
// end fix
var pts = [item.x, item.y, item.x1, item.y1, item.x2, item.y2];
svgedit.path.replacePathSeg(this.type, this.index, pts); svgedit.path.replacePathSeg(this.type, this.index, pts);
this.update(true); this.update(true);
@ -569,7 +585,10 @@ svgedit.path.Path = function(elem) {
// Reset path data // Reset path data
svgedit.path.Path.prototype.init = function() { svgedit.path.Path.prototype.init = function() {
// Hide all grips, etc // Hide all grips, etc
$(svgedit.path.getGripContainer()).find('*').attr('display', 'none');
//FIX ianb leaves last grip
$(svgedit.path.getGripContainer()).find('*').each( function() { $(this).attr('display', 'none') });
var segList = this.elem.pathSegList; var segList = this.elem.pathSegList;
var len = segList.numberOfItems; var len = segList.numberOfItems;
this.segs = []; this.segs = [];
@ -699,6 +718,7 @@ svgedit.path.Path.prototype.deleteSeg = function(index) {
var pt; var pt;
if (seg.mate) { if (seg.mate) {
// Make the next point be the "M" point // Make the next point be the "M" point
pt = [next.item.x, next.item.y]; pt = [next.item.x, next.item.y];
svgedit.path.replacePathSeg(2, next.index, pt); svgedit.path.replacePathSeg(2, next.index, pt);