From 4804704da60fa3902faa9fd1743387f859da81eb Mon Sep 17 00:00:00 2001 From: partouf Date: Fri, 8 Apr 2022 04:04:16 +0200 Subject: [PATCH] sketch of fix --- static/lib-utils.ts | 43 +++++++++++++++++++++++++++++------- static/options.interfaces.ts | 2 ++ static/panes/compiler.js | 8 +++++-- 3 files changed, 43 insertions(+), 10 deletions(-) diff --git a/static/lib-utils.ts b/static/lib-utils.ts index 0141d9ad7..c64807a15 100644 --- a/static/lib-utils.ts +++ b/static/lib-utils.ts @@ -23,11 +23,28 @@ // POSSIBILITY OF SUCH DAMAGE. import { options } from './options'; -import {LanguageLibs, Library} from './options.interfaces'; +import { LanguageLibs, Library, Libs, LibsPerRemote } from './options.interfaces'; const LIB_MATCH_RE = /([\w-]*)\.([\w-]*)/i; -export function copyAndFilterLibraries(allLibraries: LanguageLibs, filter: string[]) { +const remoteLibs: LibsPerRemote = {}; + +function getRemoteId(remoteUrl: string, language: string): string { + const url: URL = new URL(remoteUrl); + return url.host.replace(/\./g, '_') + '_' + language; +} + +function getRemoteLibraries(language: string, remoteUrl: string): Libs { + const remoteId = getRemoteId(remoteUrl, language); + if (!remoteLibs[remoteId]) { + $.get(remoteUrl + '/api/libraries/' + language, (data) => { + remoteLibs[remoteId] = data; + }); + } + return remoteLibs[remoteId]; +} + +function copyAndFilterLibraries(allLibraries: LanguageLibs, filter: string[]) { const filterLibAndVersion = filter.map(lib => { const match = lib.match(LIB_MATCH_RE); return { @@ -41,7 +58,7 @@ export function copyAndFilterLibraries(allLibraries: LanguageLibs, filter: strin const copiedLibraries: Record = {}; for (const libid in allLibraries) { if (!filterLibIds.has(libid)) continue; - const lib = {...allLibraries[libid]}; + const lib = { ...allLibraries[libid] }; for (const versionid in lib.versions) { for (const filter of filterLibAndVersion) { if (!(!filter.version || filter.version === versionid)) { @@ -55,10 +72,20 @@ export function copyAndFilterLibraries(allLibraries: LanguageLibs, filter: strin return copiedLibraries; } -export function getSupportedLibraries(supportedLibrariesArr: string[], langId: string) { - const allLibs = options.libs[langId]; - if (supportedLibrariesArr && supportedLibrariesArr.length > 0) { - return copyAndFilterLibraries(allLibs, supportedLibrariesArr); +export function getSupportedLibraries(supportedLibrariesArr: string[], langId: string, + remoteUrl: string): LanguageLibs { + if (!remoteUrl) { + const allLibs = options.libs[langId]; + if (supportedLibrariesArr && supportedLibrariesArr.length > 0) { + return copyAndFilterLibraries(allLibs, supportedLibrariesArr); + } + return allLibs; + } else { + const allRemotes = getRemoteLibraries(langId, remoteUrl); + const allLibs = allRemotes[langId]; + if (supportedLibrariesArr && supportedLibrariesArr.length > 0) { + return copyAndFilterLibraries(allLibs, supportedLibrariesArr); + } + return allLibs; } - return allLibs; } diff --git a/static/options.interfaces.ts b/static/options.interfaces.ts index 1db7deaef..3028e34f9 100644 --- a/static/options.interfaces.ts +++ b/static/options.interfaces.ts @@ -45,6 +45,8 @@ export type LanguageLibs = Record; export type Libs = Record; +export type LibsPerRemote = Record; + export interface Options { libs: Libs; languages: Record; diff --git a/static/panes/compiler.js b/static/panes/compiler.js index 64798747b..cc4266169 100644 --- a/static/panes/compiler.js +++ b/static/panes/compiler.js @@ -1634,7 +1634,9 @@ Compiler.prototype.initLibraries = function (state) { this.libsButton, state, _.bind(this.onLibsChanged, this), - LibUtils.getSupportedLibraries(this.compiler ? this.compiler.libsArr: [], this.currentLangId) + LibUtils.getSupportedLibraries(this.compiler ? this.compiler.libsArr: [], + this.currentLangId, + this.compiler ? this.compiler.remote : '') ); }; @@ -1642,7 +1644,9 @@ Compiler.prototype.updateLibraries = function () { if (this.libsWidget) { var filteredLibraries = {}; if (this.compiler) { - filteredLibraries = LibUtils.getSupportedLibraries(this.compiler.libsArr, this.currentLangId); + filteredLibraries = LibUtils.getSupportedLibraries(this.compiler.libsArr, + this.currentLangId, + this.compiler ? this.compiler.remote : ''); } this.libsWidget.setNewLangId(this.currentLangId,