BetterDiscordAddons/Plugins/ChatAliases/ChatAliases.plugin.js

584 lines
39 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-11 08:51:09 +01:00
getVersion () {return "2.0.2";}
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-11 08:51:09 +01:00
"fixed":[["Little Fix","Sendings works again, but some stuff is still broken"]]
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 = {
after: {
2019-12-11 08:51:09 +01:00
ChannelTextArea: ["render", "componentDidMount"]
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 () {
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-02-06 15:46:34 +01:00
this.chataliasesAddModalMarkup =
2019-04-18 09:28:20 +02:00
`<span class="${this.name}-modal BDFDB-modal">
2019-02-06 15:46:34 +01:00
<div class="${BDFDB.disCN.backdrop}"></div>
<div class="${BDFDB.disCN.modal}">
<div class="${BDFDB.disCN.modalinner}">
<div class="${BDFDB.disCNS.modalsub + BDFDB.disCN.modalsizemedium}">
2019-10-08 11:51:41 +02:00
<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;">
2019-02-06 15:46:34 +01:00
<div class="${BDFDB.disCN.flexchild}" style="flex: 1 1 auto;">
2019-09-07 21:13:13 +02:00
<h4 class="${BDFDB.disCNS.h4 + BDFDB.disCNS.defaultcolor + BDFDB.disCN.h4defaultmargin}">Add to ChatAliases</h4>
2019-10-09 14:18:28 +02:00
<div class="${BDFDB.disCNS.modalguildname + BDFDB.disCNS.small + BDFDB.disCNS.titlesize12 + BDFDB.disCNS.height16 + BDFDB.disCN.primary}"></div>
2019-02-06 15:46:34 +01:00
</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>
2019-10-09 14:18:28 +02:00
<div class="${BDFDB.disCNS.scrollerwrap + BDFDB.disCNS.modalcontent + BDFDB.disCNS.scrollerthemed + BDFDB.disCN.scrollerthemeghosthairline}">
2019-02-06 15:46:34 +01:00
<div class="${BDFDB.disCNS.scroller + BDFDB.disCN.modalsubinner}">
2019-10-08 11:51:41 +02:00
<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;">
2019-10-09 14:18:28 +02:00
<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;">
2019-02-06 15:46:34 +01:00
</div>
2019-10-08 11:51:41 +02:00
<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;">
2019-10-09 14:18:28 +02:00
<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;">
2019-02-06 15:46:34 +01:00
<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>
2019-10-22 18:55:25 +02:00
${BDFDB.ArrayUtils.remove(Object.keys(this.defaults.configs), "file").map((key, i) =>
2019-10-08 11:51:41 +02:00
`<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;">
2019-10-09 14:18:28 +02:00
<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>
2019-02-06 15:46:34 +01:00
<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>
2019-09-18 10:19:56 +02:00
<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}">
2019-02-06 15:46:34 +01:00
<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>`;
2018-10-11 10:21:26 +02:00
}
getSettingsPanel () {
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-10-22 19:49:57 +02:00
var amounts = BDFDB.DataUtils.get(this, "amounts");
2019-10-09 14:18:28 +02:00
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">`;
2018-10-11 10:21:26 +02:00
for (let key in settings) {
2019-10-09 14:18:28 +02:00
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>`;
2018-10-11 10:21:26 +02:00
}
2019-02-26 12:16:08 +01:00
for (let key in amounts) {
2019-10-09 14:18:28 +02:00
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>`;
2019-02-26 12:16:08 +01:00
}
2019-10-09 14:18:28 +02:00
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">`;
2019-02-06 15:46:34 +01:00
for (let config in this.defaults.configs) {
2019-11-08 15:53:36 +01:00
settingshtml += `<div class="${BDFDB.disCNS.margintop8 + BDFDB.disCNS.settingstableheadersize + BDFDB.disCNS.titlesize10 + BDFDB.disCNS.primary + BDFDB.disCN.weightbold} BDFDB-tableheadercolumn">${config.toUpperCase()}</div>`;
2018-10-11 10:21:26 +02:00
}
2019-04-18 09:28:20 +02:00
settingshtml += `</div></div><div class="BDFDB-settings-inner-list alias-list ${BDFDB.disCN.marginbottom8}">`;
2018-10-11 10:21:26 +02:00
for (let word in this.aliases) {
2019-10-23 11:10:01 +02:00
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)}">`;
2019-02-06 15:46:34 +01:00
for (let config in this.defaults.configs) {
2019-09-18 10:19:56 +02:00
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>`;
2018-10-11 10:21:26 +02:00
}
settingshtml += `</div><div word="${word}" action="remove" class="${BDFDB.disCN.hovercardbutton} remove-word"></div></div>`;
}
settingshtml += `</div>`;
2019-10-09 14:18:28 +02:00
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>`;
2019-10-22 20:16:05 +02:00
var infoHidden = BDFDB.DataUtils.load(this, "hideInfo", "hideInfo");
2019-10-08 11:51:41 +02:00
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>`;
2019-04-18 09:28:20 +02:00
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>`;
2018-10-11 10:21:26 +02:00
settingshtml += `</div>`;
2019-10-23 11:10:01 +02:00
let settingspanel = BDFDB.DOMUtils.create(settingshtml);
2018-10-11 10:21:26 +02:00
2019-01-17 23:48:29 +01:00
BDFDB.initElements(settingspanel, this);
2018-10-11 10:21:26 +02:00
2019-10-22 18:55:25 +02:00
BDFDB.ListenerUtils.add(this, settingspanel, "keypress", ".wordInputs", e => {if (e.which == 13) this.updateContainer(settingspanel, e.currentTarget);});
BDFDB.ListenerUtils.add(this, settingspanel, "keyup", BDFDB.dotCN.gamenameinput, e => {this.updateWord(e.currentTarget);});
BDFDB.ListenerUtils.add(this, settingspanel, "click", ".btn-addword, .remove-word, .remove-all", e => {this.updateContainer(settingspanel, e.currentTarget);});
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);});
2018-10-11 10:21:26 +02:00
return settingspanel;
}
//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.ListenerUtils.add(document, "click", e => {
2019-10-23 11:10:01 +02:00
if (!e.target.tagName === "TEXTAREA") BDFDB.DOMUtils.remove(".autocompleteAliases", ".autocompleteAliasesRow");
2018-10-11 10:21:26 +02:00
});
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
updateContainer (settingspanel, ele) {
2019-02-26 12:16:08 +01:00
var wordvalue = null, replacevalue = null, action = ele.getAttribute("action");
var update = () => {
2019-10-22 19:49:57 +02:00
BDFDB.DataUtils.save(this.aliases, this, "words");
2019-02-06 15:54:49 +01:00
var containerhtml = ``;
for (let word in this.aliases) {
2019-10-23 11:10:01 +02:00
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)}">`;
2019-02-06 15:54:49 +01:00
for (let config in this.defaults.configs) {
2019-09-18 10:19:56 +02:00
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>`;
2019-02-06 15:54:49 +01:00
}
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);
};
2018-10-11 10:21:26 +02:00
if (action == "add") {
var wordinput = settingspanel.querySelector("#input-wordvalue");
var replaceinput = settingspanel.querySelector("#input-replacevalue");
2019-02-06 15:46:34 +01:00
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"));
2018-10-11 10:21:26 +02:00
wordinput.value = null;
replaceinput.value = null;
2019-02-06 15:54:49 +01:00
update();
2018-10-11 10:21:26 +02:00
}
}
else if (action == "remove") {
2019-02-26 12:16:08 +01:00
wordvalue = ele.getAttribute("word");
2018-10-11 10:21:26 +02:00
if (wordvalue) {
delete this.aliases[wordvalue];
2019-02-06 15:54:49 +01:00
update();
2018-10-11 10:21:26 +02:00
}
}
else if (action == "removeall") {
2019-10-30 13:27:14 +01:00
BDFDB.ModalUtils.confirm(this, "Are you sure you want to remove all added Words from your list?", () => {
2018-10-11 10:21:26 +02:00
this.aliases = {};
2019-02-06 15:54:49 +01:00
update();
});
2018-10-11 10:21:26 +02:00
}
}
2019-09-04 12:34:02 +02:00
2019-10-22 19:49:57 +02:00
saveWord (wordvalue, replacevalue, fileselection, configs = BDFDB.DataUtils.get(this, "configs")) {
2019-02-06 15:46:34 +01:00
if (!wordvalue || !replacevalue || !fileselection) return;
var filedata = null;
2019-09-07 21:13:13 +02:00
if (fileselection.files && fileselection.files[0] && BDFDB.LibraryRequires.fs.existsSync(replacevalue)) {
2019-02-06 15:46:34 +01:00
filedata = JSON.stringify({
2019-09-07 21:13:13 +02:00
data: BDFDB.LibraryRequires.fs.readFileSync(replacevalue).toString("base64"),
2019-02-06 15:46:34 +01:00
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
};
}
2018-10-11 10:21:26 +02:00
updateWord (ele) {
2019-11-01 11:09:32 +01:00
BDFDB.TimeUtils.clear(ele.updateTimeout);
ele.updateTimeout = BDFDB.TimeUtils.timeout(() => {
2018-10-11 10:21:26 +02:00
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];
2019-10-22 19:49:57 +02:00
BDFDB.DataUtils.save(this.aliases, this, "words");
2018-10-11 10:21:26 +02:00
}
},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;
2019-10-22 19:49:57 +02:00
BDFDB.DataUtils.save(this.aliases, this, "words");
2018-10-11 10:21:26 +02:00
}
}
2019-01-17 23:48:29 +01:00
toggleInfo (ele) {
2019-10-23 11:10:01 +02:00
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");
2018-10-11 10:21:26 +02:00
}
2019-01-26 22:45:19 +01:00
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()) {
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());
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-10 13:59:44 +01:00
if (text && BDFDB.DataUtils.get(this, "settings", "addContextMenu")) this.appendItem(e, text);
2019-02-06 15:46:34 +01:00
}
}
2019-10-22 23:04:35 +02:00
2019-12-10 13:59:44 +01:00
appendItem (e, text) {
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-09-11 12:14:43 +02:00
children: [
2019-12-05 09:10:14 +01:00
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.ContextMenuItems.Item, {
2019-09-11 12:14:43 +02:00
label: "Add to ChatAliases",
2019-11-21 11:38:04 +01:00
action: _ => {
2019-12-10 13:59:44 +01:00
BDFDB.ContextMenuUtils.close(e.instance);
2019-09-11 12:14:43 +02:00
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-11 08:51:09 +01:00
if (e.returnvalue) {
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);
}
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);
}
2018-10-11 10:21:26 +02:00
}
2019-12-11 08:51:09 +01:00
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);
2018-10-11 10:21:26 +02:00
}
2019-01-26 22:45:19 +01:00
2019-12-11 08:51:09 +01:00
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);});
});
}
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);
if (result.length > 1) for (var 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") {
var 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-10-23 11:10:01 +02:00
let chataliasesAddModal = BDFDB.DOMUtils.create(this.chataliasesAddModalMarkup);
2019-02-06 15:46:34 +01:00
let wordvalueinput = chataliasesAddModal.querySelector("#input-wordvalue");
let replacevalueinput = chataliasesAddModal.querySelector("#input-replacevalue");
let addbutton = chataliasesAddModal.querySelector(".btn-add");
2019-09-04 12:34:02 +02:00
2019-02-06 15:46:34 +01:00
wordvalueinput.value = wordvalue || "";
BDFDB.appendModal(chataliasesAddModal);
2019-09-04 12:34:02 +02:00
2019-02-06 15:46:34 +01:00
let checkInputs = () => {
let validinputs = [wordvalueinput, replacevalueinput];
let invalidinputs = [];
let type = "";
if (!wordvalueinput.value.trim()) {
2019-10-22 18:55:25 +02:00
BDFDB.ArrayUtils.remove(validinputs, wordvalueinput);
2019-02-06 15:46:34 +01:00
invalidinputs.push(wordvalueinput);
type += "Wordvalue";
}
if (!replacevalueinput.value.trim()) {
2019-10-22 18:55:25 +02:00
BDFDB.ArrayUtils.remove(validinputs, replacevalueinput);
2019-02-06 15:46:34 +01:00
invalidinputs.push(replacevalueinput);
type += ((type ? " and " : "") + "Replacevalue");
}
if (type) addDisabledTooltip(invalidinputs, type);
else {
addbutton.disabled = false;
addbutton.style.removeProperty("pointer-events");
2019-10-23 11:10:01 +02:00
BDFDB.DOMUtils.remove(".chataliases-disabled-tooltip");
2019-02-06 15:46:34 +01:00
}
2019-10-23 11:10:01 +02:00
BDFDB.DOMUtils.removeClass(validinputs, "invalid");
2019-02-06 15:46:34 +01:00
};
let addDisabledTooltip = (invalidinputs, type) => {
2019-10-23 11:10:01 +02:00
BDFDB.DOMUtils.remove(".chataliases-disabled-tooltip");
2019-02-06 15:46:34 +01:00
addbutton.disabled = true;
2019-10-23 11:10:01 +02:00
BDFDB.DOMUtils.addClass(invalidinputs, "invalid");
2019-02-06 15:46:34 +01:00
addbutton.style.setProperty("pointer-events", "none", "important");
2019-10-22 18:55:25 +02:00
BDFDB.TooltipUtils.create(addbutton, "Choose a " + type, {type: "right", color: "red", selector: "chataliases-disabled-tooltip"});
2019-02-06 15:46:34 +01:00
};
wordvalueinput.addEventListener("input", checkInputs);
replacevalueinput.addEventListener("input", checkInputs);
2019-09-04 12:34:02 +02:00
2019-10-22 18:55:25 +02:00
BDFDB.ListenerUtils.addToChildren(chataliasesAddModal, "click", BDFDB.dotCNC.backdrop + BDFDB.dotCNC.modalclose + ".btn-add", () => {
2019-10-23 11:10:01 +02:00
BDFDB.DOMUtils.remove(".chataliases-disabled-tooltip");
2019-02-06 15:46:34 +01:00
});
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);
2019-10-22 19:49:57 +02:00
BDFDB.DataUtils.save(this.aliases, this, "words");
2019-02-06 15:46:34 +01:00
});
wordvalueinput.focus();
2019-09-04 12:34:02 +02:00
2019-11-01 11:09:32 +01:00
BDFDB.TimeUtils.timeout(checkInputs, 500);
2019-02-06 15:46:34 +01:00
}
2018-10-11 10:21:26 +02:00
}