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.
This commit is contained in:
Richard Hansen 2021-01-28 15:23:26 -05:00 committed by John McLear
parent 865a463154
commit 3c2e0f0e16
2 changed files with 13 additions and 31 deletions

View file

@ -53,23 +53,11 @@ function Ace2Editor() {
let actionsPendingInit = []; let actionsPendingInit = [];
function pendingInit(func, optDoNow) { const pendingInit = (func) => function (...args) {
return function () { const action = () => func.apply(this, args);
const that = this; if (loaded) return action();
const args = arguments;
const action = function () {
func.apply(that, args);
};
if (optDoNow) {
optDoNow.apply(that, args);
}
if (loaded) {
action();
} else {
actionsPendingInit.push(action); actionsPendingInit.push(action);
}
}; };
}
function doActionsPendingInit() { function doActionsPendingInit() {
_.each(actionsPendingInit, (fn, i) => { _.each(actionsPendingInit, (fn, i) => {
@ -102,21 +90,14 @@ function Ace2Editor() {
'execCommand', 'execCommand',
'replaceRange']; 'replaceRange'];
_.each(aceFunctionsPendingInit, (fnName, i) => { for (const fnName of aceFunctionsPendingInit) {
const prefix = 'ace_'; // Note: info[`ace_${fnName}`] does not exist yet, so it can't be passed directly to
const name = prefix + fnName; // pendingInit(). A simple wrapper is used to defer the info[`ace_${fnName}`] lookup until
editor[fnName] = pendingInit(function () { // method invocation.
if (fnName === 'setAuthorInfo') { editor[fnName] = pendingInit(function (...args) {
if (!arguments[0]) { info[`ace_${fnName}`].apply(this, args);
// setAuthorInfo AuthorId not set for some reason
} else {
info[prefix + fnName].apply(this, arguments);
}
} else {
info[prefix + fnName].apply(this, arguments);
}
});
}); });
}
editor.exportText = function () { editor.exportText = function () {
if (!loaded) return '(awaiting init)\n'; if (!loaded) return '(awaiting init)\n';

View file

@ -258,6 +258,7 @@ function Ace2Inner() {
}; };
const setAuthorInfo = (author, info) => { const setAuthorInfo = (author, info) => {
if (!author) return; // author ID not set for some reason
if ((typeof author) !== 'string') { if ((typeof author) !== 'string') {
// Potentially caused by: https://github.com/ether/etherpad-lite/issues/2802"); // Potentially caused by: https://github.com/ether/etherpad-lite/issues/2802");
throw new Error(`setAuthorInfo: author (${author}) is not a string`); throw new Error(`setAuthorInfo: author (${author}) is not a string`);