import SETTINGS from "../../../common/Settings" ; export default class ScrollManage { private static readonly PREF_ENABLED = 'scroll-manage-enabled' ; private static readonly PREF_SCROLL_IF_YOU = 'scroll-manage-scroll-if-you' ; private static readonly ScrollThreshold = 50 ; private static _scrollToBottomButton : JQuery < HTMLElement > ; static get enabled ( ) : boolean { return SETTINGS . get ( this . PREF_ENABLED ) ; } static get scrollToBottomIfYouSendMessage ( ) : boolean { return SETTINGS . get ( this . PREF_SCROLL_IF_YOU ) ; } static init ( ) { Hooks . on ( 'renderChatLog' , this . _renderChatLog . bind ( this ) ) ; SETTINGS . register ( this . PREF_ENABLED , { name : 'DF_CHAT_SCROLL.EnableName' , hint : 'DF_CHAT_SCROLL.EnableHint' , scope : 'world' , config : true , type : Boolean , default : true , onChange : ( newValue ) => { if ( newValue ) { libWrapper . register ( SETTINGS . MOD_NAME , 'ChatLog.prototype.postOne' , this . _ChatLog_postOne , 'OVERRIDE' ) ; } else { libWrapper . unregister ( SETTINGS . MOD_NAME , 'ChatLog.prototype.postOne' , false ) ; } } } ) ; SETTINGS . register ( this . PREF_SCROLL_IF_YOU , { name : 'DF_CHAT_SCROLL.ScrollIfYouName' , hint : 'DF_CHAT_SCROLL.ScrollIfYouHint' , scope : 'world' , config : true , type : Boolean , default : true } ) ; } static ready ( ) { if ( this . enabled ) libWrapper . register ( SETTINGS . MOD_NAME , 'ChatLog.prototype.postOne' , this . _ChatLog_postOne , 'OVERRIDE' ) ; } private static _renderChatLog ( app , html ) { this . _scrollToBottomButton = $ ( `<div id="scrollToBottom" style="display:none">
	<span>${ 'DF_CHAT_SCROLL.NewMessage' . localize ( ) }</span> ${ 'DF_CHAT_SCROLL.ScrollButton' . localize ( ) }
</div>` ) ; this . _scrollToBottomButton . on ( 'click' , ( ) => { const el = app . element ; const log = el . length ? el [ 0 ] . querySelector ( "#chat-log" ) : null ; if ( log ) { log . scrollTo ( { behavior : "smooth" , top : log . scrollHeight } ) ; } } ) ; html . find ( 'div#chat-controls' ) . before ( this . _scrollToBottomButton ) ; html . find ( 'ol#chat-log' ) . on ( 'scroll' , ( event ) => { if ( ! this . _scrollToBottomButton ) return ; const element = < HTMLOListElement > event . currentTarget ; if ( element . clientHeight > element . scrollHeight - this . ScrollThreshold ) return ; if ( element . scrollTop < ( element . scrollHeight - element . clientHeight ) - this . ScrollThreshold ) this . _scrollToBottomButton . show ( ) ; else { this . _scrollToBottomButton . hide ( ) ; this . _scrollToBottomButton . removeClass ( 'new' ) ; } } ) ; } private static async _ChatLog_postOne ( this , message , notify = false ) { if ( ! message . visible ) return ; if ( ! this . _lastId ) this . _lastId = message . id ; if ( ( message . data . whisper || [ ] ) . includes ( game . user . id ) && ! message . isRoll ) { this . _lastWhisper = message ; } const element = this . element . find ( "#chat-log" ) ; const atBottom = element [ 0 ] . scrollTop >= ( ( element [ 0 ] . scrollHeight - element [ 0 ] . clientHeight ) - ScrollManage . ScrollThreshold ) ; const html = await ( < any > message ) . getHTML ( ) ; element . append ( html ) ; if ( atBottom || ( ScrollManage . scrollToBottomIfYouSendMessage && message . isAuthor ) ) element [ 0 ] . scrollTo ( { top : element [ 0 ] . scrollHeight , behavior : 'smooth' } ) ; else { ScrollManage . _scrollToBottomButton . addClass ( 'new' ) ; } if ( notify ) this . notify ( message ) ; if ( this . _popout ) await ( < any > this . _popout ) . postOne ( message , false ) ; if ( this . popOut ) this . setPosition ( ) ; } }