@bobfrankston/rmfmail 1.0.677 → 1.0.679

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,383 @@
1
+ /**
2
+ * Live spell-check for the TinyMCE compose editor.
3
+ *
4
+ * Why this exists: WebView2's built-in spell checker isn't reliably
5
+ * enabled on our msger-hosted compose iframe (see msger main.rs around
6
+ * AreDefaultContextMenusEnabled — the comment claims it leaves
7
+ * defaults for spell-suggest menus, but `IsSpellcheckEnabled` is never
8
+ * explicitly set and the default varies by WebView2 runtime). Rather
9
+ * than depend on the host, we ship a JS spellchecker (`nspell`) + the
10
+ * standard `dictionary-en` Hunspell files.
11
+ *
12
+ * Two layers:
13
+ * 1. Live decoration — words that nspell flags get wrapped in a
14
+ * `<span data-mailx-spellerror="1">` while editing. CSS gives them
15
+ * a wavy red underline (text-decoration: underline wavy #d33). The
16
+ * markers are stripped at serialization time so they never reach
17
+ * the sent message or the saved draft.
18
+ * 2. Right-click — clicking on a marker shows our own popup with
19
+ * suggestions plus "Add to dictionary" / "Ignore (this session)".
20
+ *
21
+ * Decoration runs:
22
+ * - Once after the editor inits + dict loads (initial scan of pre-
23
+ * populated quote / signature, though we skip blockquote content).
24
+ * - Debounced after every input (~500 ms idle).
25
+ * - After setContent (paste, reply-quote insertion, …).
26
+ *
27
+ * The decoration walker:
28
+ * - Skips `<blockquote>` (the quoted reply — not the user's words),
29
+ * `<code>`, `<pre>`, `<a>` (URLs aren't natural-language words),
30
+ * and content inside any existing marker (so re-scans don't double-
31
+ * wrap).
32
+ * - Uses TinyMCE's `undoManager.ignore` + selection bookmarks so the
33
+ * decoration mutations don't pollute undo and don't move the caret.
34
+ *
35
+ * Dictionary persistence:
36
+ * - User additions: `localStorage["mailx-user-dict"]` (a JSON array).
37
+ * - "Ignore this session": in-memory only via `nspell.add()`.
38
+ */
39
+ // @ts-expect-error — nspell ships no type defs. Treated as `any`; the
40
+ // surface we use (`new NSpell({aff, dic})`, `.correct`, `.suggest`,
41
+ // `.add`) is small and stable.
42
+ import NSpell from "nspell";
43
+ type NSpell = any;
44
+
45
+ const USER_DICT_KEY = "mailx-user-dict";
46
+ const MARKER_ATTR = "data-mailx-spellerror";
47
+ const DECORATE_DEBOUNCE_MS = 500;
48
+ const MIN_WORD_LEN = 3;
49
+ const SKIP_TAGS = new Set(["BLOCKQUOTE", "CODE", "PRE", "A", "SCRIPT", "STYLE", "KBD", "SAMP", "VAR"]);
50
+
51
+ let spellPromise: Promise<NSpell> | null = null;
52
+ async function getSpell(): Promise<NSpell> {
53
+ if (spellPromise) return spellPromise;
54
+ spellPromise = (async () => {
55
+ const [affRes, dicRes] = await Promise.all([
56
+ fetch("../lib/dict/en.aff"),
57
+ fetch("../lib/dict/en.dic"),
58
+ ]);
59
+ if (!affRes.ok || !dicRes.ok) {
60
+ throw new Error(`spellcheck: dict fetch failed (aff=${affRes.status} dic=${dicRes.status})`);
61
+ }
62
+ const [aff, dic] = await Promise.all([affRes.text(), dicRes.text()]);
63
+ const sp = new NSpell({ aff, dic });
64
+ try {
65
+ const raw = localStorage.getItem(USER_DICT_KEY);
66
+ if (raw) for (const w of JSON.parse(raw) as string[]) sp.add(w);
67
+ } catch { /* corrupt entry — start clean */ }
68
+ return sp;
69
+ })();
70
+ return spellPromise;
71
+ }
72
+
73
+ function addToUserDict(word: string, sp: NSpell): void {
74
+ try {
75
+ const raw = localStorage.getItem(USER_DICT_KEY);
76
+ const arr = raw ? (JSON.parse(raw) as string[]) : [];
77
+ if (arr.includes(word)) return;
78
+ arr.push(word);
79
+ localStorage.setItem(USER_DICT_KEY, JSON.stringify(arr));
80
+ } catch { /* */ }
81
+ sp.add(word);
82
+ }
83
+
84
+ // ── Live decoration ───────────────────────────────────────────────
85
+
86
+ /** Walk the editor body, wrap newly-misspelled words, unwrap markers
87
+ * that are now correct. Mutations are wrapped in undoManager.ignore so
88
+ * they don't pollute undo history; selection is preserved via a
89
+ * TinyMCE bookmark. */
90
+ function decorate(editor: any, sp: NSpell): void {
91
+ const body: HTMLElement | null = editor.getBody?.();
92
+ const doc: Document | null = editor.getDoc?.();
93
+ if (!body || !doc) return;
94
+
95
+ // Don't fight active typing — if the caret is INSIDE a current
96
+ // misspelling marker, the user is mid-correction. Let them finish.
97
+ // We'll re-scan after the debounce on their next pause.
98
+ const sel = doc.getSelection();
99
+ if (sel && sel.rangeCount > 0) {
100
+ const focus = sel.focusNode as Node | null;
101
+ let p: Node | null = focus;
102
+ while (p && p !== body) {
103
+ if (p.nodeType === Node.ELEMENT_NODE && (p as Element).hasAttribute?.(MARKER_ATTR)) {
104
+ // Cursor inside a marker — skip this pass; re-decorate
105
+ // when they next stop typing.
106
+ return;
107
+ }
108
+ p = p.parentNode;
109
+ }
110
+ }
111
+
112
+ const bookmark = editor.selection?.getBookmark?.(2);
113
+ try {
114
+ editor.undoManager?.ignore?.(() => {
115
+ // 1. Unwrap any existing markers — we'll re-wrap fresh based
116
+ // on the current content. Cheaper than incremental update
117
+ // and avoids stale markers if the user fixed a word
118
+ // without going through our menu (just retyped it).
119
+ const old = body.querySelectorAll(`span[${MARKER_ATTR}]`);
120
+ for (const m of old) {
121
+ const parent = m.parentNode;
122
+ if (!parent) continue;
123
+ while (m.firstChild) parent.insertBefore(m.firstChild, m);
124
+ parent.removeChild(m);
125
+ }
126
+ // Merge text nodes that were split by the now-removed spans.
127
+ body.normalize();
128
+
129
+ // 2. Walk text nodes and collect words to wrap.
130
+ const walker = doc.createTreeWalker(body, NodeFilter.SHOW_TEXT, {
131
+ acceptNode(node) {
132
+ let p: Node | null = node.parentNode;
133
+ while (p && p !== body) {
134
+ if (p.nodeType === Node.ELEMENT_NODE && SKIP_TAGS.has((p as Element).tagName)) {
135
+ return NodeFilter.FILTER_REJECT;
136
+ }
137
+ p = p.parentNode;
138
+ }
139
+ return NodeFilter.FILTER_ACCEPT;
140
+ },
141
+ });
142
+ type Hit = { node: Text; start: number; end: number };
143
+ const hits: Hit[] = [];
144
+ let n: Node | null = walker.nextNode();
145
+ // Letter / digit / apostrophe / hyphen — tokenize words via
146
+ // Unicode-aware regex so we don't false-flag accented words.
147
+ const WORD_RE = /[\p{L}][\p{L}'’\-]*/gu;
148
+ while (n) {
149
+ const tn = n as Text;
150
+ const text = tn.data;
151
+ let m: RegExpExecArray | null;
152
+ WORD_RE.lastIndex = 0;
153
+ while ((m = WORD_RE.exec(text)) !== null) {
154
+ const word = m[0];
155
+ if (word.length < MIN_WORD_LEN) continue;
156
+ if (sp.correct(word)) continue;
157
+ hits.push({ node: tn, start: m.index, end: m.index + word.length });
158
+ }
159
+ n = walker.nextNode();
160
+ }
161
+
162
+ // 3. Wrap hits in reverse order — wrapping a span splits the
163
+ // text node, which would invalidate earlier offsets. Going
164
+ // right-to-left keeps the not-yet-touched offsets valid.
165
+ hits.reverse();
166
+ for (const h of hits) {
167
+ const range = doc.createRange();
168
+ range.setStart(h.node, h.start);
169
+ range.setEnd(h.node, h.end);
170
+ const span = doc.createElement("span");
171
+ span.setAttribute(MARKER_ATTR, "1");
172
+ try { range.surroundContents(span); }
173
+ catch { /* range spans a node boundary — rare; skip */ }
174
+ }
175
+ });
176
+ } finally {
177
+ if (bookmark) try { editor.selection?.moveToBookmark?.(bookmark); } catch { /* */ }
178
+ }
179
+ }
180
+
181
+ /** Inject the wavy-red CSS into the editor iframe. */
182
+ function installDecorationStyle(editor: any): void {
183
+ const doc: Document | null = editor.getDoc?.();
184
+ if (!doc) return;
185
+ if (doc.getElementById("mailx-spell-style")) return;
186
+ const style = doc.createElement("style");
187
+ style.id = "mailx-spell-style";
188
+ style.textContent = `
189
+ span[${MARKER_ATTR}] {
190
+ text-decoration: underline wavy #d33;
191
+ text-decoration-skip-ink: none;
192
+ text-underline-offset: 2px;
193
+ /* No background — keeps the styling subtle, like a native
194
+ * spell underline, not a Find-highlight. */
195
+ background: transparent;
196
+ }
197
+ `;
198
+ doc.head.appendChild(style);
199
+ }
200
+
201
+ /** Strip decoration markers from serialized output. TinyMCE fires
202
+ * attribute filters during getContent / draft-save; this filter
203
+ * unwraps the span so the saved/sent HTML carries only the text. */
204
+ function installSerializerFilter(editor: any): void {
205
+ if ((editor as any).__mailxSpellSerializerWired) return;
206
+ (editor as any).__mailxSpellSerializerWired = true;
207
+ try {
208
+ editor.serializer.addAttributeFilter(MARKER_ATTR, (nodes: any[]) => {
209
+ for (const node of nodes) {
210
+ // TinyMCE's html-node API: `unwrap()` replaces the node
211
+ // with its children. Exactly what we want — keep the
212
+ // text, drop the span.
213
+ if (typeof node.unwrap === "function") node.unwrap();
214
+ }
215
+ });
216
+ } catch (e) {
217
+ console.warn("[spellcheck] serializer filter setup failed:", e);
218
+ }
219
+ }
220
+
221
+ // ── Context menu ──────────────────────────────────────────────────
222
+
223
+ function showSuggestionsMenu(
224
+ parentDoc: Document, x: number, y: number,
225
+ items: Array<{ label: string; action: () => void; emphasized?: boolean; separator?: boolean }>,
226
+ ): void {
227
+ parentDoc.getElementById("mailx-spell-menu")?.remove();
228
+ const menu = parentDoc.createElement("div");
229
+ menu.id = "mailx-spell-menu";
230
+ menu.style.cssText = `
231
+ position: fixed;
232
+ left: ${x}px; top: ${y}px;
233
+ z-index: 10000;
234
+ background: var(--color-bg, #fff);
235
+ color: var(--color-text, #222);
236
+ border: 1px solid var(--color-border, #ccc);
237
+ border-radius: 6px;
238
+ box-shadow: 0 4px 16px rgba(0,0,0,0.18);
239
+ padding: 4px 0;
240
+ font: 13px system-ui, sans-serif;
241
+ min-width: 180px;
242
+ max-width: 320px;
243
+ `;
244
+ for (const it of items) {
245
+ if (it.separator) {
246
+ const sep = parentDoc.createElement("div");
247
+ sep.style.cssText = "border-top:1px solid var(--color-border,#ddd); margin: 4px 0;";
248
+ menu.appendChild(sep);
249
+ continue;
250
+ }
251
+ const btn = parentDoc.createElement("button");
252
+ btn.type = "button";
253
+ btn.textContent = it.label;
254
+ btn.style.cssText = `
255
+ display: block; width: 100%; text-align: left;
256
+ padding: 5px 12px; border: none; background: none;
257
+ color: inherit; cursor: pointer; font: inherit;
258
+ ${it.emphasized ? "font-weight: 600;" : ""}
259
+ `;
260
+ btn.addEventListener("mouseenter", () => { btn.style.background = "var(--color-bg-hover, #eef)"; });
261
+ btn.addEventListener("mouseleave", () => { btn.style.background = "none"; });
262
+ btn.addEventListener("click", () => {
263
+ try { it.action(); } finally { menu.remove(); }
264
+ });
265
+ menu.appendChild(btn);
266
+ }
267
+ parentDoc.body.appendChild(menu);
268
+ const r = menu.getBoundingClientRect();
269
+ if (r.right > window.innerWidth) menu.style.left = `${Math.max(8, window.innerWidth - r.width - 8)}px`;
270
+ if (r.bottom > window.innerHeight) menu.style.top = `${Math.max(8, window.innerHeight - r.height - 8)}px`;
271
+ const dismiss = (e: Event) => {
272
+ if (e.type === "keydown" && (e as KeyboardEvent).key !== "Escape") return;
273
+ if (e.type === "mousedown" && menu.contains(e.target as Node)) return;
274
+ menu.remove();
275
+ parentDoc.removeEventListener("mousedown", dismiss, true);
276
+ parentDoc.removeEventListener("keydown", dismiss, true);
277
+ };
278
+ setTimeout(() => {
279
+ parentDoc.addEventListener("mousedown", dismiss, true);
280
+ parentDoc.addEventListener("keydown", dismiss, true);
281
+ }, 0);
282
+ }
283
+
284
+ /** Replace a misspelling marker span with the correction. Done via
285
+ * range-based selection + insertText so TinyMCE's undo stack and
286
+ * dirty-tracking pick it up properly. */
287
+ function replaceMarker(editor: any, marker: HTMLElement, replacement: string): void {
288
+ const doc: Document = editor.getDoc();
289
+ const range = doc.createRange();
290
+ range.selectNode(marker);
291
+ const sel = doc.getSelection();
292
+ if (!sel) return;
293
+ sel.removeAllRanges();
294
+ sel.addRange(range);
295
+ try {
296
+ if (!doc.execCommand("insertText", false, replacement)) {
297
+ range.deleteContents();
298
+ range.insertNode(doc.createTextNode(replacement));
299
+ }
300
+ } catch {
301
+ range.deleteContents();
302
+ range.insertNode(doc.createTextNode(replacement));
303
+ }
304
+ }
305
+
306
+ // ── Public entry point ────────────────────────────────────────────
307
+
308
+ /** Wire the spell-check into a TinyMCE editor instance. Idempotent. */
309
+ export function wireSpellcheck(editor: any): void {
310
+ if ((editor as any).__mailxSpellWired) return;
311
+ (editor as any).__mailxSpellWired = true;
312
+
313
+ let sp: NSpell | null = null;
314
+ let decorateTimer: ReturnType<typeof setTimeout> | null = null;
315
+ const scheduleDecorate = (): void => {
316
+ if (!sp) return;
317
+ if (decorateTimer) clearTimeout(decorateTimer);
318
+ decorateTimer = setTimeout(() => {
319
+ decorateTimer = null;
320
+ if (sp) decorate(editor, sp);
321
+ }, DECORATE_DEBOUNCE_MS);
322
+ };
323
+
324
+ // Kick off the dictionary load. First decoration runs as soon as
325
+ // it resolves; subsequent runs are triggered by editor events.
326
+ getSpell().then((loaded) => {
327
+ sp = loaded;
328
+ installDecorationStyle(editor);
329
+ installSerializerFilter(editor);
330
+ decorate(editor, loaded);
331
+ }).catch((err) => {
332
+ console.error("[spellcheck] dict load failed:", err);
333
+ });
334
+
335
+ // Re-decorate on edits / paste / content swap. `nodechange` covers
336
+ // most of these; `input` catches typing in finer-grained events on
337
+ // some TinyMCE versions.
338
+ editor.on("input nodechange setcontent paste keyup", scheduleDecorate);
339
+
340
+ // Right-click handler. If the click landed on a marker span, show
341
+ // suggestions; otherwise let the default menu (WebView2's) fire.
342
+ const iframeDoc: Document = editor.getDoc();
343
+ iframeDoc.addEventListener("contextmenu", (ev: Event) => {
344
+ const e = ev as MouseEvent;
345
+ const target = e.target as HTMLElement | null;
346
+ if (!target) return;
347
+ const marker = target.closest?.(`span[${MARKER_ATTR}]`) as HTMLElement | null;
348
+ if (!marker) return; // not on a misspelled word — default menu fires
349
+ const word = marker.textContent || "";
350
+ if (!word || !sp) return;
351
+ e.preventDefault();
352
+ e.stopPropagation();
353
+ const sugs: string[] = sp.suggest(word).slice(0, 7);
354
+ const iframeEl = editor.iframeElement as HTMLIFrameElement | undefined;
355
+ const iframeRect = iframeEl ? iframeEl.getBoundingClientRect() : { left: 0, top: 0 };
356
+ const items: Array<{ label: string; action: () => void; emphasized?: boolean; separator?: boolean }> = [];
357
+ if (sugs.length === 0) {
358
+ items.push({ label: "(no suggestions)", action: () => { /* */ } });
359
+ } else {
360
+ for (const s of sugs) {
361
+ items.push({
362
+ label: s,
363
+ emphasized: true,
364
+ action: () => {
365
+ replaceMarker(editor, marker, s);
366
+ // Re-decorate so the replacement is checked too.
367
+ scheduleDecorate();
368
+ },
369
+ });
370
+ }
371
+ }
372
+ items.push({ label: "", action: () => { /* */ }, separator: true });
373
+ items.push({
374
+ label: `Add "${word}" to dictionary`,
375
+ action: () => { if (sp) addToUserDict(word, sp); scheduleDecorate(); },
376
+ });
377
+ items.push({
378
+ label: "Ignore (this session)",
379
+ action: () => { if (sp) sp.add(word); scheduleDecorate(); },
380
+ });
381
+ showSuggestionsMenu(document, iframeRect.left + e.clientX, iframeRect.top + e.clientY, items);
382
+ }, true);
383
+ }
@@ -0,0 +1,205 @@
1
+ SET UTF-8
2
+ TRY esianrtolcdugmphbyfvkwzESIANRTOLCDUGMPHBYFVKWZ'
3
+ ICONV 1
4
+ ICONV ’ '
5
+ NOSUGGEST !
6
+
7
+ # ordinal numbers
8
+ COMPOUNDMIN 1
9
+ # only in compounds: 1th, 2th, 3th
10
+ ONLYINCOMPOUND c
11
+ # compound rules:
12
+ # 1. [0-9]*1[0-9]th (10th, 11th, 12th, 56714th, etc.)
13
+ # 2. [0-9]*[02-9](1st|2nd|3rd|[4-9]th) (21st, 22nd, 123rd, 1234th, etc.)
14
+ COMPOUNDRULE 2
15
+ COMPOUNDRULE n*1t
16
+ COMPOUNDRULE n*mp
17
+ WORDCHARS 0123456789
18
+
19
+ PFX A Y 1
20
+ PFX A 0 re .
21
+
22
+ PFX I Y 1
23
+ PFX I 0 in .
24
+
25
+ PFX U Y 1
26
+ PFX U 0 un .
27
+
28
+ PFX C Y 1
29
+ PFX C 0 de .
30
+
31
+ PFX E Y 1
32
+ PFX E 0 dis .
33
+
34
+ PFX F Y 1
35
+ PFX F 0 con .
36
+
37
+ PFX K Y 1
38
+ PFX K 0 pro .
39
+
40
+ SFX V N 2
41
+ SFX V e ive e
42
+ SFX V 0 ive [^e]
43
+
44
+ SFX N Y 3
45
+ SFX N e ion e
46
+ SFX N y ication y
47
+ SFX N 0 en [^ey]
48
+
49
+ SFX X Y 3
50
+ SFX X e ions e
51
+ SFX X y ications y
52
+ SFX X 0 ens [^ey]
53
+
54
+ SFX H N 2
55
+ SFX H y ieth y
56
+ SFX H 0 th [^y]
57
+
58
+ SFX Y Y 1
59
+ SFX Y 0 ly .
60
+
61
+ SFX G Y 2
62
+ SFX G e ing e
63
+ SFX G 0 ing [^e]
64
+
65
+ SFX J Y 2
66
+ SFX J e ings e
67
+ SFX J 0 ings [^e]
68
+
69
+ SFX D Y 4
70
+ SFX D 0 d e
71
+ SFX D y ied [^aeiou]y
72
+ SFX D 0 ed [^ey]
73
+ SFX D 0 ed [aeiou]y
74
+
75
+ SFX T N 4
76
+ SFX T 0 st e
77
+ SFX T y iest [^aeiou]y
78
+ SFX T 0 est [aeiou]y
79
+ SFX T 0 est [^ey]
80
+
81
+ SFX R Y 4
82
+ SFX R 0 r e
83
+ SFX R y ier [^aeiou]y
84
+ SFX R 0 er [aeiou]y
85
+ SFX R 0 er [^ey]
86
+
87
+ SFX Z Y 4
88
+ SFX Z 0 rs e
89
+ SFX Z y iers [^aeiou]y
90
+ SFX Z 0 ers [aeiou]y
91
+ SFX Z 0 ers [^ey]
92
+
93
+ SFX S Y 4
94
+ SFX S y ies [^aeiou]y
95
+ SFX S 0 s [aeiou]y
96
+ SFX S 0 es [sxzh]
97
+ SFX S 0 s [^sxzhy]
98
+
99
+ SFX P Y 3
100
+ SFX P y iness [^aeiou]y
101
+ SFX P 0 ness [aeiou]y
102
+ SFX P 0 ness [^y]
103
+
104
+ SFX M Y 1
105
+ SFX M 0 's .
106
+
107
+ SFX B Y 3
108
+ SFX B 0 able [^aeiou]
109
+ SFX B 0 able ee
110
+ SFX B e able [^aeiou]e
111
+
112
+ SFX L Y 1
113
+ SFX L 0 ment .
114
+
115
+ REP 90
116
+ REP a ei
117
+ REP ei a
118
+ REP a ey
119
+ REP ey a
120
+ REP ai ie
121
+ REP ie ai
122
+ REP alot a_lot
123
+ REP are air
124
+ REP are ear
125
+ REP are eir
126
+ REP air are
127
+ REP air ere
128
+ REP ere air
129
+ REP ere ear
130
+ REP ere eir
131
+ REP ear are
132
+ REP ear air
133
+ REP ear ere
134
+ REP eir are
135
+ REP eir ere
136
+ REP ch te
137
+ REP te ch
138
+ REP ch ti
139
+ REP ti ch
140
+ REP ch tu
141
+ REP tu ch
142
+ REP ch s
143
+ REP s ch
144
+ REP ch k
145
+ REP k ch
146
+ REP f ph
147
+ REP ph f
148
+ REP gh f
149
+ REP f gh
150
+ REP i igh
151
+ REP igh i
152
+ REP i uy
153
+ REP uy i
154
+ REP i ee
155
+ REP ee i
156
+ REP j di
157
+ REP di j
158
+ REP j gg
159
+ REP gg j
160
+ REP j ge
161
+ REP ge j
162
+ REP s ti
163
+ REP ti s
164
+ REP s ci
165
+ REP ci s
166
+ REP k cc
167
+ REP cc k
168
+ REP k qu
169
+ REP qu k
170
+ REP kw qu
171
+ REP o eau
172
+ REP eau o
173
+ REP o ew
174
+ REP ew o
175
+ REP oo ew
176
+ REP ew oo
177
+ REP ew ui
178
+ REP ui ew
179
+ REP oo ui
180
+ REP ui oo
181
+ REP ew u
182
+ REP u ew
183
+ REP oo u
184
+ REP u oo
185
+ REP u oe
186
+ REP oe u
187
+ REP u ieu
188
+ REP ieu u
189
+ REP ue ew
190
+ REP ew ue
191
+ REP uff ough
192
+ REP oo ieu
193
+ REP ieu oo
194
+ REP ier ear
195
+ REP ear ier
196
+ REP ear air
197
+ REP air ear
198
+ REP w qu
199
+ REP qu w
200
+ REP z ss
201
+ REP ss z
202
+ REP shun tion
203
+ REP shun sion
204
+ REP shun cion
205
+ REP size cise