2019-09-20 22:32:52 +02:00
//META{"name":"ImageGallery","website":"https://github.com/mwittrien/BetterDiscordAddons/tree/master/Plugins/ImageGallery","source":"https://raw.githubusercontent.com/mwittrien/BetterDiscordAddons/master/Plugins/ImageGallery/ImageGallery.plugin.js"}*//
2018-10-11 10:21:26 +02:00
2020-02-04 08:20:40 +01:00
var ImageGallery = ( _ => {
var eventFired ;
return class ImageGallery {
getName ( ) { return "ImageGallery" ; }
2019-01-11 21:57:59 +01:00
2020-02-06 23:04:41 +01:00
getVersion ( ) { return "1.6.4" ; }
2019-01-11 21:57:59 +01:00
2020-02-04 08:20:40 +01:00
getAuthor ( ) { return "DevilBro" ; }
2019-01-11 21:57:59 +01:00
2020-02-04 08:20:40 +01:00
getDescription ( ) { return "Allows the user to browse through images sent inside the same message." ; }
2019-01-26 22:45:19 +01:00
2020-02-04 08:20:40 +01:00
constructor ( ) {
this . changelog = {
2020-02-06 23:04:41 +01:00
"fixed" : [ [ "First Message" , "No longer includes iamges of a different user when the opened image is in the first message" ] , [ "Message Update" , "Fixed the plugin for the new Message Update" ] ] ,
2020-02-04 08:20:40 +01:00
"improved" : [ [ "New Library Structure & React" , "Restructured my Library and switched to React rendering instead of DOM manipulation" ] ]
} ;
2019-09-04 12:34:02 +02:00
2020-02-04 08:20:40 +01:00
this . patchedModules = {
after : {
ImageModal : [ "render" , "componentDidMount" ]
}
} ;
}
2019-09-04 12:34:02 +02:00
2020-02-04 08:20:40 +01:00
initConstructor ( ) {
this . css = `
$ { BDFDB . dotCNS . _imagegallerygallery + BDFDB . dotCN . imagewrapper + BDFDB . dotCN . _imagegalleryprevious } ,
$ { BDFDB . dotCNS . _imagegallerygallery + BDFDB . dotCN . imagewrapper + BDFDB . dotCN . _imagegallerynext } {
position : fixed ;
z - index : - 1 ;
}
$ { BDFDB . dotCNS . _imagegallerygallery + BDFDB . dotCN . imagewrapper + BDFDB . dotCN . _imagegalleryprevious } {
right : 90 % ;
}
$ { BDFDB . dotCNS . _imagegallerygallery + BDFDB . dotCN . imagewrapper + BDFDB . dotCN . _imagegallerynext } {
left : 90 % ;
} ` ;
}
2019-01-26 22:45:19 +01:00
2020-02-04 08:20:40 +01:00
//legacy
load ( ) { }
start ( ) {
if ( ! window . BDFDB ) window . BDFDB = { myPlugins : { } } ;
if ( window . BDFDB && window . BDFDB . myPlugins && typeof window . BDFDB . myPlugins == "object" ) window . BDFDB . myPlugins [ this . getName ( ) ] = this ;
let libraryScript = document . querySelector ( "head script#BDFDBLibraryScript" ) ;
if ( ! libraryScript || ( performance . now ( ) - libraryScript . getAttribute ( "date" ) ) > 600000 ) {
if ( libraryScript ) libraryScript . remove ( ) ;
libraryScript = document . createElement ( "script" ) ;
libraryScript . setAttribute ( "id" , "BDFDBLibraryScript" ) ;
libraryScript . setAttribute ( "type" , "text/javascript" ) ;
libraryScript . setAttribute ( "src" , "https://mwittrien.github.io/BetterDiscordAddons/Plugins/BDFDB.min.js" ) ;
libraryScript . setAttribute ( "date" , performance . now ( ) ) ;
libraryScript . addEventListener ( "load" , _ => { this . initialize ( ) ; } ) ;
document . head . appendChild ( libraryScript ) ;
2019-11-24 09:18:13 +01:00
}
2020-02-04 08:20:40 +01:00
else if ( window . BDFDB && typeof BDFDB === "object" && BDFDB . loaded ) this . initialize ( ) ;
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
}
2020-02-04 08:20:40 +01:00
initialize ( ) {
if ( window . BDFDB && typeof BDFDB === "object" && BDFDB . loaded ) {
if ( this . started ) return ;
BDFDB . PluginUtils . init ( this ) ;
eventFired = false ;
2019-01-26 22:45:19 +01:00
2020-02-04 08:20:40 +01:00
BDFDB . ModuleUtils . forceAllUpdates ( this ) ;
}
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
}
2020-02-04 08:20:40 +01:00
stop ( ) {
if ( window . BDFDB && typeof BDFDB === "object" && BDFDB . loaded ) {
this . stopping = true ;
2019-10-22 11:37:23 +02:00
2020-02-04 08:20:40 +01:00
BDFDB . ModuleUtils . forceAllUpdates ( this ) ;
2019-01-26 22:45:19 +01:00
2020-02-04 08:20:40 +01:00
BDFDB . PluginUtils . clear ( this ) ;
}
2018-10-11 10:21:26 +02:00
}
2019-01-26 22:45:19 +01:00
2020-02-04 08:20:40 +01:00
// begin of own functions
2019-01-26 22:45:19 +01:00
2020-02-04 08:20:40 +01:00
processImageModal ( e ) {
let messages = this . getMessageGroupOfImage ( e . instance . props . src ) ;
if ( messages . length ) {
if ( e . returnvalue ) {
let images = messages . map ( n => Array . from ( n . querySelectorAll ( BDFDB . dotCNS . imagewrapper + "img" ) ) ) . flat ( ) . filter ( img => ! BDFDB . DOMUtils . getParent ( BDFDB . dotCN . spoilerhidden , img ) ) ;
let next , previous ;
for ( let i = 0 ; i < images . length ; i ++ ) if ( this . getSrcOfImage ( e . instance . props . src ) == this . getSrcOfImage ( images [ i ] ) ) {
next = this . getSrcOfImage ( images [ i + 1 ] ) ;
previous = this . getSrcOfImage ( images [ i - 1 ] ) ;
break ;
}
if ( next ) {
if ( e . instance . nextRef ) e . returnvalue . props . children . splice ( 1 , 0 , e . instance . nextRef ) ;
else this . loadImage ( e . instance , this . getSrcOfImage ( next ) , "next" ) ;
}
if ( previous ) {
if ( e . instance . previousRef ) e . returnvalue . props . children . push ( e . instance . previousRef ) ;
else this . loadImage ( e . instance , this . getSrcOfImage ( previous ) , "previous" ) ;
}
2019-01-11 21:57:59 +01:00
}
2020-02-04 08:20:40 +01:00
if ( e . node ) {
BDFDB . DOMUtils . addClass ( BDFDB . DOMUtils . getParent ( BDFDB . dotCN . modal , e . node ) , BDFDB . disCN . _imagegallerygallery ) ;
this . cleanUpListeners ( ) ;
document . keydownImageGalleryListener = event => {
if ( ! document . contains ( e . node ) ) this . cleanUpListeners ( ) ;
else if ( ! eventFired ) {
eventFired = true ;
if ( event . keyCode == 37 ) this . switchImages ( e . instance , "previous" ) ;
else if ( event . keyCode == 39 ) this . switchImages ( e . instance , "next" ) ;
}
} ;
document . keyupImageGalleryListener = _ => {
eventFired = false ;
if ( ! document . contains ( e . node ) ) this . cleanUpListeners ( ) ;
} ;
document . addEventListener ( "keydown" , document . keydownImageGalleryListener ) ;
document . addEventListener ( "keyup" , document . keyupImageGalleryListener ) ;
2018-10-11 10:21:26 +02:00
}
}
2020-02-04 08:20:40 +01:00
}
getMessageGroupOfImage ( src ) {
if ( src ) for ( let message of document . querySelectorAll ( BDFDB . dotCN . message ) ) for ( let img of message . querySelectorAll ( BDFDB . dotCNS . imagewrapper + "img" ) ) if ( img . src && this . getSrcOfImage ( img ) == this . getSrcOfImage ( src ) ) {
let previousSiblings = [ ] , nextSiblings = [ ] ;
let previousSibling = message . previousSibling , nextSibling = message . nextSibling ;
2020-02-06 23:04:41 +01:00
if ( ! BDFDB . DOMUtils . containsClass ( message , BDFDB . disCN . messagegroupstart ) ) while ( previousSibling ) {
2020-02-04 08:20:40 +01:00
previousSiblings . push ( previousSibling ) ;
if ( BDFDB . DOMUtils . containsClass ( previousSibling , BDFDB . disCN . messagegroupstart ) ) previousSibling = null ;
else previousSibling = previousSibling . previousSibling ;
}
while ( nextSibling ) {
if ( ! BDFDB . DOMUtils . containsClass ( nextSibling , BDFDB . disCN . messagegroupstart ) ) {
nextSiblings . push ( nextSibling ) ;
nextSibling = nextSibling . nextSibling ;
2019-11-14 23:15:44 +01:00
}
2020-02-04 08:20:40 +01:00
else nextSibling = null ;
}
return [ ] . concat ( previousSiblings . reverse ( ) , message , nextSiblings ) . filter ( n => n && BDFDB . DOMUtils . containsClass ( n , BDFDB . disCN . message ) ) ;
2018-10-11 10:21:26 +02:00
}
2020-02-04 08:20:40 +01:00
return [ ] ;
2018-10-11 10:21:26 +02:00
}
2019-01-26 22:45:19 +01:00
2020-02-04 08:20:40 +01:00
getSrcOfImage ( img ) {
if ( ! img ) return null ;
return ( typeof img == "string" ? img : ( img . src || ( img . querySelector ( "canvas" ) ? img . querySelector ( "canvas" ) . src : "" ) ) ) . split ( "?width=" ) [ 0 ] ;
}
loadImage ( instance , src , type ) {
let imagethrowaway = document . createElement ( "img" ) ;
imagethrowaway . src = src ;
imagethrowaway . onload = _ => {
let arects = BDFDB . DOMUtils . getRects ( document . querySelector ( BDFDB . dotCN . appmount ) ) ;
let resizeY = ( arects . height / imagethrowaway . naturalHeight ) * 0.65 , resizeX = ( arects . width / imagethrowaway . naturalWidth ) * 0.8 ;
let resize = resizeX < resizeY ? resizeX : resizeY ;
let newHeight = imagethrowaway . naturalHeight * resize ;
let newWidth = imagethrowaway . naturalWidth * resize ;
instance [ type + "Ref" ] = BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . LazyImage , {
className : BDFDB . disCN [ ` _imagegallery ${ type } ` ] ,
src : src ,
height : imagethrowaway . naturalHeight ,
width : imagethrowaway . naturalWidth ,
maxHeight : newHeight ,
maxWidth : newWidth ,
onClick : _ => { this . switchImages ( instance , type ) ; }
} ) ;
BDFDB . ReactUtils . forceUpdate ( instance ) ;
} ;
}
switchImages ( instance , type ) {
let imageRef = instance [ type + "Ref" ] ;
if ( ! imageRef ) return ;
delete instance . previousRef ;
delete instance . nextRef ;
instance . props . original = imageRef . props . src ;
instance . props . placeholder = imageRef . props . src ;
instance . props . src = imageRef . props . src ;
instance . props . height = imageRef . props . height ;
instance . props . width = imageRef . props . width ;
2019-11-14 23:15:44 +01:00
BDFDB . ReactUtils . forceUpdate ( instance ) ;
2020-02-04 08:20:40 +01:00
}
cleanUpListeners ( ) {
document . removeEventListener ( "keydown" , document . keydownImageGalleryListener ) ;
document . removeEventListener ( "keyup" , document . keyupImageGalleryListener ) ;
delete document . keydownImageGalleryListener ;
delete document . keyupImageGalleryListener ;
}
2018-10-11 10:21:26 +02:00
}
2020-02-04 08:20:40 +01:00
} ) ( ) ;