| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- (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;
- }
- 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])) && (value.undos.length === 0 || slate.Operation.isOperationList(value.undos[0]));
- }
- };
- /**
- * Weakmaps for attaching state to the editor.
- */
- var HISTORY = new WeakMap();
- var SAVING = new WeakMap();
- var MERGING = new WeakMap();
- 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];
- HistoryEditor.withoutSaving(e, function () {
- slate.Editor.withoutNormalizing(e, function () {
- var _iterator = _createForOfIteratorHelper(batch),
- _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();
- history.undos.push(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.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();
- }
- });
- });
- history.redos.push(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[lastBatch.length - 1];
- var overwrite = shouldOverwrite(op, lastOp);
- 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) || overwrite;
- }
- }
- if (lastBatch && merge) {
- if (overwrite) {
- lastBatch.pop();
- }
- lastBatch.push(op);
- } else {
- var batch = [op];
- undos.push(batch);
- }
- while (undos.length > 100) {
- undos.shift();
- }
- if (shouldClear(op)) {
- history.redos = [];
- }
- }
- apply(op);
- };
- return e;
- };
- /**
- * Check whether to merge an operation into the previous operation.
- */
- var shouldMerge = function shouldMerge(op, prev) {
- if (op.type === 'set_selection') {
- return true;
- }
- 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' && (op.properties == null || op.newProperties == null)) {
- return false;
- }
- return true;
- };
- /**
- * Check whether an operation should overwrite the previous one.
- */
- var shouldOverwrite = function shouldOverwrite(op, prev) {
- if (prev && op.type === 'set_selection' && prev.type === 'set_selection') {
- return true;
- }
- return false;
- };
- /**
- * Check whether an operation should clear the redos stack.
- */
- var shouldClear = function shouldClear(op) {
- 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;
- Object.defineProperty(exports, '__esModule', { value: true });
- })));
|