Add SpellCheck suggestions
This commit is contained in:
8
src/renderer/patches/index.ts
Normal file
8
src/renderer/patches/index.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: GPL-3.0
|
||||
* Vencord Desktop, a desktop app aiming to give you a snappier Discord Experience
|
||||
* Copyright (c) 2023 Vendicated and Vencord contributors
|
||||
*/
|
||||
|
||||
// TODO: Possibly auto generate glob if we have more patches in the future
|
||||
import "./spellCheck";
|
||||
30
src/renderer/patches/shared.ts
Normal file
30
src/renderer/patches/shared.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: GPL-3.0
|
||||
* Vencord Desktop, a desktop app aiming to give you a snappier Discord Experience
|
||||
* Copyright (c) 2023 Vendicated and Vencord contributors
|
||||
*/
|
||||
|
||||
import { Patch } from "@vencord/types/utils/types";
|
||||
|
||||
window.VCDP = {};
|
||||
|
||||
interface PatchData {
|
||||
patches: Omit<Patch, "plugin">[];
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
export function addPatch<P extends PatchData>(p: P) {
|
||||
const { patches, ...globals } = p;
|
||||
|
||||
for (const patch of patches as Patch[]) {
|
||||
if (!Array.isArray(patch.replacement)) patch.replacement = [patch.replacement];
|
||||
for (const r of patch.replacement) {
|
||||
if (typeof r.replace === "string") r.replace = r.replace.replaceAll("$self", "VCDP");
|
||||
}
|
||||
|
||||
patch.plugin = "VencordDesktop";
|
||||
Vencord.Plugins.patches.push(patch as Patch);
|
||||
}
|
||||
|
||||
Object.assign(VCDP, globals);
|
||||
}
|
||||
60
src/renderer/patches/spellCheck.tsx
Normal file
60
src/renderer/patches/spellCheck.tsx
Normal file
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: GPL-3.0
|
||||
* Vencord Desktop, a desktop app aiming to give you a snappier Discord Experience
|
||||
* Copyright (c) 2023 Vendicated and Vencord contributors
|
||||
*/
|
||||
|
||||
import { addContextMenuPatch } from "@vencord/types/api/ContextMenu";
|
||||
import { Menu } from "@vencord/types/webpack/common";
|
||||
|
||||
import { addPatch } from "./shared";
|
||||
|
||||
let word: string;
|
||||
let corrections: string[];
|
||||
|
||||
// Make spellcheck suggestions work
|
||||
addPatch({
|
||||
patches: [
|
||||
{
|
||||
find: ".enableSpellCheck)",
|
||||
replacement: {
|
||||
// if (isDesktop) { DiscordNative.onSpellcheck(openMenu(props)) } else { e.preventDefault(); openMenu(props) }
|
||||
match: /else\{.{1,3}\.preventDefault\(\);(.{1,3}\(.{1,3}\))\}/,
|
||||
// ... else { $self.onSlateContext(() => openMenu(props)) }
|
||||
replace: "else {$self.onSlateContext(() => $1)}"
|
||||
}
|
||||
}
|
||||
],
|
||||
|
||||
onSlateContext(openMenu: () => void) {
|
||||
const cb = (w: string, c: string[]) => {
|
||||
VencordDesktopNative.spellcheck.offSpellcheckResult(cb);
|
||||
word = w;
|
||||
corrections = c;
|
||||
openMenu();
|
||||
};
|
||||
VencordDesktopNative.spellcheck.onSpellcheckResult(cb);
|
||||
}
|
||||
});
|
||||
|
||||
addContextMenuPatch("textarea-context", children => () => {
|
||||
if (!word || !corrections?.length) return;
|
||||
|
||||
children.push(
|
||||
<Menu.MenuGroup>
|
||||
{corrections.map(c => (
|
||||
<Menu.MenuItem
|
||||
id={"vcd-spellcheck-suggestion-" + c}
|
||||
label={c}
|
||||
action={() => VencordDesktopNative.spellcheck.replaceMisspelling(c)}
|
||||
/>
|
||||
))}
|
||||
<Menu.MenuSeparator />
|
||||
<Menu.MenuItem
|
||||
id="vcd-spellcheck-learn"
|
||||
label={`Add ${word} to dictionary`}
|
||||
action={() => VencordDesktopNative.spellcheck.addToDictionary(word)}
|
||||
/>
|
||||
</Menu.MenuGroup>
|
||||
);
|
||||
});
|
||||
Reference in New Issue
Block a user