- 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
161 lines
6.2 KiB
JavaScript
161 lines
6.2 KiB
JavaScript
/**
|
|
* HistoryRecordingService component of history
|
|
* @module history
|
|
* @license MIT
|
|
* @copyright 2016 Flint O'Brien
|
|
*/
|
|
|
|
import {
|
|
BatchCommand, MoveElementCommand, InsertElementCommand, RemoveElementCommand,
|
|
ChangeElementCommand
|
|
} from './history.js';
|
|
|
|
/**
|
|
* History recording service.
|
|
*
|
|
* A self-contained service interface for recording history. Once injected, no other dependencies
|
|
* or globals are required (example: UndoManager, command types, etc.). Easy to mock for unit tests.
|
|
* Built on top of history classes in history.js.
|
|
*
|
|
* There is a simple start/end interface for batch commands.
|
|
*
|
|
* HistoryRecordingService.NO_HISTORY is a singleton that can be passed in to functions
|
|
* that record history. This helps when the caller requires that no history be recorded.
|
|
*
|
|
* The following will record history: insert, batch, insert.
|
|
* @example
|
|
* hrService = new history.HistoryRecordingService(this.undoMgr);
|
|
* hrService.insertElement(elem, text); // add simple command to history.
|
|
* hrService.startBatchCommand('create two elements');
|
|
* hrService.changeElement(elem, attrs, text); // add to batchCommand
|
|
* hrService.changeElement(elem, attrs2, text); // add to batchCommand
|
|
* hrService.endBatchCommand(); // add batch command with two change commands to history.
|
|
* hrService.insertElement(elem, text); // add simple command to history.
|
|
*
|
|
* @example
|
|
* // Note that all functions return this, so commands can be chained, like so:
|
|
* hrService
|
|
* .startBatchCommand('create two elements')
|
|
* .insertElement(elem, text)
|
|
* .changeElement(elem, attrs, text)
|
|
* .endBatchCommand();
|
|
*
|
|
* @memberof module:history
|
|
*/
|
|
class HistoryRecordingService {
|
|
/**
|
|
* @param {history.UndoManager|null} undoManager - The undo manager.
|
|
* A value of `null` is valid for cases where no history recording is required.
|
|
* See singleton: {@link module:history.HistoryRecordingService.HistoryRecordingService.NO_HISTORY}
|
|
*/
|
|
constructor (undoManager) {
|
|
this.undoManager_ = undoManager;
|
|
this.currentBatchCommand_ = null;
|
|
this.batchCommandStack_ = [];
|
|
}
|
|
|
|
/**
|
|
* Start a batch command so multiple commands can recorded as a single history command.
|
|
* Requires a corresponding call to endBatchCommand. Start and end commands can be nested.
|
|
*
|
|
* @param {string} text - Optional string describing the batch command.
|
|
* @returns {module:history.HistoryRecordingService}
|
|
*/
|
|
startBatchCommand (text) {
|
|
if (!this.undoManager_) { return this; }
|
|
this.currentBatchCommand_ = new BatchCommand(text);
|
|
this.batchCommandStack_.push(this.currentBatchCommand_);
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* End a batch command and add it to the history or a parent batch command.
|
|
* @returns {module:history.HistoryRecordingService}
|
|
*/
|
|
endBatchCommand () {
|
|
if (!this.undoManager_) { return this; }
|
|
if (this.currentBatchCommand_) {
|
|
const batchCommand = this.currentBatchCommand_;
|
|
this.batchCommandStack_.pop();
|
|
const {length} = this.batchCommandStack_;
|
|
this.currentBatchCommand_ = length ? this.batchCommandStack_[length - 1] : null;
|
|
this.addCommand_(batchCommand);
|
|
}
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* Add a MoveElementCommand to the history or current batch command
|
|
* @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
|
|
* @returns {module:history.HistoryRecordingService}
|
|
*/
|
|
moveElement (elem, oldNextSibling, oldParent, text) {
|
|
if (!this.undoManager_) { return this; }
|
|
this.addCommand_(new MoveElementCommand(elem, oldNextSibling, oldParent, text));
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* Add an InsertElementCommand to the history or current batch command
|
|
* @param {Element} elem - The DOM element that was added
|
|
* @param {string} [text] - An optional string visible to user related to this change
|
|
* @returns {module:history.HistoryRecordingService}
|
|
*/
|
|
insertElement (elem, text) {
|
|
if (!this.undoManager_) { return this; }
|
|
this.addCommand_(new InsertElementCommand(elem, text));
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* Add a RemoveElementCommand to the history or current batch command
|
|
* @param {Element} elem - The DOM element that was removed
|
|
* @param {Element} oldNextSibling - The element's next sibling before it was removed
|
|
* @param {Element} oldParent - The element's parent before it was removed
|
|
* @param {string} [text] - An optional string visible to user related to this change
|
|
* @returns {module:history.HistoryRecordingService}
|
|
*/
|
|
removeElement (elem, oldNextSibling, oldParent, text) {
|
|
if (!this.undoManager_) { return this; }
|
|
this.addCommand_(new RemoveElementCommand(elem, oldNextSibling, oldParent, text));
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* Add a ChangeElementCommand to the history or current batch command
|
|
* @param {Element} elem - The DOM element that was changed
|
|
* @param {module:history.CommandAttributes} 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
|
|
* @returns {module:history.HistoryRecordingService}
|
|
*/
|
|
changeElement (elem, attrs, text) {
|
|
if (!this.undoManager_) { return this; }
|
|
this.addCommand_(new ChangeElementCommand(elem, attrs, text));
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* Private function to add a command to the history or current batch command.
|
|
* @private
|
|
* @param {Command} cmd
|
|
* @returns {module:history.HistoryRecordingService}
|
|
*/
|
|
addCommand_ (cmd) {
|
|
if (!this.undoManager_) { return this; }
|
|
if (this.currentBatchCommand_) {
|
|
this.currentBatchCommand_.addSubCommand(cmd);
|
|
} else {
|
|
this.undoManager_.addCommandToHistory(cmd);
|
|
}
|
|
}
|
|
}
|
|
/**
|
|
* @memberof module:history.HistoryRecordingService
|
|
* @property {module:history.HistoryRecordingService} NO_HISTORY - Singleton that can be passed to functions that record history, but the caller requires that no history be recorded.
|
|
*/
|
|
HistoryRecordingService.NO_HISTORY = new HistoryRecordingService();
|
|
export default HistoryRecordingService;
|