ace: Asyncify Ace2Editor.init()

This commit is contained in:
Richard Hansen 2021-02-27 20:33:56 -05:00 committed by John McLear
parent 159fd5bdeb
commit c696732838
2 changed files with 34 additions and 34 deletions

View file

@ -150,15 +150,9 @@ const Ace2Editor = function () {
info = null; // prevent IE 6 closure memory leaks info = null; // prevent IE 6 closure memory leaks
}); });
this.init = function (containerId, initialCode, doneFunc) { this.init = async function (containerId, initialCode) {
this.importText(initialCode); this.importText(initialCode);
info.onEditorReady = () => {
loaded = true;
doActionsPendingInit();
doneFunc();
};
// calls to these functions ($$INCLUDE_...) are replaced when this file is processed // 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 // and compressed, putting the compressed code from the named file directly into the
// source here. // source here.
@ -190,7 +184,7 @@ const Ace2Editor = function () {
iframeHTML.push(`<script type="text/javascript" src="${url}"></script>`); iframeHTML.push(`<script type="text/javascript" src="${url}"></script>`);
} }
iframeHTML.push(scriptTag(`(() => { iframeHTML.push(scriptTag(`(async () => {
const require = window.require; const require = window.require;
require.setRootURI(${JSON.stringify(absUrl('../javascripts/src'))}); require.setRootURI(${JSON.stringify(absUrl('../javascripts/src'))});
require.setLibraryURI(${JSON.stringify(absUrl('../javascripts/lib'))}); 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.$ = window.jQuery = require('ep_etherpad-lite/static/js/rjquery').jQuery;
window.plugins.ensure(() => { await new Promise((resolve, reject) => window.plugins.ensure(
(err) => err != null ? reject(err) : resolve()));
const editorInfo = parent.parent.ace2EditorInfo; const editorInfo = parent.parent.ace2EditorInfo;
window.Ace2Inner.init(editorInfo, editorInfo.onEditorReady); await new Promise((resolve, reject) => window.Ace2Inner.init(
}); editorInfo, (err) => err != null ? reject(err) : resolve()));
editorInfo.onEditorReady();
})();`)); })();`));
iframeHTML.push('<style type="text/css" title="dynamicsyntax"></style>'); iframeHTML.push('<style type="text/css" title="dynamicsyntax"></style>');
@ -218,10 +214,10 @@ const Ace2Editor = function () {
iframeHTML.push('</head><body id="innerdocbody" class="innerdocbody" role="application" ' + iframeHTML.push('</head><body id="innerdocbody" class="innerdocbody" role="application" ' +
'spellcheck="false">&nbsp;</body></html>'); 'spellcheck="false">&nbsp;</body></html>');
const outerScript = `(() => { const outerScript = `(async () => {
window.onload = () => { await new Promise((resolve) => { window.onload = () => resolve(); });
window.onload = null; window.onload = null;
setTimeout(() => { await new Promise((resolve) => setTimeout(resolve, 0));
const iframe = document.createElement('iframe'); const iframe = document.createElement('iframe');
iframe.name = 'ace_inner'; iframe.name = 'ace_inner';
iframe.title = 'pad'; iframe.title = 'pad';
@ -234,8 +230,6 @@ const Ace2Editor = function () {
doc.open(); doc.open();
doc.write(${JSON.stringify(iframeHTML.join('\n'))}); doc.write(${JSON.stringify(iframeHTML.join('\n'))});
doc.close(); doc.close();
}, 0);
}
})();`; })();`;
const outerHTML = const outerHTML =
@ -264,9 +258,14 @@ const Ace2Editor = function () {
const editorDocument = outerFrame.contentWindow.document; const editorDocument = outerFrame.contentWindow.document;
await new Promise((resolve, reject) => {
info.onEditorReady = (err) => err != null ? reject(err) : resolve();
editorDocument.open(); editorDocument.open();
editorDocument.write(outerHTML.join('')); editorDocument.write(outerHTML.join(''));
editorDocument.close(); editorDocument.close();
});
loaded = true;
doActionsPendingInit();
}; };
}; };

View file

@ -56,7 +56,8 @@ const padeditor = (() => {
}; };
self.ace = new Ace2Editor(); 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); self.ace.setProperty('wraps', true);
if (pad.getIsDebugEnabled()) { if (pad.getIsDebugEnabled()) {
self.ace.setProperty('dmesg', pad.dmesg); self.ace.setProperty('dmesg', pad.dmesg);