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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/lexical-playground/src/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

import {AutoFocusPlugin} from '@lexical/react/LexicalAutoFocusPlugin';
import {CharacterCountPlugin} from '@lexical/react/LexicalCharacterCountPlugin';
import {CharacterLimitPlugin} from '@lexical/react/LexicalCharacterLimitPlugin';
import {CheckListPlugin} from '@lexical/react/LexicalCheckListPlugin';
import {ClearEditorPlugin} from '@lexical/react/LexicalClearEditorPlugin';
Expand Down Expand Up @@ -85,8 +86,10 @@ export default function Editor(): JSX.Element {
isCollab,
isAutocomplete,
isMaxLength,
isCharCount,
isCharLimit,
hasLinkAttributes,
isCharCountUtf8,
isCharLimitUtf8,
isRichText,
showTreeView,
Expand Down Expand Up @@ -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>

)}
{isAutocomplete && <AutocompletePlugin />}
<div>{showTableOfContents && <TableOfContentsPlugin />}</div>
{shouldUseLexicalContextMenu && <ContextMenuPlugin />}
Expand Down
2 changes: 2 additions & 0 deletions packages/lexical-playground/src/appSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export const DEFAULT_SETTINGS = {
emptyEditor: isDevPlayground,
hasLinkAttributes: false,
isAutocomplete: false,
isCharCount: false,
isCharCountUtf8: false,
isCharLimit: false,
isCharLimitUtf8: false,
isCollab: false,
Expand Down
10 changes: 10 additions & 0 deletions packages/lexical-playground/src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

color: #888;
font-size: 12px;
text-align: right;
display: block;
position: absolute;
left: 55px;
bottom: 5px;
}

.dropdown {
z-index: 100;
display: block;
Expand Down
30 changes: 30 additions & 0 deletions packages/lexical-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,36 @@
"default": "./LexicalBlockWithAlignableContents.js"
}
},
"./LexicalCharacterCountPlugin": {
"import": {
"types": "./LexicalCharacterCountPlugin.d.ts",
"development": "./LexicalCharacterCountPlugin.dev.mjs",
"production": "./LexicalCharacterCountPlugin.prod.mjs",
"node": "./LexicalCharacterCountPlugin.node.mjs",
"default": "./LexicalCharacterCountPlugin.mjs"
},
"require": {
"types": "./LexicalCharacterCountPlugin.d.ts",
"development": "./LexicalCharacterCountPlugin.dev.js",
"production": "./LexicalCharacterCountPlugin.prod.js",
"default": "./LexicalCharacterCountPlugin.js"
}
},
"./LexicalCharacterCountPlugin.js": {
"import": {
"types": "./LexicalCharacterCountPlugin.d.ts",
"development": "./LexicalCharacterCountPlugin.dev.mjs",
"production": "./LexicalCharacterCountPlugin.prod.mjs",
"node": "./LexicalCharacterCountPlugin.node.mjs",
"default": "./LexicalCharacterCountPlugin.mjs"
},
"require": {
"types": "./LexicalCharacterCountPlugin.d.ts",
"development": "./LexicalCharacterCountPlugin.dev.js",
"production": "./LexicalCharacterCountPlugin.prod.js",
"default": "./LexicalCharacterCountPlugin.js"
}
},
"./LexicalCharacterLimitPlugin": {
"import": {
"types": "./LexicalCharacterLimitPlugin.d.ts",
Expand Down
46 changes: 46 additions & 0 deletions packages/lexical-react/src/LexicalCharacterCountPlugin.tsx
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({
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 */

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

}
36 changes: 36 additions & 0 deletions packages/lexical-react/src/shared/useCharacterCount.ts
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 = {
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 */

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.

const [characterCount, setCharacterCount] = useState(0);

useEffect(() => {
const unregisterTextContentListener = editor.registerTextContentListener(
(currentText: string) => {
setCharacterCount(strlen(currentText));
},
);

return () => {
unregisterTextContentListener();
};
}, [editor, strlen]);

return characterCount;
}
3 changes: 3 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
"@lexical/react/LexicalBlockWithAlignableContents": [
"./packages/lexical-react/src/LexicalBlockWithAlignableContents.tsx"
],
"@lexical/react/LexicalCharacterCountPlugin": [
"packages/lexical-react/src/LexicalCharacterCountPlugin.tsx"
],
"@lexical/react/LexicalCharacterLimitPlugin": [
"./packages/lexical-react/src/LexicalCharacterLimitPlugin.tsx"
],
Expand Down
Loading