[lexical-react]Feature : CharacterCountPlugin#7075
[lexical-react]Feature : CharacterCountPlugin#7075h1un wants to merge 2 commits intofacebook:mainfrom
Conversation
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
|
Hi @h1un! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
size-limit report 📦
|
| useEffect(() => { | ||
| // Check if OverflowNode is registered | ||
| if (!editor.hasNodes([OverflowNode])) { | ||
| invariant( | ||
| false, | ||
| 'useCharacterCount: OverflowNode not registered on editor', | ||
| ); | ||
| } | ||
| }, [editor]); |
There was a problem hiding this comment.
Counting characters doesn't need OverflowNode
| const characterCount = useCharacterCount(editor, { | ||
| strlen: (text: string) => { | ||
| if (charset === 'UTF-8') { | ||
| return utf8Length(text); | ||
| } else if (charset === 'UTF-16') { | ||
| return text.length; | ||
| } else { | ||
| throw new Error('Unrecognized charset'); | ||
| } | ||
| }, | ||
| }); |
There was a problem hiding this comment.
This will cause the text content listener to be unregistered every time the editor changes because strlen changes causing the effect to re-fire every time. The options object or at least strlen should be computed with useMemo with this design.
| }, | ||
| }); | ||
|
|
||
| return <span className="characters-count">{characterCount}</span>; |
There was a problem hiding this comment.
It would be better to return <>{characterCount}</> so the tag and class are entirely up to the user, lexical generally doesn't have any hard-coded class names.
There was a problem hiding this comment.
The only code in lexical that hard-codes class names is the playground, which is not published as a reusable module like this. Otherwise classes come from the editor theme (which is generally for nodes, not plug-ins). I think this should just be headless, where if you don't specify a render function it returns only the text.
|
@etrepum |
|
I’ll take a look after you sign the CLA. This project can not accept PRs without a signed CLA. |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
etrepum
left a comment
There was a problem hiding this comment.
Looks very close, mostly just needs API documentation
| }, | ||
| }); | ||
|
|
||
| return <span className="characters-count">{characterCount}</span>; |
There was a problem hiding this comment.
The only code in lexical that hard-codes class names is the playground, which is not published as a reusable module like this. Otherwise classes come from the editor theme (which is generally for nodes, not plug-ins). I think this should just be headless, where if you don't specify a render function it returns only the text.
| editor: LexicalEditor, | ||
| optional: OptionalProps = Object.freeze({}), | ||
| ): number { | ||
| const {strlen = (input) => input.length} = optional; |
There was a problem hiding this comment.
This has the same useMemo type problem, if this default is used a new function will be created each time the hook is called. If you extract this to a separate function it won't have that problem.
|
|
||
| import {useEffect, useState} from 'react'; | ||
|
|
||
| type OptionalProps = { |
There was a problem hiding this comment.
It would be easier to use and produce better API documentation if this was exported and had a more descriptive name like UseCharacterCountOptions
| strlen?: (input: string) => number; | ||
| }; | ||
|
|
||
| export function useCharacterCount( |
There was a problem hiding this comment.
This should have a /** API documentation */
| return textEncoder.encode(text).length; | ||
| } | ||
|
|
||
| export function CharacterCountPlugin({ |
There was a problem hiding this comment.
This should have a /** API documentation */
| /> | ||
| )} | ||
| {(isCharCount || isCharCountUtf8) && ( | ||
| <CharacterCountPlugin charset={isCharCount ? 'UTF-16' : 'UTF-8'} /> |
There was a problem hiding this comment.
This is what it would look like when the output of the plugin is headless
| <CharacterCountPlugin charset={isCharCount ? 'UTF-16' : 'UTF-8'} /> | |
| <span className="character-count"> | |
| <CharacterCountPlugin charset={isCharCount ? 'UTF-16' : 'UTF-8'} /> | |
| </span> |
| color: red; | ||
| } | ||
|
|
||
| .characters-count { |
There was a problem hiding this comment.
| .characters-count { | |
| .character-count { |
|
The feedback on this PR hasn't been addressed in a few months so it will be closed as stale. You may reopen it when/if you are able to finish it up. |
Description
I have implemented a new
useCharacterCounthook inspired by the existinguseCharacterLimitfunctionality. This hook helps track the number of characters in real-time within text input fields or rich text editors.Closes #7063