From 3a311d21828b57fbcc97d6feeca5c59253f49d2a Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Fri, 26 Feb 2021 01:09:19 -0500 Subject: [PATCH] ace: Lint and simplify script strings --- src/static/js/ace.js | 58 +++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 31 deletions(-) diff --git a/src/static/js/ace.js b/src/static/js/ace.js index c5d517ba8..65608bb74 100644 --- a/src/static/js/ace.js +++ b/src/static/js/ace.js @@ -197,22 +197,21 @@ const Ace2Editor = function () { iframeHTML.push(``); iframeHTML.push(``); - iframeHTML.push(scriptTag(` - require.setRootURI("../javascripts/src"); - require.setLibraryURI("../javascripts/lib"); - require.setGlobalKeyPath("require"); + iframeHTML.push(scriptTag(`(() => { + const require = window.require; + require.setRootURI('../javascripts/src'); + require.setLibraryURI('../javascripts/lib'); + require.setGlobalKeyPath('require'); // intentially moved before requiring client_plugins to save a 307 - var Ace2Inner = require("ep_etherpad-lite/static/js/ace2_inner"); - var plugins = require("ep_etherpad-lite/static/js/pluginfw/client_plugins"); - plugins.adoptPluginsFromAncestorsOf(window); + window.Ace2Inner = require('ep_etherpad-lite/static/js/ace2_inner'); + window.plugins = require('ep_etherpad-lite/static/js/pluginfw/client_plugins'); + window.plugins.adoptPluginsFromAncestorsOf(window); - $ = jQuery = require("ep_etherpad-lite/static/js/rjquery").jQuery; // Expose jQuery #HACK + window.$ = window.jQuery = require('ep_etherpad-lite/static/js/rjquery').jQuery; - plugins.ensure(function () { - Ace2Inner.init(); - }); - `)); + window.plugins.ensure(() => { window.Ace2Inner.init(); }); + })();`)); iframeHTML.push(''); @@ -227,34 +226,31 @@ const Ace2Editor = function () { const gt = typeof globalThis === 'object' ? globalThis : window; gt.ChildAccessibleAce2Editor = Ace2Editor; - const outerScript = ` - editorId = ${JSON.stringify(info.id)}; - editorInfo = parent.ChildAccessibleAce2Editor.registry[editorId]; - window.onload = function () { + const outerScript = `(() => { + window.editorInfo = parent.ChildAccessibleAce2Editor.registry[${JSON.stringify(info.id)}]; + window.onload = () => { window.onload = null; - setTimeout(function () { - var iframe = document.createElement("IFRAME"); - iframe.name = "ace_inner"; - iframe.title = "pad"; - iframe.scrolling = "no"; - var outerdocbody = document.getElementById("outerdocbody"); + setTimeout(() => { + const iframe = document.createElement('iframe'); + iframe.name = 'ace_inner'; + iframe.title = 'pad'; + iframe.scrolling = 'no'; iframe.frameBorder = 0; iframe.allowTransparency = true; // for IE - outerdocbody.insertBefore(iframe, outerdocbody.firstChild); iframe.ace_outerWin = window; - readyFunc = function () { - editorInfo.onEditorReady(); - readyFunc = null; - editorInfo = null; + document.body.insertBefore(iframe, document.body.firstChild); + window.readyFunc = () => { + delete window.readyFunc; + window.editorInfo.onEditorReady(); + delete window.editorInfo; }; - var doc = iframe.contentWindow.document; + const doc = iframe.contentWindow.document; doc.open(); - var text = (${JSON.stringify(iframeHTML.join('\n'))}); - doc.write(text); + doc.write(${JSON.stringify(iframeHTML.join('\n'))}); doc.close(); }, 0); } - `; + })();`; const outerHTML = [doctype, ``];