From 3c2e0f0e16cbcae72b01b95c66d00d8a0bdbef63 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Thu, 28 Jan 2021 15:23:26 -0500 Subject: [PATCH] ace: Simplify Ace2Editor method creation * Delete the unused `optDoNow` parameter from `pendingInit()`. * Move the `setAuthorInfo()` 1st parameter check out of the wrapper and in to the `setAuthorInfo()` function itself. --- src/static/js/ace.js | 43 +++++++++++-------------------------- src/static/js/ace2_inner.js | 1 + 2 files changed, 13 insertions(+), 31 deletions(-) diff --git a/src/static/js/ace.js b/src/static/js/ace.js index 12e3dc535..c1c182728 100644 --- a/src/static/js/ace.js +++ b/src/static/js/ace.js @@ -53,23 +53,11 @@ function Ace2Editor() { let actionsPendingInit = []; - function pendingInit(func, optDoNow) { - return function () { - const that = this; - const args = arguments; - const action = function () { - func.apply(that, args); - }; - if (optDoNow) { - optDoNow.apply(that, args); - } - if (loaded) { - action(); - } else { - actionsPendingInit.push(action); - } - }; - } + const pendingInit = (func) => function (...args) { + const action = () => func.apply(this, args); + if (loaded) return action(); + actionsPendingInit.push(action); + }; function doActionsPendingInit() { _.each(actionsPendingInit, (fn, i) => { @@ -102,21 +90,14 @@ function Ace2Editor() { 'execCommand', 'replaceRange']; - _.each(aceFunctionsPendingInit, (fnName, i) => { - const prefix = 'ace_'; - const name = prefix + fnName; - editor[fnName] = pendingInit(function () { - if (fnName === 'setAuthorInfo') { - if (!arguments[0]) { - // setAuthorInfo AuthorId not set for some reason - } else { - info[prefix + fnName].apply(this, arguments); - } - } else { - info[prefix + fnName].apply(this, arguments); - } + for (const fnName of aceFunctionsPendingInit) { + // Note: info[`ace_${fnName}`] does not exist yet, so it can't be passed directly to + // pendingInit(). A simple wrapper is used to defer the info[`ace_${fnName}`] lookup until + // method invocation. + editor[fnName] = pendingInit(function (...args) { + info[`ace_${fnName}`].apply(this, args); }); - }); + } editor.exportText = function () { if (!loaded) return '(awaiting init)\n'; diff --git a/src/static/js/ace2_inner.js b/src/static/js/ace2_inner.js index e5de72f9c..31d6419ce 100644 --- a/src/static/js/ace2_inner.js +++ b/src/static/js/ace2_inner.js @@ -258,6 +258,7 @@ function Ace2Inner() { }; const setAuthorInfo = (author, info) => { + if (!author) return; // author ID not set for some reason if ((typeof author) !== 'string') { // Potentially caused by: https://github.com/ether/etherpad-lite/issues/2802"); throw new Error(`setAuthorInfo: author (${author}) is not a string`);