@vericus/slate-kit-history
Version:
plugin that group together history of selections to the next editing event on slate for undo/redo
102 lines (82 loc) • 2.62 kB
JavaScript
exports.__esModule = true;
exports.default = void 0;
var _slateHotkeys = _interopRequireDefault(require("slate-hotkeys"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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 (_slateHotkeys.default.isUndo(e)) {
return editor.handleUndo();
}
if (_slateHotkeys.default.isRedo(e)) {
return editor.handleRedo();
}
return next();
}
return {
commands: commands,
queries: queries,
onKeyDown: onKeyDown
};
}
var _default = History;
exports.default = _default;