Skip to content
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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

h1un
Copy link

@h1un h1un commented Jan 21, 2025

Description

I have implemented a new useCharacterCount hook inspired by the existing useCharacterLimit functionality. This hook helps track the number of characters in real-time within text input fields or rich text editors.

Closes #7063

Copy link

vercel bot commented Jan 21, 2025

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 Jan 22, 2025 1:31am
lexical-playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 22, 2025 1:31am

@facebook-github-bot
Copy link
Contributor

Hi @h1un!

Thank you for your pull request and welcome to our community.

Action Required

In 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.

Process

In 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 CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@h1un h1un changed the title []Feature : CharacterCountPlugin [lexical-react]Feature : CharacterCountPlugin Jan 21, 2025
Copy link

github-actions bot commented Jan 21, 2025

size-limit report 📦

Path Size
lexical - cjs 29.07 KB (0%)
lexical - esm 28.86 KB (0%)
@lexical/rich-text - cjs 38.04 KB (0%)
@lexical/rich-text - esm 30.92 KB (0%)
@lexical/plain-text - cjs 36.55 KB (0%)
@lexical/plain-text - esm 28.22 KB (0%)
@lexical/react - cjs 39.85 KB (0%)
@lexical/react - esm 32.28 KB (+0.06% 🔺)

Comment on lines 25 to 33
useEffect(() => {
// Check if OverflowNode is registered
if (!editor.hasNodes([OverflowNode])) {
invariant(
false,
'useCharacterCount: OverflowNode not registered on editor',
);
}
}, [editor]);
Copy link
Collaborator

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

Comment on lines 24 to 34
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');
}
},
});
Copy link
Collaborator

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>;
Copy link
Collaborator

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.

Copy link
Collaborator

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.

@h1un
Copy link
Author

h1un commented Jan 22, 2025

@etrepum
I received your feedback and modified the code. Thank you.

@etrepum
Copy link
Collaborator

etrepum commented Jan 22, 2025

I’ll take a look after you sign the CLA. This project can not accept PRs without a signed CLA.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jan 22, 2025
@facebook-github-bot
Copy link
Contributor

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

Copy link
Collaborator

@etrepum etrepum left a 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>;
Copy link
Collaborator

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;
Copy link
Collaborator

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 = {
Copy link
Collaborator

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(
Copy link
Collaborator

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({
Copy link
Collaborator

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'} />
Copy link
Collaborator

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

Suggested change
<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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.characters-count {
.character-count {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature: Request for Adding useCharacterCount Hook
3 participants