diff options
author | obscurity <z@x4.pm> | 2022-10-08 19:27:20 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-08 19:27:20 +0200 |
commit | 0109381a4f5a513a23258130bca06d40fcbc8ea9 (patch) | |
tree | 8d960e080b2c63ed7b98e48ca3e7cfd1b38268cf /src/plugins/quickreply.ts | |
parent | 8842ad765215a56fdf9eec610301a03163144c50 (diff) | |
download | Vencord-0109381a4f5a513a23258130bca06d40fcbc8ea9.tar.gz Vencord-0109381a4f5a513a23258130bca06d40fcbc8ea9.tar.bz2 Vencord-0109381a4f5a513a23258130bca06d40fcbc8ea9.zip |
feat(plugin): add quickreply (#61)
Diffstat (limited to 'src/plugins/quickreply.ts')
-rw-r--r-- | src/plugins/quickreply.ts | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/plugins/quickreply.ts b/src/plugins/quickreply.ts new file mode 100644 index 0000000..111d9bb --- /dev/null +++ b/src/plugins/quickreply.ts @@ -0,0 +1,61 @@ +import definePlugin from "../utils/types"; +import { Devs } from "../utils/constants"; +import { FluxDispatcher as Dispatcher } from "../webpack/common"; +import { filters } from "../webpack"; +import { lazyWebpack } from "../utils/misc"; + +const channelIdModule = lazyWebpack(filters.byProps(["getChannelId"])); +const channelModule = lazyWebpack(filters.byProps(["getChannel"])); +const messagesModule = lazyWebpack(filters.byProps(["getRawMessages"])); + +export default definePlugin({ + name: "Quickreply", + authors: [Devs.obscurity], + description: "Reply to messages faster (ctrl + direction)", + + start() { + Dispatcher.subscribe("DELETE_PENDING_REPLY", onDeletePendingReply); + document.addEventListener("keydown", keydown); + }, + + stop() { + Dispatcher.unsubscribe("DELETE_PENDING_REPLY", onDeletePendingReply); + document.removeEventListener("keydown", keydown); + }, +}); + +let idx = -1; +const onDeletePendingReply = () => { + idx = -1; +}; + +const keydown = e => { + if ( + (!e.ctrlKey && !e.metaKey) || + (e.key !== "ArrowUp" && e.key !== "ArrowDown") + ) { + return; + } + + const channelId = channelIdModule.getChannelId(); + const channel = channelModule.getChannel(channelId); + const messages = messagesModule.getMessages(channelId).toArray().reverse(); + + if (e.key === "ArrowUp") idx += 1; + else if (e.key === "ArrowDown") idx = Math.max(-1, idx - 1); + + if (idx > messages.length) idx = messages.length; + if (idx < 0) { + return Dispatcher.dispatch({ + type: "DELETE_PENDING_REPLY", + channelId, + }); + } + + Dispatcher.dispatch({ + type: "CREATE_PENDING_REPLY", + channel: channel, + message: messages[idx], + showMentionToggle: channel.guild_id !== null, + }); +}; |