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

Signed-off-by: Abdullah Atta <thecodrr@protonmail.com>
Co-authored-by: Abdullah Atta <abdullahatta@streetwriters.co>
This commit is contained in:
Muhammad Ali
2023-01-10 16:06:20 +05:00
committed by GitHub
parent a1440fd64c
commit 8e6bbb9227
3 changed files with 21 additions and 2 deletions

View File

@@ -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();
});

View File

@@ -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", "")
);
}
}

View File

@@ -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;
}