From 8e6bbb9227cefb904e31ed39df0ea2cc1eebe590 Mon Sep 17 00:00:00 2001 From: Muhammad Ali Date: Tue, 10 Jan 2023 16:06:20 +0500 Subject: [PATCH] web: do not ignore line breaks when counting words (#1625) Signed-off-by: Abdullah Atta Co-authored-by: Abdullah Atta --- apps/web/__e2e__/editor.test.ts | 10 ++++++++++ apps/web/__e2e__/models/editor.model.ts | 8 ++++++++ apps/web/src/components/editor/tiptap.tsx | 5 +++-- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/apps/web/__e2e__/editor.test.ts b/apps/web/__e2e__/editor.test.ts index 7c585a204..420cd03b3 100644 --- a/apps/web/__e2e__/editor.test.ts +++ b/apps/web/__e2e__/editor.test.ts @@ -276,3 +276,13 @@ test("creating a new note and toggling read-only mode should not empty editor co expect(await notes.editor.getContent("text")).not.toHaveLength(0); expect(await notes.editor.getContent("text")).toBe(NOTE.content); }); + +test("count number of words in an opened note", async ({ page }) => { + const app = new AppModel(page); + await app.goto(); + const notes = await app.goToNotes(); + NOTE.content = "1\n2\n3\na\nb\nc\nd\ne\nali\nwaqar"; //10 + await notes.createNote(NOTE); + + expect((await notes.editor.getWordCount()) === 10).toBeTruthy(); +}); diff --git a/apps/web/__e2e__/models/editor.model.ts b/apps/web/__e2e__/models/editor.model.ts index 25eecac79..c10ee4151 100644 --- a/apps/web/__e2e__/models/editor.model.ts +++ b/apps/web/__e2e__/models/editor.model.ts @@ -248,4 +248,12 @@ export class EditorModel { await this.previewRestoreButton.click(); await this.previewNotice.waitFor({ state: "hidden" }); } + + async getWordCount() { + return parseInt( + (await this.wordCountText.allInnerTexts()) + .toString() + .replace(" words", "") + ); + } } diff --git a/apps/web/src/components/editor/tiptap.tsx b/apps/web/src/components/editor/tiptap.tsx index cc7a513f0..cecc74853 100644 --- a/apps/web/src/components/editor/tiptap.tsx +++ b/apps/web/src/components/editor/tiptap.tsx @@ -84,7 +84,7 @@ function save( configureEditor({ statistics: { words: { - total: countWords(content.textBetween(0, content.size)), + total: countWords(content.textBetween(0, content.size, "\n", " ")), selected: 0 } } @@ -210,7 +210,7 @@ function TipTap(props: TipTapProps) { words: { total: old.statistics?.words.total || - countWords(content.textBetween(0, content.size)), + countWords(content.textBetween(0, content.size, "\n", " ")), selected: getSelectedWords( editor as Editor, transaction.selection @@ -424,5 +424,6 @@ function countWords(str: string) { } if (shouldCount) ++count; + return count; }