2024-10-27 10:39:31 +02:00
|
|
|
import mimeTypesService from "./mime_types.js";
|
2024-10-27 17:41:37 +02:00
|
|
|
import optionsService from "./options.js";
|
2024-11-02 16:40:33 +02:00
|
|
|
import { getStylesheetUrl } from "./syntax_highlight.js";
|
2024-10-27 10:39:31 +02:00
|
|
|
|
2024-12-21 19:26:12 +02:00
|
|
|
export interface Library {
|
2024-12-21 18:00:36 +02:00
|
|
|
js?: string[] | (() => string[]);
|
|
|
|
css?: string[];
|
|
|
|
}
|
|
|
|
|
|
|
|
const CKEDITOR: Library = {
|
|
|
|
js: ["libraries/ckeditor/ckeditor.js"]
|
|
|
|
};
|
|
|
|
|
|
|
|
const CODE_MIRROR: Library = {
|
2024-12-23 22:42:18 +02:00
|
|
|
js: () => {
|
|
|
|
const scriptsToLoad = [
|
|
|
|
"node_modules/codemirror/lib/codemirror.js",
|
|
|
|
"node_modules/codemirror/addon/display/placeholder.js",
|
|
|
|
"node_modules/codemirror/addon/edit/matchbrackets.js",
|
|
|
|
"node_modules/codemirror/addon/edit/matchtags.js",
|
|
|
|
"node_modules/codemirror/addon/fold/xml-fold.js",
|
|
|
|
"node_modules/codemirror/addon/lint/lint.js",
|
|
|
|
"node_modules/codemirror/addon/mode/loadmode.js",
|
|
|
|
"node_modules/codemirror/addon/mode/multiplex.js",
|
|
|
|
"node_modules/codemirror/addon/mode/overlay.js",
|
|
|
|
"node_modules/codemirror/addon/mode/simple.js",
|
|
|
|
"node_modules/codemirror/addon/search/match-highlighter.js",
|
|
|
|
"node_modules/codemirror/mode/meta.js",
|
2025-01-11 12:18:16 +02:00
|
|
|
"node_modules/codemirror/keymap/vim.js",
|
|
|
|
"libraries/codemirror/eslint.js"
|
2024-12-23 22:42:18 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
const mimeTypes = mimeTypesService.getMimeTypes();
|
|
|
|
for (const mimeType of mimeTypes) {
|
2025-02-12 22:49:47 +02:00
|
|
|
if (mimeType.enabled && mimeType.codeMirrorSource) {
|
2024-12-23 22:42:18 +02:00
|
|
|
scriptsToLoad.push(mimeType.codeMirrorSource);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return scriptsToLoad;
|
|
|
|
},
|
2025-01-09 18:07:02 +02:00
|
|
|
css: ["node_modules/codemirror/lib/codemirror.css", "node_modules/codemirror/addon/lint/lint.css"]
|
2018-03-27 22:42:46 -04:00
|
|
|
};
|
|
|
|
|
2024-12-21 18:00:36 +02:00
|
|
|
const ESLINT: Library = {
|
2025-01-11 12:18:16 +02:00
|
|
|
js: ["libraries/eslint/eslint.js"]
|
2024-12-21 18:00:36 +02:00
|
|
|
};
|
2018-03-27 22:42:46 -04:00
|
|
|
|
2024-12-21 18:00:36 +02:00
|
|
|
const RELATION_MAP: Library = {
|
2025-01-09 18:07:02 +02:00
|
|
|
js: ["node_modules/jsplumb/dist/js/jsplumb.min.js", "node_modules/panzoom/dist/panzoom.min.js"],
|
|
|
|
css: ["stylesheets/relation_map.css"]
|
2018-10-18 11:46:07 +02:00
|
|
|
};
|
|
|
|
|
2024-12-21 18:00:36 +02:00
|
|
|
const CALENDAR_WIDGET: Library = {
|
|
|
|
css: ["stylesheets/calendar.css"]
|
|
|
|
};
|
2019-09-08 16:06:42 +02:00
|
|
|
|
2024-12-21 18:00:36 +02:00
|
|
|
const KATEX: Library = {
|
2025-01-09 18:07:02 +02:00
|
|
|
js: ["node_modules/katex/dist/katex.min.js", "node_modules/katex/dist/contrib/mhchem.min.js", "node_modules/katex/dist/contrib/auto-render.min.js"],
|
|
|
|
css: ["node_modules/katex/dist/katex.min.css"]
|
2020-09-21 22:57:22 +02:00
|
|
|
};
|
|
|
|
|
2024-12-21 18:00:36 +02:00
|
|
|
const WHEEL_ZOOM: Library = {
|
2025-01-09 18:07:02 +02:00
|
|
|
js: ["node_modules/vanilla-js-wheel-zoom/dist/wheel-zoom.min.js"]
|
2021-03-15 22:24:56 +01:00
|
|
|
};
|
|
|
|
|
2024-12-21 18:00:36 +02:00
|
|
|
const MERMAID: Library = {
|
2025-01-09 18:07:02 +02:00
|
|
|
js: ["node_modules/mermaid/dist/mermaid.min.js"]
|
|
|
|
};
|
2024-11-25 21:58:56 +02:00
|
|
|
|
2024-12-21 18:00:36 +02:00
|
|
|
const MARKJS: Library = {
|
2025-01-09 18:07:02 +02:00
|
|
|
js: ["node_modules/mark.js/dist/jquery.mark.es6.min.js"]
|
2021-11-25 13:45:58 +01:00
|
|
|
};
|
|
|
|
|
2024-12-21 18:00:36 +02:00
|
|
|
const HIGHLIGHT_JS: Library = {
|
2024-10-27 10:39:31 +02:00
|
|
|
js: () => {
|
|
|
|
const mimeTypes = mimeTypesService.getMimeTypes();
|
2024-12-21 18:00:36 +02:00
|
|
|
const scriptsToLoad = new Set<string>();
|
2024-10-27 10:48:50 +02:00
|
|
|
scriptsToLoad.add("node_modules/@highlightjs/cdn-assets/highlight.min.js");
|
2024-10-27 10:39:31 +02:00
|
|
|
for (const mimeType of mimeTypes) {
|
2024-12-14 01:07:53 +02:00
|
|
|
const id = mimeType.highlightJs;
|
|
|
|
if (!mimeType.enabled || !id) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mimeType.highlightJsSource === "libraries") {
|
|
|
|
scriptsToLoad.add(`libraries/highlightjs/${id}.js`);
|
|
|
|
} else {
|
|
|
|
// Built-in module.
|
|
|
|
scriptsToLoad.add(`node_modules/@highlightjs/cdn-assets/languages/${id}.min.js`);
|
2024-10-27 10:39:31 +02:00
|
|
|
}
|
2024-10-27 17:41:37 +02:00
|
|
|
}
|
2024-12-23 22:42:18 +02:00
|
|
|
|
2024-12-21 18:00:36 +02:00
|
|
|
const currentTheme = String(optionsService.get("codeBlockTheme"));
|
2024-10-27 17:41:37 +02:00
|
|
|
loadHighlightingTheme(currentTheme);
|
|
|
|
|
2024-10-27 10:48:50 +02:00
|
|
|
return Array.from(scriptsToLoad);
|
2024-10-27 17:41:37 +02:00
|
|
|
}
|
2024-10-26 22:57:07 +03:00
|
|
|
};
|
|
|
|
|
2025-01-20 19:18:29 +02:00
|
|
|
const LEAFLET: Library = {
|
2025-03-02 20:47:57 +01:00
|
|
|
css: ["node_modules/leaflet/dist/leaflet.css"]
|
|
|
|
};
|
2025-01-20 19:18:29 +02:00
|
|
|
|
2024-12-21 18:00:36 +02:00
|
|
|
async function requireLibrary(library: Library) {
|
2018-03-27 22:42:46 -04:00
|
|
|
if (library.css) {
|
2025-01-09 18:07:02 +02:00
|
|
|
library.css.map((cssUrl) => requireCss(cssUrl));
|
2018-03-27 22:42:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (library.js) {
|
2024-10-27 10:39:31 +02:00
|
|
|
for (const scriptUrl of unwrapValue(library.js)) {
|
2018-03-27 22:42:46 -04:00
|
|
|
await requireScript(scriptUrl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-21 18:00:36 +02:00
|
|
|
function unwrapValue<T>(value: T | (() => T)) {
|
2024-10-27 10:39:31 +02:00
|
|
|
if (typeof value === "function") {
|
2024-12-21 18:00:36 +02:00
|
|
|
return (value as () => T)();
|
2024-10-27 10:39:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2018-04-07 15:56:46 -04:00
|
|
|
// we save the promises in case of the same script being required concurrently multiple times
|
2024-12-21 18:00:36 +02:00
|
|
|
const loadedScriptPromises: Record<string, JQuery.jqXHR> = {};
|
2018-03-27 22:42:46 -04:00
|
|
|
|
2024-12-21 18:00:36 +02:00
|
|
|
async function requireScript(url: string) {
|
2022-12-21 15:19:05 +01:00
|
|
|
url = `${window.glob.assetPath}/${url}`;
|
2022-10-26 23:50:54 +02:00
|
|
|
|
2018-04-07 15:56:46 -04:00
|
|
|
if (!loadedScriptPromises[url]) {
|
|
|
|
loadedScriptPromises[url] = $.ajax({
|
2018-03-27 22:42:46 -04:00
|
|
|
url: url,
|
|
|
|
dataType: "script",
|
|
|
|
cache: true
|
2018-04-07 15:56:46 -04:00
|
|
|
});
|
2018-03-27 22:42:46 -04:00
|
|
|
}
|
2018-04-07 15:56:46 -04:00
|
|
|
|
|
|
|
await loadedScriptPromises[url];
|
2018-03-27 22:42:46 -04:00
|
|
|
}
|
|
|
|
|
2024-12-21 18:00:36 +02:00
|
|
|
async function requireCss(url: string, prependAssetPath = true) {
|
2025-01-09 18:07:02 +02:00
|
|
|
const cssLinks = Array.from(document.querySelectorAll("link")).map((el) => el.href);
|
2020-02-17 19:42:52 +01:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
if (!cssLinks.some((l) => l.endsWith(url))) {
|
2022-10-26 23:50:54 +02:00
|
|
|
if (prependAssetPath) {
|
2022-12-21 15:19:05 +01:00
|
|
|
url = `${window.glob.assetPath}/${url}`;
|
2022-10-26 23:50:54 +02:00
|
|
|
}
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
$("head").append($('<link rel="stylesheet" type="text/css" />').attr("href", url));
|
2020-02-17 19:42:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-21 18:00:36 +02:00
|
|
|
let highlightingThemeEl: JQuery<HTMLElement> | null = null;
|
|
|
|
function loadHighlightingTheme(theme: string) {
|
2024-12-23 22:42:18 +02:00
|
|
|
if (!theme) {
|
2024-10-31 21:45:06 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (theme === "none") {
|
|
|
|
// Deactivate the theme.
|
|
|
|
if (highlightingThemeEl) {
|
|
|
|
highlightingThemeEl.remove();
|
|
|
|
highlightingThemeEl = null;
|
|
|
|
}
|
2024-10-27 21:39:50 +02:00
|
|
|
return;
|
|
|
|
}
|
2024-12-23 22:42:18 +02:00
|
|
|
|
2024-10-27 17:41:37 +02:00
|
|
|
if (!highlightingThemeEl) {
|
|
|
|
highlightingThemeEl = $(`<link rel="stylesheet" type="text/css" />`);
|
|
|
|
$("head").append(highlightingThemeEl);
|
|
|
|
}
|
2024-12-23 22:42:18 +02:00
|
|
|
|
2024-11-02 16:40:33 +02:00
|
|
|
const url = getStylesheetUrl(theme);
|
2024-10-27 17:41:37 +02:00
|
|
|
if (url) {
|
|
|
|
highlightingThemeEl.attr("href", url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-27 22:42:46 -04:00
|
|
|
export default {
|
2020-02-17 19:42:52 +01:00
|
|
|
requireCss,
|
2018-03-27 22:42:46 -04:00
|
|
|
requireLibrary,
|
2024-10-27 20:08:12 +02:00
|
|
|
loadHighlightingTheme,
|
2018-03-27 22:42:46 -04:00
|
|
|
CKEDITOR,
|
|
|
|
CODE_MIRROR,
|
2018-09-02 23:02:01 +02:00
|
|
|
ESLINT,
|
2019-06-03 22:55:59 +02:00
|
|
|
RELATION_MAP,
|
2020-09-21 22:57:22 +02:00
|
|
|
CALENDAR_WIDGET,
|
2021-03-15 22:24:56 +01:00
|
|
|
KATEX,
|
2021-05-29 22:52:32 +02:00
|
|
|
WHEEL_ZOOM,
|
2021-11-25 13:45:58 +01:00
|
|
|
MERMAID,
|
2024-07-20 09:32:56 +03:00
|
|
|
MARKJS,
|
2025-01-20 19:18:29 +02:00
|
|
|
HIGHLIGHT_JS,
|
|
|
|
LEAFLET
|
2025-01-09 18:07:02 +02:00
|
|
|
};
|