Skip to content

Commit

Permalink
Dist & docs
Browse files Browse the repository at this point in the history
  • Loading branch information
arnog committed Feb 13, 2019
1 parent 0dc06f3 commit 77d7bf9
Show file tree
Hide file tree
Showing 43 changed files with 314 additions and 84 deletions.
2 changes: 1 addition & 1 deletion dist/mathlive.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/mathlive.mjs

Large diffs are not rendered by default.

12 changes: 11 additions & 1 deletion dist/src/editor/editor-editableMathlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,11 @@ EditableMathlist.prototype.commitCommandStringBeforeInsertionPoint = function()
EditableMathlist.prototype.spliceCommandStringAroundInsertionPoint = function(mathlist) {
const command = this.commandOffsets();
if (command) {
// Dispatch notifications
if (typeof this.config.onContentWillChange === 'function' &&
!this.suppressContentChangeNotifications) {
this.config.onContentWillChange(this.target);
}
Array.prototype.splice.apply(this.siblings(),
[command.start, command.end - command.start].concat(mathlist));

Expand All @@ -901,6 +906,11 @@ EditableMathlist.prototype.spliceCommandStringAroundInsertionPoint = function(ma
if (newPlaceholders.length === 0 || !this.leap(+1, false)) {
this.setSelection(command.start + mathlist.length - 1);
}

// Dispatch notifications
if (typeof this.config.onContentDidChange === 'function' && !this.suppressContentChangeNotifications) {
this.config.onContentDidChange(this.target);
}
}
}

Expand Down Expand Up @@ -1799,7 +1809,7 @@ EditableMathlist.prototype._insertSmartFence = function(fence) {
if (rDelim && !(parent && (parent.type === 'leftright' && parent.leftDelim === '|'))) {
// We have a valid open fence as input
let s = '';
const collapsed = this.isCollapsed();
const collapsed = this.isCollapsed() || this.anchor().type === 'placeholder';

if (this.sibling(0).isFunction) {
// We're before a function (e.g. `\sin`)
Expand Down
69 changes: 34 additions & 35 deletions dist/src/editor/editor-mathfield.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,12 +313,9 @@ function MathField(element, config) {
this.latex(elementText);
}

// If fonts get loaded (which could happen as a result of the first pass
// rendering done in .latex()), render again.
// if (document && document.fonts) {
// const that = this;
// document.fonts.ready.then(() => that._render());
// }
// Now start recording potentially undoable actions
this.undoManager.startRecording();
this.undoManager.snapshot(this.config);
}

/**
Expand Down Expand Up @@ -923,26 +920,28 @@ MathField.prototype.$perform = function(command) {
selector += '_';

if (typeof this.mathlist[selector] === 'function') {
if (/^(delete|transpose|deleteToMathFieldEnd|deleteToGroupEnd|deleteToGroupStart|deletePreviousWord|deleteNextWord|deletePreviousChar|deleteNextChar)_$/.test(selector)) {
this.undoManager.snapshot(this.config);
if (/^(delete|transpose|add)/.test(selector)) {
if (this.selectionIsCollapsed() &&
selector === 'deletePreviousChar_' &&
this.config.inlineShortcutBackspaceCommand === 'undo') {
selector === 'deletePreviousChar_') {
this.inlineShortcutBuffer = this.inlineShortcutBuffer.substring(0, this.inlineShortcutBuffer.length - 1);
this.inlineShortcutStates.pop();
} else {
this._resetInlineShortcutBuffer();
}
}

if (/^(delete|transpose|add)/.test(selector) && this.mathlist.parseMode() !== 'command') {
// Update the undo state to account for the current selection
this.undoManager.pop();
this.undoManager.snapshot(this.config);
}
this.mathlist[selector](...args);
if (/^(delete|transpose|add)/.test(selector) && this.mathlist.parseMode() !== 'command') {
this.undoManager.snapshot(this.config);
}
dirty = true;
handled = true;
} else if (typeof this[selector] === 'function') {
if (selector === 'complete_') {
this.undoManager.snapshot(this.config);
}

dirty = this[selector](...args);

handled = true;
Expand All @@ -951,7 +950,7 @@ MathField.prototype.$perform = function(command) {
// If the command changed the selection so that it is no longer
// collapsed, or if it was an editing command, reset the inline
// shortcut buffer
if (!this.mathlist.isCollapsed() || /^(transpose|paste|((move|extent).*))_$/.test(selector)) {
if (!this.mathlist.isCollapsed() || /^(transpose|paste|complete|((move|extend).*))_$/.test(selector)) {
this._resetInlineShortcutBuffer();
}

Expand Down Expand Up @@ -1077,15 +1076,6 @@ MathField.prototype._onKeystroke = function(keystroke, evt) {
}
}
}
} else {
// If we're in the middle of a potential inline shortcut, treat
// Backspace as Undo. This deals with the case "pi<backspace>i"
if (this.config.inlineShortcutBackspaceCommand !== 'delete' &&
this.mathlist.isCollapsed() &&
this.inlineShortcutBuffer.length > 0) {
selector = this.config.inlineShortcutBackspaceCommand;
this._announce('delete');
}
}
}

Expand All @@ -1111,21 +1101,23 @@ MathField.prototype._onKeystroke = function(keystroke, evt) {
// 5.2 If we have a `moveAfterParent` selector (usually triggered with
// `spacebar), and we're at the end of a smart fence, close the fence with
// an empty (.) right delimiter
if (selector === 'moveAfterParent' && this.mathlist._insertSmartFence('.')) {
const parent = this.mathlist.parent();
if (selector === 'moveAfterParent' && parent &&
parent.type === 'leftright' &&
this.mathlist.endOffset() === this.mathlist.siblings().length - 1 &&
this.mathlist._insertSmartFence('.')) {
selector = '';
this._render(); // Re-render the closed smartfence
}
if (selector && !this.perform(selector)) {
if ((selector && !this.perform(selector)) || shortcut) {
// Perform the selector or insert the shortcut
if (shortcut) {
this.undoManager.snapshot(this.config);

// To enable the substitution to be undoable,
// insert the character before applying the substitution
this.mathlist.insert(Keyboard.eventToChar(evt));

// Create a snapshot with the inserted character
this.undoManager.snapshot(this.config);
this.undoManager.snapshotAndCoalesce(this.config);

// Revert to the state before the beginning of the shortcut
// (restore doesn't change the undo stack)
Expand All @@ -1135,6 +1127,7 @@ MathField.prototype._onKeystroke = function(keystroke, evt) {
if (!this.mathlist._insertSmartFence(shortcut)) {
this.mathlist.insert(shortcut, {format: 'latex'});
}
this.undoManager.snapshot(this.config);
this._render();
this._announce('replacement');

Expand Down Expand Up @@ -1262,7 +1255,6 @@ MathField.prototype._onTypedText = function(text, options) {
if (selector) {
this.perform(selector);
} else {
this.undoManager.snapshot(this.config);
if (!this.mathlist._insertSmartFence(c)) {
this.mathlist.insert(c);
}
Expand All @@ -1271,6 +1263,10 @@ MathField.prototype._onTypedText = function(text, options) {
}
}

if (this.mathlist.parseMode() !== 'command') {
this.undoManager.snapshotAndCoalesce(this.config);
}

// Render the mathlist
this._render();

Expand Down Expand Up @@ -1580,13 +1576,13 @@ MathField.prototype.$latex = function(text, options) {
const oldValue = this.mathlist.root.toLatex();
if (text !== oldValue) {
options = options || {};
this.undoManager.snapshot(this.config);
this.mathlist.insert(text, Object.assign(this.config, {
insertionMode: 'replaceAll',
selectionMode: 'after',
format: 'latex',
suppressContentChangeNotifications: options.suppressContentChangeNotifications
}));
this.undoManager.snapshot(this.config);
this._render();
}
return text;
Expand All @@ -1610,11 +1606,15 @@ MathField.prototype.$el = function() {
}

MathField.prototype.undo_ = MathField.prototype.undo = function() {
this.complete_();

// Undo to the previous state
this.undoManager.undo(this.config);
return true;
}

MathField.prototype.redo_ = MathField.prototype.redo = function() {
this.complete_();
this.undoManager.redo(this.config);
return true;
}
Expand Down Expand Up @@ -1653,7 +1653,6 @@ MathField.prototype.enterCommandMode_ = function() {
this.switchKeyboardLayer_('lower-command');
}

this.undoManager.snapshot(this.config);
this.mathlist.insert('\u001b');
return true;
}
Expand Down Expand Up @@ -1728,7 +1727,6 @@ MathField.prototype.$insert = function(s, options) {
this.keypressSound.play();
}
}
this.undoManager.snapshot(this.config);
if (s === '\\\\') {
// This string is interpreted as an "insert row after" command
this.mathlist.addRowAfter_();
Expand All @@ -1737,6 +1735,7 @@ MathField.prototype.$insert = function(s, options) {
} else {
this.mathlist.insert(s, options);
}
this.undoManager.snapshot(this.config);
return true;
}
return false;
Expand Down Expand Up @@ -1780,6 +1779,7 @@ MathField.prototype.complete_ = function() {
}
}
}
this.undoManager.snapshot(this.config);
this._announce('replacement');
return true;
}
Expand Down Expand Up @@ -2371,8 +2371,8 @@ MathField.prototype.toggleVirtualKeyboard_ = function(theme) {
}

MathField.prototype.applyStyle_ = function(style) {
this.undoManager.snapshot(this.config);
this.mathlist._applyStyle(style);
this.undoManager.snapshot(this.config);
return true;
}

Expand Down Expand Up @@ -2470,7 +2470,6 @@ MathField.prototype.$setConfig = function(conf) {
this.config = {
smartFence: true,
removeExtraneousParentheses: false,
inlineShortcutBackspaceCommand: 'undo',
overrideDefaultInlineShortcuts: false,
virtualKeyboard: '',
virtualKeyboardLayout: 'qwerty',
Expand Down
42 changes: 39 additions & 3 deletions dist/src/editor/editor-undo.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,17 @@
class UndoManager {
constructor(mathlist) {
this.mathlist = mathlist;
this.maximumDepth = 1000;
this.record = false;
this.canCoalesce = false;
this.reset();
}
reset() {
this.stack = [];
this.index = -1;
this.maximumDepth = 1000;
}
startRecording() {
this.record = true;
}
/**
*
Expand All @@ -23,7 +31,7 @@ class UndoManager {
* @private
*/
canUndo() {
return this.index >= 0;
return this.index > 0;
}
/**
*
Expand All @@ -46,11 +54,12 @@ class UndoManager {
if (options && typeof options.onUndoStateWillChange === 'function') {
options.onUndoStateWillChange(this.mathlist.target, 'undo');
}
this.restore(this.stack[this.index], options);
this.restore(this.stack[this.index - 1], options);
this.index -= 1;
if (options && typeof options.onUndoStateDidChange === 'function') {
options.onUndoStateDidChange(this.mathlist.target, 'undo');
}
this.canCoalesce = false;
}
}
/**
Expand All @@ -69,6 +78,18 @@ class UndoManager {
if (options && typeof options.onUndoStateDidChange === 'function') {
options.onUndoStateDidChange(this.mathlist.target, 'redo');
}
this.canCoalesce = false;
}
}
/**
*
* @memberof UndoManager
* @instance
* @private
*/
pop() {
if (this.canUndo()) {
this.index -= 1;
}
}
/**
Expand All @@ -79,6 +100,8 @@ class UndoManager {
* @private
*/
snapshot(options) {
if (!this.record) return;

if (options && options.onUndoStateWillChange === 'function') {
options.onUndoStateWillChange(this.mathlist.target, 'snapshot');
}
Expand All @@ -89,6 +112,7 @@ class UndoManager {
latex: this.mathlist.root.toLatex(),
selection: this.mathlist.toString()
});

this.index++;
// If we've reached the maximum number of undo operations, forget the
// oldest one.
Expand All @@ -98,6 +122,18 @@ class UndoManager {
if (options && typeof options.onUndoStateDidChange === 'function') {
options.onUndoStateDidChange(this.mathlist.target, 'snapshot');
}
this.canCoalesce = false;
}
/**
*
* @param {Object.<any>} options
*/
snapshotAndCoalesce(options) {
if (this.canCoalesce) {
this.pop();
}
this.snapshot(options);
this.canCoalesce = true;
}
/**
* Return an object capturing the state of the content and selection of the
Expand Down
2 changes: 1 addition & 1 deletion docs/Context.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/EditableMathlist.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/MathAtom.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/MathField.html

Large diffs are not rendered by default.

192 changes: 191 additions & 1 deletion docs/UndoManager.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/index.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/module-addons_debug.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/module-addons_maston.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/module-addons_outputLatex.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/module-addons_outputMathML.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/module-core_color.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/module-core_definitions.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/module-core_delimiters.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/module-core_fontMetrics.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/module-core_lexer.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/module-core_lexer_Lexer.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/module-core_lexer_Token.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/module-core_mathatom.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/module-core_mathstyle.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/module-core_mathstyle_Mathstyle.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/module-core_parser.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/module-core_parser_Parser.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/module-core_span.Span.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/module-core_span.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/module-editor_editableMathlist.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/module-editor_keyboard.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/module-editor_mathfield.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/module-editor_mathpath.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/module-editor_shortcuts.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/module-editor_virtualKeyboard.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/module-mathlive.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/tutorial-COMMANDS_REFERENCE.html

Large diffs are not rendered by default.

7 changes: 1 addition & 6 deletions docs/tutorial-CONFIG.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/tutorial-CONTRIBUTOR_GUIDE.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/tutorial-EXAMPLES.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/tutorial-MASTON.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/tutorial-SELECTORS.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/tutorial-USAGE_GUIDE.html

Large diffs are not rendered by default.

0 comments on commit 77d7bf9

Please sign in to comment.