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

fix: character count to work properly on editor rerenders and read only mode #5554

Merged
merged 2 commits into from
Sep 9, 2024
Merged
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
2 changes: 2 additions & 0 deletions packages/editor/src/core/extensions/read-only-extensions.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import CharacterCount from "@tiptap/extension-character-count";
import TaskItem from "@tiptap/extension-task-item";
import TaskList from "@tiptap/extension-task-list";
import TextStyle from "@tiptap/extension-text-style";
Expand Down Expand Up @@ -104,4 +105,5 @@ export const CoreReadOnlyEditorExtensions = (mentionConfig: {
mentionHighlights: mentionConfig.mentionHighlights,
readonly: true,
}),
CharacterCount,
];
10 changes: 6 additions & 4 deletions packages/editor/src/core/hooks/use-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,12 @@ export const useEditor = (props: CustomEditorProps) => {
editor.chain().focus().deleteRange({ from, to }).insertContent(contentHTML).run();
}
},
documentInfo: {
characters: editorRef.current?.storage?.characterCount?.characters?.() ?? 0,
paragraphs: getParagraphCount(editorRef.current?.state),
words: editorRef.current?.storage?.characterCount?.words?.() ?? 0,
getDocumentInfo: () => {
return {
characters: editorRef?.current?.storage?.characterCount?.characters?.() ?? 0,
paragraphs: getParagraphCount(editorRef?.current?.state),
words: editorRef?.current?.storage?.characterCount?.words?.() ?? 0,
};
},
}),
[editorRef, savedSelection, fileHandler.upload]
Expand Down
10 changes: 6 additions & 4 deletions packages/editor/src/core/hooks/use-read-only-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,12 @@ export const useReadOnlyEditor = ({
if (!editorRef.current) return;
scrollSummary(editorRef.current, marking);
},
documentInfo: {
characters: editorRef.current?.storage?.characterCount?.characters?.() ?? 0,
paragraphs: getParagraphCount(editorRef.current?.state),
words: editorRef.current?.storage?.characterCount?.words?.() ?? 0,
getDocumentInfo: () => {
return {
characters: editorRef?.current?.storage?.characterCount?.characters?.() ?? 0,
paragraphs: getParagraphCount(editorRef?.current?.state),
words: editorRef?.current?.storage?.characterCount?.words?.() ?? 0,
};
},
}));

Expand Down
2 changes: 1 addition & 1 deletion packages/editor/src/core/types/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export type EditorReadOnlyRefApi = {
clearEditor: (emitUpdate?: boolean) => void;
setEditorValue: (content: string) => void;
scrollSummary: (marking: IMarking) => void;
documentInfo: {
getDocumentInfo: () => {
characters: number;
paragraphs: number;
words: number;
Expand Down
10 changes: 6 additions & 4 deletions web/core/components/pages/editor/header/info-popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ export const PageInfoPopover: React.FC<Props> = (props) => {
placement: "bottom-start",
});

const documentsInfo = editorRef?.getDocumentInfo() || { words: 0, characters: 0, paragraphs: 0 };

const secondsToReadableTime = () => {
const wordsCount = editorRef?.documentInfo.words || 0;
const wordsCount = documentsInfo.words;
const readTimeInSeconds = Number(getReadTimeFromWordsCount(wordsCount).toFixed(0));
return readTimeInSeconds < 60 ? `${readTimeInSeconds}s` : `${Math.ceil(readTimeInSeconds / 60)}m`;
};
Expand All @@ -32,17 +34,17 @@ export const PageInfoPopover: React.FC<Props> = (props) => {
{
key: "words-count",
title: "Words",
info: editorRef?.documentInfo.words,
info: documentsInfo.words,
},
{
key: "characters-count",
title: "Characters",
info: editorRef?.documentInfo.characters,
info: documentsInfo.characters,
},
{
key: "paragraphs-count",
title: "Paragraphs",
info: editorRef?.documentInfo.paragraphs,
info: documentsInfo.paragraphs,
},
{
key: "read-time",
Expand Down
Loading