@lexical/history 0.2.9 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -129,6 +129,18 @@ function getChangeType(prevEditorState, nextEditorState, dirtyLeavesSet, dirtyEl
129
129
  return OTHER;
130
130
  }
131
131
 
132
+ function isTextNodeUnchanged(key, prevEditorState, nextEditorState) {
133
+ const prevNode = prevEditorState._nodeMap.get(key);
134
+
135
+ const nextNode = nextEditorState._nodeMap.get(key);
136
+
137
+ if (lexical.$isTextNode(prevNode) && lexical.$isTextNode(nextNode)) {
138
+ return prevNode.__type === nextNode.__type && prevNode.__text === nextNode.__text && prevNode.__mode === nextNode.__mode && prevNode.__detail === nextNode.__detail && prevNode.__style === nextNode.__style && prevNode.__format === nextNode.__format;
139
+ }
140
+
141
+ return false;
142
+ }
143
+
132
144
  function createMergeActionGetter(editor, delay) {
133
145
  let prevChangeTime = Date.now();
134
146
  let prevChangeType = OTHER;
@@ -172,6 +184,16 @@ function createMergeActionGetter(editor, delay) {
172
184
 
173
185
  if (shouldPushHistory === false && changeType !== OTHER && changeType === prevChangeType && changeTime < prevChangeTime + delay && isSameEditor) {
174
186
  return HISTORY_MERGE;
187
+ } // A single node might have been marked as dirty, but not have changed
188
+ // due to some node transform reverting the change.
189
+
190
+
191
+ if (dirtyLeaves.size === 1) {
192
+ const dirtyLeafKey = Array.from(dirtyLeaves)[0];
193
+
194
+ if (isTextNodeUnchanged(dirtyLeafKey, prevEditorState, nextEditorState)) {
195
+ return HISTORY_MERGE;
196
+ }
175
197
  }
176
198
 
177
199
  return HISTORY_PUSH;
@@ -4,12 +4,12 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
- var e=require("@lexical/utils"),u=require("lexical");
8
- function v(b,a,k,h,m){if(null===b||0===k.size&&0===h.size)return 0;var f=a._selection,c=b._selection;if(m)return 1;if(!(u.$isRangeSelection(f)&&u.$isRangeSelection(c)&&c.isCollapsed()&&f.isCollapsed()))return 0;m=a._nodeMap;const d=[];for(const g of k)k=m.get(g),void 0!==k&&d.push(k);for(const [g,l]of h)l&&(h=m.get(g),void 0===h||u.$isRootNode(h)||d.push(h));if(0===d.length)return 0;if(1<d.length)return h=a._nodeMap,a=h.get(f.anchor.key),c=h.get(c.anchor.key),a&&c&&!b._nodeMap.has(a.__key)&&u.$isTextNode(a)&&
7
+ 'use strict';var e=require("@lexical/utils"),u=require("lexical");
8
+ function v(b,a,l,k,p){if(null===b||0===l.size&&0===k.size)return 0;var f=a._selection,c=b._selection;if(p)return 1;if(!(u.$isRangeSelection(f)&&u.$isRangeSelection(c)&&c.isCollapsed()&&f.isCollapsed()))return 0;p=a._nodeMap;let d=[];for(let g of l)l=p.get(g),void 0!==l&&d.push(l);for(let [g,n]of k)n&&(k=p.get(g),void 0===k||u.$isRootNode(k)||d.push(k));if(0===d.length)return 0;if(1<d.length)return k=a._nodeMap,a=k.get(f.anchor.key),c=k.get(c.anchor.key),a&&c&&!b._nodeMap.has(a.__key)&&u.$isTextNode(a)&&
9
9
  1===a.__text.length&&1===f.anchor.offset?2:0;a=d[0];b=b._nodeMap.get(a.__key);if(!u.$isTextNode(b)||!u.$isTextNode(a)||b.__mode!==a.__mode)return 0;b=b.__text;a=a.__text;if(b===a)return 0;f=f.anchor;c=c.anchor;if(f.key!==c.key||"text"!==f.type)return 0;f=f.offset;c=c.offset;b=a.length-b.length;return 1===b&&c===f-1?2:-1===b&&c===f+1?3:-1===b&&c===f?4:0}
10
- function w(b,a){let k=Date.now(),h=0;return(m,f,c,d,g,l)=>{const p=Date.now();if(l.has("historic"))return h=0,k=p,2;const n=v(m,f,d,g,b.isComposing()),t=(()=>{const r=l.has("history-push");if(!r&&l.has("history-merge"))return 0;if(null===m)return 1;var q=f._selection;const x=m._selection;if(!(0<d.size||0<g.size))return null===x&&null!==q?0:2;q=null===c||c.editor===b;return!1===r&&0!==n&&n===h&&p<k+a&&q?0:1})();k=p;h=n;return t}}
11
- exports.createEmptyHistoryState=function(){return{current:null,redoStack:[],undoStack:[]}};
12
- exports.registerHistory=function(b,a,k){const h=w(b,k);k=({editorState:c,prevEditorState:d,dirtyLeaves:g,dirtyElements:l,tags:p})=>{const n=a.current,t=a.redoStack,r=a.undoStack,q=null===n?null:n.editorState;if(null===n||c!==q){g=h(d,c,n,g,l,p);if(1===g)0!==t.length&&(a.redoStack=[]),null!==n&&(r.push({...n,undoSelection:d.read(u.$getSelection)}),b.dispatchCommand(u.CAN_UNDO_COMMAND,!0));else if(2===g)return;a.current={editor:b,editorState:c}}};const m=e.mergeRegister(b.registerCommand(u.UNDO_COMMAND,
13
- ()=>{const c=a.redoStack,d=a.undoStack;if(0!==d.length){const g=a.current,l=d.pop();null!==g&&(c.push(g),b.dispatchCommand(u.CAN_REDO_COMMAND,!0));0===d.length&&b.dispatchCommand(u.CAN_UNDO_COMMAND,!1);a.current=l||null;l&&l.editor.setEditorState(l.editorState.clone(l.undoSelection),{tag:"historic"})}return!0},u.COMMAND_PRIORITY_EDITOR),b.registerCommand(u.REDO_COMMAND,()=>{const c=a.redoStack;var d=a.undoStack;if(0!==c.length){const g=a.current;null!==g&&(d.push(g),b.dispatchCommand(u.CAN_UNDO_COMMAND,
14
- !0));d=c.pop();0===c.length&&b.dispatchCommand(u.CAN_REDO_COMMAND,!1);a.current=d||null;d&&d.editor.setEditorState(d.editorState,{tag:"historic"})}return!0},u.COMMAND_PRIORITY_EDITOR),b.registerCommand(u.CLEAR_EDITOR_COMMAND,()=>{a.undoStack=[];a.redoStack=[];a.current=null;return!1},u.COMMAND_PRIORITY_EDITOR),b.registerCommand(u.CLEAR_HISTORY_COMMAND,()=>{a.undoStack=[];a.redoStack=[];a.current=null;return!0},u.COMMAND_PRIORITY_EDITOR),b.registerUpdateListener(k)),f=b.registerUpdateListener(k);return()=>
15
- {m();f()}};
10
+ function w(b,a){let l=Date.now(),k=0;return(p,f,c,d,g,n)=>{let r=Date.now();if(n.has("historic"))return k=0,l=r,2;let q=v(p,f,d,g,b.isComposing()),t=(()=>{var m=n.has("history-push");if(!m&&n.has("history-merge"))return 0;if(null===p)return 1;var h=f._selection;const x=p._selection;if(!(0<d.size||0<g.size))return null===x&&null!==h?0:2;h=null===c||c.editor===b;return!1===m&&0!==q&&q===k&&r<l+a&&h||1===d.size&&(h=Array.from(d)[0],m=p._nodeMap.get(h),h=f._nodeMap.get(h),m=u.$isTextNode(m)&&u.$isTextNode(h)?
11
+ m.__type===h.__type&&m.__text===h.__text&&m.__mode===h.__mode&&m.__detail===h.__detail&&m.__style===h.__style&&m.__format===h.__format:!1,m)?0:1})();l=r;k=q;return t}}exports.createEmptyHistoryState=function(){return{current:null,redoStack:[],undoStack:[]}};
12
+ exports.registerHistory=function(b,a,l){let k=w(b,l);l=({editorState:c,prevEditorState:d,dirtyLeaves:g,dirtyElements:n,tags:r})=>{const q=a.current,t=a.redoStack,m=a.undoStack,h=null===q?null:q.editorState;if(null===q||c!==h){g=k(d,c,q,g,n,r);if(1===g)0!==t.length&&(a.redoStack=[]),null!==q&&(m.push({...q,undoSelection:d.read(u.$getSelection)}),b.dispatchCommand(u.CAN_UNDO_COMMAND,!0));else if(2===g)return;a.current={editor:b,editorState:c}}};let p=e.mergeRegister(b.registerCommand(u.UNDO_COMMAND,
13
+ ()=>{let c=a.redoStack,d=a.undoStack;if(0!==d.length){let g=a.current,n=d.pop();null!==g&&(c.push(g),b.dispatchCommand(u.CAN_REDO_COMMAND,!0));0===d.length&&b.dispatchCommand(u.CAN_UNDO_COMMAND,!1);a.current=n||null;n&&n.editor.setEditorState(n.editorState.clone(n.undoSelection),{tag:"historic"})}return!0},u.COMMAND_PRIORITY_EDITOR),b.registerCommand(u.REDO_COMMAND,()=>{let c=a.redoStack;var d=a.undoStack;if(0!==c.length){let g=a.current;null!==g&&(d.push(g),b.dispatchCommand(u.CAN_UNDO_COMMAND,!0));
14
+ d=c.pop();0===c.length&&b.dispatchCommand(u.CAN_REDO_COMMAND,!1);a.current=d||null;d&&d.editor.setEditorState(d.editorState,{tag:"historic"})}return!0},u.COMMAND_PRIORITY_EDITOR),b.registerCommand(u.CLEAR_EDITOR_COMMAND,()=>{a.undoStack=[];a.redoStack=[];a.current=null;return!1},u.COMMAND_PRIORITY_EDITOR),b.registerCommand(u.CLEAR_HISTORY_COMMAND,()=>{a.undoStack=[];a.redoStack=[];a.current=null;return!0},u.COMMAND_PRIORITY_EDITOR),b.registerUpdateListener(l)),f=b.registerUpdateListener(l);return()=>
15
+ {p();f()}}
package/package.json CHANGED
@@ -8,13 +8,13 @@
8
8
  "history"
9
9
  ],
10
10
  "license": "MIT",
11
- "version": "0.2.9",
11
+ "version": "0.3.0",
12
12
  "main": "LexicalHistory.js",
13
13
  "peerDependencies": {
14
- "lexical": "0.2.9"
14
+ "lexical": "0.3.0"
15
15
  },
16
16
  "dependencies": {
17
- "@lexical/utils": "0.2.9"
17
+ "@lexical/utils": "0.3.0"
18
18
  },
19
19
  "repository": {
20
20
  "type": "git",