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 {
2019-01-10 11:09:05 +01:00
getName ( ) { return "ChatAliases" ; }
2019-09-11 12:14:43 +02:00
getVersion ( ) { return "2.0.1" ; }
2019-01-10 11:09:05 +01:00
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-09-11 12:14:43 +02:00
"fixed" : [ [ "Light Theme Update" , "Fixed bugs for the Light Theme Update, which broke 99% of my plugins" ] ]
2019-02-06 15:46:34 +01:00
} ;
2019-09-04 12:34:02 +02:00
2019-01-17 23:48:29 +01:00
this . patchModules = {
"ChannelTextArea" : "componentDidMount" ,
"StandardSidebarView" : "componentWillUnmount"
} ;
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}" > < / d i v >
< div class = "${BDFDB.disCN.modal}" >
< div class = "${BDFDB.disCN.modalinner}" >
< div class = "${BDFDB.disCNS.modalsub + BDFDB.disCN.modalsizemedium}" >
2019-09-18 10:19:56 +02:00
< div class = "${BDFDB.disCNS.flex + BDFDB.disCNS.horizontal + BDFDB.disCNS.directionrow + 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 < / h 4 >
2019-02-06 15:46:34 +01:00
< div class = "${BDFDB.disCNS.modalguildname + BDFDB.disCNS.small + BDFDB.disCNS.size12 + BDFDB.disCNS.height16 + BDFDB.disCN.primary}" > < / d i v >
< / d i v >
< 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" > < / p a t h >
< 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" > < / p a t h >
< / g >
< / s v g >
< / d i v >
< / b u t t o n >
< / d i v >
< div class = "${BDFDB.disCNS.scrollerwrap + BDFDB.disCNS.modalcontent + BDFDB.disCNS.scrollerthemed + BDFDB.disCN.themeghosthairline}" >
< div class = "${BDFDB.disCNS.scroller + BDFDB.disCN.modalsubinner}" >
2019-09-18 10:19:56 +02:00
< div class = "${BDFDB.disCNS.flex + BDFDB.disCNS.horizontal + BDFDB.disCNS.directionrow + 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.size16 + BDFDB.disCNS.height24 + BDFDB.disCN.flexchild}" style = "flex: 0 0 auto;" > Replace : < / h 3 >
2019-02-06 15:46:34 +01:00
< input action = "add" type = "text" placeholder = "Wordvalue" class = "${BDFDB.disCNS.inputdefault + BDFDB.disCNS.input + BDFDB.disCN.size16} wordInputs" id = "input-wordvalue" style = "flex: 1 1 auto;" >
< / d i v >
2019-09-18 10:19:56 +02:00
< div class = "${BDFDB.disCNS.flex + BDFDB.disCNS.horizontal + BDFDB.disCNS.directionrow + 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.size16 + BDFDB.disCNS.height24 + BDFDB.disCN.flexchild}" style = "flex: 0 0 auto;" > With : < / h 3 >
2019-02-06 15:46:34 +01:00
< input action = "add" type = "text" placeholder = "Replacevalue" class = "${BDFDB.disCNS.inputdefault + BDFDB.disCNS.input + BDFDB.disCN.size16} 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}" > < / d i v >
< input id = "input-file" type = "file" style = "display:none!important;" >
< / b u t t o n >
< / d i v >
$ { BDFDB . removeFromArray ( Object . keys ( this . defaults . configs ) , "file" ) . map ( ( key , i ) =>
2019-09-18 10:19:56 +02:00
` <div class=" ${ BDFDB . disCNS . flex + BDFDB . disCNS . horizontal + BDFDB . disCNS . directionrow + 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.size16 + BDFDB.disCNS.height24 + BDFDB.disCN.flexchild}" style = "flex: 1 1 auto;" > $ { this . defaults . configs [ key ] . description } < / h 3 >
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' : '' ) } > < / d i v >
< / d i v > ` ) . j o i n ( ' ' ) }
< / d i v >
< / d i v >
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}" > < / d i v >
< / b u t t o n >
< / d i v >
< / d i v >
< / d i v >
< / d i v >
< / s p a n > ` ;
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 ;
2018-10-11 10:21:26 +02:00
var settings = BDFDB . getAllData ( this , "settings" ) ;
2019-02-26 12:16:08 +01:00
var amounts = BDFDB . getAllData ( this , "amounts" ) ;
2019-09-18 10:19:56 +02:00
var settingshtml = ` <div class=" ${ this . name } -settings BDFDB-settings"><div class=" ${ BDFDB . disCNS . titledefault + BDFDB . disCNS . size18 + 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-09-18 10:19:56 +02:00
settingshtml += ` <div class=" ${ BDFDB . disCNS . flex + BDFDB . disCNS . horizontal + BDFDB . disCNS . directionrow + 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 . size16 + 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-09-18 10:19:56 +02:00
settingshtml += ` <div class=" ${ BDFDB . disCNS . flex + BDFDB . disCNS . horizontal + BDFDB . disCNS . directionrow + 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 . size16 + BDFDB . disCN . flexchild } " style="flex: 1 1 auto;"> ${ this . defaults . amounts [ key ] . description } </h3><div class=" ${ BDFDB . disCN . inputwrapper } inputNumberWrapper ${ BDFDB . disCNS . vertical + BDFDB . disCNS . flex2 + BDFDB . disCNS . directioncolumn } " 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 . size16 } amount-input"></div></div> ` ;
2019-02-26 12:16:08 +01:00
}
2019-09-18 10:19:56 +02:00
settingshtml += ` <div class=" ${ BDFDB . disCNS . flex + BDFDB . disCNS . horizontal + BDFDB . disCNS . directionrow + 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 . size16 + 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 . size16 } 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 . directionrow + 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 . size16 + 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 . size16 } 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 . directionrow + 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 . size16 + BDFDB . disCNS . height24 + BDFDB . disCN . flexchild } BDFDB-tableheadertext">List of Chataliases:</h3><div class=" ${ BDFDB . disCNS . flex2 + BDFDB . disCNS . horizontal + BDFDB . disCNS . directionrow + 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-08-21 17:05:37 +02:00
settingshtml += ` <div class=" ${ BDFDB . disCNS . margintop8 + BDFDB . disCNS . tableheadersize + BDFDB . disCNS . size10 + 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-09-18 10:19:56 +02:00
settingshtml += ` <div class=" ${ BDFDB . disCNS . flex + BDFDB . disCNS . vertical + BDFDB . disCNS . directioncolumn + 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 . encodeToHTML ( word ) } "><input type="text" word=" ${ word } " action="edit" class=" ${ BDFDB . disCNS . gamename + BDFDB . disCN . gamenameinput } replace-name" value=" ${ BDFDB . encodeToHTML ( 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-09-18 10:19:56 +02:00
settingshtml += ` <div class=" ${ BDFDB . disCNS . flex + BDFDB . disCNS . horizontal + BDFDB . disCNS . directionrow + 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 . size16 + 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> ` ;
2018-10-11 10:21:26 +02:00
var infoHidden = BDFDB . loadData ( "hideInfo" , this , "hideInfo" ) ;
2019-09-18 10:19:56 +02:00
settingshtml += ` <div class=" ${ BDFDB . disCNS . flex + BDFDB . disCNS . horizontal + BDFDB . disCNS . directionrow + 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-01-17 23:48:29 +01:00
let settingspanel = BDFDB . htmlToElement ( 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-01-17 23:48:29 +01:00
BDFDB . addEventListener ( this , settingspanel , "keypress" , ".wordInputs" , e => { if ( e . which == 13 ) this . updateContainer ( settingspanel , e . currentTarget ) ; } ) ;
BDFDB . addEventListener ( this , settingspanel , "keyup" , BDFDB . dotCN . gamenameinput , e => { this . updateWord ( e . currentTarget ) ; } ) ;
BDFDB . addEventListener ( this , settingspanel , "click" , ".btn-addword, .remove-word, .remove-all" , e => { this . updateContainer ( settingspanel , e . currentTarget ) ; } ) ;
BDFDB . addEventListener ( this , settingspanel , "click" , BDFDB . dotCN . checkboxinput , e => { this . updateConfig ( e . currentTarget ) ; } ) ;
BDFDB . addEventListener ( 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" ) ;
libraryScript . setAttribute ( "src" , "https://mwittrien.github.io/BetterDiscordAddons/Plugins/BDFDB.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
this . libLoadTimeout = setTimeout ( ( ) => {
libraryScript . remove ( ) ;
2019-09-07 21:13:13 +02:00
BDFDB . LibraryRequires . request ( "https://mwittrien.github.io/BetterDiscordAddons/Plugins/BDFDB.js" , ( error , response , body ) => {
2019-05-26 13:55:26 +02:00
if ( body ) {
libraryScript = document . createElement ( "script" ) ;
libraryScript . setAttribute ( "id" , "BDFDBLibraryScript" ) ;
libraryScript . setAttribute ( "type" , "text/javascript" ) ;
libraryScript . setAttribute ( "date" , performance . now ( ) ) ;
libraryScript . innerText = body ;
document . head . appendChild ( libraryScript ) ;
}
this . initialize ( ) ;
} ) ;
} , 15000 ) ;
}
else if ( global . BDFDB && typeof BDFDB === "object" && BDFDB . loaded ) this . initialize ( ) ;
2018-10-11 10:21:26 +02:00
this . startTimeout = setTimeout ( ( ) => { this . initialize ( ) ; } , 30000 ) ;
}
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 ;
2018-10-11 10:21:26 +02:00
BDFDB . loadMessage ( this ) ;
this . aliases = BDFDB . loadAllData ( this , "words" ) ;
2019-01-26 22:45:19 +01:00
2019-01-17 23:48:29 +01:00
BDFDB . addEventListener ( document , "click" , e => {
if ( ! e . target . tagName === "TEXTAREA" ) BDFDB . removeEles ( ".autocompleteAliases" , ".autocompleteAliasesRow" ) ;
2018-10-11 10:21:26 +02:00
} ) ;
2019-01-26 22:45:19 +01:00
2019-01-17 23:48:29 +01:00
BDFDB . WebModules . forceAllUpdates ( this ) ;
2018-10-11 10:21:26 +02:00
}
else {
2019-02-12 21:56:34 +01:00
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-09-04 12:34:02 +02:00
BDFDB . removeEles ( ".autocompleteAliases" , ".autocompleteAliasesRow" ) ;
2018-10-11 10:21:26 +02:00
BDFDB . unloadMessage ( this ) ;
}
}
// 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-02-06 15:54:49 +01:00
BDFDB . saveAllData ( this . aliases , this , "words" ) ;
var containerhtml = ` ` ;
for ( let word in this . aliases ) {
2019-09-18 10:19:56 +02:00
containerhtml += ` <div class=" ${ BDFDB . disCNS . flex + BDFDB . disCNS . vertical + BDFDB . disCNS . directioncolumn + 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 . encodeToHTML ( word ) } "><input type="text" word=" ${ word } " action="edit" class=" ${ BDFDB . disCNS . gamename + BDFDB . disCN . gamenameinput } replace-name" value=" ${ BDFDB . encodeToHTML ( 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-02-06 15:54:49 +01:00
BDFDB . openConfirmModal ( 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-02-06 15:46:34 +01:00
saveWord ( wordvalue , replacevalue , fileselection , configs = BDFDB . getAllData ( this , "configs" ) ) {
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 ) {
clearTimeout ( ele . updateTimeout ) ;
ele . updateTimeout = setTimeout ( ( ) => {
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 . saveAllData ( 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 . saveAllData ( this . aliases , this , "words" ) ;
}
}
2019-01-17 23:48:29 +01:00
toggleInfo ( ele ) {
2019-04-26 15:15:06 +02:00
BDFDB . toggleClass ( ele . querySelector ( "svg" ) , BDFDB . disCN . directionright ) ;
2019-01-17 23:48:29 +01:00
BDFDB . toggleEles ( ele . nextElementSibling ) ;
BDFDB . saveData ( "hideInfo" , BDFDB . isEleHidden ( ele . nextElementSibling ) , this , "hideInfo" ) ;
2018-10-11 10:21:26 +02:00
}
2019-01-26 22:45:19 +01:00
2019-09-11 12:14:43 +02:00
onNativeContextMenu ( instance , menu , returnvalue ) {
if ( instance . props && instance . props . value && instance . props . value . trim ( ) && ! menu . querySelector ( ` ${ this . name } -contextMenuItem ` ) ) {
if ( ( instance . props . type == "NATIVE_TEXT" || instance . props . type == "CHANNEL_TEXT_AREA" ) && BDFDB . getData ( "addContextMenu" , this , "settings" ) ) this . appendItem ( menu , returnvalue , instance . props . value . trim ( ) ) ;
2019-02-06 15:46:34 +01:00
}
}
2019-09-11 12:14:43 +02:00
onMessageContextMenu ( instance , menu , returnvalue ) {
if ( instance . props && instance . props . message && instance . props . channel && instance . props . target && ! menu . querySelector ( ` ${ this . name } -contextMenuItem ` ) ) {
2019-02-06 15:46:34 +01:00
let text = document . getSelection ( ) . toString ( ) . trim ( ) ;
2019-09-11 12:14:43 +02:00
if ( text && BDFDB . getData ( "addContextMenu" , this , "settings" ) ) this . appendItem ( menu , returnvalue , text ) ;
2019-02-06 15:46:34 +01:00
}
}
2019-09-11 12:14:43 +02:00
appendItem ( menu , returnvalue , text ) {
2019-09-11 14:31:41 +02:00
let [ children , index ] = BDFDB . getContextMenuGroupAndIndex ( returnvalue , [ "FluxContainer(MessageDeveloperModeGroup)" , "DeveloperModeGroup" ] ) ;
2019-09-11 12:14:43 +02:00
const itemgroup = BDFDB . React . createElement ( BDFDB . LibraryComponents . ContextMenuItemGroup , {
className : ` BDFDB-contextMenuItemGroup ${ this . name } -contextMenuItemGroup ` ,
children : [
BDFDB . React . createElement ( BDFDB . LibraryComponents . ContextMenuItem , {
label : "Add to ChatAliases" ,
className : ` BDFDB-contextMenuItem ${ this . name } -contextMenuItem ${ this . name } -addalias-contextMenuItem ` ,
action : e => {
BDFDB . closeContextMenu ( menu ) ;
this . openAddModal ( text ) ;
}
} )
]
2019-02-06 15:46:34 +01:00
} ) ;
2019-09-11 12:14:43 +02:00
if ( index > - 1 ) children . splice ( index , 0 , itemgroup ) ;
else children . push ( itemgroup ) ;
2019-02-06 15:46:34 +01:00
}
2019-09-11 12:14:43 +02:00
processStandardSidebarView ( instance , wrapper , returnvalue ) {
2019-01-17 23:48:29 +01:00
if ( this . SettingsUpdated ) {
delete this . SettingsUpdated ;
2019-01-26 23:54:46 +01:00
BDFDB . WebModules . forceAllUpdates ( this ) ;
2019-01-17 23:48:29 +01:00
}
}
2019-01-26 22:45:19 +01:00
2019-09-11 12:14:43 +02:00
processChannelTextArea ( instance , wrapper , returnvalue ) {
2019-02-26 12:16:08 +01:00
if ( instance . props && instance . props . channel && instance . props . type ) {
2019-01-17 23:48:29 +01:00
var textarea = wrapper . querySelector ( "textarea" ) ;
if ( ! textarea ) return ;
var settings = BDFDB . getAllData ( this , "settings" ) ;
BDFDB . addEventListener ( this , textarea , "input" , ( ) => {
2018-10-11 10:21:26 +02:00
if ( this . format ) {
this . format = false ;
textarea . focus ( ) ;
textarea . selectionStart = 0 ;
textarea . selectionEnd = textarea . value . length ;
if ( document . activeElement == textarea ) {
var messageInput = this . formatText ( textarea . value ) ;
2019-09-07 21:13:13 +02:00
if ( messageInput ) {
if ( messageInput . text != null ) {
document . execCommand ( "insertText" , false , messageInput . text ? messageInput . text + " " : "" ) ;
}
if ( messageInput . files . length > 0 && ( instance . props . channel . type == 1 || BDFDB . isUserAllowedTo ( "ATTACH_FILES" ) ) ) {
BDFDB . LibraryModules . UploadUtils . instantBatchUpload ( instance . props . channel . id , messageInput . files ) ;
}
2018-10-11 10:21:26 +02:00
}
}
}
2019-01-17 23:48:29 +01:00
} ) ;
BDFDB . addEventListener ( this , textarea , "keydown" , e => {
2018-10-11 10:21:26 +02:00
let autocompletemenu = textarea . parentElement . querySelector ( BDFDB . dotCN . autocomplete ) ;
2019-01-17 23:48:29 +01:00
if ( autocompletemenu && ( e . which == 9 || e . which == 13 ) ) {
if ( BDFDB . containsClass ( autocompletemenu . querySelector ( BDFDB . dotCN . autocompleteselected ) . parentElement , "autocompleteAliasesRow" ) ) {
2019-02-06 15:46:34 +01:00
BDFDB . stopEvent ( e ) ;
2019-09-04 12:34:02 +02:00
this . swapWordWithAlias ( textarea ) ;
2018-10-11 10:21:26 +02:00
}
}
2019-01-17 23:48:29 +01:00
else if ( autocompletemenu && ( e . which == 38 || e . which == 40 ) ) {
2018-10-11 10:21:26 +02:00
let autocompleteitems = autocompletemenu . querySelectorAll ( BDFDB . dotCN . autocompleteselectable + ":not(.autocompleteAliasesSelector)" ) ;
let selected = autocompletemenu . querySelector ( BDFDB . dotCN . autocompleteselected ) ;
2019-01-17 23:48:29 +01:00
if ( BDFDB . containsClass ( selected , "autocompleteAliasesSelector" ) || autocompleteitems [ e . which == 38 ? 0 : ( autocompleteitems . length - 1 ) ] == selected ) {
2019-02-06 15:46:34 +01:00
BDFDB . stopEvent ( e ) ;
2018-10-11 10:21:26 +02:00
let next = this . getNextSelection ( autocompletemenu , null , e . which == 38 ? false : true ) ;
2019-01-17 23:48:29 +01:00
BDFDB . removeClass ( selected , BDFDB . disCN . autocompleteselected ) ;
2019-01-25 10:52:48 +01:00
BDFDB . addClass ( selected , BDFDB . disCN . autocompleteselector ) ;
2019-01-17 23:48:29 +01:00
BDFDB . addClass ( next , BDFDB . disCN . autocompleteselected ) ;
2018-10-11 10:21:26 +02:00
}
}
else if ( textarea . value && ! e . shiftKey && e . which == 13 && ! autocompletemenu && textarea . value . indexOf ( "s/" ) != 0 ) {
this . format = true ;
2019-01-17 23:48:29 +01:00
textarea . dispatchEvent ( new Event ( "input" ) ) ;
2018-10-11 10:21:26 +02:00
}
2019-01-10 11:09:05 +01:00
else if ( ! e . ctrlKey && e . which != 16 && settings . addAutoComplete && textarea . selectionStart == textarea . selectionEnd && textarea . selectionEnd == textarea . value . length ) {
2019-01-29 16:49:49 +01:00
clearTimeout ( textarea . ChatAliasAutocompleteTimeout ) ;
textarea . ChatAliasAutocompleteTimeout = setTimeout ( ( ) => { this . addAutoCompleteMenu ( textarea ) ; } , 100 ) ;
2018-10-11 10:21:26 +02:00
}
2019-01-26 22:45:19 +01:00
2019-01-17 23:48:29 +01:00
if ( ! e . ctrlKey && e . which != 38 && e . which != 40 && ! ( e . which == 39 && textarea . selectionStart == textarea . selectionEnd && textarea . selectionEnd == textarea . value . length ) ) BDFDB . removeEles ( ".autocompleteAliases" , ".autocompleteAliasesRow" ) ;
} ) ;
BDFDB . addEventListener ( this , textarea , "click" , e => {
if ( settings . addAutoComplete && textarea . selectionStart == textarea . selectionEnd && textarea . selectionEnd == textarea . value . length ) setImmediate ( ( ) => { this . addAutoCompleteMenu ( textarea ) ; } ) ;
2018-10-11 10:21:26 +02:00
} ) ;
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 ;
2019-01-10 11:09:05 +01:00
let words = textarea . value . split ( /\s/ ) ;
2018-10-11 10:21:26 +02:00
let lastword = words [ words . length - 1 ] . trim ( ) ;
if ( words . length == 1 && BDFDB . isPluginEnabled ( "WriteUpperCase" ) ) {
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-02-27 17:27:40 +01:00
if ( lastword && BDFDB . getData ( "minAliasLength" , this , "amounts" ) <= 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 ;
}
}
}
if ( ! BDFDB . isObjectEmpty ( matchedaliases ) ) {
let autocompletemenu = textarea . parentElement . querySelector ( BDFDB . dotCNS . autocomplete + BDFDB . dotCN . autocompleteinner ) , amount = 15 ;
if ( ! autocompletemenu ) {
2019-01-10 11:09:05 +01:00
autocompletemenu = BDFDB . htmlToElement ( ` <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-01-10 11:09:05 +01:00
let autocompleterowheader = BDFDB . htmlToElement ( ` <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 . size12 + BDFDB . disCNS . height16 + BDFDB . disCN . weightsemibold } ">Aliases: <strong class="lastword"> ${ BDFDB . encodeToHTML ( lastword ) } </strong></div></div></div> ` ) ;
autocompletemenu . appendChild ( autocompleterowheader ) ;
2019-01-17 23:48:29 +01:00
BDFDB . addEventListener ( this , autocompletemenu , "mouseenter" , BDFDB . dotCN . autocompleteselectable , e => {
2019-01-25 10:52:48 +01:00
var selected = autocompletemenu . querySelectorAll ( BDFDB . dotCN . autocompleteselected ) ;
BDFDB . removeClass ( selected , BDFDB . disCN . autocompleteselected ) ;
BDFDB . addClass ( selected , BDFDB . disCN . autocompleteselector ) ;
2019-01-17 23:48:29 +01:00
BDFDB . addClass ( e . currentTarget , BDFDB . disCN . autocompleteselected ) ;
} ) ;
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-09-18 10:19:56 +02:00
let autocompleterow = BDFDB . htmlToElement ( ` <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 . directionrow + 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 . encodeToHTML ( word ) } </div><div class=" ${ BDFDB . disCNS . autocompletedescription + BDFDB . disCN . flexchild } "> ${ BDFDB . encodeToHTML ( matchedaliases [ word ] . replace ) } </div></div></div></div> ` ) ;
2019-01-17 23:48:29 +01:00
autocompleterow . querySelector ( BDFDB . dotCN . autocompleteselectable ) . addEventListener ( "click" , ( ) => { this . swapWordWithAlias ( textarea ) ; } ) ;
2019-01-10 11:09:05 +01:00
autocompletemenu . appendChild ( autocompleterow ) ;
2018-10-11 10:21:26 +02:00
}
if ( ! autocompletemenu . querySelector ( BDFDB . dotCN . autocompleteselected ) ) {
2019-01-17 23:48:29 +01:00
BDFDB . 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-01-10 11:09:05 +01:00
BDFDB . removeEles ( ".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 ) {
2019-01-10 11:09:05 +01:00
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 ( " " ) ;
2019-01-10 11:09:05 +01:00
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 ] ;
let escpAlias = aliasdata . regex ? word : BDFDB . regEscape ( word ) ;
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 ;
let replace = aliasdata . file ? "" : BDFDB . insertNRST ( aliasdata . replace ) ;
2018-12-14 22:29:09 +01:00
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 ;
}
2019-01-10 11:09:05 +01:00
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 ) {
let chataliasesAddModal = BDFDB . htmlToElement ( this . chataliasesAddModalMarkup ) ;
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 ( ) ) {
BDFDB . removeFromArray ( validinputs , wordvalueinput ) ;
invalidinputs . push ( wordvalueinput ) ;
type += "Wordvalue" ;
}
if ( ! replacevalueinput . value . trim ( ) ) {
BDFDB . removeFromArray ( validinputs , replacevalueinput ) ;
invalidinputs . push ( replacevalueinput ) ;
type += ( ( type ? " and " : "" ) + "Replacevalue" ) ;
}
if ( type ) addDisabledTooltip ( invalidinputs , type ) ;
else {
addbutton . disabled = false ;
addbutton . style . removeProperty ( "pointer-events" ) ;
BDFDB . removeEles ( ".chataliases-disabled-tooltip" ) ;
}
BDFDB . removeClass ( validinputs , "invalid" ) ;
} ;
let addDisabledTooltip = ( invalidinputs , type ) => {
BDFDB . removeEles ( ".chataliases-disabled-tooltip" ) ;
addbutton . disabled = true ;
BDFDB . addClass ( invalidinputs , "invalid" ) ;
addbutton . style . setProperty ( "pointer-events" , "none" , "important" ) ;
BDFDB . createTooltip ( "Choose a " + type , addbutton , { type : "right" , color : "red" , selector : "chataliases-disabled-tooltip" } ) ;
} ;
wordvalueinput . addEventListener ( "input" , checkInputs ) ;
replacevalueinput . addEventListener ( "input" , checkInputs ) ;
2019-09-04 12:34:02 +02:00
2019-02-06 15:46:34 +01:00
BDFDB . addChildEventListener ( chataliasesAddModal , "click" , BDFDB . dotCNC . backdrop + BDFDB . dotCNC . modalclose + ".btn-add" , ( ) => {
BDFDB . removeEles ( ".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 . saveAllData ( this . aliases , this , "words" ) ;
} ) ;
wordvalueinput . focus ( ) ;
2019-09-04 12:34:02 +02:00
2019-02-06 15:46:34 +01:00
setTimeout ( checkInputs , 500 ) ;
}
2018-10-11 10:21:26 +02:00
}