MCPcopy
hub / github.com/autoNumeric/autoNumeric / _historyTableAdd

Method _historyTableAdd

src/AutoNumeric.js:1648–1701  ·  view source on GitHub ↗

* Save the current raw value into the history table, along with the selection information. * * If the user has done some undos and tries to enter: * - a new and different number than the 'next' state, this drops the rest of the history table * - the very same number that result i

()

Source from the content-addressed store, hash-verified

1646 * @private
1647 */
1648 _historyTableAdd() {
1649 //TODO Add a `this.settings.saveSelectionsIntoHistory` option to prevent saving the selections (in order to gain performance)
1650 const isEmptyHistoryTable = this.historyTable.length === 0;
1651 // Only add a new value if it's different from the previous one (to prevent infinitely adding values on mouseover for instance)
1652 if (isEmptyHistoryTable || this.rawValue !== this._historyTableCurrentValueUsed()) {
1653 // Trim the history table if the user changed the value of an intermediary state
1654 let addNewHistoryState = true;
1655 if (!isEmptyHistoryTable) {
1656 // If some undo has been done and the user type the exact same data than the next entry after the current history pointer, do not drop the rest of the 'redo' list, and just advance the historyTableIndex
1657 const nextHistoryStateIndex = this.historyTableIndex + 1;
1658 if (nextHistoryStateIndex < this.historyTable.length && this.rawValue === this.historyTable[nextHistoryStateIndex].value) {
1659 // If the character input result in the same state as the next one, do not remove the next history states nor add a new one
1660 addNewHistoryState = false;
1661 } else {
1662 // First remove anything that is after the current index
1663 AutoNumericHelper.arrayTrim(this.historyTable, this.historyTableIndex + 1);
1664 }
1665 }
1666
1667 // Update the history pointer
1668 this.historyTableIndex++;
1669
1670 // Add the new history state, if needed
1671 if (addNewHistoryState) {
1672 // Save the selection info
1673 const selection = AutoNumericHelper.getElementSelection(this.domElement);
1674 this.selectionStart = selection.start;
1675 this.selectionEnd = selection.end;
1676
1677 // Then add the new raw value
1678 this.historyTable.push({
1679 // Save the rawValue and selection start/end
1680 value: this.rawValue,
1681 // The selection for this element is temporary, and will be updated when the next history state will be recorded.
1682 // That way, we are always sure we save the last caret or selection positions just before the value is changed. Otherwise, we would only save those positions when the value is first changed, and would not take into account that the user could move the caret around afterward.
1683 // For instance, this is needed if the user change the element value, and immediately undo it ; if he then does a redo, he'll see the value and the right selection
1684 // To sum up; The selection position are not always +1 character, since it could also be '2' if a group separator is added when entering one character. That's why the current history state caret/selection position is updated on each `keyup` event.
1685 start: this.selectionStart + 1, // Here we add one since the user added one character too
1686 end : this.selectionEnd + 1,
1687 });
1688
1689 // Update the selection in the previous entry, in order to keep track of the updated caret/selection positions
1690 if (this.historyTable.length > 1) {
1691 this.historyTable[this.historyTableIndex - 1].start = this.selectionStart;
1692 this.historyTable[this.historyTableIndex - 1].end = this.selectionEnd;
1693 }
1694 }
1695
1696 // Limit the history table size according to the `historySize` option
1697 if (this.historyTable.length > this.settings.historySize) {
1698 this._historyTableForget();
1699 }
1700 }
1701 }
1702
1703 /**
1704 * Debug function for the history table

Callers 1

_setRawValueMethod · 0.95

Calls 4

_historyTableForgetMethod · 0.95
arrayTrimMethod · 0.80
getElementSelectionMethod · 0.80

Tested by

no test coverage detected