es6 refactor

This commit is contained in:
JFH
2021-08-29 12:52:40 +02:00
parent 2f606232ed
commit 4c8f568c6d
4 changed files with 25 additions and 34 deletions

View File

@@ -224,14 +224,12 @@ export const convertAttrs = function (element) {
const attrs = attrsToConvert[elName];
if (!attrs) { return; }
const len = attrs.length;
for (let i = 0; i < len; i++) {
const attr = attrs[i];
attrs.forEach( (attr) => {
const cur = element.getAttribute(attr);
if (cur && !isNaN(cur)) {
element.setAttribute(attr, (cur / typeMap_[unit]) + unit);
}
}
});
};
/**

View File

@@ -136,11 +136,7 @@ export default {
} catch (err) {
// Should only occur in FF which formats points attr as "n,n n,n", so just split
const ptArr = elem.getAttribute('points').split(' ');
for (let i = 0; i < ptArr.length; i++) {
if (i === pos) {
ptArr[i] = x + ',' + y;
}
}
ptArr[pos] = x + ',' + y;
elem.setAttribute('points', ptArr.join(' '));
}
@@ -208,7 +204,7 @@ export default {
let addThis;
// Grab the ends
const parts = [];
[ 'start', 'end' ].forEach(function (pos, i) {
[ 'start', 'end' ].forEach( (pos, i) => {
const key = 'c_' + pos;
let part = dataStorage.get(ethis, key);
if (part === null || part === undefined) { // Does this ever return nullish values?

View File

@@ -41,7 +41,7 @@ export default {
$id("sidepanel_content").insertAdjacentHTML( 'beforeend', propsWindowHtml );
// Define dynamic animation of the view box.
const updateViewBox = function () {
const updateViewBox = () => {
const { workarea } = svgEditor;
const portHeight = parseFloat(getComputedStyle(workarea, null).height.replace("px", ""));
const portWidth = parseFloat(getComputedStyle(workarea, null).width.replace("px", ""));

View File

@@ -51,11 +51,11 @@ export const init = function (editorContext) {
* @type {module:path.EditorContext#remapElement}
*/
export const remapElement = function (selected, changes, m) {
const remap = function (x, y) { return transformPoint(x, y, m); };
const scalew = function (w) { return m.a * w; };
const scaleh = function (h) { return m.d * h; };
const remap = (x, y) => transformPoint(x, y, m);
const scalew = (w) => m.a * w;
const scaleh = (h) => m.d * h;
const doSnapping = editorContext_.getGridSnapping() && selected.parentNode.parentNode.localName === 'svg';
const finishUp = function () {
const finishUp = () => {
if (doSnapping) {
Object.entries(changes).forEach(([ o, value ]) => {
changes[o] = snapToGrid(value);
@@ -65,8 +65,7 @@ export const remapElement = function (selected, changes, m) {
};
const box = getBBox(selected);
for (let i = 0; i < 2; i++) {
const type = i === 0 ? 'fill' : 'stroke';
[ 'fill', 'stroke' ].forEach( (type) => {
const attrVal = selected.getAttribute(type);
if (attrVal && attrVal.startsWith('url(') && (m.a < 0 || m.d < 0)) {
const grad = getRefElem(attrVal);
@@ -90,7 +89,7 @@ export const remapElement = function (selected, changes, m) {
findDefs().append(newgrad);
selected.setAttribute(type, 'url(#' + newgrad.id + ')');
}
}
});
const elName = selected.tagName;
if (elName === 'g' || elName === 'text' || elName === 'tspan' || elName === 'use') {
@@ -181,20 +180,17 @@ export const remapElement = function (selected, changes, m) {
break;
} case 'polyline':
case 'polygon': {
const len = changes.points.length;
for (let i = 0; i < len; ++i) {
const pt = changes.points[i];
changes.points.forEach( (pt) => {
const { x, y } = remap(pt.x, pt.y);
changes.points[i].x = x;
changes.points[i].y = y;
}
pt.x = x;
pt.y = y;
});
// const len = changes.points.length;
let pstr = '';
for (let i = 0; i < len; ++i) {
const pt = changes.points[i];
changes.points.forEach( (pt) => {
pstr += pt.x + ',' + pt.y + ' ';
}
});
selected.setAttribute('points', pstr);
break;
} case 'path': {
@@ -221,9 +217,12 @@ export const remapElement = function (selected, changes, m) {
len = changes.d.length;
const firstseg = changes.d[0];
const currentpt = remap(firstseg.x, firstseg.y);
changes.d[0].x = currentpt.x;
changes.d[0].y = currentpt.y;
let currentpt;
if (len > 0) {
currentpt = remap(firstseg.x, firstseg.y);
changes.d[0].x = currentpt.x;
changes.d[0].y = currentpt.y;
}
for (let i = 1; i < len; ++i) {
const seg = changes.d[i];
const { type } = seg;
@@ -256,9 +255,7 @@ export const remapElement = function (selected, changes, m) {
} // for each segment
let dstr = '';
len = changes.d.length;
for (let i = 0; i < len; ++i) {
const seg = changes.d[i];
changes.d.forEach( (seg) => {
const { type } = seg;
dstr += pathMap[type];
switch (type) {
@@ -297,7 +294,7 @@ export const remapElement = function (selected, changes, m) {
dstr += seg.x2 + ',' + seg.y2 + ' ' + seg.x + ',' + seg.y + ' ';
break;
}
}
});
selected.setAttribute('d', dstr);
break;