ui: add provideFeature/getFeature API

This commit is contained in:
undefined committed 2023-04-11 14:51:26 +08:00
1 parent e76e1c9beb
commit c482c5fef8
5 files changed
+34 -21

No files matched your search

@@ -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, [], {