UNPKG

slate-history

Version:

An operation-based history implementation for Slate editors.

258 lines (244 loc) 9.4 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('slate')) : typeof define === 'function' && define.amd ? define(['exports', 'slate'], factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.SlateHistory = {}, global.Slate)); })(this, (function (exports, slate) { 'use strict'; /*! * is-plain-object <https://github.com/jonschlinkert/is-plain-object> * * Copyright (c) 2014-2017, Jon Schlinkert. * Released under the MIT License. */ function isObject(o) { return Object.prototype.toString.call(o) === '[object Object]'; } function isPlainObject(o) { var ctor,prot; if (isObject(o) === false) return false; // If has modified constructor ctor = o.constructor; if (ctor === undefined) return true; // If has modified prototype prot = ctor.prototype; if (isObject(prot) === false) return false; // If constructor does not have an Object-specific method if (prot.hasOwnProperty('isPrototypeOf') === false) { return false; } // Most likely a plain Object return true; } // eslint-disable-next-line no-redeclare var History = { /** * Check if a value is a `History` object. */ isHistory: function isHistory(value) { return isPlainObject(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(); // 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 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`, 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); fn(); 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); fn(); SAVING.set(editor, prev); } }; function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, 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 normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } /** * 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.setSelection(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.setSelection(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.length !== 0) { merge = true; } else { merge = shouldMerge(op, lastOp); } } 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.withHistory = withHistory; }));