Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

web: do not ignore line breaks when counting words (#1625)

Muhammad Ali8e6bbb9227ce
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;
}