mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-08-13 04:13:19 +08:00
refactor(client): remove library loader entirely
This commit is contained in:
parent
7702a87640
commit
8a1f05cd26
@ -1,7 +1,6 @@
|
|||||||
import utils from "./utils.js";
|
import utils from "./utils.js";
|
||||||
import appContext from "../components/app_context.js";
|
import appContext from "../components/app_context.js";
|
||||||
import server from "./server.js";
|
import server from "./server.js";
|
||||||
import libraryLoader from "./library_loader.js";
|
|
||||||
import ws from "./ws.js";
|
import ws from "./ws.js";
|
||||||
import froca from "./froca.js";
|
import froca from "./froca.js";
|
||||||
import linkService from "./link.js";
|
import linkService from "./link.js";
|
||||||
@ -17,7 +16,6 @@ function setupGlobs() {
|
|||||||
|
|
||||||
// required for ESLint plugin and CKEditor
|
// required for ESLint plugin and CKEditor
|
||||||
window.glob.getActiveContextNote = () => appContext.tabManager.getActiveContextNote();
|
window.glob.getActiveContextNote = () => appContext.tabManager.getActiveContextNote();
|
||||||
window.glob.requireLibrary = libraryLoader.requireLibrary;
|
|
||||||
window.glob.appContext = appContext; // for debugging
|
window.glob.appContext = appContext; // for debugging
|
||||||
window.glob.froca = froca;
|
window.glob.froca = froca;
|
||||||
window.glob.treeCache = froca; // compatibility for CKEditor builds for a while
|
window.glob.treeCache = froca; // compatibility for CKEditor builds for a while
|
||||||
|
@ -1,67 +0,0 @@
|
|||||||
export interface Library {
|
|
||||||
js?: string[] | (() => string[]);
|
|
||||||
css?: string[];
|
|
||||||
}
|
|
||||||
|
|
||||||
const KATEX: Library = {
|
|
||||||
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"]
|
|
||||||
};
|
|
||||||
|
|
||||||
async function requireLibrary(library: Library) {
|
|
||||||
if (library.css) {
|
|
||||||
library.css.map((cssUrl) => requireCss(cssUrl));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (library.js) {
|
|
||||||
for (const scriptUrl of await unwrapValue(library.js)) {
|
|
||||||
await requireScript(scriptUrl);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function unwrapValue<T>(value: T | (() => T) | Promise<T>) {
|
|
||||||
if (value && typeof value === "object" && "then" in value) {
|
|
||||||
return (await (value as Promise<() => T>))();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof value === "function") {
|
|
||||||
return (value as () => T)();
|
|
||||||
}
|
|
||||||
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
// we save the promises in case of the same script being required concurrently multiple times
|
|
||||||
const loadedScriptPromises: Record<string, JQuery.jqXHR> = {};
|
|
||||||
|
|
||||||
async function requireScript(url: string) {
|
|
||||||
url = `${window.glob.assetPath}/${url}`;
|
|
||||||
|
|
||||||
if (!loadedScriptPromises[url]) {
|
|
||||||
loadedScriptPromises[url] = $.ajax({
|
|
||||||
url: url,
|
|
||||||
dataType: "script",
|
|
||||||
cache: true
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
await loadedScriptPromises[url];
|
|
||||||
}
|
|
||||||
|
|
||||||
async function requireCss(url: string, prependAssetPath = true) {
|
|
||||||
const cssLinks = Array.from(document.querySelectorAll("link")).map((el) => el.href);
|
|
||||||
|
|
||||||
if (!cssLinks.some((l) => l.endsWith(url))) {
|
|
||||||
if (prependAssetPath) {
|
|
||||||
url = `${window.glob.assetPath}/${url}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
$("head").append($('<link rel="stylesheet" type="text/css" />').attr("href", url));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export default {
|
|
||||||
requireCss,
|
|
||||||
requireLibrary
|
|
||||||
};
|
|
1
apps/client/src/types.d.ts
vendored
1
apps/client/src/types.d.ts
vendored
@ -22,7 +22,6 @@ interface CustomGlobals {
|
|||||||
getReferenceLinkTitle: (href: string) => Promise<string>;
|
getReferenceLinkTitle: (href: string) => Promise<string>;
|
||||||
getReferenceLinkTitleSync: (href: string) => string;
|
getReferenceLinkTitleSync: (href: string) => string;
|
||||||
getActiveContextNote: () => FNote | null;
|
getActiveContextNote: () => FNote | null;
|
||||||
requireLibrary: typeof library_loader.requireLibrary;
|
|
||||||
ESLINT: Library;
|
ESLINT: Library;
|
||||||
appContext: AppContext;
|
appContext: AppContext;
|
||||||
froca: Froca;
|
froca: Froca;
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
import { t } from "../../services/i18n.js";
|
import { t } from "../../services/i18n.js";
|
||||||
import libraryLoader from "../../services/library_loader.js";
|
|
||||||
import noteAutocompleteService, { type Suggestion } from "../../services/note_autocomplete.js";
|
import noteAutocompleteService, { type Suggestion } from "../../services/note_autocomplete.js";
|
||||||
import mimeTypesService from "../../services/mime_types.js";
|
import mimeTypesService from "../../services/mime_types.js";
|
||||||
import utils, { hasTouchBar } from "../../services/utils.js";
|
import utils, { hasTouchBar } from "../../services/utils.js";
|
||||||
|
Loading…
x
Reference in New Issue
Block a user