Replace forum and admin global compat exports with a Proxy to allow namespace use (#2488)

This commit is contained in:
David Sevilla Martín 2020-12-07 13:25:24 -05:00 committed by GitHub
parent 6d96587205
commit b6c4aae067
4 changed files with 20 additions and 6 deletions

View File

@ -10,8 +10,9 @@ export { app };
// Export public API
// Export compat API
import compat from './compat';
import compatObj from './compat';
import proxifyCompat from '../common/utils/proxifyCompat';
compat.app = app;
compatObj.app = app;
export { compat };
export const compat = proxifyCompat(compatObj, 'admin');

View File

@ -21,6 +21,7 @@ import stringToColor from './utils/stringToColor';
import subclassOf from './utils/subclassOf';
import SuperTextarea from './utils/SuperTextarea';
import patchMithril from './utils/patchMithril';
import proxifyCompat from './utils/proxifyCompat';
import classList from './utils/classList';
import extractText from './utils/extractText';
import formatNumber from './utils/formatNumber';
@ -94,6 +95,7 @@ export default {
'utils/SuperTextarea': SuperTextarea,
'utils/setRouteWithForcedRefresh': setRouteWithForcedRefresh,
'utils/patchMithril': patchMithril,
'utils/proxifyCompat': proxifyCompat,
'utils/classList': classList,
'utils/extractText': extractText,
'utils/formatNumber': formatNumber,

View File

@ -0,0 +1,10 @@
export default (compat: { [key: string]: any }, namespace: string) => {
// regex to replace common/ and NAMESPACE/ for core & core extensions
// e.g. admin/utils/extract --> utils/extract
// e.g. tags/common/utils/sortTags --> tags/utils/sortTags
const regex = new RegExp(`(\\w+\\/)?(${namespace}|common)\\/`);
return new Proxy(compat, {
get: (obj, prop: string) => obj[prop] || obj[prop.replace(regex, '$1')],
});
};

View File

@ -15,8 +15,9 @@ export { app };
// export { IndexPage, DicsussionList } from './components';
// Export compat API
import compat from './compat';
import compatObj from './compat';
import proxifyCompat from '../common/utils/proxifyCompat';
compat.app = app;
compatObj.app = app;
export { compat };
export const compat = proxifyCompat(compatObj, 'forum');