Files
discord-decomp/lib/spellcheck/Spellchecker.tsx
T
2026-11-01 12:12:55 +02:00

181 lines
4.9 KiB
TypeScript

// Metro module 5193 (f5194).
import _slicedToArray from "../../_bundle/_slicedToArray";
import LocaleStore from "../../modules/user_settings/LocaleStore";
import Logger from "../../modules/debug/Logger";
import DiscordNative from "../DiscordNative";
import fallbackLocales from "./fallbackLocales";
import { parse } from "../../_bundle/Module5195";
import LanguageDetector from "./LanguageDetector";
import Module12 from "../../_bundle/Module12";
import * as _Module1859 from "../../_bundle/Module1859";
import { isNotNullish } from "../../utils/GlobalUtils";
function $mk114669($env) {
return function attachToInput(a0) {
let env31364 = {
$parent: $env
};
env31364[0] = a0;
if (document.body != null) {
document.body.addEventListener("beforeinput", (a0) => (0, env31364.$parent[10])(env31364[0], a0.target), true);
}
};
}
let c3137_6, c3137_7, c3137_10;
let env3137 = {};
const normalizeLocale = function normalizeLocale(a0) {
let r11 = fallbackLocales[a0];
if (r11 == null) {
r11 = a0;
}
const r18 = parse(r11.replace(/[_-]/g, "-"));
const r9 = r11;
if (r18 != null && r18.langtag.language != null && r18.langtag.region != null) {
const langtag = r18.langtag;
const { language, region } = langtag;
return `${language.language.toLowerCase()}-${region.toUpperCase()}`;
}
c3137_6.error(`${r9} is not a valid locale.`);
};
c3137_6 = new Logger("Spellchecker");
c3137_7 = DiscordNative?.spellCheck;
export class Spellchecker {
constructor(a0) {
let c14721_0 = a0;
this._enabled = true;
this.misspelledWord = "";
this.corrections = [];
const r20 = _slicedToArray(LocaleStore.locale.split("-"), 2);
const r21 = r20[0];
let c14721_1 = r21;
this.regionPreference = r20[1];
const availableLanguages = this.getAvailableLanguages(a0);
this.languageDetector = new LanguageDetector(r21, (a0) => {
const r12 = `${a0}-${this.regionPreference}`;
if (c14721_0.indexOf(r12) !== -1) {
this.setLocale(r12);
} else {
let r17 = availableLanguages[a0];
if (r17 == null) {
r17 = fallbackLocales[c14721_1];
}
if (r17 != null) {
this.setLocale(r17);
}
}
});
c3137_7.on("spellcheck-result", (a0, a1) => {
let misspelledWord = a0;
if (a0 == null) {
misspelledWord = "";
}
let corrections = a1;
this.misspelledWord = misspelledWord;
if (a1 == null) {
corrections = [];
}
this.corrections = corrections;
});
}
get enabled() {
return this._enabled;
}
set enabled(a0) {
this._enabled = a0;
}
setLearnedWords(a0) {
c3137_7.setLearnedWords(a0);
}
setLocale(a0) {
const r6 = c3137_7.setLocale(a0);
if (r6 != null) {
r6.then((result) => {
c3137_6.info(`Switching to ${a0}`, result ? "(available)" : "(unavailable)");
});
}
}
setAppLocale(a0) {
this.regionPreference = a0.split("-")[1];
}
detectLanguage(a0) {
if (this.enabled) {
this.languageDetector.process(a0);
}
}
getAvailableLanguages(a0) {
const r0 = {};
let c14723_0 = r0;
a0.forEach((item) => {
const [r5] = item.split("-");
let r13 = c14723_0[r5];
if (r13 == null) {
r13 = item;
}
c14723_0[r5] = r13;
});
return r0;
}
isMisspelled(a0) {
return this.misspelledWord !== "" && a0 === this.misspelledWord;
}
getCorrectionsForMisspelling(a0, a1) {
return this.isMisspelled(a0, a1) ? this.corrections : [];
}
getCachedMisspelling() {
return {
misspelledWord: this.misspelledWord,
corrections: this.corrections
};
}
replaceMisspelling(a0) {
c3137_7.replaceMisspelling(a0);
}
}
c3137_10 = Module12.debounce((a0, a1) => {
let r4;
L7: {
const r0 = a1 == null;
r4 = null;
if (!r0) {
if (!_Module1859.isElement(a1, HTMLInputElement) && !_Module1859.isElement(a1, HTMLTextAreaElement)) {
const isElement = _Module1859.isElement(a1);
r4 = void 0;
if (!isElement) {
break L7;
}
const hasAttribute = a1.hasAttribute("contenteditable");
r4 = void 0;
if (!hasAttribute) {
break L7;
}
r4 = a1.textContent;
break L7;
}
r4 = a1.value;
}
}
if (r4 != null) {
a0.detectLanguage(r4);
}
}, 250);
export async function install() {
let env29481;
const r9 = env3137;
let c25379_2 = r9;
const r26 = env29481 = {
$parent: r9
};
let c25379_1 = r26;
env29481[0] = void 0;
env29481[1] = void 0;
const availableDictionaries = await c3137_7.getAvailableDictionaries();
let c25379_0 = availableDictionaries;
if (availableDictionaries == null) {
c25379_0 = [];
}
env29481[0] = c25379_0.map(normalizeLocale).filter(isNotNullish);
env29481[1] = new Spellchecker(env29481[0]);
const r57 = env29481[1];
$mk114669(env3137)(r57);
return env29481[1];
}