trilium/src/public/javascripts/services/library_loader.js

71 lines
1.7 KiB
JavaScript
Raw Normal View History

import cssLoader from './css_loader.js';
2018-03-28 10:42:46 +08:00
const CKEDITOR = {"js": ["libraries/ckeditor/ckeditor.js"]};
const CODE_MIRROR = {
js: [
"libraries/codemirror/codemirror.js",
"libraries/codemirror/addon/mode/loadmode.js",
"libraries/codemirror/addon/fold/xml-fold.js",
"libraries/codemirror/addon/edit/matchbrackets.js",
"libraries/codemirror/addon/edit/matchtags.js",
"libraries/codemirror/addon/search/match-highlighter.js",
"libraries/codemirror/mode/meta.js",
"libraries/codemirror/addon/lint/lint.js",
"libraries/codemirror/addon/lint/eslint.js"
],
css: [
"libraries/codemirror/codemirror.css",
"libraries/codemirror/addon/lint/lint.css"
]
};
const ESLINT = {js: ["libraries/eslint.js"]};
2018-09-03 05:02:01 +08:00
const COMMONMARK = {js: ["libraries/commonmark.min.js"]};
2018-10-18 17:46:07 +08:00
const RELATION_MAP = {
js: [
"libraries/jsplumb.js",
"libraries/panzoom.js"
],
css: [
"stylesheets/relation_map.css"
2018-10-18 17:46:07 +08:00
]
};
2018-03-28 10:42:46 +08:00
async function requireLibrary(library) {
if (library.css) {
library.css.map(cssUrl => cssLoader.requireCss(cssUrl));
2018-03-28 10:42:46 +08:00
}
if (library.js) {
for (const scriptUrl of library.js) {
await requireScript(scriptUrl);
}
}
}
2018-04-08 03:56:46 +08:00
// we save the promises in case of the same script being required concurrently multiple times
const loadedScriptPromises = {};
2018-03-28 10:42:46 +08:00
async function requireScript(url) {
2018-04-08 03:56:46 +08:00
if (!loadedScriptPromises[url]) {
loadedScriptPromises[url] = $.ajax({
2018-03-28 10:42:46 +08:00
url: url,
dataType: "script",
cache: true
2018-04-08 03:56:46 +08:00
});
2018-03-28 10:42:46 +08:00
}
2018-04-08 03:56:46 +08:00
await loadedScriptPromises[url];
2018-03-28 10:42:46 +08:00
}
export default {
requireLibrary,
CKEDITOR,
CODE_MIRROR,
2018-09-03 05:02:01 +08:00
ESLINT,
2018-10-18 17:46:07 +08:00
COMMONMARK,
RELATION_MAP
2018-03-28 10:42:46 +08:00
}