Skip to content
This repository has been archived by the owner on Apr 2, 2021. It is now read-only.

Undoing back to zero loses selection #709

Merged
merged 2 commits into from Apr 21, 2015
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 20 additions & 18 deletions src/wymeditor/editor/undo-redo.js
Expand Up @@ -22,10 +22,6 @@ WYMeditor.UndoRedo = function (wym) {

undoRedo.wym = wym;

// https://github.com/mightyiam/object-history
undoRedo.history = new WYMeditor.EXTERNAL_MODULES
.ObjectHistory(wym.getCurrentState());

wym.keyboard.combokeys.bind(
"mod+z",
function () {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This always produces a first undo point that is lacking selection.

That means that when undoing all the way back to the first point makes the selection go away, meaning the user must place the caret in order to type characters again.

Expand All @@ -48,32 +44,28 @@ WYMeditor.UndoRedo = function (wym) {
WYMeditor.UndoRedo._onBodyFocus
===============================

This method is called on focus of the document's body.
This method should be called on focus of the document's body.

If the last saved history point does not include a saved selection
then the last saved history point is replaced with the current state,
which does include a selection.
It makes sure that the first undo point contains a selection.

When this method is called by the triggering of the focus event,
the selection is not yet made.
The first selection is made along with the first focus.

Hence the use of `setTimeout`--the function provided to `setTimeout`
will always be called after the event's native action. By that time
there should be a selection.
This method will run before the native action (in which the selection
is made). Therefore the instantiation of object-history is called using
`setTimeout`.
*/
WYMeditor.UndoRedo.prototype._onBodyFocus = function () {
var undoRedo = this,
wym = undoRedo.wym;

// TODO: the `current` property is private API.
if (undoRedo.history.current.savedSelection) {
// last history point has selection
if (undoRedo.history) {
return;
}

setTimeout(function () {
// this will run after the native action of the triggering event.
undoRedo.history.current = wym.getCurrentState();
undoRedo.history = new WYMeditor.EXTERNAL_MODULES
// https://github.com/mightyiam/object-history
.ObjectHistory(wym.getCurrentState());
}, 0);
};

Expand All @@ -88,6 +80,11 @@ WYMeditor.UndoRedo.prototype._add = function () {
var undoRedo = this,
wym = undoRedo.wym;

if (!undoRedo.history) {
// history was not instantiated yet
return;
}

undoRedo.history.add(wym.getCurrentState());
undoRedo.hasUnregisteredModification = false;
};
Expand All @@ -109,6 +106,11 @@ WYMeditor.UndoRedo.prototype._do = function (what) {
state,
postEventName;

if (!undoRedo.history) {
// history was not instantiated yet
return;
}

if (what === WYMeditor.UndoRedo.UN) {
if (history.lengthBackward() === 0) {
return;
Expand Down