Files
rustpad/static/js/vendor-libs.js
T
2026-09-07 15:01:58 +02:00

60 lines
2.1 KiB
JavaScript

/*
* Copyright (C) 2026 Mateusz Gruszczynski @linuxiarz.pl
* Source-Available Code / Dual-Licensed.
*
* Free for non-commercial and evaluation use under terms of BSL/GPLv3.
* Commercial or production use requires a valid paid license.
* See LICENSE file in repository root for details.
*/
import { t } from "@rustpad/i18n";
const config = window.__RUSTPAD_CONFIG__ || {};
const assetVersion = encodeURIComponent(String(config.assetVersion || "dev"));
const promiseCache = new Map();
function localAsset(path) {
const separator = path.includes("?") ? "&" : "?";
return `${path}${separator}v=${assetVersion}`;
}
function once(key, factory) {
if (!promiseCache.has(key)) promiseCache.set(key, Promise.resolve().then(factory));
return promiseCache.get(key);
}
function loadClassicScript(path, globalName) {
return once(path, () => new Promise((resolve, reject) => {
const existing = document.querySelector(`script[data-rustpad-lib="${globalName}"]`);
if (existing && window[globalName]) {
resolve(window[globalName]);
return;
}
const script = existing || document.createElement("script");
script.src = localAsset(path);
script.async = true;
script.dataset.rustpadLib = globalName;
script.addEventListener("load", () => {
if (window[globalName]) resolve(window[globalName]);
else reject(new Error(t("vendor.globalMissing", { name: globalName }, `${globalName} did not register a browser global.`)));
}, { once: true });
script.addEventListener("error", () => reject(new Error(t("vendor.loadFailed", { path }, `Failed to load ${path}.`))), { once: true });
if (!existing) document.head.append(script);
}));
}
export function loadHighlight() {
return loadClassicScript("/assets/libs/highlight/highlight.min.js", "hljs");
}
export function loadMermaid() {
return once("mermaid", async () => {
const module = await import(localAsset("/assets/libs/mermaid/mermaid.esm.min.mjs"));
return module.default;
});
}
export function loadMediaPlayer() {
return once("rustpad-player", () => import(localAsset("/assets/libs/rustpad-player/player.js")));
}