BetterDiscordAddons/Plugins/ChatAliases/ChatAliases.plugin.js

597 lines
27 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";}
2019-12-20 14:01:16 +01:00
getVersion () {return "2.0.3";}
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-20 14:01:16 +01:00
"fixed":[["New WYSIWYG Textarea","Fixed for the new WYSIWYG Textarea that is hidden by experiments"]],
"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: {
ChannelTextArea: "render"
},
2019-11-14 17:56:26 +01:00
after: {
2019-12-20 14:01:16 +01:00
ChannelTextArea: "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 = {}) {
2019-01-22 11:28:32 +01:00
if (!global.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']"));
this.refreshSettings(settingspanel, collapseStates);
}
}),
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");
this.refreshSettings(settingspanel, collapseStates);
}
})
}));
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");
this.refreshSettings(settingspanel, collapseStates);
});
},
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);
}
refreshSettings (settingspanel, collapseStates) {
settingspanel.parentElement.appendChild(this.getSettingsPanel(collapseStates));
settingspanel.remove();
2018-10-11 10:21:26 +02:00
}
//legacy
load () {}
start () {
2019-02-04 09:13:15 +01:00
if (!global.BDFDB) global.BDFDB = {myPlugins:{}};
if (global.BDFDB && global.BDFDB.myPlugins && typeof global.BDFDB.myPlugins == "object") global.BDFDB.myPlugins[this.getName()] = this;
2019-05-26 13:55:26 +02:00
var libraryScript = document.querySelector('head script#BDFDBLibraryScript');
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());
2019-05-26 13:55:26 +02: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
}
else if (global.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) this.initialize();
2019-11-01 10:27:07 +01:00
this.startTimeout = setTimeout(() => {
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 () {
2019-01-17 23:48:29 +01:00
if (global.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 () {
2019-01-17 23:48:29 +01:00
if (global.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) {
2019-10-22 11:37:23 +02:00
this.stopping = true;
2019-10-23 11:10:01 +02:00
BDFDB.DOMUtils.remove(".autocompleteAliases", ".autocompleteAliasesRow");
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
}
}
2019-12-10 13:59:44 +01:00
onMessageContextMenu (e) {
if (e.instance.props.message && e.instance.props.channel && e.instance.props.target) {
2019-02-06 15:46:34 +01:00
let text = document.getSelection().toString().trim();
2019-12-20 14:01:16 +01:00
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);
this.openAddModal(text);
}
})
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-10 13:59:44 +01:00
processChannelTextArea (e) {
if (e.instance.props.channel && e.instance.props.type) {
2019-12-20 14:01:16 +01:00
if (!e.returnvalue) {
e.instance.props.autocompleteOptions.ALIASES = {
matches: (firstChar, rest, isFirstWord) => {
let currentLastWord = e.instance._editorRef.getCurrentWord().word;
for (let word in this.aliases) {
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;
}
else {
if (aliasdata.case && word.indexOf(currentLastWord) > -1) return true;
else if (!aliasdata.case && word.toLowerCase().indexOf(currentLastWord.toLowerCase()) > -1) return true;
}
}
}
return false;
},
queryResults: (rest) => {
let currentLastWord = e.instance._editorRef.getCurrentWord().word;
let matches = [];
for (let word in this.aliases) {
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);
}
else {
if (aliasdata.case && word.indexOf(currentLastWord) > -1) matches.push(aliasdata);
else if (!aliasdata.case && word.toLowerCase().indexOf(currentLastWord.toLowerCase()) > -1) matches.push(aliasdata);
}
}
}
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", {
children: e.instance._editorRef.getCurrentWord().word
})
]
}),
autocompletes.aliases.map((aliasdata, i) => BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.AutocompleteComponents.Generic, {
onClick: chooseSelected,
onHover: setSelected,
index: i,
selected: currentSelected === i,
text: aliasdata.word,
description: aliasdata.replace,
}))
].flat(10).filter(n => n);
},
getPlainText: (eventOrIndex, autocompletes) => {
return eventOrIndex._targetInst ? eventOrIndex._targetInst.memoizedProps.text : (typeof eventOrIndex == "number" ? autocompletes.aliases[eventOrIndex].word : "");
},
getRawText: (eventOrIndex, autocompletes) => {
return eventOrIndex._targetInst ? eventOrIndex._targetInst.memoizedProps.description : (typeof eventOrIndex == "number" ? autocompletes.aliases[eventOrIndex].replace : "");
}
};
if (e.instance.state.autocompleteType == "COMMAND" && BDFDB.ArrayUtils.is(e.instance.props.autocompletes.commands)) {
for (let i in e.instance.props.autocompletes.commands) if (e.instance.props.autocompletes.commands[i].alias) e.instance.props.autocompletes.commands[i] = null;
e.instance.props.autocompletes.commands = e.instance.props.autocompletes.commands.filter(n => n);
let currentLastWord = e.instance._editorRef.getCurrentWord().word;
let commandAliases = BDFDB.ObjectUtils.filter(this.aliases, key => key.startsWith("/"), true);
if (currentLastWord) for (let word in commandAliases) {
if (e.instance.props.autocompletes.commands.length >= BDFDB.DiscordConstants.MAX_AUTOCOMPLETE_RESULTS) break;
let aliasdata = commandAliases[word];
let command = {command: word.slice(1), description: aliasdata.replace, alias:true};
if (!aliasdata.regex && aliasdata.autoc) {
if (aliasdata.exact) {
if (aliasdata.case && word.indexOf(currentLastWord) == 0) e.instance.props.autocompletes.commands.push(command);
else if (!aliasdata.case && word.toLowerCase().indexOf(currentLastWord.toLowerCase()) == 0) e.instance.props.autocompletes.commands.push(command);
}
else {
if (aliasdata.case && word.indexOf(currentLastWord) > -1) e.instance.props.autocompletes.commands.push(command);
else if (!aliasdata.case && word.toLowerCase().indexOf(currentLastWord.toLowerCase()) > -1) e.instance.props.autocompletes.commands.push(command);
}
}
}
}
}
else {
2019-12-11 08:51:09 +01:00
if (!BDFDB.ModuleUtils.isPatched(this, e.instance, "handleSubmit")) BDFDB.ModuleUtils.patch(this, e.instance, "handleSubmit", {before: e2 => {
let messageData = this.formatText(e2.methodArguments[0]);
if (messageData) {
if (messageData.text != null) {
e2.methodArguments[0] = messageData.text;
if (!messageData.text) {
e.instance.props.textValue = "";
if (e.instance.props.richValue) e.instance.props.richValue = BDFDB.StringUtils.copyRichValue("", e.instance.props.richValue);
BDFDB.ReactUtils.forceUpdate(e.instance);
2019-09-07 21:13:13 +02:00
}
2018-10-11 10:21:26 +02:00
}
2019-12-11 08:51:09 +01:00
if (messageData.files.length > 0 && (e.instance.props.channel.type == 1 || BDFDB.UserUtils.can("ATTACH_FILES"))) {
BDFDB.LibraryModules.UploadUtils.instantBatchUpload(e.instance.props.channel.id, messageData.files);
}
2018-10-11 10:21:26 +02:00
}
2019-12-11 08:51:09 +01:00
}}, true);
}
2019-01-17 23:48:29 +01:00
}
2018-10-11 10:21:26 +02:00
}
2019-01-26 22:45:19 +01:00
2018-10-11 10:21:26 +02:00
addAutoCompleteMenu (textarea) {
2019-02-27 17:27:40 +01:00
if (!textarea.value || textarea.parentElement.querySelector(".autocompleteAliasesRow")) return;
let words = textarea.value.split(/\s/);
2018-10-11 10:21:26 +02:00
let lastword = words[words.length-1].trim();
2019-10-23 11:10:01 +02:00
if (words.length == 1 && BDFDB.BDUtils.isPluginEnabled("WriteUpperCase")) {
2018-10-11 10:21:26 +02:00
let first = lastword.charAt(0);
if (first === first.toUpperCase() && lastword.toLowerCase().indexOf("http") == 0) {
lastword = lastword.charAt(0).toLowerCase() + lastword.slice(1);
}
else if (first === first.toLowerCase() && first !== first.toUpperCase() && lastword.toLowerCase().indexOf("http") != 0) {
lastword = lastword.charAt(0).toUpperCase() + lastword.slice(1);
}
}
2019-10-22 20:16:05 +02:00
if (lastword && BDFDB.DataUtils.get(this, "amounts", "minAliasLength") <= lastword.length) {
2018-10-11 10:21:26 +02:00
let matchedaliases = {};
for (let word in this.aliases) {
let aliasdata = this.aliases[word];
if (!aliasdata.regex && aliasdata.autoc) {
if (aliasdata.exact) {
if (aliasdata.case && word.indexOf(lastword) == 0) matchedaliases[word] = aliasdata;
else if (!aliasdata.case && word.toLowerCase().indexOf(lastword.toLowerCase()) == 0) matchedaliases[word] = aliasdata;
}
else {
if (aliasdata.case && word.indexOf(lastword) > -1) matchedaliases[word] = aliasdata;
else if (!aliasdata.case && word.toLowerCase().indexOf(lastword.toLowerCase()) > -1) matchedaliases[word] = aliasdata;
}
}
}
2019-10-22 18:55:25 +02:00
if (!BDFDB.ObjectUtils.isEmpty(matchedaliases)) {
2018-10-11 10:21:26 +02:00
let autocompletemenu = textarea.parentElement.querySelector(BDFDB.dotCNS.autocomplete + BDFDB.dotCN.autocompleteinner), amount = 15;
if (!autocompletemenu) {
2019-10-23 11:10:01 +02:00
autocompletemenu = BDFDB.DOMUtils.create(`<div class="${BDFDB.disCNS.autocomplete + BDFDB.disCN.autocomplete2} autocompleteAliases"><div class="${BDFDB.disCN.autocompleteinner}"></div></div>`);
2018-10-11 10:21:26 +02:00
textarea.parentElement.appendChild(autocompletemenu);
autocompletemenu = autocompletemenu.firstElementChild;
}
else {
amount -= autocompletemenu.querySelectorAll(BDFDB.dotCN.autocompleteselectable).length;
}
2019-10-23 11:10:01 +02:00
let autocompleterowheader = BDFDB.DOMUtils.create(`<div class="${BDFDB.disCNS.autocompleterowvertical + BDFDB.disCN.autocompleterow} autocompleteAliasesRow"><div class="${BDFDB.disCN.autocompleteselector} autocompleteAliasesSelector"><div class="${BDFDB.disCNS.autocompletecontenttitle + BDFDB.disCNS.small + BDFDB.disCNS.titlesize12 + BDFDB.disCNS.height16 + BDFDB.disCN.weightsemibold}">Aliases: <strong class="lastword">${BDFDB.StringUtils.htmlEscape(lastword)}</strong></div></div></div>`);
autocompletemenu.appendChild(autocompleterowheader);
2019-12-10 13:59:44 +01:00
BDFDB.ListenerUtils.add(this, autocompletemenu, "mouseenter", BDFDB.dotCN.autocompleteselectable, event => {
2019-01-25 10:52:48 +01:00
var selected = autocompletemenu.querySelectorAll(BDFDB.dotCN.autocompleteselected);
2019-10-23 11:10:01 +02:00
BDFDB.DOMUtils.removeClass(selected, BDFDB.disCN.autocompleteselected);
BDFDB.DOMUtils.addClass(selected, BDFDB.disCN.autocompleteselector);
2019-12-10 13:59:44 +01:00
BDFDB.DOMUtils.addClass(event.currentTarget, BDFDB.disCN.autocompleteselected);
2019-01-17 23:48:29 +01:00
});
2019-01-26 22:45:19 +01:00
2018-10-11 10:21:26 +02:00
for (let word in matchedaliases) {
if (amount-- < 1) break;
2019-10-23 11:10:01 +02:00
let autocompleterow = BDFDB.DOMUtils.create(`<div class="${BDFDB.disCNS.autocompleterowvertical + BDFDB.disCN.autocompleterow} autocompleteAliasesRow"><div class="${BDFDB.disCNS.autocompleteselector + BDFDB.disCN.autocompleteselectable} autocompleteAliasesSelector"><div class="${BDFDB.disCNS.flex + BDFDB.disCNS.horizontal + BDFDB.disCNS.justifystart + BDFDB.disCNS.aligncenter + BDFDB.disCNS.nowrap + BDFDB.disCN.autocompletecontent}" style="flex: 1 1 auto;"><div class="${BDFDB.disCN.flexchild} aliasword" style="flex: 1 1 auto;">${BDFDB.StringUtils.htmlEscape(word)}</div><div class="${BDFDB.disCNS.autocompletedescription + BDFDB.disCN.flexchild}">${BDFDB.StringUtils.htmlEscape(matchedaliases[word].replace)}</div></div></div></div>`);
2019-12-10 13:59:44 +01:00
autocompleterow.querySelector(BDFDB.dotCN.autocompleteselectable).addEventListener("click", _ => {this.swapWordWithAlias(textarea);});
autocompletemenu.appendChild(autocompleterow);
2018-10-11 10:21:26 +02:00
}
if (!autocompletemenu.querySelector(BDFDB.dotCN.autocompleteselected)) {
2019-10-23 11:10:01 +02:00
BDFDB.DOMUtils.addClass(autocompletemenu.querySelector(".autocompleteAliasesRow " + BDFDB.dotCN.autocompleteselectable), BDFDB.disCN.autocompleteselected);
2018-10-11 10:21:26 +02:00
}
}
}
}
2019-01-26 22:45:19 +01:00
2018-10-11 10:21:26 +02:00
getNextSelection (menu, selected, forward) {
selected = selected ? selected : menu.querySelector(BDFDB.dotCN.autocompleteselected).parentElement;
let next, sibling = forward ? selected.nextElementSibling : selected.previousElementSibling;
if (sibling) {
next = sibling.querySelector(BDFDB.dotCN.autocompleteselectable);
}
else {
let items = menu.querySelectorAll(BDFDB.dotCN.autocompleteselectable);
2019-09-04 12:34:02 +02:00
next = forward ? items[0] : items[items.length-1];
2018-10-11 10:21:26 +02:00
}
return next ? next : this.getNextSelection(menu, sibling, forward);
}
2019-01-26 22:45:19 +01:00
2018-10-11 10:21:26 +02:00
swapWordWithAlias (textarea) {
let aliasword = textarea.parentElement.querySelector(".autocompleteAliasesRow " + BDFDB.dotCN.autocompleteselected + " .aliasword").innerText;
let lastword = textarea.parentElement.querySelector(".autocompleteAliasesRow .lastword").innerText;
if (aliasword && lastword) {
2019-10-23 11:10:01 +02:00
BDFDB.DOMUtils.remove(".autocompleteAliases", ".autocompleteAliasesRow");
2019-09-04 12:34:02 +02:00
textarea.focus();
2018-10-11 10:21:26 +02:00
textarea.selectionStart = textarea.value.length - lastword.length;
textarea.selectionEnd = textarea.value.length;
document.execCommand("insertText", false, aliasword);
textarea.selectionStart = textarea.value.length;
textarea.selectionEnd = textarea.value.length;
}
}
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
}