Skip to content

Commit

Permalink
Update trumbowyg.history.js Tried to Fix the Undo and Redo Button whi…
Browse files Browse the repository at this point in the history
…ch had issues
  • Loading branch information
rockO7 authored Jun 2, 2024
1 parent b0f8963 commit 878c95b
Showing 1 changed file with 13 additions and 45 deletions.
58 changes: 13 additions & 45 deletions plugins/history/trumbowyg.history.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
var newState = t.o.plugins.history._stack[index];

t.execCmd('html', newState);
// because of some semantic optimisations we have to save the state back
// to history
// Save the state back to history
t.o.plugins.history._stack[index] = t.$ed.html();

carretToEnd();
Expand All @@ -53,8 +52,7 @@
newState = t.o.plugins.history._stack[index];

t.execCmd('html', newState);
// because of some semantic optimisations we have to save the state back
// to history
// Save the state back to history
t.o.plugins.history._stack[index] = t.$ed.html();

carretToEnd();
Expand All @@ -71,39 +69,27 @@
newState = t.$ed.html(),
focusEl = t.doc.getSelection().focusNode,
focusElText = '',
latestStateTagsList,
newStateTagsList,
latestStateText = $(latestState).text(),
newStateText = $(newState).text(),
prevFocusEl = t.o.plugins.history._focusEl;

latestStateTagsList = $('<div>' + latestState + '</div>').find('*').map(function () {
return this.localName;
});
newStateTagsList = $('<div>' + newState + '</div>').find('*').map(function () {
return this.localName;
});
if (focusEl) {
t.o.plugins.history._focusEl = focusEl;
focusElText = focusEl.outerHTML || focusEl.textContent;
}

if (newState !== prevState) {
// a new stack entry is defined when current insert ends on a whitespace character
// or count of node elements has been changed
// or focused element differs from previous one
if (newStateText !== $(prevState).text()) {
// Check if the state has changed by comparing the text content
if (focusElText.slice(-1).match(/\s/) ||
!arraysAreIdentical(latestStateTagsList, newStateTagsList) ||
t.o.plugins.history._index <= 0 || focusEl !== prevFocusEl)
{
latestStateText !== newStateText ||
t.o.plugins.history._index <= 0 || focusEl !== prevFocusEl) {
t.o.plugins.history._index += 1;
// remove newer entries in history when something new was added
// because timeline was changes with interaction
t.o.plugins.history._stack = stack.slice(
0, t.o.plugins.history._index
);
// now add new state to modified history
// Remove newer entries in history when something new was added
t.o.plugins.history._stack = stack.slice(0, t.o.plugins.history._index);
// Add new state to modified history
t.o.plugins.history._stack.push(newState);
} else {
// modify last stack entry
// Modify last stack entry
t.o.plugins.history._stack[index] = newState;
}

Expand Down Expand Up @@ -131,25 +117,6 @@
}
};

var arraysAreIdentical = function (a, b) {
if (a === b) {
return true;
}
if (a == null || b == null) {
return false;
}
if (a.length !== b.length) {
return false;
}

for (var i = 0; i < a.length; i += 1) {
if (a[i] !== b[i]) {
return false;
}
}
return true;
};

var carretToEnd = function () {
var node = t.doc.getSelection().focusNode,
range = t.doc.createRange();
Expand All @@ -171,3 +138,4 @@
}
});
})(jQuery);

0 comments on commit 878c95b

Please sign in to comment.