UNPKG

@lexical/history

Version:

This package contains selection history helpers for Lexical.

10 lines (8 loc) 4.85 kB
/** * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * */ import{effect as t,namedSignals as e,batch as n,getPeerDependencyFromEditor as r}from"@lexical/extension";import{mergeRegister as o}from"@lexical/utils";import{defineExtension as i,safeCast as a,configExtension as s,UNDO_COMMAND as d,COMMAND_PRIORITY_EDITOR as l,REDO_COMMAND as u,CLEAR_EDITOR_COMMAND as c,CLEAR_HISTORY_COMMAND as f,CAN_REDO_COMMAND as p,CAN_UNDO_COMMAND as h,COMPOSITION_START_TAG as m,HISTORIC_TAG as g,COMPOSITION_END_TAG as y,HISTORY_PUSH_TAG as S,HISTORY_MERGE_TAG as _,$isRangeSelection as k,$isTextNode as v,$isRootNode as x}from"lexical";function C(t,e,n,r,o){if(null===t||0===n.size&&0===r.size&&!o)return 0;const i=e._selection,a=t._selection;if(o)return 1;if(!(k(i)&&k(a)&&a.isCollapsed()&&i.isCollapsed()))return 0;const s=function(t,e,n){const r=t._nodeMap,o=[];for(const t of e){const e=r.get(t);void 0!==e&&o.push(e)}for(const[t,e]of n){if(!e)continue;const n=r.get(t);void 0===n||x(n)||o.push(n)}return o}(e,n,r);if(0===s.length)return 0;if(s.length>1){const n=e._nodeMap,r=n.get(i.anchor.key),o=n.get(a.anchor.key);return r&&o&&!t._nodeMap.has(r.__key)&&v(r)&&1===r.__text.length&&1===i.anchor.offset?2:0}const d=s[0],l=t._nodeMap.get(d.__key);if(!v(l)||!v(d)||l.__mode!==d.__mode)return 0;const u=l.__text,c=d.__text;if(u===c)return 0;const f=i.anchor,p=a.anchor;if(f.key!==p.key||"text"!==f.type)return 0;const h=f.offset,m=p.offset,g=c.length-u.length;return 1===g&&m===h-1?2:-1===g&&m===h+1?3:-1===g&&m===h?4:0}function b(t,e,n){let r=n(),o=0,i=r,a=0,s=null;return(d,l,u,c,f,p)=>{const h=n();if(p.has(m)&&(i=r,a=o,s=d),p.has(g))return o=0,r=h,2;p.has(y)&&s&&(r=i,o=a,d=s);const x=C(d,l,c,f,t.isComposing()),b=(()=>{const n=null===u||u.editor===t,i=p.has(S);if(!i&&n&&p.has(_))return 0;if(1===x)return 2;if(null===d)return 1;const a=l._selection;if(!(c.size>0||f.size>0))return null!==a?0:2;const s="number"==typeof e?e:e.peek();if(!1===i&&0!==x&&x===o&&h<r+s&&n)return 0;if(1===c.size){if(function(t,e,n){const r=e._nodeMap.get(t),o=n._nodeMap.get(t),i=e._selection,a=n._selection;return!(k(i)&&k(a)&&"element"===i.anchor.type&&"element"===i.focus.type&&"text"===a.anchor.type&&"text"===a.focus.type||!v(r)||!v(o)||r.__parent!==o.__parent)&&JSON.stringify(e.read(()=>r.exportJSON()))===JSON.stringify(n.read(()=>o.exportJSON()))}(Array.from(c)[0],d,l))return 0}return 1})();return r=h,o=x,b}}function w(t){t.undoStack=[],t.redoStack=[],t.current=null}function E(t,e,n,r=Date.now){const i=b(t,n,r);return o(t.registerCommand(d,()=>(function(t,e){const n=e.redoStack,r=e.undoStack;if(0!==r.length){const o=e.current,i=r.pop();null!==o&&(n.push(o),t.dispatchCommand(p,!0)),0===r.length&&t.dispatchCommand(h,!1),e.current=i||null,i&&i.editor.setEditorState(i.editorState,{tag:g})}}(t,e),!0),l),t.registerCommand(u,()=>(function(t,e){const n=e.redoStack,r=e.undoStack;if(0!==n.length){const o=e.current;null!==o&&(r.push(o),t.dispatchCommand(h,!0));const i=n.pop();0===n.length&&t.dispatchCommand(p,!1),e.current=i||null,i&&i.editor.setEditorState(i.editorState,{tag:g})}}(t,e),!0),l),t.registerCommand(c,()=>(w(e),!1),l),t.registerCommand(f,()=>(w(e),t.dispatchCommand(p,!1),t.dispatchCommand(h,!1),!0),l),t.registerUpdateListener(({editorState:n,prevEditorState:r,dirtyLeaves:o,dirtyElements:a,tags:s})=>{const d=e.current,l=e.redoStack,u=e.undoStack,c=null===d?null:d.editorState;if(null!==d&&n===c)return;const f=i(r,n,d,o,a,s);if(1===f)0!==l.length&&(e.redoStack=[],t.dispatchCommand(p,!1)),null!==d&&(u.push({...d}),t.dispatchCommand(h,!0));else if(2===f)return;e.current={editor:t,editorState:n}}))}function M(){return{current:null,redoStack:[],undoStack:[]}}const O=i({build:(t,{delay:n,createInitialHistoryState:r,disabled:o,now:i})=>e({delay:n,disabled:o,historyState:r(t),now:i}),config:a({createInitialHistoryState:M,delay:300,disabled:"undefined"==typeof window,now:Date.now}),name:"@lexical/history/History",register:(e,n,r)=>{const o=r.getOutput();return t(()=>o.disabled.value?void 0:E(e,o.historyState.value,o.delay,()=>o.now.peek()()))}});const z=i({build:(t,{disabled:n,parentEditor:r})=>e({disabled:n,parentEditor:r||t._parentEditor}),config:a({disabled:!1,parentEditor:null}),dependencies:[s(O,{disabled:!0})],name:"@lexical/history/SharedHistory",register:(e,o,i)=>t(()=>{const{disabled:t,parentEditor:e}=i.getOutput();if(!t.value){const{output:t}=i.getDependency(O),o=function(t){return t?r(t,O.name):null}(e.value);if(!o)return;const a=o.output;n(()=>{t.delay.value=a.delay.value,t.historyState.value=a.historyState.value,t.now.value=a.now.value,t.disabled.value=a.disabled.value})}})});export{O as HistoryExtension,z as SharedHistoryExtension,M as createEmptyHistoryState,E as registerHistory};