Vencord/src/plugins/quickreply.ts

62 lines
1.8 KiB
TypeScript
Raw Normal View History

2022-10-08 17:27:20 +00:00
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);
2022-10-08 18:36:57 +00:00
document.addEventListener("keydown", onKeydown);
2022-10-08 17:27:20 +00:00
},
stop() {
Dispatcher.unsubscribe("DELETE_PENDING_REPLY", onDeletePendingReply);
2022-10-08 18:36:57 +00:00
document.removeEventListener("keydown", onKeydown);
2022-10-08 17:27:20 +00:00
},
});
let idx = -1;
2022-10-08 18:36:57 +00:00
function onDeletePendingReply() {
2022-10-08 17:27:20 +00:00
idx = -1;
2022-10-08 18:36:57 +00:00
}
2022-10-08 17:27:20 +00:00
2022-10-08 18:36:57 +00:00
function onKeydown(e: KeyboardEvent) {
2022-10-08 17:27:20 +00:00
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) {
2022-10-08 18:36:57 +00:00
return void Dispatcher.dispatch({
2022-10-08 17:27:20 +00:00
type: "DELETE_PENDING_REPLY",
channelId,
});
}
Dispatcher.dispatch({
type: "CREATE_PENDING_REPLY",
channel: channel,
message: messages[idx],
showMentionToggle: channel.guild_id !== null,
});
2022-10-08 18:36:57 +00:00
}