new functions and fixes
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright (C) 2026 Mateusz Gruszczyński @linuxiarz.pl
|
||||
* Source-Available Code / Dual-Licensed.
|
||||
*/
|
||||
|
||||
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(`${globalName} did not register a browser global.`));
|
||||
}, { once: true });
|
||||
script.addEventListener("error", () => reject(new Error(`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")));
|
||||
}
|
||||
Reference in New Issue
Block a user