@inshapardaz/likhari-react 0.1.20 → 0.1.21-dev.192
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cleanup/textCleanupActions.d.ts +8 -0
- package/dist/cleanup/textCleanupActions.d.ts.map +1 -0
- package/dist/cleanup/textCleanupActions.js +28 -0
- package/dist/components/Toolbar.d.ts.map +1 -1
- package/dist/components/Toolbar.js +2 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/spellcheck/dictionaries/en/LICENSE.txt +347 -0
- package/dist/spellcheck/dictionaries/en/index.aff +205 -0
- package/dist/spellcheck/dictionaries/en/index.dic +49569 -0
- package/dist/spellcheck/spellDictionaries.d.ts +40 -0
- package/dist/spellcheck/spellDictionaries.d.ts.map +1 -0
- package/dist/spellcheck/spellDictionaries.js +62 -0
- package/package.json +8 -6
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/** Languages the spellchecker has a dictionary slot for. */
|
|
2
|
+
export type SpellLanguage = 'en' | 'ur' | 'pa-shahmukhi';
|
|
3
|
+
/** A Hunspell dictionary as the two text files every Hunspell dictionary ships with. */
|
|
4
|
+
export interface HunspellFiles {
|
|
5
|
+
/** Affix file: language, encoding, character rules. */
|
|
6
|
+
aff: string;
|
|
7
|
+
/** Dictionary file: one word per line, with optional affix flags. */
|
|
8
|
+
dic: string;
|
|
9
|
+
}
|
|
10
|
+
export interface Speller {
|
|
11
|
+
correct(word: string): boolean;
|
|
12
|
+
suggest(word: string): string[];
|
|
13
|
+
}
|
|
14
|
+
type Loader = () => Promise<HunspellFiles>;
|
|
15
|
+
/**
|
|
16
|
+
* Registers a dictionary for a language. Call once at startup with files you
|
|
17
|
+
* have licence to ship — e.g. the Urdu `.aff`/`.dic` pair — either as strings
|
|
18
|
+
* or as a loader that fetches them on demand. Registering again replaces the
|
|
19
|
+
* previous dictionary for that language.
|
|
20
|
+
*/
|
|
21
|
+
export declare function registerSpellDictionary(language: SpellLanguage, files: HunspellFiles | Loader): void;
|
|
22
|
+
/** Whether a dictionary is registered for the language. */
|
|
23
|
+
export declare function hasSpellDictionary(language: SpellLanguage): boolean;
|
|
24
|
+
/**
|
|
25
|
+
* The speller for a language, loading its dictionary the first time it is
|
|
26
|
+
* asked for. Resolves to null when no dictionary is registered, so callers
|
|
27
|
+
* can skip spellchecking that language rather than flag every word.
|
|
28
|
+
*/
|
|
29
|
+
export declare function getSpeller(language: SpellLanguage): Promise<Speller> | null;
|
|
30
|
+
/** A word in a text, with its position as character offsets. */
|
|
31
|
+
export interface WordSpan {
|
|
32
|
+
word: string;
|
|
33
|
+
start: number;
|
|
34
|
+
end: number;
|
|
35
|
+
}
|
|
36
|
+
export declare function wordsIn(text: string): WordSpan[];
|
|
37
|
+
/** The words in `text` the speller does not recognise. */
|
|
38
|
+
export declare function misspellingsIn(text: string, speller: Speller): WordSpan[];
|
|
39
|
+
export {};
|
|
40
|
+
//# sourceMappingURL=spellDictionaries.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"spellDictionaries.d.ts","sourceRoot":"","sources":["../../src/spellcheck/spellDictionaries.ts"],"names":[],"mappings":"AAEA,4DAA4D;AAC5D,MAAM,MAAM,aAAa,GAAG,IAAI,GAAG,IAAI,GAAG,cAAc,CAAC;AAEzD,wFAAwF;AACxF,MAAM,WAAW,aAAa;IAC5B,uDAAuD;IACvD,GAAG,EAAE,MAAM,CAAC;IACZ,qEAAqE;IACrE,GAAG,EAAE,MAAM,CAAC;CACb;AAED,MAAM,WAAW,OAAO;IACtB,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC;IAC/B,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;CACjC;AAED,KAAK,MAAM,GAAG,MAAM,OAAO,CAAC,aAAa,CAAC,CAAC;AAmB3C;;;;;GAKG;AACH,wBAAgB,uBAAuB,CAAC,QAAQ,EAAE,aAAa,EAAE,KAAK,EAAE,aAAa,GAAG,MAAM,GAAG,IAAI,CAGpG;AAED,2DAA2D;AAC3D,wBAAgB,kBAAkB,CAAC,QAAQ,EAAE,aAAa,GAAG,OAAO,CAEnE;AAED;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,QAAQ,EAAE,aAAa,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,IAAI,CAS3E;AAED,gEAAgE;AAChE,MAAM,WAAW,QAAQ;IACvB,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;CACb;AASD,wBAAgB,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,QAAQ,EAAE,CAMhD;AAED,0DAA0D;AAC1D,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,GAAG,QAAQ,EAAE,CAEzE"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import nspell from 'nspell';
|
|
2
|
+
const loaders = new Map();
|
|
3
|
+
const spellers = new Map();
|
|
4
|
+
async function fetchText(url) {
|
|
5
|
+
const response = await fetch(url);
|
|
6
|
+
if (!response.ok)
|
|
7
|
+
throw new Error(`Spellcheck dictionary failed to load: ${url}`);
|
|
8
|
+
return response.text();
|
|
9
|
+
}
|
|
10
|
+
/** English ships with the editor: its files are fetched on first use. */
|
|
11
|
+
const englishLoader = async () => ({
|
|
12
|
+
aff: await fetchText(new URL('./dictionaries/en/index.aff', import.meta.url).href),
|
|
13
|
+
dic: await fetchText(new URL('./dictionaries/en/index.dic', import.meta.url).href),
|
|
14
|
+
});
|
|
15
|
+
loaders.set('en', englishLoader);
|
|
16
|
+
/**
|
|
17
|
+
* Registers a dictionary for a language. Call once at startup with files you
|
|
18
|
+
* have licence to ship — e.g. the Urdu `.aff`/`.dic` pair — either as strings
|
|
19
|
+
* or as a loader that fetches them on demand. Registering again replaces the
|
|
20
|
+
* previous dictionary for that language.
|
|
21
|
+
*/
|
|
22
|
+
export function registerSpellDictionary(language, files) {
|
|
23
|
+
loaders.set(language, typeof files === 'function' ? files : async () => files);
|
|
24
|
+
spellers.delete(language);
|
|
25
|
+
}
|
|
26
|
+
/** Whether a dictionary is registered for the language. */
|
|
27
|
+
export function hasSpellDictionary(language) {
|
|
28
|
+
return loaders.has(language);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* The speller for a language, loading its dictionary the first time it is
|
|
32
|
+
* asked for. Resolves to null when no dictionary is registered, so callers
|
|
33
|
+
* can skip spellchecking that language rather than flag every word.
|
|
34
|
+
*/
|
|
35
|
+
export function getSpeller(language) {
|
|
36
|
+
const loader = loaders.get(language);
|
|
37
|
+
if (!loader)
|
|
38
|
+
return null;
|
|
39
|
+
let speller = spellers.get(language);
|
|
40
|
+
if (!speller) {
|
|
41
|
+
speller = loader().then(({ aff, dic }) => nspell(aff, dic));
|
|
42
|
+
spellers.set(language, speller);
|
|
43
|
+
}
|
|
44
|
+
return speller;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Letters and combining marks, with apostrophes inside a word kept. The
|
|
48
|
+
* Unicode classes cover Arabic-script letters and their diacritics, so Urdu
|
|
49
|
+
* and Shahmukhi words are found the same way English ones are.
|
|
50
|
+
*/
|
|
51
|
+
const WORD = /[\p{L}\p{M}]+(?:['’][\p{L}\p{M}]+)*/gu;
|
|
52
|
+
export function wordsIn(text) {
|
|
53
|
+
return [...text.matchAll(WORD)].map((match) => ({
|
|
54
|
+
word: match[0],
|
|
55
|
+
start: match.index ?? 0,
|
|
56
|
+
end: (match.index ?? 0) + match[0].length,
|
|
57
|
+
}));
|
|
58
|
+
}
|
|
59
|
+
/** The words in `text` the speller does not recognise. */
|
|
60
|
+
export function misspellingsIn(text, speller) {
|
|
61
|
+
return wordsIn(text).filter((span) => !speller.correct(span.word));
|
|
62
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inshapardaz/likhari-react",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.21-dev.192",
|
|
4
4
|
"description": "React implementation of the Likhari editor, built on @lexical/react.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -29,10 +29,12 @@
|
|
|
29
29
|
"*.css"
|
|
30
30
|
],
|
|
31
31
|
"scripts": {
|
|
32
|
-
"build": "tsc -p tsconfig.build.json && node ../../scripts/fix-esm-extensions.mjs ./dist && node ../../scripts/copy-assets.mjs src dist \"**/*.css\"",
|
|
32
|
+
"build": "tsc -p tsconfig.build.json && node ../../scripts/fix-esm-extensions.mjs ./dist && node ../../scripts/copy-assets.mjs src dist \"**/*.css\" && node ../../scripts/copy-assets.mjs src dist \"**/*.aff\" && node ../../scripts/copy-assets.mjs src dist \"**/*.dic\" && node ../../scripts/copy-assets.mjs src dist \"**/*.txt\"",
|
|
33
33
|
"prepublishOnly": "npm run build"
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
|
+
"@inshapardaz/likhari-converters": "^0.1.0",
|
|
37
|
+
"@inshapardaz/likhari-core": "^0.1.0",
|
|
36
38
|
"@lexical/link": "^0.20.0",
|
|
37
39
|
"@lexical/list": "^0.20.0",
|
|
38
40
|
"@lexical/react": "^0.20.0",
|
|
@@ -40,10 +42,9 @@
|
|
|
40
42
|
"@lexical/selection": "^0.20.0",
|
|
41
43
|
"@lexical/table": "^0.20.0",
|
|
42
44
|
"@lexical/utils": "^0.20.0",
|
|
43
|
-
"@inshapardaz/likhari-converters": "^0.1.0",
|
|
44
|
-
"@inshapardaz/likhari-core": "^0.1.0",
|
|
45
45
|
"@tabler/icons-react": "^3.46.0",
|
|
46
|
-
"lexical": "^0.20.0"
|
|
46
|
+
"lexical": "^0.20.0",
|
|
47
|
+
"nspell": "^2.1.5"
|
|
47
48
|
},
|
|
48
49
|
"peerDependencies": {
|
|
49
50
|
"@mantine/core": "^9.6.3",
|
|
@@ -53,6 +54,7 @@
|
|
|
53
54
|
},
|
|
54
55
|
"devDependencies": {
|
|
55
56
|
"@mantine/core": "^9.6.3",
|
|
56
|
-
"@mantine/hooks": "^9.6.3"
|
|
57
|
+
"@mantine/hooks": "^9.6.3",
|
|
58
|
+
"@types/nspell": "^2.1.6"
|
|
57
59
|
}
|
|
58
60
|
}
|