editor: use alfaaz for word counting

This commit is contained in:
Abdullah Atta
2024-04-09 01:28:45 +05:00
parent 646949c96c
commit ee14a292ab
3 changed files with 42 additions and 63 deletions

View File

@@ -35,6 +35,7 @@
"@tiptap/extension-underline": "2.2.4",
"@tiptap/pm": "2.2.4",
"@tiptap/starter-kit": "2.2.4",
"alfaaz": "^1.1.0",
"async-mutex": "^0.4.0",
"clipboard-polyfill": "4.0.0",
"detect-indent": "^7.0.0",
@@ -976,6 +977,22 @@
"@styled-system/css": "^5.1.5"
}
},
"node_modules/@theme-ui/color-modes": {
"version": "0.16.2",
"resolved": "https://registry.npmjs.org/@theme-ui/color-modes/-/color-modes-0.16.2.tgz",
"integrity": "sha512-jWEWx53lxNgWCT38i/kwLV2rsvJz8lVZgi5oImnVwYba9VejXD23q1ckbNFJHosQ8KKXY87ht0KPC6BQFIiHtQ==",
"dev": true,
"peer": true,
"dependencies": {
"@theme-ui/core": "^0.16.2",
"@theme-ui/css": "^0.16.2",
"deepmerge": "^4.2.2"
},
"peerDependencies": {
"@emotion/react": "^11.11.1",
"react": ">=18"
}
},
"node_modules/@theme-ui/components": {
"version": "0.16.1",
"resolved": "https://registry.npmjs.org/@theme-ui/components/-/components-0.16.1.tgz",
@@ -1021,6 +1038,22 @@
"@emotion/react": "^11.11.1"
}
},
"node_modules/@theme-ui/theme-provider": {
"version": "0.16.2",
"resolved": "https://registry.npmjs.org/@theme-ui/theme-provider/-/theme-provider-0.16.2.tgz",
"integrity": "sha512-LRnVevODcGqO0JyLJ3wht+PV3ZoZcJ7XXLJAJWDoGeII4vZcPQKwVy4Lpz/juHsZppQxKcB3U+sQDGBnP25irQ==",
"dev": true,
"peer": true,
"dependencies": {
"@theme-ui/color-modes": "^0.16.2",
"@theme-ui/core": "^0.16.2",
"@theme-ui/css": "^0.16.2"
},
"peerDependencies": {
"@emotion/react": "^11.11.1",
"react": ">=18"
}
},
"node_modules/@tiptap/core": {
"version": "2.2.4",
"resolved": "https://registry.npmjs.org/@tiptap/core/-/core-2.2.4.tgz",
@@ -1719,6 +1752,11 @@
"node": ">=0.4.0"
}
},
"node_modules/alfaaz": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/alfaaz/-/alfaaz-1.1.0.tgz",
"integrity": "sha512-J/P07R41APslK7NmD5303bwStN8jpRA4DdvtLeAr1Jhfj6XWGrASUWI0G6jbWjJAZyw3Lu1Pb4J8rsM/cb+xDQ=="
},
"node_modules/ansi-regex": {
"version": "5.0.1",
"resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.1.tgz",
@@ -2495,8 +2533,7 @@
"node_modules/js-tokens": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz",
"integrity": "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==",
"dev": true
"integrity": "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ=="
},
"node_modules/json-parse-even-better-errors": {
"version": "2.3.1",
@@ -2560,7 +2597,6 @@
"version": "1.4.0",
"resolved": "https://registry.npmjs.org/loose-envify/-/loose-envify-1.4.0.tgz",
"integrity": "sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q==",
"dev": true,
"dependencies": {
"js-tokens": "^3.0.0 || ^4.0.0"
},
@@ -3134,7 +3170,6 @@
"version": "18.2.0",
"resolved": "https://registry.npmjs.org/react/-/react-18.2.0.tgz",
"integrity": "sha512-/3IjMdb2L9QbBdWiW5e3P2/npwMBaU9mHCSCUzNln0ZCYbcfTsGbTJrU/kGemdH2IWmB2ioZ+zkxtmq6g09fGQ==",
"dev": true,
"dependencies": {
"loose-envify": "^1.1.0"
},
@@ -3155,7 +3190,6 @@
"version": "18.2.0",
"resolved": "https://registry.npmjs.org/react-dom/-/react-dom-18.2.0.tgz",
"integrity": "sha512-6IMTriUmvsjHUjNtEDudZfuDQUoWXVxKHhlEGSk81n4YFS+r/Kl99wXiwlVXtPBtJenozv2P+hxDsw9eA7Xo6g==",
"dev": true,
"dependencies": {
"loose-envify": "^1.1.0",
"scheduler": "^0.23.0"
@@ -3287,7 +3321,6 @@
"version": "0.23.0",
"resolved": "https://registry.npmjs.org/scheduler/-/scheduler-0.23.0.tgz",
"integrity": "sha512-CtuThmgHNg7zIZWAXi3AsyIzA3n4xx7aNyjwC2VJldO2LMVDhFK+63xGqq6CsJH4rTAt6/M+N4GhZiDYPx9eUw==",
"dev": true,
"dependencies": {
"loose-envify": "^1.1.0"
}

View File

@@ -30,6 +30,7 @@
"@tiptap/extension-underline": "2.2.4",
"@tiptap/pm": "2.2.4",
"@tiptap/starter-kit": "2.2.4",
"alfaaz": "^1.1.0",
"async-mutex": "^0.4.0",
"clipboard-polyfill": "4.0.0",
"detect-indent": "^7.0.0",

View File

@@ -18,6 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { Editor } from "@tiptap/core";
import { countWords } from "alfaaz";
export function getTotalWords(editor: Editor): number {
const documentText = editor.state.doc.textBetween(
@@ -29,60 +30,4 @@ export function getTotalWords(editor: Editor): number {
return countWords(documentText);
}
export function countWords(str: string) {
let count = 0;
let shouldCount = false;
let isScript = false;
for (let i = 0; i < str.length; ++i) {
const s = str[i];
if (
s === " " ||
s === "\r" ||
s === "\n" ||
s === "*" ||
s === "/" ||
s === "&" ||
(isScript = isCJKChar(s))
) {
if (!shouldCount && !isScript) continue;
++count;
shouldCount = false;
} else {
shouldCount = true;
}
}
if (shouldCount) ++count;
return count;
}
// Taken from: https://en.wikipedia.org/wiki/CJK_Unified_Ideographs
const CJK_UNICODE_RANGES = [
[19968, 40959], // CJK Unified Ideographs 4E00-9FFF Common
[13312, 19903], // CJK Unified Ideographs Extension A 3400-4DBF Rare
[131072, 173791], // CJK Unified Ideographs Extension B 20000-2A6DF Rare, historic
[173824, 177983], // CJK Unified Ideographs Extension C 2A7002B73F Rare, historic
[177984, 178207], // CJK Unified Ideographs Extension D 2B7402B81F Uncommon, some in current use
[178208, 183983], // CJK Unified Ideographs Extension E 2B8202CEAF Rare, historic
[183984, 191471], // CJK Unified Ideographs Extension F 2CEB02EBEF Rare, historic
[196608, 201551], // CJK Unified Ideographs Extension G 300003134F Rare, historic
[201552, 205743], // CJK Unified Ideographs Extension H 31350323AF Rare, historic
[63744, 64255], // CJK Compatibility Ideographs F900-FAFF Duplicates, unifiable variants, corporate characters
[194560, 195103], // CJK Compatibility Ideographs Supplement 2F800-2FA1F Unifiable variants
[12032, 12255], // CJK Radicals / Kangxi Radicals 2F002FDF
[11904, 12031], // CJK Radicals Supplement 2E802EFF
[12288, 12351], // CJK Symbols and Punctuation 3000303F
[13056, 13311], // CJK Compatibility 3300-33FF
[65072, 65103] // CJK Compatibility Forms FE30-FE4F
];
function isCJKChar(char: string) {
const code = char.charCodeAt(0);
const isIn = CJK_UNICODE_RANGES.some(
(range) => code >= range[0] && code <= range[1]
);
return isIn;
}
export { countWords };