@lexical/utils
Advanced tools
Comparing version 0.2.9 to 0.3.0
@@ -8,5 +8,12 @@ /** | ||
*/ | ||
import {Class} from 'utility-types'; | ||
import type {LexicalNode, ElementNode, LexicalEditor} from 'lexical'; | ||
import type { | ||
EditorState, | ||
LexicalNode, | ||
ElementNode, | ||
LexicalEditor, | ||
} from 'lexical'; | ||
export type DFSNode = Readonly<{ | ||
@@ -16,2 +23,3 @@ depth: number; | ||
}>; | ||
declare function addClassNamesToElement( | ||
@@ -34,3 +42,5 @@ element: HTMLElement, | ||
): T | null; | ||
export type DOMNodeToLexicalConversion = (element: Node) => LexicalNode; | ||
export type DOMNodeToLexicalConversionMap = Record< | ||
@@ -49,3 +59,2 @@ string, | ||
): ElementNode; | ||
declare function registerNestedElementResolver<N>( | ||
@@ -57,1 +66,11 @@ editor: LexicalEditor, | ||
): () => void; | ||
export declare function unstable_convertLegacyJSONEditorState( | ||
editor: LexicalEditor, | ||
maybeStringifiedEditorState: string, | ||
): EditorState; | ||
export declare function $restoreEditorState( | ||
editor: LexicalEditor, | ||
editorState: EditorState, | ||
): void; |
@@ -17,3 +17,2 @@ /** | ||
* | ||
* | ||
*/ | ||
@@ -198,4 +197,127 @@ function addClassNamesToElement(element, ...classNames) { | ||
return editor.registerNodeTransform(targetNode, elementNodeTransform); | ||
} // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
function unstable_internalCreateNodeFromParse(parsedNode, parsedNodeMap, editor, parentKey, activeEditorState) { | ||
const nodeType = parsedNode.__type; | ||
const registeredNode = editor._nodes.get(nodeType); | ||
if (registeredNode === undefined) { | ||
{ | ||
throw Error(`createNodeFromParse: type "${nodeType}" + not found`); | ||
} | ||
} // Check for properties that are editors | ||
for (const property in parsedNode) { | ||
const value = parsedNode[property]; | ||
if (value != null && typeof value === 'object') { | ||
const parsedEditorState = value.editorState; | ||
if (parsedEditorState != null) { | ||
const nestedEditor = lexical.createEditor(); | ||
nestedEditor._nodes = editor._nodes; | ||
nestedEditor._parentEditor = editor._parentEditor; | ||
nestedEditor._pendingEditorState = unstable_convertLegacyJSONEditorState(nestedEditor, parsedEditorState); | ||
parsedNode[property] = nestedEditor; | ||
} | ||
} | ||
} | ||
const NodeKlass = registeredNode.klass; | ||
const parsedKey = parsedNode.__key; // We set the parsedKey to undefined before calling clone() so that | ||
// we get a new random key assigned. | ||
parsedNode.__key = undefined; // @ts-expect-error TODO Replace Class utility type with InstanceType | ||
const node = NodeKlass.clone(parsedNode); | ||
parsedNode.__key = parsedKey; | ||
const key = node.__key; | ||
activeEditorState._nodeMap.set(key, node); | ||
node.__parent = parentKey; // We will need to recursively handle the children in the case | ||
// of a ElementNode. | ||
if (lexical.$isElementNode(node)) { | ||
const children = parsedNode.__children; | ||
for (let i = 0; i < children.length; i++) { | ||
const childKey = children[i]; | ||
const parsedChild = parsedNodeMap.get(childKey); | ||
if (parsedChild !== undefined) { | ||
const child = unstable_internalCreateNodeFromParse(parsedChild, parsedNodeMap, editor, key, activeEditorState); | ||
const newChildKey = child.__key; | ||
node.__children.push(newChildKey); | ||
} | ||
} | ||
node.__indent = parsedNode.__indent; | ||
node.__format = parsedNode.__format; | ||
node.__dir = parsedNode.__dir; | ||
} else if (lexical.$isTextNode(node)) { | ||
node.__format = parsedNode.__format; | ||
node.__style = parsedNode.__style; | ||
node.__mode = parsedNode.__mode; | ||
node.__detail = parsedNode.__detail; | ||
} | ||
return node; | ||
} | ||
function unstable_parseEditorState(parsedEditorState, editor) { | ||
// This is hacky, do not do this! | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const EditorStateClass = editor._editorState.constructor; | ||
const nodeMap = new Map(); | ||
const editorState = new EditorStateClass(nodeMap); | ||
const parsedNodeMap = new Map(parsedEditorState._nodeMap); // root always exists in Map | ||
const parsedRoot = parsedNodeMap.get('root'); | ||
const isUpdating = editor._updating; | ||
try { | ||
editor._updating = false; | ||
editor.update(() => { | ||
const dirtyElements = editor._dirtyElements; | ||
const dirtyLeaves = editor._dirtyLeaves; | ||
const dirtyType = editor._dirtyType; | ||
editor._dirtyElements = new Map(); | ||
editor._dirtyLeaves = new Set(); | ||
editor._dirtyType = 0; | ||
try { | ||
unstable_internalCreateNodeFromParse(parsedRoot, parsedNodeMap, editor, null, editorState); | ||
} finally { | ||
editor._dirtyElements = dirtyElements; | ||
editor._dirtyLeaves = dirtyLeaves; | ||
editor._dirtyType = dirtyType; | ||
} | ||
}); | ||
} finally { | ||
editor._updating = isUpdating; | ||
} | ||
editorState._readOnly = true; | ||
return editorState; | ||
} // TODO: remove this function in version 0.4 | ||
function unstable_convertLegacyJSONEditorState(editor, maybeStringifiedEditorState) { | ||
const parsedEditorState = typeof maybeStringifiedEditorState === 'string' ? JSON.parse(maybeStringifiedEditorState) : maybeStringifiedEditorState; | ||
return unstable_parseEditorState(parsedEditorState, editor); | ||
} | ||
function $restoreEditorState(editor, editorState) { | ||
const FULL_RECONCILE = 2; | ||
const nodeMap = new Map(editorState._nodeMap); | ||
const activeEditorState = editor._pendingEditorState; | ||
activeEditorState._nodeMap = nodeMap; | ||
editor._dirtyType = FULL_RECONCILE; | ||
const selection = editorState._selection; | ||
lexical.$setSelection(selection === null ? null : selection.clone()); | ||
} | ||
exports.$dfs = $dfs; | ||
@@ -205,2 +327,3 @@ exports.$findMatchingParent = $findMatchingParent; | ||
exports.$getNearestNodeOfType = $getNearestNodeOfType; | ||
exports.$restoreEditorState = $restoreEditorState; | ||
exports.addClassNamesToElement = addClassNamesToElement; | ||
@@ -210,1 +333,2 @@ exports.mergeRegister = mergeRegister; | ||
exports.removeClassNamesFromElement = removeClassNamesFromElement; | ||
exports.unstable_convertLegacyJSONEditorState = unstable_convertLegacyJSONEditorState; |
@@ -7,7 +7,10 @@ /** | ||
*/ | ||
var h=require("lexical");function n(a,b){for(;a!==h.$getRoot()&&null!=a;){if(b(a))return a;a=a.getParent()}return null} | ||
exports.$dfs=function(a,b){const e=[];a=(a||h.$getRoot()).getLatest();b=b||(h.$isElementNode(a)?a.getLastDescendant():a);for(var f=a,d=0;null!==(f=f.getParent());)d++;for(f=d;null!==a&&!a.is(b);)if(e.push({depth:f,node:a}),h.$isElementNode(a)&&0<a.getChildrenSize())a=a.getFirstChild(),f++;else for(d=null;null===d&&null!==a;)d=a.getNextSibling(),null===d?(a=a.getParent(),f--):a=d;null!==a&&a.is(b)&&e.push({depth:f,node:a});return e};exports.$findMatchingParent=n; | ||
exports.$getNearestBlockElementAncestorOrThrow=function(a){a=n(a,b=>h.$isElementNode(b)&&!b.isInline());if(!h.$isElementNode(a))throw Error("Minified Lexical error #3; see codes.json for the full message or use the non-minified dev environment for full errors and additional helpful warnings.");return a};exports.$getNearestNodeOfType=function(a,b){for(;null!=a&&!(a instanceof b);)a=a.getParent();return a};exports.addClassNamesToElement=function(a,...b){b.forEach(e=>{"string"===typeof e&&a.classList.add(...e.split(" "))})}; | ||
exports.mergeRegister=function(...a){return()=>{a.forEach(b=>b())}}; | ||
exports.registerNestedElementResolver=function(a,b,e,f){return a.registerNodeTransform(b,d=>{a:{var c=d.getChildren();for(var g=0;g<c.length;g++)if(c[g]instanceof b){c=null;break a}for(c=d;null!==c;)if(g=c,c=c.getParent(),c instanceof b){c={child:g,parent:c};break a}c=null}if(null!==c){const {child:l,parent:k}=c;if(l.is(d)){f(k,d);d=l.getNextSiblings();c=d.length;k.insertAfter(l);if(0!==c){g=e(k);l.insertAfter(g);for(let m=0;m<c;m++)g.append(d[m])}k.canBeEmpty()||0!==k.getChildrenSize()||k.remove()}}})}; | ||
exports.removeClassNamesFromElement=function(a,...b){b.forEach(e=>{"string"===typeof e&&a.classList.remove(...e.split(" "))})}; | ||
'use strict';var l=require("lexical");function n(a){throw Error(`Minified Lexical error #${a}; see codes.json for the full message or `+"use the non-minified dev environment for full errors and additional helpful warnings.");}function p(a,b){for(;a!==l.$getRoot()&&null!=a;){if(b(a))return a;a=a.getParent()}return null} | ||
function q(a,b,e,h,f){var c=a.__type,d=e._nodes.get(c);void 0===d&&n(5,c);for(var k in a)if(c=a[k],null!=c&&"object"===typeof c&&(c=c.editorState,null!=c)){var g=l.createEditor();g._nodes=e._nodes;g._parentEditor=e._parentEditor;g._pendingEditorState=r(g,c);a[k]=g}d=d.klass;k=a.__key;a.__key=void 0;d=d.clone(a);a.__key=k;k=d.__key;f._nodeMap.set(k,d);d.__parent=h;if(l.$isElementNode(d)){h=a.__children;for(c=0;c<h.length;c++)g=b.get(h[c]),void 0!==g&&(g=q(g,b,e,k,f).__key,d.__children.push(g));d.__indent= | ||
a.__indent;d.__format=a.__format;d.__dir=a.__dir}else l.$isTextNode(d)&&(d.__format=a.__format,d.__style=a.__style,d.__mode=a.__mode,d.__detail=a.__detail);return d} | ||
function t(a,b){let e=b._editorState.constructor,h=new Map,f=new e(h),c=new Map(a._nodeMap),d=c.get("root");a=b._updating;try{b._updating=!1,b.update(()=>{let k=b._dirtyElements,g=b._dirtyLeaves,m=b._dirtyType;b._dirtyElements=new Map;b._dirtyLeaves=new Set;b._dirtyType=0;try{q(d,c,b,null,f)}finally{b._dirtyElements=k,b._dirtyLeaves=g,b._dirtyType=m}})}finally{b._updating=a}f._readOnly=!0;return f}function r(a,b){b="string"===typeof b?JSON.parse(b):b;return t(b,a)} | ||
exports.$dfs=function(a,b){let e=[];a=(a||l.$getRoot()).getLatest();b=b||(l.$isElementNode(a)?a.getLastDescendant():a);for(var h=a,f=0;null!==(h=h.getParent());)f++;for(h=f;null!==a&&!a.is(b);)if(e.push({depth:h,node:a}),l.$isElementNode(a)&&0<a.getChildrenSize())a=a.getFirstChild(),h++;else for(f=null;null===f&&null!==a;)f=a.getNextSibling(),null===f?(a=a.getParent(),h--):a=f;null!==a&&a.is(b)&&e.push({depth:h,node:a});return e};exports.$findMatchingParent=p; | ||
exports.$getNearestBlockElementAncestorOrThrow=function(a){let b=p(a,e=>l.$isElementNode(e)&&!e.isInline());l.$isElementNode(b)||n(4,a.__key);return b};exports.$getNearestNodeOfType=function(a,b){for(;null!=a&&!(a instanceof b);)a=a.getParent();return a};exports.$restoreEditorState=function(a,b){let e=new Map(b._nodeMap);a._pendingEditorState._nodeMap=e;a._dirtyType=2;a=b._selection;l.$setSelection(null===a?null:a.clone())}; | ||
exports.addClassNamesToElement=function(a,...b){b.forEach(e=>{"string"===typeof e&&a.classList.add(...e.split(" "))})};exports.mergeRegister=function(...a){return()=>{a.forEach(b=>b())}}; | ||
exports.registerNestedElementResolver=function(a,b,e,h){return a.registerNodeTransform(b,f=>{a:{var c=f.getChildren();for(var d=0;d<c.length;d++)if(c[d]instanceof b){c=null;break a}for(c=f;null!==c;)if(d=c,c=c.getParent(),c instanceof b){c={child:d,parent:c};break a}c=null}if(null!==c){const {child:k,parent:g}=c;if(k.is(f)){h(g,f);f=k.getNextSiblings();c=f.length;g.insertAfter(k);if(0!==c){d=e(g);k.insertAfter(d);for(let m=0;m<c;m++)d.append(f[m])}g.canBeEmpty()||0!==g.getChildrenSize()||g.remove()}}})}; | ||
exports.removeClassNamesFromElement=function(a,...b){b.forEach(e=>{"string"===typeof e&&a.classList.remove(...e.split(" "))})};exports.unstable_convertLegacyJSONEditorState=r |
@@ -11,10 +11,10 @@ { | ||
"license": "MIT", | ||
"version": "0.2.9", | ||
"version": "0.3.0", | ||
"main": "LexicalUtils.js", | ||
"peerDependencies": { | ||
"lexical": "0.2.9" | ||
"lexical": "0.3.0" | ||
}, | ||
"dependencies": { | ||
"@lexical/list": "0.2.9", | ||
"@lexical/table": "0.2.9" | ||
"@lexical/list": "0.3.0", | ||
"@lexical/table": "0.3.0" | ||
}, | ||
@@ -21,0 +21,0 @@ "repository": { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
18729
362
0
+ Added@lexical/list@0.3.0(transitive)
+ Added@lexical/table@0.3.0(transitive)
+ Addedlexical@0.3.0(transitive)
- Removed@lexical/list@0.2.9(transitive)
- Removed@lexical/table@0.2.9(transitive)
- Removedlexical@0.2.9(transitive)
Updated@lexical/list@0.3.0
Updated@lexical/table@0.3.0