aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/quickreply.ts
blob: 111d9bba48f834370c10ab0106f354852dedb34f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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,
    });
};