when the canvas is clicked, Firefox/Opera return
if ((currentMode !== 'path' || !drawnPath) &&
t && t.parentNode &&
t.parentNode.id !== 'selectorParentGroup' &&
t.id !== 'svgcanvas' && t.id !== 'svgroot'
) {
// switch into "select" mode if we've clicked on an element
canvas.setMode('select');
selectOnly([t], true);
}
} else if (element != null) {
canvas.addedNew = true;
if (useUnit) { convertAttrs(element); }
let aniDur = 0.2;
let cAni;
if (opacAni.beginElement && parseFloat(element.getAttribute('opacity')) !== curShape.opacity) {
cAni = $(opacAni).clone().attr({
to: curShape.opacity,
dur: aniDur
}).appendTo(element);
try {
// Fails in FF4 on foreignObject
cAni[0].beginElement();
} catch (e) {}
} else {
aniDur = 0;
}
// Ideally this would be done on the endEvent of the animation,
// but that doesn't seem to be supported in Webkit
setTimeout(function () {
if (cAni) { cAni.remove(); }
element.setAttribute('opacity', curShape.opacity);
element.setAttribute('style', 'pointer-events:inherit');
cleanupElement(element);
if (currentMode === 'path') {
pathActions.toEditMode(element);
} else if (curConfig.selectNew) {
selectOnly([element], true);
}
// we create the insert command that is stored on the stack
// undo means to call cmd.unapply(), redo means to call cmd.apply()
addCommandToHistory(new InsertElementCommand(element));
call('changed', [element]);
}, aniDur * 1000);
}
startTransform = null;
};
const dblClick = function (evt) {
const evtTarget = evt.target;
const parent = evtTarget.parentNode;
// Do nothing if already in current group
if (parent === currentGroup) { return; }
let mouseTarget = getMouseTarget(evt);
const {tagName} = mouseTarget;
if (tagName === 'text' && currentMode !== 'textedit') {
const pt = transformPoint(evt.pageX, evt.pageY, rootSctm);
textActions.select(mouseTarget, pt.x, pt.y);
}
if ((tagName === 'g' || tagName === 'a') &&
getRotationAngle(mouseTarget)
) {
// TODO: Allow method of in-group editing without having to do
// this (similar to editing rotated paths)
// Ungroup and regroup
pushGroupProperties(mouseTarget);
mouseTarget = selectedElements[0];
clearSelection(true);
}
// Reset context
if (currentGroup) {
leaveContext();
}
if ((parent.tagName !== 'g' && parent.tagName !== 'a') ||
parent === getCurrentDrawing().getCurrentLayer() ||
mouseTarget === selectorManager.selectorParentGroup
) {
// Escape from in-group edit
return;
}
setContext(mouseTarget);
};
// prevent links from being followed in the canvas
const handleLinkInCanvas = function (e) {
e.preventDefault();
return false;
};
// Added mouseup to the container here.
// TODO(codedread): Figure out why after the Closure compiler, the window mouseup is ignored.
$(container).mousedown(mouseDown).mousemove(mouseMove).click(handleLinkInCanvas).dblclick(dblClick).mouseup(mouseUp);
// $(window).mouseup(mouseUp);
// TODO(rafaelcastrocouto): User preference for shift key and zoom factor
$(container).bind('mousewheel DOMMouseScroll', function (e) {
// if (!e.shiftKey) { return; }
e.preventDefault();
const evt = e.originalEvent;
rootSctm = $('#svgcontent g')[0].getScreenCTM().inverse();
const pt = transformPoint(evt.pageX, evt.pageY, rootSctm);
const bbox = {
'x': pt.x,
'y': pt.y,
'width': 0,
'height': 0
};
const delta = (evt.wheelDelta) ? evt.wheelDelta : (evt.detail) ? -evt.detail : 0;
if (!delta) { return; }
bbox.factor = Math.max(3 / 4, Math.min(4 / 3, (delta)));
call('zoomed', bbox);
});
}());
// Group: Text edit functions
// Functions relating to editing text elements
const textActions = canvas.textActions = (function () {
let curtext;
let textinput;
let cursor;
let selblock;
let blinker;
let chardata = [];
let textbb; // , transbb;
let matrix;
let lastX, lastY;
let allowDbl;
function setCursor (index) {
const empty = (textinput.value === '');
$(textinput).focus();
if (!arguments.length) {
if (empty) {
index = 0;
} else {
if (textinput.selectionEnd !== textinput.selectionStart) { return; }
index = textinput.selectionEnd;
}
}
const charbb = chardata[index];
if (!empty) {
textinput.setSelectionRange(index, index);
}
cursor = getElem('text_cursor');
if (!cursor) {
cursor = document.createElementNS(NS.SVG, 'line');
assignAttributes(cursor, {
id: 'text_cursor',
stroke: '#333',
'stroke-width': 1
});
cursor = getElem('selectorParentGroup').appendChild(cursor);
}
if (!blinker) {
blinker = setInterval(function () {
const show = (cursor.getAttribute('display') === 'none');
cursor.setAttribute('display', show ? 'inline' : 'none');
}, 600);
}
const startPt = ptToScreen(charbb.x, textbb.y);
const endPt = ptToScreen(charbb.x, (textbb.y + textbb.height));
assignAttributes(cursor, {
x1: startPt.x,
y1: startPt.y,
x2: endPt.x,
y2: endPt.y,
visibility: 'visible',
display: 'inline'
});
if (selblock) { selblock.setAttribute('d', ''); }
}
function setSelection (start, end, skipInput) {
if (start === end) {
setCursor(end);
return;
}
if (!skipInput) {
textinput.setSelectionRange(start, end);
}
selblock = getElem('text_selectblock');
if (!selblock) {
selblock = document.createElementNS(NS.SVG, 'path');
assignAttributes(selblock, {
id: 'text_selectblock',
fill: 'green',
opacity: 0.5,
style: 'pointer-events:none'
});
getElem('selectorParentGroup').appendChild(selblock);
}
const startbb = chardata[start];
const endbb = chardata[end];
cursor.setAttribute('visibility', 'hidden');
const tl = ptToScreen(startbb.x, textbb.y),
tr = ptToScreen(startbb.x + (endbb.x - startbb.x), textbb.y),
bl = ptToScreen(startbb.x, textbb.y + textbb.height),
br = ptToScreen(startbb.x + (endbb.x - startbb.x), textbb.y + textbb.height);
const dstr = 'M' + tl.x + ',' + tl.y +
' L' + tr.x + ',' + tr.y +
' ' + br.x + ',' + br.y +
' ' + bl.x + ',' + bl.y + 'z';
assignAttributes(selblock, {
d: dstr,
'display': 'inline'
});
}
function getIndexFromPoint (mouseX, mouseY) {
// Position cursor here
const pt = svgroot.createSVGPoint();
pt.x = mouseX;
pt.y = mouseY;
// No content, so return 0
if (chardata.length === 1) { return 0; }
// Determine if cursor should be on left or right of character
let charpos = curtext.getCharNumAtPosition(pt);
if (charpos < 0) {
// Out of text range, look at mouse coords
charpos = chardata.length - 2;
if (mouseX <= chardata[0].x) {
charpos = 0;
}
} else if (charpos >= chardata.length - 2) {
charpos = chardata.length - 2;
}
const charbb = chardata[charpos];
const mid = charbb.x + (charbb.width / 2);
if (mouseX > mid) {
charpos++;
}
return charpos;
}
function setCursorFromPoint (mouseX, mouseY) {
setCursor(getIndexFromPoint(mouseX, mouseY));
}
function setEndSelectionFromPoint (x, y, apply) {
const i1 = textinput.selectionStart;
const i2 = getIndexFromPoint(x, y);
const start = Math.min(i1, i2);
const end = Math.max(i1, i2);
setSelection(start, end, !apply);
}
function screenToPt (xIn, yIn) {
const out = {
x: xIn,
y: yIn
};
out.x /= currentZoom;
out.y /= currentZoom;
if (matrix) {
const pt = transformPoint(out.x, out.y, matrix.inverse());
out.x = pt.x;
out.y = pt.y;
}
return out;
}
function ptToScreen (xIn, yIn) {
const out = {
x: xIn,
y: yIn
};
if (matrix) {
const pt = transformPoint(out.x, out.y, matrix);
out.x = pt.x;
out.y = pt.y;
}
out.x *= currentZoom;
out.y *= currentZoom;
return out;
}
/*
// Not currently in use
function hideCursor () {
if (cursor) {
cursor.setAttribute('visibility', 'hidden');
}
}
*/
function selectAll (evt) {
setSelection(0, curtext.textContent.length);
$(this).unbind(evt);
}
function selectWord (evt) {
if (!allowDbl || !curtext) { return; }
const ept = transformPoint(evt.pageX, evt.pageY, rootSctm),
mouseX = ept.x * currentZoom,
mouseY = ept.y * currentZoom;
const pt = screenToPt(mouseX, mouseY);
const index = getIndexFromPoint(pt.x, pt.y);
const str = curtext.textContent;
const first = str.substr(0, index).replace(/[a-z0-9]+$/i, '').length;
const m = str.substr(index).match(/^[a-z0-9]+/i);
const last = (m ? m[0].length : 0) + index;
setSelection(first, last);
// Set tripleclick
$(evt.target).click(selectAll);
setTimeout(function () {
$(evt.target).unbind('click', selectAll);
}, 300);
}
return {
select (target, x, y) {
curtext = target;
textActions.toEditMode(x, y);
},
start (elem) {
curtext = elem;
textActions.toEditMode();
},
mouseDown (evt, mouseTarget, startX, startY) {
const pt = screenToPt(startX, startY);
textinput.focus();
setCursorFromPoint(pt.x, pt.y);
lastX = startX;
lastY = startY;
// TODO: Find way to block native selection
},
mouseMove (mouseX, mouseY) {
const pt = screenToPt(mouseX, mouseY);
setEndSelectionFromPoint(pt.x, pt.y);
},
mouseUp (evt, mouseX, mouseY) {
const pt = screenToPt(mouseX, mouseY);
setEndSelectionFromPoint(pt.x, pt.y, true);
// TODO: Find a way to make this work: Use transformed BBox instead of evt.target
// if (lastX === mouseX && lastY === mouseY
// && !rectsIntersect(transbb, {x: pt.x, y: pt.y, width: 0, height: 0})) {
// textActions.toSelectMode(true);
// }
if (
evt.target !== curtext &&
mouseX < lastX + 2 &&
mouseX > lastX - 2 &&
mouseY < lastY + 2 &&
mouseY > lastY - 2
) {
textActions.toSelectMode(true);
}
},
setCursor,
toEditMode (x, y) {
allowDbl = false;
currentMode = 'textedit';
selectorManager.requestSelector(curtext).showGrips(false);
// Make selector group accept clicks
/* const selector = */ selectorManager.requestSelector(curtext); // Do we need this? Has side effect of setting lock, so keeping for now, but next line wasn't being used
// const sel = selector.selectorRect;
textActions.init();
$(curtext).css('cursor', 'text');
// if (supportsEditableText()) {
// curtext.setAttribute('editable', 'simple');
// return;
// }
if (!arguments.length) {
setCursor();
} else {
const pt = screenToPt(x, y);
setCursorFromPoint(pt.x, pt.y);
}
setTimeout(function () {
allowDbl = true;
}, 300);
},
toSelectMode (selectElem) {
currentMode = 'select';
clearInterval(blinker);
blinker = null;
if (selblock) { $(selblock).attr('display', 'none'); }
if (cursor) { $(cursor).attr('visibility', 'hidden'); }
$(curtext).css('cursor', 'move');
if (selectElem) {
clearSelection();
$(curtext).css('cursor', 'move');
call('selected', [curtext]);
addToSelection([curtext], true);
}
if (curtext && !curtext.textContent.length) {
// No content, so delete
canvas.deleteSelectedElements();
}
$(textinput).blur();
curtext = false;
// if (supportsEditableText()) {
// curtext.removeAttribute('editable');
// }
},
setInputElem (elem) {
textinput = elem;
// $(textinput).blur(hideCursor);
},
clear () {
if (currentMode === 'textedit') {
textActions.toSelectMode();
}
},
init (inputElem) {
if (!curtext) { return; }
let i, end;
// if (supportsEditableText()) {
// curtext.select();
// return;
// }
if (!curtext.parentNode) {
// Result of the ffClone, need to get correct element
curtext = selectedElements[0];
selectorManager.requestSelector(curtext).showGrips(false);
}
const str = curtext.textContent;
const len = str.length;
const xform = curtext.getAttribute('transform');
textbb = utilsGetBBox(curtext);
matrix = xform ? getMatrix(curtext) : null;
chardata = [];
chardata.length = len;
textinput.focus();
$(curtext).unbind('dblclick', selectWord).dblclick(selectWord);
if (!len) {
end = {x: textbb.x + (textbb.width / 2), width: 0};
}
for (i = 0; i < len; i++) {
const start = curtext.getStartPositionOfChar(i);
end = curtext.getEndPositionOfChar(i);
if (!supportsGoodTextCharPos()) {
const offset = canvas.contentW * currentZoom;
start.x -= offset;
end.x -= offset;
start.x /= currentZoom;
end.x /= currentZoom;
}
// Get a "bbox" equivalent for each character. Uses the
// bbox data of the actual text for y, height purposes
// TODO: Decide if y, width and height are actually necessary
chardata[i] = {
x: start.x,
y: textbb.y, // start.y?
width: end.x - start.x,
height: textbb.height
};
}
// Add a last bbox for cursor at end of text
chardata.push({
x: end.x,
width: 0
});
setSelection(textinput.selectionStart, textinput.selectionEnd, true);
}
};
}());
// TODO: Migrate all of this code into path.js
// Group: Path edit functions
// Functions relating to editing path elements
const pathActions = canvas.pathActions = (function () {
let subpath = false;
let newPoint, firstCtrl;
let currentPath = null;
let hasMoved = false;
drawnPath = null;
// This function converts a polyline (created by the fh_path tool) into
// a path element and coverts every three line segments into a single bezier
// curve in an attempt to smooth out the free-hand
const smoothPolylineIntoPath = function (element) {
let i;
const {points} = element;
const N = points.numberOfItems;
if (N >= 4) {
// loop through every 3 points and convert to a cubic bezier curve segment
//
// NOTE: this is cheating, it means that every 3 points has the potential to
// be a corner instead of treating each point in an equal manner. In general,
// this technique does not look that good.
//
// I am open to better ideas!
//
// Reading:
// - http://www.efg2.com/Lab/Graphics/Jean-YvesQueinecBezierCurves.htm
// - https://www.codeproject.com/KB/graphics/BezierSpline.aspx?msg=2956963
// - https://www.ian-ko.com/ET_GeoWizards/UserGuide/smooth.htm
// - https://www.cs.mtu.edu/~shene/COURSES/cs3621/NOTES/spline/Bezier/bezier-der.html
let curpos = points.getItem(0), prevCtlPt = null;
let d = [];
d.push(['M', curpos.x, ',', curpos.y, ' C'].join(''));
for (i = 1; i <= (N - 4); i += 3) {
let ct1 = points.getItem(i);
const ct2 = points.getItem(i + 1);
const end = points.getItem(i + 2);
// if the previous segment had a control point, we want to smooth out
// the control points on both sides
if (prevCtlPt) {
const newpts = pathModule.smoothControlPoints(prevCtlPt, ct1, curpos);
if (newpts && newpts.length === 2) {
const prevArr = d[d.length - 1].split(',');
prevArr[2] = newpts[0].x;
prevArr[3] = newpts[0].y;
d[d.length - 1] = prevArr.join(',');
ct1 = newpts[1];
}
}
d.push([ct1.x, ct1.y, ct2.x, ct2.y, end.x, end.y].join(','));
curpos = end;
prevCtlPt = ct2;
}
// handle remaining line segments
d.push('L');
while (i < N) {
const pt = points.getItem(i);
d.push([pt.x, pt.y].join(','));
i++;
}
d = d.join(' ');
// create new path element
element = addSvgElementFromJson({
element: 'path',
curStyles: true,
attr: {
id: getId(),
d,
fill: 'none'
}
});
// No need to call "changed", as this is already done under mouseUp
}
return element;
};
return {
mouseDown (evt, mouseTarget, startX, startY) {
let id;
if (currentMode === 'path') {
let mouseX = startX; // Was this meant to work with the other `mouseX`? (was defined globally so adding `let` to at least avoid a global)
let mouseY = startY; // Was this meant to work with the other `mouseY`? (was defined globally so adding `let` to at least avoid a global)
let x = mouseX / currentZoom,
y = mouseY / currentZoom,
stretchy = getElem('path_stretch_line');
newPoint = [x, y];
if (curConfig.gridSnapping) {
x = snapToGrid(x);
y = snapToGrid(y);
mouseX = snapToGrid(mouseX);
mouseY = snapToGrid(mouseY);
}
if (!stretchy) {
stretchy = document.createElementNS(NS.SVG, 'path');
assignAttributes(stretchy, {
id: 'path_stretch_line',
stroke: '#22C',
'stroke-width': '0.5',
fill: 'none'
});
stretchy = getElem('selectorParentGroup').appendChild(stretchy);
}
stretchy.setAttribute('display', 'inline');
let keep = null;
let index;
// if pts array is empty, create path element with M at current point
if (!drawnPath) {
const dAttr = 'M' + x + ',' + y + ' '; // Was this meant to work with the other `dAttr`? (was defined globally so adding `var` to at least avoid a global)
drawnPath = addSvgElementFromJson({
element: 'path',
curStyles: true,
attr: {
d: dAttr,
id: getNextId(),
opacity: curShape.opacity / 2
}
});
// set stretchy line to first point
stretchy.setAttribute('d', ['M', mouseX, mouseY, mouseX, mouseY].join(' '));
index = subpath ? pathModule.path.segs.length : 0;
pathModule.addPointGrip(index, mouseX, mouseY);
} else {
// determine if we clicked on an existing point
const seglist = drawnPath.pathSegList;
let i = seglist.numberOfItems;
const FUZZ = 6 / currentZoom;
let clickOnPoint = false;
while (i) {
i--;
const item = seglist.getItem(i);
const px = item.x, py = item.y;
// found a matching point
if (x >= (px - FUZZ) && x <= (px + FUZZ) &&
y >= (py - FUZZ) && y <= (py + FUZZ)
) {
clickOnPoint = true;
break;
}
}
// get path element that we are in the process of creating
id = getId();
// Remove previous path object if previously created
pathModule.removePath_(id);
const newpath = getElem(id);
let newseg;
let sSeg;
const len = seglist.numberOfItems;
// if we clicked on an existing point, then we are done this path, commit it
// (i, i+1) are the x,y that were clicked on
if (clickOnPoint) {
// if clicked on any other point but the first OR
// the first point was clicked on and there are less than 3 points
// then leave the path open
// otherwise, close the path
if (i <= 1 && len >= 2) {
// Create end segment
const absX = seglist.getItem(0).x;
const absY = seglist.getItem(0).y;
sSeg = stretchy.pathSegList.getItem(1);
if (sSeg.pathSegType === 4) {
newseg = drawnPath.createSVGPathSegLinetoAbs(absX, absY);
} else {
newseg = drawnPath.createSVGPathSegCurvetoCubicAbs(
absX,
absY,
sSeg.x1 / currentZoom,
sSeg.y1 / currentZoom,
absX,
absY
);
}
const endseg = drawnPath.createSVGPathSegClosePath();
seglist.appendItem(newseg);
seglist.appendItem(endseg);
} else if (len < 3) {
keep = false;
return keep;
}
$(stretchy).remove();
// This will signal to commit the path
// const element = newpath; // Other event handlers define own `element`, so this was probably not meant to interact with them or one which shares state (as there were none); I therefore adding a missing `var` to avoid a global
drawnPath = null;
started = false;
if (subpath) {
if (pathModule.path.matrix) {
remapElement(newpath, {}, pathModule.path.matrix.inverse());
}
const newD = newpath.getAttribute('d');
const origD = $(pathModule.path.elem).attr('d');
$(pathModule.path.elem).attr('d', origD + newD);
$(newpath).remove();
if (pathModule.path.matrix) {
pathModule.recalcRotatedPath();
}
pathModule.path.init();
pathActions.toEditMode(pathModule.path.elem);
pathModule.path.selectPt();
return false;
}
// else, create a new point, update path element
} else {
// Checks if current target or parents are #svgcontent
if (!$.contains(container, getMouseTarget(evt))) {
// Clicked outside canvas, so don't make point
console.log('Clicked outside canvas');
return false;
}
const num = drawnPath.pathSegList.numberOfItems;
const last = drawnPath.pathSegList.getItem(num - 1);
const lastx = last.x, lasty = last.y;
if (evt.shiftKey) {
const xya = snapToAngle(lastx, lasty, x, y);
({x, y} = xya);
}
// Use the segment defined by stretchy
sSeg = stretchy.pathSegList.getItem(1);
if (sSeg.pathSegType === 4) {
newseg = drawnPath.createSVGPathSegLinetoAbs(round(x), round(y));
} else {
newseg = drawnPath.createSVGPathSegCurvetoCubicAbs(
round(x),
round(y),
sSeg.x1 / currentZoom,
sSeg.y1 / currentZoom,
sSeg.x2 / currentZoom,
sSeg.y2 / currentZoom
);
}
drawnPath.pathSegList.appendItem(newseg);
x *= currentZoom;
y *= currentZoom;
// set stretchy line to latest point
stretchy.setAttribute('d', ['M', x, y, x, y].join(' '));
index = num;
if (subpath) { index += pathModule.path.segs.length; }
pathModule.addPointGrip(index, x, y);
}
// keep = true;
}
return;
}
// TODO: Make sure currentPath isn't null at this point
if (!pathModule.path) { return; }
pathModule.path.storeD();
({id} = evt.target);
let curPt;
if (id.substr(0, 14) === 'pathpointgrip_') {
// Select this point
curPt = pathModule.path.cur_pt = parseInt(id.substr(14), 10);
pathModule.path.dragging = [startX, startY];
const seg = pathModule.path.segs[curPt];
// only clear selection if shift is not pressed (otherwise, add
// node to selection)
if (!evt.shiftKey) {
if (pathModule.path.selected_pts.length <= 1 || !seg.selected) {
pathModule.path.clearSelection();
}
pathModule.path.addPtsToSelection(curPt);
} else if (seg.selected) {
pathModule.path.removePtFromSelection(curPt);
} else {
pathModule.path.addPtsToSelection(curPt);
}
} else if (id.startsWith('ctrlpointgrip_')) {
pathModule.path.dragging = [startX, startY];
const parts = id.split('_')[1].split('c');
curPt = Number(parts[0]);
const ctrlNum = Number(parts[1]);
pathModule.path.selectPt(curPt, ctrlNum);
}
// Start selection box
if (!pathModule.path.dragging) {
if (rubberBox == null) {
rubberBox = selectorManager.getRubberBandBox();
}
assignAttributes(rubberBox, {
'x': startX * currentZoom,
'y': startY * currentZoom,
'width': 0,
'height': 0,
'display': 'inline'
}, 100);
}
},
mouseMove (mouseX, mouseY) {
hasMoved = true;
if (currentMode === 'path') {
if (!drawnPath) { return; }
const seglist = drawnPath.pathSegList;
const index = seglist.numberOfItems - 1;
if (newPoint) {
// First point
// if (!index) { return; }
// Set control points
const pointGrip1 = pathModule.addCtrlGrip('1c1');
const pointGrip2 = pathModule.addCtrlGrip('0c2');
// dragging pointGrip1
pointGrip1.setAttribute('cx', mouseX);
pointGrip1.setAttribute('cy', mouseY);
pointGrip1.setAttribute('display', 'inline');
const ptX = newPoint[0];
const ptY = newPoint[1];
// set curve
// const seg = seglist.getItem(index);
const curX = mouseX / currentZoom;
const curY = mouseY / currentZoom;
const altX = (ptX + (ptX - curX));
const altY = (ptY + (ptY - curY));
pointGrip2.setAttribute('cx', altX * currentZoom);
pointGrip2.setAttribute('cy', altY * currentZoom);
pointGrip2.setAttribute('display', 'inline');
const ctrlLine = pathModule.getCtrlLine(1);
assignAttributes(ctrlLine, {
x1: mouseX,
y1: mouseY,
x2: altX * currentZoom,
y2: altY * currentZoom,
display: 'inline'
});
if (index === 0) {
firstCtrl = [mouseX, mouseY];
} else {
const last = seglist.getItem(index - 1);
let lastX = last.x;
let lastY = last.y;
if (last.pathSegType === 6) {
lastX += (lastX - last.x2);
lastY += (lastY - last.y2);
} else if (firstCtrl) {
lastX = firstCtrl[0] / currentZoom;
lastY = firstCtrl[1] / currentZoom;
}
pathModule.replacePathSeg(6, index, [ptX, ptY, lastX, lastY, altX, altY], drawnPath);
}
} else {
const stretchy = getElem('path_stretch_line');
if (stretchy) {
const prev = seglist.getItem(index);
if (prev.pathSegType === 6) {
const prevX = prev.x + (prev.x - prev.x2);
const prevY = prev.y + (prev.y - prev.y2);
pathModule.replacePathSeg(6, 1, [mouseX, mouseY, prevX * currentZoom, prevY * currentZoom, mouseX, mouseY], stretchy);
} else if (firstCtrl) {
pathModule.replacePathSeg(6, 1, [mouseX, mouseY, firstCtrl[0], firstCtrl[1], mouseX, mouseY], stretchy);
} else {
pathModule.replacePathSeg(4, 1, [mouseX, mouseY], stretchy);
}
}
}
return;
}
// if we are dragging a point, let's move it
if (pathModule.path.dragging) {
const pt = pathModule.getPointFromGrip({
x: pathModule.path.dragging[0],
y: pathModule.path.dragging[1]
}, pathModule.path);
const mpt = pathModule.getPointFromGrip({
x: mouseX,
y: mouseY
}, pathModule.path);
const diffX = mpt.x - pt.x;
const diffY = mpt.y - pt.y;
pathModule.path.dragging = [mouseX, mouseY];
if (pathModule.path.dragctrl) {
pathModule.path.moveCtrl(diffX, diffY);
} else {
pathModule.path.movePts(diffX, diffY);
}
} else {
pathModule.path.selected_pts = [];
pathModule.path.eachSeg(function (i) {
const seg = this;
if (!seg.next && !seg.prev) { return; }
// const {item} = seg;
const rbb = rubberBox.getBBox();
const pt = pathModule.getGripPt(seg);
const ptBb = {
x: pt.x,
y: pt.y,
width: 0,
height: 0
};
const sel = rectsIntersect(rbb, ptBb);
this.select(sel);
// Note that addPtsToSelection is not being run
if (sel) { pathModule.path.selected_pts.push(seg.index); }
});
}
},
mouseUp (evt, element, mouseX, mouseY) {
// Create mode
if (currentMode === 'path') {
newPoint = null;
if (!drawnPath) {
element = getElem(getId());
started = false;
firstCtrl = null;
}
return {
keep: true,
element
};
}
// Edit mode
if (pathModule.path.dragging) {
const lastPt = pathModule.path.cur_pt;
pathModule.path.dragging = false;
pathModule.path.dragctrl = false;
pathModule.path.update();
if (hasMoved) {
pathModule.path.endChanges('Move path point(s)');
}
if (!evt.shiftKey && !hasMoved) {
pathModule.path.selectPt(lastPt);
}
} else if (rubberBox && rubberBox.getAttribute('display') !== 'none') {
// Done with multi-node-select
rubberBox.setAttribute('display', 'none');
if (rubberBox.getAttribute('width') <= 2 && rubberBox.getAttribute('height') <= 2) {
pathActions.toSelectMode(evt.target);
}
// else, move back to select mode
} else {
pathActions.toSelectMode(evt.target);
}
hasMoved = false;
},
toEditMode (element) {
pathModule.path = pathModule.getPath_(element);
currentMode = 'pathedit';
clearSelection();
pathModule.path.show(true).update();
pathModule.path.oldbbox = utilsGetBBox(pathModule.path.elem);
subpath = false;
},
toSelectMode (elem) {
const selPath = (elem === pathModule.path.elem);
currentMode = 'select';
pathModule.path.show(false);
currentPath = false;
clearSelection();
if (pathModule.path.matrix) {
// Rotated, so may need to re-calculate the center
pathModule.recalcRotatedPath();
}
if (selPath) {
call('selected', [elem]);
addToSelection([elem], true);
}
},
addSubPath (on) {
if (on) {
// Internally we go into "path" mode, but in the UI it will
// still appear as if in "pathedit" mode.
currentMode = 'path';
subpath = true;
} else {
pathActions.clear(true);
pathActions.toEditMode(pathModule.path.elem);
}
},
select (target) {
if (currentPath === target) {
pathActions.toEditMode(target);
currentMode = 'pathedit';
// going into pathedit mode
} else {
currentPath = target;
}
},
reorient () {
const elem = selectedElements[0];
if (!elem) { return; }
const angle = getRotationAngle(elem);
if (angle === 0) { return; }
const batchCmd = new BatchCommand('Reorient path');
const changes = {
d: elem.getAttribute('d'),
transform: elem.getAttribute('transform')
};
batchCmd.addSubCommand(new ChangeElementCommand(elem, changes));
clearSelection();
this.resetOrientation(elem);
addCommandToHistory(batchCmd);
// Set matrix to null
pathModule.getPath_(elem).show(false).matrix = null;
this.clear();
addToSelection([elem], true);
call('changed', selectedElements);
},
clear (remove) {
currentPath = null;
if (drawnPath) {
const elem = getElem(getId());
$(getElem('path_stretch_line')).remove();
$(elem).remove();
$(getElem('pathpointgrip_container')).find('*').attr('display', 'none');
drawnPath = firstCtrl = null;
started = false;
} else if (currentMode === 'pathedit') {
this.toSelectMode();
}
if (pathModule.path) { pathModule.path.init().show(false); }
},
resetOrientation (path) {
if (path == null || path.nodeName !== 'path') { return false; }
const tlist = getTransformList(path);
const m = transformListToTransform(tlist).matrix;
tlist.clear();
path.removeAttribute('transform');
const segList = path.pathSegList;
// Opera/win/non-EN throws an error here.
// TODO: Find out why!
// Presumed fixed in Opera 10.5, so commented out for now
// try {
const len = segList.numberOfItems;
// } catch(err) {
// const fixed_d = pathActions.convertPath(path);
// path.setAttribute('d', fixed_d);
// segList = path.pathSegList;
// const len = segList.numberOfItems;
// }
// let lastX, lastY;
for (let i = 0; i < len; ++i) {
const seg = segList.getItem(i);
const type = seg.pathSegType;
if (type === 1) { continue; }
const pts = [];
$.each(['', 1, 2], function (j, n) {
const x = seg['x' + n], y = seg['y' + n];
if (x !== undefined && y !== undefined) {
const pt = transformPoint(x, y, m);
pts.splice(pts.length, 0, pt.x, pt.y);
}
});
pathModule.replacePathSeg(type, i, pts, path);
}
reorientGrads(path, m);
},
zoomChange () {
if (currentMode === 'pathedit') {
pathModule.path.update();
}
},
getNodePoint () {
const selPt = pathModule.path.selected_pts.length ? pathModule.path.selected_pts[0] : 1;
const seg = pathModule.path.segs[selPt];
return {
x: seg.item.x,
y: seg.item.y,
type: seg.type
};
},
linkControlPoints (linkPoints) {
pathModule.setLinkControlPoints(linkPoints);
},
clonePathNode () {
pathModule.path.storeD();
const selPts = pathModule.path.selected_pts;
// const {segs} = pathModule.path;
let i = selPts.length;
const nums = [];
while (i--) {
const pt = selPts[i];
pathModule.path.addSeg(pt);
nums.push(pt + i);
nums.push(pt + i + 1);
}
pathModule.path.init().addPtsToSelection(nums);
pathModule.path.endChanges('Clone path node(s)');
},
opencloseSubPath () {
const selPts = pathModule.path.selected_pts;
// Only allow one selected node for now
if (selPts.length !== 1) { return; }
const {elem} = pathModule.path;
const list = elem.pathSegList;
// const len = list.numberOfItems;
const index = selPts[0];
let openPt = null;
let startItem = null;
// Check if subpath is already open
pathModule.path.eachSeg(function (i) {
if (this.type === 2 && i <= index) {
startItem = this.item;
}
if (i <= index) { return true; }
if (this.type === 2) {
// Found M first, so open
openPt = i;
return false;
}
if (this.type === 1) {
// Found Z first, so closed
openPt = false;
return false;
}
});
if (openPt == null) {
// Single path, so close last seg
openPt = pathModule.path.segs.length - 1;
}
if (openPt !== false) {
// Close this path
// Create a line going to the previous "M"
const newseg = elem.createSVGPathSegLinetoAbs(startItem.x, startItem.y);
const closer = elem.createSVGPathSegClosePath();
if (openPt === pathModule.path.segs.length - 1) {
list.appendItem(newseg);
list.appendItem(closer);
} else {
pathModule.insertItemBefore(elem, closer, openPt);
pathModule.insertItemBefore(elem, newseg, openPt);
}
pathModule.path.init().selectPt(openPt + 1);
return;
}
// M 1,1 L 2,2 L 3,3 L 1,1 z // open at 2,2
// M 2,2 L 3,3 L 1,1
// M 1,1 L 2,2 L 1,1 z M 4,4 L 5,5 L6,6 L 5,5 z
// M 1,1 L 2,2 L 1,1 z [M 4,4] L 5,5 L(M)6,6 L 5,5 z
const seg = pathModule.path.segs[index];
if (seg.mate) {
list.removeItem(index); // Removes last "L"
list.removeItem(index); // Removes the "Z"
pathModule.path.init().selectPt(index - 1);
return;
}
let lastM, zSeg;
// Find this sub-path's closing point and remove
for (let i = 0; i < list.numberOfItems; i++) {
const item = list.getItem(i);
if (item.pathSegType === 2) {
// Find the preceding M
lastM = i;
} else if (i === index) {
// Remove it
list.removeItem(lastM);
// index--;
} else if (item.pathSegType === 1 && index < i) {
// Remove the closing seg of this subpath
zSeg = i - 1;
list.removeItem(i);
break;
}
}
let num = (index - lastM) - 1;
while (num--) {
pathModule.insertItemBefore(elem, list.getItem(lastM), zSeg);
}
const pt = list.getItem(lastM);
// Make this point the new "M"
pathModule.replacePathSeg(2, lastM, [pt.x, pt.y]);
// i = index; // i is local here, so has no effect; what was the intent for this?
pathModule.path.init().selectPt(0);
},
deletePathNode () {
if (!pathActions.canDeleteNodes) { return; }
pathModule.path.storeD();
const selPts = pathModule.path.selected_pts;
let i = selPts.length;
while (i--) {
const pt = selPts[i];
pathModule.path.deleteSeg(pt);
}
// Cleanup
const cleanup = function () {
const segList = pathModule.path.elem.pathSegList;
let len = segList.numberOfItems;
const remItems = function (pos, count) {
while (count--) {
segList.removeItem(pos);
}
};
if (len <= 1) { return true; }
while (len--) {
const item = segList.getItem(len);
if (item.pathSegType === 1) {
const prev = segList.getItem(len - 1);
const nprev = segList.getItem(len - 2);
if (prev.pathSegType === 2) {
remItems(len - 1, 2);
cleanup();
break;
} else if (nprev.pathSegType === 2) {
remItems(len - 2, 3);
cleanup();
break;
}
} else if (item.pathSegType === 2) {
if (len > 0) {
const prevType = segList.getItem(len - 1).pathSegType;
// Path has M M
if (prevType === 2) {
remItems(len - 1, 1);
cleanup();
break;
// Entire path ends with Z M
} else if (prevType === 1 && segList.numberOfItems - 1 === len) {
remItems(len, 1);
cleanup();
break;
}
}
}
}
return false;
};
cleanup();
// Completely delete a path with 1 or 0 segments
if (pathModule.path.elem.pathSegList.numberOfItems <= 1) {
pathActions.toSelectMode(pathModule.path.elem);
canvas.deleteSelectedElements();
return;
}
pathModule.path.init();
pathModule.path.clearSelection();
// TODO: Find right way to select point now
// path.selectPt(selPt);
if (window.opera) { // Opera repaints incorrectly
const cp = $(pathModule.path.elem);
cp.attr('d', cp.attr('d'));
}
pathModule.path.endChanges('Delete path node(s)');
},
smoothPolylineIntoPath,
setSegType (v) {
pathModule.path.setSegType(v);
},
moveNode (attr, newValue) {
const selPts = pathModule.path.selected_pts;
if (!selPts.length) { return; }
pathModule.path.storeD();
// Get first selected point
const seg = pathModule.path.segs[selPts[0]];
const diff = {x: 0, y: 0};
diff[attr] = newValue - seg.item[attr];
seg.move(diff.x, diff.y);
pathModule.path.endChanges('Move path point');
},
fixEnd (elem) {
// Adds an extra segment if the last seg before a Z doesn't end
// at its M point
// M0,0 L0,100 L100,100 z
const segList = elem.pathSegList;
const len = segList.numberOfItems;
let lastM;
for (let i = 0; i < len; ++i) {
const item = segList.getItem(i);
if (item.pathSegType === 2) {
lastM = item;
}
if (item.pathSegType === 1) {
const prev = segList.getItem(i - 1);
if (prev.x !== lastM.x || prev.y !== lastM.y) {
// Add an L segment here
const newseg = elem.createSVGPathSegLinetoAbs(lastM.x, lastM.y);
pathModule.insertItemBefore(elem, newseg, i);
// Can this be done better?
pathActions.fixEnd(elem);
break;
}
}
}
if (isWebkit()) { resetD(elem); }
},
// Convert a path to one with only absolute or relative values
convertPath
};
})();
// end pathActions
// Group: Serialization
// Function: removeUnusedDefElems
// Looks at DOM elements inside the to see if they are referred to,
// removes them from the DOM if they are not.
//
// Returns:
// The amount of elements that were removed
const removeUnusedDefElems = this.removeUnusedDefElems = function () {
const defs = svgcontent.getElementsByTagNameNS(NS.SVG, 'defs');
if (!defs || !defs.length) { return 0; }
// if (!defs.firstChild) { return; }
const defelemUses = [];
let numRemoved = 0;
const attrs = ['fill', 'stroke', 'filter', 'marker-start', 'marker-mid', 'marker-end'];
const alen = attrs.length;
const allEls = svgcontent.getElementsByTagNameNS(NS.SVG, '*');
const allLen = allEls.length;
let i, j;
for (i = 0; i < allLen; i++) {
const el = allEls[i];
for (j = 0; j < alen; j++) {
const ref = getUrlFromAttr(el.getAttribute(attrs[j]));
if (ref) {
defelemUses.push(ref.substr(1));
}
}
// gradients can refer to other gradients
const href = getHref(el);
if (href && href.startsWith('#')) {
defelemUses.push(href.substr(1));
}
}
const defelems = $(defs).find('linearGradient, radialGradient, filter, marker, svg, symbol');
i = defelems.length;
while (i--) {
const defelem = defelems[i];
const {id} = defelem;
if (!defelemUses.includes(id)) {
// Not found, so remove (but remember)
removedElements[id] = defelem;
defelem.parentNode.removeChild(defelem);
numRemoved++;
}
}
return numRemoved;
};
// Function: svgCanvasToString
// Main function to set up the SVG content for output
//
// Returns:
// String containing the SVG image for output
this.svgCanvasToString = function () {
// keep calling it until there are none to remove
while (removeUnusedDefElems() > 0) {}
pathActions.clear(true);
// Keep SVG-Edit comment on top
$.each(svgcontent.childNodes, function (i, node) {
if (i && node.nodeType === 8 && node.data.includes('Created with')) {
svgcontent.insertBefore(node, svgcontent.firstChild);
}
});
// Move out of in-group editing mode
if (currentGroup) {
leaveContext();
selectOnly([currentGroup]);
}
const nakedSvgs = [];
// Unwrap gsvg if it has no special attributes (only id and style)
$(svgcontent).find('g:data(gsvg)').each(function () {
const attrs = this.attributes;
let len = attrs.length;
for (let i = 0; i < len; i++) {
if (attrs[i].nodeName === 'id' || attrs[i].nodeName === 'style') {
len--;
}
}
// No significant attributes, so ungroup
if (len <= 0) {
const svg = this.firstChild;
nakedSvgs.push(svg);
$(this).replaceWith(svg);
}
});
const output = this.svgToString(svgcontent, 0);
// Rewrap gsvg
if (nakedSvgs.length) {
$(nakedSvgs).each(function () {
groupSvgElem(this);
});
}
return output;
};
// Function: svgToString
// Sub function ran on each SVG element to convert it to a string as desired
//
// Parameters:
// elem - The SVG element to convert
// indent - Integer with the amount of spaces to indent this tag
//
// Returns:
// String with the given element as an SVG tag
this.svgToString = function (elem, indent) {
const out = [];
const unit = curConfig.baseUnit;
const unitRe = new RegExp('^-?[\\d\\.]+' + unit + '$');
if (elem) {
cleanupElement(elem);
const attrs = elem.attributes;
let attr, i;
const childs = elem.childNodes;
for (i = 0; i < indent; i++) { out.push(' '); }
out.push('<'); out.push(elem.nodeName);
if (elem.id === 'svgcontent') {
// Process root element separately
const res = getResolution();
const vb = '';
// TODO: Allow this by dividing all values by current baseVal
// Note that this also means we should properly deal with this on import
// if (curConfig.baseUnit !== 'px') {
// const unit = curConfig.baseUnit;
// const unitM = getTypeMap()[unit];
// res.w = shortFloat(res.w / unitM);
// res.h = shortFloat(res.h / unitM);
// vb = ' viewBox="' + [0, 0, res.w, res.h].join(' ') + '"';
// res.w += unit;
// res.h += unit;
// }
if (unit !== 'px') {
res.w = convertUnit(res.w, unit) + unit;
res.h = convertUnit(res.h, unit) + unit;
}
out.push(' width="' + res.w + '" height="' + res.h + '"' + vb + ' xmlns="' + NS.SVG + '"');
const nsuris = {};
// Check elements for namespaces, add if found
$(elem).find('*').andSelf().each(function () {
// const el = this;
// for some elements have no attribute
const uri = this.namespaceURI;
if (uri && !nsuris[uri] && nsMap[uri] && nsMap[uri] !== 'xmlns' && nsMap[uri] !== 'xml') {
nsuris[uri] = true;
out.push(' xmlns:' + nsMap[uri] + '="' + uri + '"');
}
$.each(this.attributes, function (i, attr) {
const uri = attr.namespaceURI;
if (uri && !nsuris[uri] && nsMap[uri] !== 'xmlns' && nsMap[uri] !== 'xml') {
nsuris[uri] = true;
out.push(' xmlns:' + nsMap[uri] + '="' + uri + '"');
}
});
});
i = attrs.length;
const attrNames = ['width', 'height', 'xmlns', 'x', 'y', 'viewBox', 'id', 'overflow'];
while (i--) {
attr = attrs.item(i);
const attrVal = toXml(attr.value);
// Namespaces have already been dealt with, so skip
if (attr.nodeName.startsWith('xmlns:')) { continue; }
// only serialize attributes we don't use internally
if (attrVal !== '' && !attrNames.includes(attr.localName)) {
if (!attr.namespaceURI || nsMap[attr.namespaceURI]) {
out.push(' ');
out.push(attr.nodeName); out.push('="');
out.push(attrVal); out.push('"');
}
}
}
} else {
// Skip empty defs
if (elem.nodeName === 'defs' && !elem.firstChild) { return; }
const mozAttrs = ['-moz-math-font-style', '_moz-math-font-style'];
for (i = attrs.length - 1; i >= 0; i--) {
attr = attrs.item(i);
let attrVal = toXml(attr.value);
// remove bogus attributes added by Gecko
if (mozAttrs.includes(attr.localName)) { continue; }
if (attrVal !== '') {
if (attrVal.startsWith('pointer-events')) { continue; }
if (attr.localName === 'class' && attrVal.startsWith('se_')) { continue; }
out.push(' ');
if (attr.localName === 'd') { attrVal = pathActions.convertPath(elem, true); }
if (!isNaN(attrVal)) {
attrVal = shortFloat(attrVal);
} else if (unitRe.test(attrVal)) {
attrVal = shortFloat(attrVal) + unit;
}
// Embed images when saving
if (saveOptions.apply &&
elem.nodeName === 'image' &&
attr.localName === 'href' &&
saveOptions.images &&
saveOptions.images === 'embed'
) {
const img = encodableImages[attrVal];
if (img) { attrVal = img; }
}
// map various namespaces to our fixed namespace prefixes
// (the default xmlns attribute itself does not get a prefix)
if (!attr.namespaceURI || attr.namespaceURI === NS.SVG || nsMap[attr.namespaceURI]) {
out.push(attr.nodeName); out.push('="');
out.push(attrVal); out.push('"');
}
}
}
}
if (elem.hasChildNodes()) {
out.push('>');
indent++;
let bOneLine = false;
for (i = 0; i < childs.length; i++) {
const child = childs.item(i);
switch (child.nodeType) {
case 1: // element node
out.push('\n');
out.push(this.svgToString(childs.item(i), indent));
break;
case 3: // text node
const str = child.nodeValue.replace(/^\s+|\s+$/g, '');
if (str !== '') {
bOneLine = true;
out.push(String(toXml(str)));
}
break;
case 4: // cdata node
out.push('\n');
out.push(new Array(indent + 1).join(' '));
out.push('');
break;
case 8: // comment
out.push('\n');
out.push(new Array(indent + 1).join(' '));
out.push('');
break;
} // switch on node type
}
indent--;
if (!bOneLine) {
out.push('\n');
for (i = 0; i < indent; i++) { out.push(' '); }
}
out.push(''); out.push(elem.nodeName); out.push('>');
} else {
out.push('/>');
}
}
return out.join('');
}; // end svgToString()
// Function: embedImage
// Converts a given image file to a data URL when possible, then runs a given callback
//
// Parameters:
// val - String with the path/URL of the image
// callback - Optional function to run when image data is found, supplies the
// result (data URL or false) as first parameter.
this.embedImage = function (val, callback) {
// load in the image and once it's loaded, get the dimensions
$(new Image()).load(function () {
// create a canvas the same size as the raster image
const canvas = document.createElement('canvas');
canvas.width = this.width;
canvas.height = this.height;
// load the raster image into the canvas
canvas.getContext('2d').drawImage(this, 0, 0);
// retrieve the data: URL
try {
let urldata = ';svgedit_url=' + encodeURIComponent(val);
urldata = canvas.toDataURL().replace(';base64', urldata + ';base64');
encodableImages[val] = urldata;
} catch (e) {
encodableImages[val] = false;
}
lastGoodImgUrl = val;
if (callback) { callback(encodableImages[val]); }
}).attr('src', val);
};
// Function: setGoodImage
// Sets a given URL to be a "last good image" URL
this.setGoodImage = function (val) {
lastGoodImgUrl = val;
};
this.open = function () {
// Nothing by default, handled by optional widget/extension
};
// Function: save
// Serializes the current drawing into SVG XML text and returns it to the 'saved' handler.
// This function also includes the XML prolog. Clients of the SvgCanvas bind their save
// function to the 'saved' event.
//
// Returns:
// Nothing
this.save = function (opts) {
// remove the selected outline before serializing
clearSelection();
// Update save options if provided
if (opts) { $.extend(saveOptions, opts); }
saveOptions.apply = true;
// no need for doctype, see https://jwatt.org/svg/authoring/#doctype-declaration
const str = this.svgCanvasToString();
call('saved', str);
};
/**
* Codes only is useful for locale-independent detection
*/
function getIssues ({codesOnly = false} = {}) {
// remove the selected outline before serializing
clearSelection();
// Check for known CanVG issues
const issues = [];
// Selector and notice
const issueList = {
'feGaussianBlur': uiStrings.exportNoBlur,
'foreignObject': uiStrings.exportNoforeignObject,
'[stroke-dasharray]': uiStrings.exportNoDashArray
};
const content = $(svgcontent);
// Add font/text check if Canvas Text API is not implemented
if (!('font' in $('')[0].getContext('2d'))) {
issueList.text = uiStrings.exportNoText;
}
$.each(issueList, function (sel, descr) {
if (content.find(sel).length) {
issues.push(codesOnly ? sel : descr);
}
});
return issues;
}
// Function: rasterExport
// Generates a Data URL based on the current image, then calls "exported"
// with an object including the string, image information, and any issues found
this.rasterExport = function (imgType, quality, exportWindowName) {
const mimeType = 'image/' + imgType.toLowerCase();
const issues = getIssues();
const issueCodes = getIssues({codesOnly: true});
const str = this.svgCanvasToString();
buildCanvgCallback(function () {
const type = imgType || 'PNG';
if (!$('#export_canvas').length) {
$('', {id: 'export_canvas'}).hide().appendTo('body');
}
const c = $('#export_canvas')[0];
c.width = canvas.contentW;
c.height = canvas.contentH;
canvg(c, str, {renderCallback () {
const dataURLType = (type === 'ICO' ? 'BMP' : type).toLowerCase();
const datauri = quality ? c.toDataURL('image/' + dataURLType, quality) : c.toDataURL('image/' + dataURLType);
if (c.toBlob) {
c.toBlob(function (blob) {
const bloburl = createObjectURL(blob);
call('exported', {datauri, bloburl, svg: str, issues, issueCodes, type: imgType, mimeType, quality, exportWindowName});
}, mimeType, quality);
return;
}
const bloburl = dataURLToObjectURL(datauri);
call('exported', {datauri, bloburl, svg: str, issues, issueCodes, type: imgType, mimeType, quality, exportWindowName});
}});
})();
};
this.exportPDF = function (exportWindowName, outputType) {
const that = this;
buildJSPDFCallback(function () {
const res = getResolution();
const orientation = res.w > res.h ? 'landscape' : 'portrait';
const unit = 'pt'; // curConfig.baseUnit; // We could use baseUnit, but that is presumably not intended for export purposes
const doc = jsPDF({
orientation,
unit,
format: [res.w, res.h]
// , compressPdf: true
}); // Todo: Give options to use predefined jsPDF formats like "a4", etc. from pull-down (with option to keep customizable)
const docTitle = getDocumentTitle();
doc.setProperties({
title: docTitle /* ,
subject: '',
author: '',
keywords: '',
creator: '' */
});
const issues = getIssues();
const issueCodes = getIssues({codesOnly: true});
const str = that.svgCanvasToString();
doc.addSVG(str, 0, 0);
// doc.output('save'); // Works to open in a new
// window; todo: configure this and other export
// options to optionally work in this manner as
// opposed to opening a new tab
const obj = {svg: str, issues, issueCodes, exportWindowName};
const method = outputType || 'dataurlstring';
obj[method] = doc.output(method);
call('exportedPDF', obj);
})();
};
// Function: getSvgString
// Returns the current drawing as raw SVG XML text.
//
// Returns:
// The current drawing as raw SVG XML text.
this.getSvgString = function () {
saveOptions.apply = false;
return this.svgCanvasToString();
};
// Function: randomizeIds
// This function determines whether to use a nonce in the prefix, when
// generating IDs for future documents in SVG-Edit.
//
// Parameters:
// an optional boolean, which, if true, adds a nonce to the prefix. Thus
// svgCanvas.randomizeIds() <==> svgCanvas.randomizeIds(true)
//
// if you're controlling SVG-Edit externally, and want randomized IDs, call
// this BEFORE calling svgCanvas.setSvgString
//
this.randomizeIds = function (enableRandomization) {
if (arguments.length > 0 && enableRandomization === false) {
draw.randomizeIds(false, getCurrentDrawing());
} else {
draw.randomizeIds(true, getCurrentDrawing());
}
};
// Function: uniquifyElems
// Ensure each element has a unique ID
//
// Parameters:
// g - The parent element of the tree to give unique IDs
const uniquifyElems = this.uniquifyElems = function (g) {
const ids = {};
// TODO: Handle markers and connectors. These are not yet re-identified properly
// as their referring elements do not get remapped.
//
//
//
//
// Problem #1: if svg_1 gets renamed, we do not update the polyline's se:connector attribute
// Problem #2: if the polyline svg_7 gets renamed, we do not update the marker id nor the polyline's marker-end attribute
const refElems = ['filter', 'linearGradient', 'pattern', 'radialGradient', 'symbol', 'textPath', 'use'];
walkTree(g, function (n) {
// if it's an element node
if (n.nodeType === 1) {
// and the element has an ID
if (n.id) {
// and we haven't tracked this ID yet
if (!(n.id in ids)) {
// add this id to our map
ids[n.id] = {elem: null, attrs: [], hrefs: []};
}
ids[n.id].elem = n;
}
// now search for all attributes on this element that might refer
// to other elements
$.each(refAttrs, function (i, attr) {
const attrnode = n.getAttributeNode(attr);
if (attrnode) {
// the incoming file has been sanitized, so we should be able to safely just strip off the leading #
const url = getUrlFromAttr(attrnode.value),
refid = url ? url.substr(1) : null;
if (refid) {
if (!(refid in ids)) {
// add this id to our map
ids[refid] = {elem: null, attrs: [], hrefs: []};
}
ids[refid].attrs.push(attrnode);
}
}
});
// check xlink:href now
const href = getHref(n);
// TODO: what if an or element refers to an element internally?
if (href && refElems.includes(n.nodeName)) {
const refid = href.substr(1);
if (refid) {
if (!(refid in ids)) {
// add this id to our map
ids[refid] = {elem: null, attrs: [], hrefs: []};
}
ids[refid].hrefs.push(n);
}
}
}
});
// in ids, we now have a map of ids, elements and attributes, let's re-identify
for (const oldid in ids) {
if (!oldid) { continue; }
const {elem} = ids[oldid];
if (elem) {
const newid = getNextId();
// assign element its new id
elem.id = newid;
// remap all url() attributes
const {attrs} = ids[oldid];
let j = attrs.length;
while (j--) {
const attr = attrs[j];
attr.ownerElement.setAttribute(attr.name, 'url(#' + newid + ')');
}
// remap all href attributes
const hreffers = ids[oldid].hrefs;
let k = hreffers.length;
while (k--) {
const hreffer = hreffers[k];
setHref(hreffer, '#' + newid);
}
}
}
};
// Function setUseData
// Assigns reference data for each use element
const setUseData = this.setUseData = function (parent) {
let elems = $(parent);
if (parent.tagName !== 'use') {
elems = elems.find('use');
}
elems.each(function () {
const id = getHref(this).substr(1);
const refElem = getElem(id);
if (!refElem) { return; }
$(this).data('ref', refElem);
if (refElem.tagName === 'symbol' || refElem.tagName === 'svg') {
$(this).data('symbol', refElem).data('ref', refElem);
}
});
};
// Function convertGradients
// Converts gradients from userSpaceOnUse to objectBoundingBox
const convertGradients = this.convertGradients = function (elem) {
let elems = $(elem).find('linearGradient, radialGradient');
if (!elems.length && isWebkit()) {
// Bug in webkit prevents regular *Gradient selector search
elems = $(elem).find('*').filter(function () {
return (this.tagName.includes('Gradient'));
});
}
elems.each(function () {
const grad = this;
if ($(grad).attr('gradientUnits') === 'userSpaceOnUse') {
// TODO: Support more than one element with this ref by duplicating parent grad
const elems = $(svgcontent).find('[fill="url(#' + grad.id + ')"],[stroke="url(#' + grad.id + ')"]');
if (!elems.length) { return; }
// get object's bounding box
const bb = utilsGetBBox(elems[0]);
// This will occur if the element is inside a or a ,
// in which we shouldn't need to convert anyway.
if (!bb) { return; }
if (grad.tagName === 'linearGradient') {
const gCoords = $(grad).attr(['x1', 'y1', 'x2', 'y2']);
// If has transform, convert
const tlist = grad.gradientTransform.baseVal;
if (tlist && tlist.numberOfItems > 0) {
const m = transformListToTransform(tlist).matrix;
const pt1 = transformPoint(gCoords.x1, gCoords.y1, m);
const pt2 = transformPoint(gCoords.x2, gCoords.y2, m);
gCoords.x1 = pt1.x;
gCoords.y1 = pt1.y;
gCoords.x2 = pt2.x;
gCoords.y2 = pt2.y;
grad.removeAttribute('gradientTransform');
}
$(grad).attr({
x1: (gCoords.x1 - bb.x) / bb.width,
y1: (gCoords.y1 - bb.y) / bb.height,
x2: (gCoords.x2 - bb.x) / bb.width,
y2: (gCoords.y2 - bb.y) / bb.height
});
grad.removeAttribute('gradientUnits');
}
// else {
// Note: radialGradient elements cannot be easily converted
// because userSpaceOnUse will keep circular gradients, while
// objectBoundingBox will x/y scale the gradient according to
// its bbox.
//
// For now we'll do nothing, though we should probably have
// the gradient be updated as the element is moved, as
// inkscape/illustrator do.
//
// const gCoords = $(grad).attr(['cx', 'cy', 'r']);
//
// $(grad).attr({
// cx: (gCoords.cx - bb.x) / bb.width,
// cy: (gCoords.cy - bb.y) / bb.height,
// r: gCoords.r
// });
//
// grad.removeAttribute('gradientUnits');
// }
}
});
};
// Function: convertToGroup
// Converts selected/given or child SVG element to a group
const convertToGroup = this.convertToGroup = function (elem) {
if (!elem) {
elem = selectedElements[0];
}
const $elem = $(elem);
const batchCmd = new BatchCommand();
let ts;
if ($elem.data('gsvg')) {
// Use the gsvg as the new group
const svg = elem.firstChild;
const pt = $(svg).attr(['x', 'y']);
$(elem.firstChild.firstChild).unwrap();
$(elem).removeData('gsvg');
const tlist = getTransformList(elem);
const xform = svgroot.createSVGTransform();
xform.setTranslate(pt.x, pt.y);
tlist.appendItem(xform);
recalculateDimensions(elem);
call('selected', [elem]);
} else if ($elem.data('symbol')) {
elem = $elem.data('symbol');
ts = $elem.attr('transform');
const pos = $elem.attr(['x', 'y']);
const vb = elem.getAttribute('viewBox');
if (vb) {
const nums = vb.split(' ');
pos.x -= +nums[0];
pos.y -= +nums[1];
}
// Not ideal, but works
ts += ' translate(' + (pos.x || 0) + ',' + (pos.y || 0) + ')';
const prev = $elem.prev();
// Remove element
batchCmd.addSubCommand(new RemoveElementCommand($elem[0], $elem[0].nextSibling, $elem[0].parentNode));
$elem.remove();
// See if other elements reference this symbol
const hasMore = $(svgcontent).find('use:data(symbol)').length;
const g = svgdoc.createElementNS(NS.SVG, 'g');
const childs = elem.childNodes;
let i;
for (i = 0; i < childs.length; i++) {
g.appendChild(childs[i].cloneNode(true));
}
// Duplicate the gradients for Gecko, since they weren't included in the
if (isGecko()) {
const dupeGrads = $(findDefs()).children('linearGradient,radialGradient,pattern').clone();
$(g).append(dupeGrads);
}
if (ts) {
g.setAttribute('transform', ts);
}
const parent = elem.parentNode;
uniquifyElems(g);
// Put the dupe gradients back into (after uniquifying them)
if (isGecko()) {
$(findDefs()).append($(g).find('linearGradient,radialGradient,pattern'));
}
// now give the g itself a new id
g.id = getNextId();
prev.after(g);
if (parent) {
if (!hasMore) {
// remove symbol/svg element
const {nextSibling} = elem;
parent.removeChild(elem);
batchCmd.addSubCommand(new RemoveElementCommand(elem, nextSibling, parent));
}
batchCmd.addSubCommand(new InsertElementCommand(g));
}
setUseData(g);
if (isGecko()) {
convertGradients(findDefs());
} else {
convertGradients(g);
}
// recalculate dimensions on the top-level children so that unnecessary transforms
// are removed
walkTreePost(g, function (n) {
try {
recalculateDimensions(n);
} catch (e) {
console.log(e);
}
});
// Give ID for any visible element missing one
$(g).find(visElems).each(function () {
if (!this.id) { this.id = getNextId(); }
});
selectOnly([g]);
const cm = pushGroupProperties(g, true);
if (cm) {
batchCmd.addSubCommand(cm);
}
addCommandToHistory(batchCmd);
} else {
console.log('Unexpected element to ungroup:', elem);
}
};
//
// Function: setSvgString
// This function sets the current drawing as the input SVG XML.
//
// Parameters:
// xmlString - The SVG as XML text.
// preventUndo - Boolean (defaults to false) indicating if we want to do the
// changes without adding them to the undo stack - e.g. for initializing a
// drawing on page load.
//
// Returns:
// This function returns false if the set was unsuccessful, true otherwise.
this.setSvgString = function (xmlString, preventUndo) {
try {
// convert string into XML document
const newDoc = text2xml(xmlString);
this.prepareSvg(newDoc);
const batchCmd = new BatchCommand('Change Source');
// remove old svg document
const {nextSibling} = svgcontent;
const oldzoom = svgroot.removeChild(svgcontent);
batchCmd.addSubCommand(new RemoveElementCommand(oldzoom, nextSibling, svgroot));
// set new svg document
// If DOM3 adoptNode() available, use it. Otherwise fall back to DOM2 importNode()
if (svgdoc.adoptNode) {
svgcontent = svgdoc.adoptNode(newDoc.documentElement);
} else {
svgcontent = svgdoc.importNode(newDoc.documentElement, true);
}
svgroot.appendChild(svgcontent);
const content = $(svgcontent);
canvas.current_drawing_ = new draw.Drawing(svgcontent, idprefix);
// retrieve or set the nonce
const nonce = getCurrentDrawing().getNonce();
if (nonce) {
call('setnonce', nonce);
} else {
call('unsetnonce');
}
// change image href vals if possible
content.find('image').each(function () {
const image = this;
preventClickDefault(image);
const val = getHref(this);
if (val) {
if (val.startsWith('data:')) {
// Check if an SVG-edit data URI
const m = val.match(/svgedit_url=(.*?);/);
if (m) {
const url = decodeURIComponent(m[1]);
$(new Image()).load(function () {
image.setAttributeNS(NS.XLINK, 'xlink:href', url);
}).attr('src', url);
}
}
// Add to encodableImages if it loads
canvas.embedImage(val);
}
});
// Wrap child SVGs in group elements
content.find('svg').each(function () {
// Skip if it's in a
if ($(this).closest('defs').length) { return; }
uniquifyElems(this);
// Check if it already has a gsvg group
const pa = this.parentNode;
if (pa.childNodes.length === 1 && pa.nodeName === 'g') {
$(pa).data('gsvg', this);
pa.id = pa.id || getNextId();
} else {
groupSvgElem(this);
}
});
// For Firefox: Put all paint elems in defs
if (isGecko()) {
content.find('linearGradient, radialGradient, pattern').appendTo(findDefs());
}
// Set ref element for elements
// TODO: This should also be done if the object is re-added through "redo"
setUseData(content);
convertGradients(content[0]);
const attrs = {
id: 'svgcontent',
overflow: curConfig.show_outside_canvas ? 'visible' : 'hidden'
};
let percs = false;
// determine proper size
if (content.attr('viewBox')) {
const vb = content.attr('viewBox').split(' ');
attrs.width = vb[2];
attrs.height = vb[3];
// handle content that doesn't have a viewBox
} else {
$.each(['width', 'height'], function (i, dim) {
// Set to 100 if not given
const val = content.attr(dim) || '100%';
if (String(val).substr(-1) === '%') {
// Use user units if percentage given
percs = true;
} else {
attrs[dim] = convertToNum(dim, val);
}
});
}
// identify layers
identifyLayers();
// Give ID for any visible layer children missing one
content.children().find(visElems).each(function () {
if (!this.id) { this.id = getNextId(); }
});
// Percentage width/height, so let's base it on visible elements
if (percs) {
const bb = getStrokedBBox();
attrs.width = bb.width + bb.x;
attrs.height = bb.height + bb.y;
}
// Just in case negative numbers are given or
// result from the percs calculation
if (attrs.width <= 0) { attrs.width = 100; }
if (attrs.height <= 0) { attrs.height = 100; }
content.attr(attrs);
this.contentW = attrs.width;
this.contentH = attrs.height;
batchCmd.addSubCommand(new InsertElementCommand(svgcontent));
// update root to the correct size
const changes = content.attr(['width', 'height']);
batchCmd.addSubCommand(new ChangeElementCommand(svgroot, changes));
// reset zoom
currentZoom = 1;
// reset transform lists
resetListMap();
clearSelection();
pathModule.clearData();
svgroot.appendChild(selectorManager.selectorParentGroup);
if (!preventUndo) addCommandToHistory(batchCmd);
call('changed', [svgcontent]);
} catch (e) {
console.log(e);
return false;
}
return true;
};
// Function: importSvgString
// This function imports the input SVG XML as a in the , then adds a
// to the current layer.
//
// Parameters:
// xmlString - The SVG as XML text.
//
// Returns:
// This function returns null if the import was unsuccessful, or the element otherwise.
// TODO:
// * properly handle if namespace is introduced by imported content (must add to svgcontent
// and update all prefixes in the imported node)
// * properly handle recalculating dimensions, recalculateDimensions() doesn't handle
// arbitrary transform lists, but makes some assumptions about how the transform list
// was obtained
// * import should happen in top-left of current zoomed viewport
this.importSvgString = function (xmlString) {
let j, ts, useEl;
try {
// Get unique ID
const uid = encode64(xmlString.length + xmlString).substr(0, 32);
let useExisting = false;
// Look for symbol and make sure symbol exists in image
if (importIds[uid]) {
if ($(importIds[uid].symbol).parents('#svgroot').length) {
useExisting = true;
}
}
const batchCmd = new BatchCommand('Import Image');
let symbol;
if (useExisting) {
({symbol} = importIds[uid]);
ts = importIds[uid].xform;
} else {
// convert string into XML document
const newDoc = text2xml(xmlString);
this.prepareSvg(newDoc);
// import new svg document into our document
let svg;
// If DOM3 adoptNode() available, use it. Otherwise fall back to DOM2 importNode()
if (svgdoc.adoptNode) {
svg = svgdoc.adoptNode(newDoc.documentElement);
} else {
svg = svgdoc.importNode(newDoc.documentElement, true);
}
uniquifyElems(svg);
const innerw = convertToNum('width', svg.getAttribute('width')),
innerh = convertToNum('height', svg.getAttribute('height')),
innervb = svg.getAttribute('viewBox'),
// if no explicit viewbox, create one out of the width and height
vb = innervb ? innervb.split(' ') : [0, 0, innerw, innerh];
for (j = 0; j < 4; ++j) {
vb[j] = +(vb[j]);
}
// TODO: properly handle preserveAspectRatio
const // canvasw = +svgcontent.getAttribute('width'),
canvash = +svgcontent.getAttribute('height');
// imported content should be 1/3 of the canvas on its largest dimension
if (innerh > innerw) {
ts = 'scale(' + (canvash / 3) / vb[3] + ')';
} else {
ts = 'scale(' + (canvash / 3) / vb[2] + ')';
}
// Hack to make recalculateDimensions understand how to scale
ts = 'translate(0) ' + ts + ' translate(0)';
symbol = svgdoc.createElementNS(NS.SVG, 'symbol');
const defs = findDefs();
if (isGecko()) {
// Move all gradients into root for Firefox, workaround for this bug:
// https://bugzilla.mozilla.org/show_bug.cgi?id=353575
// TODO: Make this properly undo-able.
$(svg).find('linearGradient, radialGradient, pattern').appendTo(defs);
}
while (svg.firstChild) {
const first = svg.firstChild;
symbol.appendChild(first);
}
const attrs = svg.attributes;
for (let i = 0; i < attrs.length; i++) {
const attr = attrs[i];
symbol.setAttribute(attr.nodeName, attr.value);
}
symbol.id = getNextId();
// Store data
importIds[uid] = {
symbol,
xform: ts
};
findDefs().appendChild(symbol);
batchCmd.addSubCommand(new InsertElementCommand(symbol));
}
useEl = svgdoc.createElementNS(NS.SVG, 'use');
useEl.id = getNextId();
setHref(useEl, '#' + symbol.id);
(currentGroup || getCurrentDrawing().getCurrentLayer()).appendChild(useEl);
batchCmd.addSubCommand(new InsertElementCommand(useEl));
clearSelection();
useEl.setAttribute('transform', ts);
recalculateDimensions(useEl);
$(useEl).data('symbol', symbol).data('ref', symbol);
addToSelection([useEl]);
// TODO: Find way to add this in a recalculateDimensions-parsable way
// if (vb[0] !== 0 || vb[1] !== 0) {
// ts = 'translate(' + (-vb[0]) + ',' + (-vb[1]) + ') ' + ts;
// }
addCommandToHistory(batchCmd);
call('changed', [svgcontent]);
} catch (e) {
console.log(e);
return null;
}
// we want to return the element so we can automatically select it
return useEl;
};
// TODO(codedread): Move all layer/context functions in draw.js
// Layer API Functions
// Group: Layers
// Function: identifyLayers
// Updates layer system
const identifyLayers = canvas.identifyLayers = function () {
leaveContext();
getCurrentDrawing().identifyLayers();
};
// Function: createLayer
// Creates a new top-level layer in the drawing with the given name, sets the current layer
// to it, and then clears the selection. This function then calls the 'changed' handler.
// This is an undoable action.
//
// Parameters:
// name - The given name
this.createLayer = function (name, hrService) {
const newLayer = getCurrentDrawing().createLayer(name, historyRecordingService(hrService));
clearSelection();
call('changed', [newLayer]);
};
/**
* Creates a new top-level layer in the drawing with the given name, copies all the current layer's contents
* to it, and then clears the selection. This function then calls the 'changed' handler.
* This is an undoable action.
* @param {string} name - The given name. If the layer name exists, a new name will be generated.
* @param {svgedit.history.HistoryRecordingService} hrService - History recording service
*/
this.cloneLayer = function (name, hrService) {
// Clone the current layer and make the cloned layer the new current layer
const newLayer = getCurrentDrawing().cloneLayer(name, historyRecordingService(hrService));
clearSelection();
leaveContext();
call('changed', [newLayer]);
};
// Function: deleteCurrentLayer
// Deletes the current layer from the drawing and then clears the selection. This function
// then calls the 'changed' handler. This is an undoable action.
this.deleteCurrentLayer = function () {
let currentLayer = getCurrentDrawing().getCurrentLayer();
const {nextSibling} = currentLayer;
const parent = currentLayer.parentNode;
currentLayer = getCurrentDrawing().deleteCurrentLayer();
if (currentLayer) {
const batchCmd = new BatchCommand('Delete Layer');
// store in our Undo History
batchCmd.addSubCommand(new RemoveElementCommand(currentLayer, nextSibling, parent));
addCommandToHistory(batchCmd);
clearSelection();
call('changed', [parent]);
return true;
}
return false;
};
// Function: setCurrentLayer
// Sets the current layer. If the name is not a valid layer name, then this function returns
// false. Otherwise it returns true. This is not an undo-able action.
//
// Parameters:
// name - the name of the layer you want to switch to.
//
// Returns:
// true if the current layer was switched, otherwise false
this.setCurrentLayer = function (name) {
const result = getCurrentDrawing().setCurrentLayer(toXml(name));
if (result) {
clearSelection();
}
return result;
};
// Function: renameCurrentLayer
// Renames the current layer. If the layer name is not valid (i.e. unique), then this function
// does nothing and returns false, otherwise it returns true. This is an undo-able action.
//
// Parameters:
// newname - the new name you want to give the current layer. This name must be unique
// among all layer names.
//
// Returns:
// true if the rename succeeded, false otherwise.
this.renameCurrentLayer = function (newname) {
const drawing = getCurrentDrawing();
const layer = drawing.getCurrentLayer();
if (layer) {
const result = drawing.setCurrentLayerName(newname, historyRecordingService());
if (result) {
call('changed', [layer]);
return true;
}
}
return false;
};
// Function: setCurrentLayerPosition
// Changes the position of the current layer to the new value. If the new index is not valid,
// this function does nothing and returns false, otherwise it returns true. This is an
// undo-able action.
//
// Parameters:
// newpos - The zero-based index of the new position of the layer. This should be between
// 0 and (number of layers - 1)
//
// Returns:
// true if the current layer position was changed, false otherwise.
this.setCurrentLayerPosition = function (newpos) {
const drawing = getCurrentDrawing();
const result = drawing.setCurrentLayerPosition(newpos);
if (result) {
addCommandToHistory(new MoveElementCommand(result.currentGroup, result.oldNextSibling, svgcontent));
return true;
}
return false;
};
// Function: setLayerVisibility
// Sets the visibility of the layer. If the layer name is not valid, this function return
// false, otherwise it returns true. This is an undo-able action.
//
// Parameters:
// layername - the name of the layer to change the visibility
// bVisible - true/false, whether the layer should be visible
//
// Returns:
// true if the layer's visibility was set, false otherwise
this.setLayerVisibility = function (layername, bVisible) {
const drawing = getCurrentDrawing();
const prevVisibility = drawing.getLayerVisibility(layername);
const layer = drawing.setLayerVisibility(layername, bVisible);
if (layer) {
const oldDisplay = prevVisibility ? 'inline' : 'none';
addCommandToHistory(new ChangeElementCommand(layer, {'display': oldDisplay}, 'Layer Visibility'));
} else {
return false;
}
if (layer === drawing.getCurrentLayer()) {
clearSelection();
pathActions.clear();
}
// call('changed', [selected]);
return true;
};
// Function: moveSelectedToLayer
// Moves the selected elements to layername. If the name is not a valid layer name, then false
// is returned. Otherwise it returns true. This is an undo-able action.
//
// Parameters:
// layername - the name of the layer you want to which you want to move the selected elements
//
// Returns:
// true if the selected elements were moved to the layer, false otherwise.
this.moveSelectedToLayer = function (layername) {
// find the layer
const drawing = getCurrentDrawing();
const layer = drawing.getLayerByName(layername);
if (!layer) { return false; }
const batchCmd = new BatchCommand('Move Elements to Layer');
// loop for each selected element and move it
const selElems = selectedElements;
let i = selElems.length;
while (i--) {
const elem = selElems[i];
if (!elem) { continue; }
const oldNextSibling = elem.nextSibling;
// TODO: this is pretty brittle!
const oldLayer = elem.parentNode;
layer.appendChild(elem);
batchCmd.addSubCommand(new MoveElementCommand(elem, oldNextSibling, oldLayer));
}
addCommandToHistory(batchCmd);
return true;
};
this.mergeLayer = function (hrService) {
getCurrentDrawing().mergeLayer(historyRecordingService(hrService));
clearSelection();
leaveContext();
call('changed', [svgcontent]);
};
this.mergeAllLayers = function (hrService) {
getCurrentDrawing().mergeAllLayers(historyRecordingService(hrService));
clearSelection();
leaveContext();
call('changed', [svgcontent]);
};
// Function: leaveContext
// Return from a group context to the regular kind, make any previously
// disabled elements enabled again
const leaveContext = this.leaveContext = function () {
const len = disabledElems.length;
if (len) {
for (let i = 0; i < len; i++) {
const elem = disabledElems[i];
const orig = elData(elem, 'orig_opac');
if (orig !== 1) {
elem.setAttribute('opacity', orig);
} else {
elem.removeAttribute('opacity');
}
elem.setAttribute('style', 'pointer-events: inherit');
}
disabledElems = [];
clearSelection(true);
call('contextset', null);
}
currentGroup = null;
};
// Function: setContext
// Set the current context (for in-group editing)
const setContext = this.setContext = function (elem) {
leaveContext();
if (typeof elem === 'string') {
elem = getElem(elem);
}
// Edit inside this group
currentGroup = elem;
// Disable other elements
$(elem).parentsUntil('#svgcontent').andSelf().siblings().each(function () {
const opac = this.getAttribute('opacity') || 1;
// Store the original's opacity
elData(this, 'orig_opac', opac);
this.setAttribute('opacity', opac * 0.33);
this.setAttribute('style', 'pointer-events: none');
disabledElems.push(this);
});
clearSelection();
call('contextset', currentGroup);
};
// Group: Document functions
// Function: clear
// Clears the current document. This is not an undoable action.
this.clear = function () {
pathActions.clear();
clearSelection();
// clear the svgcontent node
canvas.clearSvgContentElement();
// create new document
canvas.current_drawing_ = new draw.Drawing(svgcontent);
// create empty first layer
canvas.createLayer('Layer 1');
// clear the undo stack
canvas.undoMgr.resetUndoStack();
// reset the selector manager
selectorManager.initGroup();
// reset the rubber band box
rubberBox = selectorManager.getRubberBandBox();
call('cleared');
};
// Function: linkControlPoints
// Alias function
this.linkControlPoints = pathActions.linkControlPoints;
// Function: getContentElem
// Returns the content DOM element
this.getContentElem = function () { return svgcontent; };
// Function: getRootElem
// Returns the root DOM element
this.getRootElem = function () { return svgroot; };
// Function: getSelectedElems
// Returns the array with selected DOM elements
this.getSelectedElems = function () { return selectedElements; };
// Function: getResolution
// Returns the current dimensions and zoom level in an object
const getResolution = this.getResolution = function () {
// const vb = svgcontent.getAttribute('viewBox').split(' ');
// return {'w':vb[2], 'h':vb[3], 'zoom': currentZoom};
const w = svgcontent.getAttribute('width') / currentZoom;
const h = svgcontent.getAttribute('height') / currentZoom;
return {
w,
h,
zoom: currentZoom
};
};
// Function: getZoom
// Returns the current zoom level
this.getZoom = function () { return currentZoom; };
// Function: getSnapToGrid
// Returns the current snap to grid setting
this.getSnapToGrid = function () { return curConfig.gridSnapping; };
// Function: getVersion
// Returns a string which describes the revision number of SvgCanvas.
this.getVersion = function () {
return 'svgcanvas.js ($Rev$)';
};
/**
* Update interface strings with given values
* @param strs - Object with strings (see locales file)
*/
this.setUiStrings = function (strs) {
Object.assign(uiStrings, strs.notification);
pathModule.setUiStrings(strs);
};
// Function: setConfig
// Update configuration options with given values
//
// Parameters:
// opts - Object with options (see curConfig for examples)
this.setConfig = function (opts) {
Object.assign(curConfig, opts);
};
// Function: getTitle
// Returns the current group/SVG's title contents
this.getTitle = function (elem) {
elem = elem || selectedElements[0];
if (!elem) { return; }
elem = $(elem).data('gsvg') || $(elem).data('symbol') || elem;
const childs = elem.childNodes;
for (let i = 0; i < childs.length; i++) {
if (childs[i].nodeName === 'title') {
return childs[i].textContent;
}
}
return '';
};
// Function: setGroupTitle
// Sets the group/SVG's title content
// TODO: Combine this with setDocumentTitle
this.setGroupTitle = function (val) {
let elem = selectedElements[0];
elem = $(elem).data('gsvg') || elem;
const ts = $(elem).children('title');
const batchCmd = new BatchCommand('Set Label');
let title;
if (!val.length) {
// Remove title element
const tsNextSibling = ts.nextSibling;
batchCmd.addSubCommand(new RemoveElementCommand(ts[0], tsNextSibling, elem));
ts.remove();
} else if (ts.length) {
// Change title contents
title = ts[0];
batchCmd.addSubCommand(new ChangeElementCommand(title, {'#text': title.textContent}));
title.textContent = val;
} else {
// Add title element
title = svgdoc.createElementNS(NS.SVG, 'title');
title.textContent = val;
$(elem).prepend(title);
batchCmd.addSubCommand(new InsertElementCommand(title));
}
addCommandToHistory(batchCmd);
};
// Function: getDocumentTitle
// Returns the current document title or an empty string if not found
const getDocumentTitle = this.getDocumentTitle = function () {
return canvas.getTitle(svgcontent);
};
// Function: setDocumentTitle
// Adds/updates a title element for the document with the given name.
// This is an undoable action
//
// Parameters:
// newtitle - String with the new title
this.setDocumentTitle = function (newtitle) {
const childs = svgcontent.childNodes;
let docTitle = false, oldTitle = '';
const batchCmd = new BatchCommand('Change Image Title');
for (let i = 0; i < childs.length; i++) {
if (childs[i].nodeName === 'title') {
docTitle = childs[i];
oldTitle = docTitle.textContent;
break;
}
}
if (!docTitle) {
docTitle = svgdoc.createElementNS(NS.SVG, 'title');
svgcontent.insertBefore(docTitle, svgcontent.firstChild);
}
if (newtitle.length) {
docTitle.textContent = newtitle;
} else {
// No title given, so element is not necessary
docTitle.parentNode.removeChild(docTitle);
}
batchCmd.addSubCommand(new ChangeElementCommand(docTitle, {'#text': oldTitle}));
addCommandToHistory(batchCmd);
};
// Function: getEditorNS
// Returns the editor's namespace URL, optionally adds it to root element
//
// Parameters:
// add - Boolean to indicate whether or not to add the namespace value
this.getEditorNS = function (add) {
if (add) {
svgcontent.setAttribute('xmlns:se', NS.SE);
}
return NS.SE;
};
// Function: setResolution
// Changes the document's dimensions to the given size
//
// Parameters:
// x - Number with the width of the new dimensions in user units.
// Can also be the string "fit" to indicate "fit to content"
// y - Number with the height of the new dimensions in user units.
//
// Returns:
// Boolean to indicate if resolution change was succesful.
// It will fail on "fit to content" option with no content to fit to.
this.setResolution = function (x, y) {
const res = getResolution();
const {w, h} = res;
let batchCmd;
if (x === 'fit') {
// Get bounding box
const bbox = getStrokedBBox();
if (bbox) {
batchCmd = new BatchCommand('Fit Canvas to Content');
const visEls = getVisibleElements();
addToSelection(visEls);
const dx = [], dy = [];
$.each(visEls, function (i, item) {
dx.push(bbox.x * -1);
dy.push(bbox.y * -1);
});
const cmd = canvas.moveSelectedElements(dx, dy, true);
batchCmd.addSubCommand(cmd);
clearSelection();
x = Math.round(bbox.width);
y = Math.round(bbox.height);
} else {
return false;
}
}
if (x !== w || y !== h) {
if (!batchCmd) {
batchCmd = new BatchCommand('Change Image Dimensions');
}
x = convertToNum('width', x);
y = convertToNum('height', y);
svgcontent.setAttribute('width', x);
svgcontent.setAttribute('height', y);
this.contentW = x;
this.contentH = y;
batchCmd.addSubCommand(new ChangeElementCommand(svgcontent, {'width': w, 'height': h}));
svgcontent.setAttribute('viewBox', [0, 0, x / currentZoom, y / currentZoom].join(' '));
batchCmd.addSubCommand(new ChangeElementCommand(svgcontent, {'viewBox': ['0 0', w, h].join(' ')}));
addCommandToHistory(batchCmd);
call('changed', [svgcontent]);
}
return true;
};
// Function: getOffset
// Returns an object with x, y values indicating the svgcontent element's
// position in the editor's canvas.
this.getOffset = function () {
return $(svgcontent).attr(['x', 'y']);
};
// Function: setBBoxZoom
// Sets the zoom level on the canvas-side based on the given value
//
// Parameters:
// val - Bounding box object to zoom to or string indicating zoom option
// editorW - Integer with the editor's workarea box's width
// editorH - Integer with the editor's workarea box's height
this.setBBoxZoom = function (val, editorW, editorH) {
let spacer = 0.85;
let bb;
const calcZoom = function (bb) {
if (!bb) { return false; }
const wZoom = Math.round((editorW / bb.width) * 100 * spacer) / 100;
const hZoom = Math.round((editorH / bb.height) * 100 * spacer) / 100;
const zoom = Math.min(wZoom, hZoom);
canvas.setZoom(zoom);
return {zoom, bbox: bb};
};
if (typeof val === 'object') {
bb = val;
if (bb.width === 0 || bb.height === 0) {
const newzoom = bb.zoom ? bb.zoom : currentZoom * bb.factor;
canvas.setZoom(newzoom);
return {'zoom': currentZoom, 'bbox': bb};
}
return calcZoom(bb);
}
switch (val) {
case 'selection':
if (!selectedElements[0]) { return; }
const selectedElems = $.map(selectedElements, function (n) { if (n) { return n; } });
bb = getStrokedBBox(selectedElems);
break;
case 'canvas':
const res = getResolution();
spacer = 0.95;
bb = {width: res.w, height: res.h, x: 0, y: 0};
break;
case 'content':
bb = getStrokedBBox();
break;
case 'layer':
bb = getStrokedBBox(getVisibleElements(getCurrentDrawing().getCurrentLayer()));
break;
default:
return;
}
return calcZoom(bb);
};
// Function: setZoom
// Sets the zoom to the given level
//
// Parameters:
// zoomlevel - Float indicating the zoom level to change to
this.setZoom = function (zoomlevel) {
const res = getResolution();
svgcontent.setAttribute('viewBox', '0 0 ' + res.w / zoomlevel + ' ' + res.h / zoomlevel);
currentZoom = zoomlevel;
$.each(selectedElements, function (i, elem) {
if (!elem) { return; }
selectorManager.requestSelector(elem).resize();
});
pathActions.zoomChange();
runExtensions('zoomChanged', zoomlevel);
};
// Function: getMode
// Returns the current editor mode string
this.getMode = function () {
return currentMode;
};
// Function: setMode
// Sets the editor's mode to the given string
//
// Parameters:
// name - String with the new mode to change to
this.setMode = function (name) {
pathActions.clear(true);
textActions.clear();
curProperties = (selectedElements[0] && selectedElements[0].nodeName === 'text') ? curText : curShape;
currentMode = name;
};
// Group: Element Styling
// Function: getColor
// Returns the current fill/stroke option
this.getColor = function (type) {
return curProperties[type];
};
// Function: setColor
// Change the current stroke/fill color/gradient value
//
// Parameters:
// type - String indicating fill or stroke
// val - The value to set the stroke attribute to
// preventUndo - Boolean indicating whether or not this should be and undoable option
this.setColor = function (type, val, preventUndo) {
curShape[type] = val;
curProperties[type + '_paint'] = {type: 'solidColor'};
const elems = [];
function addNonG (e) {
if (e.nodeName !== 'g') {
elems.push(e);
}
}
let i = selectedElements.length;
while (i--) {
const elem = selectedElements[i];
if (elem) {
if (elem.tagName === 'g') {
walkTree(elem, addNonG);
} else {
if (type === 'fill') {
if (elem.tagName !== 'polyline' && elem.tagName !== 'line') {
elems.push(elem);
}
} else {
elems.push(elem);
}
}
}
}
if (elems.length > 0) {
if (!preventUndo) {
changeSelectedAttribute(type, val, elems);
call('changed', elems);
} else {
changeSelectedAttributeNoUndo(type, val, elems);
}
}
};
// Function: setGradient
// Apply the current gradient to selected element's fill or stroke
//
// Parameters
// type - String indicating "fill" or "stroke" to apply to an element
const setGradient = this.setGradient = function (type) {
if (!curProperties[type + '_paint'] || curProperties[type + '_paint'].type === 'solidColor') { return; }
let grad = canvas[type + 'Grad'];
// find out if there is a duplicate gradient already in the defs
const duplicateGrad = findDuplicateGradient(grad);
const defs = findDefs();
// no duplicate found, so import gradient into defs
if (!duplicateGrad) {
// const origGrad = grad;
grad = defs.appendChild(svgdoc.importNode(grad, true));
// get next id and set it on the grad
grad.id = getNextId();
} else { // use existing gradient
grad = duplicateGrad;
}
canvas.setColor(type, 'url(#' + grad.id + ')');
};
// Function: findDuplicateGradient
// Check if exact gradient already exists
//
// Parameters:
// grad - The gradient DOM element to compare to others
//
// Returns:
// The existing gradient if found, null if not
const findDuplicateGradient = function (grad) {
const defs = findDefs();
const existingGrads = $(defs).find('linearGradient, radialGradient');
let i = existingGrads.length;
const radAttrs = ['r', 'cx', 'cy', 'fx', 'fy'];
while (i--) {
const og = existingGrads[i];
if (grad.tagName === 'linearGradient') {
if (grad.getAttribute('x1') !== og.getAttribute('x1') ||
grad.getAttribute('y1') !== og.getAttribute('y1') ||
grad.getAttribute('x2') !== og.getAttribute('x2') ||
grad.getAttribute('y2') !== og.getAttribute('y2')
) {
continue;
}
} else {
const gradAttrs = $(grad).attr(radAttrs);
const ogAttrs = $(og).attr(radAttrs);
let diff = false;
$.each(radAttrs, function (i, attr) {
if (gradAttrs[attr] !== ogAttrs[attr]) { diff = true; }
});
if (diff) { continue; }
}
// else could be a duplicate, iterate through stops
const stops = grad.getElementsByTagNameNS(NS.SVG, 'stop');
const ostops = og.getElementsByTagNameNS(NS.SVG, 'stop');
if (stops.length !== ostops.length) {
continue;
}
let j = stops.length;
while (j--) {
const stop = stops[j];
const ostop = ostops[j];
if (stop.getAttribute('offset') !== ostop.getAttribute('offset') ||
stop.getAttribute('stop-opacity') !== ostop.getAttribute('stop-opacity') ||
stop.getAttribute('stop-color') !== ostop.getAttribute('stop-color')) {
break;
}
}
if (j === -1) {
return og;
}
} // for each gradient in defs
return null;
};
function reorientGrads (elem, m) {
const bb = utilsGetBBox(elem);
for (let i = 0; i < 2; i++) {
const type = i === 0 ? 'fill' : 'stroke';
const attrVal = elem.getAttribute(type);
if (attrVal && attrVal.startsWith('url(')) {
const grad = getRefElem(attrVal);
if (grad.tagName === 'linearGradient') {
let x1 = grad.getAttribute('x1') || 0;
let y1 = grad.getAttribute('y1') || 0;
let x2 = grad.getAttribute('x2') || 1;
let y2 = grad.getAttribute('y2') || 0;
// Convert to USOU points
x1 = (bb.width * x1) + bb.x;
y1 = (bb.height * y1) + bb.y;
x2 = (bb.width * x2) + bb.x;
y2 = (bb.height * y2) + bb.y;
// Transform those points
const pt1 = transformPoint(x1, y1, m);
const pt2 = transformPoint(x2, y2, m);
// Convert back to BB points
const gCoords = {};
gCoords.x1 = (pt1.x - bb.x) / bb.width;
gCoords.y1 = (pt1.y - bb.y) / bb.height;
gCoords.x2 = (pt2.x - bb.x) / bb.width;
gCoords.y2 = (pt2.y - bb.y) / bb.height;
const newgrad = grad.cloneNode(true);
$(newgrad).attr(gCoords);
newgrad.id = getNextId();
findDefs().appendChild(newgrad);
elem.setAttribute(type, 'url(#' + newgrad.id + ')');
}
}
}
}
// Function: setPaint
// Set a color/gradient to a fill/stroke
//
// Parameters:
// type - String with "fill" or "stroke"
// paint - The jGraduate paint object to apply
this.setPaint = function (type, paint) {
// make a copy
const p = new $.jGraduate.Paint(paint);
this.setPaintOpacity(type, p.alpha / 100, true);
// now set the current paint object
curProperties[type + '_paint'] = p;
switch (p.type) {
case 'solidColor':
this.setColor(type, p.solidColor !== 'none' ? '#' + p.solidColor : 'none');
break;
case 'linearGradient':
case 'radialGradient':
canvas[type + 'Grad'] = p[p.type];
setGradient(type);
break;
}
};
// alias
this.setStrokePaint = function (paint) {
this.setPaint('stroke', paint);
};
this.setFillPaint = function (paint) {
this.setPaint('fill', paint);
};
// Function: getStrokeWidth
// Returns the current stroke-width value
this.getStrokeWidth = function () {
return curProperties.stroke_width;
};
// Function: setStrokeWidth
// Sets the stroke width for the current selected elements
// When attempting to set a line's width to 0, this changes it to 1 instead
//
// Parameters:
// val - A Float indicating the new stroke width value
this.setStrokeWidth = function (val) {
if (val === 0 && ['line', 'path'].includes(currentMode)) {
canvas.setStrokeWidth(1);
return;
}
curProperties.stroke_width = val;
const elems = [];
function addNonG (e) {
if (e.nodeName !== 'g') {
elems.push(e);
}
}
let i = selectedElements.length;
while (i--) {
const elem = selectedElements[i];
if (elem) {
if (elem.tagName === 'g') {
walkTree(elem, addNonG);
} else {
elems.push(elem);
}
}
}
if (elems.length > 0) {
changeSelectedAttribute('stroke-width', val, elems);
call('changed', selectedElements);
}
};
// Function: setStrokeAttr
// Set the given stroke-related attribute the given value for selected elements
//
// Parameters:
// attr - String with the attribute name
// val - String or number with the attribute value
this.setStrokeAttr = function (attr, val) {
curShape[attr.replace('-', '_')] = val;
const elems = [];
let i = selectedElements.length;
while (i--) {
const elem = selectedElements[i];
if (elem) {
if (elem.tagName === 'g') {
walkTree(elem, function (e) { if (e.nodeName !== 'g') { elems.push(e); } });
} else {
elems.push(elem);
}
}
}
if (elems.length > 0) {
changeSelectedAttribute(attr, val, elems);
call('changed', selectedElements);
}
};
// Function: getStyle
// Returns current style options
this.getStyle = function () {
return curShape;
};
// Function: getOpacity
// Returns the current opacity
this.getOpacity = function () {
return curShape.opacity;
};
// Function: setOpacity
// Sets the given opacity to the current selected elements
this.setOpacity = function (val) {
curShape.opacity = val;
changeSelectedAttribute('opacity', val);
};
// Function: getOpacity
// Returns the current fill opacity
this.getFillOpacity = function () {
return curShape.fill_opacity;
};
// Function: getStrokeOpacity
// Returns the current stroke opacity
this.getStrokeOpacity = function () {
return curShape.stroke_opacity;
};
// Function: setPaintOpacity
// Sets the current fill/stroke opacity
//
// Parameters:
// type - String with "fill" or "stroke"
// val - Float with the new opacity value
// preventUndo - Boolean indicating whether or not this should be an undoable action
this.setPaintOpacity = function (type, val, preventUndo) {
curShape[type + '_opacity'] = val;
if (!preventUndo) {
changeSelectedAttribute(type + '-opacity', val);
} else {
changeSelectedAttributeNoUndo(type + '-opacity', val);
}
};
// Function: getPaintOpacity
// Gets the current fill/stroke opacity
//
// Parameters:
// type - String with "fill" or "stroke"
this.getPaintOpacity = function (type) {
return type === 'fill' ? this.getFillOpacity() : this.getStrokeOpacity();
};
// Function: getBlur
// Gets the stdDeviation blur value of the given element
//
// Parameters:
// elem - The element to check the blur value for
this.getBlur = function (elem) {
let val = 0;
// const elem = selectedElements[0];
if (elem) {
const filterUrl = elem.getAttribute('filter');
if (filterUrl) {
const blur = getElem(elem.id + '_blur');
if (blur) {
val = blur.firstChild.getAttribute('stdDeviation');
}
}
}
return val;
};
(function () {
let curCommand = null;
let filter = null;
let filterHidden = false;
// Function: setBlurNoUndo
// Sets the stdDeviation blur value on the selected element without being undoable
//
// Parameters:
// val - The new stdDeviation value
canvas.setBlurNoUndo = function (val) {
if (!filter) {
canvas.setBlur(val);
return;
}
if (val === 0) {
// Don't change the StdDev, as that will hide the element.
// Instead, just remove the value for "filter"
changeSelectedAttributeNoUndo('filter', '');
filterHidden = true;
} else {
const elem = selectedElements[0];
if (filterHidden) {
changeSelectedAttributeNoUndo('filter', 'url(#' + elem.id + '_blur)');
}
if (isWebkit()) {
console.log('e', elem);
elem.removeAttribute('filter');
elem.setAttribute('filter', 'url(#' + elem.id + '_blur)');
}
changeSelectedAttributeNoUndo('stdDeviation', val, [filter.firstChild]);
canvas.setBlurOffsets(filter, val);
}
};
function finishChange () {
const bCmd = canvas.undoMgr.finishUndoableChange();
curCommand.addSubCommand(bCmd);
addCommandToHistory(curCommand);
curCommand = null;
filter = null;
}
// Function: setBlurOffsets
// Sets the x, y, with, height values of the filter element in order to
// make the blur not be clipped. Removes them if not neeeded
//
// Parameters:
// filter - The filter DOM element to update
// stdDev - The standard deviation value on which to base the offset size
canvas.setBlurOffsets = function (filter, stdDev) {
if (stdDev > 3) {
// TODO: Create algorithm here where size is based on expected blur
assignAttributes(filter, {
x: '-50%',
y: '-50%',
width: '200%',
height: '200%'
}, 100);
} else {
// Removing these attributes hides text in Chrome (see Issue 579)
if (!isWebkit()) {
filter.removeAttribute('x');
filter.removeAttribute('y');
filter.removeAttribute('width');
filter.removeAttribute('height');
}
}
};
// Function: setBlur
// Adds/updates the blur filter to the selected element
//
// Parameters:
// val - Float with the new stdDeviation blur value
// complete - Boolean indicating whether or not the action should be completed (to add to the undo manager)
canvas.setBlur = function (val, complete) {
if (curCommand) {
finishChange();
return;
}
// Looks for associated blur, creates one if not found
const elem = selectedElements[0];
const elemId = elem.id;
filter = getElem(elemId + '_blur');
val -= 0;
const batchCmd = new BatchCommand();
// Blur found!
if (filter) {
if (val === 0) {
filter = null;
}
} else {
// Not found, so create
const newblur = addSvgElementFromJson({ 'element': 'feGaussianBlur',
'attr': {
'in': 'SourceGraphic',
'stdDeviation': val
}
});
filter = addSvgElementFromJson({ 'element': 'filter',
'attr': {
'id': elemId + '_blur'
}
});
filter.appendChild(newblur);
findDefs().appendChild(filter);
batchCmd.addSubCommand(new InsertElementCommand(filter));
}
const changes = {filter: elem.getAttribute('filter')};
if (val === 0) {
elem.removeAttribute('filter');
batchCmd.addSubCommand(new ChangeElementCommand(elem, changes));
return;
}
changeSelectedAttribute('filter', 'url(#' + elemId + '_blur)');
batchCmd.addSubCommand(new ChangeElementCommand(elem, changes));
canvas.setBlurOffsets(filter, val);
curCommand = batchCmd;
canvas.undoMgr.beginUndoableChange('stdDeviation', [filter ? filter.firstChild : null]);
if (complete) {
canvas.setBlurNoUndo(val);
finishChange();
}
};
}());
// Function: getBold
// Check whether selected element is bold or not
//
// Returns:
// Boolean indicating whether or not element is bold
this.getBold = function () {
// should only have one element selected
const selected = selectedElements[0];
if (selected != null && selected.tagName === 'text' &&
selectedElements[1] == null) {
return (selected.getAttribute('font-weight') === 'bold');
}
return false;
};
// Function: setBold
// Make the selected element bold or normal
//
// Parameters:
// b - Boolean indicating bold (true) or normal (false)
this.setBold = function (b) {
const selected = selectedElements[0];
if (selected != null && selected.tagName === 'text' &&
selectedElements[1] == null) {
changeSelectedAttribute('font-weight', b ? 'bold' : 'normal');
}
if (!selectedElements[0].textContent) {
textActions.setCursor();
}
};
// Function: getItalic
// Check whether selected element is italic or not
//
// Returns:
// Boolean indicating whether or not element is italic
this.getItalic = function () {
const selected = selectedElements[0];
if (selected != null && selected.tagName === 'text' &&
selectedElements[1] == null) {
return (selected.getAttribute('font-style') === 'italic');
}
return false;
};
// Function: setItalic
// Make the selected element italic or normal
//
// Parameters:
// b - Boolean indicating italic (true) or normal (false)
this.setItalic = function (i) {
const selected = selectedElements[0];
if (selected != null && selected.tagName === 'text' &&
selectedElements[1] == null) {
changeSelectedAttribute('font-style', i ? 'italic' : 'normal');
}
if (!selectedElements[0].textContent) {
textActions.setCursor();
}
};
// Function: getFontFamily
// Returns the current font family
this.getFontFamily = function () {
return curText.font_family;
};
// Function: setFontFamily
// Set the new font family
//
// Parameters:
// val - String with the new font family
this.setFontFamily = function (val) {
curText.font_family = val;
changeSelectedAttribute('font-family', val);
if (selectedElements[0] && !selectedElements[0].textContent) {
textActions.setCursor();
}
};
// Function: setFontColor
// Set the new font color
//
// Parameters:
// val - String with the new font color
this.setFontColor = function (val) {
curText.fill = val;
changeSelectedAttribute('fill', val);
};
// Function: getFontColor
// Returns the current font color
this.getFontColor = function () {
return curText.fill;
};
// Function: getFontSize
// Returns the current font size
this.getFontSize = function () {
return curText.font_size;
};
// Function: setFontSize
// Applies the given font size to the selected element
//
// Parameters:
// val - Float with the new font size
this.setFontSize = function (val) {
curText.font_size = val;
changeSelectedAttribute('font-size', val);
if (!selectedElements[0].textContent) {
textActions.setCursor();
}
};
// Function: getText
// Returns the current text (textContent) of the selected element
this.getText = function () {
const selected = selectedElements[0];
if (selected == null) { return ''; }
return selected.textContent;
};
// Function: setTextContent
// Updates the text element with the given string
//
// Parameters:
// val - String with the new text
this.setTextContent = function (val) {
changeSelectedAttribute('#text', val);
textActions.init(val);
textActions.setCursor();
};
// Function: setImageURL
// Sets the new image URL for the selected image element. Updates its size if
// a new URL is given
//
// Parameters:
// val - String with the image URL/path
this.setImageURL = function (val) {
const elem = selectedElements[0];
if (!elem) { return; }
const attrs = $(elem).attr(['width', 'height']);
let setsize = (!attrs.width || !attrs.height);
const curHref = getHref(elem);
// Do nothing if no URL change or size change
if (curHref !== val) {
setsize = true;
} else if (!setsize) { return; }
const batchCmd = new BatchCommand('Change Image URL');
setHref(elem, val);
batchCmd.addSubCommand(new ChangeElementCommand(elem, {
'#href': curHref
}));
if (setsize) {
$(new Image()).load(function () {
const changes = $(elem).attr(['width', 'height']);
$(elem).attr({
width: this.width,
height: this.height
});
selectorManager.requestSelector(elem).resize();
batchCmd.addSubCommand(new ChangeElementCommand(elem, changes));
addCommandToHistory(batchCmd);
call('changed', [elem]);
}).attr('src', val);
} else {
addCommandToHistory(batchCmd);
}
};
// Function: setLinkURL
// Sets the new link URL for the selected anchor element.
//
// Parameters:
// val - String with the link URL/path
this.setLinkURL = function (val) {
let elem = selectedElements[0];
if (!elem) { return; }
if (elem.tagName !== 'a') {
// See if parent is an anchor
const parentsA = $(elem).parents('a');
if (parentsA.length) {
elem = parentsA[0];
} else {
return;
}
}
const curHref = getHref(elem);
if (curHref === val) { return; }
const batchCmd = new BatchCommand('Change Link URL');
setHref(elem, val);
batchCmd.addSubCommand(new ChangeElementCommand(elem, {
'#href': curHref
}));
addCommandToHistory(batchCmd);
};
// Function: setRectRadius
// Sets the rx & ry values to the selected rect element to change its corner radius
//
// Parameters:
// val - The new radius
this.setRectRadius = function (val) {
const selected = selectedElements[0];
if (selected != null && selected.tagName === 'rect') {
const r = selected.getAttribute('rx');
if (r !== String(val)) {
selected.setAttribute('rx', val);
selected.setAttribute('ry', val);
addCommandToHistory(new ChangeElementCommand(selected, {'rx': r, 'ry': r}, 'Radius'));
call('changed', [selected]);
}
}
};
// Function: makeHyperlink
// Wraps the selected element(s) in an anchor element or converts group to one
this.makeHyperlink = function (url) {
canvas.groupSelectedElements('a', url);
// TODO: If element is a single "g", convert to "a"
// if (selectedElements.length > 1 && selectedElements[1]) {
};
// Function: removeHyperlink
this.removeHyperlink = function () {
canvas.ungroupSelectedElement();
};
// Group: Element manipulation
// Function: setSegType
// Sets the new segment type to the selected segment(s).
//
// Parameters:
// new_type - Integer with the new segment type
// See https://www.w3.org/TR/SVG/paths.html#InterfaceSVGPathSeg for list
this.setSegType = function (newType) {
pathActions.setSegType(newType);
};
// TODO(codedread): Remove the getBBox argument and split this function into two.
// Function: convertToPath
// Convert selected element to a path, or get the BBox of an element-as-path
//
// Parameters:
// elem - The DOM element to be converted
// getBBox - Boolean on whether or not to only return the path's BBox
//
// Returns:
// If the getBBox flag is true, the resulting path's bounding box object.
// Otherwise the resulting path element is returned.
this.convertToPath = function (elem, getBBox) {
if (elem == null) {
const elems = selectedElements;
$.each(elems, function (i, elem) {
if (elem) { canvas.convertToPath(elem); }
});
return;
}
if (getBBox) {
return getBBoxOfElementAsPath(elem, addSvgElementFromJson, pathActions);
} else {
// TODO: Why is this applying attributes from curShape, then inside utilities.convertToPath it's pulling addition attributes from elem?
// TODO: If convertToPath is called with one elem, curShape and elem are probably the same; but calling with multiple is a bug or cool feature.
const attrs = {
'fill': curShape.fill,
'fill-opacity': curShape.fill_opacity,
'stroke': curShape.stroke,
'stroke-width': curShape.stroke_width,
'stroke-dasharray': curShape.stroke_dasharray,
'stroke-linejoin': curShape.stroke_linejoin,
'stroke-linecap': curShape.stroke_linecap,
'stroke-opacity': curShape.stroke_opacity,
'opacity': curShape.opacity,
'visibility': 'hidden'
};
return convertToPath(elem, attrs, addSvgElementFromJson, pathActions, clearSelection, addToSelection, history, addCommandToHistory);
}
};
// Function: changeSelectedAttributeNoUndo
// This function makes the changes to the elements. It does not add the change
// to the history stack.
//
// Parameters:
// attr - String with the attribute name
// newValue - String or number with the new attribute value
// elems - The DOM elements to apply the change to
const changeSelectedAttributeNoUndo = function (attr, newValue, elems) {
if (currentMode === 'pathedit') {
// Editing node
pathActions.moveNode(attr, newValue);
}
elems = elems || selectedElements;
let i = elems.length;
const noXYElems = ['g', 'polyline', 'path'];
const goodGAttrs = ['transform', 'opacity', 'filter'];
while (i--) {
let elem = elems[i];
if (elem == null) { continue; }
// Set x,y vals on elements that don't have them
if ((attr === 'x' || attr === 'y') && noXYElems.includes(elem.tagName)) {
const bbox = getStrokedBBox([elem]);
const diffX = attr === 'x' ? newValue - bbox.x : 0;
const diffY = attr === 'y' ? newValue - bbox.y : 0;
canvas.moveSelectedElements(diffX * currentZoom, diffY * currentZoom, true);
continue;
}
// only allow the transform/opacity/filter attribute to change on elements, slightly hacky
// TODO: FIXME: This doesn't seem right. Where's the body of this if statement?
if (elem.tagName === 'g' && goodGAttrs.includes(attr)) {}
let oldval = attr === '#text' ? elem.textContent : elem.getAttribute(attr);
if (oldval == null) { oldval = ''; }
if (oldval !== String(newValue)) {
if (attr === '#text') {
// const oldW = utilsGetBBox(elem).width;
elem.textContent = newValue;
// FF bug occurs on on rotated elements
if ((/rotate/).test(elem.getAttribute('transform'))) {
elem = ffClone(elem);
}
// Hoped to solve the issue of moving text with text-anchor="start",
// but this doesn't actually fix it. Hopefully on the right track, though. -Fyrd
// const box = getBBox(elem), left = box.x, top = box.y, {width, height} = box,
// dx = width - oldW, dy = 0;
// const angle = getRotationAngle(elem, true);
// if (angle) {
// const r = Math.sqrt(dx * dx + dy * dy);
// const theta = Math.atan2(dy, dx) - angle;
// dx = r * Math.cos(theta);
// dy = r * Math.sin(theta);
//
// elem.setAttribute('x', elem.getAttribute('x') - dx);
// elem.setAttribute('y', elem.getAttribute('y') - dy);
// }
} else if (attr === '#href') {
setHref(elem, newValue);
} else { elem.setAttribute(attr, newValue); }
// Go into "select" mode for text changes
// NOTE: Important that this happens AFTER elem.setAttribute() or else attributes like
// font-size can get reset to their old value, ultimately by svgEditor.updateContextPanel(),
// after calling textActions.toSelectMode() below
if (currentMode === 'textedit' && attr !== '#text' && elem.textContent.length) {
textActions.toSelectMode(elem);
}
// if (i === 0) {
// selectedBBoxes[0] = utilsGetBBox(elem);
// }
// Use the Firefox ffClone hack for text elements with gradients or
// where other text attributes are changed.
if (isGecko() && elem.nodeName === 'text' && (/rotate/).test(elem.getAttribute('transform'))) {
if (String(newValue).startsWith('url') || (['font-size', 'font-family', 'x', 'y'].includes(attr) && elem.textContent)) {
elem = ffClone(elem);
}
}
// Timeout needed for Opera & Firefox
// codedread: it is now possible for this function to be called with elements
// that are not in the selectedElements array, we need to only request a
// selector if the element is in that array
if (selectedElements.includes(elem)) {
setTimeout(function () {
// Due to element replacement, this element may no longer
// be part of the DOM
if (!elem.parentNode) { return; }
selectorManager.requestSelector(elem).resize();
}, 0);
}
// if this element was rotated, and we changed the position of this element
// we need to update the rotational transform attribute
const angle = getRotationAngle(elem);
if (angle !== 0 && attr !== 'transform') {
const tlist = getTransformList(elem);
let n = tlist.numberOfItems;
while (n--) {
const xform = tlist.getItem(n);
if (xform.type === 4) {
// remove old rotate
tlist.removeItem(n);
const box = utilsGetBBox(elem);
const center = transformPoint(box.x + box.width / 2, box.y + box.height / 2, transformListToTransform(tlist).matrix);
const cx = center.x,
cy = center.y;
const newrot = svgroot.createSVGTransform();
newrot.setRotate(angle, cx, cy);
tlist.insertItemBefore(newrot, n);
break;
}
}
}
} // if oldValue != newValue
} // for each elem
};
// Function: changeSelectedAttribute
// Change the given/selected element and add the original value to the history stack
// If you want to change all selectedElements, ignore the elems argument.
// If you want to change only a subset of selectedElements, then send the
// subset to this function in the elems argument.
//
// Parameters:
// attr - String with the attribute name
// newValue - String or number with the new attribute value
// elems - The DOM elements to apply the change to
const changeSelectedAttribute = this.changeSelectedAttribute = function (attr, val, elems) {
elems = elems || selectedElements;
canvas.undoMgr.beginUndoableChange(attr, elems);
// const i = elems.length;
changeSelectedAttributeNoUndo(attr, val, elems);
const batchCmd = canvas.undoMgr.finishUndoableChange();
if (!batchCmd.isEmpty()) {
addCommandToHistory(batchCmd);
}
};
// Function: deleteSelectedElements
// Removes all selected elements from the DOM and adds the change to the
// history stack
this.deleteSelectedElements = function () {
const batchCmd = new BatchCommand('Delete Elements');
const len = selectedElements.length;
const selectedCopy = []; // selectedElements is being deleted
for (let i = 0; i < len; ++i) {
const selected = selectedElements[i];
if (selected == null) { break; }
let parent = selected.parentNode;
let t = selected;
// this will unselect the element and remove the selectedOutline
selectorManager.releaseSelector(t);
// Remove the path if present.
pathModule.removePath_(t.id);
// Get the parent if it's a single-child anchor
if (parent.tagName === 'a' && parent.childNodes.length === 1) {
t = parent;
parent = parent.parentNode;
}
const {nextSibling} = t;
const elem = parent.removeChild(t);
selectedCopy.push(selected); // for the copy
batchCmd.addSubCommand(new RemoveElementCommand(elem, nextSibling, parent));
}
selectedElements = [];
if (!batchCmd.isEmpty()) { addCommandToHistory(batchCmd); }
call('changed', selectedCopy);
clearSelection();
};
/**
* Removes all selected elements from the DOM and adds the change to the
* history stack. Remembers removed elements on the clipboard
*/
this.cutSelectedElements = function () {
canvas.copySelectedElements();
canvas.deleteSelectedElements();
};
/**
* Remembers the current selected elements on the clipboard
*/
this.copySelectedElements = function () {
localStorage.setItem('svgedit_clipboard', JSON.stringify(
selectedElements.map(function (x) { return getJsonFromSvgElement(x); })
));
$('#cmenu_canvas').enableContextMenuItems('#paste,#paste_in_place');
};
this.pasteElements = function (type, x, y) {
let cb = JSON.parse(localStorage.getItem('svgedit_clipboard'));
let len = cb.length;
if (!len) { return; }
const pasted = [];
const batchCmd = new BatchCommand('Paste elements');
// const drawing = getCurrentDrawing();
const changedIDs = {};
// Recursively replace IDs and record the changes
function checkIDs (elem) {
if (elem.attr && elem.attr.id) {
changedIDs[elem.attr.id] = getNextId();
elem.attr.id = changedIDs[elem.attr.id];
}
if (elem.children) elem.children.forEach(checkIDs);
}
cb.forEach(checkIDs);
// Give extensions like the connector extension a chance to reflect new IDs and remove invalid elements
runExtensions('IDsUpdated', {elems: cb, changes: changedIDs}, true).forEach(function (extChanges) {
if (!extChanges || !('remove' in extChanges)) return;
extChanges.remove.forEach(function (removeID) {
cb = cb.filter(function (cbItem) {
return cbItem.attr.id !== removeID;
});
});
});
// Move elements to lastClickPoint
while (len--) {
const elem = cb[len];
if (!elem) { continue; }
const copy = addSvgElementFromJson(elem);
pasted.push(copy);
batchCmd.addSubCommand(new InsertElementCommand(copy));
restoreRefElems(copy);
}
selectOnly(pasted);
if (type !== 'in_place') {
let ctrX, ctrY;
if (!type) {
ctrX = lastClickPoint.x;
ctrY = lastClickPoint.y;
} else if (type === 'point') {
ctrX = x;
ctrY = y;
}
const bbox = getStrokedBBox(pasted);
const cx = ctrX - (bbox.x + bbox.width / 2),
cy = ctrY - (bbox.y + bbox.height / 2),
dx = [],
dy = [];
$.each(pasted, function (i, item) {
dx.push(cx);
dy.push(cy);
});
const cmd = canvas.moveSelectedElements(dx, dy, false);
if (cmd) batchCmd.addSubCommand(cmd);
}
addCommandToHistory(batchCmd);
call('changed', pasted);
};
// Function: groupSelectedElements
// Wraps all the selected elements in a group (g) element
// Parameters:
// type - type of element to group into, defaults to
this.groupSelectedElements = function (type, urlArg) {
if (!type) { type = 'g'; }
let cmdStr = '';
let url;
switch (type) {
case 'a': {
cmdStr = 'Make hyperlink';
url = '';
if (arguments.length > 1) {
url = urlArg;
}
break;
} default: {
type = 'g';
cmdStr = 'Group Elements';
break;
}
}
const batchCmd = new BatchCommand(cmdStr);
// create and insert the group element
const g = addSvgElementFromJson({
'element': type,
'attr': {
'id': getNextId()
}
});
if (type === 'a') {
setHref(g, url);
}
batchCmd.addSubCommand(new InsertElementCommand(g));
// now move all children into the group
let i = selectedElements.length;
while (i--) {
let elem = selectedElements[i];
if (elem == null) { continue; }
if (elem.parentNode.tagName === 'a' && elem.parentNode.childNodes.length === 1) {
elem = elem.parentNode;
}
const oldNextSibling = elem.nextSibling;
const oldParent = elem.parentNode;
g.appendChild(elem);
batchCmd.addSubCommand(new MoveElementCommand(elem, oldNextSibling, oldParent));
}
if (!batchCmd.isEmpty()) { addCommandToHistory(batchCmd); }
// update selection
selectOnly([g], true);
};
// Function: pushGroupProperties
// Pushes all appropriate parent group properties down to its children, then
// removes them from the group
const pushGroupProperties = this.pushGroupProperties = function (g, undoable) {
const children = g.childNodes;
const len = children.length;
const xform = g.getAttribute('transform');
const glist = getTransformList(g);
const m = transformListToTransform(glist).matrix;
const batchCmd = new BatchCommand('Push group properties');
// TODO: get all fill/stroke properties from the group that we are about to destroy
// "fill", "fill-opacity", "fill-rule", "stroke", "stroke-dasharray", "stroke-dashoffset",
// "stroke-linecap", "stroke-linejoin", "stroke-miterlimit", "stroke-opacity",
// "stroke-width"
// and then for each child, if they do not have the attribute (or the value is 'inherit')
// then set the child's attribute
const gangle = getRotationAngle(g);
const gattrs = $(g).attr(['filter', 'opacity']);
let gfilter, gblur, changes;
const drawing = getCurrentDrawing();
for (let i = 0; i < len; i++) {
const elem = children[i];
if (elem.nodeType !== 1) { continue; }
if (gattrs.opacity !== null && gattrs.opacity !== 1) {
// const c_opac = elem.getAttribute('opacity') || 1;
const newOpac = Math.round((elem.getAttribute('opacity') || 1) * gattrs.opacity * 100) / 100;
changeSelectedAttribute('opacity', newOpac, [elem]);
}
if (gattrs.filter) {
let cblur = this.getBlur(elem);
const origCblur = cblur;
if (!gblur) { gblur = this.getBlur(g); }
if (cblur) {
// Is this formula correct?
cblur = Number(gblur) + Number(cblur);
} else if (cblur === 0) {
cblur = gblur;
}
// If child has no current filter, get group's filter or clone it.
if (!origCblur) {
// Set group's filter to use first child's ID
if (!gfilter) {
gfilter = getRefElem(gattrs.filter);
} else {
// Clone the group's filter
gfilter = drawing.copyElem(gfilter);
findDefs().appendChild(gfilter);
}
} else {
gfilter = getRefElem(elem.getAttribute('filter'));
}
// Change this in future for different filters
const suffix = (gfilter.firstChild.tagName === 'feGaussianBlur') ? 'blur' : 'filter';
gfilter.id = elem.id + '_' + suffix;
changeSelectedAttribute('filter', 'url(#' + gfilter.id + ')', [elem]);
// Update blur value
if (cblur) {
changeSelectedAttribute('stdDeviation', cblur, [gfilter.firstChild]);
canvas.setBlurOffsets(gfilter, cblur);
}
}
let chtlist = getTransformList(elem);
// Don't process gradient transforms
if (elem.tagName.includes('Gradient')) { chtlist = null; }
// Hopefully not a problem to add this. Necessary for elements like
if (!chtlist) { continue; }
// Apparently can get get a transformlist, but we don't want it to have one!
if (elem.tagName === 'defs') { continue; }
if (glist.numberOfItems) {
// TODO: if the group's transform is just a rotate, we can always transfer the
// rotate() down to the children (collapsing consecutive rotates and factoring
// out any translates)
if (gangle && glist.numberOfItems === 1) {
// [Rg] [Rc] [Mc]
// we want [Tr] [Rc2] [Mc] where:
// - [Rc2] is at the child's current center but has the
// sum of the group and child's rotation angles
// - [Tr] is the equivalent translation that this child
// undergoes if the group wasn't there
// [Tr] = [Rg] [Rc] [Rc2_inv]
// get group's rotation matrix (Rg)
const rgm = glist.getItem(0).matrix;
// get child's rotation matrix (Rc)
let rcm = svgroot.createSVGMatrix();
const cangle = getRotationAngle(elem);
if (cangle) {
rcm = chtlist.getItem(0).matrix;
}
// get child's old center of rotation
const cbox = utilsGetBBox(elem);
const ceqm = transformListToTransform(chtlist).matrix;
const coldc = transformPoint(cbox.x + cbox.width / 2, cbox.y + cbox.height / 2, ceqm);
// sum group and child's angles
const sangle = gangle + cangle;
// get child's rotation at the old center (Rc2_inv)
const r2 = svgroot.createSVGTransform();
r2.setRotate(sangle, coldc.x, coldc.y);
// calculate equivalent translate
const trm = matrixMultiply(rgm, rcm, r2.matrix.inverse());
// set up tlist
if (cangle) {
chtlist.removeItem(0);
}
if (sangle) {
if (chtlist.numberOfItems) {
chtlist.insertItemBefore(r2, 0);
} else {
chtlist.appendItem(r2);
}
}
if (trm.e || trm.f) {
const tr = svgroot.createSVGTransform();
tr.setTranslate(trm.e, trm.f);
if (chtlist.numberOfItems) {
chtlist.insertItemBefore(tr, 0);
} else {
chtlist.appendItem(tr);
}
}
} else { // more complicated than just a rotate
// transfer the group's transform down to each child and then
// call recalculateDimensions()
const oldxform = elem.getAttribute('transform');
changes = {};
changes.transform = oldxform || '';
const newxform = svgroot.createSVGTransform();
// [ gm ] [ chm ] = [ chm ] [ gm' ]
// [ gm' ] = [ chmInv ] [ gm ] [ chm ]
const chm = transformListToTransform(chtlist).matrix,
chmInv = chm.inverse();
const gm = matrixMultiply(chmInv, m, chm);
newxform.setMatrix(gm);
chtlist.appendItem(newxform);
}
const cmd = recalculateDimensions(elem);
if (cmd) { batchCmd.addSubCommand(cmd); }
}
}
// remove transform and make it undo-able
if (xform) {
changes = {};
changes.transform = xform;
g.setAttribute('transform', '');
g.removeAttribute('transform');
batchCmd.addSubCommand(new ChangeElementCommand(g, changes));
}
if (undoable && !batchCmd.isEmpty()) {
return batchCmd;
}
};
// Function: ungroupSelectedElement
// Unwraps all the elements in a selected group (g) element. This requires
// significant recalculations to apply group's transforms, etc to its children
this.ungroupSelectedElement = function () {
let g = selectedElements[0];
if (!g) {
return;
}
if ($(g).data('gsvg') || $(g).data('symbol')) {
// Is svg, so actually convert to group
convertToGroup(g);
return;
}
if (g.tagName === 'use') {
// Somehow doesn't have data set, so retrieve
const symbol = getElem(getHref(g).substr(1));
$(g).data('symbol', symbol).data('ref', symbol);
convertToGroup(g);
return;
}
const parentsA = $(g).parents('a');
if (parentsA.length) {
g = parentsA[0];
}
// Look for parent "a"
if (g.tagName === 'g' || g.tagName === 'a') {
const batchCmd = new BatchCommand('Ungroup Elements');
const cmd = pushGroupProperties(g, true);
if (cmd) { batchCmd.addSubCommand(cmd); }
const parent = g.parentNode;
const anchor = g.nextSibling;
const children = new Array(g.childNodes.length);
let i = 0;
while (g.firstChild) {
let elem = g.firstChild;
const oldNextSibling = elem.nextSibling;
const oldParent = elem.parentNode;
// Remove child title elements
if (elem.tagName === 'title') {
const {nextSibling} = elem;
batchCmd.addSubCommand(new RemoveElementCommand(elem, nextSibling, oldParent));
oldParent.removeChild(elem);
continue;
}
children[i++] = elem = parent.insertBefore(elem, anchor);
batchCmd.addSubCommand(new MoveElementCommand(elem, oldNextSibling, oldParent));
}
// remove the group from the selection
clearSelection();
// delete the group element (but make undo-able)
const gNextSibling = g.nextSibling;
g = parent.removeChild(g);
batchCmd.addSubCommand(new RemoveElementCommand(g, gNextSibling, parent));
if (!batchCmd.isEmpty()) { addCommandToHistory(batchCmd); }
// update selection
addToSelection(children);
}
};
// Function: moveToTopSelectedElement
// Repositions the selected element to the bottom in the DOM to appear on top of
// other elements
this.moveToTopSelectedElement = function () {
const selected = selectedElements[0];
if (selected != null) {
let t = selected;
const oldParent = t.parentNode;
const oldNextSibling = t.nextSibling;
t = t.parentNode.appendChild(t);
// If the element actually moved position, add the command and fire the changed
// event handler.
if (oldNextSibling !== t.nextSibling) {
addCommandToHistory(new MoveElementCommand(t, oldNextSibling, oldParent, 'top'));
call('changed', [t]);
}
}
};
// Function: moveToBottomSelectedElement
// Repositions the selected element to the top in the DOM to appear under
// other elements
this.moveToBottomSelectedElement = function () {
const selected = selectedElements[0];
if (selected != null) {
let t = selected;
const oldParent = t.parentNode;
const oldNextSibling = t.nextSibling;
let {firstChild} = t.parentNode;
if (firstChild.tagName === 'title') {
firstChild = firstChild.nextSibling;
}
// This can probably be removed, as the defs should not ever apppear
// inside a layer group
if (firstChild.tagName === 'defs') {
firstChild = firstChild.nextSibling;
}
t = t.parentNode.insertBefore(t, firstChild);
// If the element actually moved position, add the command and fire the changed
// event handler.
if (oldNextSibling !== t.nextSibling) {
addCommandToHistory(new MoveElementCommand(t, oldNextSibling, oldParent, 'bottom'));
call('changed', [t]);
}
}
};
// Function: moveUpDownSelected
// Moves the select element up or down the stack, based on the visibly
// intersecting elements
//
// Parameters:
// dir - String that's either 'Up' or 'Down'
this.moveUpDownSelected = function (dir) {
const selected = selectedElements[0];
if (!selected) { return; }
curBBoxes = [];
let closest, foundCur;
// jQuery sorts this list
const list = $(getIntersectionList(getStrokedBBox([selected]))).toArray();
if (dir === 'Down') { list.reverse(); }
$.each(list, function () {
if (!foundCur) {
if (this === selected) {
foundCur = true;
}
return;
}
closest = this;
return false;
});
if (!closest) { return; }
const t = selected;
const oldParent = t.parentNode;
const oldNextSibling = t.nextSibling;
$(closest)[dir === 'Down' ? 'before' : 'after'](t);
// If the element actually moved position, add the command and fire the changed
// event handler.
if (oldNextSibling !== t.nextSibling) {
addCommandToHistory(new MoveElementCommand(t, oldNextSibling, oldParent, 'Move ' + dir));
call('changed', [t]);
}
};
// Function: moveSelectedElements
// Moves selected elements on the X/Y axis
//
// Parameters:
// dx - Float with the distance to move on the x-axis
// dy - Float with the distance to move on the y-axis
// undoable - Boolean indicating whether or not the action should be undoable
//
// Returns:
// Batch command for the move
this.moveSelectedElements = function (dx, dy, undoable) {
// if undoable is not sent, default to true
// if single values, scale them to the zoom
if (dx.constructor !== Array) {
dx /= currentZoom;
dy /= currentZoom;
}
undoable = undoable || true;
const batchCmd = new BatchCommand('position');
let i = selectedElements.length;
while (i--) {
const selected = selectedElements[i];
if (selected != null) {
// if (i === 0) {
// selectedBBoxes[0] = utilsGetBBox(selected);
// }
// const b = {};
// for (const j in selectedBBoxes[i]) b[j] = selectedBBoxes[i][j];
// selectedBBoxes[i] = b;
const xform = svgroot.createSVGTransform();
const tlist = getTransformList(selected);
// dx and dy could be arrays
if (dx.constructor === Array) {
// if (i === 0) {
// selectedBBoxes[0].x += dx[0];
// selectedBBoxes[0].y += dy[0];
// }
xform.setTranslate(dx[i], dy[i]);
} else {
// if (i === 0) {
// selectedBBoxes[0].x += dx;
// selectedBBoxes[0].y += dy;
// }
xform.setTranslate(dx, dy);
}
if (tlist.numberOfItems) {
tlist.insertItemBefore(xform, 0);
} else {
tlist.appendItem(xform);
}
const cmd = recalculateDimensions(selected);
if (cmd) {
batchCmd.addSubCommand(cmd);
}
selectorManager.requestSelector(selected).resize();
}
}
if (!batchCmd.isEmpty()) {
if (undoable) {
addCommandToHistory(batchCmd);
}
call('changed', selectedElements);
return batchCmd;
}
};
// Function: cloneSelectedElements
// Create deep DOM copies (clones) of all selected elements and move them slightly
// from their originals
this.cloneSelectedElements = function (x, y) {
let i, elem;
const batchCmd = new BatchCommand('Clone Elements');
// find all the elements selected (stop at first null)
const len = selectedElements.length;
function sortfunction (a, b) {
return ($(b).index() - $(a).index()); // causes an array to be sorted numerically and ascending
}
selectedElements.sort(sortfunction);
for (i = 0; i < len; ++i) {
elem = selectedElements[i];
if (elem == null) { break; }
}
// use slice to quickly get the subset of elements we need
const copiedElements = selectedElements.slice(0, i);
this.clearSelection(true);
// note that we loop in the reverse way because of the way elements are added
// to the selectedElements array (top-first)
const drawing = getCurrentDrawing();
i = copiedElements.length;
while (i--) {
// clone each element and replace it within copiedElements
elem = copiedElements[i] = drawing.copyElem(copiedElements[i]);
(currentGroup || drawing.getCurrentLayer()).appendChild(elem);
batchCmd.addSubCommand(new InsertElementCommand(elem));
}
if (!batchCmd.isEmpty()) {
addToSelection(copiedElements.reverse()); // Need to reverse for correct selection-adding
this.moveSelectedElements(x, y, false);
addCommandToHistory(batchCmd);
}
};
// Function: alignSelectedElements
// Aligns selected elements
//
// Parameters:
// type - String with single character indicating the alignment type
// relativeTo - String that must be one of the following:
// "selected", "largest", "smallest", "page"
this.alignSelectedElements = function (type, relativeTo) {
const bboxes = []; // angles = [];
const len = selectedElements.length;
if (!len) { return; }
let minx = Number.MAX_VALUE, maxx = Number.MIN_VALUE,
miny = Number.MAX_VALUE, maxy = Number.MIN_VALUE;
let curwidth = Number.MIN_VALUE, curheight = Number.MIN_VALUE;
for (let i = 0; i < len; ++i) {
if (selectedElements[i] == null) { break; }
const elem = selectedElements[i];
bboxes[i] = getStrokedBBox([elem]);
// now bbox is axis-aligned and handles rotation
switch (relativeTo) {
case 'smallest':
if (((type === 'l' || type === 'c' || type === 'r') &&
(curwidth === Number.MIN_VALUE || curwidth > bboxes[i].width)) ||
((type === 't' || type === 'm' || type === 'b') &&
(curheight === Number.MIN_VALUE || curheight > bboxes[i].height))
) {
minx = bboxes[i].x;
miny = bboxes[i].y;
maxx = bboxes[i].x + bboxes[i].width;
maxy = bboxes[i].y + bboxes[i].height;
curwidth = bboxes[i].width;
curheight = bboxes[i].height;
}
break;
case 'largest':
if (((type === 'l' || type === 'c' || type === 'r') &&
(curwidth === Number.MIN_VALUE || curwidth < bboxes[i].width)) ||
((type === 't' || type === 'm' || type === 'b') &&
(curheight === Number.MIN_VALUE || curheight < bboxes[i].height))
) {
minx = bboxes[i].x;
miny = bboxes[i].y;
maxx = bboxes[i].x + bboxes[i].width;
maxy = bboxes[i].y + bboxes[i].height;
curwidth = bboxes[i].width;
curheight = bboxes[i].height;
}
break;
default: // 'selected'
if (bboxes[i].x < minx) { minx = bboxes[i].x; }
if (bboxes[i].y < miny) { miny = bboxes[i].y; }
if (bboxes[i].x + bboxes[i].width > maxx) { maxx = bboxes[i].x + bboxes[i].width; }
if (bboxes[i].y + bboxes[i].height > maxy) { maxy = bboxes[i].y + bboxes[i].height; }
break;
}
} // loop for each element to find the bbox and adjust min/max
if (relativeTo === 'page') {
minx = 0;
miny = 0;
maxx = canvas.contentW;
maxy = canvas.contentH;
}
const dx = new Array(len);
const dy = new Array(len);
for (let i = 0; i < len; ++i) {
if (selectedElements[i] == null) { break; }
// const elem = selectedElements[i];
const bbox = bboxes[i];
dx[i] = 0;
dy[i] = 0;
switch (type) {
case 'l': // left (horizontal)
dx[i] = minx - bbox.x;
break;
case 'c': // center (horizontal)
dx[i] = (minx + maxx) / 2 - (bbox.x + bbox.width / 2);
break;
case 'r': // right (horizontal)
dx[i] = maxx - (bbox.x + bbox.width);
break;
case 't': // top (vertical)
dy[i] = miny - bbox.y;
break;
case 'm': // middle (vertical)
dy[i] = (miny + maxy) / 2 - (bbox.y + bbox.height / 2);
break;
case 'b': // bottom (vertical)
dy[i] = maxy - (bbox.y + bbox.height);
break;
}
}
this.moveSelectedElements(dx, dy);
};
// Group: Additional editor tools
this.contentW = getResolution().w;
this.contentH = getResolution().h;
// Function: updateCanvas
// Updates the editor canvas width/height/position after a zoom has occurred
//
// Parameters:
// w - Float with the new width
// h - Float with the new height
//
// Returns:
// Object with the following values:
// * x - The canvas' new x coordinate
// * y - The canvas' new y coordinate
// * oldX - The canvas' old x coordinate
// * oldY - The canvas' old y coordinate
// * d_x - The x position difference
// * d_y - The y position difference
this.updateCanvas = function (w, h) {
svgroot.setAttribute('width', w);
svgroot.setAttribute('height', h);
const bg = $('#canvasBackground')[0];
const oldX = svgcontent.getAttribute('x');
const oldY = svgcontent.getAttribute('y');
const x = (w / 2 - this.contentW * currentZoom / 2);
const y = (h / 2 - this.contentH * currentZoom / 2);
assignAttributes(svgcontent, {
width: this.contentW * currentZoom,
height: this.contentH * currentZoom,
x,
y,
viewBox: '0 0 ' + this.contentW + ' ' + this.contentH
});
assignAttributes(bg, {
width: svgcontent.getAttribute('width'),
height: svgcontent.getAttribute('height'),
x,
y
});
const bgImg = getElem('background_image');
if (bgImg) {
assignAttributes(bgImg, {
'width': '100%',
'height': '100%'
});
}
selectorManager.selectorParentGroup.setAttribute('transform', 'translate(' + x + ',' + y + ')');
runExtensions('canvasUpdated', {new_x: x, new_y: y, old_x: oldX, old_y: oldY, d_x: x - oldX, d_y: y - oldY});
return {x, y, old_x: oldX, old_y: oldY, d_x: x - oldX, d_y: y - oldY};
};
// Function: setBackground
// Set the background of the editor (NOT the actual document)
//
// Parameters:
// color - String with fill color to apply
// url - URL or path to image to use
this.setBackground = function (color, url) {
const bg = getElem('canvasBackground');
const border = $(bg).find('rect')[0];
let bgImg = getElem('background_image');
border.setAttribute('fill', color);
if (url) {
if (!bgImg) {
bgImg = svgdoc.createElementNS(NS.SVG, 'image');
assignAttributes(bgImg, {
'id': 'background_image',
'width': '100%',
'height': '100%',
'preserveAspectRatio': 'xMinYMin',
'style': 'pointer-events:none'
});
}
setHref(bgImg, url);
bg.appendChild(bgImg);
} else if (bgImg) {
bgImg.parentNode.removeChild(bgImg);
}
};
// Function: cycleElement
// Select the next/previous element within the current layer
//
// Parameters:
// next - Boolean where true = next and false = previous element
this.cycleElement = function (next) {
let num;
const curElem = selectedElements[0];
let elem = false;
const allElems = getVisibleElements(currentGroup || getCurrentDrawing().getCurrentLayer());
if (!allElems.length) { return; }
if (curElem == null) {
num = next ? allElems.length - 1 : 0;
elem = allElems[num];
} else {
let i = allElems.length;
while (i--) {
if (allElems[i] === curElem) {
num = next ? i - 1 : i + 1;
if (num >= allElems.length) {
num = 0;
} else if (num < 0) {
num = allElems.length - 1;
}
elem = allElems[num];
break;
}
}
}
selectOnly([elem], true);
call('selected', selectedElements);
};
this.clear();
// DEPRECATED: getPrivateMethods
// Since all methods are/should be public somehow, this function should be removed
// Being able to access private methods publicly seems wrong somehow,
// but currently appears to be the best way to allow testing and provide
// access to them to plugins.
this.getPrivateMethods = function () {
const obj = {
addCommandToHistory,
setGradient,
addSvgElementFromJson,
assignAttributes,
BatchCommand,
call,
ChangeElementCommand,
copyElem (elem) { return getCurrentDrawing().copyElem(elem); },
ffClone,
findDefs,
findDuplicateGradient,
getElem,
getId,
getIntersectionList,
getMouseTarget,
getNextId,
getPathBBox,
getUrlFromAttr,
hasMatrixTransform,
identifyLayers,
InsertElementCommand,
isIdentity,
logMatrix,
matrixMultiply,
MoveElementCommand,
preventClickDefault,
recalculateAllSelectedDimensions,
recalculateDimensions,
remapElement,
RemoveElementCommand,
removeUnusedDefElems,
round,
runExtensions,
sanitizeSvg,
SVGEditTransformList,
toString,
transformBox,
transformListToTransform,
transformPoint,
walkTree
};
return obj;
};
};