@inshapardaz/likhari-react 0.1.22 → 0.1.23-dev.221

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.
Files changed (48) hide show
  1. package/dist/EditorRoot.d.ts +12 -0
  2. package/dist/EditorRoot.d.ts.map +1 -1
  3. package/dist/EditorRoot.js +24 -11
  4. package/dist/autocorrect/AutoCorrectPlugin.d.ts +27 -0
  5. package/dist/autocorrect/AutoCorrectPlugin.d.ts.map +1 -0
  6. package/dist/autocorrect/AutoCorrectPlugin.js +124 -0
  7. package/dist/autocorrect/autoCorrectActions.d.ts +41 -0
  8. package/dist/autocorrect/autoCorrectActions.d.ts.map +1 -0
  9. package/dist/autocorrect/autoCorrectActions.js +212 -0
  10. package/dist/autocorrect/autoCorrectStores.d.ts +52 -0
  11. package/dist/autocorrect/autoCorrectStores.d.ts.map +1 -0
  12. package/dist/autocorrect/autoCorrectStores.js +115 -0
  13. package/dist/autocorrect/punctuationRules.d.ts +26 -0
  14. package/dist/autocorrect/punctuationRules.d.ts.map +1 -0
  15. package/dist/autocorrect/punctuationRules.js +53 -0
  16. package/dist/cleanup/textCleanupActions.d.ts +8 -0
  17. package/dist/cleanup/textCleanupActions.d.ts.map +1 -0
  18. package/dist/cleanup/textCleanupActions.js +28 -0
  19. package/dist/components/AutoCorrectPanel.d.ts +19 -0
  20. package/dist/components/AutoCorrectPanel.d.ts.map +1 -0
  21. package/dist/components/AutoCorrectPanel.js +44 -0
  22. package/dist/components/SpellcheckPanel.d.ts +5 -1
  23. package/dist/components/SpellcheckPanel.d.ts.map +1 -1
  24. package/dist/components/SpellcheckPanel.js +12 -2
  25. package/dist/components/Toolbar.d.ts +10 -1
  26. package/dist/components/Toolbar.d.ts.map +1 -1
  27. package/dist/components/Toolbar.js +128 -109
  28. package/dist/i18n/strings.d.ts +25 -0
  29. package/dist/i18n/strings.d.ts.map +1 -1
  30. package/dist/i18n/strings.js +80 -5
  31. package/dist/index.d.ts +7 -0
  32. package/dist/index.d.ts.map +1 -1
  33. package/dist/index.js +3 -0
  34. package/dist/normalization/urduNormalize.d.ts +24 -0
  35. package/dist/normalization/urduNormalize.d.ts.map +1 -0
  36. package/dist/normalization/urduNormalize.js +116 -0
  37. package/dist/spellcheck/SpellHighlightPlugin.d.ts.map +1 -1
  38. package/dist/spellcheck/SpellHighlightPlugin.js +3 -0
  39. package/dist/spellcheck/spellDictionaries.d.ts.map +1 -1
  40. package/dist/spellcheck/spellDictionaries.js +8 -1
  41. package/dist/spellcheck/spellingMenu.d.ts +15 -0
  42. package/dist/spellcheck/spellingMenu.d.ts.map +1 -0
  43. package/dist/spellcheck/spellingMenu.js +27 -0
  44. package/dist/spellcheck/userWords.d.ts +39 -0
  45. package/dist/spellcheck/userWords.d.ts.map +1 -0
  46. package/dist/spellcheck/userWords.js +132 -0
  47. package/dist/theme/editor.css +58 -0
  48. package/package.json +1 -1
@@ -4,6 +4,10 @@ import { type FormatId } from '@inshapardaz/likhari-converters';
4
4
  import { type EditorFeatureConfig, type FeatureConfigPresetName } from '@inshapardaz/likhari-core';
5
5
  import { type FontOption } from './fonts/index.js';
6
6
  import { type DraftRestoreMode } from './components/DraftRestore.js';
7
+ import { type AutoCorrectStore } from './autocorrect/autoCorrectStores.js';
8
+ import type { UrduNormalizationOptions } from './normalization/urduNormalize.js';
9
+ import type { PunctuationOptions } from './autocorrect/punctuationRules.js';
10
+ import { type UserWordStore } from './spellcheck/userWords.js';
7
11
  import { type Locale } from './i18n/index.js';
8
12
  /** What happens when the user is about to leave with unsaved changes. */
9
13
  export type NavigationGuardMode = 'confirm' | 'save-draft' | 'off';
@@ -13,6 +17,14 @@ export interface EditorInitialContent {
13
17
  }
14
18
  export interface EditorRootProps {
15
19
  documentId?: string;
20
+ /** Where auto-corrections are loaded from and saved to, in priority order. Pass a stable array. */
21
+ autoCorrectStores?: AutoCorrectStore[];
22
+ /** Where words added to the spelling dictionary are saved, in priority order. Pass a stable array. */
23
+ dictionaryStores?: UserWordStore[];
24
+ /** Urdu normalisation applied as part of auto-correct. Diacritics are kept unless `removeDiacritics` is set. */
25
+ urduNormalization?: UrduNormalizationOptions;
26
+ /** Common punctuation fixes, and whether a straight " becomes ”. Both on by default. */
27
+ punctuation?: PunctuationOptions;
16
28
  initialContent?: EditorInitialContent;
17
29
  featureConfig?: EditorFeatureConfig;
18
30
  featurePreset?: FeatureConfigPresetName;
@@ -1 +1 @@
1
- {"version":3,"file":"EditorRoot.d.ts","sourceRoot":"","sources":["../src/EditorRoot.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,eAAe,CAAC;AAY1D,OAAO,KAAK,EAA8B,qBAAqB,EAAE,MAAM,SAAS,CAAC;AAEjF,OAAO,EAAyB,KAAK,QAAQ,EAAE,MAAM,iCAAiC,CAAC;AACvF,OAAO,EAAwB,KAAK,mBAAmB,EAAE,KAAK,uBAAuB,EAAE,MAAM,2BAA2B,CAAC;AAKzH,OAAO,EAAyB,KAAK,UAAU,EAAE,MAAM,SAAS,CAAC;AAOjE,OAAO,EAAgB,KAAK,gBAAgB,EAAE,MAAM,2BAA2B,CAAC;AAehF,OAAO,EAAgC,KAAK,MAAM,EAAE,MAAM,QAAQ,CAAC;AAMnE,yEAAyE;AACzE,MAAM,MAAM,mBAAmB,GAAG,SAAS,GAAG,YAAY,GAAG,KAAK,CAAC;AAEnE,MAAM,WAAW,oBAAoB;IACnC,MAAM,EAAE,QAAQ,CAAC;IACjB,KAAK,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,eAAe;IAC9B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,cAAc,CAAC,EAAE,oBAAoB,CAAC;IACtC,aAAa,CAAC,EAAE,mBAAmB,CAAC;IACpC,aAAa,CAAC,EAAE,uBAAuB,CAAC;IACxC,KAAK,CAAC,EAAE,oBAAoB,CAAC;IAC7B,WAAW,CAAC,EAAE,OAAO,GAAG,MAAM,CAAC;IAC/B;;;;;;OAMG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;;;;;;;;OAeG;IACH,eAAe,CAAC,EAAE,mBAAmB,CAAC;IACtC,uGAAuG;IACvG,eAAe,CAAC,EAAE,CAAC,KAAK,EAAE;QAAE,UAAU,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAA;KAAE,KAAK,IAAI,CAAC;IAC3E,6EAA6E;IAC7E,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB;;;;;OAKG;IACH,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B;;;;OAIG;IACH,iBAAiB,CAAC,EAAE,MAAM,CAAC;CAC5B;AAED,MAAM,WAAW,SAAS;IACxB,UAAU,CAAC,MAAM,EAAE,QAAQ,GAAG,MAAM,CAAC;IACrC,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,GAAG,IAAI,CAAC;IAClD,iBAAiB,IAAI,OAAO,CAAC;IAC7B,cAAc,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC;IACnC,KAAK,IAAI,IAAI,CAAC;CACf;AAQD,eAAO,MAAM,UAAU,uGAibrB,CAAC"}
@@ -27,8 +27,12 @@ import { PoetryPlugin } from './plugins/PoetryPlugin.js';
27
27
  import { PoetryResizer } from './blocks/PoetryResizer.js';
28
28
  import { DraftRestore } from './components/DraftRestore.js';
29
29
  import { FindReplaceBar } from './components/FindReplaceBar.js';
30
+ import { AutoCorrectPanel } from './components/AutoCorrectPanel.js';
31
+ import { AutoCorrectPlugin } from './autocorrect/AutoCorrectPlugin.js';
32
+ import { localStorageAutoCorrectStore } from './autocorrect/autoCorrectStores.js';
30
33
  import { SpellcheckPanel } from './components/SpellcheckPanel.js';
31
34
  import { SpellHighlightPlugin } from './spellcheck/SpellHighlightPlugin.js';
35
+ import { loadUserWords, localStorageUserWordStore } from './spellcheck/userWords.js';
32
36
  import { LeaveDialog } from './components/LeaveDialog.js';
33
37
  import { clearDraft, createDraftId, hasDraftContent, pruneDrafts, writeDraft, } from './persistence/draftStorage.js';
34
38
  import { ImageOptionsContext } from './image/ImageOptionsContext.js';
@@ -37,13 +41,17 @@ import { UiStringsContext, getStrings } from './i18n/index.js';
37
41
  const DEFAULT_AUTOSAVE_DELAY_MS = 750;
38
42
  const DEFAULT_AUTOSAVE_MAX_BYTES = 2000000;
39
43
  const DEFAULT_AUTOSAVE_MAX_DRAFTS = 20;
44
+ /** Used when the host passes no stores: corrections are kept in this browser. */
45
+ const DEFAULT_AUTOCORRECT_STORES = [localStorageAutoCorrectStore()];
46
+ /** Used when the host passes no dictionary stores: added words are kept in this browser. */
47
+ const DEFAULT_DICTIONARY_STORES = [localStorageUserWordStore()];
40
48
  function initialEditorStateJson(initialContent) {
41
49
  if (!initialContent)
42
50
  return undefined;
43
51
  const state = defaultFormatRegistry.parse(initialContent.format, initialContent.value);
44
52
  return JSON.stringify(state);
45
53
  }
46
- export const EditorRoot = forwardRef(function EditorRoot({ documentId, initialContent, featureConfig, featurePreset, theme, colorScheme, accentColor, locale = 'en', placeholder, height = '480px', onChange, onSave, showSave = Boolean(onSave), onImageUpload, fetchImage, fontOptions, autosave = true, restoreDraft = 'prompt', navigationGuard = 'confirm', onDraftRestored, autosaveDelayMs = DEFAULT_AUTOSAVE_DELAY_MS, autosaveMaxBytes = DEFAULT_AUTOSAVE_MAX_BYTES, autosaveMaxDrafts = DEFAULT_AUTOSAVE_MAX_DRAFTS, }, ref) {
54
+ export const EditorRoot = forwardRef(function EditorRoot({ documentId, autoCorrectStores, dictionaryStores, urduNormalization, punctuation, initialContent, featureConfig, featurePreset, theme, colorScheme, accentColor, locale = 'en', placeholder, height = '480px', onChange, onSave, showSave = Boolean(onSave), onImageUpload, fetchImage, fontOptions, autosave = true, restoreDraft = 'prompt', navigationGuard = 'confirm', onDraftRestored, autosaveDelayMs = DEFAULT_AUTOSAVE_DELAY_MS, autosaveMaxBytes = DEFAULT_AUTOSAVE_MAX_BYTES, autosaveMaxDrafts = DEFAULT_AUTOSAVE_MAX_DRAFTS, }, ref) {
47
55
  const config = useMemo(() => resolveFeatureConfig(featureConfig, featurePreset), [featureConfig, featurePreset]);
48
56
  const strings = useMemo(() => getStrings(locale), [locale]);
49
57
  const resolvedPlaceholder = placeholder ?? strings.editor.placeholder;
@@ -70,8 +78,19 @@ export const EditorRoot = forwardRef(function EditorRoot({ documentId, initialCo
70
78
  const editorStateRef = useRef(null);
71
79
  const lastSavedJsonRef = useRef(null);
72
80
  const [isDirty, setIsDirty] = useState(false);
73
- const [findOpen, setFindOpen] = useState(false);
74
- const [spellOpen, setSpellOpen] = useState(false);
81
+ const [openPanel, setOpenPanel] = useState(null);
82
+ const [autoCorrectPrefill, setAutoCorrectPrefill] = useState(null);
83
+ const openAutoCorrectFor = (from, language) => {
84
+ setAutoCorrectPrefill({ id: Date.now(), from, language });
85
+ setOpenPanel('autocorrect');
86
+ };
87
+ const [autoCorrectVersion, setAutoCorrectVersion] = useState(0);
88
+ const stores = autoCorrectStores ?? DEFAULT_AUTOCORRECT_STORES;
89
+ const wordStores = dictionaryStores ?? DEFAULT_DICTIONARY_STORES;
90
+ useEffect(() => {
91
+ void loadUserWords(wordStores);
92
+ }, [wordStores]);
93
+ const togglePanel = (panel) => setOpenPanel((current) => (current === panel ? null : panel));
75
94
  const isDirtyRef = useRef(isDirty);
76
95
  isDirtyRef.current = isDirty;
77
96
  const autosaveTimerRef = useRef(null);
@@ -284,15 +303,9 @@ export const EditorRoot = forwardRef(function EditorRoot({ documentId, initialCo
284
303
  // rather than colliding on a shared one.
285
304
  const portalTargetId = useId();
286
305
  const portalTargetSelector = `#${CSS.escape(portalTargetId)}`;
287
- return (_jsx(EditorThemeProvider, { theme: theme, colorScheme: colorScheme, accentColor: accentColor, scopeElementId: portalTargetId, children: _jsxs("div", { className: "likhari-root", dir: dir, ref: rootElementRef, "data-document-id": documentId, style: { height: typeof height === 'number' ? `${height}px` : height }, children: [_jsx("div", { id: portalTargetId, className: "likhari-portal-target" }), _jsx(PortalTargetContext.Provider, { value: portalTargetSelector, children: _jsxs(UiStringsContext.Provider, { value: strings, children: [_jsx(ImageOptionsContext.Provider, { value: imageOptions, children: _jsxs(LexicalComposer, { initialConfig: initialConfig, children: [_jsx(Toolbar, { config: config, onSave: handleSave, findOpen: findOpen, onToggleFind: () => {
288
- setFindOpen((v) => !v);
289
- setSpellOpen(false);
290
- }, spellOpen: spellOpen, onToggleSpell: () => {
291
- setSpellOpen((v) => !v);
292
- setFindOpen(false);
293
- }, isDirty: isDirty, showSave: showSave, fontOptions: fontOptions, direction: dir, locale: locale, drafts: autosave
306
+ return (_jsx(EditorThemeProvider, { theme: theme, colorScheme: colorScheme, accentColor: accentColor, scopeElementId: portalTargetId, children: _jsxs("div", { className: "likhari-root", dir: dir, ref: rootElementRef, "data-document-id": documentId, style: { height: typeof height === 'number' ? `${height}px` : height }, children: [_jsx("div", { id: portalTargetId, className: "likhari-portal-target" }), _jsx(PortalTargetContext.Provider, { value: portalTargetSelector, children: _jsxs(UiStringsContext.Provider, { value: strings, children: [_jsx(ImageOptionsContext.Provider, { value: imageOptions, children: _jsxs(LexicalComposer, { initialConfig: initialConfig, children: [_jsx(Toolbar, { config: config, dictionaryStores: wordStores, onAddAutoCorrect: config.language.autocorrect ? openAutoCorrectFor : undefined, onSave: handleSave, findOpen: openPanel === 'find', onToggleFind: () => togglePanel('find'), spellOpen: openPanel === 'spell', onToggleSpell: () => togglePanel('spell'), autoCorrectOpen: openPanel === 'autocorrect', onToggleAutoCorrect: () => togglePanel('autocorrect'), isDirty: isDirty, showSave: showSave, fontOptions: fontOptions, direction: dir, locale: locale, drafts: autosave
294
307
  ? { currentId: draftId, maxBytes: autosaveMaxBytes, adoptOnRestore: !documentId, onRestored: handleDraftRestored }
295
- : undefined }), documentId && (_jsx(DraftRestore, { draftId: documentId, mode: restoreDraft, initialJson: initialContentJsonRef.current, locale: locale, onRestored: handleDraftRestored })), _jsxs("div", { className: "likhari-canvas-frame", children: [config.findReplace && findOpen && _jsx(FindReplaceBar, { strings: strings, dir: dir, onClose: () => setFindOpen(false) }), config.language.spellCheck && spellOpen && _jsx(SpellcheckPanel, { strings: strings, dir: dir, onClose: () => setSpellOpen(false) }), _jsx("div", { className: "likhari-canvas", children: _jsx(RichTextPlugin, { contentEditable: _jsx(ContentEditable, { className: "likhari-content-editable", dir: dir, spellCheck: false, "aria-label": strings.editor.contentLabel }), placeholder: _jsx("div", { className: "likhari-placeholder", children: resolvedPlaceholder }), ErrorBoundary: LexicalErrorBoundary }) })] }), config.history && _jsx(HistoryPlugin, {}), (config.lists.bullet || config.lists.numbered || config.lists.check) && _jsx(ListPlugin, {}), config.lists.check && _jsx(CheckListPlugin, {}), config.links && _jsx(LinkPlugin, {}), config.links && _jsx(LinkPastePlugin, {}), config.blocks.pageBreak && _jsx(PageBreakPlugin, {}), config.language.spellCheck && _jsx(SpellHighlightPlugin, {}), config.columns && _jsx(LayoutPlugin, {}), config.footnotes && _jsx(FootnotePlugin, {}), config.poetry.enabled && _jsx(PoetryPlugin, {}), config.poetry.enabled && _jsx(PoetryResizer, {}), config.tables && _jsx(TablePlugin, { hasCellMerge: true, hasTabHandler: true }), config.blocks.horizontalRule && _jsx(HorizontalRulePlugin, {}), _jsx(EditorInstancePlugin, { instanceRef: editorInstanceRef }), _jsx(OnChangePlugin, { onChange: handleChange })] }) }), _jsx(LeaveDialog, { opened: leavePrompt !== null, canSave: Boolean(onSave), canSaveDraft: autosave || navigationGuard === 'save-draft', draftFailed: leavePrompt?.draftFailed ?? false, onSave: () => {
308
+ : undefined }), documentId && (_jsx(DraftRestore, { draftId: documentId, mode: restoreDraft, initialJson: initialContentJsonRef.current, locale: locale, onRestored: handleDraftRestored })), _jsxs("div", { className: "likhari-canvas-frame", children: [config.findReplace && openPanel === 'find' && _jsx(FindReplaceBar, { strings: strings, dir: dir, onClose: () => setOpenPanel(null) }), config.language.spellCheck && openPanel === 'spell' && (_jsx(SpellcheckPanel, { strings: strings, dir: dir, dictionaryStores: wordStores, onAddAutoCorrect: config.language.autocorrect ? openAutoCorrectFor : undefined, onClose: () => setOpenPanel(null) })), config.language.autocorrect && openPanel === 'autocorrect' && (_jsx(AutoCorrectPanel, { strings: strings, dir: dir, stores: stores, initialFrom: autoCorrectPrefill?.from, initialLanguage: autoCorrectPrefill?.language, onSaved: () => setAutoCorrectVersion((v) => v + 1), onClose: () => setOpenPanel(null) }, autoCorrectPrefill?.id ?? 0)), _jsx("div", { className: "likhari-canvas", children: _jsx(RichTextPlugin, { contentEditable: _jsx(ContentEditable, { className: "likhari-content-editable", dir: dir, spellCheck: false, "aria-label": strings.editor.contentLabel }), placeholder: _jsx("div", { className: "likhari-placeholder", children: resolvedPlaceholder }), ErrorBoundary: LexicalErrorBoundary }) })] }), config.history && _jsx(HistoryPlugin, {}), (config.lists.bullet || config.lists.numbered || config.lists.check) && _jsx(ListPlugin, {}), config.lists.check && _jsx(CheckListPlugin, {}), config.links && _jsx(LinkPlugin, {}), config.links && _jsx(LinkPastePlugin, {}), config.blocks.pageBreak && _jsx(PageBreakPlugin, {}), config.language.spellCheck && _jsx(SpellHighlightPlugin, {}), (config.language.autocorrect || config.language.textCleanup) && _jsx(AutoCorrectPlugin, { stores: stores, version: autoCorrectVersion, enabled: true, typing: config.language.autocorrect, urduNormalization: urduNormalization, punctuation: punctuation }), config.columns && _jsx(LayoutPlugin, {}), config.footnotes && _jsx(FootnotePlugin, {}), config.poetry.enabled && _jsx(PoetryPlugin, {}), config.poetry.enabled && _jsx(PoetryResizer, {}), config.tables && _jsx(TablePlugin, { hasCellMerge: true, hasTabHandler: true }), config.blocks.horizontalRule && _jsx(HorizontalRulePlugin, {}), _jsx(EditorInstancePlugin, { instanceRef: editorInstanceRef }), _jsx(OnChangePlugin, { onChange: handleChange })] }) }), _jsx(LeaveDialog, { opened: leavePrompt !== null, canSave: Boolean(onSave), canSaveDraft: autosave || navigationGuard === 'save-draft', draftFailed: leavePrompt?.draftFailed ?? false, onSave: () => {
296
309
  handleSave();
297
310
  settleLeave(true);
298
311
  }, onSaveDraft: () => {
@@ -0,0 +1,27 @@
1
+ import { type LexicalCommand } from 'lexical';
2
+ import { type PunctuationOptions } from './punctuationRules.js';
3
+ import { type AutoCorrectStore } from './autoCorrectStores.js';
4
+ import { type UrduNormalizationOptions } from '../normalization/urduNormalize.js';
5
+ /** Keys that end a word: a space or punctuation, in Latin and Arabic-script forms. */
6
+ /** Corrects every word in the document, e.g. after pasting or loading content. */
7
+ export declare const CORRECT_DOCUMENT_COMMAND: LexicalCommand<void>;
8
+ /**
9
+ * Corrects typed words as the user finishes them, the way the reference editor
10
+ * does, but from any set of stores. Corrections are loaded once per change
11
+ * of `stores` or `version` (bump `version` after saving a new correction so
12
+ * the table reloads). The table used depends on the block's direction: RTL
13
+ * blocks use the Urdu and Shahmukhi tables, LTR blocks the English one.
14
+ */
15
+ export declare function AutoCorrectPlugin({ stores, version, enabled, typing, urduNormalization, punctuation, }: {
16
+ stores: AutoCorrectStore[];
17
+ version: number;
18
+ /** Whether the tables and the whole-document command are active. */
19
+ enabled: boolean;
20
+ /** Whether corrections run as the user types (auto-correct), not only on demand. */
21
+ typing: boolean;
22
+ /** Urdu normalisation for right-to-left text; character mapping always applies. */
23
+ urduNormalization?: UrduNormalizationOptions;
24
+ /** Common punctuation fixes (from the bundled Urdu rule list). */
25
+ punctuation?: PunctuationOptions;
26
+ }): null;
27
+ //# sourceMappingURL=AutoCorrectPlugin.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AutoCorrectPlugin.d.ts","sourceRoot":"","sources":["../../src/autocorrect/AutoCorrectPlugin.tsx"],"names":[],"mappings":"AAEA,OAAO,EAQL,KAAK,cAAc,EACpB,MAAM,SAAS,CAAC;AAWjB,OAAO,EAA0B,KAAK,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AACrF,OAAO,EAAuB,KAAK,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AACjF,OAAO,EAAiB,KAAK,wBAAwB,EAAE,MAAM,gCAAgC,CAAC;AAG9F,sFAAsF;AACtF,kFAAkF;AAClF,eAAO,MAAM,wBAAwB,EAAE,cAAc,CAAC,IAAI,CAA6C,CAAC;AAMxG;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,EAChC,MAAM,EACN,OAAO,EACP,OAAO,EACP,MAAM,EACN,iBAAiB,EACjB,WAAW,GACZ,EAAE;IACD,MAAM,EAAE,gBAAgB,EAAE,CAAC;IAC3B,OAAO,EAAE,MAAM,CAAC;IAChB,oEAAoE;IACpE,OAAO,EAAE,OAAO,CAAC;IACjB,oFAAoF;IACpF,MAAM,EAAE,OAAO,CAAC;IAChB,mFAAmF;IACnF,iBAAiB,CAAC,EAAE,wBAAwB,CAAC;IAC7C,kEAAkE;IAClE,WAAW,CAAC,EAAE,kBAAkB,CAAC;CAClC,QA0GA"}
@@ -0,0 +1,124 @@
1
+ import { useEffect, useRef } from 'react';
2
+ import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
3
+ import { $getSelection, $isRangeSelection, COMMAND_PRIORITY_EDITOR, BLUR_COMMAND, COMMAND_PRIORITY_HIGH, KEY_ENTER_COMMAND, createCommand, } from 'lexical';
4
+ import { $correctDocument, $correctPunctuationBeforeCaret, $correctPunctuationDocument, $correctWordBeforeCaret, $correctWordEndingAtCaret, $preservingSelection, $spacePunctuationDocument, } from './autoCorrectActions.js';
5
+ import { $cleanUpText } from '../cleanup/textCleanupActions.js';
6
+ import { activePunctuationRules } from './punctuationRules.js';
7
+ import { loadAutoCorrections } from './autoCorrectStores.js';
8
+ import { normalizeUrdu } from '../normalization/urduNormalize.js';
9
+ /** Keys that end a word: a space or punctuation, in Latin and Arabic-script forms. */
10
+ /** Corrects every word in the document, e.g. after pasting or loading content. */
11
+ export const CORRECT_DOCUMENT_COMMAND = createCommand('CORRECT_DOCUMENT_COMMAND');
12
+ const BOUNDARY_KEYS = new Set([' ', '.', ',', ';', ':', '!', '?', '،', '؛', '؟', '۔']);
13
+ const LANGUAGES = ['en', 'ur', 'pa-shahmukhi'];
14
+ const RTL_LANGUAGES = ['ur', 'pa-shahmukhi'];
15
+ /**
16
+ * Corrects typed words as the user finishes them, the way the reference editor
17
+ * does, but from any set of stores. Corrections are loaded once per change
18
+ * of `stores` or `version` (bump `version` after saving a new correction so
19
+ * the table reloads). The table used depends on the block's direction: RTL
20
+ * blocks use the Urdu and Shahmukhi tables, LTR blocks the English one.
21
+ */
22
+ export function AutoCorrectPlugin({ stores, version, enabled, typing, urduNormalization, punctuation, }) {
23
+ const [editor] = useLexicalComposerContext();
24
+ // Effects below run once per editor, so they reach the latest options through this ref.
25
+ const normalizeOptions = useRef(urduNormalization);
26
+ normalizeOptions.current = urduNormalization;
27
+ const punctuationRules = useRef(activePunctuationRules(punctuation));
28
+ punctuationRules.current = activePunctuationRules(punctuation);
29
+ const normalizeRtl = (text) => normalizeUrdu(text, normalizeOptions.current);
30
+ const tables = useRef(new Map());
31
+ const rtlTable = useRef(new Map());
32
+ useEffect(() => {
33
+ if (!enabled)
34
+ return;
35
+ let active = true;
36
+ Promise.all(LANGUAGES.map(async (language) => [language, await loadAutoCorrections(stores, language)])).then((loaded) => {
37
+ if (!active)
38
+ return;
39
+ tables.current = new Map(loaded);
40
+ // Urdu and Shahmukhi share right-to-left blocks, so both apply there; Urdu wins a clash.
41
+ const combined = new Map();
42
+ for (const language of RTL_LANGUAGES) {
43
+ for (const [from, to] of tables.current.get(language) ?? [])
44
+ if (!combined.has(from))
45
+ combined.set(from, to);
46
+ }
47
+ rtlTable.current = combined;
48
+ });
49
+ return () => {
50
+ active = false;
51
+ };
52
+ }, [stores, version, enabled]);
53
+ useEffect(() => {
54
+ if (!enabled)
55
+ return;
56
+ return editor.registerCommand(CORRECT_DOCUMENT_COMMAND, () => {
57
+ editor.update(() => {
58
+ $preservingSelection(() => {
59
+ $cleanUpText();
60
+ if (punctuation?.spacing)
61
+ $spacePunctuationDocument();
62
+ $correctPunctuationDocument(punctuationRules.current);
63
+ $correctDocument(tables.current.get('en') ?? new Map(), rtlTable.current, normalizeRtl);
64
+ });
65
+ }, { discrete: true });
66
+ return true;
67
+ }, COMMAND_PRIORITY_EDITOR);
68
+ }, [editor, enabled, punctuation?.spacing]);
69
+ useEffect(() => {
70
+ if (!enabled || !typing)
71
+ return;
72
+ const tableFor = (rtl) => (rtl ? rtlTable.current : tables.current.get('en'));
73
+ const onKeyDown = (event) => {
74
+ if (event.isComposing || event.key.length !== 1)
75
+ return;
76
+ const boundary = BOUNDARY_KEYS.has(event.key);
77
+ // The character is inserted after this event; correct once it is there.
78
+ setTimeout(() => {
79
+ editor.update(() => {
80
+ const selection = $getSelection();
81
+ if (!$isRangeSelection(selection))
82
+ return;
83
+ // Punctuation can end in any character (e.g. the letter ه), so every key is checked.
84
+ $correctPunctuationBeforeCaret(punctuationRules.current);
85
+ if (!boundary)
86
+ return;
87
+ const block = selection.anchor.getNode().getTopLevelElement();
88
+ const rtl = block?.getDirection() === 'rtl';
89
+ const table = tableFor(rtl);
90
+ if (table)
91
+ $correctWordBeforeCaret(table, rtl ? normalizeRtl : undefined);
92
+ }, { discrete: true });
93
+ }, 0);
94
+ };
95
+ let root = null;
96
+ const attach = (next) => {
97
+ root?.removeEventListener('keydown', onKeyDown);
98
+ root = next;
99
+ root?.addEventListener('keydown', onKeyDown);
100
+ };
101
+ const unregister = editor.registerRootListener((next) => attach(next));
102
+ // A word with nothing after it (end of line or document) is corrected when the
103
+ // caret leaves it: Enter splits the line, and blur means the user has moved on.
104
+ const correctEndingWord = () => {
105
+ const selection = $getSelection();
106
+ if (!$isRangeSelection(selection))
107
+ return false;
108
+ const rtl = selection.anchor.getNode().getTopLevelElement()?.getDirection() === 'rtl';
109
+ const table = tableFor(rtl);
110
+ if (table)
111
+ $correctWordEndingAtCaret(table, rtl ? normalizeRtl : undefined);
112
+ return false;
113
+ };
114
+ const unregisterEnter = editor.registerCommand(KEY_ENTER_COMMAND, correctEndingWord, COMMAND_PRIORITY_HIGH);
115
+ const unregisterBlur = editor.registerCommand(BLUR_COMMAND, correctEndingWord, COMMAND_PRIORITY_EDITOR);
116
+ return () => {
117
+ unregister();
118
+ unregisterEnter();
119
+ unregisterBlur();
120
+ attach(null);
121
+ };
122
+ }, [editor, enabled, typing]);
123
+ return null;
124
+ }
@@ -0,0 +1,41 @@
1
+ import { type PunctuationRule } from './punctuationRules.js';
2
+ /**
3
+ * Corrects the word that ends just before the caret, when the character just
4
+ * typed (the word boundary: space or punctuation) is not a word character. The
5
+ * correction replaces only the word, and the caret stays right after the
6
+ * boundary. Returns true if a correction was made.
7
+ */
8
+ export declare function $correctWordBeforeCaret(table: Map<string, string>, normalize?: (word: string) => string): boolean;
9
+ /**
10
+ * Corrects every whole word in the document, for text that never went through
11
+ * typing (pasted or loaded content). Words are found per paragraph or cell, so a
12
+ * word split by formatting is still one word. Each block uses the table for its
13
+ * direction. Returns how many words were corrected.
14
+ */
15
+ export declare function $correctDocument(ltr: Map<string, string>, rtl: Map<string, string>, normalizeRtl?: (text: string) => string): number;
16
+ /**
17
+ * Applies a punctuation rule to the text just before the caret, when that text
18
+ * ends with a rule's incorrect form. Longest match first. The caret ends after
19
+ * the replacement.
20
+ */
21
+ export declare function $correctPunctuationBeforeCaret(rules: PunctuationRule[]): boolean;
22
+ /**
23
+ * Applies the punctuation rules to every paragraph and cell in the document.
24
+ * Where two matches overlap, the longer one wins. Returns how many were applied.
25
+ */
26
+ export declare function $correctPunctuationDocument(rules: PunctuationRule[]): number;
27
+ /**
28
+ * Runs `change` and then puts the caret back where it was. Replacements move the
29
+ * selection onto each match they make, so a whole-document pass would otherwise
30
+ * leave the caret at the last match, or at the end of the document.
31
+ */
32
+ export declare function $preservingSelection(change: () => void): void;
33
+ /** Applies urduhack-style punctuation spacing to every text node. Returns how many changed. */
34
+ export declare function $spacePunctuationDocument(): number;
35
+ /**
36
+ * Corrects the word that ends at the caret when nothing follows it, which is the
37
+ * end of a line: the caret is about to leave the line (Enter). The caret ends
38
+ * after the replacement.
39
+ */
40
+ export declare function $correctWordEndingAtCaret(table: Map<string, string>, normalize?: (word: string) => string): boolean;
41
+ //# sourceMappingURL=autoCorrectActions.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"autoCorrectActions.d.ts","sourceRoot":"","sources":["../../src/autocorrect/autoCorrectActions.ts"],"names":[],"mappings":"AAWA,OAAO,EAAwB,KAAK,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAMhF;;;;;GAKG;AACH,wBAAgB,uBAAuB,CAAC,KAAK,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,SAAS,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,MAAM,GAAG,OAAO,CAiCjH;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAC9B,GAAG,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,EACxB,GAAG,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,EACxB,YAAY,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,MAAM,GACtC,MAAM,CA8BR;AAQD;;;;GAIG;AACH,wBAAgB,8BAA8B,CAAC,KAAK,EAAE,eAAe,EAAE,GAAG,OAAO,CAqBhF;AAED;;;GAGG;AACH,wBAAgB,2BAA2B,CAAC,KAAK,EAAE,eAAe,EAAE,GAAG,MAAM,CA2B5E;AAID;;;;GAIG;AACH,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,MAAM,IAAI,GAAG,IAAI,CAoB7D;AAED,+FAA+F;AAC/F,wBAAgB,yBAAyB,IAAI,MAAM,CAWlD;AAED;;;;GAIG;AACH,wBAAgB,yBAAyB,CAAC,KAAK,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,SAAS,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,MAAM,GAAG,OAAO,CAuBnH"}
@@ -0,0 +1,212 @@
1
+ import { $createRangeSelection, $getNodeByKey, $getRoot, $getSelection, $isRangeSelection, $isTextNode, $setSelection, } from 'lexical';
2
+ import { $locate, $replaceMatch, $textGroups } from '../find/findReplaceActions.js';
3
+ import { wordsIn } from '../spellcheck/spellDictionaries.js';
4
+ import { spacePunctuationText } from './punctuationRules.js';
5
+ /** The part of a word that counts as word characters at its end. */
6
+ const WORD_END = /[\p{L}\p{M}'’]+$/u;
7
+ const WORD_CHAR = /[\p{L}\p{M}'’]/u;
8
+ /**
9
+ * Corrects the word that ends just before the caret, when the character just
10
+ * typed (the word boundary: space or punctuation) is not a word character. The
11
+ * correction replaces only the word, and the caret stays right after the
12
+ * boundary. Returns true if a correction was made.
13
+ */
14
+ export function $correctWordBeforeCaret(table, normalize) {
15
+ const selection = $getSelection();
16
+ if (!$isRangeSelection(selection) || !selection.isCollapsed())
17
+ return false;
18
+ const anchor = selection.anchor;
19
+ const node = anchor.getNode();
20
+ if (!$isTextNode(node))
21
+ return false;
22
+ const before = node.getTextContent().slice(0, anchor.offset);
23
+ const boundary = before.slice(-1);
24
+ if (boundary === '' || WORD_CHAR.test(boundary))
25
+ return false;
26
+ const body = before.slice(0, -1);
27
+ const word = WORD_END.exec(body)?.[0];
28
+ if (!word)
29
+ return false;
30
+ const replacement = correctionFor(word, table, normalize);
31
+ if (replacement === undefined || replacement === word)
32
+ return false;
33
+ const start = body.length - word.length;
34
+ $replaceMatch({ anchorKey: node.getKey(), anchorOffset: start, focusKey: node.getKey(), focusOffset: body.length }, replacement);
35
+ // Put the caret back after the boundary the user typed, not at the end of the replacement.
36
+ const after = $getSelection();
37
+ if ($isRangeSelection(after)) {
38
+ const focusNode = after.focus.getNode();
39
+ const caret = after.focus.offset + boundary.length;
40
+ after.anchor.set(focusNode.getKey(), caret, 'text');
41
+ after.focus.set(focusNode.getKey(), caret, 'text');
42
+ }
43
+ return true;
44
+ }
45
+ /**
46
+ * Corrects every whole word in the document, for text that never went through
47
+ * typing (pasted or loaded content). Words are found per paragraph or cell, so a
48
+ * word split by formatting is still one word. Each block uses the table for its
49
+ * direction. Returns how many words were corrected.
50
+ */
51
+ export function $correctDocument(ltr, rtl, normalizeRtl) {
52
+ if (normalizeRtl) {
53
+ for (const node of $getRoot().getAllTextNodes()) {
54
+ if (node.getParentOrThrow().getDirection() !== 'rtl')
55
+ continue;
56
+ const text = node.getTextContent();
57
+ const normalized = normalizeRtl(text);
58
+ if (normalized !== text)
59
+ node.setTextContent(normalized);
60
+ }
61
+ }
62
+ let corrected = 0;
63
+ for (const group of $textGroups()) {
64
+ const rightToLeft = group[0].getParentOrThrow().getDirection() === 'rtl';
65
+ const table = rightToLeft ? rtl : ltr;
66
+ const text = group.map((node) => node.getTextContent()).join('');
67
+ const spans = wordsIn(text)
68
+ .map((span) => ({ span, replacement: correctionFor(span.word, table, rightToLeft ? normalizeRtl : undefined) }))
69
+ .filter((item) => item.replacement !== undefined && item.replacement !== item.span.word);
70
+ // Right to left, so earlier positions stay valid as each word is replaced.
71
+ for (const { span, replacement } of spans.reverse()) {
72
+ const start = $locate(group, span.start, false);
73
+ const end = $locate(group, span.end, true);
74
+ $replaceMatch({ anchorKey: start.key, anchorOffset: start.offset, focusKey: end.key, focusOffset: end.offset }, replacement);
75
+ corrected += 1;
76
+ }
77
+ }
78
+ return corrected;
79
+ }
80
+ /** What a word should become: its normalised form, then any table correction for that. */
81
+ function correctionFor(word, table, normalize) {
82
+ const normalized = normalize ? normalize(word) : word;
83
+ return table.get(normalized) ?? (normalized !== word ? normalized : undefined);
84
+ }
85
+ /**
86
+ * Applies a punctuation rule to the text just before the caret, when that text
87
+ * ends with a rule's incorrect form. Longest match first. The caret ends after
88
+ * the replacement.
89
+ */
90
+ export function $correctPunctuationBeforeCaret(rules) {
91
+ const selection = $getSelection();
92
+ if (!$isRangeSelection(selection) || !selection.isCollapsed())
93
+ return false;
94
+ const anchor = selection.anchor;
95
+ const node = anchor.getNode();
96
+ if (!$isTextNode(node))
97
+ return false;
98
+ const before = node.getTextContent().slice(0, anchor.offset);
99
+ const rule = rules.find((r) => before.endsWith(r.incorrect) && (!r.completeWord || !WORD_CHAR.test(before.charAt(before.length - r.incorrect.length - 1) || ' ')));
100
+ if (!rule)
101
+ return false;
102
+ const start = before.length - rule.incorrect.length;
103
+ $replaceMatch({ anchorKey: node.getKey(), anchorOffset: start, focusKey: node.getKey(), focusOffset: before.length }, rule.correct);
104
+ const after = $getSelection();
105
+ if ($isRangeSelection(after)) {
106
+ const caret = start + rule.correct.length;
107
+ after.anchor.set(node.getKey(), caret, 'text');
108
+ after.focus.set(node.getKey(), caret, 'text');
109
+ }
110
+ return true;
111
+ }
112
+ /**
113
+ * Applies the punctuation rules to every paragraph and cell in the document.
114
+ * Where two matches overlap, the longer one wins. Returns how many were applied.
115
+ */
116
+ export function $correctPunctuationDocument(rules) {
117
+ let applied = 0;
118
+ for (const group of $textGroups()) {
119
+ const text = group.map((node) => node.getTextContent()).join('');
120
+ const candidates = [];
121
+ for (const rule of rules) {
122
+ let index = text.indexOf(rule.incorrect);
123
+ while (index !== -1) {
124
+ candidates.push({ start: index, end: index + rule.incorrect.length, correct: rule.correct });
125
+ index = text.indexOf(rule.incorrect, index + 1);
126
+ }
127
+ }
128
+ // Longest first, then earliest; keep only matches that do not overlap a kept one.
129
+ candidates.sort((a, b) => b.end - b.start - (a.end - a.start) || a.start - b.start);
130
+ const kept = [];
131
+ for (const c of candidates) {
132
+ if (!kept.some((k) => c.start < k.end && k.start < c.end))
133
+ kept.push(c);
134
+ }
135
+ // Right to left, so earlier positions stay valid as each match is replaced.
136
+ for (const c of kept.sort((a, b) => b.start - a.start)) {
137
+ const start = $locate(group, c.start, false);
138
+ const end = $locate(group, c.end, true);
139
+ $replaceMatch({ anchorKey: start.key, anchorOffset: start.offset, focusKey: end.key, focusOffset: end.offset }, c.correct);
140
+ applied += 1;
141
+ }
142
+ }
143
+ return applied;
144
+ }
145
+ /**
146
+ * Runs `change` and then puts the caret back where it was. Replacements move the
147
+ * selection onto each match they make, so a whole-document pass would otherwise
148
+ * leave the caret at the last match, or at the end of the document.
149
+ */
150
+ export function $preservingSelection(change) {
151
+ const before = $getSelection();
152
+ const saved = $isRangeSelection(before)
153
+ ? {
154
+ anchor: { key: before.anchor.key, offset: before.anchor.offset, type: before.anchor.type },
155
+ focus: { key: before.focus.key, offset: before.focus.offset, type: before.focus.type },
156
+ }
157
+ : null;
158
+ change();
159
+ if (!saved) {
160
+ $setSelection(null);
161
+ return;
162
+ }
163
+ if (!$getNodeByKey(saved.anchor.key) || !$getNodeByKey(saved.focus.key))
164
+ return;
165
+ const selection = $createRangeSelection();
166
+ selection.anchor.set(saved.anchor.key, saved.anchor.offset, saved.anchor.type);
167
+ selection.focus.set(saved.focus.key, saved.focus.offset, saved.focus.type);
168
+ $setSelection(selection);
169
+ }
170
+ /** Applies urduhack-style punctuation spacing to every text node. Returns how many changed. */
171
+ export function $spacePunctuationDocument() {
172
+ let changed = 0;
173
+ for (const node of $getRoot().getAllTextNodes()) {
174
+ const text = node.getTextContent();
175
+ const spaced = spacePunctuationText(text);
176
+ if (spaced !== text) {
177
+ node.setTextContent(spaced);
178
+ changed += 1;
179
+ }
180
+ }
181
+ return changed;
182
+ }
183
+ /**
184
+ * Corrects the word that ends at the caret when nothing follows it, which is the
185
+ * end of a line: the caret is about to leave the line (Enter). The caret ends
186
+ * after the replacement.
187
+ */
188
+ export function $correctWordEndingAtCaret(table, normalize) {
189
+ const selection = $getSelection();
190
+ if (!$isRangeSelection(selection) || !selection.isCollapsed())
191
+ return false;
192
+ const anchor = selection.anchor;
193
+ const node = anchor.getNode();
194
+ if (!$isTextNode(node))
195
+ return false;
196
+ const before = node.getTextContent().slice(0, anchor.offset);
197
+ const word = WORD_END.exec(before)?.[0];
198
+ if (!word)
199
+ return false;
200
+ const replacement = correctionFor(word, table, normalize);
201
+ if (replacement === undefined || replacement === word)
202
+ return false;
203
+ const start = before.length - word.length;
204
+ $replaceMatch({ anchorKey: node.getKey(), anchorOffset: start, focusKey: node.getKey(), focusOffset: before.length }, replacement);
205
+ const after = $getSelection();
206
+ if ($isRangeSelection(after)) {
207
+ const caret = start + replacement.length;
208
+ after.anchor.set(node.getKey(), caret, 'text');
209
+ after.focus.set(node.getKey(), caret, 'text');
210
+ }
211
+ return true;
212
+ }
@@ -0,0 +1,52 @@
1
+ import type { SpellLanguage } from '../spellcheck/spellDictionaries.js';
2
+ /** One correction: a typed word and what it should become. */
3
+ export interface AutoCorrectEntry {
4
+ from: string;
5
+ to: string;
6
+ }
7
+ /**
8
+ * Where auto-corrections come from and go. The host supplies any number of
9
+ * stores; corrections from all of them are merged, and when two stores
10
+ * correct the same word, the one listed first wins. New corrections are
11
+ * appended to the first store that accepts writes.
12
+ */
13
+ export interface AutoCorrectStore {
14
+ readonly id: string;
15
+ /** A store without `append` (or with `readOnly`) is never written to. */
16
+ readonly readOnly?: boolean;
17
+ load(language: SpellLanguage): Promise<AutoCorrectEntry[]>;
18
+ append?(language: SpellLanguage, entry: AutoCorrectEntry): Promise<void>;
19
+ }
20
+ /**
21
+ * Corrections kept in this browser's localStorage. Storage can be disabled or
22
+ * full, so every access is guarded: a failure means "no corrections", never a
23
+ * broken editor.
24
+ */
25
+ export declare function localStorageAutoCorrectStore(options?: {
26
+ key?: string;
27
+ }): AutoCorrectStore;
28
+ /**
29
+ * Corrections served by an API. `load` calls `GET {url}?language=xx` and expects
30
+ * a JSON array of `{ from, to }`; `append` calls `POST {url}` with
31
+ * `{ language, from, to }`. Credentials and headers belong to the host.
32
+ */
33
+ export declare function apiAutoCorrectStore(options: {
34
+ id?: string;
35
+ url: string;
36
+ headers?: Record<string, string>;
37
+ fetch?: typeof fetch;
38
+ }): AutoCorrectStore;
39
+ /**
40
+ * Corrections from a JSON file the host publishes — a list of `{ language, from, to }`.
41
+ * Read-only: it has no way to write back to a static file.
42
+ */
43
+ export declare function fileAutoCorrectStore(options: {
44
+ id?: string;
45
+ url: string;
46
+ fetch?: typeof fetch;
47
+ }): AutoCorrectStore;
48
+ /** Corrections for one language, merged across stores. Failing stores are skipped. */
49
+ export declare function loadAutoCorrections(stores: AutoCorrectStore[], language: SpellLanguage): Promise<Map<string, string>>;
50
+ /** Saves a correction to the first store that accepts writes. Returns false if none does. */
51
+ export declare function appendAutoCorrection(stores: AutoCorrectStore[], language: SpellLanguage, entry: AutoCorrectEntry): Promise<boolean>;
52
+ //# sourceMappingURL=autoCorrectStores.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"autoCorrectStores.d.ts","sourceRoot":"","sources":["../../src/autocorrect/autoCorrectStores.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,iCAAiC,CAAC;AAErE,8DAA8D;AAC9D,MAAM,WAAW,gBAAgB;IAC/B,IAAI,EAAE,MAAM,CAAC;IACb,EAAE,EAAE,MAAM,CAAC;CACZ;AAED;;;;;GAKG;AACH,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,yEAAyE;IACzE,QAAQ,CAAC,QAAQ,CAAC,EAAE,OAAO,CAAC;IAC5B,IAAI,CAAC,QAAQ,EAAE,aAAa,GAAG,OAAO,CAAC,gBAAgB,EAAE,CAAC,CAAC;IAC3D,MAAM,CAAC,CAAC,QAAQ,EAAE,aAAa,EAAE,KAAK,EAAE,gBAAgB,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAC1E;AAkBD;;;;GAIG;AACH,wBAAgB,4BAA4B,CAAC,OAAO,GAAE;IAAE,GAAG,CAAC,EAAE,MAAM,CAAA;CAAO,GAAG,gBAAgB,CA6B7F;AAED;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,OAAO,EAAE;IAC3C,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACjC,KAAK,CAAC,EAAE,OAAO,KAAK,CAAC;CACtB,GAAG,gBAAgB,CAmBnB;AAED;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,OAAO,EAAE;IAAE,EAAE,CAAC,EAAE,MAAM,CAAC;IA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