Notes/apps/client/src/share.ts

74 lines
2.0 KiB
TypeScript
Raw Normal View History

2025-05-17 10:03:37 +03:00
import "normalize.css";
2025-05-17 10:13:03 +03:00
import "@triliumnext/ckeditor5/content.css";
2025-06-09 14:13:35 +03:00
import "@triliumnext/share-theme/styles/index.css";
import "@triliumnext/share-theme/scripts/index.js";
async function loadIcons() {
if (document.getElementById("menu")) {
await import("boxicons/css/boxicons.min.css");
}
}
async function ensureJQuery() {
const $ = (await import("jquery")).default;
(window as any).$ = $;
}
2025-06-09 21:35:29 +03:00
async function applyMath() {
const anyMathBlock = document.querySelector("#content .math-tex");
if (!anyMathBlock) {
return;
}
const renderMathInElement = (await import("./services/math.js")).renderMathInElement;
renderMathInElement(document.getElementById("content"));
}
async function formatCodeBlocks() {
2025-06-09 21:35:29 +03:00
const anyCodeBlock = document.querySelector("#content pre");
if (!anyCodeBlock) {
return;
}
await ensureJQuery();
const { formatCodeBlocks } = await import("./services/syntax_highlight.js");
await formatCodeBlocks($("#content"));
}
/**
* Fetch note with given ID from backend
*
* @param noteId of the given note to be fetched. If false, fetches current note.
*/
2025-01-26 20:54:30 +01:00
async function fetchNote(noteId: string | null = null) {
if (!noteId) {
2022-01-01 22:32:38 +01:00
noteId = document.body.getAttribute("data-note-id");
}
2022-01-01 22:32:38 +01:00
const resp = await fetch(`api/notes/${noteId}`);
return await resp.json();
}
2025-01-09 18:07:02 +02:00
document.addEventListener(
"DOMContentLoaded",
() => {
formatCodeBlocks();
loadIcons();
2025-06-09 21:35:29 +03:00
applyMath();
2025-01-09 18:07:02 +02:00
const toggleMenuButton = document.getElementById("toggleMenuButton");
const layout = document.getElementById("layout");
2025-01-09 18:07:02 +02:00
if (toggleMenuButton && layout) {
toggleMenuButton.addEventListener("click", () => layout.classList.toggle("showMenu"));
}
},
false
);
// workaround to prevent webpack from removing "fetchNote" as dead code:
// add fetchNote as property to the window object
Object.defineProperty(window, "fetchNote", {
value: fetchNote
2025-03-02 20:47:57 +01:00
});