Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Editor fast follow #653

Merged
merged 8 commits into from
Oct 5, 2017
Prev Previous commit
force focus back to editor after undo/redo
  • Loading branch information
erquhart committed Oct 5, 2017
commit 65c9e9d5720bc1ca8cc569023e19de2c624d9b94
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,42 @@ import { Block, Text } from 'slate';

export default onKeyDown;

/**
* Minimal re-implementation of Slate's undo/redo functionality, but with focus
* forced back into editor afterward.
*/
function changeHistory(change, type) {

/**
* Get the history for undo or redo (determined via `type` param).
*/
const { history } = change.state;
if (!history) return;
const historyOfType = history[`${type}s`];

/**
* If there is a next history item to apply, and it's valid, apply it.
*/
const next = historyOfType.first();
const historyOfTypeIsValid = historyOfType.size > 1
|| next.length > 1
|| next[0].type !== 'set_selection';

if (next && historyOfTypeIsValid) {
change[type]();
}

/**
* Always ensure focus is set.
*/
return change.focus();
}

function onKeyDown(e, data, change) {
const createDefaultBlock = () => {
return Block.create({
type: 'paragraph',
nodes: [Text.create('')]
nodes: [Text.create('')],
});
};
if (data.key === 'enter') {
Expand Down Expand Up @@ -37,6 +68,22 @@ function onKeyDown(e, data, change) {
}

if (data.isMod) {

/**
* Undo and redo work automatically with Slate, but focus is lost in certain
* actions. We override Slate's built in undo/redo here and force focus
* back to the editor each time.
*/
if (data.key === 'y') {
e.preventDefault();
return changeHistory(change, 'redo');
}

if (data.key === 'z') {
e.preventDefault();
return changeHistory(change, data.isShift ? 'redo' : 'undo');
}

const marks = {
b: 'bold',
i: 'italic',
Expand Down