-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[lexical-react]Feature : CharacterCountPlugin #7075
base: main
Are you sure you want to change the base?
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 [email protected]. 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks very close, mostly just needs API documentation
}, | ||
}); | ||
|
||
return <span className="characters-count">{characterCount}</span>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should have a /** API documentation */
return textEncoder.encode(text).length; | ||
} | ||
|
||
export function CharacterCountPlugin({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should have a /** API documentation */
@@ -257,6 +260,9 @@ export default function Editor(): JSX.Element { | |||
maxLength={5} | |||
/> | |||
)} | |||
{(isCharCount || isCharCountUtf8) && ( | |||
<CharacterCountPlugin charset={isCharCount ? 'UTF-16' : 'UTF-8'} /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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> |
@@ -734,6 +734,16 @@ i.page-break, | |||
color: red; | |||
} | |||
|
|||
.characters-count { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.characters-count { | |
.character-count { |
Description
I have implemented a new
useCharacterCount
hook inspired by the existinguseCharacterLimit
functionality. This hook helps track the number of characters in real-time within text input fields or rich text editors.Closes #7063