Update CustomQuoter.plugin.js

This commit is contained in:
Mirco Wittrien 2020-07-01 16:37:43 +02:00
parent 84ea5a9cbc
commit 0443a5a741
1 changed files with 143 additions and 36 deletions

View File

@ -2,7 +2,7 @@
var CustomQuoter = (_ => {
var _this;
var settings = {}, formats = {};
var settings = {}, formats = {}, format = null;
const PreviewMessage = class PreviewMessage extends BdApi.React.Component {
render() {
@ -26,7 +26,7 @@ var CustomQuoter = (_ => {
username: "Test User"
}),
channel_id: spoofChannel.id,
content: _this.parseQuote(spoofQuotedMessage, spoofChannel)
content: _this.parseQuote(spoofQuotedMessage, spoofChannel, this.props.format)
});
return BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.MessageGroup, {
className: BDFDB.disCNS.message + BDFDB.disCN.messagecozymessage,
@ -39,11 +39,18 @@ var CustomQuoter = (_ => {
return class CustomQuoter {
getName () {return "CustomQuoter";}
getVersion () {return "1.1.0";}
getVersion () {return "1.1.1";}
getAuthor () {return "DevilBro";}
getDescription () {return "Let's you customize the output of the native quote feature of Discord.";}
constructor () {
this.changelog = {
"added":[["More Choices","You can now add more formats, which you can choose from in the context submenu (clicking the main item will use first 'standard' format"],["Clipboard","You can now quote in channels that you got no writting permissions in, meaning quoting a message in such a channel will copy the quote to the clipboard instead of the channel textarea"]],
"improved":[["Quote selected text","Works more smoothly with formating symbols like (~, _, `, etc.)"]]
};
}
initConstructor () {
_this = this;
@ -53,9 +60,6 @@ var CustomQuoter = (_ => {
quoteOnlySelected: {value:true, description:"Only insert selected text in a quoted message"},
ignoreMentionInDM: {value:true, description:"Do not add a mention in DM channels"},
forceZeros: {value:false, description:"Force leading Zeros"}
},
formats: {
customQuote: {value:"$quote $mention", description:"Custom Format:"}
}
};
}
@ -84,31 +88,85 @@ var CustomQuoter = (_ => {
innerItems = [];
for (let key in formats) innerItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsSaveItem, {
innerItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.Flex, {
className: BDFDB.disCN.marginbottom8,
type: "TextInput",
plugin: this,
keys: ["formats", key],
label: this.defaults.formats[key].description,
basis: "70%",
value: formats[key],
onChange: (value, instance) => {
formats[key] = value;
BDFDB.ReactUtils.forceUpdate(BDFDB.ReactUtils.findOwner(instance._reactInternalFiber.return, {key:"PREVIEW_MESSAGE"}));
}
align: BDFDB.LibraryComponents.Flex.Align.END,
children: [
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.Flex.Child, {
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.FormComponents.FormItem, {
title: "Name:",
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.TextInput, {
className: "input-newquote input-name",
value: "",
placeholder: "Formatname"
})
})
}),
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.Flex.Child, {
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.FormComponents.FormItem, {
title: "Quote:",
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.TextInput, {
className: "input-newquote input-quote",
value: "",
placeholder: "Quote"
})
})
}),
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.Button, {
style: {marginBottom: 1},
onClick: _ => {
for (let input of settingsPanel.querySelectorAll(".input-newquote " + BDFDB.dotCN.input)) if (!input.value || input.value.length == 0 || input.value.trim().length == 0) return BDFDB.NotificationUtils.toast("Fill out all fields to add a new quote.", {type:"danger"});
let key = settingsPanel.querySelector(".input-name " + BDFDB.dotCN.input).value.trim();
let quote = settingsPanel.querySelector(".input-quote " + BDFDB.dotCN.input).value.trim();
if (formats[key]) return BDFDB.NotificationUtils.toast("A quote with the choosen name already exists. Please choose another name.", {type:"danger"});
else {
formats[key] = quote;
BDFDB.DataUtils.save(formats, this, "formats");
BDFDB.PluginUtils.refreshSettingsPanel(this, settingsPanel, collapseStates);
}
},
children: BDFDB.LanguageUtils.LanguageStrings.ADD
})
]
}));
innerItems.push(BDFDB.ReactUtils.createElement("div", {
className: BDFDB.disCN.marginbottom8,
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsLabel, {
label: "Preview:"
innerItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.FormComponents.FormDivider, {
className: BDFDB.disCN.marginbottom20
}));
for (let key in formats) innerItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.Card, {
cardId: key,
noRemove: key == "Standard",
onRemove: _ => {
delete formats[key];
BDFDB.DataUtils.save(formats, this, "formats");
BDFDB.PluginUtils.refreshSettingsPanel(this, settingsPanel, collapseStates);
},
children: BDFDB.ReactUtils.createElement("div", {
children: [
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsSaveItem, {
className: BDFDB.disCN.marginbottom8,
type: "TextInput",
plugin: this,
keys: ["formats", key],
label: key + ":",
basis: "70%",
value: formats[key],
onChange: (value, instance) => {
formats[key] = value;
BDFDB.ReactUtils.forceUpdate(BDFDB.ReactUtils.findOwner(instance._reactInternalFiber.return, {key: "PREVIEW_MESSAGE_" + key.replace(/\s/g, "_")}));
}
}),
BDFDB.ReactUtils.createElement(PreviewMessage, {
key: "PREVIEW_MESSAGE_" + key.replace(/\s/g, "_"),
format: key
})
]
})
}));
innerItems.push(BDFDB.ReactUtils.createElement(PreviewMessage, {
key: "PREVIEW_MESSAGE"
}));
settingsItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.CollapseContainer, {
title: "Format",
title: "Formats",
collapseStates: collapseStates,
dividertop: true,
children: innerItems
@ -181,6 +239,9 @@ var CustomQuoter = (_ => {
if (this.started) return;
BDFDB.PluginUtils.init(this);
let customQuote = BDFDB.DataUtils.load(this, "formats", "customQuote"); // REMOVE 01.07.2020
if (typeof customQuote == "string") BDFDB.DataUtils.save(customQuote, this, "formats", "Standard");
BDFDB.ModuleUtils.patch(this, BDFDB.LibraryModules.QuoteUtils, "createQuotedText", {instead: e => {
return this.parseQuote(e.methodArguments[0], e.methodArguments[1]);
}});
@ -209,12 +270,50 @@ var CustomQuoter = (_ => {
}
}
parseQuote (message, channel) {
onMessageContextMenu (e) {
if (e.instance.props.message && e.instance.props.channel) {
let item = null, action = choice => {
format = choice;
if (!BDFDB.LibraryModules.QuoteUtils.canQuote(e.instance.props.message, e.instance.props.channel)) {
BDFDB.LibraryRequires.electron.clipboard.write({text:this.parseQuote(e.instance.props.message, e.instance.props.channel)});
BDFDB.NotificationUtils.toast("Quote has been copied to clipboard.", {type:"success"});
}
else BDFDB.LibraryModules.MessageManageUtils.quoteMessage(e.instance.props.channel, e.instance.props.message);
format = null;
};
let [children, index] = BDFDB.ContextMenuUtils.findItem(e.returnvalue, {id: "quote"});
if (index > -1) item = children[index];
else {
item = BDFDB.ContextMenuUtils.createItem(BDFDB.LibraryComponents.MenuItems.MenuItem, {
label: BDFDB.LanguageUtils.LanguageStrings.QUOTE,
id: "quote",
action: _ => {action(null);}
});
let [unreadChildren, unreadIndex] = BDFDB.ContextMenuUtils.findItem(e.returnvalue, {id: "mark-unread"});
unreadChildren.splice(unreadIndex > -1 ? unreadIndex - 1 : unreadChildren.length, 0, item);
}
let addedFormats = BDFDB.ObjectUtils.exclude(formats, "Standard");
if (!BDFDB.ObjectUtils.isEmpty(addedFormats)) item.props.children = BDFDB.ContextMenuUtils.createItem(BDFDB.LibraryComponents.MenuItems.MenuGroup, {
children: Object.keys(addedFormats).map(key => BDFDB.ContextMenuUtils.createItem(BDFDB.LibraryComponents.MenuItems.MenuItem, {
label: key,
id: BDFDB.ContextMenuUtils.createItemId(this.name, "added-quote", key),
action: _ => {action(key);}
}))
});
}
}
parseQuote (message, channel, choice = format) {
let languageId = BDFDB.LanguageUtils.getLanguage().id;
let quoteFormat = formats[choice] || formats.Standard || "";
let guild = channel.guild_id ? (BDFDB.LibraryModules.GuildStore.getGuild(channel.guild_id) || {id: channel.guild_id, name: "Test Server"}) : {id: BDFDB.DiscordConstants.ME, name: BDFDB.LanguageUtils.LanguageStrings.DIRECT_MESSAGES};
let member = guild && BDFDB.LibraryModules.MemberStore.getMember(guild.id, message.author.id);
let timestamp = new Date(message.editedTimestamp || message.timestamp);
let hour = timestamp.getHours(), minute = timestamp.getMinutes(), second = timestamp.getSeconds(), msecond = timestamp.getMilliseconds(), day = timestamp.getDate(), month = timestamp.getMonth()+1, timemode = "";
if (formats.customQuote.indexOf("$timemode") > -1) {
if (quoteFormat.indexOf("$timemode") > -1) {
timemode = hour >= 12 ? "PM" : "AM";
hour = hour % 12;
hour = hour ? hour : 12;
@ -222,15 +321,23 @@ var CustomQuoter = (_ => {
let content = message.content;
let selectedText = settings.quoteOnlySelected && document.getSelection().toString().trim();
if (selectedText && content.replace(/[`~_-]/g, "").indexOf(selectedText) > -1) content = selectedText;
if (selectedText) content = BDFDB.StringUtils.extractSelection(content, selectedText);
let unquotedLines = content.split("\n").filter(line => !line.startsWith("> "));
unquotedLines = unquotedLines.slice(unquotedLines.findIndex(line => line.trim().length > 0)).join("\n");
let quotedLines = unquotedLines.slice(unquotedLines.findIndex(line => line.trim().length > 0)).map(line => "> " + line + "\n").join("");
if (quotedLines) {
quotedLines = quotedLines.replace(/(@everyone|@here)/g, "`$1`").replace(/``(@everyone|@here)``/g, "`$1`");
quotedLines = quotedLines.replace(/<@[!&]{0,1}([0-9]{10,})>/g, (string, match) => {
let user = BDFDB.LibraryModules.UserStore.getUser(match);
if (user) {
let userMember = guild && BDFDB.LibraryModules.MemberStore.getMember(guild.id, match);
return `\`@${userMember && userMember.nick || user.username}\``;
}
else if (guild && guild.roles[match] && guild.roles[match].name) return `\`${guild.roles[match].name.indexOf("@") == 0 ? "" : "@"}${guild.roles[match].name}\``;
return string;
});
}
let guild = channel.guild_id ? (BDFDB.LibraryModules.GuildStore.getGuild(channel.guild_id) || {id: channel.guild_id, name: "Test Server"}) : {id: BDFDB.DiscordConstants.ME, name: BDFDB.LanguageUtils.LanguageStrings.DIRECT_MESSAGES};
let member = BDFDB.LibraryModules.MemberStore.getMember(guild && guild.id, message.author.id);
return BDFDB.StringUtils.insertNRST(formats.customQuote)
return BDFDB.StringUtils.insertNRST(quoteFormat)
.replace("$mention", settings.ignoreMentionInDM && channel.isDM() ? "" : `<@!${message.author.id}>`)
.replace("$link", `https://discordapp.com/channels/${guild.id}/${channel.id}/${message.id}`)
.replace("$authorName", member && member.nick || message.author.username || "")
@ -253,7 +360,7 @@ var CustomQuoter = (_ => {
.replace("$day", settings.forceZeros && day < 10 ? "0" + day : day)
.replace("$month", settings.forceZeros && month < 10 ? "0" + month : month)
.replace("$year", timestamp.getFullYear())
.replace("$quote", unquotedLines.split("\n").map(line => "> " + line + "\n").join("") || "")
.replace("$quote", quotedLines || "")
.split(" ").filter(n => n).join(" ");
}
@ -267,7 +374,7 @@ var CustomQuoter = (_ => {
forceUpdateAll() {
settings = BDFDB.DataUtils.get(this, "settings");
formats = BDFDB.DataUtils.get(this, "formats");
formats = Object.assign({"Standard": "$quote $mention"}, BDFDB.DataUtils.load(this, "formats"));
BDFDB.ModuleUtils.forceAllUpdates(this);
BDFDB.MessageUtils.rerenderAll();