diff --git a/apps/client/src/share.ts b/apps/client/src/share.ts index 602f7e95c..579b9e422 100644 --- a/apps/client/src/share.ts +++ b/apps/client/src/share.ts @@ -9,8 +9,6 @@ async function ensureJQuery() { (window as any).$ = $; } - - async function formatCodeBlocks() { const anyCodeBlock = document.querySelector("#content pre"); if (!anyCodeBlock) { @@ -24,49 +22,3 @@ async function formatCodeBlocks() { async function setupTextNote() { formatCodeBlocks(); } - -/** - * Fetch note with given ID from backend - * - * @param noteId of the given note to be fetched. If false, fetches current note. - */ -async function fetchNote(noteId: string | null = null) { - if (!noteId) { - noteId = document.body.getAttribute("data-note-id"); - } - - const resp = await fetch(`api/notes/${noteId}`); - - return await resp.json(); -} - -document.addEventListener( - "DOMContentLoaded", - () => { - const noteType = determineNoteType(); - - if (noteType === "text") { - setupTextNote(); - } - - const toggleMenuButton = document.getElementById("toggleMenuButton"); - const layout = document.getElementById("layout"); - - if (toggleMenuButton && layout) { - toggleMenuButton.addEventListener("click", () => layout.classList.toggle("showMenu")); - } - }, - false -); - -function determineNoteType() { - const bodyClass = document.body.className; - const match = bodyClass.match(/type-([^\s]+)/); - return match ? match[1] : null; -} - -// workaround to prevent webpack from removing "fetchNote" as dead code: -// add fetchNote as property to the window object -Object.defineProperty(window, "fetchNote", { - value: fetchNote -}); diff --git a/packages/share-theme/src/scripts/index.ts b/packages/share-theme/src/scripts/index.ts index fbbc864d0..ea08330b0 100644 --- a/packages/share-theme/src/scripts/index.ts +++ b/packages/share-theme/src/scripts/index.ts @@ -5,6 +5,7 @@ import setupSearch from "./modules/search"; import setupThemeSelector from "./modules/theme"; import setupMermaid from "./modules/mermaid"; import setupMath from "./modules/math"; +import api from "./modules/api"; function $try unknown>(func: T, ...args: Parameters) { try { @@ -15,10 +16,39 @@ function $try unknown>(func: T, ...args: Paramete } } +Object.assign(window, api); $try(setupThemeSelector); $try(setupToC); $try(setupExpanders); $try(setupMobileMenu); $try(setupSearch); -$try(setupMermaid); -$try(setupMath); + +function setupTextNote() { + $try(setupMermaid); + $try(setupMath); +} + +document.addEventListener( + "DOMContentLoaded", + () => { + const noteType = determineNoteType(); + + if (noteType === "text") { + setupTextNote(); + } + + const toggleMenuButton = document.getElementById("toggleMenuButton"); + const layout = document.getElementById("layout"); + + if (toggleMenuButton && layout) { + toggleMenuButton.addEventListener("click", () => layout.classList.toggle("showMenu")); + } + }, + false +); + +function determineNoteType() { + const bodyClass = document.body.className; + const match = bodyClass.match(/type-([^\s]+)/); + return match ? match[1] : null; +} diff --git a/packages/share-theme/src/scripts/modules/api.ts b/packages/share-theme/src/scripts/modules/api.ts new file mode 100644 index 000000000..adaca77d1 --- /dev/null +++ b/packages/share-theme/src/scripts/modules/api.ts @@ -0,0 +1,18 @@ +/** + * Fetch note with given ID from backend + * + * @param noteId of the given note to be fetched. If false, fetches current note. + */ +async function fetchNote(noteId: string | null = null) { + if (!noteId) { + noteId = document.body.getAttribute("data-note-id"); + } + + const resp = await fetch(`api/notes/${noteId}`); + + return await resp.json(); +} + +export default { + fetchNote +};