@ekz/lexical-utils 0.40.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.
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+
9
+ 'use strict'
10
+ const EkzLexicalUtils = process.env.NODE_ENV !== 'production' ? require('./EkzLexicalUtils.dev.js') : require('./EkzLexicalUtils.prod.js');
11
+ module.exports = EkzLexicalUtils;
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+
9
+ import * as modDev from './EkzLexicalUtils.dev.mjs';
10
+ import * as modProd from './EkzLexicalUtils.prod.mjs';
11
+ const mod = process.env.NODE_ENV !== 'production' ? modDev : modProd;
12
+ export const $descendantsMatching = mod.$descendantsMatching;
13
+ export const $dfs = mod.$dfs;
14
+ export const $dfsIterator = mod.$dfsIterator;
15
+ export const $filter = mod.$filter;
16
+ export const $findMatchingParent = mod.$findMatchingParent;
17
+ export const $firstToLastIterator = mod.$firstToLastIterator;
18
+ export const $getAdjacentCaret = mod.$getAdjacentCaret;
19
+ export const $getAdjacentSiblingOrParentSiblingCaret = mod.$getAdjacentSiblingOrParentSiblingCaret;
20
+ export const $getDepth = mod.$getDepth;
21
+ export const $getNearestBlockElementAncestorOrThrow = mod.$getNearestBlockElementAncestorOrThrow;
22
+ export const $getNearestNodeOfType = mod.$getNearestNodeOfType;
23
+ export const $getNextRightPreorderNode = mod.$getNextRightPreorderNode;
24
+ export const $getNextSiblingOrParentSibling = mod.$getNextSiblingOrParentSibling;
25
+ export const $insertFirst = mod.$insertFirst;
26
+ export const $insertNodeToNearestRoot = mod.$insertNodeToNearestRoot;
27
+ export const $insertNodeToNearestRootAtCaret = mod.$insertNodeToNearestRootAtCaret;
28
+ export const $isEditorIsNestedEditor = mod.$isEditorIsNestedEditor;
29
+ export const $lastToFirstIterator = mod.$lastToFirstIterator;
30
+ export const $restoreEditorState = mod.$restoreEditorState;
31
+ export const $reverseDfs = mod.$reverseDfs;
32
+ export const $reverseDfsIterator = mod.$reverseDfsIterator;
33
+ export const $splitNode = mod.$splitNode;
34
+ export const $unwrapAndFilterDescendants = mod.$unwrapAndFilterDescendants;
35
+ export const $unwrapNode = mod.$unwrapNode;
36
+ export const $wrapNodeInElement = mod.$wrapNodeInElement;
37
+ export const CAN_USE_BEFORE_INPUT = mod.CAN_USE_BEFORE_INPUT;
38
+ export const CAN_USE_DOM = mod.CAN_USE_DOM;
39
+ export const IS_ANDROID = mod.IS_ANDROID;
40
+ export const IS_ANDROID_CHROME = mod.IS_ANDROID_CHROME;
41
+ export const IS_APPLE = mod.IS_APPLE;
42
+ export const IS_APPLE_WEBKIT = mod.IS_APPLE_WEBKIT;
43
+ export const IS_CHROME = mod.IS_CHROME;
44
+ export const IS_FIREFOX = mod.IS_FIREFOX;
45
+ export const IS_IOS = mod.IS_IOS;
46
+ export const IS_SAFARI = mod.IS_SAFARI;
47
+ export const addClassNamesToElement = mod.addClassNamesToElement;
48
+ export const calculateZoomLevel = mod.calculateZoomLevel;
49
+ export const isBlockDomNode = mod.isBlockDomNode;
50
+ export const isHTMLAnchorElement = mod.isHTMLAnchorElement;
51
+ export const isHTMLElement = mod.isHTMLElement;
52
+ export const isInlineDomNode = mod.isInlineDomNode;
53
+ export const isMimeType = mod.isMimeType;
54
+ export const makeStateWrapper = mod.makeStateWrapper;
55
+ export const markSelection = mod.markSelection;
56
+ export const mediaFileReader = mod.mediaFileReader;
57
+ export const mergeRegister = mod.mergeRegister;
58
+ export const objectKlassEquals = mod.objectKlassEquals;
59
+ export const positionNodeOnRange = mod.positionNodeOnRange;
60
+ export const registerNestedElementResolver = mod.registerNestedElementResolver;
61
+ export const removeClassNamesFromElement = mod.removeClassNamesFromElement;
62
+ export const selectionAlwaysOnDisplay = mod.selectionAlwaysOnDisplay;
@@ -0,0 +1,60 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+
9
+ const mod = await (process.env.NODE_ENV !== 'production' ? import('./EkzLexicalUtils.dev.mjs') : import('./EkzLexicalUtils.prod.mjs'));
10
+ export const $descendantsMatching = mod.$descendantsMatching;
11
+ export const $dfs = mod.$dfs;
12
+ export const $dfsIterator = mod.$dfsIterator;
13
+ export const $filter = mod.$filter;
14
+ export const $findMatchingParent = mod.$findMatchingParent;
15
+ export const $firstToLastIterator = mod.$firstToLastIterator;
16
+ export const $getAdjacentCaret = mod.$getAdjacentCaret;
17
+ export const $getAdjacentSiblingOrParentSiblingCaret = mod.$getAdjacentSiblingOrParentSiblingCaret;
18
+ export const $getDepth = mod.$getDepth;
19
+ export const $getNearestBlockElementAncestorOrThrow = mod.$getNearestBlockElementAncestorOrThrow;
20
+ export const $getNearestNodeOfType = mod.$getNearestNodeOfType;
21
+ export const $getNextRightPreorderNode = mod.$getNextRightPreorderNode;
22
+ export const $getNextSiblingOrParentSibling = mod.$getNextSiblingOrParentSibling;
23
+ export const $insertFirst = mod.$insertFirst;
24
+ export const $insertNodeToNearestRoot = mod.$insertNodeToNearestRoot;
25
+ export const $insertNodeToNearestRootAtCaret = mod.$insertNodeToNearestRootAtCaret;
26
+ export const $isEditorIsNestedEditor = mod.$isEditorIsNestedEditor;
27
+ export const $lastToFirstIterator = mod.$lastToFirstIterator;
28
+ export const $restoreEditorState = mod.$restoreEditorState;
29
+ export const $reverseDfs = mod.$reverseDfs;
30
+ export const $reverseDfsIterator = mod.$reverseDfsIterator;
31
+ export const $splitNode = mod.$splitNode;
32
+ export const $unwrapAndFilterDescendants = mod.$unwrapAndFilterDescendants;
33
+ export const $unwrapNode = mod.$unwrapNode;
34
+ export const $wrapNodeInElement = mod.$wrapNodeInElement;
35
+ export const CAN_USE_BEFORE_INPUT = mod.CAN_USE_BEFORE_INPUT;
36
+ export const CAN_USE_DOM = mod.CAN_USE_DOM;
37
+ export const IS_ANDROID = mod.IS_ANDROID;
38
+ export const IS_ANDROID_CHROME = mod.IS_ANDROID_CHROME;
39
+ export const IS_APPLE = mod.IS_APPLE;
40
+ export const IS_APPLE_WEBKIT = mod.IS_APPLE_WEBKIT;
41
+ export const IS_CHROME = mod.IS_CHROME;
42
+ export const IS_FIREFOX = mod.IS_FIREFOX;
43
+ export const IS_IOS = mod.IS_IOS;
44
+ export const IS_SAFARI = mod.IS_SAFARI;
45
+ export const addClassNamesToElement = mod.addClassNamesToElement;
46
+ export const calculateZoomLevel = mod.calculateZoomLevel;
47
+ export const isBlockDomNode = mod.isBlockDomNode;
48
+ export const isHTMLAnchorElement = mod.isHTMLAnchorElement;
49
+ export const isHTMLElement = mod.isHTMLElement;
50
+ export const isInlineDomNode = mod.isInlineDomNode;
51
+ export const isMimeType = mod.isMimeType;
52
+ export const makeStateWrapper = mod.makeStateWrapper;
53
+ export const markSelection = mod.markSelection;
54
+ export const mediaFileReader = mod.mediaFileReader;
55
+ export const mergeRegister = mod.mergeRegister;
56
+ export const objectKlassEquals = mod.objectKlassEquals;
57
+ export const positionNodeOnRange = mod.positionNodeOnRange;
58
+ export const registerNestedElementResolver = mod.registerNestedElementResolver;
59
+ export const removeClassNamesFromElement = mod.removeClassNamesFromElement;
60
+ export const selectionAlwaysOnDisplay = mod.selectionAlwaysOnDisplay;
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+
9
+ "use strict";var e=require("@ekz/lexical"),t=require("@ekz/lexical-selection");function n(e,...t){const n=new URL("https://lexical.dev/docs/error"),o=new URLSearchParams;o.append("code",e);for(const e of t)o.append("v",e);throw n.search=o.toString(),Error(`Minified Lexical error #${e}; visit ${n.toString()} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`)}const o="undefined"!=typeof window&&void 0!==window.document&&void 0!==window.document.createElement,r=o&&"documentMode"in document?document.documentMode:null,i=o&&/Mac|iPod|iPhone|iPad/.test(navigator.platform),s=o&&/^(?!.*Seamonkey)(?=.*Firefox).*/i.test(navigator.userAgent),l=!(!o||!("InputEvent"in window)||r)&&"getTargetRanges"in new window.InputEvent("input"),a=o&&/Version\/[\d.]+.*Safari/.test(navigator.userAgent),c=o&&/iPad|iPhone|iPod/.test(navigator.userAgent)&&!window.MSStream,u=o&&/Android/.test(navigator.userAgent),d=o&&/^(?=.*Chrome).*/i.test(navigator.userAgent),g=o&&u&&d,p=o&&/AppleWebKit\/[\d.]+/.test(navigator.userAgent)&&i&&!d;function f(e){return`${e}px`}const m={attributes:!0,characterData:!0,childList:!0,subtree:!0};function h(o,r,i){let s=null,l=null,a=null,c=[];const u=document.createElement("div");function d(){null===s&&n(182),null===l&&n(183);const{left:e,top:a}=l.getBoundingClientRect(),d=t.createRectsFromDOMRange(o,r);var g,p;u.isConnected||(p=u,(g=l).insertBefore(p,g.firstChild));let m=!1;for(let t=0;t<d.length;t++){const n=d[t],o=c[t]||document.createElement("div"),r=o.style;"absolute"!==r.position&&(r.position="absolute",m=!0);const i=f(n.left-e);r.left!==i&&(r.left=i,m=!0);const s=f(n.top-a);r.top!==s&&(o.style.top=s,m=!0);const l=f(n.width);r.width!==l&&(o.style.width=l,m=!0);const g=f(n.height);r.height!==g&&(o.style.height=g,m=!0),o.parentNode!==u&&(u.append(o),m=!0),c[t]=o}for(;c.length>d.length;)c.pop();m&&i(c)}function g(){l=null,s=null,null!==a&&a.disconnect(),a=null,u.remove();for(const e of c)e.remove();c=[]}u.style.position="relative";const p=o.registerRootListener(function t(){const n=o.getRootElement();if(null===n)return g();const r=n.parentElement;if(!e.isHTMLElement(r))return g();g(),s=n,l=r,a=new MutationObserver(e=>{const n=o.getRootElement(),r=n&&n.parentElement;if(n!==s||r!==l)return t();for(const t of e)if(!u.contains(t.target))return d()}),a.observe(r,m),d()});return()=>{p(),g()}}function $(t,n,o){if("text"!==t.type&&e.$isElementNode(n)){const e=n.getDOMSlot(o);return[e.element,e.getFirstChildOffset()+t.offset]}return[e.getDOMTextNode(o)||o,t.offset]}function x(t,n){let o=null,r=null,i=null,s=null,l=null,a=null,c=()=>{};function u(u){u.read(()=>{const u=e.$getSelection();if(!e.$isRangeSelection(u))return o=null,i=null,s=null,a=null,c(),void(c=()=>{});const[d,g]=function(e){const t=e.getStartEndPoints();return e.isBackward()?[t[1],t[0]]:t}(u),p=d.getNode(),m=p.getKey(),x=d.offset,C=g.getNode(),S=C.getKey(),E=g.offset,N=t.getElementByKey(m),v=t.getElementByKey(S),w=null===o||N!==r||x!==i||m!==o.getKey(),b=null===s||v!==l||E!==a||S!==s.getKey();if((w||b)&&null!==N&&null!==v){const e=function(e,t,n,o,r,i,s){const l=(e._window?e._window.document:document).createRange();return l.setStart(...$(t,n,o)),l.setEnd(...$(r,i,s)),l}(t,d,p,N,g,C,v);c(),c=h(t,e,e=>{if(void 0===n)for(const t of e){const e=t.style;"Highlight"!==e.background&&(e.background="Highlight"),"HighlightText"!==e.color&&(e.color="HighlightText"),e.marginTop!==f(-1.5)&&(e.marginTop=f(-1.5)),e.paddingTop!==f(4)&&(e.paddingTop=f(4)),e.paddingBottom!==f(0)&&(e.paddingBottom=f(0))}else n(e)})}o=p,r=N,i=x,s=C,l=v,a=E})}return u(t.getEditorState()),e.mergeRegister(t.registerUpdateListener(({editorState:e})=>u(e)),()=>{c()})}const C=l,S=o,E=u,N=g,v=i,w=p,b=d,y=s,A=c,R=a;function P(e,t){for(const n of t)if(e.type.startsWith(n))return!0;return!1}function I(e,t){return O("next",e,t)}function T(t,n){const o=e.$getAdjacentSiblingOrParentSiblingCaret(e.$getSiblingCaret(t,n));return o&&o[0]}function O(t,n,o){const r=e.$getRoot(),i=n||r,s=e.$isElementNode(i)?e.$getChildCaret(i,t):e.$getSiblingCaret(i,t),l=M(i),a=o?e.$getAdjacentChildCaret(e.$getChildCaretOrSelf(e.$getSiblingCaret(o,t)))||T(o,t):T(i,t);let c=l;return e.makeStepwiseIterator({hasNext:e=>null!==e,initial:s,map:e=>({depth:c,node:e.origin}),step:t=>{if(t.isSameNodeCaret(a))return null;e.$isChildCaret(t)&&c++;const n=e.$getAdjacentSiblingOrParentSiblingCaret(t);return!n||n[0].isSameNodeCaret(a)?null:(c+=n[1],n[0])}})}function M(e){let t=-1;for(let n=e;null!==n;n=n.getParent())t++;return t}function D(e,t){return O("previous",e,t)}function _(t,o,r){let i=e.$getCaretInDirection(o,"next");for(let t=i;t;t=e.$splitAtPointCaretNext(t,r))i=t;return e.$isTextPointCaret(i)&&n(283),i.insert(t.isInline()?e.$createParagraphNode().append(t):t),e.$getCaretInDirection(e.$getSiblingCaret(t.getLatest(),"next"),o.direction)}let L=!(y||!S)&&void 0;function F(t,n,o){let r=!1;for(const i of k(t))n(i)?null!==o&&o(i):(r=!0,e.$isElementNode(i)&&F(i,n,o||(e=>i.insertAfter(e))),i.remove());return r}function k(t){return B(e.$getChildCaret(t,"previous"))}function B(t){return e.makeStepwiseIterator({hasNext:e.$isSiblingCaret,initial:t.getAdjacentCaret(),map:e=>e.origin.getLatest(),step:e=>e.getAdjacentCaret()})}exports.$findMatchingParent=e.$findMatchingParent,exports.$getAdjacentSiblingOrParentSiblingCaret=e.$getAdjacentSiblingOrParentSiblingCaret,exports.$splitNode=e.$splitNode,exports.addClassNamesToElement=e.addClassNamesToElement,exports.isBlockDomNode=e.isBlockDomNode,exports.isHTMLAnchorElement=e.isHTMLAnchorElement,exports.isHTMLElement=e.isHTMLElement,exports.isInlineDomNode=e.isInlineDomNode,exports.mergeRegister=e.mergeRegister,exports.removeClassNamesFromElement=e.removeClassNamesFromElement,exports.$descendantsMatching=function(t,n){const o=[],r=Array.from(t).reverse();for(let t=r.pop();void 0!==t;t=r.pop())if(n(t))o.push(t);else if(e.$isElementNode(t))for(const e of k(t))r.push(e);return o},exports.$dfs=function(e,t){return Array.from(I(e,t))},exports.$dfsIterator=I,exports.$filter=function(e,t){const n=[];for(let o=0;o<e.length;o++){const r=t(e[o]);null!==r&&n.push(r)}return n},exports.$firstToLastIterator=function(t){return B(e.$getChildCaret(t,"next"))},exports.$getAdjacentCaret=function(e){return e?e.getAdjacentCaret():null},exports.$getDepth=M,exports.$getNearestBlockElementAncestorOrThrow=function(t){const o=e.$findMatchingParent(t,t=>e.$isElementNode(t)&&!t.isInline());return e.$isElementNode(o)||n(4,t.__key),o},exports.$getNearestNodeOfType=function(e,t){let n=e;for(;null!=n;){if(n instanceof t)return n;n=n.getParent()}return null},exports.$getNextRightPreorderNode=function(t){const n=e.$getChildCaretOrSelf(e.$getSiblingCaret(t,"previous")),o=e.$getAdjacentSiblingOrParentSiblingCaret(n,"root");return o&&o[0].origin},exports.$getNextSiblingOrParentSibling=function(t){const n=e.$getAdjacentSiblingOrParentSiblingCaret(e.$getSiblingCaret(t,"next"));return n&&[n[0].origin,n[1]]},exports.$insertFirst=function(t,n){e.$getChildCaret(t,"next").insert(n)},exports.$insertNodeToNearestRoot=function(t){const n=e.$getSelection()||e.$getPreviousSelection();let o;if(e.$isRangeSelection(n))o=e.$caretFromPoint(n.focus,"next");else{if(null!=n){const t=n.getNodes(),r=t[t.length-1];r&&(o=e.$getSiblingCaret(r,"next"))}o=o||e.$getChildCaret(e.$getRoot(),"previous").getFlipped().insert(e.$createParagraphNode())}const r=_(t,o),i=e.$getAdjacentChildCaret(r),s=e.$isChildCaret(i)?e.$normalizeCaret(i):r;return e.$setSelectionFromCaretRange(e.$getCollapsedCaretRange(s)),t.getLatest()},exports.$insertNodeToNearestRootAtCaret=_,exports.$isEditorIsNestedEditor=function(e){return null!==e._parentEditor},exports.$lastToFirstIterator=k,exports.$restoreEditorState=function(t,n){const o=new Map,r=t._pendingEditorState;for(const[t,r]of n._nodeMap)o.set(t,e.$cloneWithProperties(r));r&&(r._nodeMap=o),t._dirtyType=2;const i=n._selection;e.$setSelection(null===i?null:i.clone())},exports.$reverseDfs=function(e,t){return Array.from(D(e,t))},exports.$reverseDfsIterator=D,exports.$unwrapAndFilterDescendants=function(e,t){return F(e,t,null)},exports.$unwrapNode=function(t){e.$rewindSiblingCaret(e.$getSiblingCaret(t,"next")).splice(1,t.getChildren())},exports.$wrapNodeInElement=function(e,t){const n=t();return e.replace(n),n.append(e),n},exports.CAN_USE_BEFORE_INPUT=C,exports.CAN_USE_DOM=S,exports.IS_ANDROID=E,exports.IS_ANDROID_CHROME=N,exports.IS_APPLE=v,exports.IS_APPLE_WEBKIT=w,exports.IS_CHROME=b,exports.IS_FIREFOX=y,exports.IS_IOS=A,exports.IS_SAFARI=R,exports.calculateZoomLevel=function(e,t=!1){let n=1;if(function(){if(void 0===L){const e=document.createElement("div");e.style.cssText="position: absolute; opacity: 0; width: 100px; left: -1000px;",document.body.appendChild(e);const t=e.getBoundingClientRect();e.style.setProperty("zoom","2"),L=e.getBoundingClientRect().width===t.width,document.body.removeChild(e)}return L}()||t)for(;e;)n*=Number(window.getComputedStyle(e).getPropertyValue("zoom")),e=e.parentElement;return n},exports.isMimeType=P,exports.makeStateWrapper=function(t){const n=n=>e.$getState(n,t),o=(n,o)=>e.$setState(n,t,o);return{$get:n,$set:o,accessors:[n,o],makeGetterMethod:()=>function(){return n(this)},makeSetterMethod:()=>function(e){return o(this,e)},stateConfig:t}},exports.markSelection=x,exports.mediaFileReader=function(e,t){const n=e[Symbol.iterator]();return new Promise((e,o)=>{const r=[],i=()=>{const{done:s,value:l}=n.next();if(s)return e(r);const a=new FileReader;a.addEventListener("error",o),a.addEventListener("load",()=>{const e=a.result;"string"==typeof e&&r.push({file:l,result:e}),i()}),P(l,t)?a.readAsDataURL(l):i()};i()})},exports.objectKlassEquals=function(e,t){return null!==e&&Object.getPrototypeOf(e).constructor.name===t.name},exports.positionNodeOnRange=h,exports.registerNestedElementResolver=function(e,t,n,o){const r=e=>e instanceof t;return e.registerNodeTransform(t,e=>{const t=(e=>{const t=e.getChildren();for(let e=0;e<t.length;e++){const n=t[e];if(r(n))return null}let n=e,o=e;for(;null!==n;)if(o=n,n=n.getParent(),r(n))return{child:o,parent:n};return null})(e);if(null!==t){const{child:r,parent:i}=t;if(r.is(e)){o(i,e);const t=r.getNextSiblings(),s=t.length;if(i.insertAfter(r),0!==s){const e=n(i);r.insertAfter(e);for(let n=0;n<s;n++)e.append(t[n])}i.canBeEmpty()||0!==i.getChildrenSize()||i.remove()}}})},exports.selectionAlwaysOnDisplay=function(t,n){let o=null;const r=()=>{const r=t.getRootElement();if(!r)return;let i=null,s=r;for(;s;){if(e.isDocumentFragment(s.nodeType)){const e=s;if("getComposedRanges"in Selection.prototype){const t=window.getSelection();if(t){t.getComposedRanges({shadowRoots:[e]}).length>0&&(i=t)}}break}s=s.parentNode}i||(i=getSelection());const l=i&&i.anchorNode;null!==l&&null!==r&&r.contains(l)?null!==o&&(o(),o=null):null===o&&(o=x(t,n))},i=t.getRootElement();let s=document;if(i){let t=i;for(;t;){if(e.isDocumentFragment(t.nodeType)){s=t.ownerDocument||document;break}t=t.parentNode}s=i.ownerDocument||document}return s.addEventListener("selectionchange",r),()=>{null!==o&&o(),s.removeEventListener("selectionchange",r)}};
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+
9
+ import{isHTMLElement as t,mergeRegister as e,$getSelection as n,$isRangeSelection as o,$isElementNode as r,getDOMTextNode as i,isDocumentFragment as l,$getAdjacentSiblingOrParentSiblingCaret as s,$getSiblingCaret as u,$getChildCaretOrSelf as c,$findMatchingParent as a,$cloneWithProperties as d,$setSelection as f,$getPreviousSelection as g,$caretFromPoint as p,$getChildCaret as m,$getRoot as h,$createParagraphNode as v,$getAdjacentChildCaret as w,$isChildCaret as y,$normalizeCaret as E,$setSelectionFromCaretRange as x,$getCollapsedCaretRange as S,$getCaretInDirection as C,$splitAtPointCaretNext as b,$isTextPointCaret as A,$rewindSiblingCaret as N,$getState as R,$setState as P,makeStepwiseIterator as L,$isSiblingCaret as M}from"@ekz/lexical";export{$findMatchingParent,$getAdjacentSiblingOrParentSiblingCaret,$splitNode,addClassNamesToElement,isBlockDomNode,isHTMLAnchorElement,isHTMLElement,isInlineDomNode,mergeRegister,removeClassNamesFromElement}from"@ekz/lexical";import{createRectsFromDOMRange as T}from"@ekz/lexical-selection";function k(t,...e){const n=new URL("https://lexical.dev/docs/error"),o=new URLSearchParams;o.append("code",t);for(const t of e)o.append("v",t);throw n.search=o.toString(),Error(`Minified Lexical error #${t}; visit ${n.toString()} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`)}const B="undefined"!=typeof window&&void 0!==window.document&&void 0!==window.document.createElement,_=B&&"documentMode"in document?document.documentMode:null,$=B&&/Mac|iPod|iPhone|iPad/.test(navigator.platform),D=B&&/^(?!.*Seamonkey)(?=.*Firefox).*/i.test(navigator.userAgent),K=!(!B||!("InputEvent"in window)||_)&&"getTargetRanges"in new window.InputEvent("input"),O=B&&/Version\/[\d.]+.*Safari/.test(navigator.userAgent),z=B&&/iPad|iPhone|iPod/.test(navigator.userAgent)&&!window.MSStream,H=B&&/Android/.test(navigator.userAgent),j=B&&/^(?=.*Chrome).*/i.test(navigator.userAgent),F=B&&H&&j,I=B&&/AppleWebKit\/[\d.]+/.test(navigator.userAgent)&&$&&!j;function U(t){return`${t}px`}const V={attributes:!0,characterData:!0,childList:!0,subtree:!0};function W(e,n,o){let r=null,i=null,l=null,s=[];const u=document.createElement("div");function c(){null===r&&k(182),null===i&&k(183);const{left:t,top:l}=i.getBoundingClientRect(),c=T(e,n);var a,d;u.isConnected||(d=u,(a=i).insertBefore(d,a.firstChild));let f=!1;for(let e=0;e<c.length;e++){const n=c[e],o=s[e]||document.createElement("div"),r=o.style;"absolute"!==r.position&&(r.position="absolute",f=!0);const i=U(n.left-t);r.left!==i&&(r.left=i,f=!0);const a=U(n.top-l);r.top!==a&&(o.style.top=a,f=!0);const d=U(n.width);r.width!==d&&(o.style.width=d,f=!0);const g=U(n.height);r.height!==g&&(o.style.height=g,f=!0),o.parentNode!==u&&(u.append(o),f=!0),s[e]=o}for(;s.length>c.length;)s.pop();f&&o(s)}function a(){i=null,r=null,null!==l&&l.disconnect(),l=null,u.remove();for(const t of s)t.remove();s=[]}u.style.position="relative";const d=e.registerRootListener(function n(){const o=e.getRootElement();if(null===o)return a();const s=o.parentElement;if(!t(s))return a();a(),r=o,i=s,l=new MutationObserver(t=>{const o=e.getRootElement(),l=o&&o.parentElement;if(o!==r||l!==i)return n();for(const e of t)if(!u.contains(e.target))return c()}),l.observe(s,V),c()});return()=>{d(),a()}}function G(t,e,n){if("text"!==t.type&&r(e)){const o=e.getDOMSlot(n);return[o.element,o.getFirstChildOffset()+t.offset]}return[i(n)||n,t.offset]}function q(t,r){let i=null,l=null,s=null,u=null,c=null,a=null,d=()=>{};function f(e){e.read(()=>{const e=n();if(!o(e))return i=null,s=null,u=null,a=null,d(),void(d=()=>{});const[f,g]=function(t){const e=t.getStartEndPoints();return t.isBackward()?[e[1],e[0]]:e}(e),p=f.getNode(),m=p.getKey(),h=f.offset,v=g.getNode(),w=v.getKey(),y=g.offset,E=t.getElementByKey(m),x=t.getElementByKey(w),S=null===i||E!==l||h!==s||m!==i.getKey(),C=null===u||x!==c||y!==a||w!==u.getKey();if((S||C)&&null!==E&&null!==x){const e=function(t,e,n,o,r,i,l){const s=(t._window?t._window.document:document).createRange();return s.setStart(...G(e,n,o)),s.setEnd(...G(r,i,l)),s}(t,f,p,E,g,v,x);d(),d=W(t,e,t=>{if(void 0===r)for(const e of t){const t=e.style;"Highlight"!==t.background&&(t.background="Highlight"),"HighlightText"!==t.color&&(t.color="HighlightText"),t.marginTop!==U(-1.5)&&(t.marginTop=U(-1.5)),t.paddingTop!==U(4)&&(t.paddingTop=U(4)),t.paddingBottom!==U(0)&&(t.paddingBottom=U(0))}else r(t)})}i=p,l=E,s=h,u=v,c=x,a=y})}return f(t.getEditorState()),e(t.registerUpdateListener(({editorState:t})=>f(t)),()=>{d()})}function J(t,e){let n=null;const o=()=>{const o=t.getRootElement();if(!o)return;let r=null,i=o;for(;i;){if(l(i.nodeType)){const t=i;if("getComposedRanges"in Selection.prototype){const e=window.getSelection();if(e){e.getComposedRanges({shadowRoots:[t]}).length>0&&(r=e)}}break}i=i.parentNode}r||(r=getSelection());const s=r&&r.anchorNode;null!==s&&null!==o&&o.contains(s)?null!==n&&(n(),n=null):null===n&&(n=q(t,e))},r=t.getRootElement();let i=document;if(r){let t=r;for(;t;){if(l(t.nodeType)){i=t.ownerDocument||document;break}t=t.parentNode}i=r.ownerDocument||document}return i.addEventListener("selectionchange",o),()=>{null!==n&&n(),i.removeEventListener("selectionchange",o)}}const Q=K,X=B,Y=H,Z=F,tt=$,et=I,nt=j,ot=D,rt=z,it=O;function lt(t,e){for(const n of e)if(t.type.startsWith(n))return!0;return!1}function st(t,e){const n=t[Symbol.iterator]();return new Promise((t,o)=>{const r=[],i=()=>{const{done:l,value:s}=n.next();if(l)return t(r);const u=new FileReader;u.addEventListener("error",o),u.addEventListener("load",()=>{const t=u.result;"string"==typeof t&&r.push({file:s,result:t}),i()}),lt(s,e)?u.readAsDataURL(s):i()};i()})}function ut(t,e){return Array.from(dt(t,e))}function ct(t){return t?t.getAdjacentCaret():null}function at(t,e){return Array.from(vt(t,e))}function dt(t,e){return gt("next",t,e)}function ft(t,e){const n=s(u(t,e));return n&&n[0]}function gt(t,e,n){const o=h(),i=e||o,l=r(i)?m(i,t):u(i,t),a=mt(i),d=n?w(c(u(n,t)))||ft(n,t):ft(i,t);let f=a;return L({hasNext:t=>null!==t,initial:l,map:t=>({depth:f,node:t.origin}),step:t=>{if(t.isSameNodeCaret(d))return null;y(t)&&f++;const e=s(t);return!e||e[0].isSameNodeCaret(d)?null:(f+=e[1],e[0])}})}function pt(t){const e=s(u(t,"next"));return e&&[e[0].origin,e[1]]}function mt(t){let e=-1;for(let n=t;null!==n;n=n.getParent())e++;return e}function ht(t){const e=c(u(t,"previous")),n=s(e,"root");return n&&n[0].origin}function vt(t,e){return gt("previous",t,e)}function wt(t,e){let n=t;for(;null!=n;){if(n instanceof e)return n;n=n.getParent()}return null}function yt(t){const e=a(t,t=>r(t)&&!t.isInline());return r(e)||k(4,t.__key),e}function Et(t,e,n,o){const r=t=>t instanceof e;return t.registerNodeTransform(e,t=>{const e=(t=>{const e=t.getChildren();for(let t=0;t<e.length;t++){const n=e[t];if(r(n))return null}let n=t,o=t;for(;null!==n;)if(o=n,n=n.getParent(),r(n))return{child:o,parent:n};return null})(t);if(null!==e){const{child:r,parent:i}=e;if(r.is(t)){o(i,t);const e=r.getNextSiblings(),l=e.length;if(i.insertAfter(r),0!==l){const t=n(i);r.insertAfter(t);for(let n=0;n<l;n++)t.append(e[n])}i.canBeEmpty()||0!==i.getChildrenSize()||i.remove()}}})}function xt(t,e){const n=new Map,o=t._pendingEditorState;for(const[t,o]of e._nodeMap)n.set(t,d(o));o&&(o._nodeMap=n),t._dirtyType=2;const r=e._selection;f(null===r?null:r.clone())}function St(t){const e=n()||g();let r;if(o(e))r=p(e.focus,"next");else{if(null!=e){const t=e.getNodes(),n=t[t.length-1];n&&(r=u(n,"next"))}r=r||m(h(),"previous").getFlipped().insert(v())}const i=Ct(t,r),l=w(i),s=y(l)?E(l):i;return x(S(s)),t.getLatest()}function Ct(t,e,n){let o=C(e,"next");for(let t=o;t;t=b(t,n))o=t;return A(o)&&k(283),o.insert(t.isInline()?v().append(t):t),C(u(t.getLatest(),"next"),e.direction)}function bt(t,e){const n=e();return t.replace(n),n.append(t),n}function At(t,e){return null!==t&&Object.getPrototypeOf(t).constructor.name===e.name}function Nt(t,e){const n=[];for(let o=0;o<t.length;o++){const r=e(t[o]);null!==r&&n.push(r)}return n}function Rt(t,e){m(t,"next").insert(e)}let Pt=!(ot||!X)&&void 0;function Lt(t,e=!1){let n=1;if(function(){if(void 0===Pt){const t=document.createElement("div");t.style.cssText="position: absolute; opacity: 0; width: 100px; left: -1000px;",document.body.appendChild(t);const e=t.getBoundingClientRect();t.style.setProperty("zoom","2"),Pt=t.getBoundingClientRect().width===e.width,document.body.removeChild(t)}return Pt}()||e)for(;t;)n*=Number(window.getComputedStyle(t).getPropertyValue("zoom")),t=t.parentElement;return n}function Mt(t){return null!==t._parentEditor}function Tt(t,e){return kt(t,e,null)}function kt(t,e,n){let o=!1;for(const i of $t(t))e(i)?null!==n&&n(i):(o=!0,r(i)&&kt(i,e,n||(t=>i.insertAfter(t))),i.remove());return o}function Bt(t,e){const n=[],o=Array.from(t).reverse();for(let t=o.pop();void 0!==t;t=o.pop())if(e(t))n.push(t);else if(r(t))for(const e of $t(t))o.push(e);return n}function _t(t){return Dt(m(t,"next"))}function $t(t){return Dt(m(t,"previous"))}function Dt(t){return L({hasNext:M,initial:t.getAdjacentCaret(),map:t=>t.origin.getLatest(),step:t=>t.getAdjacentCaret()})}function Kt(t){N(u(t,"next")).splice(1,t.getChildren())}function Ot(t){const e=e=>R(e,t),n=(e,n)=>P(e,t,n);return{$get:e,$set:n,accessors:[e,n],makeGetterMethod:()=>function(){return e(this)},makeSetterMethod:()=>function(t){return n(this,t)},stateConfig:t}}export{Bt as $descendantsMatching,ut as $dfs,dt as $dfsIterator,Nt as $filter,_t as $firstToLastIterator,ct as $getAdjacentCaret,mt as $getDepth,yt as $getNearestBlockElementAncestorOrThrow,wt as $getNearestNodeOfType,ht as $getNextRightPreorderNode,pt as $getNextSiblingOrParentSibling,Rt as $insertFirst,St as $insertNodeToNearestRoot,Ct as $insertNodeToNearestRootAtCaret,Mt as $isEditorIsNestedEditor,$t as $lastToFirstIterator,xt as $restoreEditorState,at as $reverseDfs,vt as $reverseDfsIterator,Tt as $unwrapAndFilterDescendants,Kt as $unwrapNode,bt as $wrapNodeInElement,Q as CAN_USE_BEFORE_INPUT,X as CAN_USE_DOM,Y as IS_ANDROID,Z as IS_ANDROID_CHROME,tt as IS_APPLE,et as IS_APPLE_WEBKIT,nt as IS_CHROME,ot as IS_FIREFOX,rt as IS_IOS,it as IS_SAFARI,Lt as calculateZoomLevel,lt as isMimeType,Ot as makeStateWrapper,q as markSelection,st as mediaFileReader,At as objectKlassEquals,W as positionNodeOnRange,Et as registerNestedElementResolver,J as selectionAlwaysOnDisplay};
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Meta Platforms, Inc. and affiliates.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,224 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ * @flow strict
8
+ */
9
+ import type {
10
+ EditorState,
11
+ LexicalEditor,
12
+ LexicalNode,
13
+ ElementNode,
14
+ NodeCaret,
15
+ CaretDirection,
16
+ SiblingCaret,
17
+ RootMode,
18
+ SplitAtPointCaretNextOptions,
19
+ PointCaret,
20
+ StateConfig,
21
+ ValueOrUpdater
22
+ } from '@ekz/lexical';
23
+ declare export function addClassNamesToElement(
24
+ element: HTMLElement,
25
+ ...classNames: Array<typeof undefined | boolean | null | string>
26
+ ): void;
27
+ declare export function removeClassNamesFromElement(
28
+ element: HTMLElement,
29
+ ...classNames: Array<typeof undefined | boolean | null | string>
30
+ ): void;
31
+ declare export function isMimeType(
32
+ file: File,
33
+ acceptableMimeTypes: Array<string>,
34
+ ): boolean;
35
+ declare export function mediaFileReader(
36
+ files: Array<File>,
37
+ acceptableMimeTypes: Array<string>,
38
+ ): Promise<Array<$ReadOnly<{file: File, result: string}>>>;
39
+ export type DFSNode = $ReadOnly<{
40
+ depth: number,
41
+ node: LexicalNode,
42
+ }>;
43
+ declare export function $dfs(
44
+ startNode?: LexicalNode,
45
+ endNode?: LexicalNode,
46
+ ): Array<DFSNode>;
47
+ type DFSIterator = {
48
+ next: () => IteratorResult<DFSNode, void>;
49
+ @@iterator: () => DFSIterator;
50
+ };
51
+ declare export function $dfsIterator(
52
+ startNode?: LexicalNode,
53
+ endNode?: LexicalNode,
54
+ ): DFSIterator;
55
+ declare export function $getNextSiblingOrParentSibling(
56
+ node: LexicalNode,
57
+ ): null | [LexicalNode, number];
58
+ declare export function $getDepth(node: null | LexicalNode): number;
59
+ declare export function $getNextRightPreorderNode(
60
+ startingNode: LexicalNode,
61
+ ): LexicalNode | null;
62
+ declare export function $getNearestNodeOfType<T: LexicalNode>(
63
+ node: LexicalNode,
64
+ klass: Class<T>,
65
+ ): T | null;
66
+ export type DOMNodeToLexicalConversion = (element: Node) => LexicalNode;
67
+ export type DOMNodeToLexicalConversionMap = {
68
+ [string]: DOMNodeToLexicalConversion,
69
+ };
70
+ declare export function $findMatchingParent(
71
+ startingNode: LexicalNode,
72
+ findFn: (LexicalNode) => boolean,
73
+ ): LexicalNode | null;
74
+ declare export function mergeRegister(...func: Array<() => void>): () => void;
75
+ declare export function markSelection(
76
+ editor: LexicalEditor,
77
+ onReposition?: (node: Array<HTMLElement>) => void,
78
+ ): () => void;
79
+ declare export function positionNodeOnRange(
80
+ editor: LexicalEditor,
81
+ range: Range,
82
+ onReposition: (node: Array<HTMLElement>) => void,
83
+ ): () => void;
84
+ declare export function selectionAlwaysOnDisplay(
85
+ editor: LexicalEditor,
86
+ onReposition?: (node: Array<HTMLElement>) => void,
87
+ ): () => void;
88
+ declare export function $getNearestBlockElementAncestorOrThrow(
89
+ startNode: LexicalNode,
90
+ ): ElementNode;
91
+
92
+ declare export function registerNestedElementResolver<N: ElementNode>(
93
+ editor: LexicalEditor,
94
+ targetNode: Class<N>,
95
+ cloneNode: (from: N) => N,
96
+ handleOverlap: (from: N, to: N) => void,
97
+ ): () => void;
98
+
99
+ declare export function unstable_convertLegacyJSONEditorState(
100
+ editor: LexicalEditor,
101
+ maybeStringifiedEditorState: string,
102
+ ): EditorState;
103
+
104
+ declare export function $restoreEditorState(
105
+ editor: LexicalEditor,
106
+ editorState: EditorState,
107
+ ): void;
108
+
109
+
110
+ declare export function $insertNodeToNearestRoot<T: LexicalNode>(node: T): T;
111
+ declare export function $insertNodeToNearestRootAtCaret<T: LexicalNode>(node: T, caret: PointCaret<CaretDirection>, options?: SplitAtPointCaretNextOptions): T;
112
+
113
+ declare export function $wrapNodeInElement(
114
+ node: LexicalNode,
115
+ createElementNode: () => ElementNode,
116
+ ): ElementNode;
117
+
118
+ declare export function objectKlassEquals<T>(
119
+ object: mixed,
120
+ objectClass: Class<T>,
121
+ ): boolean;
122
+
123
+ declare export function $filter<T>(
124
+ nodes: Array<LexicalNode>,
125
+ filterFn: (node: LexicalNode) => null | T,
126
+ ): Array<T>;
127
+
128
+ declare export function $insertFirst(parent: ElementNode, node: LexicalNode): void;
129
+
130
+ declare export function $splitNode(
131
+ node: ElementNode,
132
+ offset: number,
133
+ ): [ElementNode | null, ElementNode];
134
+
135
+ declare export function calculateZoomLevel(element: Element | null, useManualZoom?: boolean): number;
136
+
137
+ declare export function $isEditorIsNestedEditor(editor: LexicalEditor): boolean;
138
+
139
+ declare export function $unwrapAndFilterDescendants(
140
+ root: ElementNode,
141
+ $predicate: (node: LexicalNode) => boolean,
142
+ ): boolean;
143
+
144
+ declare export function $firstToLastIterator(node: ElementNode): Iterable<LexicalNode>;
145
+
146
+ declare export function $lastToFirstIterator(node: ElementNode): Iterable<LexicalNode>;
147
+
148
+ declare export function $unwrapNode(node: ElementNode): void;
149
+
150
+ declare export function $getAdjacentCaret<D: CaretDirection>(
151
+ caret: null | NodeCaret<D>,
152
+ ): null | SiblingCaret<LexicalNode, D>;
153
+
154
+ declare export function $reverseDfs(
155
+ startNode?: LexicalNode,
156
+ endNode?: LexicalNode,
157
+ ): Array<DFSNode>;
158
+
159
+ declare export function $reverseDfsIterator(
160
+ startNode?: LexicalNode,
161
+ endNode?: LexicalNode,
162
+ ): Iterable<DFSNode>;
163
+
164
+ declare export function $descendantsMatching<T: LexicalNode>(
165
+ children: LexicalNode[],
166
+ $predicate: (node: LexicalNode) => node is T,
167
+ ): T[];
168
+ declare export function $descendantsMatching(
169
+ children: LexicalNode[],
170
+ $predicate: (node: LexicalNode) => boolean,
171
+ ): LexicalNode[];
172
+
173
+ declare export function $getAdjacentSiblingOrParentSiblingCaret<
174
+ D: CaretDirection,
175
+ >(
176
+ startCaret: NodeCaret<D>,
177
+ rootMode?: RootMode,
178
+ ): null | [NodeCaret<D>, number];
179
+
180
+ export type StateConfigWrapper<K: string, V> = {
181
+ +stateConfig: StateConfig<K, V>;
182
+ +$get: <T: LexicalNode>(node: T) => V;
183
+ +$set: <T: LexicalNode>(
184
+ node: T,
185
+ valueOrUpdater: ValueOrUpdater<V>,
186
+ ) => T;
187
+ /** `[$get, $set]` */
188
+ +accessors: $ReadOnly<[$get: StateConfigWrapper<K, V>['$get'], $set: StateConfigWrapper<K, V>['$set']]>;
189
+ /**
190
+ * `() => function () { return $get(this) }`
191
+ *
192
+ * Should be called with an explicit `this` type parameter.
193
+ *
194
+ * @example
195
+ * ```ts
196
+ * class MyNode {
197
+ * // …
198
+ * myGetter = myWrapper.makeGetterMethod<this>();
199
+ * }
200
+ * ```
201
+ */
202
+ makeGetterMethod<T: LexicalNode>(): (this: T) => V;
203
+ /**
204
+ * `() => function (valueOrUpdater) { return $set(this, valueOrUpdater) }`
205
+ *
206
+ * Must be called with an explicit `this` type parameter.
207
+ *
208
+ * @example
209
+ * ```ts
210
+ * class MyNode {
211
+ * // …
212
+ * mySetter = myWrapper.makeSetterMethod<this>();
213
+ * }
214
+ * ```
215
+ */
216
+ makeSetterMethod<T: LexicalNode>(): (
217
+ this: T,
218
+ valueOrUpdater: ValueOrUpdater<V>,
219
+ ) => T;
220
+ };
221
+
222
+ declare export function makeStateWrapper<K: string, V>(
223
+ stateConfig: StateConfig<K, V>,
224
+ ): StateConfigWrapper<K, V>;
package/README.md ADDED
@@ -0,0 +1,5 @@
1
+ # `@lexical/utils`
2
+
3
+ [![See API Documentation](https://lexical.dev/img/see-api-documentation.svg)](https://lexical.dev/docs/api/modules/lexical_utils)
4
+
5
+ This package contains misc utilities for Lexical.