Pad: Deprecate use of global variables

This commit is contained in:
Richard Hansen 2021-08-14 00:06:32 -04:00
parent 2b53c69749
commit b325a145d6

View file

@ -486,11 +486,24 @@
window._postPluginUpdateForTestingDone = true; window._postPluginUpdateForTestingDone = true;
$(() => hooks.aCallAll('documentReady')); $(() => hooks.aCallAll('documentReady'));
// TODO: These globals shouldn't exist. // TODO: Delete these global variables once plugins have been updated to not use them.
window.pad = require('ep_etherpad-lite/static/js/pad').pad; const globals = {
window.chat = require('ep_etherpad-lite/static/js/chat').chat; pad: require('ep_etherpad-lite/static/js/pad').pad,
window.padeditbar = require('ep_etherpad-lite/static/js/pad_editbar').padeditbar; chat: require('ep_etherpad-lite/static/js/chat').chat,
window.padimpexp = require('ep_etherpad-lite/static/js/pad_impexp').padimpexp; padeditbar: require('ep_etherpad-lite/static/js/pad_editbar').padeditbar,
padimpexp: require('ep_etherpad-lite/static/js/pad_impexp').padimpexp,
};
// Wrap each of the above globals in a Proxy object that triggers an unhandled Promise
// rejection if the global is used (but otherwise forwards the access so that everything
// continues to work, just with an ugly error popup).
const proxyOp = (op, ...args) => {
Promise.reject(new Error('deprecated global variable accessed'));
return op(...args);
};
const proxyHandler = Object.fromEntries(
['get', 'set'].map((opName) => [opName, proxyOp.bind(null, Reflect[opName])]));
Object.assign(window, Object.fromEntries(
Object.entries(globals).map(([k, v]) => [k, new Proxy(v, proxyHandler)])));
require('ep_etherpad-lite/static/js/skin_variants'); require('ep_etherpad-lite/static/js/skin_variants');
const pad = require('ep_etherpad-lite/static/js/pad'); const pad = require('ep_etherpad-lite/static/js/pad');