BetterDiscordAddons/Plugins/ChatAliases/ChatAliases.plugin.js

527 lines
22 KiB
JavaScript
Raw Normal View History

2019-09-20 22:32:52 +02:00
//META{"name":"ChatAliases","website":"https://github.com/mwittrien/BetterDiscordAddons/tree/master/Plugins/ChatAliases","source":"https://raw.githubusercontent.com/mwittrien/BetterDiscordAddons/master/Plugins/ChatAliases/ChatAliases.plugin.js"}*//
2018-10-11 10:21:26 +02:00
class ChatAliases {
getName () {return "ChatAliases";}
2020-01-21 12:56:26 +01:00
getVersion () {return "2.0.8";}
getAuthor () {return "DevilBro";}
getDescription () {return "Allows the user to configure their own chat-aliases which will automatically be replaced before the message is being sent.";}
2019-01-26 22:45:19 +01:00
2019-09-04 12:34:02 +02:00
constructor () {
2019-02-06 15:46:34 +01:00
this.changelog = {
2019-12-22 14:30:41 +01:00
"fixed":[["File Aliases","Fixed issue where file aliases would get replaced for the filepath and not the file"],["New WYSIWYG Textarea","Fixed for the new WYSIWYG Textarea that is hidden by experiments"]],
2019-12-20 14:01:16 +01:00
"improved":[["New Library Structure & React","Restructured my Library and switched to React rendering instead of DOM manipulation"]]
2019-02-06 15:46:34 +01:00
};
2019-09-04 12:34:02 +02:00
2019-11-14 17:56:26 +01:00
this.patchedModules = {
2019-12-20 14:01:16 +01:00
before: {
2019-12-21 01:14:53 +01:00
ChannelAutoComplete: "render",
2019-12-21 11:35:33 +01:00
ChannelTextAreaForm: "render",
MessageEditor: "render",
Upload: "render"
2019-11-14 17:56:26 +01:00
}
2019-01-17 23:48:29 +01:00
};
2019-09-04 12:34:02 +02:00
}
2019-01-26 22:45:19 +01:00
2019-09-04 12:34:02 +02:00
initConstructor () {
2019-12-20 14:01:16 +01:00
this.css = `
${BDFDB.dotCN.autocompleteicon} {
flex: 0 0 auto;
}
`;
2018-10-11 10:21:26 +02:00
this.defaults = {
2019-02-06 15:46:34 +01:00
configs: {
case: {value:false, description:"Handle the wordvalue case sensitive"},
exact: {value:true, description:"Handle the wordvalue as an exact word and not as part of a word"},
2019-09-11 12:14:43 +02:00
autoc: {value:true, description:"Add this alias in the autocomplete menu (not for RegExp)"},
regex: {value:false, description:"Handle the wordvalue as a RegExp string"},
2019-02-06 15:46:34 +01:00
file: {value:false, description:"Handle the replacevalue as a filepath"}
},
2018-10-11 10:21:26 +02:00
settings: {
2019-02-06 15:46:34 +01:00
addContextMenu: {value:true, description:"Add a ContextMenu entry to faster add new Aliases:"},
2019-09-11 12:14:43 +02:00
addAutoComplete: {value:true, description:"Add an Autocomplete-Menu for Non-RegExp Aliases:"}
2019-02-26 12:16:08 +01:00
},
amounts: {
minAliasLength: {value:2, min:1, description:"Minimal Character Length to open Autocomplete-Menu:"}
2018-10-11 10:21:26 +02:00
}
};
}
2019-12-20 14:01:16 +01:00
getSettingsPanel (collapseStates = {}) {
2020-01-17 19:50:31 +01:00
if (!window.BDFDB || typeof BDFDB != "object" || !BDFDB.loaded || !this.started) return;
2019-10-22 23:04:35 +02:00
let settings = BDFDB.DataUtils.get(this, "settings");
2019-12-20 14:01:16 +01:00
let amounts = BDFDB.DataUtils.get(this, "amounts");
let settingspanel, settingsitems = [], inneritems = [];
for (let key in settings) inneritems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsSaveItem, {
className: BDFDB.disCN.marginbottom8,
type: "Switch",
plugin: this,
keys: ["settings", key],
label: this.defaults.settings[key].description,
value: settings[key]
}));
for (let key in amounts) inneritems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsSaveItem, {
className: BDFDB.disCN.marginbottom8,
type: "TextInput",
childProps: {
type: "number"
},
plugin: this,
keys: ["amounts", key],
label: this.defaults.amounts[key].description,
basis: "20%",
min: this.defaults.amounts[key].min,
max: this.defaults.amounts[key].max,
value: amounts[key]
}));
settingsitems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.CollapseContainer, {
title: "Settings",
collapseStates: collapseStates,
children: inneritems
}));
let values = {wordvalue:"", replacevalue:""};
settingsitems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.CollapseContainer, {
title: "Add new Alias",
collapseStates: collapseStates,
dividertop: true,
children: [
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsItem, {
type: "Button",
label: "Pick a Wordvalue and Replacevalue:",
key: "ADDBUTTON",
disabled: !Object.keys(values).every(valuename => values[valuename]),
children: BDFDB.LanguageUtils.LanguageStrings.ADD,
onClick: _ => {
this.saveWord(values.wordvalue, values.replacevalue, settingspanel.querySelector(".input-replacevalue input[type='file']"));
2020-01-22 11:26:18 +01:00
BDFDB.PluginUtils.refreshSettingsPanel(this, settingspanel, collapseStates);
2019-12-20 14:01:16 +01:00
}
}),
this.createInputs(values)
].flat(10).filter(n => n)
}));
if (!BDFDB.ObjectUtils.isEmpty(this.aliases)) settingsitems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.CollapseContainer, {
title: "Added Aliases",
collapseStates: collapseStates,
dividertop: true,
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsList, {
settings: Object.keys(this.defaults.configs),
data: Object.keys(this.aliases).map((wordvalue, i) => Object.assign({}, this.aliases[wordvalue], {
key: wordvalue,
label: wordvalue
})),
renderLabel: data => BDFDB.ReactUtils.createElement("div", {
style: {width: "100%"},
children: [
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.TextInput, {
value: data.label,
placeholder: data.label,
size: BDFDB.LibraryComponents.TextInput.Sizes.MINI,
maxLength: 100000000000000000000,
onChange: value => {
this.aliases[value] = this.aliases[data.label];
delete this.aliases[data.label];
data.label = value;
BDFDB.DataUtils.save(this.aliases, this, "words");
}
}),
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.TextInput, {
value: data.replace,
placeholder: data.replace,
size: BDFDB.LibraryComponents.TextInput.Sizes.MINI,
maxLength: 100000000000000000000,
onChange: value => {
this.aliases[data.label].replace = value;
BDFDB.DataUtils.save(this.aliases, this, "words");
}
})
]
}),
onCheckboxChange: (value, instance) => {
this.aliases[instance.props.cardId][instance.props.settingId] = value;
BDFDB.DataUtils.save(this.aliases, this, "words");
},
onRemove: (e, instance) => {
delete this.aliases[instance.props.cardId];
BDFDB.DataUtils.save(this.aliases, this, "words");
2020-01-22 11:26:18 +01:00
BDFDB.PluginUtils.refreshSettingsPanel(this, settingspanel, collapseStates);
2019-12-20 14:01:16 +01:00
}
})
}));
settingsitems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.CollapseContainer, {
title: "Remove All",
collapseStates: collapseStates,
dividertop: true,
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsItem, {
type: "Button",
className: BDFDB.disCN.marginbottom8,
color: BDFDB.LibraryComponents.Button.Colors.RED,
label: "Remove all added Aliases",
onClick: _ => {
BDFDB.ModalUtils.confirm(this, "Are you sure you want to remove all added Aliases?", _ => {
this.aliases = {};
BDFDB.DataUtils.remove(this, "words");
2020-01-22 11:26:18 +01:00
BDFDB.PluginUtils.refreshSettingsPanel(this, settingspanel, collapseStates);
2019-12-20 14:01:16 +01:00
});
},
children: BDFDB.LanguageUtils.LanguageStrings.REMOVE
})
}));
settingsitems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.CollapseContainer, {
title: "Config Guide",
collapseStates: collapseStates,
dividertop: true,
children: ["Case: Will replace words while comparing lowercase/uppercase. apple => apple, not APPLE or AppLe", "Not Case: Will replace words while ignoring lowercase/uppercase. apple => apple, APPLE and AppLe", "Exact: Will replace words that are exactly the replaceword. apple to pear => applepie stays applepie", "Not Exact: Will replace words anywhere they appear. apple to pear => applepieapple to pearpiepear", "Autoc: Will appear in the Autocomplete Menu (if enabled).", "Regex: Will treat the entered wordvalue as a regular expression. Help", "File: If the replacevalue is a filepath it will try to upload the file located at the filepath."].map(string => BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.FormComponents.FormText, {
type: BDFDB.LibraryComponents.FormComponents.FormTextTypes.DESCRIPTION,
children: string
}))
}));
return settingspanel = BDFDB.PluginUtils.createSettingsPanel(this, settingsitems);
}
2018-10-11 10:21:26 +02:00
//legacy
load () {}
start () {
2020-01-17 19:50:31 +01:00
if (!window.BDFDB) window.BDFDB = {myPlugins:{}};
if (window.BDFDB && window.BDFDB.myPlugins && typeof window.BDFDB.myPlugins == "object") window.BDFDB.myPlugins[this.getName()] = this;
2020-01-21 12:56:26 +01:00
let libraryScript = document.querySelector("head script#BDFDBLibraryScript");
2019-05-26 13:55:26 +02:00
if (!libraryScript || (performance.now() - libraryScript.getAttribute("date")) > 600000) {
2018-10-11 10:21:26 +02:00
if (libraryScript) libraryScript.remove();
libraryScript = document.createElement("script");
2019-05-26 13:55:26 +02:00
libraryScript.setAttribute("id", "BDFDBLibraryScript");
2018-10-11 10:21:26 +02:00
libraryScript.setAttribute("type", "text/javascript");
2019-10-18 10:56:41 +02:00
libraryScript.setAttribute("src", "https://mwittrien.github.io/BetterDiscordAddons/Plugins/BDFDB.min.js");
2019-01-17 23:48:29 +01:00
libraryScript.setAttribute("date", performance.now());
2020-01-14 00:06:07 +01:00
libraryScript.addEventListener("load", _ => {this.initialize();});
2018-10-11 10:21:26 +02:00
document.head.appendChild(libraryScript);
2019-05-26 13:55:26 +02:00
}
2020-01-17 19:50:31 +01:00
else if (window.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) this.initialize();
2020-01-14 00:06:07 +01:00
this.startTimeout = setTimeout(_ => {
2019-11-01 10:27:07 +01:00
try {return this.initialize();}
catch (err) {console.error(`%c[${this.getName()}]%c`, "color: #3a71c1; font-weight: 700;", "", "Fatal Error: Could not initiate plugin! " + err);}
}, 30000);
2018-10-11 10:21:26 +02:00
}
initialize () {
2020-01-17 19:50:31 +01:00
if (window.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) {
2019-01-22 11:05:54 +01:00
if (this.started) return;
2019-10-22 18:55:25 +02:00
BDFDB.PluginUtils.init(this);
2018-10-11 10:21:26 +02:00
2019-10-22 19:49:57 +02:00
this.aliases = BDFDB.DataUtils.load(this, "words");
2019-01-26 22:45:19 +01:00
2019-10-22 18:55:25 +02:00
BDFDB.ModuleUtils.forceAllUpdates(this);
2018-10-11 10:21:26 +02:00
}
2019-11-01 10:14:50 +01:00
else console.error(`%c[${this.getName()}]%c`, "color: #3a71c1; font-weight: 700;", "", "Fatal Error: Could not load BD functions!");
2018-10-11 10:21:26 +02:00
}
stop () {
2020-01-17 19:50:31 +01:00
if (window.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) {
2019-10-22 11:37:23 +02:00
this.stopping = true;
2019-12-22 14:30:41 +01:00
BDFDB.ModuleUtils.forceAllUpdates(this);
2019-10-22 18:55:25 +02:00
BDFDB.PluginUtils.clear(this);
2018-10-11 10:21:26 +02:00
}
}
// begin of own functions
2019-12-10 13:59:44 +01:00
onSettingsClosed () {
if (this.SettingsUpdated) {
delete this.SettingsUpdated;
BDFDB.ModuleUtils.forceAllUpdates(this);
}
}
onNativeContextMenu (e) {
if (e.instance.props.value && e.instance.props.value.trim()) {
2019-12-20 14:01:16 +01:00
if ((e.instance.props.type == "NATIVE_TEXT" || e.instance.props.type == "CHANNEL_TEXT_AREA") && BDFDB.DataUtils.get(this, "settings", "addContextMenu")) this.injectItem(e, e.instance.props.value.trim());
2019-02-06 15:46:34 +01:00
}
}
2020-01-15 10:45:35 +01:00
onSlateContextMenu (e) {
let text = document.getSelection().toString().trim();
if (text && BDFDB.DataUtils.get(this, "settings", "addContextMenu")) this.injectItem(e, text);
}
2019-12-10 13:59:44 +01:00
onMessageContextMenu (e) {
2020-01-15 10:45:35 +01:00
let text = document.getSelection().toString().trim();
if (text && BDFDB.DataUtils.get(this, "settings", "addContextMenu")) this.injectItem(e, text);
2019-02-06 15:46:34 +01:00
}
2019-10-22 23:04:35 +02:00
2019-12-20 14:01:16 +01:00
injectItem (e, text) {
2019-12-10 13:59:44 +01:00
let [children, index] = BDFDB.ReactUtils.findChildren(e.returnvalue, {name:["FluxContainer(MessageDeveloperModeGroup)", "DeveloperModeGroup"]});
children.splice(index > -1 ? index : children.length, 0, BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.ContextMenuItems.Group, {
2019-12-20 14:01:16 +01:00
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.ContextMenuItems.Item, {
label: "Add to ChatAliases",
action: _ => {
BDFDB.ContextMenuUtils.close(e.instance);
2020-01-15 10:45:35 +01:00
this.openAddModal(text.replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/\t/g, "\\t"));
2019-12-20 14:01:16 +01:00
}
})
2019-12-10 13:59:44 +01:00
}));
2019-01-17 23:48:29 +01:00
}
2019-01-26 22:45:19 +01:00
2019-12-21 01:14:53 +01:00
processChannelAutoComplete (e) {
2019-12-21 02:11:03 +01:00
let minLength = BDFDB.DataUtils.get(this, "amounts", "minAliasLength");
2019-12-21 01:14:53 +01:00
e.instance.state.autocompleteOptions.ALIASES = {
matches: (firstChar, rest, isFirstWord) => {
2020-01-21 12:56:26 +01:00
let currentLastWord = BDFDB.SlateUtils.getCurrentWord(e.instance.props.editorRef.current).word || "";
2019-12-21 02:11:03 +01:00
if (currentLastWord.length >= minLength) for (let word in this.aliases) {
2019-12-21 01:14:53 +01:00
let aliasdata = this.aliases[word];
if (!aliasdata.regex && aliasdata.autoc) {
if (aliasdata.exact) {
if (aliasdata.case && word.indexOf(currentLastWord) == 0) return true;
else if (!aliasdata.case && word.toLowerCase().indexOf(currentLastWord.toLowerCase()) == 0) return true;
2019-12-20 14:01:16 +01:00
}
2019-12-21 01:14:53 +01:00
else {
if (aliasdata.case && word.indexOf(currentLastWord) > -1) return true;
else if (!aliasdata.case && word.toLowerCase().indexOf(currentLastWord.toLowerCase()) > -1) return true;
2019-12-20 14:01:16 +01:00
}
}
}
2019-12-21 01:14:53 +01:00
return false;
},
queryResults: (rest) => {
2020-01-21 12:56:26 +01:00
let currentLastWord = BDFDB.SlateUtils.getCurrentWord(e.instance.props.editorRef.current).word || "";
2019-12-21 01:14:53 +01:00
let matches = [];
for (let word in this.aliases) {
if (matches.length >= BDFDB.DiscordConstants.MAX_AUTOCOMPLETE_RESULTS) break;
let aliasdata = Object.assign({word}, this.aliases[word]);
if (!aliasdata.regex && aliasdata.autoc) {
if (aliasdata.exact) {
if (aliasdata.case && word.indexOf(currentLastWord) == 0) matches.push(aliasdata);
else if (!aliasdata.case && word.toLowerCase().indexOf(currentLastWord.toLowerCase()) == 0) matches.push(aliasdata);
2018-10-11 10:21:26 +02:00
}
2019-12-21 01:14:53 +01:00
else {
if (aliasdata.case && word.indexOf(currentLastWord) > -1) matches.push(aliasdata);
else if (!aliasdata.case && word.toLowerCase().indexOf(currentLastWord.toLowerCase()) > -1) matches.push(aliasdata);
2019-12-11 08:51:09 +01:00
}
2018-10-11 10:21:26 +02:00
}
2019-12-21 01:14:53 +01:00
}
if (matches.length) return {aliases: matches};
},
renderResults: (rest, currentSelected, setSelected, chooseSelected, autocompletes) => {
return [
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.AutocompleteComponents.Title, {
title: [
"Aliases: ",
BDFDB.ReactUtils.createElement("strong", {
2020-01-21 12:56:26 +01:00
children: BDFDB.SlateUtils.getCurrentWord(e.instance.props.editorRef.current).word || ""
2019-12-21 01:14:53 +01:00
})
]
}),
autocompletes.aliases.map((aliasdata, i) => BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.AutocompleteComponents.Generic, {
onClick: chooseSelected,
onHover: setSelected,
index: i,
selected: currentSelected === i,
2019-12-22 14:30:41 +01:00
alias: aliasdata,
2019-12-21 01:14:53 +01:00
text: aliasdata.word,
description: aliasdata.replace,
}))
].flat(10).filter(n => n);
},
getPlainText: (eventOrIndex, autocompletes) => {
2019-12-22 14:30:41 +01:00
let aliasdata = eventOrIndex._targetInst ? eventOrIndex._targetInst.memoizedProps.alias : typeof eventOrIndex == "number" && autocompletes.aliases[eventOrIndex];
return aliasdata.word;
2019-12-21 01:14:53 +01:00
},
getRawText: (eventOrIndex, autocompletes) => {
2019-12-22 14:30:41 +01:00
let aliasdata = eventOrIndex._targetInst ? eventOrIndex._targetInst.memoizedProps.alias : typeof eventOrIndex == "number" && autocompletes.aliases[eventOrIndex];
2020-01-18 16:42:02 +01:00
return aliasdata.file ? aliasdata.word : BDFDB.StringUtils.insertNRST(aliasdata.replace);
2018-10-11 10:21:26 +02:00
}
2019-12-21 01:14:53 +01:00
};
if (e.instance.state.autocompleteType == "COMMAND" && BDFDB.ArrayUtils.is(e.instance.state.autocompletes.commands)) {
for (let i in e.instance.state.autocompletes.commands) if (e.instance.state.autocompletes.commands[i].alias) e.instance.state.autocompletes.commands[i] = null;
e.instance.state.autocompletes.commands = e.instance.state.autocompletes.commands.filter(n => n);
2020-01-21 12:56:26 +01:00
let currentLastWord = BDFDB.SlateUtils.getCurrentWord(e.instance.props.editorRef.current).word || "";
2019-12-21 01:14:53 +01:00
let commandAliases = BDFDB.ObjectUtils.filter(this.aliases, key => key.startsWith("/"), true);
2019-12-21 02:11:03 +01:00
if (currentLastWord.length >= minLength) for (let word in commandAliases) {
2019-12-21 01:14:53 +01:00
if (e.instance.state.autocompletes.commands.length >= BDFDB.DiscordConstants.MAX_AUTOCOMPLETE_RESULTS) break;
let aliasdata = commandAliases[word];
2020-01-18 16:42:02 +01:00
let command = {command: word.slice(1), description: BDFDB.StringUtils.insertNRST(aliasdata.replace), alias:true};
2018-10-11 10:21:26 +02:00
if (!aliasdata.regex && aliasdata.autoc) {
if (aliasdata.exact) {
2019-12-21 01:14:53 +01:00
if (aliasdata.case && word.indexOf(currentLastWord) == 0) e.instance.state.autocompletes.commands.push(command);
else if (!aliasdata.case && word.toLowerCase().indexOf(currentLastWord.toLowerCase()) == 0) e.instance.state.autocompletes.commands.push(command);
2018-10-11 10:21:26 +02:00
}
else {
2019-12-21 01:14:53 +01:00
if (aliasdata.case && word.indexOf(currentLastWord) > -1) e.instance.state.autocompletes.commands.push(command);
else if (!aliasdata.case && word.toLowerCase().indexOf(currentLastWord.toLowerCase()) > -1) e.instance.state.autocompletes.commands.push(command);
2018-10-11 10:21:26 +02:00
}
}
}
}
}
2019-12-21 01:14:53 +01:00
2019-12-21 11:35:33 +01:00
processChannelTextAreaForm (e) {
if (!BDFDB.ModuleUtils.isPatched(this, e.instance, "handleSendMessage")) BDFDB.ModuleUtils.patch(this, e.instance, "handleSendMessage", {before: e2 => {
this.handleSubmit(e, e2, 0);
}}, true);
}
processMessageEditor (e) {
if (!BDFDB.ModuleUtils.isPatched(this, e.instance, "onSubmit")) BDFDB.ModuleUtils.patch(this, e.instance, "onSubmit", {before: e2 => {
this.handleSubmit(e, e2, 0);
}}, true);
}
processUpload (e) {
if (!BDFDB.ModuleUtils.isPatched(this, e.instance, "submitUpload")) BDFDB.ModuleUtils.patch(this, e.instance, "submitUpload", {before: e2 => {
this.handleSubmit(e, e2, 1);
}}, true);
}
handleSubmit (e, e2, textIndex) {
let messageData = this.formatText(e2.methodArguments[textIndex]);
if (messageData) {
if (messageData.text != null) {
e2.methodArguments[textIndex] = messageData.text;
if (!messageData.text) {
e.instance.props.textValue = "";
2020-01-21 12:56:26 +01:00
if (e.instance.props.richValue) e.instance.props.richValue = BDFDB.SlateUtils.copyRichValue("", e.instance.props.richValue);
2019-12-21 11:35:33 +01:00
BDFDB.ReactUtils.forceUpdate(e.instance);
2019-12-21 01:14:53 +01:00
}
2019-12-21 11:35:33 +01:00
}
2020-01-18 16:42:02 +01:00
if (messageData.files.length > 0 && (BDFDB.DMUtils.isDMChannel(e.instance.props.channel.id) || BDFDB.UserUtils.can("ATTACH_FILES"))) {
2019-12-21 11:35:33 +01:00
BDFDB.LibraryModules.UploadUtils.instantBatchUpload(e.instance.props.channel.id, messageData.files);
}
2018-10-11 10:21:26 +02:00
}
}
formatText (text) {
text = text.replace(/([\n\t\r])/g, " $1 ");
2018-10-11 10:21:26 +02:00
var newText = [], files = [], wordAliases = {}, multiAliases = {};
for (let word in this.aliases) {
if (!this.aliases[word].regex && word.indexOf(" ") == -1) wordAliases[word] = this.aliases[word];
else multiAliases[word] = this.aliases[word];
}
for (let word of text.trim().split(" ")) {
newText.push(this.useAliases(word, wordAliases, files, true));
}
newText = newText.length == 1 ? newText[0] : newText.join(" ");
newText = newText.replace(/ ([\n\t\r]) /g, "$1");
2018-10-11 10:21:26 +02:00
newText = this.useAliases(newText, multiAliases, files, false);
return {text:newText, files};
}
useAliases (string, aliases, files, singleword) {
for (let word in aliases) {
let aliasdata = aliases[word];
2019-10-23 11:10:01 +02:00
let escpAlias = aliasdata.regex ? word : BDFDB.StringUtils.regEscape(word);
2018-10-11 10:21:26 +02:00
let result = true, replaced = false, tempstring1 = string, tempstring2 = "";
let regstring = aliasdata.exact ? "^" + escpAlias + "$" : escpAlias;
while (result != null) {
result = new RegExp(regstring, (aliasdata.case ? "" : "i") + (aliasdata.exact ? "" : "g")).exec(tempstring1);
if (result) {
replaced = true;
2019-10-23 11:10:01 +02:00
let replace = aliasdata.file ? "" : BDFDB.StringUtils.insertNRST(aliasdata.replace);
2019-12-20 14:01:16 +01:00
if (result.length > 1) for (let i = 1; i < result.length; i++) replace = replace.replace(new RegExp("\\\\" + i + "|\\$" + i, "g"), result[i]);
2018-10-11 10:21:26 +02:00
tempstring2 += tempstring1.slice(0, result.index + result[0].length).replace(result[0], replace);
tempstring1 = tempstring1.slice(result.index + result[0].length);
if (aliasdata.file && typeof aliasdata.filedata == "string") {
2019-12-20 14:01:16 +01:00
let filedata = JSON.parse(aliasdata.filedata);
2019-09-07 21:13:13 +02:00
files.push(new File([Uint8Array.from(atob(filedata.data), c => c.charCodeAt(0))], filedata.name, {type:filedata.type}));
2018-10-11 10:21:26 +02:00
}
if (aliasdata.regex && regstring.indexOf("^") == 0) result = null;
}
if (!result) tempstring2 += tempstring1;
2018-10-11 10:21:26 +02:00
}
if (replaced) {
string = tempstring2;
if (singleword) break;
}
}
return string;
}
2019-09-04 12:34:02 +02:00
2019-02-06 15:46:34 +01:00
openAddModal (wordvalue) {
2019-12-20 14:01:16 +01:00
let values = {
wordvalue,
replacevalue: ""
2019-02-06 15:46:34 +01:00
};
2019-12-20 14:01:16 +01:00
BDFDB.ModalUtils.open(this, {
size: "MEDIUM",
header: "Add to ChatAliases",
subheader: "",
children: [
this.createInputs(values),
BDFDB.ArrayUtils.remove(Object.keys(this.defaults.configs), "file").map(key => BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsItem, {
type: "Switch",
className: BDFDB.disCN.marginbottom8 + " input-config" + key,
label: this.defaults.configs[key].description,
value: this.defaults.configs[key].value
}))
].flat(10).filter(n => n),
buttons: [{
key: "ADDBUTTON",
disabled: !Object.keys(values).every(valuename => values[valuename]),
contents: BDFDB.LanguageUtils.LanguageStrings.ADD,
color: "BRAND",
close: true,
click: modal => {
let configs = {};
for (let key in this.defaults.configs) {
let configinput = modal.querySelector(`.input-config${key} ${BDFDB.dotCN.switchinner}`);
if (configinput) configs[key] = configinput.checked;
}
this.saveWord(values.wordvalue, values.replacevalue, modal.querySelector(".input-replacevalue input[type='file']"), configs);
}
}]
2019-02-06 15:46:34 +01:00
});
2019-12-20 14:01:16 +01:00
}
createInputs (values) {
return [{title:"Replace:", type:"", error:"Choose a Wordvalue", valuename:"wordvalue"}, {title:"With:", type:"file", error:"Choose a Replacevalue", valuename:"replacevalue"}].map(inputdata => BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.FormComponents.FormItem, {
title: inputdata.title,
className: BDFDB.disCN.marginbottom8 + " input-" + inputdata.valuename,
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.TextInput, {
type: inputdata.type,
useFilepath: inputdata.type == "file",
value: values[inputdata.valuename],
placeholder: values[inputdata.valuename],
autoFocus: inputdata.valuename == "replacevalue",
errorMessage: !values[inputdata.valuename] && inputdata.error,
onChange: (value, instance) => {
values[inputdata.valuename] = value.trim();
if (values[inputdata.valuename]) delete instance.props.errorMessage;
else instance.props.errorMessage = inputdata.error;
let addbuttonins = BDFDB.ReactUtils.findOwner(BDFDB.ReactUtils.findOwner(instance, {name:["BDFDB_Modal", "BDFDB_SettingsPanel"], up:true}), {key:"ADDBUTTON"});
if (addbuttonins) {
addbuttonins.props.disabled = !Object.keys(values).every(valuename => values[valuename]);
BDFDB.ReactUtils.forceUpdate(addbuttonins);
}
}
})
}));
}
2019-09-04 12:34:02 +02:00
2019-12-20 14:01:16 +01:00
saveWord (wordvalue, replacevalue, fileselection, configs = BDFDB.DataUtils.get(this, "configs")) {
if (!wordvalue || !replacevalue || !fileselection) return;
var filedata = null;
if (fileselection.files && fileselection.files[0] && BDFDB.LibraryRequires.fs.existsSync(replacevalue)) {
filedata = JSON.stringify({
data: BDFDB.LibraryRequires.fs.readFileSync(replacevalue).toString("base64"),
name: fileselection.files[0].name,
type: fileselection.files[0].type
});
}
this.aliases[wordvalue] = {
replace: replacevalue,
filedata: filedata,
case: configs.case,
exact: wordvalue.indexOf(" ") > -1 ? false : configs.exact,
autoc: configs.regex ? false : configs.autoc,
regex: configs.regex,
file: filedata != null
};
BDFDB.DataUtils.save(this.aliases, this, "words");
2019-02-06 15:46:34 +01:00
}
2018-10-11 10:21:26 +02:00
}