diff --git a/src/addons/mod/forum/components/post/post.ts b/src/addons/mod/forum/components/post/post.ts index a59a9c1da..0f1107cb6 100644 --- a/src/addons/mod/forum/components/post/post.ts +++ b/src/addons/mod/forum/components/post/post.ts @@ -225,6 +225,7 @@ export class AddonModForumPostComponent implements OnInit, OnDestroy, OnChanges cmId: this.forum.cmid, }, event, + waitForDismiss: true, }); if (popoverData && popoverData.action) { @@ -319,15 +320,7 @@ export class AddonModForumPostComponent implements OnInit, OnDestroy, OnChanges await this.confirmDiscard(); this.setReplyFormData(this.post.id); - if (this.content) { - setTimeout(() => { - CoreDomUtils.scrollToElementBySelector( - this.elementRef.nativeElement, - this.content, - '#addon-forum-reply-edit-form-' + this.uniqueId, - ); - }); - } + this.scrollToForm(); } catch { // Cancelled. } @@ -351,16 +344,7 @@ export class AddonModForumPostComponent implements OnInit, OnDestroy, OnChanges this.messageControl.setValue(this.replyData.message); } - if (this.content) { - setTimeout(() => { - CoreDomUtils.scrollToElementBySelector( - this.elementRef.nativeElement, - this.content, - '#addon-forum-reply-edit-form-' + this.uniqueId, - ); - }); - } - + this.scrollToForm(); } /** @@ -382,6 +366,8 @@ export class AddonModForumPostComponent implements OnInit, OnDestroy, OnChanges this.post.attachments, this.post.isprivatereply, ); + + this.scrollToForm(5); } catch (error) { // Cancelled. } @@ -600,4 +586,24 @@ export class AddonModForumPostComponent implements OnInit, OnDestroy, OnChanges delete this.replyData.syncId; } + /** + * Scroll to reply/edit form. + * + * @param ticksToWait Number of ticks to wait before scrolling. + * @return Promise resolved when done. + */ + protected async scrollToForm(ticksToWait = 1): Promise { + if (!this.content) { + return; + } + + await CoreUtils.nextTicks(ticksToWait); + + CoreDomUtils.scrollToElementBySelector( + this.elementRef.nativeElement, + this.content, + '#addon-forum-reply-edit-form-' + this.uniqueId, + ); + } + } diff --git a/src/core/services/utils/dom.ts b/src/core/services/utils/dom.ts index 7a3d37e90..966324808 100644 --- a/src/core/services/utils/dom.ts +++ b/src/core/services/utils/dom.ts @@ -1728,13 +1728,13 @@ export class CoreDomUtilsProvider { /** * Opens a popover. * - * @param popoverOptions Modal Options. + * @param options Options. */ async openPopover( - popoverOptions: PopoverOptions, + options: OpenPopoverOptions, ): Promise { - const popover = await PopoverController.create(popoverOptions); + const popover = await PopoverController.create(options); const zoomLevel = await CoreConfig.get(CoreConstants.SETTINGS_ZOOM_LEVEL, CoreZoomLevel.NORMAL); await popover.present(); @@ -1743,16 +1743,16 @@ export class CoreDomUtilsProvider { if (zoomLevel !== CoreZoomLevel.NORMAL) { switch (getMode()) { case 'ios': - fixIOSPopoverPosition(popover, popoverOptions.event); + fixIOSPopoverPosition(popover, options.event); break; case 'md': - fixMDPopoverPosition(popover, popoverOptions.event); + fixMDPopoverPosition(popover, options.event); break; } } // If onDidDismiss is nedded we can add a new param to the function to wait one function or the other. - const result = await popover.onWillDismiss(); + const result = options.waitForDismiss ? await popover.onDidDismiss() : await popover.onWillDismiss(); if (result?.data) { return result?.data; } @@ -2046,3 +2046,10 @@ export const CoreDomUtils = makeSingleton(CoreDomUtilsProvider); type AnchorOrMediaElement = HTMLAnchorElement | HTMLImageElement | HTMLAudioElement | HTMLVideoElement | HTMLSourceElement | HTMLTrackElement; + +/** + * Options for the openPopover function. + */ +export type OpenPopoverOptions = PopoverOptions & { + waitForDismiss?: boolean; +};