From c696732838121b056d03cfed5760064697106775 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Sat, 27 Feb 2021 20:33:56 -0500 Subject: [PATCH] ace: Asyncify `Ace2Editor.init()` --- src/static/js/ace.js | 65 ++++++++++++++++++------------------- src/static/js/pad_editor.js | 3 +- 2 files changed, 34 insertions(+), 34 deletions(-) diff --git a/src/static/js/ace.js b/src/static/js/ace.js index 2255a7a9a..8613d9e37 100644 --- a/src/static/js/ace.js +++ b/src/static/js/ace.js @@ -150,15 +150,9 @@ const Ace2Editor = function () { info = null; // prevent IE 6 closure memory leaks }); - this.init = function (containerId, initialCode, doneFunc) { + this.init = async function (containerId, initialCode) { this.importText(initialCode); - info.onEditorReady = () => { - loaded = true; - doActionsPendingInit(); - doneFunc(); - }; - // calls to these functions ($$INCLUDE_...) are replaced when this file is processed // and compressed, putting the compressed code from the named file directly into the // source here. @@ -190,7 +184,7 @@ const Ace2Editor = function () { iframeHTML.push(``); } - iframeHTML.push(scriptTag(`(() => { + iframeHTML.push(scriptTag(`(async () => { const require = window.require; require.setRootURI(${JSON.stringify(absUrl('../javascripts/src'))}); require.setLibraryURI(${JSON.stringify(absUrl('../javascripts/lib'))}); @@ -203,10 +197,12 @@ const Ace2Editor = function () { window.$ = window.jQuery = require('ep_etherpad-lite/static/js/rjquery').jQuery; - window.plugins.ensure(() => { - const editorInfo = parent.parent.ace2EditorInfo; - window.Ace2Inner.init(editorInfo, editorInfo.onEditorReady); - }); + await new Promise((resolve, reject) => window.plugins.ensure( + (err) => err != null ? reject(err) : resolve())); + const editorInfo = parent.parent.ace2EditorInfo; + await new Promise((resolve, reject) => window.Ace2Inner.init( + editorInfo, (err) => err != null ? reject(err) : resolve())); + editorInfo.onEditorReady(); })();`)); iframeHTML.push(''); @@ -218,24 +214,22 @@ const Ace2Editor = function () { iframeHTML.push(' '); - const outerScript = `(() => { - window.onload = () => { - window.onload = null; - setTimeout(() => { - const iframe = document.createElement('iframe'); - iframe.name = 'ace_inner'; - iframe.title = 'pad'; - iframe.scrolling = 'no'; - iframe.frameBorder = 0; - iframe.allowTransparency = true; // for IE - iframe.ace_outerWin = window; - document.body.insertBefore(iframe, document.body.firstChild); - const doc = iframe.contentWindow.document; - doc.open(); - doc.write(${JSON.stringify(iframeHTML.join('\n'))}); - doc.close(); - }, 0); - } + const outerScript = `(async () => { + await new Promise((resolve) => { window.onload = () => resolve(); }); + window.onload = null; + await new Promise((resolve) => setTimeout(resolve, 0)); + const iframe = document.createElement('iframe'); + iframe.name = 'ace_inner'; + iframe.title = 'pad'; + iframe.scrolling = 'no'; + iframe.frameBorder = 0; + iframe.allowTransparency = true; // for IE + iframe.ace_outerWin = window; + document.body.insertBefore(iframe, document.body.firstChild); + const doc = iframe.contentWindow.document; + doc.open(); + doc.write(${JSON.stringify(iframeHTML.join('\n'))}); + doc.close(); })();`; const outerHTML = @@ -264,9 +258,14 @@ const Ace2Editor = function () { const editorDocument = outerFrame.contentWindow.document; - editorDocument.open(); - editorDocument.write(outerHTML.join('')); - editorDocument.close(); + await new Promise((resolve, reject) => { + info.onEditorReady = (err) => err != null ? reject(err) : resolve(); + editorDocument.open(); + editorDocument.write(outerHTML.join('')); + editorDocument.close(); + }); + loaded = true; + doActionsPendingInit(); }; }; diff --git a/src/static/js/pad_editor.js b/src/static/js/pad_editor.js index a32af1644..75481fe5e 100644 --- a/src/static/js/pad_editor.js +++ b/src/static/js/pad_editor.js @@ -56,7 +56,8 @@ const padeditor = (() => { }; self.ace = new Ace2Editor(); - self.ace.init('editorcontainer', '', aceReady); + self.ace.init('editorcontainer', '').then( + () => aceReady(), (err) => { throw err || new Error(err); }); self.ace.setProperty('wraps', true); if (pad.getIsDebugEnabled()) { self.ace.setProperty('dmesg', pad.dmesg);