- Breaking change: Treat callbacks to `editor.ready` as Promises, only resolving after all resolve - Breaking change: Make `editor.runCallbacks` return a `Promise` which resolves upon all callbacks resolving - Breaking change: Require `npx` (used with `babel-node`) to allow Node files for HTML building and JSDoc type checking to be expressed as ESM. - Breaking change: `addExtension` now throws upon a repeated attempt to add an already-added extension - Breaking change (storage preference cookies): Namespace the cookie as "svgeditstore" instead of just "store" - Breaking change (API): Remove `svgCanvas.rasterExport` fourth (callback) argument, collapsing fifth (options) to fourth - Breaking change (API): Remove `svgCanvas.exportPDF` third (callback) argument - Breaking change (API): `editor/contextmenu.js` `add` now throws instead of giving a console error only upon detecting a bad menuitem or preexisting context menu - Breaking change (API): Remove `svgCanvas.embedImage` second (callback) argument - Breaking change (API): Make `getHelpXML` a class instead of instance method of `RGBColor` - Breaking change (internal API): Refactor `dbox` (and `alert`/`confirm`/`process`/`prompt`/`select`) to avoid a callback argument in favor of return a Promise - Fix: Avoid running in extension `langReady` multiple times or serially - Enhancement (API): Add svgCanvas.runExtension to run just one extension and add `nameFilter` callback to `runExtensions` - Enhancement (API): Supply `$` (our wrapped jQuery) to extensions so can use its plugins, e.g., dbox with its `alert` - Enhancement: Use alert dialog in place of `alert` in webappfind - Enhancement: `editor.ready` now returns a Promise resolving when all callbacks have resolved - Enhancement: Allow `noAlert` option as part of second argument to `loadSvgString` (and `loadFromURL` and `loadFromDataURI`) to avoid UI alert (and trigger promise rejection) - Enhancement: Make `dbox` as a separate module for alert, prompt, etc. dialogs - Refactoring: Internal `PaintBox` as class; other misc. tweaks; no bitwise in canvg - Linting (ESLint): Further linting changes (for editor); rename `.eslintrc` -> `.eslintrc.json` per recommendation - Optimization: Recompress images (imageoptim-cli updated) - npm: Update devDeps - npm: Bump to 4.0.0
104 lines
3.2 KiB
JavaScript
104 lines
3.2 KiB
JavaScript
/* globals jQuery */
|
|
/**
|
|
* Adds context menu functionality
|
|
* @module contextmenu
|
|
* @license Apache-2.0
|
|
* @author Adam Bender
|
|
*/
|
|
// Dependencies:
|
|
// 1) jQuery (for dom injection of context menus)
|
|
|
|
const $ = jQuery;
|
|
|
|
let contextMenuExtensions = {};
|
|
|
|
/**
|
|
* Signature depends on what the user adds; in the case of our uses with
|
|
* SVGEditor, no parameters are passed nor anything expected for a return.
|
|
* @callback module:contextmenu.MenuItemAction
|
|
*/
|
|
|
|
/**
|
|
* @typedef {PlainObject} module:contextmenu.MenuItem
|
|
* @property {string} id
|
|
* @property {string} label
|
|
* @property {module:contextmenu.MenuItemAction} action
|
|
*/
|
|
|
|
/**
|
|
* @param {module:contextmenu.MenuItem} menuItem
|
|
* @returns {boolean}
|
|
*/
|
|
const menuItemIsValid = function (menuItem) {
|
|
return menuItem && menuItem.id && menuItem.label && menuItem.action && typeof menuItem.action === 'function';
|
|
};
|
|
|
|
/**
|
|
* @function module:contextmenu.add
|
|
* @param {module:contextmenu.MenuItem} menuItem
|
|
* @throws {Error|TypeError}
|
|
* @returns {undefined}
|
|
*/
|
|
export const add = function (menuItem) {
|
|
// menuItem: {id, label, shortcut, action}
|
|
if (!menuItemIsValid(menuItem)) {
|
|
throw new TypeError('Menu items must be defined and have at least properties: id, label, action, where action must be a function');
|
|
}
|
|
if (menuItem.id in contextMenuExtensions) {
|
|
throw new Error('Cannot add extension "' + menuItem.id + '", an extension by that name already exists"');
|
|
}
|
|
// Register menuItem action, see below for deferred menu dom injection
|
|
console.log('Registered contextmenu item: {id:' + menuItem.id + ', label:' + menuItem.label + '}'); // eslint-disable-line no-console
|
|
contextMenuExtensions[menuItem.id] = menuItem;
|
|
// TODO: Need to consider how to handle custom enable/disable behavior
|
|
};
|
|
|
|
/**
|
|
* @function module:contextmenu.hasCustomHandler
|
|
* @param {string} handlerKey
|
|
* @returns {boolean}
|
|
*/
|
|
export const hasCustomHandler = function (handlerKey) {
|
|
return Boolean(contextMenuExtensions[handlerKey]);
|
|
};
|
|
|
|
/**
|
|
* @function module:contextmenu.getCustomHandler
|
|
* @param {string} handlerKey
|
|
* @returns {module:contextmenu.MenuItemAction}
|
|
*/
|
|
export const getCustomHandler = function (handlerKey) {
|
|
return contextMenuExtensions[handlerKey].action;
|
|
};
|
|
|
|
/**
|
|
* @param {module:contextmenu.MenuItem} menuItem
|
|
* @returns {undefined}
|
|
*/
|
|
const injectExtendedContextMenuItemIntoDom = function (menuItem) {
|
|
if (!Object.keys(contextMenuExtensions).length) {
|
|
// all menuItems appear at the bottom of the menu in their own container.
|
|
// if this is the first extension menu we need to add the separator.
|
|
$('#cmenu_canvas').append("<li class='separator'>");
|
|
}
|
|
const shortcut = menuItem.shortcut || '';
|
|
$('#cmenu_canvas').append("<li class='disabled'><a href='#" + menuItem.id + "'>" +
|
|
menuItem.label + "<span class='shortcut'>" +
|
|
shortcut + '</span></a></li>');
|
|
};
|
|
|
|
/**
|
|
* @function module:contextmenu.injectExtendedContextMenuItemsIntoDom
|
|
* @returns {undefined}
|
|
*/
|
|
export const injectExtendedContextMenuItemsIntoDom = function () {
|
|
Object.values(contextMenuExtensions).forEach((menuItem) => {
|
|
injectExtendedContextMenuItemIntoDom(menuItem);
|
|
});
|
|
};
|
|
/**
|
|
* @function module:contextmenu.resetCustomMenus
|
|
* @returns {undefined}
|
|
*/
|
|
export const resetCustomMenus = function () { contextMenuExtensions = {}; };
|