diff --git a/src/core/features/editor/components/rich-text-editor/rich-text-editor.ts b/src/core/features/editor/components/rich-text-editor/rich-text-editor.ts index 07bf8b799..d9b8c4abf 100644 --- a/src/core/features/editor/components/rich-text-editor/rich-text-editor.ts +++ b/src/core/features/editor/components/rich-text-editor/rich-text-editor.ts @@ -107,6 +107,7 @@ export class CoreEditorRichTextEditorComponent implements OnInit, AfterContentIn protected languageChangedSubscription?: Subscription; protected resizeListener?: CoreEventObserver; protected domPromise?: CoreCancellablePromise; + protected buttonsDomPromise?: CoreCancellablePromise; rteEnabled = false; isPhone = false; @@ -836,7 +837,10 @@ export class CoreEditorRichTextEditorComponent implements OnInit, AfterContentIn const length = await this.toolbarSlides.length(); - await CoreDomUtils.waitToBeInDOM(this.toolbar.nativeElement, 5000); + // Cancel previous one, if any. + this.buttonsDomPromise?.cancel(); + this.buttonsDomPromise = CoreDomUtils.waitToBeInDOM(this.toolbar.nativeElement); + await this.buttonsDomPromise; const width = this.toolbar.nativeElement.getBoundingClientRect().width; @@ -1097,6 +1101,7 @@ export class CoreEditorRichTextEditorComponent implements OnInit, AfterContentIn this.labelObserver?.disconnect(); this.resizeListener?.off(); this.domPromise?.cancel(); + this.buttonsDomPromise?.cancel(); } } diff --git a/src/core/services/utils/dom.ts b/src/core/services/utils/dom.ts index 0e8f1dfc1..65f00c7d3 100644 --- a/src/core/services/utils/dom.ts +++ b/src/core/services/utils/dom.ts @@ -97,10 +97,9 @@ export class CoreDomUtilsProvider { * Wait an element to be in dom of another element. * * @param element Element to wait. - * @param timeout If defined, timeout to wait before rejecting the promise. * @return Cancellable promise. */ - waitToBeInDOM(element: HTMLElement, timeout?: number): CoreCancellablePromise { + waitToBeInDOM(element: HTMLElement): CoreCancellablePromise { const root = element.getRootNode({ composed: true }); if (root === document) { @@ -109,33 +108,24 @@ export class CoreDomUtilsProvider { } let observer: MutationObserver; - let observerTimeout: number | undefined; return new CoreCancellablePromise( - (resolve, reject) => { + (resolve) => { observer = new MutationObserver(() => { const root = element.getRootNode({ composed: true }); - if (root === document) { - observer?.disconnect(); - observerTimeout && clearTimeout(observerTimeout); - resolve(); + if (root !== document) { + return; } + + observer?.disconnect(); + resolve(); }); - if (timeout) { - observerTimeout = window.setTimeout(() => { - observer?.disconnect(); - - reject(new Error('Waiting for DOM timeout reached')); - }, timeout); - } - observer.observe(document.body, { subtree: true, childList: true }); }, () => { observer?.disconnect(); - observerTimeout && clearTimeout(observerTimeout); }, ); }