2019-09-20 22:32:52 +02:00
//META{"name":"MessageUtilities","website":"https://github.com/mwittrien/BetterDiscordAddons/tree/master/Plugins/MessageUtilities","source":"https://raw.githubusercontent.com/mwittrien/BetterDiscordAddons/master/Plugins/MessageUtilities/MessageUtilities.plugin.js"}*//
2018-10-11 10:21:26 +02:00
class MessageUtilities {
2019-01-17 23:48:29 +01:00
getName ( ) { return "MessageUtilities" ; }
2019-11-17 12:12:09 +01:00
getVersion ( ) { return "1.6.1" ; }
2019-01-17 23:48:29 +01:00
getAuthor ( ) { return "DevilBro" ; }
getDescription ( ) { return "Offers a number of useful message options. Remap the keybindings in the settings." ; }
2019-01-26 22:45:19 +01:00
2019-09-04 12:34:02 +02:00
constructor ( ) {
2019-11-15 21:48:30 +01:00
this . changelog = {
2019-11-18 20:09:03 +01:00
"fixed" : [ [ "Editing" , "No longer triggers while clicking the edit textarea for messages" ] ] ,
2019-11-15 21:48:30 +01:00
"improved" : [ [ "New Library Structure & React" , "Restructured my Library and switched to React rendering instead of DOM manipulation" ] ]
} ;
2019-11-14 17:56:26 +01:00
this . patchedModules = {
after : {
2019-11-15 21:48:30 +01:00
MessageContextMenu : [ "componentDidMount" , "componentDidUpdate" ]
2019-11-14 17:56:26 +01:00
}
2019-02-28 17:07:39 +01:00
} ;
2019-09-04 12:34:02 +02:00
}
initConstructor ( ) {
2018-10-11 10:21:26 +02:00
this . bindings = { } ;
2019-01-26 22:45:19 +01:00
2018-10-11 10:21:26 +02:00
this . firedEvents = [ ] ;
2019-01-26 22:45:19 +01:00
2019-11-15 21:48:30 +01:00
this . clickMap = [ "CLICK" , "DBLCLICK" ] ;
2019-01-26 22:45:19 +01:00
2018-10-11 10:21:26 +02:00
this . defaults = {
settings : {
2019-09-28 10:27:59 +02:00
"addHints" : { value : true , description : "Add keycombo hints to contextmenus:" } ,
2019-09-26 20:09:46 +02:00
"clearOnEscape" : { value : true , description : "Clear chat input when Escape is pressed:" }
2018-10-11 10:21:26 +02:00
} ,
2019-09-26 20:09:46 +02:00
toasts : { } ,
2018-10-11 10:21:26 +02:00
bindings : {
2019-11-15 21:48:30 +01:00
"Edit_Message" : { name : "Edit Message" , func : this . doEdit , value : { click : 1 , keycombo : [ ] } } ,
"Delete_Message" : { name : "Delete Message" , func : this . doDelete , value : { click : 0 , keycombo : [ 46 ] } } ,
"Pin/Unpin_Message" : { name : "Pin/Unpin Message" , func : this . doPinUnPin , value : { click : 0 , keycombo : [ 17 ] } } ,
"React_to_Message" : { name : "React to Message" , func : this . doOpenReact , value : { click : 0 , keycombo : [ 17 , 83 ] } } ,
"Copy_Raw" : { name : "Copy raw Message" , func : this . doCopyRaw , value : { click : 0 , keycombo : [ 17 , 68 ] } } ,
"Copy_Link" : { name : "Copy Message Link" , func : this . doCopyLink , value : { click : 0 , keycombo : [ 17 , 81 ] } } ,
"__Note_Message" : { name : "Note Message (Pesonal Pins)" , func : this . doNote , value : { click : 0 , keycombo : [ 16 ] } , plugin : "PersonalPins" } ,
"__Translate_Message" : { name : "Translate Message (Google Translate Option)" , func : this . doTranslate , value : { click : 0 , keycombo : [ 20 ] } , plugin : "GoogleTranslateOption" } ,
"__Quote_Message" : { name : "Quote Message (Quoter)" , func : this . doQuote , value : { click : 0 , keycombo : [ 17 , 87 ] } , plugin : "Quoter" } ,
"__Citate_Message" : { name : "Quote Message (Citador)" , func : this . doCitate , value : { click : 0 , keycombo : [ 17 , 78 ] } , plugin : "Citador" } ,
"__Reveal_Spoilers" : { name : "Reveal All Spoilers (RevealAllSpoilersOption)" , func : this . doReveal , value : { click : 0 , keycombo : [ 17 , 74 ] } , plugin : "RevealAllSpoilersOption" }
2018-10-11 10:21:26 +02:00
}
} ;
2019-09-26 20:09:46 +02:00
for ( let type in this . defaults . bindings ) {
let nativeaction = type . indexOf ( "__" ) != 0 ;
this . defaults . settings [ type ] = { value : nativeaction } ;
if ( nativeaction ) this . defaults . toasts [ type ] = { value : type != "Edit_Message" && type != "React_to_Message" } ;
}
2018-10-11 10:21:26 +02:00
}
2019-01-26 22:45:19 +01:00
2018-10-11 10:21:26 +02:00
getSettingsPanel ( ) {
2019-11-15 21:48:30 +01:00
if ( ! global . BDFDB || typeof BDFDB != "object" || ! BDFDB . loaded || ! this . started ) return ;
let settings = BDFDB . DataUtils . get ( this , "settings" ) ;
let bindings = BDFDB . DataUtils . get ( this , "bindings" ) ;
let toasts = BDFDB . DataUtils . get ( this , "toasts" ) ;
let settingsitems = [ ] ;
for ( let key in settings ) if ( this . defaults . settings [ key ] . description ) settingsitems . push ( BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . SettingsSaveItem , {
className : BDFDB . disCN . marginbottom8 ,
type : "Switch" ,
plugin : this ,
keys : [ "settings" , key ] ,
label : this . defaults . settings [ key ] . description ,
value : settings [ key ]
} ) ) ;
settingsitems . push ( BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . FormComponents . FormDivider , {
className : BDFDB . disCN . marginbottom8
} ) ) ;
for ( let action in bindings ) if ( ! this . defaults . bindings [ action ] . plugin || BDFDB . BDUtils . isPluginEnabled ( this . defaults . bindings [ action ] . plugin ) ) {
settingsitems . push ( BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . Flex , {
className : BDFDB . disCN . marginbottom8 ,
align : BDFDB . LibraryComponents . Flex . Align . CENTER ,
direction : BDFDB . LibraryComponents . Flex . Direction . HORIZONTAL ,
children : [
BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . SettingsLabel , {
label : this . defaults . bindings [ action ] . name
} ) ,
toasts [ action ] != undefined ? BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . SettingsSaveItem , {
type : "Switch" ,
mini : true ,
plugin : this ,
keys : [ "toasts" , action ] ,
grow : 0 ,
label : "Toast:" ,
value : toasts [ action ]
} ) : null
] . filter ( n => n )
} ) ) ;
settingsitems . push ( BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . SettingsSaveItem , {
className : BDFDB . disCN . marginbottom8 ,
type : "Switch" ,
dividerbottom : true ,
mini : true ,
plugin : this ,
keys : [ "settings" , action ] ,
value : settings [ action ] ,
labelchildren : BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . Flex , {
direction : BDFDB . LibraryComponents . Flex . Direction . HORIZONTAL ,
children : [
BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . Select , {
value : bindings [ action ] . click ,
options : this . clickMap . map ( ( label , i ) => { return { value : i , label : label } } ) ,
onChange : choice => {
bindings [ action ] . click = choice . value ;
BDFDB . DataUtils . save ( bindings , this , "bindings" ) ;
}
} ) ,
BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . KeybindRecorder , {
defaultValue : bindings [ action ] . keycombo . filter ( n => n ) ,
reset : true ,
onChange : keycombo => {
bindings [ action ] . keycombo = keycombo ;
BDFDB . DataUtils . save ( bindings , this , "bindings" ) ;
}
} )
]
} )
} ) ) ;
}
settingsitems . push ( BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . SettingsItem , {
type : "Button" ,
className : BDFDB . disCN . marginbottom8 ,
color : BDFDB . LibraryComponents . Button . Colors . RED ,
label : "Reset all Key Bindings" ,
onClick : ( e , instance ) => {
BDFDB . ModalUtils . confirm ( this , "Are you sure you want to reset all Key Bindings?" , _ => {
BDFDB . DataUtils . remove ( this , "bindings" ) ;
BDFDB . ReactUtils . forceUpdate ( BDFDB . ReactUtils . findOwner ( instance , { key : ` ${ this . name } -settingspanel ` , up : true } ) ) ;
} ) ;
} ,
children : BDFDB . LanguageUtils . LanguageStrings . RESET
} ) ) ;
return BDFDB . PluginUtils . createSettingsPanel ( this , settingsitems ) ;
}
2018-10-11 10:21:26 +02:00
//legacy
load ( ) { }
start ( ) {
2019-02-04 09:13:15 +01:00
if ( ! global . BDFDB ) global . BDFDB = { myPlugins : { } } ;
if ( global . BDFDB && global . BDFDB . myPlugins && typeof global . BDFDB . myPlugins == "object" ) global . BDFDB . myPlugins [ this . getName ( ) ] = this ;
2019-05-26 13:55:26 +02:00
var libraryScript = document . querySelector ( 'head script#BDFDBLibraryScript' ) ;
if ( ! libraryScript || ( performance . now ( ) - libraryScript . getAttribute ( "date" ) ) > 600000 ) {
2018-10-11 10:21:26 +02:00
if ( libraryScript ) libraryScript . remove ( ) ;
libraryScript = document . createElement ( "script" ) ;
2019-05-26 13:55:26 +02:00
libraryScript . setAttribute ( "id" , "BDFDBLibraryScript" ) ;
2018-10-11 10:21:26 +02:00
libraryScript . setAttribute ( "type" , "text/javascript" ) ;
2019-10-18 10:56:41 +02:00
libraryScript . setAttribute ( "src" , "https://mwittrien.github.io/BetterDiscordAddons/Plugins/BDFDB.min.js" ) ;
2019-01-17 23:48:29 +01:00
libraryScript . setAttribute ( "date" , performance . now ( ) ) ;
2019-05-26 13:55:26 +02:00
libraryScript . addEventListener ( "load" , ( ) => { this . initialize ( ) ; } ) ;
2018-10-11 10:21:26 +02:00
document . head . appendChild ( libraryScript ) ;
}
2019-01-17 23:48:29 +01: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 ) ;
2019-11-15 21:48:30 +01:00
// REMOVE 15.11.2019
let bindings = BDFDB . DataUtils . load ( this , "bindings" ) ;
if ( bindings [ Object . keys ( bindings ) [ 0 ] ] . keycombo == undefined ) {
for ( let key in bindings ) bindings [ key ] = { click : bindings [ key ] . click , keycombo : [ bindings [ key ] . key1 , bindings [ key ] . key2 ] . filter ( n => n ) } ;
BDFDB . DataUtils . save ( bindings , this , "bindings" ) ;
}
2019-01-26 22:45:19 +01:00
2019-10-22 18:55:25 +02:00
BDFDB . ListenerUtils . add ( this , document , "click" , BDFDB . dotCN . messagegroup + "> [aria-disabled]," + BDFDB . dotCN . messagegroup + "> * > [aria-disabled]," + BDFDB . dotCN . messagesystem , e => {
2019-09-28 10:27:59 +02:00
this . onClick ( e , 0 , "onSglClick" ) ;
2019-01-17 23:48:29 +01:00
} )
2019-10-22 18:55:25 +02:00
BDFDB . ListenerUtils . add ( this , document , "dblclick" , BDFDB . dotCN . messagegroup + "> [aria-disabled]," + BDFDB . dotCN . messagegroup + "> * > [aria-disabled]," + BDFDB . dotCN . messagesystem , e => {
2019-09-28 10:27:59 +02:00
this . onClick ( e , 1 , "onDblClick" ) ;
2019-01-17 23:48:29 +01:00
} ) ;
2019-10-22 18:55:25 +02:00
BDFDB . ListenerUtils . add ( this , document , "keydown" , BDFDB . dotCN . textareawrapchat , e => {
2019-09-28 10:27:59 +02:00
this . onKeyDown ( e , e . which , "onKeyDown" ) ;
2019-01-17 23:48:29 +01:00
} ) ;
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-22 18:55:25 +02:00
BDFDB . PluginUtils . clear ( this ) ;
2018-10-11 10:21:26 +02:00
}
}
2019-01-26 22:45:19 +01:00
2018-10-11 10:21:26 +02:00
//begin of own functions
2019-09-28 10:27:59 +02:00
2019-11-15 21:48:30 +01:00
processMessageContextMenu ( e ) {
if ( e . instance . props . message && e . instance . props . channel && e . instance . props . target ) {
for ( let itemlabel of e . node . querySelectorAll ( BDFDB . dotCN . contextmenulabel ) ) {
2019-09-30 09:16:14 +02:00
let hint = itemlabel . parentElement . querySelector ( BDFDB . dotCN . contextmenuhint ) ;
2019-11-15 21:48:30 +01:00
if ( hint ) {
2019-11-18 20:09:03 +01:00
let hintlabel ;
if ( itemlabel . innerText == BDFDB . LanguageUtils . LanguageStrings . MARK _UNREAD ) {
if ( BDFDB . DataUtils . get ( this , "settings" , "addHints" ) ) hintlabel = ` ${ this . clickMap [ 0 ] } + ${ BDFDB . LibraryModules . KeyCodeUtils . getString ( 18 ) } ` ;
2019-09-30 09:16:14 +02:00
}
2019-11-18 20:09:03 +01:00
else {
let action = null ;
switch ( itemlabel . innerText ) {
case BDFDB . LanguageUtils . LanguageStrings . COPY _MESSAGE _LINK :
action = "Copy_Link" ;
break ;
case BDFDB . LanguageUtils . LanguageStrings . EDIT _MESSAGE :
action = "Edit_Message" ;
break ;
case BDFDB . LanguageUtils . LanguageStrings . PIN _MESSAGE :
case BDFDB . LanguageUtils . LanguageStrings . UNPIN _MESSAGE :
action = "Pin/Unpin_Message" ;
break ;
case BDFDB . LanguageUtils . LanguageStrings . DELETE _MESSAGE :
action = "Delete_Message" ;
break ;
2019-09-30 09:16:14 +02:00
}
2019-11-18 20:09:03 +01:00
if ( action ) hintlabel = this . getActiveShortcutString ( action ) ;
}
if ( hintlabel ) {
hint . style . setProperty ( "width" , "42px" ) ;
hint . style . setProperty ( "max-width" , "42px" ) ;
hint . style . setProperty ( "margin-left" , "8px" ) ;
BDFDB . ReactUtils . render ( BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . TextScroller , { speed : 2 , children : hintlabel } ) , hint ) ;
2019-09-28 10:27:59 +02:00
}
}
}
}
}
2019-01-26 22:45:19 +01:00
2019-09-28 10:27:59 +02:00
onClick ( e , click , name ) {
2018-10-11 10:21:26 +02:00
if ( ! this . isEventFired ( name ) ) {
this . fireEvent ( name ) ;
2019-10-22 19:49:57 +02:00
let settings = BDFDB . DataUtils . get ( this , "settings" ) ;
let bindings = BDFDB . ObjectUtils . filter ( BDFDB . DataUtils . get ( this , "bindings" ) , action => { return settings [ action ] } , true ) ;
2019-09-27 08:36:43 +02:00
let priorityaction = null ;
for ( let action in bindings ) {
2019-09-26 16:18:55 +02:00
let binding = bindings [ action ] ;
2019-09-27 08:36:43 +02:00
let prioritybinding = bindings [ priorityaction ] ;
2019-11-15 21:48:30 +01:00
if ( this . checkIfBindingIsValid ( binding , click ) && ( ! bindings [ priorityaction ] || binding . click > prioritybinding . click || binding . keycombo . length > prioritybinding . keycombo . length ) ) priorityaction = action ;
2019-09-26 16:18:55 +02:00
}
if ( priorityaction ) {
let { messagediv , pos , message } = this . getMessageData ( e . currentTarget ) ;
if ( messagediv && pos > - 1 && message ) {
2019-10-22 18:55:25 +02:00
BDFDB . ListenerUtils . stopEvent ( e ) ;
2019-11-01 11:09:32 +01:00
BDFDB . TimeUtils . clear ( this . clickTimeout ) ;
2019-09-28 10:27:59 +02:00
if ( ! this . hasDoubleClickOverwrite ( bindings , bindings [ priorityaction ] ) ) {
2019-11-15 21:48:30 +01:00
this . defaults . bindings [ priorityaction ] . func . apply ( this , [ { messagediv , pos , message } , priorityaction ] ) ;
2019-09-28 10:27:59 +02:00
}
2019-11-01 11:09:32 +01:00
else this . clickTimeout = BDFDB . TimeUtils . timeout ( ( ) => {
2019-11-15 21:48:30 +01:00
this . defaults . bindings [ priorityaction ] . func . apply ( this , [ { messagediv , pos , message } , priorityaction ] ) ;
2019-09-28 10:27:59 +02:00
} , 500 ) ;
2018-10-11 10:21:26 +02:00
}
}
this . cancelEvent ( name ) ;
}
}
2019-01-26 22:45:19 +01:00
2019-09-28 10:27:59 +02:00
checkIfBindingIsValid ( binding , doneclick ) {
2019-01-03 00:35:05 +01:00
let valid = true ;
2019-09-28 10:27:59 +02:00
if ( binding . click != doneclick ) valid = false ;
2019-11-15 21:48:30 +01:00
for ( let key of binding . keycombo ) if ( ! BDFDB . InternalData . pressedKeys . includes ( key ) ) valid = false ;
2018-10-11 10:21:26 +02:00
return valid ;
}
2019-01-26 22:45:19 +01:00
2019-09-28 10:27:59 +02:00
hasDoubleClickOverwrite ( bindings , binding ) {
if ( binding . click == 1 ) return false ;
2019-10-22 18:55:25 +02:00
let dblbindings = BDFDB . ObjectUtils . filter ( bindings , bndg => { return bndg . click == 1 } ) ;
2019-09-28 10:27:59 +02:00
for ( let dblaction in dblbindings ) {
let dblbndg = dblbindings [ dblaction ] ;
let overwrite = true ;
2019-11-15 21:48:30 +01:00
if ( BDFDB . equals ( binding . keycombo , dblbndg . keycombo ) ) return true ;
2019-09-28 10:27:59 +02:00
}
return false ;
}
2019-09-26 20:09:46 +02:00
doDelete ( { messagediv , pos , message } , action ) {
2019-03-01 21:25:48 +01:00
let deletelink = messagediv . parentElement . querySelector ( BDFDB . dotCNS . messagelocalbotmessage + BDFDB . dotCN . anchor ) ;
if ( deletelink ) deletelink . click ( ) ;
else {
2019-09-11 12:14:43 +02:00
let channel = BDFDB . LibraryModules . ChannelStore . getChannel ( message . channel _id ) ;
2019-10-22 18:55:25 +02:00
if ( ( channel && BDFDB . UserUtils . can ( "MANAGE_MESSAGES" ) ) || message . author . id == BDFDB . UserUtils . me . id && message . type != 1 && message . type != 2 && message . type != 3 ) {
2019-09-11 12:14:43 +02:00
BDFDB . LibraryModules . MessageUtils . deleteMessage ( message . channel _id , message . id , message . state != "SENT" ) ;
2019-10-22 23:04:35 +02:00
if ( BDFDB . DataUtils . get ( this , "toasts" , action ) ) BDFDB . NotificationUtils . toast ( "Message has been deleted." , { type : "success" } ) ;
2019-03-01 21:25:48 +01:00
}
2018-10-11 10:21:26 +02:00
}
}
2019-01-26 22:45:19 +01:00
2019-09-26 20:09:46 +02:00
doEdit ( { messagediv , pos , message } , action ) {
2019-10-22 18:55:25 +02:00
if ( message . author . id == BDFDB . UserUtils . me . id && ! messagediv . querySelector ( "textarea" ) ) {
2019-09-11 12:14:43 +02:00
BDFDB . LibraryModules . MessageUtils . startEditMessage ( message . channel _id , message . id , message . content ) ;
2019-10-22 23:04:35 +02:00
if ( BDFDB . DataUtils . get ( this , "toasts" , action ) ) BDFDB . NotificationUtils . toast ( "Started editing." , { type : "success" } ) ;
2018-10-11 10:21:26 +02:00
}
}
2019-01-26 22:45:19 +01:00
2019-09-26 20:09:46 +02:00
doOpenReact ( { messagediv , pos , message } , action ) {
2019-01-03 00:35:05 +01:00
let reactButton = messagediv . querySelector ( BDFDB . dotCN . emojipickerbutton ) ;
2019-09-26 20:09:46 +02:00
if ( reactButton ) {
reactButton . click ( ) ;
2019-10-22 23:04:35 +02:00
if ( BDFDB . DataUtils . get ( this , "toasts" , action ) ) BDFDB . NotificationUtils . toast ( "Reaction popout has been opened." , { type : "success" } ) ;
2019-09-26 20:09:46 +02:00
}
2018-10-11 10:21:26 +02:00
}
2019-01-26 22:45:19 +01:00
2019-09-26 20:09:46 +02:00
doPinUnPin ( { messagediv , pos , message } , action ) {
2019-03-01 21:25:48 +01:00
if ( message . state == "SENT" ) {
2019-09-11 12:14:43 +02:00
let channel = BDFDB . LibraryModules . ChannelStore . getChannel ( message . channel _id ) ;
2019-10-22 18:55:25 +02:00
if ( channel && ( channel . type == 1 || channel . type == 3 || BDFDB . UserUtils . can ( "MANAGE_MESSAGES" ) ) && message . type == 0 ) {
2019-09-26 20:09:46 +02:00
if ( message . pinned ) {
BDFDB . LibraryModules . MessagePinUtils . unpinMessage ( channel , message . id ) ;
2019-10-22 23:04:35 +02:00
if ( BDFDB . DataUtils . get ( this , "toasts" , action ) ) BDFDB . NotificationUtils . toast ( "Message has been unpinned." , { type : "error" } ) ;
2019-09-26 20:09:46 +02:00
}
else {
BDFDB . LibraryModules . MessagePinUtils . pinMessage ( channel , message . id ) ;
2019-10-22 23:04:35 +02:00
if ( BDFDB . DataUtils . get ( this , "toasts" , action ) ) BDFDB . NotificationUtils . toast ( "Message has been pinned." , { type : "success" } ) ;
2019-09-26 20:09:46 +02:00
}
2019-03-01 21:25:48 +01:00
}
2018-10-11 10:21:26 +02:00
}
}
2019-01-26 22:45:19 +01:00
2019-09-26 20:09:46 +02:00
doCopyRaw ( { messagediv , pos , message } , action ) {
if ( message . content ) {
BDFDB . LibraryRequires . electron . clipboard . write ( { text : message . content } ) ;
2019-10-22 23:04:35 +02:00
if ( BDFDB . DataUtils . get ( this , "toasts" , action ) ) BDFDB . NotificationUtils . toast ( "Raw message content has been copied." , { type : "success" } ) ;
2019-09-26 20:09:46 +02:00
}
2019-09-26 16:18:55 +02:00
}
2019-09-26 20:09:46 +02:00
doCopyLink ( { messagediv , pos , message } , action ) {
2019-09-26 16:18:55 +02:00
let channel = BDFDB . LibraryModules . ChannelStore . getChannel ( message . channel _id ) ;
2019-09-26 20:09:46 +02:00
if ( channel ) {
BDFDB . LibraryRequires . electron . clipboard . write ( { text : ` https://discordapp.com/channels/ ${ channel . guild _id } / ${ channel . id } / ${ message . id } ` } ) ;
2019-10-22 23:04:35 +02:00
if ( BDFDB . DataUtils . get ( this , "toasts" , action ) ) BDFDB . NotificationUtils . toast ( "Messagelink has been copied." , { type : "success" } ) ;
2019-09-26 20:09:46 +02:00
}
2019-09-26 16:18:55 +02:00
}
2019-09-26 20:09:46 +02:00
doNote ( { messagediv , pos , message } , action ) {
2019-10-23 11:10:01 +02:00
if ( BDFDB . BDUtils . isPluginEnabled ( this . defaults . bindings . _ _Note _Message . plugin ) ) {
2019-09-11 12:14:43 +02:00
let channel = BDFDB . LibraryModules . ChannelStore . getChannel ( message . channel _id ) ;
2019-10-23 11:10:01 +02:00
if ( channel ) BDFDB . BDUtils . getPlugin ( this . defaults . bindings . _ _Note _Message . plugin ) . addMessageToNotes ( message , messagediv , channel ) ;
2018-10-11 10:21:26 +02:00
}
}
2019-01-26 22:45:19 +01:00
2019-09-26 20:09:46 +02:00
doTranslate ( { messagediv , pos , message } , action ) {
2019-10-23 11:10:01 +02:00
if ( BDFDB . BDUtils . isPluginEnabled ( this . defaults . bindings . _ _Translate _Message . plugin ) ) {
2019-09-11 12:14:43 +02:00
let channel = BDFDB . LibraryModules . ChannelStore . getChannel ( message . channel _id ) ;
2019-10-23 11:10:01 +02:00
if ( channel ) BDFDB . BDUtils . getPlugin ( this . defaults . bindings . _ _Translate _Message . plugin ) . translateMessage ( message , messagediv , channel ) ;
2018-10-11 10:21:26 +02:00
}
}
2019-01-26 22:45:19 +01:00
2019-09-26 20:09:46 +02:00
doQuote ( { messagediv , pos , message } , action ) {
2019-10-23 11:10:01 +02:00
if ( BDFDB . BDUtils . isPluginEnabled ( this . defaults . bindings . _ _Quote _Message . plugin ) ) {
2019-01-03 00:35:05 +01:00
let quoteButton = messagediv . querySelector ( ".btn-quote" ) ;
2018-10-11 10:21:26 +02:00
if ( quoteButton ) quoteButton . click ( ) ;
}
}
2019-01-26 22:45:19 +01:00
2019-09-26 20:09:46 +02:00
doCitate ( { messagediv , pos , message } , action ) {
2019-10-23 11:10:01 +02:00
if ( BDFDB . BDUtils . isPluginEnabled ( this . defaults . bindings . _ _Citate _Message . plugin ) ) {
2019-01-03 00:35:05 +01:00
let citarButton = messagediv . parentElement . querySelector ( ".citar-btn" ) ;
2018-10-11 10:21:26 +02:00
if ( citarButton ) citarButton . click ( ) ;
}
}
2019-01-26 22:45:19 +01:00
2019-09-26 20:09:46 +02:00
doReveal ( { messagediv , pos , message } , action ) {
2019-10-23 11:10:01 +02:00
if ( BDFDB . BDUtils . isPluginEnabled ( this . defaults . bindings . _ _Reveal _Spoilers . plugin ) ) {
BDFDB . BDUtils . getPlugin ( this . defaults . bindings . _ _Reveal _Spoilers . plugin ) . revealAllSpoilers ( messagediv ) ;
2019-02-01 12:34:20 +01:00
}
}
2019-01-31 21:40:52 +01:00
onKeyDown ( e , key , name ) {
2018-10-11 10:21:26 +02:00
if ( ! this . isEventFired ( name ) ) {
this . fireEvent ( name ) ;
2019-10-22 20:16:05 +02:00
if ( key == 27 && BDFDB . DataUtils . get ( this , "settings" , "clearOnEscape" ) ) {
2019-10-23 11:10:01 +02:00
let instance = BDFDB . ReactUtils . findOwner ( BDFDB . DOMUtils . getParent ( BDFDB . dotCNS . chat + "form" , e . currentTarget ) , { name : "ChannelTextAreaForm" , up : true } ) ;
2019-02-25 08:45:46 +01:00
if ( instance ) instance . setState ( { textValue : "" } ) ;
2018-10-11 10:21:26 +02:00
}
this . cancelEvent ( name ) ;
}
}
2019-01-26 22:45:19 +01:00
2018-12-29 11:32:07 +01:00
getActiveShortcutString ( action ) {
2019-09-28 10:27:59 +02:00
if ( ! action ) return null ;
2019-10-22 19:49:57 +02:00
let str = "" , settings = BDFDB . DataUtils . get ( this , "settings" ) ;
2019-09-28 10:27:59 +02:00
if ( settings . addHints && settings [ action ] ) {
2019-10-22 20:16:05 +02:00
let binding = BDFDB . DataUtils . get ( this , "bindings" , action ) ;
2019-11-15 21:48:30 +01:00
if ( binding ) {
2019-11-17 12:12:09 +01:00
str += this . clickMap [ binding . click ] ;
2019-11-15 21:48:30 +01:00
if ( binding . keycombo . length ) str += " + " + BDFDB . LibraryModules . KeyCodeUtils . getString ( binding . keycombo ) ;
2018-12-29 11:32:07 +01:00
}
}
2019-11-15 21:48:30 +01:00
return str . replace ( / /g , "" ) ;
2018-12-29 11:32:07 +01:00
}
2019-01-26 22:45:19 +01:00
2019-01-03 00:35:05 +01:00
getMessageData ( target ) {
2019-10-23 11:10:01 +02:00
let messagediv = BDFDB . DOMUtils . getParent ( BDFDB . dotCN . messagegroup + "> [aria-disabled]" , target ) || BDFDB . DOMUtils . getParent ( BDFDB . dotCN . messagegroup + "> * > [aria-disabled]" , target ) || BDFDB . DOMUtils . getParent ( BDFDB . dotCN . messagesystem , target ) ;
2019-11-18 20:09:03 +01:00
if ( messagediv && messagediv . querySelector ( BDFDB . dotCN . textareaedit ) ) return { messagediv : null , pos : - 1 , message : null } ;
2019-09-11 12:14:43 +02:00
let pos = messagediv ? Array . from ( messagediv . parentElement . childNodes ) . filter ( n => n . nodeType != Node . TEXT _NODE ) . indexOf ( messagediv ) : - 1 ;
2019-10-22 18:55:25 +02:00
let instance = BDFDB . ReactUtils . getInstance ( messagediv ) ;
2019-10-22 19:49:57 +02:00
let message = instance ? BDFDB . ReactUtils . findValue ( instance , "message" , { up : true } ) : null ;
2019-01-03 00:35:05 +01:00
return { messagediv , pos , message } ;
2018-10-11 10:21:26 +02:00
}
2019-01-26 22:45:19 +01:00
2018-10-11 10:21:26 +02:00
fireEvent ( name ) {
this . firedEvents . push ( name ) ;
}
2019-01-26 22:45:19 +01:00
2018-10-11 10:21:26 +02:00
isEventFired ( name ) {
return this . firedEvents . includes ( name ) ;
}
2019-01-26 22:45:19 +01:00
2018-10-11 10:21:26 +02:00
cancelEvent ( name ) {
2019-11-15 21:48:30 +01:00
BDFDB . TimeUtils . timeout ( _ => { BDFDB . ArrayUtils . remove ( this . firedEvents , name ) } ) ;
2018-10-11 10:21:26 +02:00
}
}