lexical
lexical copied to clipboard
Make target node type to generic on HTMLConfig
Currently the type of target is LexicalNode.
So it's not possible to use the certain methods of target node instance in callback function.
For example:
html: {
export: new Map([
[
TextNode,
(_, target) => {
if (target.hasFormat('bold')) {
return {element: document.createElement('bar')};
}
return {element: document.createElement('foo')};
},
],
]),
},
On current implementation, trying to use hasFormat throws a Type error: Property 'hasFormat' does not exist on type 'LexicalNode'.
With this change utilizing generic, it would be like this and no TS error:
html: {
export: new Map([
[
TextNode,
(_, target: TextNode) => {
if (target.hasFormat('bold')) {
return {element: document.createElement('bar')};
}
return {element: document.createElement('foo')};
},
],
]),
},
referenced LexicalNodeReplacement:
https://github.com/facebook/lexical/blob/17a2e3bb44d36321d2ea04867c1d3a11db032a72/packages/lexical/src/LexicalEditor.ts#L161-L164
The latest updates on your projects. Learn more about Vercel for Git ↗︎
| Name | Status | Preview | Comments | Updated (UTC) |
|---|---|---|---|---|
| lexical | ✅ Ready (Inspect) | Visit Preview | 💬 Add feedback | Aug 25, 2024 1:55pm |
| lexical-playground | ✅ Ready (Inspect) | Visit Preview | 💬 Add feedback | Aug 25, 2024 1:55pm |
size-limit report 📦
| Path | Size |
|---|---|
| lexical - cjs | 29.38 KB (0%) |
| lexical - esm | 29.24 KB (0%) |
| @lexical/rich-text - cjs | 37.89 KB (0%) |
| @lexical/rich-text - esm | 31.07 KB (0%) |
| @lexical/plain-text - cjs | 36.49 KB (0%) |
| @lexical/plain-text - esm | 28.41 KB (0%) |
| @lexical/react - cjs | 39.61 KB (0%) |
| @lexical/react - esm | 32.52 KB (0%) |
I don't think it would be possible to express something like that directly with Map's type
@etrepum You are right. I reverted that and changed this PR to focus on adding unit tests for HTMLConfig.