- 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
286 lines
7.6 KiB
JavaScript
286 lines
7.6 KiB
JavaScript
/* globals jQuery */
|
|
/**
|
|
* ext-arrows.js
|
|
*
|
|
* @license MIT
|
|
*
|
|
* @copyright 2010 Alexis Deveria
|
|
*
|
|
*/
|
|
export default {
|
|
name: 'arrows',
|
|
async init (S) {
|
|
const strings = await S.importLocale();
|
|
const svgEditor = this;
|
|
const svgCanvas = svgEditor.canvas;
|
|
const $ = jQuery;
|
|
const // {svgcontent} = S,
|
|
addElem = S.addSVGElementFromJson,
|
|
{nonce} = S,
|
|
prefix = 'se_arrow_';
|
|
|
|
let selElems, arrowprefix, randomizeIds = S.randomize_ids;
|
|
|
|
function setArrowNonce (window, n) {
|
|
randomizeIds = true;
|
|
arrowprefix = prefix + n + '_';
|
|
pathdata.fw.id = arrowprefix + 'fw';
|
|
pathdata.bk.id = arrowprefix + 'bk';
|
|
}
|
|
|
|
function unsetArrowNonce (window) {
|
|
randomizeIds = false;
|
|
arrowprefix = prefix;
|
|
pathdata.fw.id = arrowprefix + 'fw';
|
|
pathdata.bk.id = arrowprefix + 'bk';
|
|
}
|
|
|
|
svgCanvas.bind('setnonce', setArrowNonce);
|
|
svgCanvas.bind('unsetnonce', unsetArrowNonce);
|
|
|
|
if (randomizeIds) {
|
|
arrowprefix = prefix + nonce + '_';
|
|
} else {
|
|
arrowprefix = prefix;
|
|
}
|
|
|
|
const pathdata = {
|
|
fw: {d: 'm0,0l10,5l-10,5l5,-5l-5,-5z', refx: 8, id: arrowprefix + 'fw'},
|
|
bk: {d: 'm10,0l-10,5l10,5l-5,-5l5,-5z', refx: 2, id: arrowprefix + 'bk'}
|
|
};
|
|
|
|
function getLinked (elem, attr) {
|
|
const str = elem.getAttribute(attr);
|
|
if (!str) { return null; }
|
|
const m = str.match(/\(#(.*)\)/);
|
|
if (!m || m.length !== 2) {
|
|
return null;
|
|
}
|
|
return S.getElem(m[1]);
|
|
}
|
|
|
|
function showPanel (on) {
|
|
$('#arrow_panel').toggle(on);
|
|
if (on) {
|
|
const el = selElems[0];
|
|
const end = el.getAttribute('marker-end');
|
|
const start = el.getAttribute('marker-start');
|
|
const mid = el.getAttribute('marker-mid');
|
|
let val;
|
|
if (end && start) {
|
|
val = 'both';
|
|
} else if (end) {
|
|
val = 'end';
|
|
} else if (start) {
|
|
val = 'start';
|
|
} else if (mid) {
|
|
val = 'mid';
|
|
if (mid.includes('bk')) {
|
|
val = 'mid_bk';
|
|
}
|
|
}
|
|
|
|
if (!start && !mid && !end) {
|
|
val = 'none';
|
|
}
|
|
|
|
$('#arrow_list').val(val);
|
|
}
|
|
}
|
|
|
|
function resetMarker () {
|
|
const el = selElems[0];
|
|
el.removeAttribute('marker-start');
|
|
el.removeAttribute('marker-mid');
|
|
el.removeAttribute('marker-end');
|
|
}
|
|
|
|
function addMarker (dir, type, id) {
|
|
// TODO: Make marker (or use?) per arrow type, since refX can be different
|
|
id = id || arrowprefix + dir;
|
|
|
|
const data = pathdata[dir];
|
|
|
|
if (type === 'mid') {
|
|
data.refx = 5;
|
|
}
|
|
|
|
let marker = S.getElem(id);
|
|
if (!marker) {
|
|
marker = addElem({
|
|
element: 'marker',
|
|
attr: {
|
|
viewBox: '0 0 10 10',
|
|
id,
|
|
refY: 5,
|
|
markerUnits: 'strokeWidth',
|
|
markerWidth: 5,
|
|
markerHeight: 5,
|
|
orient: 'auto',
|
|
style: 'pointer-events:none' // Currently needed for Opera
|
|
}
|
|
});
|
|
const arrow = addElem({
|
|
element: 'path',
|
|
attr: {
|
|
d: data.d,
|
|
fill: '#000000'
|
|
}
|
|
});
|
|
marker.append(arrow);
|
|
S.findDefs().append(marker);
|
|
}
|
|
|
|
marker.setAttribute('refX', data.refx);
|
|
|
|
return marker;
|
|
}
|
|
|
|
function setArrow () {
|
|
resetMarker();
|
|
|
|
let type = this.value;
|
|
if (type === 'none') {
|
|
return;
|
|
}
|
|
|
|
// Set marker on element
|
|
let dir = 'fw';
|
|
if (type === 'mid_bk') {
|
|
type = 'mid';
|
|
dir = 'bk';
|
|
} else if (type === 'both') {
|
|
addMarker('bk', type);
|
|
svgCanvas.changeSelectedAttribute('marker-start', 'url(#' + pathdata.bk.id + ')');
|
|
type = 'end';
|
|
dir = 'fw';
|
|
} else if (type === 'start') {
|
|
dir = 'bk';
|
|
}
|
|
|
|
addMarker(dir, type);
|
|
svgCanvas.changeSelectedAttribute('marker-' + type, 'url(#' + pathdata[dir].id + ')');
|
|
S.call('changed', selElems);
|
|
}
|
|
|
|
function colorChanged (elem) {
|
|
const color = elem.getAttribute('stroke');
|
|
const mtypes = ['start', 'mid', 'end'];
|
|
const defs = S.findDefs();
|
|
|
|
$.each(mtypes, function (i, type) {
|
|
const marker = getLinked(elem, 'marker-' + type);
|
|
if (!marker) { return; }
|
|
|
|
const curColor = $(marker).children().attr('fill');
|
|
const curD = $(marker).children().attr('d');
|
|
if (curColor === color) { return; }
|
|
|
|
const allMarkers = $(defs).find('marker');
|
|
let newMarker = null;
|
|
// Different color, check if already made
|
|
allMarkers.each(function () {
|
|
const attrs = $(this).children().attr(['fill', 'd']);
|
|
if (attrs.fill === color && attrs.d === curD) {
|
|
// Found another marker with this color and this path
|
|
newMarker = this;
|
|
}
|
|
});
|
|
|
|
if (!newMarker) {
|
|
// Create a new marker with this color
|
|
const lastId = marker.id;
|
|
const dir = lastId.includes('_fw') ? 'fw' : 'bk';
|
|
|
|
newMarker = addMarker(dir, type, arrowprefix + dir + allMarkers.length);
|
|
|
|
$(newMarker).children().attr('fill', color);
|
|
}
|
|
|
|
$(elem).attr('marker-' + type, 'url(#' + newMarker.id + ')');
|
|
|
|
// Check if last marker can be removed
|
|
let remove = true;
|
|
$(S.svgcontent).find('line, polyline, path, polygon').each(function () {
|
|
const elem = this;
|
|
$.each(mtypes, function (j, mtype) {
|
|
if ($(elem).attr('marker-' + mtype) === 'url(#' + marker.id + ')') {
|
|
remove = false;
|
|
return remove;
|
|
}
|
|
});
|
|
if (!remove) { return false; }
|
|
});
|
|
|
|
// Not found, so can safely remove
|
|
if (remove) {
|
|
$(marker).remove();
|
|
}
|
|
});
|
|
}
|
|
|
|
const contextTools = [
|
|
{
|
|
type: 'select',
|
|
panel: 'arrow_panel',
|
|
id: 'arrow_list',
|
|
defval: 'none',
|
|
events: {
|
|
change: setArrow
|
|
}
|
|
}
|
|
];
|
|
|
|
return {
|
|
name: strings.name,
|
|
context_tools: strings.contextTools.map((contextTool, i) => {
|
|
return Object.assign(contextTools[i], contextTool);
|
|
}),
|
|
callback () {
|
|
$('#arrow_panel').hide();
|
|
// Set ID so it can be translated in locale file
|
|
$('#arrow_list option')[0].id = 'connector_no_arrow';
|
|
},
|
|
async addLangData ({lang, importLocale}) {
|
|
const strings = await importLocale();
|
|
return {
|
|
data: strings.langList
|
|
};
|
|
},
|
|
selectedChanged (opts) {
|
|
// Use this to update the current selected elements
|
|
selElems = opts.elems;
|
|
|
|
const markerElems = ['line', 'path', 'polyline', 'polygon'];
|
|
let i = selElems.length;
|
|
while (i--) {
|
|
const elem = selElems[i];
|
|
if (elem && markerElems.includes(elem.tagName)) {
|
|
if (opts.selectedElement && !opts.multiselected) {
|
|
showPanel(true);
|
|
} else {
|
|
showPanel(false);
|
|
}
|
|
} else {
|
|
showPanel(false);
|
|
}
|
|
}
|
|
},
|
|
elementChanged (opts) {
|
|
const elem = opts.elems[0];
|
|
if (elem && (
|
|
elem.getAttribute('marker-start') ||
|
|
elem.getAttribute('marker-mid') ||
|
|
elem.getAttribute('marker-end')
|
|
)) {
|
|
// const start = elem.getAttribute('marker-start');
|
|
// const mid = elem.getAttribute('marker-mid');
|
|
// const end = elem.getAttribute('marker-end');
|
|
// Has marker, so see if it should match color
|
|
colorChanged(elem);
|
|
}
|
|
}
|
|
};
|
|
}
|
|
};
|