-
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -734,6 +734,16 @@ i.page-break, | |||||
color: red; | ||||||
} | ||||||
|
||||||
.characters-count { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
color: #888; | ||||||
font-size: 12px; | ||||||
text-align: right; | ||||||
display: block; | ||||||
position: absolute; | ||||||
left: 55px; | ||||||
bottom: 5px; | ||||||
} | ||||||
|
||||||
.dropdown { | ||||||
z-index: 100; | ||||||
display: block; | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext'; | ||
import * as React from 'react'; | ||
|
||
import {useCharacterCount} from './shared/useCharacterCount'; | ||
|
||
function utf8Length(text: string) { | ||
const textEncoder = new TextEncoder(); | ||
return textEncoder.encode(text).length; | ||
} | ||
|
||
export function CharacterCountPlugin({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should have a |
||
charset = 'UTF-16', | ||
render = DefaultRenderer, | ||
}: { | ||
charset?: 'UTF-16' | 'UTF-8'; | ||
render?: (characterCount: number) => JSX.Element; | ||
}): JSX.Element { | ||
const [editor] = useLexicalComposerContext(); | ||
|
||
const strlen = React.useMemo(() => { | ||
return (text: string) => { | ||
if (charset === 'UTF-8') { | ||
return utf8Length(text); | ||
} else if (charset === 'UTF-16') { | ||
return text.length; | ||
} else { | ||
throw new Error('Unrecognized charset'); | ||
} | ||
}; | ||
}, [charset]); | ||
|
||
const characterCount = useCharacterCount(editor, {strlen}); | ||
|
||
return render(characterCount); | ||
} | ||
|
||
function DefaultRenderer(characterCount: number) { | ||
return <span className="characters-count">{characterCount}</span>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be better to return There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
import type {LexicalEditor} from 'lexical'; | ||
|
||
import {useEffect, useState} from 'react'; | ||
|
||
type OptionalProps = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. This should have a |
||
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 commentThe 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. |
||
const [characterCount, setCharacterCount] = useState(0); | ||
|
||
useEffect(() => { | ||
const unregisterTextContentListener = editor.registerTextContentListener( | ||
(currentText: string) => { | ||
setCharacterCount(strlen(currentText)); | ||
}, | ||
); | ||
|
||
return () => { | ||
unregisterTextContentListener(); | ||
}; | ||
}, [editor, strlen]); | ||
|
||
return characterCount; | ||
} |
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