Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
chore(share): bring back most of the logic
Elian Doran21b20cf5750d
diff --git a/apps/client/src/share.ts b/apps/client/src/share.tsindex 602f7e95c6..579b9e422f 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.tsindex fbbc864d06..ea08330b07 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<T extends (...a: unknown[]) => unknown>(func: T, ...args: Parameters<T>) {try {@@ -15,10 +16,39 @@ function $try<T extends (...a: unknown[]) => 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.tsnew file mode 100644index 0000000000..adaca77d14--- /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+};