@lexical/utils 0.14.2 → 0.14.3
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.
- package/LexicalUtils.dev.js +51 -0
- package/{LexicalUtils.dev.esm.js → LexicalUtils.dev.mjs} +51 -1
- package/{LexicalUtils.esm.js → LexicalUtils.mjs} +3 -2
- package/LexicalUtils.node.mjs +29 -0
- package/LexicalUtils.prod.js +17 -16
- package/LexicalUtils.prod.mjs +7 -0
- package/index.d.ts +6 -0
- package/package.json +21 -9
- package/LexicalUtils.prod.esm.js +0 -7
package/LexicalUtils.dev.js
CHANGED
|
@@ -9,6 +9,39 @@
|
|
|
9
9
|
var selection = require('@lexical/selection');
|
|
10
10
|
var lexical = require('lexical');
|
|
11
11
|
|
|
12
|
+
/**
|
|
13
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
14
|
+
*
|
|
15
|
+
* This source code is licensed under the MIT license found in the
|
|
16
|
+
* LICENSE file in the root directory of this source tree.
|
|
17
|
+
*
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
const CAN_USE_DOM = typeof window !== 'undefined' && typeof window.document !== 'undefined' && typeof window.document.createElement !== 'undefined';
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
24
|
+
*
|
|
25
|
+
* This source code is licensed under the MIT license found in the
|
|
26
|
+
* LICENSE file in the root directory of this source tree.
|
|
27
|
+
*
|
|
28
|
+
*/
|
|
29
|
+
const documentMode = CAN_USE_DOM && 'documentMode' in document ? document.documentMode : null;
|
|
30
|
+
CAN_USE_DOM && /Mac|iPod|iPhone|iPad/.test(navigator.platform);
|
|
31
|
+
const IS_FIREFOX = CAN_USE_DOM && /^(?!.*Seamonkey)(?=.*Firefox).*/i.test(navigator.userAgent);
|
|
32
|
+
CAN_USE_DOM && 'InputEvent' in window && !documentMode ? 'getTargetRanges' in new window.InputEvent('input') : false;
|
|
33
|
+
CAN_USE_DOM && /Version\/[\d.]+.*Safari/.test(navigator.userAgent);
|
|
34
|
+
CAN_USE_DOM && /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
|
|
35
|
+
const IS_ANDROID = CAN_USE_DOM && /Android/.test(navigator.userAgent);
|
|
36
|
+
|
|
37
|
+
// Keep these in case we need to use them in the future.
|
|
38
|
+
// export const IS_WINDOWS: boolean = CAN_USE_DOM && /Win/.test(navigator.platform);
|
|
39
|
+
const IS_CHROME = CAN_USE_DOM && /^(?=.*Chrome).*/i.test(navigator.userAgent);
|
|
40
|
+
// export const canUseTextInputEvent: boolean = CAN_USE_DOM && 'TextEvent' in window && !documentMode;
|
|
41
|
+
|
|
42
|
+
CAN_USE_DOM && IS_ANDROID && IS_CHROME;
|
|
43
|
+
CAN_USE_DOM && /AppleWebKit\/[\d.]+/.test(navigator.userAgent) && !IS_CHROME;
|
|
44
|
+
|
|
12
45
|
/**
|
|
13
46
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
14
47
|
*
|
|
@@ -728,6 +761,23 @@ function $insertFirst(parent, node) {
|
|
|
728
761
|
}
|
|
729
762
|
}
|
|
730
763
|
|
|
764
|
+
/**
|
|
765
|
+
* Calculates the zoom level of an element as a result of using
|
|
766
|
+
* css zoom property.
|
|
767
|
+
* @param element
|
|
768
|
+
*/
|
|
769
|
+
function calculateZoomLevel(element) {
|
|
770
|
+
if (IS_FIREFOX) {
|
|
771
|
+
return 1;
|
|
772
|
+
}
|
|
773
|
+
let zoom = 1;
|
|
774
|
+
while (element) {
|
|
775
|
+
zoom *= Number(window.getComputedStyle(element).getPropertyValue('zoom'));
|
|
776
|
+
element = element.parentElement;
|
|
777
|
+
}
|
|
778
|
+
return zoom;
|
|
779
|
+
}
|
|
780
|
+
|
|
731
781
|
exports.$splitNode = lexical.$splitNode;
|
|
732
782
|
exports.isHTMLAnchorElement = lexical.isHTMLAnchorElement;
|
|
733
783
|
exports.isHTMLElement = lexical.isHTMLElement;
|
|
@@ -741,6 +791,7 @@ exports.$insertNodeToNearestRoot = $insertNodeToNearestRoot;
|
|
|
741
791
|
exports.$restoreEditorState = $restoreEditorState;
|
|
742
792
|
exports.$wrapNodeInElement = $wrapNodeInElement;
|
|
743
793
|
exports.addClassNamesToElement = addClassNamesToElement;
|
|
794
|
+
exports.calculateZoomLevel = calculateZoomLevel;
|
|
744
795
|
exports.isMimeType = isMimeType;
|
|
745
796
|
exports.markSelection = markSelection;
|
|
746
797
|
exports.mediaFileReader = mediaFileReader;
|
|
@@ -8,6 +8,39 @@ import { createRectsFromDOMRange, $cloneWithProperties } from '@lexical/selectio
|
|
|
8
8
|
import { $getSelection, $isRangeSelection, TextNode, $getRoot, $isElementNode, $isTextNode, $setSelection, $getPreviousSelection, $isRootOrShadowRoot, $splitNode, $createParagraphNode } from 'lexical';
|
|
9
9
|
export { $splitNode, isHTMLAnchorElement, isHTMLElement } from 'lexical';
|
|
10
10
|
|
|
11
|
+
/**
|
|
12
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
13
|
+
*
|
|
14
|
+
* This source code is licensed under the MIT license found in the
|
|
15
|
+
* LICENSE file in the root directory of this source tree.
|
|
16
|
+
*
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
const CAN_USE_DOM = typeof window !== 'undefined' && typeof window.document !== 'undefined' && typeof window.document.createElement !== 'undefined';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
23
|
+
*
|
|
24
|
+
* This source code is licensed under the MIT license found in the
|
|
25
|
+
* LICENSE file in the root directory of this source tree.
|
|
26
|
+
*
|
|
27
|
+
*/
|
|
28
|
+
const documentMode = CAN_USE_DOM && 'documentMode' in document ? document.documentMode : null;
|
|
29
|
+
CAN_USE_DOM && /Mac|iPod|iPhone|iPad/.test(navigator.platform);
|
|
30
|
+
const IS_FIREFOX = CAN_USE_DOM && /^(?!.*Seamonkey)(?=.*Firefox).*/i.test(navigator.userAgent);
|
|
31
|
+
CAN_USE_DOM && 'InputEvent' in window && !documentMode ? 'getTargetRanges' in new window.InputEvent('input') : false;
|
|
32
|
+
CAN_USE_DOM && /Version\/[\d.]+.*Safari/.test(navigator.userAgent);
|
|
33
|
+
CAN_USE_DOM && /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
|
|
34
|
+
const IS_ANDROID = CAN_USE_DOM && /Android/.test(navigator.userAgent);
|
|
35
|
+
|
|
36
|
+
// Keep these in case we need to use them in the future.
|
|
37
|
+
// export const IS_WINDOWS: boolean = CAN_USE_DOM && /Win/.test(navigator.platform);
|
|
38
|
+
const IS_CHROME = CAN_USE_DOM && /^(?=.*Chrome).*/i.test(navigator.userAgent);
|
|
39
|
+
// export const canUseTextInputEvent: boolean = CAN_USE_DOM && 'TextEvent' in window && !documentMode;
|
|
40
|
+
|
|
41
|
+
CAN_USE_DOM && IS_ANDROID && IS_CHROME;
|
|
42
|
+
CAN_USE_DOM && /AppleWebKit\/[\d.]+/.test(navigator.userAgent) && !IS_CHROME;
|
|
43
|
+
|
|
11
44
|
/**
|
|
12
45
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
13
46
|
*
|
|
@@ -727,4 +760,21 @@ function $insertFirst(parent, node) {
|
|
|
727
760
|
}
|
|
728
761
|
}
|
|
729
762
|
|
|
730
|
-
|
|
763
|
+
/**
|
|
764
|
+
* Calculates the zoom level of an element as a result of using
|
|
765
|
+
* css zoom property.
|
|
766
|
+
* @param element
|
|
767
|
+
*/
|
|
768
|
+
function calculateZoomLevel(element) {
|
|
769
|
+
if (IS_FIREFOX) {
|
|
770
|
+
return 1;
|
|
771
|
+
}
|
|
772
|
+
let zoom = 1;
|
|
773
|
+
while (element) {
|
|
774
|
+
zoom *= Number(window.getComputedStyle(element).getPropertyValue('zoom'));
|
|
775
|
+
element = element.parentElement;
|
|
776
|
+
}
|
|
777
|
+
return zoom;
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
export { $dfs, $filter, $findMatchingParent, $getNearestBlockElementAncestorOrThrow, $getNearestNodeOfType, $insertFirst, $insertNodeToNearestRoot, $restoreEditorState, $wrapNodeInElement, addClassNamesToElement, calculateZoomLevel, isMimeType, markSelection, mediaFileReader, mergeRegister, objectKlassEquals, positionNodeOnRange, registerNestedElementResolver, removeClassNamesFromElement };
|
|
@@ -4,8 +4,8 @@
|
|
|
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
|
-
import * as modDev from './LexicalUtils.dev.
|
|
8
|
-
import * as modProd from './LexicalUtils.prod.
|
|
7
|
+
import * as modDev from './LexicalUtils.dev.mjs';
|
|
8
|
+
import * as modProd from './LexicalUtils.prod.mjs';
|
|
9
9
|
const mod = process.env.NODE_ENV === 'development' ? modDev : modProd;
|
|
10
10
|
export const $dfs = mod.$dfs;
|
|
11
11
|
export const $filter = mod.$filter;
|
|
@@ -18,6 +18,7 @@ export const $restoreEditorState = mod.$restoreEditorState;
|
|
|
18
18
|
export const $splitNode = mod.$splitNode;
|
|
19
19
|
export const $wrapNodeInElement = mod.$wrapNodeInElement;
|
|
20
20
|
export const addClassNamesToElement = mod.addClassNamesToElement;
|
|
21
|
+
export const calculateZoomLevel = mod.calculateZoomLevel;
|
|
21
22
|
export const isHTMLAnchorElement = mod.isHTMLAnchorElement;
|
|
22
23
|
export const isHTMLElement = mod.isHTMLElement;
|
|
23
24
|
export const isMimeType = mod.isMimeType;
|
|
@@ -0,0 +1,29 @@
|
|
|
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
|
+
const mod = await (process.env.NODE_ENV === 'development' ? import('./LexicalUtils.dev.mjs') : import('./LexicalUtils.prod.mjs'));
|
|
8
|
+
export const $dfs = mod.$dfs;
|
|
9
|
+
export const $filter = mod.$filter;
|
|
10
|
+
export const $findMatchingParent = mod.$findMatchingParent;
|
|
11
|
+
export const $getNearestBlockElementAncestorOrThrow = mod.$getNearestBlockElementAncestorOrThrow;
|
|
12
|
+
export const $getNearestNodeOfType = mod.$getNearestNodeOfType;
|
|
13
|
+
export const $insertFirst = mod.$insertFirst;
|
|
14
|
+
export const $insertNodeToNearestRoot = mod.$insertNodeToNearestRoot;
|
|
15
|
+
export const $restoreEditorState = mod.$restoreEditorState;
|
|
16
|
+
export const $splitNode = mod.$splitNode;
|
|
17
|
+
export const $wrapNodeInElement = mod.$wrapNodeInElement;
|
|
18
|
+
export const addClassNamesToElement = mod.addClassNamesToElement;
|
|
19
|
+
export const calculateZoomLevel = mod.calculateZoomLevel;
|
|
20
|
+
export const isHTMLAnchorElement = mod.isHTMLAnchorElement;
|
|
21
|
+
export const isHTMLElement = mod.isHTMLElement;
|
|
22
|
+
export const isMimeType = mod.isMimeType;
|
|
23
|
+
export const markSelection = mod.markSelection;
|
|
24
|
+
export const mediaFileReader = mod.mediaFileReader;
|
|
25
|
+
export const mergeRegister = mod.mergeRegister;
|
|
26
|
+
export const objectKlassEquals = mod.objectKlassEquals;
|
|
27
|
+
export const positionNodeOnRange = mod.positionNodeOnRange;
|
|
28
|
+
export const registerNestedElementResolver = mod.registerNestedElementResolver;
|
|
29
|
+
export const removeClassNamesFromElement = mod.removeClassNamesFromElement;
|
package/LexicalUtils.prod.js
CHANGED
|
@@ -4,20 +4,21 @@
|
|
|
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
|
-
'use strict';var h=require("@lexical/selection"),
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
exports.$
|
|
14
|
-
exports.$
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
exports
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
exports.
|
|
7
|
+
'use strict';var h=require("@lexical/selection"),y=require("lexical");function C(a){let b=new URLSearchParams;b.append("code",a);for(let c=1;c<arguments.length;c++)b.append("v",arguments[c]);throw Error(`Minified Lexical error #${a}; visit https://lexical.dev/docs/error?${b} for the full message or `+"use the non-minified dev environment for full errors and additional helpful warnings.");}
|
|
8
|
+
let D="undefined"!==typeof window&&"undefined"!==typeof window.document&&"undefined"!==typeof window.document.createElement,E=D&&"documentMode"in document?document.documentMode:null;D&&/Mac|iPod|iPhone|iPad/.test(navigator.platform);let F=D&&/^(?!.*Seamonkey)(?=.*Firefox).*/i.test(navigator.userAgent);D&&"InputEvent"in window&&!E?"getTargetRanges"in new window.InputEvent("input"):!1;D&&/Version\/[\d.]+.*Safari/.test(navigator.userAgent);D&&/iPad|iPhone|iPod/.test(navigator.userAgent)&&!window.MSStream;
|
|
9
|
+
let G=D&&/Android/.test(navigator.userAgent),H=D&&/^(?=.*Chrome).*/i.test(navigator.userAgent);D&&G&&H;D&&/AppleWebKit\/[\d.]+/.test(navigator.userAgent)&&!H;function I(...a){let b=[];for(let c of a)if(c&&"string"===typeof c)for(let [e]of c.matchAll(/\S+/g))b.push(e);return b}function J(...a){return()=>{a.forEach(b=>b())}}let K={attributes:!0,characterData:!0,childList:!0,subtree:!0};
|
|
10
|
+
function L(a,b,c){function e(){if(null===g)throw Error("Unexpected null rootDOMNode");if(null===n)throw Error("Unexpected null parentDOMNode");let {left:p,top:A}=g.getBoundingClientRect();var q=n;let r=h.createRectsFromDOMRange(a,b);t.isConnected||q.append(t);q=!1;for(let x=0;x<r.length;x++){var w=r[x];let u=k[x]||document.createElement("div"),z=u.style;"absolute"!==z.position&&(z.position="absolute",q=!0);var l=`${w.left-p}px`;z.left!==l&&(z.left=l,q=!0);l=`${w.top-A}px`;z.top!==l&&(u.style.top=
|
|
11
|
+
l,q=!0);l=`${w.width}px`;z.width!==l&&(u.style.width=l,q=!0);w=`${w.height}px`;z.height!==w&&(u.style.height=w,q=!0);u.parentNode!==t&&(t.append(u),q=!0);k[x]=u}for(;k.length>r.length;)k.pop();q&&c(k)}function d(){g=n=null;null!==m&&m.disconnect();m=null;t.remove();for(let p of k)p.remove();k=[]}function f(){let p=a.getRootElement();if(null===p)return d();let A=p.parentElement;if(!(A instanceof HTMLElement))return d();d();g=p;n=A;m=new MutationObserver(q=>{let r=a.getRootElement(),w=r&&r.parentElement;
|
|
12
|
+
if(r!==g||w!==n)return f();for(let l of q)if(!t.contains(l.target))return e()});m.observe(A,K);e()}let g=null,n=null,m=null,k=[],t=document.createElement("div"),B=a.registerRootListener(f);return()=>{B();d()}}function M(a,b){for(let c of b)if(a.type.startsWith(c))return!0;return!1}let N=(a,b)=>{for(;a!==y.$getRoot()&&null!=a;){if(b(a))return a;a=a.getParent()}return null};exports.$splitNode=y.$splitNode;exports.isHTMLAnchorElement=y.isHTMLAnchorElement;exports.isHTMLElement=y.isHTMLElement;
|
|
13
|
+
exports.$dfs=function(a,b){let c=[];a=(a||y.$getRoot()).getLatest();b=b||(y.$isElementNode(a)?a.getLastDescendant():a);for(var e=a,d=0;null!==(e=e.getParent());)d++;for(e=d;null!==a&&!a.is(b);)if(c.push({depth:e,node:a}),y.$isElementNode(a)&&0<a.getChildrenSize())a=a.getFirstChild(),e++;else for(d=null;null===d&&null!==a;)d=a.getNextSibling(),null===d?(a=a.getParent(),e--):a=d;null!==a&&a.is(b)&&c.push({depth:e,node:a});return c};
|
|
14
|
+
exports.$filter=function(a,b){let c=[];for(let e=0;e<a.length;e++){let d=b(a[e]);null!==d&&c.push(d)}return c};exports.$findMatchingParent=N;exports.$getNearestBlockElementAncestorOrThrow=function(a){let b=N(a,c=>y.$isElementNode(c)&&!c.isInline());y.$isElementNode(b)||C(4,a.__key);return b};exports.$getNearestNodeOfType=function(a,b){for(;null!=a;){if(a instanceof b)return a;a=a.getParent()}return null};exports.$insertFirst=function(a,b){let c=a.getFirstChild();null!==c?c.insertBefore(b):a.append(b)};
|
|
15
|
+
exports.$insertNodeToNearestRoot=function(a){var b=y.$getSelection()||y.$getPreviousSelection();if(y.$isRangeSelection(b)){var {focus:c}=b;b=c.getNode();c=c.offset;if(y.$isRootOrShadowRoot(b))c=b.getChildAtIndex(c),null==c?b.append(a):c.insertBefore(a),a.selectNext();else{let e,d;y.$isTextNode(b)?(e=b.getParentOrThrow(),d=b.getIndexWithinParent(),0<c&&(d+=1,b.splitText(c))):(e=b,d=c);[,b]=y.$splitNode(e,d);b.insertBefore(a);b.selectStart()}}else null!=b?(b=b.getNodes(),b[b.length-1].getTopLevelElementOrThrow().insertAfter(a)):
|
|
16
|
+
y.$getRoot().append(a),b=y.$createParagraphNode(),a.insertAfter(b),b.select();return a.getLatest()};exports.$restoreEditorState=function(a,b){let c=new Map,e=a._pendingEditorState;for(let [d,f]of b._nodeMap){let g=h.$cloneWithProperties(f);if(y.$isTextNode(g)){if(!y.$isTextNode(f))throw Error("Expected node be a TextNode");g.__text=f.__text}c.set(d,g)}e&&(e._nodeMap=c);a._dirtyType=2;a=b._selection;y.$setSelection(null===a?null:a.clone())};
|
|
17
|
+
exports.$wrapNodeInElement=function(a,b){b=b();a.replace(b);b.append(a);return b};exports.addClassNamesToElement=function(a,...b){b=I(...b);0<b.length&&a.classList.add(...b)};exports.calculateZoomLevel=function(a){if(F)return 1;let b=1;for(;a;)b*=Number(window.getComputedStyle(a).getPropertyValue("zoom")),a=a.parentElement;return b};exports.isMimeType=M;
|
|
18
|
+
exports.markSelection=function(a,b){function c(m){m.read(()=>{var k=y.$getSelection();if(y.$isRangeSelection(k)){var {anchor:t,focus:B}=k;k=t.getNode();var p=k.getKey(),A=t.offset,q=B.getNode(),r=q.getKey(),w=B.offset,l=a.getElementByKey(p),x=a.getElementByKey(r);p=null===e||null===l||A!==d||p!==e.getKey()||k!==e&&(!(e instanceof y.TextNode)||k.updateDOM(e,l,a._config));r=null===f||null===x||w!==g||r!==f.getKey()||q!==f&&(!(f instanceof y.TextNode)||q.updateDOM(f,x,a._config));if(p||r){l=a.getElementByKey(t.getNode().getKey());
|
|
19
|
+
var u=a.getElementByKey(B.getNode().getKey());if(null!==l&&null!==u&&"SPAN"===l.tagName&&"SPAN"===u.tagName){r=document.createRange();B.isBefore(t)?(p=u,x=B.offset,u=l,l=t.offset):(p=l,x=t.offset,l=B.offset);p=p.firstChild;if(null===p)throw Error("Expected text node to be first child of span");u=u.firstChild;if(null===u)throw Error("Expected text node to be first child of span");r.setStart(p,x);r.setEnd(u,l);n();n=L(a,r,z=>{for(let O of z){let v=O.style;"Highlight"!==v.background&&(v.background="Highlight");
|
|
20
|
+
"HighlightText"!==v.color&&(v.color="HighlightText");"-1"!==v.zIndex&&(v.zIndex="-1");"none"!==v.pointerEvents&&(v.pointerEvents="none");"-1.5px"!==v.marginTop&&(v.marginTop="-1.5px");"4px"!==v.paddingTop&&(v.paddingTop="4px");"0px"!==v.paddingBottom&&(v.paddingBottom="0px")}void 0!==b&&b(z)})}}e=k;d=A;f=q;g=w}else g=f=d=e=null,n(),n=()=>{}})}let e=null,d=null,f=null,g=null,n=()=>{};c(a.getEditorState());return J(a.registerUpdateListener(({editorState:m})=>c(m)),n,()=>{n()})};
|
|
21
|
+
exports.mediaFileReader=function(a,b){let c=a[Symbol.iterator]();return new Promise((e,d)=>{let f=[],g=()=>{const {done:n,value:m}=c.next();if(n)return e(f);const k=new FileReader;k.addEventListener("error",d);k.addEventListener("load",()=>{const t=k.result;"string"===typeof t&&f.push({file:m,result:t});g()});M(m,b)?k.readAsDataURL(m):g()};g()})};exports.mergeRegister=J;exports.objectKlassEquals=function(a,b){return null!==a?Object.getPrototypeOf(a).constructor.name===b.name:!1};
|
|
22
|
+
exports.positionNodeOnRange=L;
|
|
22
23
|
exports.registerNestedElementResolver=function(a,b,c,e){return a.registerNodeTransform(b,d=>{a:{var f=d.getChildren();for(var g=0;g<f.length;g++)if(f[g]instanceof b){f=null;break a}for(f=d;null!==f;)if(g=f,f=f.getParent(),f instanceof b){f={child:g,parent:f};break a}f=null}if(null!==f){const {child:n,parent:m}=f;if(n.is(d)){e(m,d);d=n.getNextSiblings();f=d.length;m.insertAfter(n);if(0!==f){g=c(m);n.insertAfter(g);for(let k=0;k<f;k++)g.append(d[k])}m.canBeEmpty()||0!==m.getChildrenSize()||m.remove()}}})};
|
|
23
|
-
exports.removeClassNamesFromElement=function(a,...b){b=
|
|
24
|
+
exports.removeClassNamesFromElement=function(a,...b){b=I(...b);0<b.length&&a.classList.remove(...b)}
|
|
@@ -0,0 +1,7 @@
|
|
|
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
|
+
import{createRectsFromDOMRange as e,$cloneWithProperties as t}from"@lexical/selection";import{$getSelection as n,$isRangeSelection as o,TextNode as r,$getRoot as l,$isElementNode as i,$isTextNode as s,$setSelection as c,$getPreviousSelection as u,$isRootOrShadowRoot as f,$splitNode as d,$createParagraphNode as a}from"lexical";export{$splitNode,isHTMLAnchorElement,isHTMLElement}from"lexical";var g=function(e){const t=new URLSearchParams;t.append("code",e);for(let e=1;e<arguments.length;e++)t.append("v",arguments[e]);throw Error(`Minified Lexical error #${e}; visit https://lexical.dev/docs/error?${t} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`)};const p="undefined"!=typeof window&&void 0!==window.document&&void 0!==window.document.createElement,h=p&&"documentMode"in document?document.documentMode:null;p&&/Mac|iPod|iPhone|iPad/.test(navigator.platform);const m=p&&/^(?!.*Seamonkey)(?=.*Firefox).*/i.test(navigator.userAgent);p&&"InputEvent"in window&&!h&&new window.InputEvent("input"),p&&/Version\/[\d.]+.*Safari/.test(navigator.userAgent),p&&/iPad|iPhone|iPod/.test(navigator.userAgent)&&window.MSStream;p&&/Android/.test(navigator.userAgent),p&&/^(?=.*Chrome).*/i.test(navigator.userAgent);function E(...e){const t=[];for(const n of e)if(n&&"string"==typeof n)for(const[e]of n.matchAll(/\S+/g))t.push(e);return t}function v(...e){return()=>{e.forEach((e=>e()))}}function w(e){return`${e}px`}p&&/AppleWebKit\/[\d.]+/.test(navigator.userAgent);const y={attributes:!0,characterData:!0,childList:!0,subtree:!0};function x(t,n,o){let r=null,l=null,i=null,s=[];const c=document.createElement("div");function u(){if(null===r)throw Error("Unexpected null rootDOMNode");if(null===l)throw Error("Unexpected null parentDOMNode");const{left:i,top:u}=r.getBoundingClientRect(),f=l,d=e(t,n);c.isConnected||f.append(c);let a=!1;for(let e=0;e<d.length;e++){const t=d[e],n=s[e]||document.createElement("div"),o=n.style;"absolute"!==o.position&&(o.position="absolute",a=!0);const r=w(t.left-i);o.left!==r&&(o.left=r,a=!0);const l=w(t.top-u);o.top!==l&&(n.style.top=l,a=!0);const f=w(t.width);o.width!==f&&(n.style.width=f,a=!0);const g=w(t.height);o.height!==g&&(n.style.height=g,a=!0),n.parentNode!==c&&(c.append(n),a=!0),s[e]=n}for(;s.length>d.length;)s.pop();a&&o(s)}function f(){l=null,r=null,null!==i&&i.disconnect(),i=null,c.remove();for(const e of s)e.remove();s=[]}const d=t.registerRootListener((function e(){const n=t.getRootElement();if(null===n)return f();const o=n.parentElement;if(!(o instanceof HTMLElement))return f();f(),r=n,l=o,i=new MutationObserver((n=>{const o=t.getRootElement(),i=o&&o.parentElement;if(o!==r||i!==l)return e();for(const e of n)if(!c.contains(e.target))return u()})),i.observe(o,y),u()}));return()=>{d(),f()}}function N(e,t){let l=null,i=null,s=null,c=null,u=()=>{};function f(f){f.read((()=>{const f=n();if(!o(f))return l=null,i=null,s=null,c=null,u(),void(u=()=>{});const{anchor:d,focus:a}=f,g=d.getNode(),p=g.getKey(),h=d.offset,m=a.getNode(),E=m.getKey(),v=a.offset,y=e.getElementByKey(p),N=e.getElementByKey(E),P=null===l||null===y||h!==i||p!==l.getKey()||g!==l&&(!(l instanceof r)||g.updateDOM(l,y,e._config)),S=null===s||null===N||v!==c||E!==s.getKey()||m!==s&&(!(s instanceof r)||m.updateDOM(s,N,e._config));if(P||S){const n=e.getElementByKey(d.getNode().getKey()),o=e.getElementByKey(a.getNode().getKey());if(null!==n&&null!==o&&"SPAN"===n.tagName&&"SPAN"===o.tagName){const r=document.createRange();let l,i,s,c;a.isBefore(d)?(l=o,i=a.offset,s=n,c=d.offset):(l=n,i=d.offset,s=o,c=a.offset);const f=l.firstChild;if(null===f)throw Error("Expected text node to be first child of span");const g=s.firstChild;if(null===g)throw Error("Expected text node to be first child of span");r.setStart(f,i),r.setEnd(g,c),u(),u=x(e,r,(e=>{for(const t of e){const e=t.style;"Highlight"!==e.background&&(e.background="Highlight"),"HighlightText"!==e.color&&(e.color="HighlightText"),"-1"!==e.zIndex&&(e.zIndex="-1"),"none"!==e.pointerEvents&&(e.pointerEvents="none"),e.marginTop!==w(-1.5)&&(e.marginTop=w(-1.5)),e.paddingTop!==w(4)&&(e.paddingTop=w(4)),e.paddingBottom!==w(0)&&(e.paddingBottom=w(0))}void 0!==t&&t(e)}))}}l=g,i=h,s=m,c=v}))}return f(e.getEditorState()),v(e.registerUpdateListener((({editorState:e})=>f(e))),u,(()=>{u()}))}function P(e,...t){const n=E(...t);n.length>0&&e.classList.add(...n)}function S(e,...t){const n=E(...t);n.length>0&&e.classList.remove(...n)}function A(e,t){for(const n of t)if(e.type.startsWith(n))return!0;return!1}function b(e,t){const n=e[Symbol.iterator]();return new Promise(((e,o)=>{const r=[],l=()=>{const{done:i,value:s}=n.next();if(i)return e(r);const c=new FileReader;c.addEventListener("error",o),c.addEventListener("load",(()=>{const e=c.result;"string"==typeof e&&r.push({file:s,result:e}),l()})),A(s,t)?c.readAsDataURL(s):l()};l()}))}function L(e,t){const n=[],o=(e||l()).getLatest(),r=t||(i(o)?o.getLastDescendant():o);let s=o,c=function(e){let t=e,n=0;for(;null!==(t=t.getParent());)n++;return n}(s);for(;null!==s&&!s.is(r);)if(n.push({depth:c,node:s}),i(s)&&s.getChildrenSize()>0)s=s.getFirstChild(),c++;else{let e=null;for(;null===e&&null!==s;)e=s.getNextSibling(),null===e?(s=s.getParent(),c--):s=e}return null!==s&&s.is(r)&&n.push({depth:c,node:s}),n}function M(e,t){let n=e;for(;null!=n;){if(n instanceof t)return n;n=n.getParent()}return null}function T(e){const t=_(e,(e=>i(e)&&!e.isInline()));return i(t)||g(4,e.__key),t}const _=(e,t)=>{let n=e;for(;n!==l()&&null!=n;){if(t(n))return n;n=n.getParent()}return null};function B(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:l}=t;if(r.is(e)){o(l,e);const t=r.getNextSiblings(),i=t.length;if(l.insertAfter(r),0!==i){const e=n(l);r.insertAfter(e);for(let n=0;n<i;n++)e.append(t[n])}l.canBeEmpty()||0!==l.getChildrenSize()||l.remove()}}}))}function C(e,n){const o=new Map,r=e._pendingEditorState;for(const[e,r]of n._nodeMap){const n=t(r);if(s(n)){if(!s(r))throw Error("Expected node be a TextNode");n.__text=r.__text}o.set(e,n)}r&&(r._nodeMap=o),e._dirtyType=2;const l=n._selection;c(null===l?null:l.clone())}function K(e){const t=n()||u();if(o(t)){const{focus:n}=t,o=n.getNode(),r=n.offset;if(f(o)){const t=o.getChildAtIndex(r);null==t?o.append(e):t.insertBefore(e),e.selectNext()}else{let t,n;s(o)?(t=o.getParentOrThrow(),n=o.getIndexWithinParent(),r>0&&(n+=1,o.splitText(r))):(t=o,n=r);const[,l]=d(t,n);l.insertBefore(e),l.selectStart()}}else{if(null!=t){const n=t.getNodes();n[n.length-1].getTopLevelElementOrThrow().insertAfter(e)}else{l().append(e)}const n=a();e.insertAfter(n),n.select()}return e.getLatest()}function O(e,t){const n=t();return e.replace(n),n.append(e),n}function R(e,t){return null!==e&&Object.getPrototypeOf(e).constructor.name===t.name}function D(e,t){const n=[];for(let o=0;o<e.length;o++){const r=t(e[o]);null!==r&&n.push(r)}return n}function H(e,t){const n=e.getFirstChild();null!==n?n.insertBefore(t):e.append(t)}function I(e){if(m)return 1;let t=1;for(;e;)t*=Number(window.getComputedStyle(e).getPropertyValue("zoom")),e=e.parentElement;return t}export{L as $dfs,D as $filter,_ as $findMatchingParent,T as $getNearestBlockElementAncestorOrThrow,M as $getNearestNodeOfType,H as $insertFirst,K as $insertNodeToNearestRoot,C as $restoreEditorState,O as $wrapNodeInElement,P as addClassNamesToElement,I as calculateZoomLevel,A as isMimeType,N as markSelection,b as mediaFileReader,v as mergeRegister,R as objectKlassEquals,x as positionNodeOnRange,B as registerNestedElementResolver,S as removeClassNamesFromElement};
|
package/index.d.ts
CHANGED
|
@@ -149,3 +149,9 @@ export declare function $filter<T>(nodes: Array<LexicalNode>, filterFn: (node: L
|
|
|
149
149
|
* @param node Node that needs to be appended
|
|
150
150
|
*/
|
|
151
151
|
export declare function $insertFirst(parent: ElementNode, node: LexicalNode): void;
|
|
152
|
+
/**
|
|
153
|
+
* Calculates the zoom level of an element as a result of using
|
|
154
|
+
* css zoom property.
|
|
155
|
+
* @param element
|
|
156
|
+
*/
|
|
157
|
+
export declare function calculateZoomLevel(element: Element | null): number;
|
package/package.json
CHANGED
|
@@ -8,21 +8,33 @@
|
|
|
8
8
|
"utils"
|
|
9
9
|
],
|
|
10
10
|
"license": "MIT",
|
|
11
|
-
"version": "0.14.
|
|
11
|
+
"version": "0.14.3",
|
|
12
12
|
"main": "LexicalUtils.js",
|
|
13
|
-
"
|
|
14
|
-
"lexical": "0.14.2"
|
|
15
|
-
},
|
|
13
|
+
"types": "index.d.ts",
|
|
16
14
|
"dependencies": {
|
|
17
|
-
"@lexical/list": "0.14.
|
|
18
|
-
"@lexical/
|
|
19
|
-
"@lexical/
|
|
15
|
+
"@lexical/list": "0.14.3",
|
|
16
|
+
"@lexical/selection": "0.14.3",
|
|
17
|
+
"@lexical/table": "0.14.3",
|
|
18
|
+
"lexical": "0.14.3"
|
|
20
19
|
},
|
|
21
20
|
"repository": {
|
|
22
21
|
"type": "git",
|
|
23
22
|
"url": "https://github.com/facebook/lexical",
|
|
24
23
|
"directory": "packages/lexical-utils"
|
|
25
24
|
},
|
|
26
|
-
"module": "LexicalUtils.
|
|
27
|
-
"sideEffects": false
|
|
25
|
+
"module": "LexicalUtils.mjs",
|
|
26
|
+
"sideEffects": false,
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"import": {
|
|
30
|
+
"types": "./index.d.ts",
|
|
31
|
+
"node": "./LexicalUtils.node.mjs",
|
|
32
|
+
"default": "./LexicalUtils.mjs"
|
|
33
|
+
},
|
|
34
|
+
"require": {
|
|
35
|
+
"types": "./index.d.ts",
|
|
36
|
+
"default": "./LexicalUtils.js"
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
28
40
|
}
|
package/LexicalUtils.prod.esm.js
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
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
|
-
import{createRectsFromDOMRange as t,$cloneWithProperties as e}from"@lexical/selection";import{$getSelection as n,$isRangeSelection as o,TextNode as r,$getRoot as l,$isElementNode as i,$isTextNode as s,$setSelection as c,$getPreviousSelection as f,$isRootOrShadowRoot as u,$splitNode as a,$createParagraphNode as d}from"lexical";export{$splitNode,isHTMLAnchorElement,isHTMLElement}from"lexical";var g=function(t){const e=new URLSearchParams;e.append("code",t);for(let t=1;t<arguments.length;t++)e.append("v",arguments[t]);throw Error(`Minified Lexical error #${t}; visit https://lexical.dev/docs/error?${e} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`)};function p(...t){const e=[];for(const n of t)if(n&&"string"==typeof n)for(const[t]of n.matchAll(/\S+/g))e.push(t);return e}function h(...t){return()=>{t.forEach((t=>t()))}}function m(t){return`${t}px`}const E={attributes:!0,characterData:!0,childList:!0,subtree:!0};function x(e,n,o){let r=null,l=null,i=null,s=[];const c=document.createElement("div");function f(){if(null===r)throw Error("Unexpected null rootDOMNode");if(null===l)throw Error("Unexpected null parentDOMNode");const{left:i,top:f}=r.getBoundingClientRect(),u=l,a=t(e,n);c.isConnected||u.append(c);let d=!1;for(let t=0;t<a.length;t++){const e=a[t],n=s[t]||document.createElement("div"),o=n.style;"absolute"!==o.position&&(o.position="absolute",d=!0);const r=m(e.left-i);o.left!==r&&(o.left=r,d=!0);const l=m(e.top-f);o.top!==l&&(n.style.top=l,d=!0);const u=m(e.width);o.width!==u&&(n.style.width=u,d=!0);const g=m(e.height);o.height!==g&&(n.style.height=g,d=!0),n.parentNode!==c&&(c.append(n),d=!0),s[t]=n}for(;s.length>a.length;)s.pop();d&&o(s)}function u(){l=null,r=null,null!==i&&i.disconnect(),i=null,c.remove();for(const t of s)t.remove();s=[]}const a=e.registerRootListener((function t(){const n=e.getRootElement();if(null===n)return u();const o=n.parentElement;if(!(o instanceof HTMLElement))return u();u(),r=n,l=o,i=new MutationObserver((n=>{const o=e.getRootElement(),i=o&&o.parentElement;if(o!==r||i!==l)return t();for(const t of n)if(!c.contains(t.target))return f()})),i.observe(o,E),f()}));return()=>{a(),u()}}function y(t,e){let l=null,i=null,s=null,c=null,f=()=>{};function u(u){u.read((()=>{const u=n();if(!o(u))return l=null,i=null,s=null,c=null,f(),void(f=()=>{});const{anchor:a,focus:d}=u,g=a.getNode(),p=g.getKey(),h=a.offset,E=d.getNode(),y=E.getKey(),v=d.offset,N=t.getElementByKey(p),w=t.getElementByKey(y),L=null===l||null===N||h!==i||p!==l.getKey()||g!==l&&(!(l instanceof r)||g.updateDOM(l,N,t._config)),T=null===s||null===w||v!==c||y!==s.getKey()||E!==s&&(!(s instanceof r)||E.updateDOM(s,w,t._config));if(L||T){const n=t.getElementByKey(a.getNode().getKey()),o=t.getElementByKey(d.getNode().getKey());if(null!==n&&null!==o&&"SPAN"===n.tagName&&"SPAN"===o.tagName){const r=document.createRange();let l,i,s,c;d.isBefore(a)?(l=o,i=d.offset,s=n,c=a.offset):(l=n,i=a.offset,s=o,c=d.offset);const u=l.firstChild;if(null===u)throw Error("Expected text node to be first child of span");const g=s.firstChild;if(null===g)throw Error("Expected text node to be first child of span");r.setStart(u,i),r.setEnd(g,c),f(),f=x(t,r,(t=>{for(const e of t){const t=e.style;"Highlight"!==t.background&&(t.background="Highlight"),"HighlightText"!==t.color&&(t.color="HighlightText"),"-1"!==t.zIndex&&(t.zIndex="-1"),"none"!==t.pointerEvents&&(t.pointerEvents="none"),t.marginTop!==m(-1.5)&&(t.marginTop=m(-1.5)),t.paddingTop!==m(4)&&(t.paddingTop=m(4)),t.paddingBottom!==m(0)&&(t.paddingBottom=m(0))}void 0!==e&&e(t)}))}}l=g,i=h,s=E,c=v}))}return u(t.getEditorState()),h(t.registerUpdateListener((({editorState:t})=>u(t))),f,(()=>{f()}))}function v(t,...e){const n=p(...e);n.length>0&&t.classList.add(...n)}function N(t,...e){const n=p(...e);n.length>0&&t.classList.remove(...n)}function w(t,e){for(const n of e)if(t.type.startsWith(n))return!0;return!1}function L(t,e){const n=t[Symbol.iterator]();return new Promise(((t,o)=>{const r=[],l=()=>{const{done:i,value:s}=n.next();if(i)return t(r);const c=new FileReader;c.addEventListener("error",o),c.addEventListener("load",(()=>{const t=c.result;"string"==typeof t&&r.push({file:s,result:t}),l()})),w(s,e)?c.readAsDataURL(s):l()};l()}))}function T(t,e){const n=[],o=(t||l()).getLatest(),r=e||(i(o)?o.getLastDescendant():o);let s=o,c=function(t){let e=t,n=0;for(;null!==(e=e.getParent());)n++;return n}(s);for(;null!==s&&!s.is(r);)if(n.push({depth:c,node:s}),i(s)&&s.getChildrenSize()>0)s=s.getFirstChild(),c++;else{let t=null;for(;null===t&&null!==s;)t=s.getNextSibling(),null===t?(s=s.getParent(),c--):s=t}return null!==s&&s.is(r)&&n.push({depth:c,node:s}),n}function b(t,e){let n=t;for(;null!=n;){if(n instanceof e)return n;n=n.getParent()}return null}function S(t){const e=_(t,(t=>i(t)&&!t.isInline()));return i(e)||g(4,t.__key),e}const _=(t,e)=>{let n=t;for(;n!==l()&&null!=n;){if(e(n))return n;n=n.getParent()}return null};function B(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:l}=e;if(r.is(t)){o(l,t);const e=r.getNextSiblings(),i=e.length;if(l.insertAfter(r),0!==i){const t=n(l);r.insertAfter(t);for(let n=0;n<i;n++)t.append(e[n])}l.canBeEmpty()||0!==l.getChildrenSize()||l.remove()}}}))}function M(t,n){const o=new Map,r=t._pendingEditorState;for(const[t,r]of n._nodeMap){const n=e(r);if(s(n)){if(!s(r))throw Error("Expected node be a TextNode");n.__text=r.__text}o.set(t,n)}r&&(r._nodeMap=o),t._dirtyType=2;const l=n._selection;c(null===l?null:l.clone())}function P(t){const e=n()||f();if(o(e)){const{focus:n}=e,o=n.getNode(),r=n.offset;if(u(o)){const e=o.getChildAtIndex(r);null==e?o.append(t):e.insertBefore(t),t.selectNext()}else{let e,n;s(o)?(e=o.getParentOrThrow(),n=o.getIndexWithinParent(),r>0&&(n+=1,o.splitText(r))):(e=o,n=r);const[,l]=a(e,n);l.insertBefore(t),l.selectStart()}}else{if(null!=e){const n=e.getNodes();n[n.length-1].getTopLevelElementOrThrow().insertAfter(t)}else{l().append(t)}const n=d();t.insertAfter(n),n.select()}return t.getLatest()}function A(t,e){const n=e();return t.replace(n),n.append(t),n}function C(t,e){return null!==t&&Object.getPrototypeOf(t).constructor.name===e.name}function K(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 O(t,e){const n=t.getFirstChild();null!==n?n.insertBefore(e):t.append(e)}export{T as $dfs,K as $filter,_ as $findMatchingParent,S as $getNearestBlockElementAncestorOrThrow,b as $getNearestNodeOfType,O as $insertFirst,P as $insertNodeToNearestRoot,M as $restoreEditorState,A as $wrapNodeInElement,v as addClassNamesToElement,w as isMimeType,y as markSelection,L as mediaFileReader,h as mergeRegister,C as objectKlassEquals,x as positionNodeOnRange,B as registerNestedElementResolver,N as removeClassNamesFromElement};
|