-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
180 lines (161 loc) · 4.61 KB
/
index.js
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import ReactButton from "./components/ReactButton";
import PublishButton from "./components/PublishButton";
import ErrorBoundary from "./components/ErrorBoundary";
import findInReactTree from "./findInReactTree";
const {
React,
findModule,
findModuleByProps,
findModuleByDisplayName,
Patcher,
getData,
saveData,
} = BdApi;
const MiniPopover = findModule(
(m) => m?.default?.displayName === "MiniPopover"
);
const SwitchItem = findModuleByDisplayName("SwitchItem");
const ConfirmationActions = findModuleByProps("confirmDelete");
const MessageActions = findModuleByProps("deleteMessage");
const FluxDispatcher = findModuleByProps("dirtyDispatch");
let emojiPickerMessage = "";
let addedListener = false;
export default class ShowAllMessageButtons {
start() {
Patcher.after(
this.constructor.name,
MiniPopover,
"default",
(that, args, ret) => {
const props = findInReactTree(
ret,
(r) => r?.hasOwnProperty("expanded") && r?.hasOwnProperty("message")
);
if (!props) return ret;
// Force the buttons to be expanded.
props.expanded = true;
// I am Really NOT Amused.
// Add my own react button because it's removed for some reason.
// TODO: Make the message stay "hovered" when the emoji picker is open.
if (props.message.id == emojiPickerMessage) {
props.showEmojiPicker = true;
if (!addedListener) {
addedListener = true;
// Listen for clicks and only stop listening once the picker is removed.
const pickerClick = (event) => {
const onPicker =
event.path.filter(
(element) => element.id == "emoji-picker-tab-panel"
).length == 1;
if ((onPicker && !event.shiftKey) || !onPicker) {
removePickerClick();
addedListener = false;
emojiPickerMessage = "";
// Timeout or the reaction won't be added.
setTimeout(() => {
this.updateMessage(props.message, false);
}, 0);
}
};
const removePickerClick = () => {
document.body.removeEventListener("click", pickerClick);
};
document.body.addEventListener("click", pickerClick);
}
}
if (props.canReact) {
ret.props.children.splice(
ret.props.children.length - 2,
0,
<ErrorBoundary fallback={(() => null)()}>
<ReactButton
showEmojiPicker={(show) => {
this.updateMessage(props.message, show);
}}
/>
</ErrorBoundary>
);
}
// Not quite as annoying as the react button.
if (props.canPublish) {
ret.props.children.splice(
ret.props.children.length - 2,
0,
<ErrorBoundary fallback={(() => null)()}>
<PublishButton
id={props.message.id}
channel_id={props.message.channel_id}
/>
</ErrorBoundary>
);
}
return ret;
}
);
if (this.canBypassDeleteConfirmation()) this.patchConfirmDelete();
}
canBypassDeleteConfirmation() {
return !!getData(this.constructor.name, "bypassDeleteConfirmation") ?? true;
}
patchConfirmDelete() {
this.unpatchConfirmDelete();
this.confirmDelete = Patcher.instead(
this.constructor.name,
ConfirmationActions,
"confirmDelete",
(that, args) => {
MessageActions.deleteMessage(args[0].id, args[1].id);
}
);
}
unpatchConfirmDelete() {
try {
this.confirmDelete();
} catch {}
}
stop() {
if (this.patchMiniPopoverAnonInterval) {
clearInterval(this.patchMiniPopoverAnonInterval);
this.patchMiniPopoverAnonInterval = null;
}
Patcher.unpatchAll(this.constructor.name);
}
getSettingsPanel() {
return () => {
const [
bypassDeleteConfirmation,
setBypassDeleteConfirmation,
] = React.useState(this.canBypassDeleteConfirmation());
return (
<ErrorBoundary>
<SwitchItem
value={bypassDeleteConfirmation}
onChange={(value) => {
setBypassDeleteConfirmation(value);
saveData(
this.constructor.name,
"bypassDeleteConfirmation",
value
);
if (value) {
this.patchConfirmDelete();
} else {
this.unpatchConfirmDelete();
}
}}
note="When enabled this will bypass the delete message confirmation and delete the message right away."
>
Bypass Delete Confirmation
</SwitchItem>
</ErrorBoundary>
);
};
}
updateMessage(message, showEmojiPicker) {
emojiPickerMessage = showEmojiPicker ? message.id : "";
FluxDispatcher.dirtyDispatch({
type: "MESSAGE_UPDATE",
message,
});
}
}