1212// See the License for the specific language governing permissions and
1313// limitations under the License.
1414
15- import { Component , OnDestroy , OnInit , ViewChild } from '@angular/core' ;
15+ import { Component , OnDestroy , OnInit , ViewChild , AfterViewInit } from '@angular/core' ;
1616import { CoreEventObserver , CoreEvents } from '@singletons/events' ;
1717import { ActivatedRoute } from '@angular/router' ;
1818import { CoreSites } from '@services/sites' ;
@@ -40,11 +40,12 @@ import { CoreNetwork } from '@services/network';
4040import moment from 'moment-timezone' ;
4141import { Subscription } from 'rxjs' ;
4242import { CoreAnimations } from '@components/animations' ;
43- import { CoreKeyboard } from '@singletons/keyboard' ;
4443import { CoreToasts , ToastDuration } from '@services/overlays/toasts' ;
4544import { CoreLoadings } from '@services/overlays/loadings' ;
4645import { CORE_COMMENTS_AUTO_SYNCED } from '@features/comments/constants' ;
4746import { CoreAlerts } from '@services/overlays/alerts' ;
47+ import { CoreWait } from '@singletons/wait' ;
48+ import { CoreDom } from '@singletons/dom' ;
4849
4950/**
5051 * Page that displays comments.
@@ -55,7 +56,7 @@ import { CoreAlerts } from '@services/overlays/alerts';
5556 animations : [ CoreAnimations . SLIDE_IN_OUT ] ,
5657 styleUrls : [ '../../../../../theme/components/discussion.scss' , 'viewer.scss' ] ,
5758} )
58- export class CoreCommentsViewerPage implements OnInit , OnDestroy {
59+ export class CoreCommentsViewerPage implements OnInit , OnDestroy , AfterViewInit {
5960
6061 @ViewChild ( IonContent ) content ?: IonContent ;
6162
@@ -86,7 +87,10 @@ export class CoreCommentsViewerPage implements OnInit, OnDestroy {
8687 protected addDeleteCommentsAvailable = false ;
8788 protected syncObserver ?: CoreEventObserver ;
8889 protected onlineObserver : Subscription ;
90+ protected keyboardObserver : CoreEventObserver ;
8991 protected viewDestroyed = false ;
92+ protected scrollBottom = true ;
93+ protected scrollElement ?: HTMLElement ;
9094
9195 constructor (
9296 protected route : ActivatedRoute ,
@@ -118,6 +122,11 @@ export class CoreCommentsViewerPage implements OnInit, OnDestroy {
118122 this . isOnline = CoreNetwork . isOnline ( ) ;
119123 } ) ;
120124 } ) ;
125+
126+ this . keyboardObserver = CoreEvents . on ( CoreEvents . KEYBOARD_CHANGE , ( keyboardHeight : number ) => {
127+ // Force when opening.
128+ this . scrollToBottom ( keyboardHeight > 0 ) ;
129+ } ) ;
121130 }
122131
123132 /**
@@ -150,6 +159,13 @@ export class CoreCommentsViewerPage implements OnInit, OnDestroy {
150159 await this . fetchComments ( true ) ;
151160 }
152161
162+ /**
163+ * View has been initialized.
164+ */
165+ async ngAfterViewInit ( ) : Promise < void > {
166+ this . scrollElement = await this . content ?. getScrollElement ( ) ;
167+ }
168+
153169 /**
154170 * Fetches the comments.
155171 *
@@ -164,6 +180,8 @@ export class CoreCommentsViewerPage implements OnInit, OnDestroy {
164180 await CorePromiseUtils . ignoreErrors ( this . syncComments ( showErrors ) ) ;
165181 }
166182
183+ this . scrollBottom = CoreDom . scrollIsBottom ( this . scrollElement , 5 ) ;
184+
167185 try {
168186 // Get comments data.
169187 const commentsResponse = await CoreComments . getComments (
@@ -210,9 +228,7 @@ export class CoreCommentsViewerPage implements OnInit, OnDestroy {
210228 this . refreshIcon = CoreConstants . ICON_REFRESH ;
211229 this . syncIcon = CoreConstants . ICON_SYNC ;
212230
213- if ( this . page == 0 ) {
214- this . scrollToBottom ( ) ;
215- }
231+ this . scrollToBottom ( this . page === 0 ) ;
216232 }
217233
218234 }
@@ -314,7 +330,6 @@ export class CoreCommentsViewerPage implements OnInit, OnDestroy {
314330 * @param text Comment text to add.
315331 */
316332 async addComment ( text : string ) : Promise < void > {
317- CoreKeyboard . close ( ) ;
318333 const loadingModal = await CoreLoadings . show ( 'core.sending' , true ) ;
319334 // Freeze the add comment button.
320335 this . sending = true ;
@@ -328,14 +343,6 @@ export class CoreCommentsViewerPage implements OnInit, OnDestroy {
328343 this . area ,
329344 ) ;
330345
331- CoreToasts . show ( {
332- message : commentsResponse ? 'core.comments.eventcommentcreated' : 'core.datastoredoffline' ,
333- translateMessage : true ,
334- duration : ToastDuration . LONG ,
335- position : 'bottom' ,
336- positionAnchor : 'viewer-footer' ,
337- } ) ;
338-
339346 if ( commentsResponse ) {
340347 this . invalidateComments ( ) ;
341348
@@ -364,11 +371,11 @@ export class CoreCommentsViewerPage implements OnInit, OnDestroy {
364371 } catch ( error ) {
365372 CoreAlerts . showError ( error ) ;
366373 } finally {
367- loadingModal . dismiss ( ) ;
368374 this . sending = false ;
375+ await loadingModal . dismiss ( ) ;
369376
370377 // New comments.
371- this . scrollToBottom ( ) ;
378+ this . scrollToBottom ( true ) ;
372379 }
373380 }
374381
@@ -604,14 +611,25 @@ export class CoreCommentsViewerPage implements OnInit, OnDestroy {
604611
605612 /**
606613 * Scroll bottom when render has finished.
614+ *
615+ * @param force Whether to force scroll to bottom.
607616 */
608- protected scrollToBottom ( ) : void {
609- // Need a timeout to leave time to the view to be rendered.
610- setTimeout ( ( ) => {
611- if ( ! this . viewDestroyed ) {
612- this . content ?. scrollToBottom ( ) ;
613- }
614- } , 100 ) ;
617+ protected async scrollToBottom ( force = false ) : Promise < void > {
618+ if ( this . viewDestroyed ) {
619+ return ;
620+ }
621+
622+ // Check if scroll is at bottom. If so, scroll bottom after rendering since there might be something new.
623+ if ( ! this . scrollBottom && ! force ) {
624+ return ;
625+ }
626+
627+ // Leave time for the view to be rendered.
628+ await CoreWait . nextTicks ( 5 ) ;
629+
630+ if ( ! this . viewDestroyed && this . content ) {
631+ this . content . scrollToBottom ( 0 ) ;
632+ }
615633 }
616634
617635 /**
@@ -650,6 +668,7 @@ export class CoreCommentsViewerPage implements OnInit, OnDestroy {
650668 this . syncObserver ?. off ( ) ;
651669 this . onlineObserver . unsubscribe ( ) ;
652670 this . viewDestroyed = true ;
671+ this . keyboardObserver . off ( ) ;
653672 }
654673
655674}
0 commit comments