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

95 lines
2.2 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
]
};
2019-06-09 17:50:11 +08:00
const LINK_MAP = {
js: [
"libraries/jsplumb.js",
"libraries/panzoom.js",
"libraries/springy.js"
],
css: [
2019-06-10 03:48:30 +08:00
"stylesheets/link_map.css"
2019-06-09 17:50:11 +08:00
]
};
2019-06-04 04:55:59 +08:00
2019-06-30 04:57:47 +08:00
const PRINT_THIS = {js: ["libraries/printThis.js"]};
2019-08-21 05:30:19 +08:00
const SORTABLE = {js: ["libraries/sortable.min.js"]};
const KNOCKOUT = {js: ["libraries/knockout.min.js"]};
2019-09-08 22:06:42 +08:00
const CALENDAR_WIDGET = {css: ["stylesheets/calendar.css"]};
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,
2019-06-04 04:55:59 +08:00
RELATION_MAP,
2019-06-30 04:57:47 +08:00
LINK_MAP,
2019-08-21 05:30:19 +08:00
PRINT_THIS,
SORTABLE,
2019-09-08 22:06:42 +08:00
KNOCKOUT,
CALENDAR_WIDGET
2018-03-28 10:42:46 +08:00
}