- Breaking change: Rename "svgutils.js" to "utilities.js" (make in conformity with JSDoc module naming convention) - Breaking change: Rename "svgedit.js" to "namespaces.js" (to make clear purpose and avoid confusing with editor) - Breaking change: Rename "jquery-svg.js" to "jQuery.attr.js" - Breaking change: Rename "jquery.contextMenu.js" to "jQuery.contextMenu.js" - Breaking change: Rename "jquery.jpicker.js" to "jQuery.jPicker.js" - Breaking change: Rename "JQuerySpinBtn.css" to "jQuery.SpinButton.css" - Breaking change: Rename "JQuerySpinBtn.js" to "jQuery.SpinButton.js" (to have file name more closely reflect name) - Breaking change: Rename "jquery.svgicons.js" to "jQuery.svgIcons.js" - Breaking change: Rename "jquery.jgraduate.js" to "jQuery.jGraduate.js" - Breaking change: Rename "pathseg.js" to "svgpathseg.js" (as it is a poyfill of SVGPathSeg) - Breaking change: Rename `addSvgElementFromJson()` to `addSVGElementFromJson` for consistency - Breaking change: Rename `changeSvgContent()` to `changeSVGContent()` for consistency - Breaking change: Have `exportPDF` resolve with `output` and `outputType` rather than `dataurlstring` (as type may vary) - Breaking change: Rename `extensions/mathjax/MathJax.js` to `extensions/mathjax/MathJax.min.js` - Breaking change: Avoid recent change to have editor ready callbacks return Promises (we're not using and advantageous to keep sequential) - Breaking change: Avoid recent addition of locale-side function in ext-imagelib for l10n - Breaking change: Change name of ext-arrows.js from `Arrows` to `arrows` for sake of file path (not localized anyways). - Breaking change: Change `addlangData` extension event to `addLangData` for consistency with method name - Breaking change: Have `readLang` return lang and data but do not call `setLang` - Fix: Have general locales load first so extensions may use - Fix: Provide `importLocale` to extensions `init` so it may delay adding of the extension until locale data loaded - Fix: Ensure call to `rasterExport` without `imgType` properly sets MIME type to PNG - Fix: Wrong name for moinsave - Update: Update WebAppFind per new API changes - Enhancement: Make `setStrings` public on editor for late setting (used by `ext-shapes.js`) - Enhancement: Add `extensions_added` event - Enhancement: Add `message` event (Relay messages including those which have been been received prior to extension load) - Enhancement: Allow SVGEdit to work out of the box--avoid need for copying sample config file. Should also help with Github-based file servers - Enhancement: Allow avoiding "name" in extension export (just extract out of file name) - Enhancement: Add stack blur to canvg by default (and refactoring it) - Enhancement: Return `Promise` for `embedImage` (as with some other loading methods) - Enhancement: Supply `importLocale` to `langReady` to facilitate extension locale loading - Enhancement: Recover if an extension fails to load (just log and otherwise ignore) - Enhancement: More i18n of extensions (also fixed issue with some console warnings about missing locale strings); i18nize Hello World too - Enhancement: Allowing importing of locales within `addLangData` - npm: Update devDeps - Docs: Migrate copies of all old wiki pages to docs/from-old-wiki folder; intended for a possible move to Markdown, so raw HTML (with formatting) was not preserved, though named links had their absolute URL links preserved - Docs: Begin deleting `SvgCanvas.md` as ensuring jsdoc has replacements - Docs: Add Edtior doc file for help to general users - Docs: Clarify/simplify install instructions - npm/Docs (JSDoc): Add script to check for overly generic types - Docs (JSDoc): For config/prefs and extension creating, link to tutorials (moved tutorials to own directory to avoid recursion problems by jsdoc) - Docs (JSDoc): Add modules (upper case for usual main entrance files or regular names) - Docs (JSDoc): Fill out missing areas; indicate return of `undefined`; consistency with `@returns` - Docs (JSDoc): Add our own layout template to support overflow - Docs (JSDoc): Use cleverLinks and disallow unknown tags - Docs (JSDoc): Insist on "pedantic" flag; put output directory in config - Docs (JSDoc): Use more precise Integer/Float over number, the specific type of array/function/object - Docs (JSDoc): Use `@throws`, `@enum`, `@event`/`@fires`/`@listens` - Docs: Generally update/improve docs (fixes #92) - Docs: Update links to `latest` path (Avoid needing to update such references upon each release) - Docs: 80 chars max - Refactoring: Drop code for extension as function (already requiring export to be an object) - Refactoring: Object destructuring, `Object.entries`, Object shorthand, array extras, more camelCase variable names - Refactoring: Add a `Command` base class - Refactoring: Simplify svgicons `callback` ready detection - Refactoring: Put `let` or `const` closer to scope - Refactoring: Remove unneeded `delimiter` from regex escaping utility - Refactoring: Clearer variable names - Refactoring: Use (non-deprecated) Event constructors - Testing: Use new Sinon
331 lines
13 KiB
JavaScript
331 lines
13 KiB
JavaScript
var svgEditorExtension_polygon = (function () {
|
|
'use strict';
|
|
|
|
var asyncToGenerator = function (fn) {
|
|
return function () {
|
|
var gen = fn.apply(this, arguments);
|
|
return new Promise(function (resolve, reject) {
|
|
function step(key, arg) {
|
|
try {
|
|
var info = gen[key](arg);
|
|
var value = info.value;
|
|
} catch (error) {
|
|
reject(error);
|
|
return;
|
|
}
|
|
|
|
if (info.done) {
|
|
resolve(value);
|
|
} else {
|
|
return Promise.resolve(value).then(function (value) {
|
|
step("next", value);
|
|
}, function (err) {
|
|
step("throw", err);
|
|
});
|
|
}
|
|
}
|
|
|
|
return step("next");
|
|
});
|
|
};
|
|
};
|
|
|
|
/* globals jQuery */
|
|
/**
|
|
* ext-polygon.js
|
|
*
|
|
*
|
|
* @copyright 2010 CloudCanvas, Inc. All rights reserved
|
|
*
|
|
*/
|
|
var extPolygon = {
|
|
name: 'polygon',
|
|
init: function () {
|
|
var _ref = asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(S) {
|
|
var svgEditor, $, svgCanvas, importLocale, editingitex, strings, selElems, started, newFO, showPanel, setAttr, cot, sec, buttons, contextTools;
|
|
return regeneratorRuntime.wrap(function _callee$(_context) {
|
|
while (1) {
|
|
switch (_context.prev = _context.next) {
|
|
case 0:
|
|
sec = function sec(n) {
|
|
return 1 / Math.cos(n);
|
|
};
|
|
|
|
cot = function cot(n) {
|
|
return 1 / Math.tan(n);
|
|
};
|
|
|
|
setAttr = function setAttr(attr, val) {
|
|
svgCanvas.changeSelectedAttribute(attr, val);
|
|
S.call('changed', selElems);
|
|
};
|
|
|
|
showPanel = function showPanel(on) {
|
|
var fcRules = $('#fc_rules');
|
|
if (!fcRules.length) {
|
|
fcRules = $('<style id="fc_rules"></style>').appendTo('head');
|
|
}
|
|
fcRules.text(!on ? '' : ' #tool_topath { display: none !important; }');
|
|
$('#polygon_panel').toggle(on);
|
|
};
|
|
|
|
svgEditor = this;
|
|
$ = jQuery;
|
|
svgCanvas = svgEditor.canvas;
|
|
importLocale = S.importLocale, editingitex = false;
|
|
_context.next = 10;
|
|
return importLocale();
|
|
|
|
case 10:
|
|
strings = _context.sent;
|
|
selElems = void 0, started = void 0, newFO = void 0;
|
|
|
|
// const ccZoom;
|
|
// const wEl, hEl;
|
|
// const wOffset, hOffset;
|
|
// const ccRBG;
|
|
// const ccOpacity;
|
|
// const brushW, brushH;
|
|
|
|
// const ccDebug = document.getElementById('debugpanel');
|
|
|
|
/* const properlySourceSizeTextArea = function(){
|
|
// TODO: remove magic numbers here and get values from CSS
|
|
const height = $('#svg_source_container').height() - 80;
|
|
$('#svg_source_textarea').css('height', height);
|
|
}; */
|
|
|
|
|
|
/*
|
|
function toggleSourceButtons(on){
|
|
$('#tool_source_save, #tool_source_cancel').toggle(!on);
|
|
$('#polygon_save, #polygon_cancel').toggle(on);
|
|
}
|
|
*/
|
|
|
|
/**
|
|
* Obtained from http://code.google.com/p/passenger-top/source/browse/instiki/public/svg-edit/editor/extensions/ext-itex.js?r=3
|
|
* This function sets the content of of the currently-selected foreignObject element,
|
|
* based on the itex contained in string.
|
|
* @param {string} tex The itex text.
|
|
* @returns {boolean} This function returns false if the set was unsuccessful, true otherwise.
|
|
*/
|
|
/*
|
|
function setItexString(tex) {
|
|
const mathns = 'http://www.w3.org/1998/Math/MathML',
|
|
xmlnsns = 'http://www.w3.org/2000/xmlns/',
|
|
ajaxEndpoint = '../../itex';
|
|
const elt = selElems[0];
|
|
try {
|
|
const math = svgdoc.createElementNS(mathns, 'math');
|
|
math.setAttributeNS(xmlnsns, 'xmlns', mathns);
|
|
math.setAttribute('display', 'inline');
|
|
const semantics = document.createElementNS(mathns, 'semantics');
|
|
const annotation = document.createElementNS(mathns, 'annotation');
|
|
annotation.setAttribute('encoding', 'application/x-tex');
|
|
annotation.textContent = tex;
|
|
const mrow = document.createElementNS(mathns, 'mrow');
|
|
semantics.append(mrow, annotation);
|
|
math.append(semantics);
|
|
// make an AJAX request to the server, to get the MathML
|
|
$.post(ajaxEndpoint, {tex, display: 'inline'}, function(data){
|
|
const children = data.documentElement.childNodes;
|
|
while (children.length > 0) {
|
|
mrow.append(svgdoc.adoptNode(children[0], true));
|
|
}
|
|
S.sanitizeSvg(math);
|
|
S.call('changed', [elt]);
|
|
});
|
|
elt.firstChild.replaceWith(math);
|
|
S.call('changed', [elt]);
|
|
svgCanvas.clearSelection();
|
|
} catch(e) {
|
|
console.log(e);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
*/
|
|
buttons = [{
|
|
id: 'tool_polygon',
|
|
type: 'mode',
|
|
position: 11,
|
|
events: {
|
|
click: function click() {
|
|
svgCanvas.setMode('polygon');
|
|
showPanel(true);
|
|
}
|
|
}
|
|
}];
|
|
contextTools = [{
|
|
type: 'input',
|
|
panel: 'polygon_panel',
|
|
id: 'polySides',
|
|
size: 3,
|
|
defval: 5,
|
|
events: {
|
|
change: function change() {
|
|
setAttr('sides', this.value);
|
|
}
|
|
}
|
|
}];
|
|
return _context.abrupt('return', {
|
|
name: strings.name,
|
|
svgicons: svgEditor.curConfig.extIconsPath + 'polygon-icons.svg',
|
|
buttons: strings.buttons.map(function (button, i) {
|
|
return Object.assign(buttons[i], button);
|
|
}),
|
|
context_tools: strings.contextTools.map(function (contextTool, i) {
|
|
return Object.assign(contextTools[i], contextTool);
|
|
}),
|
|
|
|
callback: function callback() {
|
|
$('#polygon_panel').hide();
|
|
|
|
// TODO: Needs to be done after orig icon loads
|
|
setTimeout(function () {
|
|
// Create source save/cancel buttons
|
|
/* const save = */$('#tool_source_save').clone().hide().attr('id', 'polygon_save').unbind().appendTo('#tool_source_back').click(function () {
|
|
if (!editingitex) {
|
|
return;
|
|
}
|
|
// }
|
|
// setSelectMode();
|
|
});
|
|
|
|
/* const cancel = */$('#tool_source_cancel').clone().hide().attr('id', 'polygon_cancel').unbind().appendTo('#tool_source_back').click(function () {
|
|
});
|
|
}, 3000);
|
|
},
|
|
mouseDown: function mouseDown(opts) {
|
|
// const e = opts.event;
|
|
var rgb = svgCanvas.getColor('fill');
|
|
// const ccRgbEl = rgb.substring(1, rgb.length);
|
|
var sRgb = svgCanvas.getColor('stroke');
|
|
// ccSRgbEl = sRgb.substring(1, rgb.length);
|
|
var sWidth = svgCanvas.getStrokeWidth();
|
|
|
|
if (svgCanvas.getMode() === 'polygon') {
|
|
started = true;
|
|
|
|
newFO = S.addSVGElementFromJson({
|
|
element: 'polygon',
|
|
attr: {
|
|
cx: opts.start_x,
|
|
cy: opts.start_y,
|
|
id: S.getNextId(),
|
|
shape: 'regularPoly',
|
|
sides: document.getElementById('polySides').value,
|
|
orient: 'x',
|
|
edge: 0,
|
|
fill: rgb,
|
|
strokecolor: sRgb,
|
|
strokeWidth: sWidth
|
|
}
|
|
});
|
|
|
|
return {
|
|
started: true
|
|
};
|
|
}
|
|
},
|
|
mouseMove: function mouseMove(opts) {
|
|
if (!started) {
|
|
return;
|
|
}
|
|
if (svgCanvas.getMode() === 'polygon') {
|
|
// const e = opts.event;
|
|
var c = $(newFO).attr(['cx', 'cy', 'sides', 'orient', 'fill', 'strokecolor', 'strokeWidth']);
|
|
var x = opts.mouse_x;
|
|
var y = opts.mouse_y;
|
|
var cx = c.cx,
|
|
cy = c.cy,
|
|
fill = c.fill,
|
|
strokecolor = c.strokecolor,
|
|
strokeWidth = c.strokeWidth,
|
|
sides = c.sides,
|
|
edg = Math.sqrt((x - cx) * (x - cx) + (y - cy) * (y - cy)) / 1.5;
|
|
|
|
newFO.setAttributeNS(null, 'edge', edg);
|
|
|
|
var inradius = edg / 2 * cot(Math.PI / sides);
|
|
var circumradius = inradius * sec(Math.PI / sides);
|
|
var points = '';
|
|
for (var s = 0; sides >= s; s++) {
|
|
var angle = 2.0 * Math.PI * s / sides;
|
|
x = circumradius * Math.cos(angle) + cx;
|
|
y = circumradius * Math.sin(angle) + cy;
|
|
|
|
points += x + ',' + y + ' ';
|
|
}
|
|
|
|
// const poly = newFO.createElementNS(NS.SVG, 'polygon');
|
|
newFO.setAttributeNS(null, 'points', points);
|
|
newFO.setAttributeNS(null, 'fill', fill);
|
|
newFO.setAttributeNS(null, 'stroke', strokecolor);
|
|
newFO.setAttributeNS(null, 'stroke-width', strokeWidth);
|
|
// newFO.setAttributeNS(null, 'transform', 'rotate(-90)');
|
|
// const shape = newFO.getAttributeNS(null, 'shape');
|
|
// newFO.append(poly);
|
|
// DrawPoly(cx, cy, sides, edg, orient);
|
|
return {
|
|
started: true
|
|
};
|
|
}
|
|
},
|
|
mouseUp: function mouseUp(opts) {
|
|
if (svgCanvas.getMode() === 'polygon') {
|
|
var attrs = $(newFO).attr('edge');
|
|
var keep = attrs.edge !== '0';
|
|
// svgCanvas.addToSelection([newFO], true);
|
|
return {
|
|
keep: keep,
|
|
element: newFO
|
|
};
|
|
}
|
|
},
|
|
selectedChanged: function selectedChanged(opts) {
|
|
// Use this to update the current selected elements
|
|
selElems = opts.elems;
|
|
|
|
var i = selElems.length;
|
|
while (i--) {
|
|
var elem = selElems[i];
|
|
if (elem && elem.getAttributeNS(null, 'shape') === 'regularPoly') {
|
|
if (opts.selectedElement && !opts.multiselected) {
|
|
$('#polySides').val(elem.getAttribute('sides'));
|
|
|
|
showPanel(true);
|
|
} else {
|
|
showPanel(false);
|
|
}
|
|
} else {
|
|
showPanel(false);
|
|
}
|
|
}
|
|
},
|
|
elementChanged: function elementChanged(opts) {
|
|
// const elem = opts.elems[0];
|
|
}
|
|
});
|
|
|
|
case 15:
|
|
case 'end':
|
|
return _context.stop();
|
|
}
|
|
}
|
|
}, _callee, this);
|
|
}));
|
|
|
|
function init(_x) {
|
|
return _ref.apply(this, arguments);
|
|
}
|
|
|
|
return init;
|
|
}()
|
|
};
|
|
|
|
return extPolygon;
|
|
|
|
}());
|