2010-11-12 19:08:29 +00:00
|
|
|
/**
|
|
|
|
* Package: svedit.history
|
|
|
|
*
|
2012-09-16 18:53:27 +00:00
|
|
|
* Licensed under the MIT License
|
2010-11-12 19:08:29 +00:00
|
|
|
*
|
|
|
|
* Copyright(c) 2010 Jeff Schiller
|
|
|
|
*/
|
|
|
|
|
- Breaking change: Rename config file to `svgedit-config-iife.js` (or for the module version, `svgedit-config-es.js`);
also expect one directory higher; incorporates #207 (@iuyiuy)
- Breaking change: Separate `extIconsPath` from `extPath` (not copying over icons)
- Breaking change: Don't reference `custom.css` in HTML; can instead be referenced in JavaScript through
the config file (provided in `svgedit-config-sample-iife.js`/`svgedit-config-sample-es.js` as `svgedit-custom.css` for
better namespacing); incorporates #207 (@iuyiuy)
- Breaking change: Remove minified jgraduate/spinbtn files (minified within Rollup routine)
- Fix: Zoom when scrolled; incorporates #169 (@AndrolGenhald), adapting for conventions; also allow avoidance when shift key pressed
- Fix: Update Atom feed reference in HTML
- Fixes related to recent commits: Some path and method name fixes needed, function order, missing methods, variable scope declaration, no need for DOMContentLoaded listeners in modules, switch back to non-default export, avoid trimming nullish, deal with mock tests, fix `math.matrixMultiply`, use jquery-svg where needed for array/SVG attributes; add babel-polyfill and defer script to imagelib; other misc. fixes
- Enhancement: Move config-sample.js out of `editor` directory
- Enhancement: For `callback`-style extensions, also provide config object; add following
to that object: buildCanvgCallback, canvg, decode64, encode64, executeAfterLoads, getTypeMap, isChrome, ieIE, NS, text2xml
- Enhancement: Complete ES6 modules work (extensions, locales, tests), along with Babel;
make Node build routine for converting modular source to non-modular,
use `loadStylesheets` for modular stylehsheet defining (but parallel loading);
- Enhancement: Add `stylesheets` config for modular but parallel stylesheet loading with `@default` option for simple inclusion/exclusion of defaults (if not going with default).
- Refactoring: Clean up `svg-editor.html`: consistent indents; avoid extra lbs, avoid long lines
- Refactoring: Avoid embedded API adding inline JavaScript listener
- Refactoring: Move layers and context code to `draw.js`
- Refactoring: Move `pathActions` from `svgcanvas.js` (though preserve aliases to these methods on `canvas`) and `convertPath` from `svgutils.js` to `path.js`
- Refactoring: Move `getStrokedBBox` from `svgcanvas.js` (while keeping an alias) to `svgutils.js` (as `getStrokedBBoxDefaultVisible` to avoid conflict with existing)
- Docs: Remove "dependencies" comments in code except where summarizing role of jQuery or a non-obvious dependency
- Refactoring/Linting: Enfore `no-extra-semi` and `quote-props` rules
- Refactoring: Further avoidance of quotes on properties (as possible)
- Refactoring: Use `class` in place of functions where intended as classes
- Refactoring: Consistency and granularity in extensions imports
- Testing: Update QUnit to 2.6.1 (node_modules) and Sinon to 5.0.8 (and add sinon-test at 2.1.3) and enforce eslint-plugin-qunit linting rules; update custom extensions
- Testing: Add node-static for automating (and accessing out-of-directory contents)
- Testing: Avoid HTML attributes for styling
- Testing: Add npm `test` script
- Testing: Comment out unused jQuery SVG test
- Testing: Add test1 and svgutils_performance_test to all tests page
- Testing: Due apparently to Path having not been a formal class, the test was calling it without `new`; refactored now with sufficient mock data to take into account it is a class
- npm: Update devDeps
- npm: Add html modules and config build to test script
2018-05-22 10:03:16 +00:00
|
|
|
import {getHref, setHref, getRotationAngle} from './svgutils.js';
|
2018-05-18 03:25:45 +00:00
|
|
|
import {removeElementFromListMap} from './svgtransformlist.js';
|
2010-11-12 19:08:29 +00:00
|
|
|
|
2018-05-29 12:31:24 +00:00
|
|
|
/**
|
|
|
|
* Group: Undo/Redo history management
|
|
|
|
*/
|
2018-05-18 03:25:45 +00:00
|
|
|
export const HistoryEventTypes = {
|
2018-05-18 04:02:30 +00:00
|
|
|
BEFORE_APPLY: 'before_apply',
|
|
|
|
AFTER_APPLY: 'after_apply',
|
|
|
|
BEFORE_UNAPPLY: 'before_unapply',
|
|
|
|
AFTER_UNAPPLY: 'after_unapply'
|
2010-11-12 19:08:29 +00:00
|
|
|
};
|
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
// const removedElements = {};
|
2010-11-12 19:08:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* An interface that all command objects must implement.
|
2018-05-29 08:39:29 +00:00
|
|
|
* @typedef {Object} svgedit.history.HistoryCommand
|
2011-02-02 17:28:43 +00:00
|
|
|
* void apply(svgedit.history.HistoryEventHandler);
|
|
|
|
* void unapply(svgedit.history.HistoryEventHandler);
|
2010-11-12 19:08:29 +00:00
|
|
|
* Element[] elements();
|
2010-11-12 23:10:19 +00:00
|
|
|
* String getText();
|
|
|
|
*
|
|
|
|
* static String type();
|
2010-11-12 19:08:29 +00:00
|
|
|
* }
|
2011-02-02 17:28:43 +00:00
|
|
|
*
|
|
|
|
* Interface: svgedit.history.HistoryEventHandler
|
|
|
|
* An interface for objects that will handle history events.
|
|
|
|
*
|
|
|
|
* interface svgedit.history.HistoryEventHandler {
|
|
|
|
* void handleHistoryEvent(eventType, command);
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* eventType is a string conforming to one of the HistoryEvent types.
|
|
|
|
* command is an object fulfilling the HistoryCommand interface.
|
2010-11-12 19:08:29 +00:00
|
|
|
*/
|
|
|
|
|
2014-04-03 14:21:55 +00:00
|
|
|
/**
|
|
|
|
* @class svgedit.history.MoveElementCommand
|
|
|
|
* @implements svgedit.history.HistoryCommand
|
|
|
|
* History command for an element that had its DOM position changed
|
|
|
|
* @param {Element} elem - The DOM element that was moved
|
|
|
|
* @param {Element} oldNextSibling - The element's next sibling before it was moved
|
|
|
|
* @param {Element} oldParent - The element's parent before it was moved
|
|
|
|
* @param {string} [text] - An optional string visible to user related to this change
|
|
|
|
*/
|
2018-05-18 03:25:45 +00:00
|
|
|
export class MoveElementCommand {
|
|
|
|
constructor (elem, oldNextSibling, oldParent, text) {
|
|
|
|
this.elem = elem;
|
|
|
|
this.text = text ? ('Move ' + elem.tagName + ' to ' + text) : ('Move ' + elem.tagName);
|
|
|
|
this.oldNextSibling = oldNextSibling;
|
|
|
|
this.oldParent = oldParent;
|
|
|
|
this.newNextSibling = elem.nextSibling;
|
|
|
|
this.newParent = elem.parentNode;
|
|
|
|
}
|
|
|
|
getText () {
|
|
|
|
return this.text;
|
|
|
|
}
|
|
|
|
type () {
|
|
|
|
return 'svgedit.history.MoveElementCommand';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Re-positions the element
|
2018-05-29 08:39:29 +00:00
|
|
|
* @param {{handleHistoryEvent: function}} handler
|
2018-05-18 03:25:45 +00:00
|
|
|
*/
|
|
|
|
apply (handler) {
|
|
|
|
// TODO(codedread): Refactor this common event code into a base HistoryCommand class.
|
|
|
|
if (handler) {
|
|
|
|
handler.handleHistoryEvent(HistoryEventTypes.BEFORE_APPLY, this);
|
|
|
|
}
|
2010-11-12 19:08:29 +00:00
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
this.elem = this.newParent.insertBefore(this.elem, this.newNextSibling);
|
2010-11-12 23:10:19 +00:00
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
if (handler) {
|
|
|
|
handler.handleHistoryEvent(HistoryEventTypes.AFTER_APPLY, this);
|
|
|
|
}
|
2018-05-18 04:02:30 +00:00
|
|
|
}
|
2010-11-13 09:58:51 +00:00
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
/**
|
|
|
|
* Positions the element back to its original location
|
2018-05-29 08:39:29 +00:00
|
|
|
* @param {{handleHistoryEvent: function}} handler
|
2018-05-18 03:25:45 +00:00
|
|
|
*/
|
|
|
|
unapply (handler) {
|
|
|
|
if (handler) {
|
|
|
|
handler.handleHistoryEvent(HistoryEventTypes.BEFORE_UNAPPLY, this);
|
|
|
|
}
|
2010-11-13 09:58:51 +00:00
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
this.elem = this.oldParent.insertBefore(this.elem, this.oldNextSibling);
|
2010-11-12 19:08:29 +00:00
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
if (handler) {
|
|
|
|
handler.handleHistoryEvent(HistoryEventTypes.AFTER_UNAPPLY, this);
|
|
|
|
}
|
2018-05-18 04:02:30 +00:00
|
|
|
}
|
2013-02-14 12:57:15 +00:00
|
|
|
|
2018-05-29 12:31:24 +00:00
|
|
|
/**
|
|
|
|
* @returns {Array} Array with element associated with this command
|
|
|
|
*/
|
2018-05-18 03:25:45 +00:00
|
|
|
elements () {
|
|
|
|
return [this.elem];
|
2018-05-18 04:02:30 +00:00
|
|
|
}
|
2018-05-18 03:25:45 +00:00
|
|
|
}
|
|
|
|
MoveElementCommand.type = MoveElementCommand.prototype.type;
|
2010-11-12 19:08:29 +00:00
|
|
|
|
2018-05-29 12:31:24 +00:00
|
|
|
/**
|
|
|
|
* @implements svgedit.history.HistoryCommand
|
|
|
|
* History command for an element that was added to the DOM
|
|
|
|
*
|
|
|
|
* @param elem - The newly added DOM element
|
|
|
|
* @param text - An optional string visible to user related to this change
|
|
|
|
*/
|
2018-05-18 03:25:45 +00:00
|
|
|
export class InsertElementCommand {
|
|
|
|
constructor (elem, text) {
|
|
|
|
this.elem = elem;
|
|
|
|
this.text = text || ('Create ' + elem.tagName);
|
|
|
|
this.parent = elem.parentNode;
|
|
|
|
this.nextSibling = this.elem.nextSibling;
|
|
|
|
}
|
2010-11-12 19:08:29 +00:00
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
type () {
|
|
|
|
return 'svgedit.history.InsertElementCommand';
|
- Breaking change: Rename config file to `svgedit-config-iife.js` (or for the module version, `svgedit-config-es.js`);
also expect one directory higher; incorporates #207 (@iuyiuy)
- Breaking change: Separate `extIconsPath` from `extPath` (not copying over icons)
- Breaking change: Don't reference `custom.css` in HTML; can instead be referenced in JavaScript through
the config file (provided in `svgedit-config-sample-iife.js`/`svgedit-config-sample-es.js` as `svgedit-custom.css` for
better namespacing); incorporates #207 (@iuyiuy)
- Breaking change: Remove minified jgraduate/spinbtn files (minified within Rollup routine)
- Fix: Zoom when scrolled; incorporates #169 (@AndrolGenhald), adapting for conventions; also allow avoidance when shift key pressed
- Fix: Update Atom feed reference in HTML
- Fixes related to recent commits: Some path and method name fixes needed, function order, missing methods, variable scope declaration, no need for DOMContentLoaded listeners in modules, switch back to non-default export, avoid trimming nullish, deal with mock tests, fix `math.matrixMultiply`, use jquery-svg where needed for array/SVG attributes; add babel-polyfill and defer script to imagelib; other misc. fixes
- Enhancement: Move config-sample.js out of `editor` directory
- Enhancement: For `callback`-style extensions, also provide config object; add following
to that object: buildCanvgCallback, canvg, decode64, encode64, executeAfterLoads, getTypeMap, isChrome, ieIE, NS, text2xml
- Enhancement: Complete ES6 modules work (extensions, locales, tests), along with Babel;
make Node build routine for converting modular source to non-modular,
use `loadStylesheets` for modular stylehsheet defining (but parallel loading);
- Enhancement: Add `stylesheets` config for modular but parallel stylesheet loading with `@default` option for simple inclusion/exclusion of defaults (if not going with default).
- Refactoring: Clean up `svg-editor.html`: consistent indents; avoid extra lbs, avoid long lines
- Refactoring: Avoid embedded API adding inline JavaScript listener
- Refactoring: Move layers and context code to `draw.js`
- Refactoring: Move `pathActions` from `svgcanvas.js` (though preserve aliases to these methods on `canvas`) and `convertPath` from `svgutils.js` to `path.js`
- Refactoring: Move `getStrokedBBox` from `svgcanvas.js` (while keeping an alias) to `svgutils.js` (as `getStrokedBBoxDefaultVisible` to avoid conflict with existing)
- Docs: Remove "dependencies" comments in code except where summarizing role of jQuery or a non-obvious dependency
- Refactoring/Linting: Enfore `no-extra-semi` and `quote-props` rules
- Refactoring: Further avoidance of quotes on properties (as possible)
- Refactoring: Use `class` in place of functions where intended as classes
- Refactoring: Consistency and granularity in extensions imports
- Testing: Update QUnit to 2.6.1 (node_modules) and Sinon to 5.0.8 (and add sinon-test at 2.1.3) and enforce eslint-plugin-qunit linting rules; update custom extensions
- Testing: Add node-static for automating (and accessing out-of-directory contents)
- Testing: Avoid HTML attributes for styling
- Testing: Add npm `test` script
- Testing: Comment out unused jQuery SVG test
- Testing: Add test1 and svgutils_performance_test to all tests page
- Testing: Due apparently to Path having not been a formal class, the test was calling it without `new`; refactored now with sufficient mock data to take into account it is a class
- npm: Update devDeps
- npm: Add html modules and config build to test script
2018-05-22 10:03:16 +00:00
|
|
|
}
|
2010-11-12 23:10:19 +00:00
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
getText () {
|
|
|
|
return this.text;
|
2018-05-18 04:02:30 +00:00
|
|
|
}
|
2010-11-13 09:58:51 +00:00
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
// Re-Inserts the new element
|
|
|
|
apply (handler) {
|
|
|
|
if (handler) {
|
|
|
|
handler.handleHistoryEvent(HistoryEventTypes.BEFORE_APPLY, this);
|
|
|
|
}
|
2010-11-13 09:58:51 +00:00
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
this.elem = this.parent.insertBefore(this.elem, this.nextSibling);
|
2010-11-12 19:08:29 +00:00
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
if (handler) {
|
|
|
|
handler.handleHistoryEvent(HistoryEventTypes.AFTER_APPLY, this);
|
|
|
|
}
|
2018-05-18 04:02:30 +00:00
|
|
|
}
|
2010-11-13 09:58:51 +00:00
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
// Removes the element
|
|
|
|
unapply (handler) {
|
|
|
|
if (handler) {
|
|
|
|
handler.handleHistoryEvent(HistoryEventTypes.BEFORE_UNAPPLY, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.parent = this.elem.parentNode;
|
|
|
|
this.elem = this.elem.parentNode.removeChild(this.elem);
|
2010-11-13 09:58:51 +00:00
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
if (handler) {
|
|
|
|
handler.handleHistoryEvent(HistoryEventTypes.AFTER_UNAPPLY, this);
|
|
|
|
}
|
2018-05-18 04:02:30 +00:00
|
|
|
}
|
2010-11-12 19:08:29 +00:00
|
|
|
|
2018-05-29 12:31:24 +00:00
|
|
|
/**
|
|
|
|
* @returns {Array} Array with element associated with this command
|
|
|
|
*/
|
2018-05-18 03:25:45 +00:00
|
|
|
elements () {
|
|
|
|
return [this.elem];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
InsertElementCommand.type = InsertElementCommand.prototype.type;
|
2010-11-12 19:08:29 +00:00
|
|
|
|
2018-05-29 12:31:24 +00:00
|
|
|
/**
|
|
|
|
* @implements svgedit.history.HistoryCommand
|
|
|
|
* History command for an element removed from the DOM
|
|
|
|
* @param elem - The removed DOM element
|
|
|
|
* @param oldNextSibling - The DOM element's nextSibling when it was in the DOM
|
|
|
|
* @param oldParent - The DOM element's parent
|
|
|
|
* @param {String} [text] - An optional string visible to user related to this change
|
|
|
|
*/
|
2018-05-18 03:25:45 +00:00
|
|
|
export class RemoveElementCommand {
|
|
|
|
constructor (elem, oldNextSibling, oldParent, text) {
|
|
|
|
this.elem = elem;
|
|
|
|
this.text = text || ('Delete ' + elem.tagName);
|
|
|
|
this.nextSibling = oldNextSibling;
|
|
|
|
this.parent = oldParent;
|
2010-11-12 19:08:29 +00:00
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
// special hack for webkit: remove this element's entry in the svgTransformLists map
|
|
|
|
removeElementFromListMap(elem);
|
|
|
|
}
|
|
|
|
type () {
|
|
|
|
return 'svgedit.history.RemoveElementCommand';
|
|
|
|
}
|
2010-11-12 23:10:19 +00:00
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
getText () {
|
|
|
|
return this.text;
|
2018-05-18 04:02:30 +00:00
|
|
|
}
|
2010-11-12 19:08:29 +00:00
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
// Re-removes the new element
|
|
|
|
apply (handler) {
|
|
|
|
if (handler) {
|
|
|
|
handler.handleHistoryEvent(HistoryEventTypes.BEFORE_APPLY, this);
|
|
|
|
}
|
2010-11-13 09:58:51 +00:00
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
removeElementFromListMap(this.elem);
|
|
|
|
this.parent = this.elem.parentNode;
|
|
|
|
this.elem = this.parent.removeChild(this.elem);
|
2010-11-12 19:08:29 +00:00
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
if (handler) {
|
|
|
|
handler.handleHistoryEvent(HistoryEventTypes.AFTER_APPLY, this);
|
|
|
|
}
|
2018-05-18 04:02:30 +00:00
|
|
|
}
|
2010-11-13 09:58:51 +00:00
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
// Re-adds the new element
|
|
|
|
unapply (handler) {
|
|
|
|
if (handler) {
|
|
|
|
handler.handleHistoryEvent(HistoryEventTypes.BEFORE_UNAPPLY, this);
|
2018-05-18 04:02:30 +00:00
|
|
|
}
|
2010-11-12 19:08:29 +00:00
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
removeElementFromListMap(this.elem);
|
|
|
|
if (this.nextSibling == null) {
|
|
|
|
if (window.console) {
|
|
|
|
console.log('Error: reference element was lost');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.parent.insertBefore(this.elem, this.nextSibling);
|
|
|
|
|
|
|
|
if (handler) {
|
|
|
|
handler.handleHistoryEvent(HistoryEventTypes.AFTER_UNAPPLY, this);
|
|
|
|
}
|
2018-05-18 04:02:30 +00:00
|
|
|
}
|
2010-11-12 19:08:29 +00:00
|
|
|
|
2018-05-29 12:31:24 +00:00
|
|
|
/**
|
|
|
|
* @returns {Array} Array with element associated with this command
|
|
|
|
*/
|
2018-05-18 03:25:45 +00:00
|
|
|
elements () {
|
|
|
|
return [this.elem];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RemoveElementCommand.type = RemoveElementCommand.prototype.type;
|
2010-11-12 19:08:29 +00:00
|
|
|
|
2018-05-29 12:31:24 +00:00
|
|
|
/**
|
|
|
|
* @implements svgedit.history.HistoryCommand
|
|
|
|
* History command to make a change to an element.
|
|
|
|
* Usually an attribute change, but can also be textcontent.
|
|
|
|
* @param elem - The DOM element that was changed
|
|
|
|
* @param attrs - An object with the attributes to be changed and the values they had *before* the change
|
|
|
|
* @param {String} text - An optional string visible to user related to this change
|
|
|
|
*/
|
2018-05-18 03:25:45 +00:00
|
|
|
export class ChangeElementCommand {
|
|
|
|
constructor (elem, attrs, text) {
|
|
|
|
this.elem = elem;
|
|
|
|
this.text = text ? ('Change ' + elem.tagName + ' ' + text) : ('Change ' + elem.tagName);
|
|
|
|
this.newValues = {};
|
|
|
|
this.oldValues = attrs;
|
|
|
|
for (const attr in attrs) {
|
2018-05-18 04:02:30 +00:00
|
|
|
if (attr === '#text') {
|
2018-05-18 03:25:45 +00:00
|
|
|
this.newValues[attr] = elem.textContent;
|
2018-05-18 04:02:30 +00:00
|
|
|
} else if (attr === '#href') {
|
2018-05-18 03:25:45 +00:00
|
|
|
this.newValues[attr] = getHref(elem);
|
2018-05-18 04:02:30 +00:00
|
|
|
} else {
|
2018-05-18 03:25:45 +00:00
|
|
|
this.newValues[attr] = elem.getAttribute(attr);
|
2018-05-18 04:02:30 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-18 03:25:45 +00:00
|
|
|
}
|
|
|
|
type () {
|
|
|
|
return 'svgedit.history.ChangeElementCommand';
|
- Breaking change: Rename config file to `svgedit-config-iife.js` (or for the module version, `svgedit-config-es.js`);
also expect one directory higher; incorporates #207 (@iuyiuy)
- Breaking change: Separate `extIconsPath` from `extPath` (not copying over icons)
- Breaking change: Don't reference `custom.css` in HTML; can instead be referenced in JavaScript through
the config file (provided in `svgedit-config-sample-iife.js`/`svgedit-config-sample-es.js` as `svgedit-custom.css` for
better namespacing); incorporates #207 (@iuyiuy)
- Breaking change: Remove minified jgraduate/spinbtn files (minified within Rollup routine)
- Fix: Zoom when scrolled; incorporates #169 (@AndrolGenhald), adapting for conventions; also allow avoidance when shift key pressed
- Fix: Update Atom feed reference in HTML
- Fixes related to recent commits: Some path and method name fixes needed, function order, missing methods, variable scope declaration, no need for DOMContentLoaded listeners in modules, switch back to non-default export, avoid trimming nullish, deal with mock tests, fix `math.matrixMultiply`, use jquery-svg where needed for array/SVG attributes; add babel-polyfill and defer script to imagelib; other misc. fixes
- Enhancement: Move config-sample.js out of `editor` directory
- Enhancement: For `callback`-style extensions, also provide config object; add following
to that object: buildCanvgCallback, canvg, decode64, encode64, executeAfterLoads, getTypeMap, isChrome, ieIE, NS, text2xml
- Enhancement: Complete ES6 modules work (extensions, locales, tests), along with Babel;
make Node build routine for converting modular source to non-modular,
use `loadStylesheets` for modular stylehsheet defining (but parallel loading);
- Enhancement: Add `stylesheets` config for modular but parallel stylesheet loading with `@default` option for simple inclusion/exclusion of defaults (if not going with default).
- Refactoring: Clean up `svg-editor.html`: consistent indents; avoid extra lbs, avoid long lines
- Refactoring: Avoid embedded API adding inline JavaScript listener
- Refactoring: Move layers and context code to `draw.js`
- Refactoring: Move `pathActions` from `svgcanvas.js` (though preserve aliases to these methods on `canvas`) and `convertPath` from `svgutils.js` to `path.js`
- Refactoring: Move `getStrokedBBox` from `svgcanvas.js` (while keeping an alias) to `svgutils.js` (as `getStrokedBBoxDefaultVisible` to avoid conflict with existing)
- Docs: Remove "dependencies" comments in code except where summarizing role of jQuery or a non-obvious dependency
- Refactoring/Linting: Enfore `no-extra-semi` and `quote-props` rules
- Refactoring: Further avoidance of quotes on properties (as possible)
- Refactoring: Use `class` in place of functions where intended as classes
- Refactoring: Consistency and granularity in extensions imports
- Testing: Update QUnit to 2.6.1 (node_modules) and Sinon to 5.0.8 (and add sinon-test at 2.1.3) and enforce eslint-plugin-qunit linting rules; update custom extensions
- Testing: Add node-static for automating (and accessing out-of-directory contents)
- Testing: Avoid HTML attributes for styling
- Testing: Add npm `test` script
- Testing: Comment out unused jQuery SVG test
- Testing: Add test1 and svgutils_performance_test to all tests page
- Testing: Due apparently to Path having not been a formal class, the test was calling it without `new`; refactored now with sufficient mock data to take into account it is a class
- npm: Update devDeps
- npm: Add html modules and config build to test script
2018-05-22 10:03:16 +00:00
|
|
|
}
|
2018-05-18 04:02:30 +00:00
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
getText () {
|
|
|
|
return this.text;
|
2018-05-18 04:02:30 +00:00
|
|
|
}
|
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
// Performs the stored change action
|
|
|
|
apply (handler) {
|
|
|
|
if (handler) {
|
|
|
|
handler.handleHistoryEvent(HistoryEventTypes.BEFORE_APPLY, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
let bChangedTransform = false;
|
|
|
|
for (const attr in this.newValues) {
|
|
|
|
if (this.newValues[attr]) {
|
|
|
|
if (attr === '#text') {
|
|
|
|
this.elem.textContent = this.newValues[attr];
|
|
|
|
} else if (attr === '#href') {
|
|
|
|
setHref(this.elem, this.newValues[attr]);
|
|
|
|
} else {
|
|
|
|
this.elem.setAttribute(attr, this.newValues[attr]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (attr === '#text') {
|
|
|
|
this.elem.textContent = '';
|
|
|
|
} else {
|
|
|
|
this.elem.setAttribute(attr, '');
|
|
|
|
this.elem.removeAttribute(attr);
|
|
|
|
}
|
2018-05-18 04:02:30 +00:00
|
|
|
}
|
2018-05-18 03:25:45 +00:00
|
|
|
|
|
|
|
if (attr === 'transform') { bChangedTransform = true; }
|
2018-05-18 04:02:30 +00:00
|
|
|
}
|
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
// relocate rotational transform, if necessary
|
|
|
|
if (!bChangedTransform) {
|
|
|
|
const angle = getRotationAngle(this.elem);
|
|
|
|
if (angle) {
|
|
|
|
const bbox = this.elem.getBBox();
|
|
|
|
const cx = bbox.x + bbox.width / 2,
|
|
|
|
cy = bbox.y + bbox.height / 2;
|
|
|
|
const rotate = ['rotate(', angle, ' ', cx, ',', cy, ')'].join('');
|
|
|
|
if (rotate !== this.elem.getAttribute('transform')) {
|
|
|
|
this.elem.setAttribute('transform', rotate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-18 04:02:30 +00:00
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
if (handler) {
|
|
|
|
handler.handleHistoryEvent(HistoryEventTypes.AFTER_APPLY, this);
|
|
|
|
}
|
2010-11-12 19:08:29 +00:00
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
return true;
|
2018-05-18 04:02:30 +00:00
|
|
|
}
|
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
// Reverses the stored change action
|
|
|
|
unapply (handler) {
|
|
|
|
if (handler) {
|
|
|
|
handler.handleHistoryEvent(HistoryEventTypes.BEFORE_UNAPPLY, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
let bChangedTransform = false;
|
|
|
|
for (const attr in this.oldValues) {
|
|
|
|
if (this.oldValues[attr]) {
|
|
|
|
if (attr === '#text') {
|
|
|
|
this.elem.textContent = this.oldValues[attr];
|
|
|
|
} else if (attr === '#href') {
|
|
|
|
setHref(this.elem, this.oldValues[attr]);
|
|
|
|
} else {
|
|
|
|
this.elem.setAttribute(attr, this.oldValues[attr]);
|
|
|
|
}
|
2018-05-18 04:02:30 +00:00
|
|
|
} else {
|
2018-05-18 03:25:45 +00:00
|
|
|
if (attr === '#text') {
|
|
|
|
this.elem.textContent = '';
|
|
|
|
} else {
|
|
|
|
this.elem.removeAttribute(attr);
|
|
|
|
}
|
2018-05-18 04:02:30 +00:00
|
|
|
}
|
2018-05-18 03:25:45 +00:00
|
|
|
if (attr === 'transform') { bChangedTransform = true; }
|
2018-05-18 04:02:30 +00:00
|
|
|
}
|
2018-05-18 03:25:45 +00:00
|
|
|
// relocate rotational transform, if necessary
|
|
|
|
if (!bChangedTransform) {
|
|
|
|
const angle = getRotationAngle(this.elem);
|
|
|
|
if (angle) {
|
|
|
|
const bbox = this.elem.getBBox();
|
|
|
|
const cx = bbox.x + bbox.width / 2,
|
|
|
|
cy = bbox.y + bbox.height / 2;
|
|
|
|
const rotate = ['rotate(', angle, ' ', cx, ',', cy, ')'].join('');
|
|
|
|
if (rotate !== this.elem.getAttribute('transform')) {
|
|
|
|
this.elem.setAttribute('transform', rotate);
|
|
|
|
}
|
2018-05-18 04:02:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
// Remove transformlist to prevent confusion that causes bugs like 575.
|
|
|
|
removeElementFromListMap(this.elem);
|
2018-05-18 04:02:30 +00:00
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
if (handler) {
|
|
|
|
handler.handleHistoryEvent(HistoryEventTypes.AFTER_UNAPPLY, this);
|
|
|
|
}
|
2018-05-18 04:02:30 +00:00
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
return true;
|
|
|
|
}
|
2010-11-12 19:08:29 +00:00
|
|
|
|
2018-05-29 12:31:24 +00:00
|
|
|
/**
|
|
|
|
* @returns {Array} Array with element associated with this command
|
|
|
|
*/
|
2018-05-18 03:25:45 +00:00
|
|
|
elements () {
|
|
|
|
return [this.elem];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ChangeElementCommand.type = ChangeElementCommand.prototype.type;
|
2010-11-12 19:08:29 +00:00
|
|
|
|
|
|
|
// TODO: create a 'typing' command object that tracks changes in text
|
|
|
|
// if a new Typing command is created and the top command on the stack is also a Typing
|
|
|
|
// and they both affect the same element, then collapse the two commands into one
|
|
|
|
|
2018-05-29 12:31:24 +00:00
|
|
|
/**
|
|
|
|
* @implements svgedit.history.HistoryCommand
|
|
|
|
* History command that can contain/execute multiple other commands
|
|
|
|
* @param {String} [text] - An optional string visible to user related to this change
|
|
|
|
*/
|
2018-05-18 03:25:45 +00:00
|
|
|
export class BatchCommand {
|
|
|
|
constructor (text) {
|
|
|
|
this.text = text || 'Batch Command';
|
|
|
|
this.stack = [];
|
2018-05-18 04:02:30 +00:00
|
|
|
}
|
2010-11-13 09:58:51 +00:00
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
type () {
|
|
|
|
return 'svgedit.history.BatchCommand';
|
2018-05-18 04:02:30 +00:00
|
|
|
}
|
2010-11-13 09:58:51 +00:00
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
getText () {
|
|
|
|
return this.text;
|
2018-05-18 04:02:30 +00:00
|
|
|
}
|
2010-11-12 19:08:29 +00:00
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
// Runs "apply" on all subcommands
|
|
|
|
apply (handler) {
|
|
|
|
if (handler) {
|
|
|
|
handler.handleHistoryEvent(HistoryEventTypes.BEFORE_APPLY, this);
|
|
|
|
}
|
2010-11-13 09:58:51 +00:00
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
const len = this.stack.length;
|
|
|
|
for (let i = 0; i < len; ++i) {
|
|
|
|
this.stack[i].apply(handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (handler) {
|
|
|
|
handler.handleHistoryEvent(HistoryEventTypes.AFTER_APPLY, this);
|
|
|
|
}
|
2018-05-18 04:02:30 +00:00
|
|
|
}
|
2010-11-13 09:58:51 +00:00
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
// Runs "unapply" on all subcommands
|
|
|
|
unapply (handler) {
|
|
|
|
if (handler) {
|
|
|
|
handler.handleHistoryEvent(HistoryEventTypes.BEFORE_UNAPPLY, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = this.stack.length - 1; i >= 0; i--) {
|
|
|
|
this.stack[i].unapply(handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (handler) {
|
|
|
|
handler.handleHistoryEvent(HistoryEventTypes.AFTER_UNAPPLY, this);
|
|
|
|
}
|
2018-05-18 04:02:30 +00:00
|
|
|
}
|
2010-11-12 19:08:29 +00:00
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
// Iterate through all our subcommands and returns all the elements we are changing
|
|
|
|
elements () {
|
|
|
|
const elems = [];
|
|
|
|
let cmd = this.stack.length;
|
|
|
|
while (cmd--) {
|
|
|
|
const thisElems = this.stack[cmd].elements();
|
|
|
|
let elem = thisElems.length;
|
|
|
|
while (elem--) {
|
|
|
|
if (!elems.includes(thisElems[elem])) { elems.push(thisElems[elem]); }
|
|
|
|
}
|
2018-05-18 04:02:30 +00:00
|
|
|
}
|
2018-05-18 03:25:45 +00:00
|
|
|
return elems;
|
2018-05-18 04:02:30 +00:00
|
|
|
}
|
2010-11-12 19:08:29 +00:00
|
|
|
|
2018-05-29 12:31:24 +00:00
|
|
|
/**
|
|
|
|
* Adds a given command to the history stack
|
|
|
|
* @param cmd - The undo command object to add
|
|
|
|
*/
|
2018-05-18 03:25:45 +00:00
|
|
|
addSubCommand (cmd) {
|
|
|
|
this.stack.push(cmd);
|
|
|
|
}
|
2010-11-12 19:08:29 +00:00
|
|
|
|
2018-05-29 12:31:24 +00:00
|
|
|
/**
|
|
|
|
* @returns {Boolean} Indicates whether or not the batch command is empty
|
|
|
|
*/
|
2018-05-18 03:25:45 +00:00
|
|
|
isEmpty () {
|
|
|
|
return !this.stack.length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
BatchCommand.type = BatchCommand.prototype.type;
|
2010-11-12 19:08:29 +00:00
|
|
|
|
2018-05-29 12:31:24 +00:00
|
|
|
/**
|
|
|
|
* @param historyEventHandler - an object that conforms to the HistoryEventHandler interface
|
|
|
|
* (see above)
|
|
|
|
*/
|
2018-05-18 03:25:45 +00:00
|
|
|
export class UndoManager {
|
|
|
|
constructor (historyEventHandler) {
|
|
|
|
this.handler_ = historyEventHandler || null;
|
|
|
|
this.undoStackPointer = 0;
|
|
|
|
this.undoStack = [];
|
2013-02-14 13:11:01 +00:00
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
// this is the stack that stores the original values, the elements and
|
|
|
|
// the attribute name for begin/finish
|
|
|
|
this.undoChangeStackPointer = -1;
|
|
|
|
this.undoableChangeStack = [];
|
|
|
|
}
|
2010-11-12 19:08:29 +00:00
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
// Resets the undo stack, effectively clearing the undo/redo history
|
|
|
|
resetUndoStack () {
|
|
|
|
this.undoStack = [];
|
|
|
|
this.undoStackPointer = 0;
|
|
|
|
}
|
2010-11-12 19:08:29 +00:00
|
|
|
|
2018-05-29 12:31:24 +00:00
|
|
|
/**
|
|
|
|
* @returns {Number} Integer with the current size of the undo history stack
|
|
|
|
*/
|
2018-05-18 03:25:45 +00:00
|
|
|
getUndoStackSize () {
|
|
|
|
return this.undoStackPointer;
|
|
|
|
}
|
2010-11-12 19:08:29 +00:00
|
|
|
|
2018-05-29 12:31:24 +00:00
|
|
|
/**
|
|
|
|
* @returns {Number} Integer with the current size of the redo history stack
|
|
|
|
*/
|
2018-05-18 03:25:45 +00:00
|
|
|
getRedoStackSize () {
|
|
|
|
return this.undoStack.length - this.undoStackPointer;
|
|
|
|
}
|
2010-11-12 19:08:29 +00:00
|
|
|
|
2018-05-29 12:31:24 +00:00
|
|
|
/**
|
|
|
|
* @returns {String} String associated with the next undo command
|
|
|
|
*/
|
2018-05-18 03:25:45 +00:00
|
|
|
getNextUndoCommandText () {
|
|
|
|
return this.undoStackPointer > 0 ? this.undoStack[this.undoStackPointer - 1].getText() : '';
|
2018-05-18 04:02:30 +00:00
|
|
|
}
|
2010-11-12 19:08:29 +00:00
|
|
|
|
2018-05-29 12:31:24 +00:00
|
|
|
/**
|
|
|
|
* @returns {String} String associated with the next redo command
|
|
|
|
*/
|
2018-05-18 03:25:45 +00:00
|
|
|
getNextRedoCommandText () {
|
|
|
|
return this.undoStackPointer < this.undoStack.length ? this.undoStack[this.undoStackPointer].getText() : '';
|
2018-05-18 04:02:30 +00:00
|
|
|
}
|
2013-02-14 13:11:01 +00:00
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
// Performs an undo step
|
|
|
|
undo () {
|
|
|
|
if (this.undoStackPointer > 0) {
|
|
|
|
const cmd = this.undoStack[--this.undoStackPointer];
|
|
|
|
cmd.unapply(this.handler_);
|
|
|
|
}
|
|
|
|
}
|
2010-11-12 19:08:29 +00:00
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
// Performs a redo step
|
|
|
|
redo () {
|
|
|
|
if (this.undoStackPointer < this.undoStack.length && this.undoStack.length > 0) {
|
|
|
|
const cmd = this.undoStack[this.undoStackPointer++];
|
|
|
|
cmd.apply(this.handler_);
|
|
|
|
}
|
|
|
|
}
|
2010-11-12 19:08:29 +00:00
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
/**
|
|
|
|
* Adds a command object to the undo history stack
|
|
|
|
* @param cmd - The command object to add
|
|
|
|
*/
|
|
|
|
addCommandToHistory (cmd) {
|
|
|
|
// FIXME: we MUST compress consecutive text changes to the same element
|
|
|
|
// (right now each keystroke is saved as a separate command that includes the
|
|
|
|
// entire text contents of the text element)
|
|
|
|
// TODO: consider limiting the history that we store here (need to do some slicing)
|
|
|
|
|
|
|
|
// if our stack pointer is not at the end, then we have to remove
|
|
|
|
// all commands after the pointer and insert the new command
|
|
|
|
if (this.undoStackPointer < this.undoStack.length && this.undoStack.length > 0) {
|
|
|
|
this.undoStack = this.undoStack.splice(0, this.undoStackPointer);
|
|
|
|
}
|
|
|
|
this.undoStack.push(cmd);
|
|
|
|
this.undoStackPointer = this.undoStack.length;
|
|
|
|
}
|
|
|
|
|
2018-05-29 12:31:24 +00:00
|
|
|
/**
|
|
|
|
* This function tells the canvas to remember the old values of the
|
|
|
|
* attrName attribute for each element sent in. The elements and values
|
|
|
|
* are stored on a stack, so the next call to finishUndoableChange() will
|
|
|
|
* pop the elements and old values off the stack, gets the current values
|
|
|
|
* from the DOM and uses all of these to construct the undo-able command.
|
|
|
|
* @param attrName - The name of the attribute being changed
|
|
|
|
* @param elems - Array of DOM elements being changed
|
|
|
|
*/
|
2018-05-18 03:25:45 +00:00
|
|
|
beginUndoableChange (attrName, elems) {
|
|
|
|
const p = ++this.undoChangeStackPointer;
|
|
|
|
let i = elems.length;
|
|
|
|
const oldValues = new Array(i), elements = new Array(i);
|
|
|
|
while (i--) {
|
|
|
|
const elem = elems[i];
|
|
|
|
if (elem == null) { continue; }
|
|
|
|
elements[i] = elem;
|
|
|
|
oldValues[i] = elem.getAttribute(attrName);
|
|
|
|
}
|
|
|
|
this.undoableChangeStack[p] = {
|
|
|
|
attrName,
|
|
|
|
oldValues,
|
|
|
|
elements
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-05-29 12:31:24 +00:00
|
|
|
/**
|
|
|
|
* This function returns a BatchCommand object which summarizes the
|
|
|
|
* change since beginUndoableChange was called. The command can then
|
|
|
|
* be added to the command history
|
|
|
|
* @returns Batch command object with resulting changes
|
|
|
|
*/
|
2018-05-18 03:25:45 +00:00
|
|
|
finishUndoableChange () {
|
|
|
|
const p = this.undoChangeStackPointer--;
|
|
|
|
const changeset = this.undoableChangeStack[p];
|
|
|
|
const {attrName} = changeset;
|
|
|
|
const batchCmd = new BatchCommand('Change ' + attrName);
|
|
|
|
let i = changeset.elements.length;
|
|
|
|
while (i--) {
|
|
|
|
const elem = changeset.elements[i];
|
|
|
|
if (elem == null) { continue; }
|
|
|
|
const changes = {};
|
|
|
|
changes[attrName] = changeset.oldValues[i];
|
|
|
|
if (changes[attrName] !== elem.getAttribute(attrName)) {
|
|
|
|
batchCmd.addSubCommand(new ChangeElementCommand(elem, changes, attrName));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.undoableChangeStack[p] = null;
|
|
|
|
return batchCmd;
|
|
|
|
}
|
|
|
|
}
|