@vericus/slate-kit-history
Version:
plugin that group together history of selections to the next editing event on slate for undo/redo
96 lines (79 loc) • 2.43 kB
JavaScript
import hotkeys from 'slate-hotkeys';
function handleUndo(editor) {
if (!editor.hasUndo()) return;
editor.undo();
if (editor.props.onUndo && typeof editor.props.onUndo === "function") {
editor.props.onUndo(editor.operations.filter(function (operation) {
return !!(operation && operation.type !== "set_value");
}));
}
}
function handleRedo(editor) {
if (!editor.hasRedo()) return;
editor.redo();
if (editor.props.onRedo && typeof editor.props.onRedo === "function") {
editor.props.onRedo(editor.operations.filter(function (operation) {
return !!(operation && operation.type !== "set_value");
}));
}
}
function createCommands() {
return {
handleUndo: handleUndo,
handleRedo: handleRedo
};
}
function hasRedo(editor, value) {
var redos = value ? value.data.get("redos") : editor.value.data.get("redos");
return !(redos && redos.filter(function (redo) {
return !(redo.size === 1 && redo.get(0).type === "set_selection");
}).isEmpty());
}
function hasUndo(editor, value) {
var undos = value ? value.data.get("undos") : editor.value.data.get("undos");
return !(undos && undos.filter(function (undo) {
return !(undo.size === 1 && (undo.get(0).type === "set_selection" || undo.get(0).type === "set_value"));
}).isEmpty());
}
function isRedo(prevValue, currValue) {
var currRedos = currValue.data.get("redos");
var prevRedos = prevValue.data.get("redos");
return prevRedos.size > currRedos.size;
}
function isUndo(prevValue, currValue) {
var currUndos = currValue.data.get("undos");
var prevUndos = prevValue.data.get("undos");
return prevUndos.size > currUndos.size;
}
function createQueries() {
return {
hasRedo: hasRedo,
hasUndo: hasUndo,
isRedo: function isRedo$1(editor, prevValue, currValue) {
return isRedo(prevValue, currValue);
},
isUndo: function isUndo$1(editor, prevValue, currValue) {
return isUndo(prevValue, currValue);
}
};
}
function History() {
var queries = createQueries();
var commands = createCommands();
function onKeyDown(e, editor, next) {
if (hotkeys.isUndo(e)) {
return editor.handleUndo();
}
if (hotkeys.isRedo(e)) {
return editor.handleRedo();
}
return next();
}
return {
commands: commands,
queries: queries,
onKeyDown: onKeyDown
};
}
export default History;
//# sourceMappingURL=slate-kit-history.es.js.map