2020-10-20 23:25:34 +02:00
/ * *
* @ name ChatAliases
2021-03-05 13:26:41 +01:00
* @ author DevilBro
2020-10-20 23:25:34 +02:00
* @ authorId 278543574059057154
2023-01-29 12:41:55 +01:00
* @ version 2.4 . 6
2021-03-05 13:26:41 +01:00
* @ description Allows you to configure your own Aliases / Commands
2020-10-20 23:25:34 +02:00
* @ invite Jx3TjNS
2020-11-19 16:45:36 +01:00
* @ donate https : //www.paypal.me/MircoWittrien
* @ patreon https : //www.patreon.com/MircoWittrien
2021-03-09 15:10:55 +01:00
* @ website https : //mwittrien.github.io/
* @ source https : //github.com/mwittrien/BetterDiscordAddons/tree/master/Plugins/ChatAliases/
2021-03-10 09:17:37 +01:00
* @ updateUrl https : //mwittrien.github.io/BetterDiscordAddons/Plugins/ChatAliases/ChatAliases.plugin.js
2020-10-20 23:25:34 +02:00
* /
2018-10-11 10:21:26 +02:00
2020-09-19 20:49:33 +02:00
module . exports = ( _ => {
2022-09-01 14:40:11 +02:00
const changeLog = {
2023-01-28 13:19:36 +01:00
"improved" : {
"Alias Order" : "Automatically sets a higher priority for longer aliases, to avoid short aliases from overwritting longer ones"
2022-12-01 15:51:28 +01:00
}
2020-09-19 20:49:33 +02:00
} ;
2020-11-13 19:47:44 +01:00
2022-02-05 21:14:17 +01:00
return ! window . BDFDB _Global || ( ! window . BDFDB _Global . loaded && ! window . BDFDB _Global . started ) ? class {
2022-09-01 14:55:22 +02:00
constructor ( meta ) { for ( let key in meta ) this [ key ] = meta [ key ] ; }
getName ( ) { return this . name ; }
getAuthor ( ) { return this . author ; }
getVersion ( ) { return this . version ; }
getDescription ( ) { return ` The Library Plugin needed for ${ this . name } is missing. Open the Plugin Settings to download it. \n \n ${ this . description } ` ; }
2021-02-01 17:13:13 +01:00
downloadLibrary ( ) {
require ( "request" ) . get ( "https://mwittrien.github.io/BetterDiscordAddons/Library/0BDFDB.plugin.js" , ( e , r , b ) => {
2021-03-05 13:14:18 +01:00
if ( ! e && b && r . statusCode == 200 ) require ( "fs" ) . writeFile ( require ( "path" ) . join ( BdApi . Plugins . folder , "0BDFDB.plugin.js" ) , b , _ => BdApi . showToast ( "Finished downloading BDFDB Library" , { type : "success" } ) ) ;
2021-03-06 14:59:48 +01:00
else BdApi . alert ( "Error" , "Could not download BDFDB Library Plugin. Try again later or download it manually from GitHub: https://mwittrien.github.io/downloader/?library" ) ;
2021-02-01 17:13:13 +01:00
} ) ;
}
2020-09-19 20:49:33 +02:00
2021-01-06 12:38:36 +01:00
load ( ) {
2020-11-19 16:51:14 +01:00
if ( ! window . BDFDB _Global || ! Array . isArray ( window . BDFDB _Global . pluginQueue ) ) window . BDFDB _Global = Object . assign ( { } , window . BDFDB _Global , { pluginQueue : [ ] } ) ;
2020-09-19 20:49:33 +02:00
if ( ! window . BDFDB _Global . downloadModal ) {
window . BDFDB _Global . downloadModal = true ;
2022-09-01 14:55:22 +02:00
BdApi . showConfirmationModal ( "Library Missing" , ` The Library Plugin needed for ${ this . name } is missing. Please click "Download Now" to install it. ` , {
2020-09-19 20:49:33 +02:00
confirmText : "Download Now" ,
cancelText : "Cancel" ,
onCancel : _ => { delete window . BDFDB _Global . downloadModal ; } ,
2020-09-20 08:15:13 +02:00
onConfirm : _ => {
delete window . BDFDB _Global . downloadModal ;
2021-02-01 17:13:13 +01:00
this . downloadLibrary ( ) ;
2020-09-20 08:15:13 +02:00
}
2020-09-19 20:49:33 +02:00
} ) ;
}
2022-09-01 14:55:22 +02:00
if ( ! window . BDFDB _Global . pluginQueue . includes ( this . name ) ) window . BDFDB _Global . pluginQueue . push ( this . name ) ;
2020-10-09 21:09:35 +02:00
}
2021-01-06 12:38:36 +01:00
start ( ) { this . load ( ) ; }
stop ( ) { }
getSettingsPanel ( ) {
2020-11-28 23:12:09 +01:00
let template = document . createElement ( "template" ) ;
2022-09-01 14:55:22 +02:00
template . innerHTML = ` <div style="color: var(--header-primary); font-size: 16px; font-weight: 300; white-space: pre; line-height: 22px;">The Library Plugin needed for ${ this . name } is missing. \n Please click <a style="font-weight: 500;">Download Now</a> to install it.</div> ` ;
2021-02-01 17:13:13 +01:00
template . content . firstElementChild . querySelector ( "a" ) . addEventListener ( "click" , this . downloadLibrary ) ;
2020-11-28 23:12:09 +01:00
return template . content . firstElementChild ;
}
2020-10-09 21:09:35 +02:00
} : ( ( [ Plugin , BDFDB ] ) => {
2022-12-01 15:51:28 +01:00
var aliases = { } ;
2020-09-19 20:49:33 +02:00
2020-10-09 21:09:35 +02:00
return class ChatAliases extends Plugin {
2021-01-06 12:38:36 +01:00
onLoad ( ) {
2020-09-19 20:49:33 +02:00
this . defaults = {
configs : {
2022-12-01 15:51:28 +01:00
case : { value : false , description : "Handles the Word Value case sensitive" } ,
exact : { value : true , description : "Handles the Word Value as an exact Word and not as part of a Word" } ,
regex : { value : false , description : "Handles the Word Value as a RegExp String" }
2020-02-11 12:58:16 +01:00
} ,
2021-05-03 19:59:24 +02:00
general : {
2022-12-01 15:51:28 +01:00
addContextMenu : { value : true , inner : false , description : "Adds a Context Menu Entry to more freely add new Aliases" }
2021-05-03 19:59:24 +02:00
} ,
places : {
normal : { value : true , inner : true , description : "Normal Message Textarea" } ,
2022-12-01 15:51:28 +01:00
edit : { value : true , inner : true , description : "Edit Message Textarea" }
2020-09-19 20:49:33 +02:00
}
} ;
2022-12-01 15:51:28 +01:00
this . modulePatches = {
before : [
"ChannelTextAreaContainer"
]
2020-09-19 20:49:33 +02:00
} ;
2020-02-11 12:58:16 +01:00
}
2020-09-19 20:49:33 +02:00
2021-01-06 12:38:36 +01:00
onStart ( ) {
2020-06-08 20:07:08 +02:00
aliases = BDFDB . DataUtils . load ( this , "words" ) ;
2019-01-26 22:45:19 +01:00
2021-05-03 19:59:24 +02:00
BDFDB . PatchUtils . forceAllUpdates ( this ) ;
2020-02-11 12:58:16 +01:00
}
2020-09-19 20:49:33 +02:00
2021-01-06 12:38:36 +01:00
onStop ( ) {
2021-05-03 19:59:24 +02:00
BDFDB . PatchUtils . forceAllUpdates ( this ) ;
2020-02-11 12:58:16 +01:00
}
2018-10-11 10:21:26 +02:00
2020-09-19 20:49:33 +02:00
getSettingsPanel ( collapseStates = { } ) {
2020-11-23 20:56:13 +01:00
let settingsPanel ;
return settingsPanel = BDFDB . PluginUtils . createSettingsPanel ( this , {
2020-09-19 20:49:33 +02:00
collapseStates : collapseStates ,
2020-11-23 20:56:13 +01:00
children : _ => {
let settingsItems = [ ] ;
settingsItems . push ( BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . CollapseContainer , {
title : "Settings" ,
collapseStates : collapseStates ,
2021-05-03 19:59:24 +02:00
children : Object . keys ( this . defaults . general ) . map ( key => BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . SettingsSaveItem , {
2020-11-23 20:56:13 +01:00
type : "Switch" ,
plugin : this ,
2021-05-03 19:59:24 +02:00
keys : [ "general" , key ] ,
label : this . defaults . general [ key ] . description ,
value : this . settings . general [ key ]
2022-12-01 15:51:28 +01:00
} ) ) . concat ( BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . SettingsPanelList , {
2021-01-26 21:40:18 +01:00
title : "Automatically replace Aliases in:" ,
2021-05-03 19:59:24 +02:00
children : Object . keys ( this . settings . places ) . map ( key => BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . SettingsSaveItem , {
2020-11-23 20:56:13 +01:00
type : "Switch" ,
plugin : this ,
2021-05-03 19:59:24 +02:00
keys : [ "places" , key ] ,
label : this . defaults . places [ key ] . description ,
value : this . settings . places [ key ]
2020-11-23 20:56:13 +01:00
} ) )
} ) )
} ) ) ;
2021-01-26 21:40:18 +01:00
let values = { wordValue : "" , replaceValue : "" } ;
2020-11-23 20:56:13 +01:00
settingsItems . push ( BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . CollapseContainer , {
2021-01-26 21:40:18 +01:00
title : "Add new Alias" ,
2020-11-23 20:56:13 +01:00
collapseStates : collapseStates ,
2020-09-19 20:49:33 +02:00
children : [
2020-11-23 20:56:13 +01:00
BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . SettingsItem , {
type : "Button" ,
2021-01-26 21:40:18 +01:00
label : "Pick a Word Value and Replacement Value:" ,
2021-01-29 20:57:25 +01:00
disabled : ! Object . keys ( values ) . every ( valueName => values [ valueName ] ) ,
2020-11-23 20:56:13 +01:00
children : BDFDB . LanguageUtils . LanguageStrings . ADD ,
2021-01-29 20:57:25 +01:00
ref : instance => { if ( instance ) values . addButton = instance ; } ,
2020-11-23 20:56:13 +01:00
onClick : _ => {
2021-01-29 20:57:25 +01:00
this . saveWord ( values ) ;
2020-11-23 20:56:13 +01:00
BDFDB . PluginUtils . refreshSettingsPanel ( this , settingsPanel , collapseStates ) ;
2020-09-19 20:49:33 +02:00
}
} ) ,
2020-11-23 20:56:13 +01:00
this . createInputs ( values )
] . flat ( 10 ) . filter ( n => n )
} ) ) ;
if ( ! BDFDB . ObjectUtils . isEmpty ( aliases ) ) settingsItems . push ( BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . CollapseContainer , {
2021-01-26 21:40:18 +01:00
title : "Added Aliases" ,
2020-11-23 20:56:13 +01:00
collapseStates : collapseStates ,
children : BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . SettingsList , {
settings : Object . keys ( this . defaults . configs ) ,
2021-01-26 21:40:18 +01:00
data : Object . keys ( aliases ) . map ( ( wordValue , i ) => Object . assign ( { } , aliases [ wordValue ] , {
key : wordValue ,
label : wordValue
2020-11-23 20:56:13 +01:00
} ) ) ,
renderLabel : data => BDFDB . ReactUtils . createElement ( "div" , {
style : { width : "100%" } ,
children : [
BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . TextInput , {
value : data . label ,
placeholder : data . label ,
size : BDFDB . LibraryComponents . TextInput . Sizes . MINI ,
maxLength : 100000000000000000000 ,
onChange : value => {
aliases [ value ] = aliases [ data . label ] ;
delete aliases [ data . label ] ;
data . label = value ;
BDFDB . DataUtils . save ( aliases , this , "words" ) ;
}
} ) ,
BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . TextInput , {
value : data . replace ,
placeholder : data . replace ,
size : BDFDB . LibraryComponents . TextInput . Sizes . MINI ,
maxLength : 100000000000000000000 ,
onChange : value => {
aliases [ data . label ] . replace = value ;
BDFDB . DataUtils . save ( aliases , this , "words" ) ;
}
} )
]
} ) ,
onCheckboxChange : ( value , instance ) => {
aliases [ instance . props . cardId ] [ instance . props . settingId ] = value ;
BDFDB . DataUtils . save ( aliases , this , "words" ) ;
} ,
onRemove : ( e , instance ) => {
delete aliases [ instance . props . cardId ] ;
BDFDB . DataUtils . save ( aliases , this , "words" ) ;
BDFDB . PluginUtils . refreshSettingsPanel ( this , settingsPanel , collapseStates ) ;
}
} )
} ) ) ;
settingsItems . push ( BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . CollapseContainer , {
title : "Remove All" ,
collapseStates : collapseStates ,
children : BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . SettingsItem , {
type : "Button" ,
color : BDFDB . LibraryComponents . Button . Colors . RED ,
2021-01-26 21:40:18 +01:00
label : "Remove all added Aliases" ,
2020-11-23 20:56:13 +01:00
onClick : _ => {
2021-01-26 21:40:18 +01:00
BDFDB . ModalUtils . confirm ( this , "Are you sure you want to remove all added Aliases?" , _ => {
2020-11-23 20:56:13 +01:00
aliases = { } ;
BDFDB . DataUtils . remove ( this , "words" ) ;
BDFDB . PluginUtils . refreshSettingsPanel ( this , settingsPanel , collapseStates ) ;
} ) ;
} ,
children : BDFDB . LanguageUtils . LanguageStrings . REMOVE
} )
} ) ) ;
settingsItems . push ( BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . CollapseContainer , {
title : "Config Guide" ,
collapseStates : collapseStates ,
children : [
2021-01-26 21:40:18 +01:00
"Case: Will replace Words while comparing lowercase/uppercase. apple => apple, not APPLE or AppLe" ,
"Not Case: Will replace Words while ignoring lowercase/uppercase. apple => apple, APPLE and AppLe" ,
"Exact: Will replace Words that are exactly the Replacement Value. apple to pear => applepie stays applepie" ,
"Not Exact: Will replace Words anywhere they appear. apple to pear => applepieapple to pearpiepear" ,
2020-11-23 20:56:13 +01:00
[
2021-01-26 21:40:18 +01:00
"Regex: Will treat the entered Word Value as a Regular Expression - " ,
2020-11-23 20:56:13 +01:00
BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . Anchor , { href : "https://regexr.com/" , children : BDFDB . LanguageUtils . LanguageStrings . HELP + "?" } )
2022-08-20 20:49:32 +02:00
]
2020-11-23 20:56:13 +01:00
] . map ( string => BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . FormComponents . FormText , {
2022-10-01 15:07:31 +02:00
type : BDFDB . LibraryComponents . FormComponents . FormText . Types . DESCRIPTION ,
2020-11-23 20:56:13 +01:00
children : string
} ) )
} ) ) ;
return settingsItems ;
}
} ) ;
2020-09-19 20:49:33 +02:00
}
2018-10-11 10:21:26 +02:00
2021-01-06 12:38:36 +01:00
onSettingsClosed ( ) {
2020-09-19 20:49:33 +02:00
if ( this . SettingsUpdated ) {
delete this . SettingsUpdated ;
2021-05-03 19:59:24 +02:00
BDFDB . PatchUtils . forceAllUpdates ( this ) ;
2020-09-19 20:49:33 +02:00
}
}
2019-12-10 13:59:44 +01:00
2022-12-01 15:51:28 +01:00
onTextAreaContextMenu ( e ) {
2020-09-19 20:49:33 +02:00
let text = document . getSelection ( ) . toString ( ) . trim ( ) ;
2021-05-03 19:59:24 +02:00
if ( text && this . settings . general . addContextMenu ) this . injectItem ( e , text ) ;
2020-09-19 20:49:33 +02:00
}
2020-01-15 10:45:35 +01:00
2020-09-19 20:49:33 +02:00
onMessageContextMenu ( e ) {
let text = document . getSelection ( ) . toString ( ) . trim ( ) ;
2021-05-03 19:59:24 +02:00
if ( text && this . settings . general . addContextMenu ) this . injectItem ( e , text ) ;
2020-09-19 20:49:33 +02:00
}
injectItem ( e , text ) {
let [ children , index ] = BDFDB . ContextMenuUtils . findItem ( e . returnvalue , { id : "devmode-copy-id" , group : true } ) ;
children . splice ( index > - 1 ? index : children . length , 0 , BDFDB . ContextMenuUtils . createItem ( BDFDB . LibraryComponents . MenuItems . MenuGroup , {
children : BDFDB . ContextMenuUtils . createItem ( BDFDB . LibraryComponents . MenuItems . MenuItem , {
2020-12-22 20:55:39 +01:00
label : BDFDB . LanguageUtils . LibraryStringsFormat ( "add_to" , "ChatAliases" ) ,
2020-09-19 20:49:33 +02:00
id : BDFDB . ContextMenuUtils . createItemId ( this . name , "add-alias" ) ,
2022-07-27 13:32:37 +02:00
action : _ => this . openAddModal ( text . replace ( /\n/g , "\\n" ) . replace ( /\r/g , "\\r" ) . replace ( /\t/g , "\\t" ) )
2020-09-19 20:49:33 +02:00
} )
} ) ) ;
}
2020-06-08 20:07:08 +02:00
2022-12-01 15:51:28 +01:00
processChannelTextAreaContainer ( e ) {
if ( ! this . shouldInject ( e . instance . props . type ) ) return ;
BDFDB . PatchUtils . patch ( this , e . instance . props , "onSubmit" , { before : e2 => {
if ( BDFDB . LibraryStores . SlowmodeStore . getSlowmodeCooldownGuess ( e . instance . props . channel . id ) > 0 ) return ;
2022-07-27 13:32:37 +02:00
let messageData = this . formatText ( e2 . methodArguments [ 0 ] . value ) ;
if ( messageData ) {
if ( messageData . text != null && e2 . methodArguments [ 0 ] . value != messageData . text ) {
e2 . methodArguments [ 0 ] . value = messageData . text ;
e . instance . props . textValue = "" ;
if ( e . instance . props . richValue ) e . instance . props . richValue = BDFDB . SlateUtils . toRichValue ( "" ) ;
if ( e . instance . state ) {
e . instance . state . textValue = "" ;
if ( e . instance . state . richValue ) e . instance . state . richValue = BDFDB . SlateUtils . toRichValue ( "" ) ;
}
BDFDB . ReactUtils . forceUpdate ( e . instance ) ;
}
}
2022-10-25 17:22:33 +02:00
} } , { noCache : true } ) ;
2019-12-21 11:35:33 +01:00
}
2020-09-19 20:49:33 +02:00
2022-12-01 15:51:28 +01:00
shouldInject ( type ) {
return this . settings . places . normal && ( type == BDFDB . DiscordConstants . ChannelTextAreaTypes . NORMAL || type == BDFDB . DiscordConstants . ChannelTextAreaTypes . NORMAL _WITH _ACTIVITY || type == BDFDB . DiscordConstants . ChannelTextAreaTypes . SIDEBAR ) || this . settings . places . edit && type == BDFDB . DiscordConstants . ChannelTextAreaTypes . EDIT ;
2020-09-19 20:49:33 +02:00
}
formatText ( text ) {
text = text . replace ( /([\n\t\r])/g , " $1 " ) ;
2022-08-20 20:49:32 +02:00
let newText = [ ] , wordAliases = { } , multiAliases = { } ;
2020-09-19 20:49:33 +02:00
for ( let word in aliases ) {
if ( ! aliases [ word ] . regex && word . indexOf ( " " ) == - 1 ) wordAliases [ word ] = aliases [ word ] ;
else multiAliases [ word ] = aliases [ word ] ;
}
2023-01-28 13:19:36 +01:00
for ( let word of text . trim ( ) . split ( " " ) ) newText . push ( this . useAliases ( word , wordAliases , true ) ) ;
2020-09-19 20:49:33 +02:00
newText = newText . length == 1 ? newText [ 0 ] : newText . join ( " " ) ;
newText = newText . replace ( / ([\n\t\r]) /g , "$1" ) ;
2022-08-20 20:49:32 +02:00
newText = this . useAliases ( newText , multiAliases , false ) ;
return { text : newText } ;
2018-10-11 10:21:26 +02:00
}
2019-09-04 12:34:02 +02:00
2022-08-20 20:49:32 +02:00
useAliases ( string , aliases , singleWord ) {
2023-01-29 12:41:55 +01:00
for ( let word of Object . keys ( aliases ) . filter ( n => n ) . sort ( ( x , y ) => x . length > y . length ? - 1 : x . length < y . length ? 1 : 0 ) ) {
2020-09-19 20:49:33 +02:00
let result = true , replaced = false , tempString1 = string , tempString2 = "" ;
let config = aliases [ word ] ;
let escpAlias = config . regex ? word : BDFDB . StringUtils . regEscape ( word ) ;
let regString = config . exact ? "^" + escpAlias + "$" : escpAlias ;
while ( result != null ) {
result = new RegExp ( regString , ` ${ config . case ? "" : "i" } ${ config . exact ? "" : "g" } ` ) . exec ( tempString1 ) ;
if ( result ) {
replaced = true ;
2022-08-20 20:49:32 +02:00
let replacement = BDFDB . StringUtils . insertNRST ( config . replace ) ;
2021-05-12 10:40:52 +02:00
if ( result . length > 1 ) for ( let i = 1 ; i < result . length ; i ++ ) replacement = replacement . replace ( new RegExp ( "\\\\" + i + "|\\$" + i , "g" ) , result [ i ] ) ;
tempString2 += tempString1 . slice ( 0 , result . index + result [ 0 ] . length ) . replace ( result [ 0 ] , ! config . regex && ! config . case ? BDFDB . StringUtils . equalCase ( result [ 0 ] , replacement ) : replacement ) ;
2020-09-19 20:49:33 +02:00
tempString1 = tempString1 . slice ( result . index + result [ 0 ] . length ) ;
if ( config . regex && regString . indexOf ( "^" ) == 0 ) result = null ;
2020-02-11 12:58:16 +01:00
}
2020-09-19 20:49:33 +02:00
if ( ! result ) tempString2 += tempString1 ;
2019-12-20 14:01:16 +01:00
}
2020-09-19 20:49:33 +02:00
if ( replaced ) {
string = tempString2 ;
if ( singleWord ) break ;
}
}
return string ;
}
2021-01-26 21:40:18 +01:00
openAddModal ( wordValue ) {
2021-01-29 20:57:25 +01:00
let values = { wordValue , replaceValue : "" } ;
2022-08-20 20:49:32 +02:00
let configs = BDFDB . ObjectUtils . map ( this . defaults . configs , n => n . value ) ;
2021-01-29 20:57:25 +01:00
2020-09-19 20:49:33 +02:00
BDFDB . ModalUtils . open ( this , {
size : "MEDIUM" ,
2020-12-22 20:55:39 +01:00
header : BDFDB . LanguageUtils . LibraryStringsFormat ( "add_to" , "ChatAliases" ) ,
2021-01-23 18:50:24 +01:00
subHeader : "" ,
2020-09-19 20:49:33 +02:00
children : [
this . createInputs ( values ) ,
2021-01-29 20:57:25 +01:00
Object . keys ( configs ) . map ( key => BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . SettingsItem , {
2020-09-19 20:49:33 +02:00
type : "Switch" ,
label : this . defaults . configs [ key ] . description ,
2021-01-29 20:57:25 +01:00
value : configs [ key ] ,
onChange : value => { configs [ key ] = value ; }
2020-09-19 20:49:33 +02:00
} ) )
] . flat ( 10 ) . filter ( n => n ) ,
buttons : [ {
2021-01-29 20:57:25 +01:00
disabled : ! Object . keys ( values ) . every ( valueName => values [ valueName ] ) ,
2020-09-19 20:49:33 +02:00
contents : BDFDB . LanguageUtils . LanguageStrings . ADD ,
color : "BRAND" ,
close : true ,
2021-01-29 20:57:25 +01:00
ref : instance => { if ( instance ) values . addButton = instance ; } ,
onClick : _ => {
this . saveWord ( values , configs ) ;
2021-05-03 19:59:24 +02:00
BDFDB . PatchUtils . forceAllUpdates ( this ) ;
2020-02-11 12:58:16 +01:00
}
2020-09-19 20:49:33 +02:00
} ]
} ) ;
}
createInputs ( values ) {
return [
BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . FormComponents . FormItem , {
2020-11-19 16:45:36 +01:00
title : "Replace:" ,
2021-01-13 18:54:21 +01:00
className : BDFDB . disCN . marginbottom8 ,
2020-09-19 20:49:33 +02:00
children : BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . TextInput , {
2021-01-26 21:40:18 +01:00
value : values . wordValue ,
placeholder : values . wordValue ,
errorMessage : ! values . wordValue && "Choose a Word Value" || aliases [ values . wordValue ] && "Word Value already used, saving will overwrite old Alias" ,
2020-09-19 20:49:33 +02:00
onChange : ( value , instance ) => {
2021-01-26 21:40:18 +01:00
values . wordValue = value . trim ( ) ;
if ( ! values . wordValue ) instance . props . errorMessage = "Choose a Word Value" ;
else if ( aliases [ values . wordValue ] ) instance . props . errorMessage = "Word Value already used, saving will overwrite old Alias" ;
2020-09-19 20:49:33 +02:00
else delete instance . props . errorMessage ;
2021-01-29 20:57:25 +01:00
values . addButton . props . disabled = ! Object . keys ( values ) . every ( valueName => values [ valueName ] ) ;
BDFDB . ReactUtils . forceUpdate ( values . addButton ) ;
2020-07-02 10:42:56 +02:00
}
2020-09-19 20:49:33 +02:00
} )
} ) ,
BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . FormComponents . FormItem , {
2020-11-19 16:45:36 +01:00
title : "With:" ,
2021-01-13 18:54:21 +01:00
className : BDFDB . disCN . marginbottom8 ,
2020-09-19 20:49:33 +02:00
children : BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . TextInput , {
2021-01-26 21:40:18 +01:00
value : values . replaceValue ,
placeholder : values . replaceValue ,
2020-09-19 20:49:33 +02:00
autoFocus : true ,
2021-01-26 21:40:18 +01:00
errorMessage : ! values . replaceValue && "Choose a Replacement Value" ,
2020-09-19 20:49:33 +02:00
onChange : ( value , instance ) => {
2021-01-26 21:40:18 +01:00
values . replaceValue = value . trim ( ) ;
if ( ! values . replaceValue ) instance . props . errorMessage = "Choose a Replacement Value" ;
2020-09-19 20:49:33 +02:00
else delete instance . props . errorMessage ;
2021-01-29 20:57:25 +01:00
values . addButton . props . disabled = ! Object . keys ( values ) . every ( valueName => values [ valueName ] ) ;
BDFDB . ReactUtils . forceUpdate ( values . addButton ) ;
2020-09-19 20:49:33 +02:00
}
} )
2020-07-02 10:42:56 +02:00
} )
2020-09-19 20:49:33 +02:00
] ;
2020-02-11 12:58:16 +01:00
}
2020-07-26 16:39:51 +02:00
2021-05-03 19:59:24 +02:00
saveWord ( values , aliasConfigs = this . settings . configs ) {
2022-08-20 20:49:32 +02:00
if ( ! values . wordValue || ! values . replaceValue ) return ;
2021-01-29 20:57:25 +01:00
aliases [ values . wordValue ] = {
replace : values . replaceValue ,
2020-09-19 20:49:33 +02:00
case : aliasConfigs . case ,
2021-01-29 20:57:25 +01:00
exact : values . wordValue . indexOf ( " " ) > - 1 ? false : aliasConfigs . exact ,
2022-08-20 20:49:32 +02:00
regex : aliasConfigs . regex
2020-09-19 20:49:33 +02:00
} ;
BDFDB . DataUtils . save ( aliases , this , "words" ) ;
}
} ;
2022-09-01 14:40:11 +02:00
} ) ( window . BDFDB _Global . PluginUtils . buildPlugin ( changeLog ) ) ;
2021-04-13 07:16:28 +02:00
} ) ( ) ;