Update ChatAliases.plugin.js

This commit is contained in:
Mirco Wittrien 2019-12-20 14:01:16 +01:00
parent d2b98836ed
commit 7e4d0fb40e
1 changed files with 324 additions and 311 deletions

View File

@ -3,7 +3,7 @@
class ChatAliases { class ChatAliases {
getName () {return "ChatAliases";} getName () {return "ChatAliases";}
getVersion () {return "2.0.2";} getVersion () {return "2.0.3";}
getAuthor () {return "DevilBro";} getAuthor () {return "DevilBro";}
@ -11,17 +11,27 @@ class ChatAliases {
constructor () { constructor () {
this.changelog = { this.changelog = {
"fixed":[["Little Fix","Sendings works again, but some stuff is still broken"]] "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"]]
}; };
this.patchedModules = { this.patchedModules = {
before: {
ChannelTextArea: "render"
},
after: { after: {
ChannelTextArea: ["render", "componentDidMount"] ChannelTextArea: "render"
} }
}; };
} }
initConstructor () { initConstructor () {
this.css = `
${BDFDB.dotCN.autocompleteicon} {
flex: 0 0 auto;
}
`;
this.defaults = { this.defaults = {
configs: { configs: {
case: {value:false, description:"Handle the wordvalue case sensitive"}, case: {value:false, description:"Handle the wordvalue case sensitive"},
@ -38,103 +48,144 @@ class ChatAliases {
minAliasLength: {value:2, min:1, description:"Minimal Character Length to open Autocomplete-Menu:"} minAliasLength: {value:2, min:1, description:"Minimal Character Length to open Autocomplete-Menu:"}
} }
}; };
this.chataliasesAddModalMarkup =
`<span class="${this.name}-modal BDFDB-modal">
<div class="${BDFDB.disCN.backdrop}"></div>
<div class="${BDFDB.disCN.modal}">
<div class="${BDFDB.disCN.modalinner}">
<div class="${BDFDB.disCNS.modalsub + BDFDB.disCN.modalsizemedium}">
<div class="${BDFDB.disCNS.flex + BDFDB.disCNS.horizontal + BDFDB.disCNS.justifystart + BDFDB.disCNS.aligncenter + BDFDB.disCNS.nowrap + BDFDB.disCN.modalheader}" style="flex: 0 0 auto;">
<div class="${BDFDB.disCN.flexchild}" style="flex: 1 1 auto;">
<h4 class="${BDFDB.disCNS.h4 + BDFDB.disCNS.defaultcolor + BDFDB.disCN.h4defaultmargin}">Add to ChatAliases</h4>
<div class="${BDFDB.disCNS.modalguildname + BDFDB.disCNS.small + BDFDB.disCNS.titlesize12 + BDFDB.disCNS.height16 + BDFDB.disCN.primary}"></div>
</div>
<button type="button" class="${BDFDB.disCNS.modalclose + BDFDB.disCNS.flexchild + BDFDB.disCNS.button + BDFDB.disCNS.buttonlookblank + BDFDB.disCNS.buttoncolorbrand + BDFDB.disCN.buttongrow}">
<div class="${BDFDB.disCN.buttoncontents}">
<svg name="Close" width="18" height="18" viewBox="0 0 12 12" style="flex: 0 1 auto;">
<g fill="none" fill-rule="evenodd">
<path d="M0 0h12v12H0"></path>
<path class="fill" fill="currentColor" d="M9.5 3.205L8.795 2.5 6 5.295 3.205 2.5l-.705.705L5.295 6 2.5 8.795l.705.705L6 6.705 8.795 9.5l.705-.705L6.705 6"></path>
</g>
</svg>
</div>
</button>
</div>
<div class="${BDFDB.disCNS.scrollerwrap + BDFDB.disCNS.modalcontent + BDFDB.disCNS.scrollerthemed + BDFDB.disCN.scrollerthemeghosthairline}">
<div class="${BDFDB.disCNS.scroller + BDFDB.disCN.modalsubinner}">
<div class="${BDFDB.disCNS.flex + BDFDB.disCNS.horizontal + BDFDB.disCNS.justifystart + BDFDB.disCNS.aligncenter + BDFDB.disCNS.nowrap + BDFDB.disCN.marginbottom8}" style="flex: 0 0 auto;">
<h3 class="${BDFDB.disCNS.titledefault + BDFDB.disCNS.marginreset + BDFDB.disCNS.weightmedium + BDFDB.disCNS.titlesize16 + BDFDB.disCNS.height24 + BDFDB.disCN.flexchild}" style="flex: 0 0 auto;">Replace:</h3>
<input action="add" type="text" placeholder="Wordvalue" class="${BDFDB.disCNS.inputdefault + BDFDB.disCNS.input + BDFDB.disCN.titlesize16} wordInputs" id="input-wordvalue" style="flex: 1 1 auto;">
</div>
<div class="${BDFDB.disCNS.flex + BDFDB.disCNS.horizontal + BDFDB.disCNS.justifystart + BDFDB.disCNS.aligncenter + BDFDB.disCNS.nowrap + BDFDB.disCN.marginbottom8}" style="flex: 0 0 auto;">
<h3 class="${BDFDB.disCNS.titledefault + BDFDB.disCNS.marginreset + BDFDB.disCNS.weightmedium + BDFDB.disCNS.titlesize16 + BDFDB.disCNS.height24 + BDFDB.disCN.flexchild}" style="flex: 0 0 auto;">With:</h3>
<input action="add" type="text" placeholder="Replacevalue" class="${BDFDB.disCNS.inputdefault + BDFDB.disCNS.input + BDFDB.disCN.titlesize16} wordInputs" id="input-replacevalue" style="flex: 1 1 auto;">
<button type="button" class="${BDFDB.disCNS.flexchild + BDFDB.disCNS.button + BDFDB.disCNS.buttonlookfilled + BDFDB.disCNS.buttoncolorbrand + BDFDB.disCNS.buttonsizemedium + BDFDB.disCN.buttongrow} file-navigator" style="flex: 0 0 auto;">
<div class="${BDFDB.disCN.buttoncontents}"></div>
<input id="input-file" type="file" style="display:none!important;">
</button>
</div>
${BDFDB.ArrayUtils.remove(Object.keys(this.defaults.configs), "file").map((key, i) =>
`<div class="${BDFDB.disCNS.flex + BDFDB.disCNS.horizontal + BDFDB.disCNS.justifystart + BDFDB.disCNS.aligncenter + BDFDB.disCNS.nowrap + BDFDB.disCN.marginbottom8}" style="flex: 1 1 auto;">
<h3 class="${BDFDB.disCNS.titledefault + BDFDB.disCNS.marginreset + BDFDB.disCNS.weightmedium + BDFDB.disCNS.titlesize16 + BDFDB.disCNS.height24 + BDFDB.disCN.flexchild}" style="flex: 1 1 auto;">${this.defaults.configs[key].description}</h3>
<div class="${BDFDB.disCNS.flexchild + BDFDB.disCNS.switchenabled + BDFDB.disCNS.switch + BDFDB.disCNS.switchvalue + BDFDB.disCNS.switchsizedefault + BDFDB.disCNS.switchsize + BDFDB.disCN.switchthemedefault}" style="flex: 0 0 auto;">
<input type="checkbox" id="input-config${key}" value="${key}" class="${BDFDB.disCNS.switchinnerenabled + BDFDB.disCN.switchinner}"${(this.defaults.configs[key].value ? ' checked' : '')}></div>
</div>`).join('')}
</div>
</div>
<div class="${BDFDB.disCNS.flex + BDFDB.disCNS.horizontalreverse + BDFDB.disCNS.horizontalreverse2 + BDFDB.disCNS.directionrowreverse + BDFDB.disCNS.justifystart + BDFDB.disCNS.alignstretch + BDFDB.disCNS.nowrap + BDFDB.disCN.modalfooter}">
<button type="button" class="btn-close btn-add ${BDFDB.disCNS.button + BDFDB.disCNS.buttonlookfilled + BDFDB.disCNS.buttoncolorbrand + BDFDB.disCNS.buttonsizemedium + BDFDB.disCN.buttongrow}">
<div class="${BDFDB.disCN.buttoncontents}"></div>
</button>
</div>
</div>
</div>
</div>
</span>`;
} }
getSettingsPanel () { getSettingsPanel (collapseStates = {}) {
if (!global.BDFDB || typeof BDFDB != "object" || !BDFDB.loaded || !this.started) return; if (!global.BDFDB || typeof BDFDB != "object" || !BDFDB.loaded || !this.started) return;
let settings = BDFDB.DataUtils.get(this, "settings"); let settings = BDFDB.DataUtils.get(this, "settings");
var amounts = BDFDB.DataUtils.get(this, "amounts"); let amounts = BDFDB.DataUtils.get(this, "amounts");
var settingshtml = `<div class="${this.name}-settings BDFDB-settings"><div class="${BDFDB.disCNS.titledefault + BDFDB.disCNS.titlesize18 + BDFDB.disCNS.height24 + BDFDB.disCNS.weightnormal + BDFDB.disCN.marginbottom8}">${this.name}</div><div class="BDFDB-settings-inner">`; let settingspanel, settingsitems = [], inneritems = [];
for (let key in settings) {
settingshtml += `<div class="${BDFDB.disCNS.flex + BDFDB.disCNS.horizontal + BDFDB.disCNS.justifystart + BDFDB.disCNS.aligncenter + BDFDB.disCNS.nowrap + BDFDB.disCN.marginbottom8}" style="flex: 1 1 auto;"><h3 class="${BDFDB.disCNS.titledefault + BDFDB.disCNS.marginreset + BDFDB.disCNS.weightmedium + BDFDB.disCNS.titlesize16 + BDFDB.disCNS.height24 + BDFDB.disCN.flexchild}" style="flex: 1 1 auto;">${this.defaults.settings[key].description}</h3><div class="${BDFDB.disCNS.flexchild + BDFDB.disCNS.switchenabled + BDFDB.disCNS.switch + BDFDB.disCNS.switchvalue + BDFDB.disCNS.switchsizedefault + BDFDB.disCNS.switchsize + BDFDB.disCN.switchthemedefault}" style="flex: 0 0 auto;"><input type="checkbox" value="settings ${key}" class="${BDFDB.disCNS.switchinnerenabled + BDFDB.disCN.switchinner} settings-switch"${settings[key] ? " checked" : ""}></div></div>`;
}
for (let key in amounts) {
settingshtml += `<div class="${BDFDB.disCNS.flex + BDFDB.disCNS.horizontal + BDFDB.disCNS.justifystart + BDFDB.disCNS.aligncenter + BDFDB.disCNS.nowrap + BDFDB.disCN.marginbottom8}" style="flex: 1 1 auto;"><h3 class="${BDFDB.disCNS.titledefault + BDFDB.disCNS.weightmedium + BDFDB.disCNS.titlesize16 + BDFDB.disCN.flexchild}" style="flex: 1 1 auto;">${this.defaults.amounts[key].description}</h3><div class="${BDFDB.disCN.inputwrapper} inputNumberWrapper ${BDFDB.disCNS.vertical}" style="flex: 1 1 20%;"><span class="numberinput-buttons-zone"><span class="numberinput-button-up"></span><span class="numberinput-button-down"></span></span><input type="number"${(!isNaN(this.defaults.amounts[key].min) && this.defaults.amounts[key].min !== null ? ' min="' + this.defaults.amounts[key].min + '"' : '') + (!isNaN(this.defaults.amounts[key].max) && this.defaults.amounts[key].max !== null ? ' max="' + this.defaults.amounts[key].max + '"' : '')} option="${key}" value="${amounts[key]}" class="${BDFDB.disCNS.inputdefault + BDFDB.disCNS.input + BDFDB.disCN.titlesize16} amount-input"></div></div>`;
}
settingshtml += `<div class="${BDFDB.disCNS.flex + BDFDB.disCNS.horizontal + BDFDB.disCNS.justifystart + BDFDB.disCNS.aligncenter + BDFDB.disCNS.nowrap + BDFDB.disCN.marginbottom8}" style="flex: 0 0 auto;"><h3 class="${BDFDB.disCNS.titledefault + BDFDB.disCNS.marginreset + BDFDB.disCNS.weightmedium + BDFDB.disCNS.titlesize16 + BDFDB.disCNS.height24 + BDFDB.disCN.flexchild}" style="flex: 0 0 auto;">Replace:</h3><input action="add" type="text" placeholder="Wordvalue" class="${BDFDB.disCNS.inputdefault + BDFDB.disCNS.input + BDFDB.disCN.titlesize16} wordInputs" id="input-wordvalue" style="flex: 1 1 auto;"><button action="add" type="button" class="${BDFDB.disCNS.flexchild + BDFDB.disCNS.button + BDFDB.disCNS.buttonlookfilled + BDFDB.disCNS.buttoncolorbrand + BDFDB.disCNS.buttonsizemedium + BDFDB.disCN.buttongrow} btn-add btn-addword" style="flex: 0 0 auto;"><div class="${BDFDB.disCN.buttoncontents}"></div></button></div><div class="${BDFDB.disCNS.flex + BDFDB.disCNS.horizontal + BDFDB.disCNS.justifystart + BDFDB.disCNS.aligncenter + BDFDB.disCNS.nowrap + BDFDB.disCN.marginbottom8}" style="flex: 0 0 auto;"><h3 class="${BDFDB.disCNS.titledefault + BDFDB.disCNS.marginreset + BDFDB.disCNS.weightmedium + BDFDB.disCNS.titlesize16 + BDFDB.disCNS.height24 + BDFDB.disCN.flexchild}" style="flex: 0 0 auto;">With:</h3><input action="add" type="text" placeholder="Replacevalue" class="${BDFDB.disCNS.inputdefault + BDFDB.disCNS.input + BDFDB.disCN.titlesize16} wordInputs" id="input-replacevalue" style="flex: 1 1 auto;"><button type="button" class="${BDFDB.disCNS.flexchild + BDFDB.disCNS.button + BDFDB.disCNS.buttonlookfilled + BDFDB.disCNS.buttoncolorbrand + BDFDB.disCNS.buttonsizemedium + BDFDB.disCN.buttongrow} file-navigator" style="flex: 0 0 auto;"><div class="${BDFDB.disCN.buttoncontents}"></div><input id="input-file" type="file" style="display:none !important;"></button></div>`;
settingshtml += `<div class="${BDFDB.disCNS.flex + BDFDB.disCNS.horizontal + BDFDB.disCNS.justifystart + BDFDB.disCNS.aligncenter + BDFDB.disCNS.nowrap + BDFDB.disCN.marginbottom8} BDFDB-tableheader" table-id="aliases" style="flex: 0 0 auto;"><h3 class="${BDFDB.disCNS.titledefault + BDFDB.disCNS.marginreset + BDFDB.disCNS.weightmedium + BDFDB.disCNS.titlesize16 + BDFDB.disCNS.height24 + BDFDB.disCN.flexchild} BDFDB-tableheadertext">List of Chataliases:</h3><div class="${BDFDB.disCNS.flex2 + BDFDB.disCNS.horizontal + BDFDB.disCNS.justifycenter + BDFDB.disCNS.alignend + BDFDB.disCN.nowrap} BDFDB-tableheadercolumns">`;
for (let config in this.defaults.configs) {
settingshtml += `<div class="${BDFDB.disCNS.margintop8 + BDFDB.disCNS.settingstableheadersize + BDFDB.disCNS.titlesize10 + BDFDB.disCNS.primary + BDFDB.disCN.weightbold} BDFDB-tableheadercolumn">${config.toUpperCase()}</div>`;
}
settingshtml += `</div></div><div class="BDFDB-settings-inner-list alias-list ${BDFDB.disCN.marginbottom8}">`;
for (let word in this.aliases) {
settingshtml += `<div class="${BDFDB.disCNS.flex + BDFDB.disCNS.vertical + BDFDB.disCNS.justifystart + BDFDB.disCNS.aligncenter + BDFDB.disCNS.nowrap + BDFDB.disCNS.margintop4 + BDFDB.disCNS.marginbottom4 + BDFDB.disCN.hovercard}"><div class="${BDFDB.disCN.hovercardinner}"><input type="text" word="${word}" action="edit" class="${BDFDB.disCNS.gamename + BDFDB.disCN.gamenameinput} word-name" value="${BDFDB.StringUtils.htmlEscape(word)}"><input type="text" word="${word}" action="edit" class="${BDFDB.disCNS.gamename + BDFDB.disCN.gamenameinput} replace-name" value="${BDFDB.StringUtils.htmlEscape(this.aliases[word].replace)}">`;
for (let config in this.defaults.configs) {
settingshtml += `<div class="${BDFDB.disCNS.checkboxcontainer + BDFDB.disCN.marginreset} BDFDB-tablecheckbox" table-id="aliases" style="flex: 0 0 auto;"><label class="${BDFDB.disCN.checkboxwrapper}"><input word="${word}" config="${config}" type="checkbox" class="${BDFDB.disCN.checkboxinputdefault}"${this.aliases[word][config] ? " checked" : ""}><div class="${BDFDB.disCNS.checkbox + BDFDB.disCNS.flexcenter + BDFDB.disCNS.flex2 + BDFDB.disCNS.justifystart + BDFDB.disCNS.aligncenter + BDFDB.disCN.checkboxround}"><svg name="Checkmark" width="18" height="18" viewBox="0 0 18 18" xmlns="http://www.w3.org/2000/svg"><g fill="none" fill-rule="evenodd"><polyline stroke="transparent" stroke-width="2" points="3.5 9.5 7 13 15 5"></polyline></g></svg></div></label></div>`;
}
settingshtml += `</div><div word="${word}" action="remove" class="${BDFDB.disCN.hovercardbutton} remove-word"></div></div>`;
}
settingshtml += `</div>`;
settingshtml += `<div class="${BDFDB.disCNS.flex + BDFDB.disCNS.horizontal + BDFDB.disCNS.justifystart + BDFDB.disCNS.aligncenter + BDFDB.disCNS.nowrap + BDFDB.disCN.marginbottom20}" style="flex: 0 0 auto;"><h3 class="${BDFDB.disCNS.titledefault + BDFDB.disCNS.marginreset + BDFDB.disCNS.weightmedium + BDFDB.disCNS.titlesize16 + BDFDB.disCNS.height24 + BDFDB.disCN.flexchild}" style="flex: 1 1 auto;">Remove all added words.</h3><button action="removeall" type="button" class="${BDFDB.disCNS.flexchild + BDFDB.disCNS.button + BDFDB.disCNS.buttonlookfilled + BDFDB.disCNS.buttoncolorred + BDFDB.disCNS.buttonsizemedium + BDFDB.disCN.buttongrow} remove-all" style="flex: 0 0 auto;"><div class="${BDFDB.disCN.buttoncontents}">Reset</div></button></div>`;
var infoHidden = BDFDB.DataUtils.load(this, "hideInfo", "hideInfo");
settingshtml += `<div class="${BDFDB.disCNS.flex + BDFDB.disCNS.horizontal + BDFDB.disCNS.justifystart + BDFDB.disCNS.aligncenter + BDFDB.disCNS.nowrap + BDFDB.disCNS.cursorpointer} toggle-info" style="flex: 1 1 auto;"><svg class="toggle-infoarrow${infoHidden ? (" " + BDFDB.disCN.directionright) : ""}" width="12" height="12" viewBox="0 0 24 24"><path fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" d="M7 10L12 15 17 10"></path></svg><div class="toggle-infotext" style="flex: 1 1 auto;">Information</div></div>`;
settingshtml += `<div class="BDFDB-settings-inner-list info-container" ${infoHidden ? "style='display:none;'" : ""}><div class="${BDFDB.disCNS.description + BDFDB.disCNS.formtext + BDFDB.disCNS.note + BDFDB.disCNS.modedefault + BDFDB.disCN.primary}">Case: Will replace words while comparing lowercase/uppercase. apple => apple, not APPLE or AppLe</div><div class="${BDFDB.disCNS.description + BDFDB.disCNS.formtext + BDFDB.disCNS.note + BDFDB.disCNS.modedefault + BDFDB.disCN.primary}">Not Case: Will replace words while ignoring lowercase/uppercase. apple => apple, APPLE and AppLe</div><div class="${BDFDB.disCNS.description + BDFDB.disCNS.formtext + BDFDB.disCNS.note + BDFDB.disCNS.modedefault + BDFDB.disCN.primary}">Exact: Will replace words that are exactly the replaceword. apple to pear => applepie stays applepie</div><div class="${BDFDB.disCNS.description + BDFDB.disCNS.formtext + BDFDB.disCNS.note + BDFDB.disCNS.modedefault + BDFDB.disCN.primary}">Not Exact: Will replace words anywhere they appear. apple to pear => applepieapple to pearpiepear</div><div class="${BDFDB.disCNS.description + BDFDB.disCNS.formtext + BDFDB.disCNS.note + BDFDB.disCNS.modedefault + BDFDB.disCN.primary}">Autoc: Will appear in the Autocomplete Menu (if enabled).</div><div class="${BDFDB.disCNS.description + BDFDB.disCNS.formtext + BDFDB.disCNS.note + BDFDB.disCNS.modedefault + BDFDB.disCN.primary}">Regex: Will treat the entered wordvalue as a regular expression. <a class="${BDFDB.disCNS.anchor + BDFDB.disCN.anchorunderlineonhover}" target="_blank" href="https://regexr.com/">Help</a></div><div class="${BDFDB.disCNS.description + BDFDB.disCNS.formtext + BDFDB.disCNS.note + BDFDB.disCNS.modedefault + BDFDB.disCN.primary}">File: If the replacevalue is a filepath it will try to upload the file located at the filepath.</div></div>`;
settingshtml += `</div>`;
let settingspanel = BDFDB.DOMUtils.create(settingshtml); 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
}))
}));
BDFDB.initElements(settingspanel, this); return settingspanel = BDFDB.PluginUtils.createSettingsPanel(this, settingsitems);
}
BDFDB.ListenerUtils.add(this, settingspanel, "keypress", ".wordInputs", e => {if (e.which == 13) this.updateContainer(settingspanel, e.currentTarget);}); refreshSettings (settingspanel, collapseStates) {
BDFDB.ListenerUtils.add(this, settingspanel, "keyup", BDFDB.dotCN.gamenameinput, e => {this.updateWord(e.currentTarget);}); settingspanel.parentElement.appendChild(this.getSettingsPanel(collapseStates));
BDFDB.ListenerUtils.add(this, settingspanel, "click", ".btn-addword, .remove-word, .remove-all", e => {this.updateContainer(settingspanel, e.currentTarget);}); settingspanel.remove();
BDFDB.ListenerUtils.add(this, settingspanel, "click", BDFDB.dotCN.checkboxinput, e => {this.updateConfig(e.currentTarget);});
BDFDB.ListenerUtils.add(this, settingspanel, "click", ".toggle-info", e => {this.toggleInfo(e.currentTarget);});
return settingspanel;
} }
//legacy //legacy
@ -168,10 +219,6 @@ class ChatAliases {
this.aliases = BDFDB.DataUtils.load(this, "words"); this.aliases = BDFDB.DataUtils.load(this, "words");
BDFDB.ListenerUtils.add(document, "click", e => {
if (!e.target.tagName === "TEXTAREA") BDFDB.DOMUtils.remove(".autocompleteAliases", ".autocompleteAliasesRow");
});
BDFDB.ModuleUtils.forceAllUpdates(this); BDFDB.ModuleUtils.forceAllUpdates(this);
} }
else console.error(`%c[${this.getName()}]%c`, "color: #3a71c1; font-weight: 700;", "", "Fatal Error: Could not load BD functions!"); else console.error(`%c[${this.getName()}]%c`, "color: #3a71c1; font-weight: 700;", "", "Fatal Error: Could not load BD functions!");
@ -189,109 +236,6 @@ class ChatAliases {
// begin of own functions // begin of own functions
updateContainer (settingspanel, ele) {
var wordvalue = null, replacevalue = null, action = ele.getAttribute("action");
var update = () => {
BDFDB.DataUtils.save(this.aliases, this, "words");
var containerhtml = ``;
for (let word in this.aliases) {
containerhtml += `<div class="${BDFDB.disCNS.flex + BDFDB.disCNS.vertical + BDFDB.disCNS.justifystart + BDFDB.disCNS.aligncenter + BDFDB.disCNS.nowrap + BDFDB.disCNS.margintop4 + BDFDB.disCNS.marginbottom4 + BDFDB.disCN.hovercard}"><div class="${BDFDB.disCN.hovercardinner}"><input type="text" word="${word}" action="edit" class="${BDFDB.disCNS.gamename + BDFDB.disCN.gamenameinput} word-name" value="${BDFDB.StringUtils.htmlEscape(word)}"><input type="text" word="${word}" action="edit" class="${BDFDB.disCNS.gamename + BDFDB.disCN.gamenameinput} replace-name" value="${BDFDB.StringUtils.htmlEscape(this.aliases[word].replace)}">`;
for (let config in this.defaults.configs) {
containerhtml += `<div class="${BDFDB.disCNS.checkboxcontainer + BDFDB.disCN.marginreset} BDFDB-tablecheckbox" table-id="aliases" style="flex: 0 0 auto;"><label class="${BDFDB.disCN.checkboxwrapper}"><input word="${word}" config="${config}" type="checkbox" class="${BDFDB.disCN.checkboxinputdefault}"${this.aliases[word][config] ? " checked" : ""}><div class="${BDFDB.disCNS.checkbox + BDFDB.disCNS.flexcenter + BDFDB.disCNS.flex2 + BDFDB.disCNS.justifystart + BDFDB.disCNS.aligncenter + BDFDB.disCN.checkboxround}"><svg name="Checkmark" width="18" height="18" viewBox="0 0 18 18" xmlns="http://www.w3.org/2000/svg"><g fill="none" fill-rule="evenodd"><polyline stroke="transparent" stroke-width="2" points="3.5 9.5 7 13 15 5"></polyline></g></svg></div></label></div>`;
}
containerhtml += `</div><div word="${word}" action="remove" class="${BDFDB.disCN.hovercardbutton} remove-word"></div></div>`;
}
settingspanel.querySelector(".alias-list").innerHTML = containerhtml;
BDFDB.initElements(settingspanel, this);
};
if (action == "add") {
var wordinput = settingspanel.querySelector("#input-wordvalue");
var replaceinput = settingspanel.querySelector("#input-replacevalue");
if (wordinput.value && wordinput.value.trim().length > 0 && replaceinput.value && replaceinput.value.trim().length > 0) {
this.saveWord(wordinput.value.trim(), replaceinput.value.trim(), settingspanel.querySelector("#input-file"));
wordinput.value = null;
replaceinput.value = null;
update();
}
}
else if (action == "remove") {
wordvalue = ele.getAttribute("word");
if (wordvalue) {
delete this.aliases[wordvalue];
update();
}
}
else if (action == "removeall") {
BDFDB.ModalUtils.confirm(this, "Are you sure you want to remove all added Words from your list?", () => {
this.aliases = {};
update();
});
}
}
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
};
}
updateWord (ele) {
BDFDB.TimeUtils.clear(ele.updateTimeout);
ele.updateTimeout = BDFDB.TimeUtils.timeout(() => {
var card = ele.parentElement.parentElement;
var oldwordvalue = ele.getAttribute("word");
if (oldwordvalue && this.aliases[oldwordvalue]) {
var wordinput = card.querySelector(".word-name");
var replaceinput = card.querySelector(".replace-name");
var removebutton = card.querySelector(".remove-word");
var newwordvalue = wordinput.value;
var newreplacevalue = replaceinput.value;
wordinput.setAttribute("word", newwordvalue);
wordinput.setAttribute("value", newwordvalue);
replaceinput.setAttribute("word", newwordvalue);
replaceinput.setAttribute("value", newreplacevalue);
removebutton.setAttribute("word", newwordvalue);
this.aliases[newwordvalue] = this.aliases[oldwordvalue];
this.aliases[newwordvalue].replace = newreplacevalue;
if (newwordvalue != oldwordvalue) delete this.aliases[oldwordvalue];
BDFDB.DataUtils.save(this.aliases, this, "words");
}
},500);
}
updateConfig (ele) {
var wordvalue = ele.getAttribute("word");
var config = ele.getAttribute("config");
if (wordvalue && this.aliases[wordvalue] && config) {
this.aliases[wordvalue][config] = ele.checked;
BDFDB.DataUtils.save(this.aliases, this, "words");
}
}
toggleInfo (ele) {
BDFDB.DOMUtils.toggleClass(ele.querySelector("svg"), BDFDB.disCN.directionright);
BDFDB.DOMUtils.toggle(ele.nextElementSibling);
BDFDB.DataUtils.save(BDFDB.DOMUtils.isHidden(ele.nextElementSibling), this, "hideInfo", "hideInfo");
}
onSettingsClosed () { onSettingsClosed () {
if (this.SettingsUpdated) { if (this.SettingsUpdated) {
delete this.SettingsUpdated; delete this.SettingsUpdated;
@ -301,35 +245,119 @@ class ChatAliases {
onNativeContextMenu (e) { onNativeContextMenu (e) {
if (e.instance.props.value && e.instance.props.value.trim()) { if (e.instance.props.value && e.instance.props.value.trim()) {
if ((e.instance.props.type == "NATIVE_TEXT" || e.instance.props.type == "CHANNEL_TEXT_AREA") && BDFDB.DataUtils.get(this, "settings", "addContextMenu")) this.appendItem(e, e.instance.props.value.trim()); 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());
} }
} }
onMessageContextMenu (e) { onMessageContextMenu (e) {
if (e.instance.props.message && e.instance.props.channel && e.instance.props.target) { if (e.instance.props.message && e.instance.props.channel && e.instance.props.target) {
let text = document.getSelection().toString().trim(); let text = document.getSelection().toString().trim();
if (text && BDFDB.DataUtils.get(this, "settings", "addContextMenu")) this.appendItem(e, text); if (text && BDFDB.DataUtils.get(this, "settings", "addContextMenu")) this.injectItem(e, text);
} }
} }
appendItem (e, text) { injectItem (e, text) {
let [children, index] = BDFDB.ReactUtils.findChildren(e.returnvalue, {name:["FluxContainer(MessageDeveloperModeGroup)", "DeveloperModeGroup"]}); 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, { children.splice(index > -1 ? index : children.length, 0, BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.ContextMenuItems.Group, {
children: [ children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.ContextMenuItems.Item, {
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.ContextMenuItems.Item, { label: "Add to ChatAliases",
label: "Add to ChatAliases", action: _ => {
action: _ => { BDFDB.ContextMenuUtils.close(e.instance);
BDFDB.ContextMenuUtils.close(e.instance); this.openAddModal(text);
this.openAddModal(text); }
} })
})
]
})); }));
} }
processChannelTextArea (e) { processChannelTextArea (e) {
if (e.instance.props.channel && e.instance.props.type) { if (e.instance.props.channel && e.instance.props.type) {
if (e.returnvalue) { 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 {
if (!BDFDB.ModuleUtils.isPatched(this, e.instance, "handleSubmit")) BDFDB.ModuleUtils.patch(this, e.instance, "handleSubmit", {before: e2 => { if (!BDFDB.ModuleUtils.isPatched(this, e.instance, "handleSubmit")) BDFDB.ModuleUtils.patch(this, e.instance, "handleSubmit", {before: e2 => {
let messageData = this.formatText(e2.methodArguments[0]); let messageData = this.formatText(e2.methodArguments[0]);
if (messageData) { if (messageData) {
@ -347,44 +375,6 @@ class ChatAliases {
} }
}}, true); }}, true);
} }
if (e.node) {
let textarea = e.node.querySelector("textarea");
if (!textarea) return;
let settings = BDFDB.DataUtils.get(this, "settings");
BDFDB.ListenerUtils.add(this, textarea, "keydown", event => {
let autocompletemenu = textarea.parentElement.querySelector(BDFDB.dotCN.autocomplete);
if (autocompletemenu && (event.which == 9 || event.which == 13)) {
if (BDFDB.DOMUtils.containsClass(autocompletemenu.querySelector(BDFDB.dotCN.autocompleteselected).parentElement, "autocompleteAliasesRow")) {
BDFDB.ListenerUtils.stopEvent(event);
this.swapWordWithAlias(textarea);
}
}
else if (autocompletemenu && (event.which == 38 || event.which == 40)) {
let autocompleteitems = autocompletemenu.querySelectorAll(BDFDB.dotCN.autocompleteselectable + ":not(.autocompleteAliasesSelector)");
let selected = autocompletemenu.querySelector(BDFDB.dotCN.autocompleteselected);
if (BDFDB.DOMUtils.containsClass(selected, "autocompleteAliasesSelector") || autocompleteitems[event.which == 38 ? 0 : (autocompleteitems.length-1)] == selected) {
BDFDB.ListenerUtils.stopEvent(event);
let next = this.getNextSelection(autocompletemenu, null, event.which == 38 ? false : true);
BDFDB.DOMUtils.removeClass(selected, BDFDB.disCN.autocompleteselected);
BDFDB.DOMUtils.addClass(selected, BDFDB.disCN.autocompleteselector);
BDFDB.DOMUtils.addClass(next, BDFDB.disCN.autocompleteselected);
}
}
else if (textarea.value && !event.shiftKey && event.which == 13 && !autocompletemenu && textarea.value.indexOf("s/") != 0) {
this.format = true;
textarea.dispatchEvent(new Event("input"));
}
else if (!event.ctrlKey && event.which != 16 && settings.addAutoComplete && textarea.selectionStart == textarea.selectionEnd && textarea.selectionEnd == textarea.value.length) {
BDFDB.TimeUtils.clear(textarea.ChatAliasAutocompleteTimeout);
textarea.ChatAliasAutocompleteTimeout = BDFDB.TimeUtils.timeout(() => {this.addAutoCompleteMenu(textarea);},100);
}
if (!event.ctrlKey && event.which != 38 && event.which != 40 && !(event.which == 39 && textarea.selectionStart == textarea.selectionEnd && textarea.selectionEnd == textarea.value.length)) BDFDB.DOMUtils.remove(".autocompleteAliases", ".autocompleteAliasesRow");
});
BDFDB.ListenerUtils.add(this, textarea, "click", _ => {
if (settings.addAutoComplete && textarea.selectionStart == textarea.selectionEnd && textarea.selectionEnd == textarea.value.length) BDFDB.TimeUtils.timeout(() => {this.addAutoCompleteMenu(textarea);});
});
}
} }
} }
@ -502,11 +492,11 @@ class ChatAliases {
if (result) { if (result) {
replaced = true; replaced = true;
let replace = aliasdata.file ? "" : BDFDB.StringUtils.insertNRST(aliasdata.replace); let replace = aliasdata.file ? "" : BDFDB.StringUtils.insertNRST(aliasdata.replace);
if (result.length > 1) for (var i = 1; i < result.length; i++) replace = replace.replace(new RegExp("\\\\" + i + "|\\$" + i, "g"), result[i]); if (result.length > 1) for (let i = 1; i < result.length; i++) replace = replace.replace(new RegExp("\\\\" + i + "|\\$" + i, "g"), result[i]);
tempstring2 += tempstring1.slice(0, result.index + result[0].length).replace(result[0], replace); tempstring2 += tempstring1.slice(0, result.index + result[0].length).replace(result[0], replace);
tempstring1 = tempstring1.slice(result.index + result[0].length); tempstring1 = tempstring1.slice(result.index + result[0].length);
if (aliasdata.file && typeof aliasdata.filedata == "string") { if (aliasdata.file && typeof aliasdata.filedata == "string") {
var filedata = JSON.parse(aliasdata.filedata); let filedata = JSON.parse(aliasdata.filedata);
files.push(new File([Uint8Array.from(atob(filedata.data), c => c.charCodeAt(0))], filedata.name, {type:filedata.type})); files.push(new File([Uint8Array.from(atob(filedata.data), c => c.charCodeAt(0))], filedata.name, {type:filedata.type}));
} }
if (aliasdata.regex && regstring.indexOf("^") == 0) result = null; if (aliasdata.regex && regstring.indexOf("^") == 0) result = null;
@ -522,62 +512,85 @@ class ChatAliases {
} }
openAddModal (wordvalue) { openAddModal (wordvalue) {
let chataliasesAddModal = BDFDB.DOMUtils.create(this.chataliasesAddModalMarkup); let values = {
let wordvalueinput = chataliasesAddModal.querySelector("#input-wordvalue"); wordvalue,
let replacevalueinput = chataliasesAddModal.querySelector("#input-replacevalue"); replacevalue: ""
let addbutton = chataliasesAddModal.querySelector(".btn-add");
wordvalueinput.value = wordvalue || "";
BDFDB.appendModal(chataliasesAddModal);
let checkInputs = () => {
let validinputs = [wordvalueinput, replacevalueinput];
let invalidinputs = [];
let type = "";
if (!wordvalueinput.value.trim()) {
BDFDB.ArrayUtils.remove(validinputs, wordvalueinput);
invalidinputs.push(wordvalueinput);
type += "Wordvalue";
}
if (!replacevalueinput.value.trim()) {
BDFDB.ArrayUtils.remove(validinputs, replacevalueinput);
invalidinputs.push(replacevalueinput);
type += ((type ? " and " : "") + "Replacevalue");
}
if (type) addDisabledTooltip(invalidinputs, type);
else {
addbutton.disabled = false;
addbutton.style.removeProperty("pointer-events");
BDFDB.DOMUtils.remove(".chataliases-disabled-tooltip");
}
BDFDB.DOMUtils.removeClass(validinputs, "invalid");
}; };
let addDisabledTooltip = (invalidinputs, type) => { BDFDB.ModalUtils.open(this, {
BDFDB.DOMUtils.remove(".chataliases-disabled-tooltip"); size: "MEDIUM",
addbutton.disabled = true; header: "Add to ChatAliases",
BDFDB.DOMUtils.addClass(invalidinputs, "invalid"); subheader: "",
addbutton.style.setProperty("pointer-events", "none", "important"); children: [
BDFDB.TooltipUtils.create(addbutton, "Choose a " + type, {type: "right", color: "red", selector: "chataliases-disabled-tooltip"}); 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);
}
}]
});
}
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);
}
}
})
}));
}
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
}; };
wordvalueinput.addEventListener("input", checkInputs); BDFDB.DataUtils.save(this.aliases, this, "words");
replacevalueinput.addEventListener("input", checkInputs);
BDFDB.ListenerUtils.addToChildren(chataliasesAddModal, "click", BDFDB.dotCNC.backdrop + BDFDB.dotCNC.modalclose + ".btn-add", () => {
BDFDB.DOMUtils.remove(".chataliases-disabled-tooltip");
});
addbutton.addEventListener("click", e => {
let configs = {};
for (let key in this.defaults.configs) {
let configinput = chataliasesAddModal.querySelector("#input-config" + key);
if (configinput) configs[key] = configinput.checked;
}
this.saveWord(wordvalueinput.value.trim(), replacevalueinput.value.trim(), chataliasesAddModal.querySelector("#input-file"), configs);
BDFDB.DataUtils.save(this.aliases, this, "words");
});
wordvalueinput.focus();
BDFDB.TimeUtils.timeout(checkInputs, 500);
} }
} }