diff --git a/apps/client/src/print.ts b/apps/client/src/print.ts index b03df2c43..3fb238674 100644 --- a/apps/client/src/print.ts +++ b/apps/client/src/print.ts @@ -1 +1,19 @@ -console.log("Print script is here."); +import FNote from "./entities/fnote"; + +async function main() { + const noteId = window.location.pathname.split("/")[2]; + const froca = (await import("./services/froca")).default; + const note = await froca.getNote(noteId); + + if (!note) return; + + if (note.type === "book") { + handleCollection(note); + } +} + +function handleCollection(note: FNote) { + console.log("Rendering collection."); +} + +main(); diff --git a/apps/client/src/services/froca.ts b/apps/client/src/services/froca.ts index 6bbc3a50d..a1529db72 100644 --- a/apps/client/src/services/froca.ts +++ b/apps/client/src/services/froca.ts @@ -40,20 +40,23 @@ class FrocaImpl implements Froca { constructor() { this.initializedPromise = this.loadInitialTree(); + this.#clear(); } async loadInitialTree() { const resp = await server.get("tree"); // clear the cache only directly before adding new content which is important for e.g., switching to protected session + this.#clear(); + this.addResp(resp); + } + #clear() { this.notes = {}; this.branches = {}; this.attributes = {}; this.attachments = {}; this.blobPromises = {}; - - this.addResp(resp); } async loadSubTree(subTreeNoteId: string) { diff --git a/apps/client/src/services/protected_session.ts b/apps/client/src/services/protected_session.ts index 1e1984ae5..a2f04a9f0 100644 --- a/apps/client/src/services/protected_session.ts +++ b/apps/client/src/services/protected_session.ts @@ -70,26 +70,26 @@ async function setupProtectedSession(password: string) { protectedSessionHolder.enableProtectedSession(); } -ws.subscribeToMessages(async (message) => { - if (message.type === "protectedSessionLogin") { - await reloadData(); +// ws.subscribeToMessages(async (message) => { +// if (message.type === "protectedSessionLogin") { +// await reloadData(); - await appContext.triggerEvent("frocaReloaded", {}); +// await appContext.triggerEvent("frocaReloaded", {}); - appContext.triggerEvent("protectedSessionStarted", {}); +// appContext.triggerEvent("protectedSessionStarted", {}); - appContext.triggerCommand("closeProtectedSessionPasswordDialog"); +// appContext.triggerCommand("closeProtectedSessionPasswordDialog"); - if (protectedSessionDeferred !== null) { - protectedSessionDeferred.resolve(true); - protectedSessionDeferred = null; - } +// if (protectedSessionDeferred !== null) { +// protectedSessionDeferred.resolve(true); +// protectedSessionDeferred = null; +// } - toastService.showMessage(t("protected_session.started")); - } else if (message.type === "protectedSessionLogout") { - utils.reloadFrontendApp(`Protected session logout`); - } -}); +// toastService.showMessage(t("protected_session.started")); +// } else if (message.type === "protectedSessionLogout") { +// utils.reloadFrontendApp(`Protected session logout`); +// } +// }); async function protectNote(noteId: string, protect: boolean, includingSubtree: boolean) { await enterProtectedSession(); @@ -106,29 +106,29 @@ function makeToast(message: Message, title: string, text: string): ToastOptions }; } -ws.subscribeToMessages(async (message) => { - if (!("taskType" in message) || message.taskType !== "protectNotes") { - return; - } +// ws.subscribeToMessages(async (message) => { +// if (!("taskType" in message) || message.taskType !== "protectNotes") { +// return; +// } - const isProtecting = message.data?.protect; - const title = isProtecting ? t("protected_session.protecting-title") : t("protected_session.unprotecting-title"); +// const isProtecting = message.data?.protect; +// const title = isProtecting ? t("protected_session.protecting-title") : t("protected_session.unprotecting-title"); - if (message.type === "taskError") { - toastService.closePersistent(message.taskId); - toastService.showError(message.message); - } else if (message.type === "taskProgressCount") { - const count = message.progressCount; - const text = isProtecting ? t("protected_session.protecting-in-progress", { count }) : t("protected_session.unprotecting-in-progress-count", { count }); - toastService.showPersistent(makeToast(message, title, text)); - } else if (message.type === "taskSucceeded") { - const text = isProtecting ? t("protected_session.protecting-finished-successfully") : t("protected_session.unprotecting-finished-successfully"); - const toast = makeToast(message, title, text); - toast.closeAfter = 3000; +// if (message.type === "taskError") { +// toastService.closePersistent(message.taskId); +// toastService.showError(message.message); +// } else if (message.type === "taskProgressCount") { +// const count = message.progressCount; +// const text = isProtecting ? t("protected_session.protecting-in-progress", { count }) : t("protected_session.unprotecting-in-progress-count", { count }); +// toastService.showPersistent(makeToast(message, title, text)); +// } else if (message.type === "taskSucceeded") { +// const text = isProtecting ? t("protected_session.protecting-finished-successfully") : t("protected_session.unprotecting-finished-successfully"); +// const toast = makeToast(message, title, text); +// toast.closeAfter = 3000; - toastService.showPersistent(toast); - } -}); +// toastService.showPersistent(toast); +// } +// }); export default { protectNote, diff --git a/apps/client/src/services/tree.ts b/apps/client/src/services/tree.ts index c508654f5..75715a377 100644 --- a/apps/client/src/services/tree.ts +++ b/apps/client/src/services/tree.ts @@ -122,17 +122,17 @@ async function resolveNotePathToSegments(notePath: string, hoistedNoteId = "root } } -ws.subscribeToMessages((message) => { - if (message.type === "openNote") { - appContext.tabManager.activateOrOpenNote(message.noteId); +// ws.subscribeToMessages((message) => { +// if (message.type === "openNote") { +// appContext.tabManager.activateOrOpenNote(message.noteId); - if (utils.isElectron()) { - const currentWindow = utils.dynamicRequire("@electron/remote").getCurrentWindow(); +// if (utils.isElectron()) { +// const currentWindow = utils.dynamicRequire("@electron/remote").getCurrentWindow(); - currentWindow.show(); - } - } -}); +// currentWindow.show(); +// } +// } +// }); function getParentProtectedStatus(node: Fancytree.FancytreeNode) { return hoistedNoteService.isHoistedNode(node) ? false : node.getParent().data.isProtected; diff --git a/apps/server/src/assets/views/partials/windowGlobal.ejs b/apps/server/src/assets/views/partials/windowGlobal.ejs index 3536d5265..c69351e1a 100644 --- a/apps/server/src/assets/views/partials/windowGlobal.ejs +++ b/apps/server/src/assets/views/partials/windowGlobal.ejs @@ -3,7 +3,7 @@ window.glob = { device: "<%= device %>", - baseApiUrl: 'api/', + baseApiUrl: "<%= baseApiUrl %>", activeDialog: null, maxEntityChangeIdAtLoad: <%= maxEntityChangeIdAtLoad %>, maxEntityChangeSyncIdAtLoad: <%= maxEntityChangeSyncIdAtLoad %>, diff --git a/apps/server/src/assets/views/print.ejs b/apps/server/src/assets/views/print.ejs index 7e39b9567..c1808e507 100644 --- a/apps/server/src/assets/views/print.ejs +++ b/apps/server/src/assets/views/print.ejs @@ -21,6 +21,8 @@ document.getElementsByTagName("body")[0].style.display = "none"; +<%- include("./partials/windowGlobal.ejs", locals) %> + diff --git a/apps/server/src/routes/api/print.ts b/apps/server/src/routes/api/print.ts deleted file mode 100644 index 844769d56..000000000 --- a/apps/server/src/routes/api/print.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { Request, Response } from "express"; -import assetPath from "../../services/asset_path"; -import app_path from "../../services/app_path"; -import { getCurrentLocale } from "../../services/i18n"; - -export function getPrintablePage(req: Request, res: Response) { - const { noteId } = req.params; - - res.render("print", { - assetPath: assetPath, - appPath: app_path, - currentLocale: getCurrentLocale() - }); -} diff --git a/apps/server/src/routes/index.ts b/apps/server/src/routes/index.ts index 6bede94d7..0d968ce09 100644 --- a/apps/server/src/routes/index.ts +++ b/apps/server/src/routes/index.ts @@ -16,9 +16,19 @@ import type { Request, Response } from "express"; import type BNote from "../becca/entities/bnote.js"; import { getCurrentLocale } from "../services/i18n.js"; +type View = "desktop" | "mobile" | "print"; + function index(req: Request, res: Response) { - const options = optionService.getOptionMap(); const view = getView(req); + renderView(req, res, view); +} + +export function printIndex(req: Request, res: Response) { + renderView(req, res, "print"); +} + +function renderView(req: Request, res: Response, view: View) { + const options = optionService.getOptionMap(); //'overwrite' set to false (default) => the existing token will be re-used and validated //'validateOnReuse' set to false => if validation fails, generate a new token instead of throwing an error @@ -57,8 +67,9 @@ function index(req: Request, res: Response) { isProtectedSessionAvailable: protectedSessionService.isProtectedSessionAvailable(), maxContentWidth: Math.max(640, parseInt(options.maxContentWidth)), triliumVersion: packageJson.version, - assetPath: assetPath, - appPath: appPath, + assetPath: view !== "print" ? assetPath : "../" + assetPath, + appPath: view !== "print" ? appPath : "../" + appPath, + baseApiUrl: view !== "print" ? 'api/' : "../api/", currentLocale: getCurrentLocale() }); } @@ -122,5 +133,6 @@ function getAppCssNoteIds() { } export default { - index + index, + printIndex }; diff --git a/apps/server/src/routes/routes.ts b/apps/server/src/routes/routes.ts index a76810feb..30758b36e 100644 --- a/apps/server/src/routes/routes.ts +++ b/apps/server/src/routes/routes.ts @@ -72,7 +72,6 @@ import etapiBackupRoute from "../etapi/backup.js"; import etapiMetricsRoute from "../etapi/metrics.js"; import apiDocsRoute from "./api_docs.js"; import { apiResultHandler, apiRoute, asyncApiRoute, asyncRoute, route, router, uploadMiddlewareWithErrorHandling } from "./route_api.js"; -import { getPrintablePage } from "./api/print.js"; const GET = "get", PST = "post", @@ -82,6 +81,7 @@ const GET = "get", function register(app: express.Application) { route(GET, "/", [auth.checkAuth, csrfMiddleware], indexRoute.index); + route(GET, "/print/:noteId", [ auth.checkAuth ], indexRoute.printIndex); route(GET, "/login", [auth.checkAppInitialized, auth.checkPasswordSet], loginRoute.loginPage); route(GET, "/set-password", [auth.checkAppInitialized, auth.checkPasswordNotSet], loginRoute.setPasswordPage); @@ -387,9 +387,6 @@ function register(app: express.Application) { // API Documentation apiDocsRoute(app); - // Printing route - route(GET, "/print/:noteId", [ auth.checkAuth ], getPrintablePage); - app.use("", router); }