lexical icon indicating copy to clipboard operation
lexical copied to clipboard

Make target node type to generic on HTMLConfig

Open 2wheeh opened this issue 1 year ago • 2 comments

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

2wheeh avatar Jan 18 '24 08:01 2wheeh

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

vercel[bot] avatar Jan 18 '24 08:01 vercel[bot]

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%)

github-actions[bot] avatar May 22 '24 08:05 github-actions[bot]

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.

2wheeh avatar Aug 25 '24 07:08 2wheeh