@inshapardaz/likhari-react 0.1.19 → 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.
@@ -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
+ }
@@ -160,6 +160,14 @@
160
160
  /* Canvas — editor-ui-design-spec.md §4. flex: 1 1 auto + min-height: 0 lets
161
161
  this pane absorb the root's height and scroll internally instead of the
162
162
  editor (and the page around it) growing to fit the content. */
163
+ .likhari-canvas-frame {
164
+ position: relative;
165
+ display: flex;
166
+ flex-direction: column;
167
+ flex: 1 1 auto;
168
+ min-height: 0;
169
+ }
170
+
163
171
  .likhari-canvas {
164
172
  position: relative;
165
173
  flex: 1 1 auto;
@@ -755,3 +763,110 @@
755
763
  outline: 2px solid var(--editor-accent);
756
764
  outline-offset: 1px;
757
765
  }
766
+
767
+ /* Floats over the top-right of the canvas, so it takes no space from the
768
+ toolbar or the page. */
769
+ .likhari-find-widget {
770
+ position: absolute;
771
+ z-index: 5;
772
+ top: 8px;
773
+ right: 16px;
774
+ display: flex;
775
+ flex-direction: column;
776
+ gap: 6px;
777
+ width: fit-content;
778
+ padding: 6px;
779
+ border: 1px solid var(--editor-line);
780
+ border-radius: 6px;
781
+ background: var(--editor-paper-dim);
782
+ box-shadow: 0 4px 12px rgba(0, 0, 0, 0.12);
783
+ font-size: 13px;
784
+ }
785
+
786
+ /* Both rows share one grid. Columns: chevron, input, then two narrow tracks
787
+ that hold either the match count (search row) or the two replace buttons
788
+ (replace row), then prev, next and close. */
789
+ .likhari-find-row {
790
+ display: grid;
791
+ grid-template-columns: 24px 180px 24px 24px 24px 24px 24px;
792
+ align-items: center;
793
+ column-gap: 4px;
794
+ }
795
+
796
+ .likhari-find-replace-input {
797
+ grid-column: 2;
798
+ }
799
+
800
+ .likhari-find-status {
801
+ grid-column: 3 / span 2;
802
+ }
803
+
804
+ .likhari-find-prev {
805
+ grid-column: 5;
806
+ }
807
+
808
+ .likhari-find-next {
809
+ grid-column: 6;
810
+ }
811
+
812
+ .likhari-find-close {
813
+ grid-column: 7;
814
+ }
815
+
816
+ .likhari-find-replace-one {
817
+ grid-column: 3;
818
+ }
819
+
820
+ .likhari-find-replace-all {
821
+ grid-column: 4;
822
+ }
823
+
824
+ .likhari-find-widget[dir='rtl'] {
825
+ right: auto;
826
+ left: 16px;
827
+ }
828
+
829
+ .likhari-find-widget input {
830
+ width: 100%;
831
+ min-width: 0;
832
+ padding: 3px 6px;
833
+ border: 1px solid var(--editor-line);
834
+ border-radius: 4px;
835
+ background: var(--editor-paper);
836
+ color: inherit;
837
+ }
838
+
839
+ .likhari-find-widget button {
840
+ padding: 2px 8px;
841
+ border: 1px solid var(--editor-line);
842
+ border-radius: 4px;
843
+ background: transparent;
844
+ color: inherit;
845
+ cursor: pointer;
846
+ white-space: nowrap;
847
+ }
848
+
849
+ .likhari-find-widget button:disabled {
850
+ opacity: 0.5;
851
+ cursor: default;
852
+ }
853
+
854
+ .likhari-find-widget .likhari-find-icon {
855
+ display: inline-flex;
856
+ align-items: center;
857
+ justify-content: center;
858
+ width: 24px;
859
+ height: 24px;
860
+ padding: 0;
861
+ border: none;
862
+ }
863
+
864
+ .likhari-find-icon-spacer {
865
+ display: block;
866
+ }
867
+
868
+ .likhari-find-status {
869
+ text-align: end;
870
+ opacity: 0.8;
871
+ white-space: nowrap;
872
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inshapardaz/likhari-react",
3
- "version": "0.1.19",
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
  }