ui: add provideFeature/getFeature API

pull/557/head
undefined 2 years ago
parent e76e1c9beb
commit c482c5fef8
No known key found for this signature in database

@ -18,6 +18,7 @@ export * from './misc/Page';
export { initPageLoader } from './hydro';
const lazyModules = {};
const features = {};
export default async function load(name: string) {
if (window.node_modules[name]) return window.node_modules[name];
if (name === 'echarts') return import('echarts');
@ -37,6 +38,15 @@ export default async function load(name: string) {
document.body.appendChild(tag);
return lazyModules[name];
}
export async function getFeatures(name: string) {
const legacy = Object.keys(window.externalModules).filter((i) => i === name || i.startsWith(`${name}@`));
const c = Object.keys(features).filter((i) => i === name || i.startsWith(`${name}@`));
return legacy.concat(c);
}
export function provideFeature(name: string, content: string) {
features[name] = content;
}
export interface EventMap { }

@ -1,6 +1,22 @@
import { loadExternalModule } from 'vj/utils';
import { load as loadModule } from '@hydrooj/ui-default';
let loaded;
const val: Record<string, any> = {};
/** @deprecated */
export async function legacyLoadExternalModule(target: string) {
if (val[target]) return val[target];
const ele = document.createElement('script');
ele.src = target;
await new Promise((resolve, reject) => {
ele.onload = resolve;
ele.onerror = reject;
document.head.appendChild(ele);
});
val[target] = window.exports;
return val[target];
}
const loaders = {
i18n: async () => {
const { setLocaleData } = await import('./nls');
@ -21,7 +37,9 @@ const loaders = {
external: async (monaco, feat) => {
const items = Object.keys(window.externalModules).filter((i) => i === `monaco-${feat}` || i.startsWith(`monaco-${feat}@`));
for (const item of items) {
let apply = await loadExternalModule(window.externalModules[item]);
let apply = (item.startsWith('http') || item.startsWith('/'))
? await legacyLoadExternalModule(window.externalModules[item])
: (await loadModule(item)).apply;
if (typeof apply !== 'function') apply = apply.default || apply.apply;
if (typeof apply === 'function') await apply(monaco);
}

@ -4,9 +4,9 @@ export default class Sock {
sock: ReconnectingWebSocket;
interval: NodeJS.Timer;
constructor(public url: string) {
constructor(public url: string, nocookie = false) {
const i = new URL(url, window.location.href);
if (i.host !== window.location.host) i.searchParams.append('sid', document.cookie.split('sid=')[1].split(';')[0]);
if (i.host !== window.location.host && !nocookie) i.searchParams.append('sid', document.cookie.split('sid=')[1].split(';')[0]);
i.protocol = i.protocol.replace('http', 'ws');
this.url = i.toString();
this.sock = new ReconnectingWebSocket(this.url, [], {

@ -11,7 +11,8 @@ declare global {
UserContext: any;
UiContext: any;
Hydro: any;
externalModules: Record<string, string | (() => Promise<any>)>;
/** @deprecated */
externalModules: Record<string, string>;
captureException?: (e: Error) => void;
}
}

@ -77,21 +77,6 @@ export function mongoId(idstring: string) {
};
}
const loaded = {};
export async function loadExternalModule(target: string) {
if (loaded[target]) return loaded[target];
const ele = document.createElement('script');
ele.src = target;
await new Promise((resolve, reject) => {
ele.onload = resolve;
ele.onerror = reject;
document.head.appendChild(ele);
});
loaded[target] = window.exports;
return loaded[target];
}
export function emulateAnchorClick(ev: KeyboardEvent, targetUrl: string, alwaysOpenInNewWindow = false) {
let openInNewWindow;
if (alwaysOpenInNewWindow) openInNewWindow = true;
@ -112,6 +97,5 @@ Object.assign(window.Hydro.utils, {
zip,
pipeStream,
mongoId,
loadExternalModule,
emulateAnchorClick,
});

Loading…
Cancel
Save