slate-history
Advanced tools
Comparing version 0.82.0-2022622233410 to 0.85.0-202292321100
@@ -1,2 +0,6 @@ | ||
import { Operation } from 'slate'; | ||
import { Operation, Range } from 'slate'; | ||
interface Batch { | ||
operations: Operation[]; | ||
selectionBefore: Range | null; | ||
} | ||
/** | ||
@@ -7,4 +11,4 @@ * `History` objects hold all of the operations that are applied to a value, so | ||
export interface History { | ||
redos: Operation[][]; | ||
undos: Operation[][]; | ||
redos: Batch[]; | ||
undos: Batch[]; | ||
} | ||
@@ -17,2 +21,3 @@ export declare const History: { | ||
}; | ||
export {}; | ||
//# sourceMappingURL=history.d.ts.map |
import { isPlainObject } from 'is-plain-object'; | ||
import { Operation, Editor, Path } from 'slate'; | ||
import { Operation, Editor, Transforms, Path } from 'slate'; | ||
@@ -112,5 +112,10 @@ var History = { | ||
var batch = redos[redos.length - 1]; | ||
if (batch.selectionBefore) { | ||
Transforms.setSelection(e, batch.selectionBefore); | ||
} | ||
HistoryEditor.withoutSaving(e, () => { | ||
Editor.withoutNormalizing(e, () => { | ||
for (var op of batch) { | ||
for (var op of batch.operations) { | ||
e.apply(op); | ||
@@ -137,3 +142,3 @@ } | ||
Editor.withoutNormalizing(e, () => { | ||
var inverseOps = batch.map(Operation.inverse).reverse(); | ||
var inverseOps = batch.operations.map(Operation.inverse).reverse(); | ||
@@ -143,2 +148,6 @@ for (var op of inverseOps) { | ||
} | ||
if (batch.selectionBefore) { | ||
Transforms.setSelection(e, batch.selectionBefore); | ||
} | ||
}); | ||
@@ -160,4 +169,3 @@ }); | ||
var lastBatch = undos[undos.length - 1]; | ||
var lastOp = lastBatch && lastBatch[lastBatch.length - 1]; | ||
var overwrite = shouldOverwrite(op, lastOp); | ||
var lastOp = lastBatch && lastBatch.operations[lastBatch.operations.length - 1]; | ||
var save = HistoryEditor.isSaving(e); | ||
@@ -177,3 +185,3 @@ var merge = HistoryEditor.isMerging(e); | ||
} else { | ||
merge = shouldMerge(op, lastOp) || overwrite; | ||
merge = shouldMerge(op, lastOp); | ||
} | ||
@@ -183,9 +191,8 @@ } | ||
if (lastBatch && merge) { | ||
if (overwrite) { | ||
lastBatch.pop(); | ||
} | ||
lastBatch.push(op); | ||
lastBatch.operations.push(op); | ||
} else { | ||
var batch = [op]; | ||
var batch = { | ||
operations: [op], | ||
selectionBefore: e.selection | ||
}; | ||
undos.push(batch); | ||
@@ -198,5 +205,3 @@ } | ||
if (shouldClear(op)) { | ||
history.redos = []; | ||
} | ||
history.redos = []; | ||
} | ||
@@ -214,6 +219,2 @@ | ||
var 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 && Path.equals(op.path, prev.path)) { | ||
@@ -235,26 +236,2 @@ return true; | ||
var 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 = (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 = op => { | ||
if (op.type === 'set_selection') { | ||
@@ -261,0 +238,0 @@ return false; |
@@ -113,5 +113,10 @@ 'use strict'; | ||
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), | ||
var _iterator = _createForOfIteratorHelper(batch.operations), | ||
_step; | ||
@@ -144,3 +149,3 @@ | ||
slate.Editor.withoutNormalizing(e, function () { | ||
var inverseOps = batch.map(slate.Operation.inverse).reverse(); | ||
var inverseOps = batch.operations.map(slate.Operation.inverse).reverse(); | ||
@@ -160,2 +165,6 @@ var _iterator2 = _createForOfIteratorHelper(inverseOps), | ||
} | ||
if (batch.selectionBefore) { | ||
slate.Transforms.setSelection(e, batch.selectionBefore); | ||
} | ||
}); | ||
@@ -173,4 +182,3 @@ }); | ||
var lastBatch = undos[undos.length - 1]; | ||
var lastOp = lastBatch && lastBatch[lastBatch.length - 1]; | ||
var overwrite = shouldOverwrite(op, lastOp); | ||
var lastOp = lastBatch && lastBatch.operations[lastBatch.operations.length - 1]; | ||
var save = HistoryEditor.isSaving(e); | ||
@@ -190,3 +198,3 @@ var merge = HistoryEditor.isMerging(e); | ||
} else { | ||
merge = shouldMerge(op, lastOp) || overwrite; | ||
merge = shouldMerge(op, lastOp); | ||
} | ||
@@ -196,9 +204,8 @@ } | ||
if (lastBatch && merge) { | ||
if (overwrite) { | ||
lastBatch.pop(); | ||
} | ||
lastBatch.push(op); | ||
lastBatch.operations.push(op); | ||
} else { | ||
var batch = [op]; | ||
var batch = { | ||
operations: [op], | ||
selectionBefore: e.selection | ||
}; | ||
undos.push(batch); | ||
@@ -211,5 +218,3 @@ } | ||
if (shouldClear(op)) { | ||
history.redos = []; | ||
} | ||
history.redos = []; | ||
} | ||
@@ -227,6 +232,2 @@ | ||
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)) { | ||
@@ -248,26 +249,2 @@ return true; | ||
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') { | ||
@@ -274,0 +251,0 @@ return false; |
@@ -145,5 +145,10 @@ (function (global, factory) { | ||
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), | ||
var _iterator = _createForOfIteratorHelper(batch.operations), | ||
_step; | ||
@@ -176,3 +181,3 @@ | ||
slate.Editor.withoutNormalizing(e, function () { | ||
var inverseOps = batch.map(slate.Operation.inverse).reverse(); | ||
var inverseOps = batch.operations.map(slate.Operation.inverse).reverse(); | ||
@@ -192,2 +197,6 @@ var _iterator2 = _createForOfIteratorHelper(inverseOps), | ||
} | ||
if (batch.selectionBefore) { | ||
slate.Transforms.setSelection(e, batch.selectionBefore); | ||
} | ||
}); | ||
@@ -205,4 +214,3 @@ }); | ||
var lastBatch = undos[undos.length - 1]; | ||
var lastOp = lastBatch && lastBatch[lastBatch.length - 1]; | ||
var overwrite = shouldOverwrite(op, lastOp); | ||
var lastOp = lastBatch && lastBatch.operations[lastBatch.operations.length - 1]; | ||
var save = HistoryEditor.isSaving(e); | ||
@@ -222,3 +230,3 @@ var merge = HistoryEditor.isMerging(e); | ||
} else { | ||
merge = shouldMerge(op, lastOp) || overwrite; | ||
merge = shouldMerge(op, lastOp); | ||
} | ||
@@ -228,9 +236,8 @@ } | ||
if (lastBatch && merge) { | ||
if (overwrite) { | ||
lastBatch.pop(); | ||
} | ||
lastBatch.push(op); | ||
lastBatch.operations.push(op); | ||
} else { | ||
var batch = [op]; | ||
var batch = { | ||
operations: [op], | ||
selectionBefore: e.selection | ||
}; | ||
undos.push(batch); | ||
@@ -243,5 +250,3 @@ } | ||
if (shouldClear(op)) { | ||
history.redos = []; | ||
} | ||
history.redos = []; | ||
} | ||
@@ -259,6 +264,2 @@ | ||
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)) { | ||
@@ -280,26 +281,2 @@ return true; | ||
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') { | ||
@@ -306,0 +283,0 @@ return false; |
@@ -7,2 +7,2 @@ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("slate")):"function"==typeof define&&define.amd?define(["exports","slate"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).SlateHistory={},t.Slate)}(this,(function(t,e){"use strict"; | ||
* Released under the MIT License. | ||
*/function n(t){return"[object Object]"===Object.prototype.toString.call(t)}var r={isHistory:function(t){return!1!==n(r=t)&&(void 0===(o=r.constructor)||!1!==n(i=o.prototype)&&!1!==i.hasOwnProperty("isPrototypeOf"))&&Array.isArray(t.redos)&&Array.isArray(t.undos)&&(0===t.redos.length||e.Operation.isOperationList(t.redos[0]))&&(0===t.undos.length||e.Operation.isOperationList(t.undos[0]));var r,o,i}},o=new WeakMap,i=new WeakMap,s=new WeakMap,u={isHistoryEditor:function(t){return r.isHistory(t.history)&&e.Editor.isEditor(t)},isMerging:function(t){return s.get(t)},isSaving:function(t){return i.get(t)},redo:function(t){t.redo()},undo:function(t){t.undo()},withoutMerging:function(t,e){var n=u.isMerging(t);s.set(t,!1),e(),s.set(t,n)},withoutSaving:function(t,e){var n=u.isSaving(t);i.set(t,!1),e(),i.set(t,n)}};function a(t,e){var n="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(!n){if(Array.isArray(t)||(n=function(t,e){if(!t)return;if("string"==typeof t)return f(t,e);var n=Object.prototype.toString.call(t).slice(8,-1);"Object"===n&&t.constructor&&(n=t.constructor.name);if("Map"===n||"Set"===n)return Array.from(t);if("Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))return f(t,e)}(t))||e&&t&&"number"==typeof t.length){n&&(t=n);var r=0,o=function(){};return{s:o,n:function(){return r>=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:o}}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 i,s=!0,u=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return s=t.done,t},e:function(t){u=!0,i=t},f:function(){try{s||null==n.return||n.return()}finally{if(u)throw i}}}}function f(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,r=new Array(e);n<e;n++)r[n]=t[n];return r}var l=function(t,n){return"set_selection"===t.type||(!(!n||"insert_text"!==t.type||"insert_text"!==n.type||t.offset!==n.offset+n.text.length||!e.Path.equals(t.path,n.path))||!(!n||"remove_text"!==t.type||"remove_text"!==n.type||t.offset+t.text.length!==n.offset||!e.Path.equals(t.path,n.path)))},p=function(t,e){return"set_selection"!==t.type||null!=t.properties&&null!=t.newProperties},c=function(t,e){return!(!e||"set_selection"!==t.type||"set_selection"!==e.type)},y=function(t){return"set_selection"!==t.type};t.HISTORY=o,t.History=r,t.HistoryEditor=u,t.MERGING=s,t.SAVING=i,t.withHistory=function(t){var n=t,r=n.apply;return n.history={undos:[],redos:[]},n.redo=function(){var t=n.history,r=t.redos;if(r.length>0){var o=r[r.length-1];u.withoutSaving(n,(function(){e.Editor.withoutNormalizing(n,(function(){var t,e=a(o);try{for(e.s();!(t=e.n()).done;){var r=t.value;n.apply(r)}}catch(t){e.e(t)}finally{e.f()}}))})),t.redos.pop(),t.undos.push(o)}},n.undo=function(){var t=n.history,r=t.undos;if(r.length>0){var o=r[r.length-1];u.withoutSaving(n,(function(){e.Editor.withoutNormalizing(n,(function(){var t,r=a(o.map(e.Operation.inverse).reverse());try{for(r.s();!(t=r.n()).done;){var i=t.value;n.apply(i)}}catch(t){r.e(t)}finally{r.f()}}))})),t.redos.push(o),t.undos.pop()}},n.apply=function(t){var e=n.operations,o=n.history,i=o.undos,s=i[i.length-1],a=s&&s[s.length-1],f=c(t,a),d=u.isSaving(n),h=u.isMerging(n);if(null==d&&(d=p(t)),d){if(null==h&&(h=null!=s&&(0!==e.length||(l(t,a)||f))),s&&h)f&&s.pop(),s.push(t);else{var g=[t];i.push(g)}for(;i.length>100;)i.shift();y(t)&&(o.redos=[])}r(t)},n},Object.defineProperty(t,"__esModule",{value:!0})})); | ||
*/function n(t){return"[object Object]"===Object.prototype.toString.call(t)}var r={isHistory:function(t){return!1!==n(r=t)&&(void 0===(o=r.constructor)||!1!==n(i=o.prototype)&&!1!==i.hasOwnProperty("isPrototypeOf"))&&Array.isArray(t.redos)&&Array.isArray(t.undos)&&(0===t.redos.length||e.Operation.isOperationList(t.redos[0]))&&(0===t.undos.length||e.Operation.isOperationList(t.undos[0]));var r,o,i}},o=new WeakMap,i=new WeakMap,s=new WeakMap,a={isHistoryEditor:function(t){return r.isHistory(t.history)&&e.Editor.isEditor(t)},isMerging:function(t){return s.get(t)},isSaving:function(t){return i.get(t)},redo:function(t){t.redo()},undo:function(t){t.undo()},withoutMerging:function(t,e){var n=a.isMerging(t);s.set(t,!1),e(),s.set(t,n)},withoutSaving:function(t,e){var n=a.isSaving(t);i.set(t,!1),e(),i.set(t,n)}};function u(t,e){var n="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(!n){if(Array.isArray(t)||(n=function(t,e){if(!t)return;if("string"==typeof t)return f(t,e);var n=Object.prototype.toString.call(t).slice(8,-1);"Object"===n&&t.constructor&&(n=t.constructor.name);if("Map"===n||"Set"===n)return Array.from(t);if("Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))return f(t,e)}(t))||e&&t&&"number"==typeof t.length){n&&(t=n);var r=0,o=function(){};return{s:o,n:function(){return r>=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:o}}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 i,s=!0,a=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return s=t.done,t},e:function(t){a=!0,i=t},f:function(){try{s||null==n.return||n.return()}finally{if(a)throw i}}}}function f(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,r=new Array(e);n<e;n++)r[n]=t[n];return r}var l=function(t,n){return!(!n||"insert_text"!==t.type||"insert_text"!==n.type||t.offset!==n.offset+n.text.length||!e.Path.equals(t.path,n.path))||!(!n||"remove_text"!==t.type||"remove_text"!==n.type||t.offset+t.text.length!==n.offset||!e.Path.equals(t.path,n.path))},c=function(t,e){return"set_selection"!==t.type};t.HISTORY=o,t.History=r,t.HistoryEditor=a,t.MERGING=s,t.SAVING=i,t.withHistory=function(t){var n=t,r=n.apply;return n.history={undos:[],redos:[]},n.redo=function(){var t=n.history,r=t.redos;if(r.length>0){var o=r[r.length-1];o.selectionBefore&&e.Transforms.setSelection(n,o.selectionBefore),a.withoutSaving(n,(function(){e.Editor.withoutNormalizing(n,(function(){var t,e=u(o.operations);try{for(e.s();!(t=e.n()).done;){var r=t.value;n.apply(r)}}catch(t){e.e(t)}finally{e.f()}}))})),t.redos.pop(),t.undos.push(o)}},n.undo=function(){var t=n.history,r=t.undos;if(r.length>0){var o=r[r.length-1];a.withoutSaving(n,(function(){e.Editor.withoutNormalizing(n,(function(){var t,r=u(o.operations.map(e.Operation.inverse).reverse());try{for(r.s();!(t=r.n()).done;){var i=t.value;n.apply(i)}}catch(t){r.e(t)}finally{r.f()}o.selectionBefore&&e.Transforms.setSelection(n,o.selectionBefore)}))})),t.redos.push(o),t.undos.pop()}},n.apply=function(t){var e=n.operations,o=n.history,i=o.undos,s=i[i.length-1],u=s&&s.operations[s.operations.length-1],f=a.isSaving(n),p=a.isMerging(n);if(null==f&&(f=c(t)),f){if(null==p&&(p=null!=s&&(0!==e.length||l(t,u))),s&&p)s.operations.push(t);else{var y={operations:[t],selectionBefore:n.selection};i.push(y)}for(;i.length>100;)i.shift();o.redos=[]}r(t)},n},Object.defineProperty(t,"__esModule",{value:!0})})); |
{ | ||
"name": "slate-history", | ||
"description": "An operation-based history implementation for Slate editors.", | ||
"version": "0.82.0-2022622233410", | ||
"version": "0.85.0-202292321100", | ||
"license": "MIT", | ||
@@ -22,8 +22,8 @@ "repository": "git://github.com/ianstormtaylor/slate.git", | ||
"lodash": "^4.17.21", | ||
"slate": "^0.82.0-2022622233410", | ||
"slate-hyperscript": "^0.82.0-2022622233410", | ||
"slate": "^0.84.0", | ||
"slate-hyperscript": "^0.81.3", | ||
"source-map-loader": "^4.0.0" | ||
}, | ||
"peerDependencies": { | ||
"slate": ">=0.82.0-2022622233410" | ||
"slate": ">=0.65.3" | ||
}, | ||
@@ -30,0 +30,0 @@ "umdGlobals": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
57098
763