UNPKG

slate-history

Version:

An operation-based history implementation for Slate editors.

275 lines (269 loc) • 9.12 kB
'use strict'; var slate = require('slate'); // eslint-disable-next-line no-redeclare var History = { /** * Check if a value is a `History` object. */ isHistory: function isHistory(value) { return slate.isObject(value) && Array.isArray(value.redos) && Array.isArray(value.undos) && (value.redos.length === 0 || slate.Operation.isOperationList(value.redos[0].operations)) && (value.undos.length === 0 || slate.Operation.isOperationList(value.undos[0].operations)); } }; /** * Weakmaps for attaching state to the editor. */ var HISTORY = new WeakMap(); var SAVING = new WeakMap(); var MERGING = new WeakMap(); var SPLITTING_ONCE = new WeakMap(); // eslint-disable-next-line no-redeclare var HistoryEditor = { /** * Check if a value is a `HistoryEditor` object. */ isHistoryEditor: function isHistoryEditor(value) { return History.isHistory(value.history) && slate.Editor.isEditor(value); }, /** * Get the merge flag's current value. */ isMerging: function isMerging(editor) { return MERGING.get(editor); }, /** * Get the splitting once flag's current value. */ isSplittingOnce: function isSplittingOnce(editor) { return SPLITTING_ONCE.get(editor); }, setSplittingOnce: function setSplittingOnce(editor, value) { SPLITTING_ONCE.set(editor, value); }, /** * Get the saving flag's current value. */ isSaving: function isSaving(editor) { return SAVING.get(editor); }, /** * Redo to the previous saved state. */ redo: function redo(editor) { editor.redo(); }, /** * Undo to the previous saved state. */ undo: function undo(editor) { editor.undo(); }, /** * Apply a series of changes inside a synchronous `fn`, These operations will * be merged into the previous history. */ withMerging: function withMerging(editor, fn) { var prev = HistoryEditor.isMerging(editor); MERGING.set(editor, true); try { fn(); } finally { MERGING.set(editor, prev); } }, /** * Apply a series of changes inside a synchronous `fn`, ensuring that the first * operation starts a new batch in the history. Subsequent operations will be * merged as usual. */ withNewBatch: function withNewBatch(editor, fn) { var prevMerging = HistoryEditor.isMerging(editor); var prevSplitting = HistoryEditor.isSplittingOnce(editor); MERGING.set(editor, true); SPLITTING_ONCE.set(editor, true); try { fn(); } finally { MERGING.set(editor, prevMerging); if (!prevSplitting) { SPLITTING_ONCE["delete"](editor); } } }, /** * Apply a series of changes inside a synchronous `fn`, without merging any of * the new operations into previous save point in the history. */ withoutMerging: function withoutMerging(editor, fn) { var prev = HistoryEditor.isMerging(editor); MERGING.set(editor, false); try { fn(); } finally { MERGING.set(editor, prev); } }, /** * Apply a series of changes inside a synchronous `fn`, without saving any of * their operations into the history. */ withoutSaving: function withoutSaving(editor, fn) { var prev = HistoryEditor.isSaving(editor); SAVING.set(editor, false); try { fn(); } finally { SAVING.set(editor, prev); } } }; function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: true } : { done: false, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = true, u = false; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = true, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } /** * The `withHistory` plugin keeps track of the operation history of a Slate * editor as operations are applied to it, using undo and redo stacks. * * If you are using TypeScript, you must extend Slate's CustomTypes to use * this plugin. * * See https://docs.slatejs.org/concepts/11-typescript to learn how. */ var withHistory = function withHistory(editor) { var e = editor; var apply = e.apply; e.history = { undos: [], redos: [] }; e.redo = function () { var history = e.history; var redos = history.redos; if (redos.length > 0) { var batch = redos[redos.length - 1]; if (batch.selectionBefore) { slate.Transforms.select(e, batch.selectionBefore); } HistoryEditor.withoutSaving(e, function () { slate.Editor.withoutNormalizing(e, function () { var _iterator = _createForOfIteratorHelper(batch.operations), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var op = _step.value; e.apply(op); } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } }); }); history.redos.pop(); e.writeHistory('undos', batch); } }; e.undo = function () { var history = e.history; var undos = history.undos; if (undos.length > 0) { var batch = undos[undos.length - 1]; HistoryEditor.withoutSaving(e, function () { slate.Editor.withoutNormalizing(e, function () { var inverseOps = batch.operations.map(slate.Operation.inverse).reverse(); var _iterator2 = _createForOfIteratorHelper(inverseOps), _step2; try { for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) { var op = _step2.value; e.apply(op); } } catch (err) { _iterator2.e(err); } finally { _iterator2.f(); } if (batch.selectionBefore) { slate.Transforms.select(e, batch.selectionBefore); } }); }); e.writeHistory('redos', batch); history.undos.pop(); } }; e.apply = function (op) { var operations = e.operations, history = e.history; var undos = history.undos; var lastBatch = undos[undos.length - 1]; var lastOp = lastBatch && lastBatch.operations[lastBatch.operations.length - 1]; var save = HistoryEditor.isSaving(e); var merge = HistoryEditor.isMerging(e); if (save == null) { save = shouldSave(op); } if (save) { if (merge == null) { if (lastBatch == null) { merge = false; } else if (operations.includes(lastOp)) { merge = true; } else { merge = shouldMerge(op, lastOp); } } if (HistoryEditor.isSplittingOnce(e)) { merge = false; HistoryEditor.setSplittingOnce(e, undefined); } if (lastBatch && merge) { lastBatch.operations.push(op); } else { var batch = { operations: [op], selectionBefore: e.selection }; e.writeHistory('undos', batch); } while (undos.length > 100) { undos.shift(); } history.redos = []; } apply(op); }; e.writeHistory = function (stack, batch) { e.history[stack].push(batch); }; return e; }; /** * Check whether to merge an operation into the previous operation. */ var shouldMerge = function shouldMerge(op, prev) { if (prev && op.type === 'insert_text' && prev.type === 'insert_text' && op.offset === prev.offset + prev.text.length && slate.Path.equals(op.path, prev.path)) { return true; } if (prev && op.type === 'remove_text' && prev.type === 'remove_text' && op.offset + op.text.length === prev.offset && slate.Path.equals(op.path, prev.path)) { return true; } return false; }; /** * Check whether an operation needs to be saved to the history. */ var shouldSave = function shouldSave(op, prev) { if (op.type === 'set_selection') { return false; } return true; }; exports.HISTORY = HISTORY; exports.History = History; exports.HistoryEditor = HistoryEditor; exports.MERGING = MERGING; exports.SAVING = SAVING; exports.SPLITTING_ONCE = SPLITTING_ONCE; exports.withHistory = withHistory; //# sourceMappingURL=index.js.map