Notes/src/routes/index.ts

82 lines
3.2 KiB
TypeScript
Raw Normal View History

2017-10-21 21:10:33 -04:00
"use strict";
import sql from "../services/sql.js";
import attributeService from "../services/attributes.js";
import config from "../services/config.js";
import optionService from "../services/options.js";
import log from "../services/log.js";
import env from "../services/env.js";
import utils from "../services/utils.js";
import protectedSessionService from "../services/protected_session.js";
2024-07-24 20:52:46 +03:00
import packageJson from "../../package.json" with { type: "json" };
import assetPath from "../services/asset_path.js";
import appPath from "../services/app_path.js";
2024-04-07 14:36:47 +03:00
import { Request, Response } from 'express';
2024-04-07 14:36:47 +03:00
function index(req: Request, res: Response) {
2023-06-29 22:10:13 +02:00
const options = optionService.getOptionMap();
2018-09-06 11:54:04 +02:00
2023-06-29 23:32:19 +02:00
const view = (!utils.isElectron() && req.cookies['trilium-device'] === 'mobile')
? 'mobile'
: 'desktop';
2020-04-23 23:08:15 +02:00
const csrfToken = req.csrfToken();
log.info(`Generated CSRF token ${csrfToken} with secret ${res.getHeader('set-cookie')}`);
// We force the page to not be cached since on mobile the CSRF token can be
// broken when closing the browser and coming back in to the page.
// The page is restored from cache, but the API call fail.
res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
const isElectron = utils.isElectron();
2018-12-28 23:47:06 +01:00
res.render(view, {
csrfToken: csrfToken,
themeCssUrl: getThemeCssUrl(options.theme),
headingStyle: options.headingStyle,
layoutOrientation: options.layoutOrientation,
2024-12-04 22:55:07 +02:00
platform: process.platform,
isElectron,
hasNativeTitleBar: (isElectron && options.nativeTitleBarVisible === "true"),
2019-01-13 21:27:32 +01:00
mainFontSize: parseInt(options.mainFontSize),
treeFontSize: parseInt(options.treeFontSize),
detailFontSize: parseInt(options.detailFontSize),
maxEntityChangeIdAtLoad: sql.getValue("SELECT COALESCE(MAX(id), 0) FROM entity_changes"),
2021-03-21 22:43:41 +01:00
maxEntityChangeSyncIdAtLoad: sql.getValue("SELECT COALESCE(MAX(id), 0) FROM entity_changes WHERE isSynced = 1"),
2018-04-02 21:34:28 -04:00
instanceName: config.General ? config.General.instanceName : null,
2020-06-20 12:31:38 +02:00
appCssNoteIds: getAppCssNoteIds(),
2020-04-25 23:52:13 +02:00
isDev: env.isDev(),
2023-04-03 23:47:24 +02:00
isMainWindow: !req.query.extraWindow,
isProtectedSessionAvailable: protectedSessionService.isProtectedSessionAvailable(),
maxContentWidth: Math.max(640, parseInt(options.maxContentWidth)),
triliumVersion: packageJson.version,
assetPath: assetPath,
appPath: appPath
});
}
2024-04-07 14:36:47 +03:00
function getThemeCssUrl(theme: string) {
if (theme === 'light') {
return false; // light theme is always loaded as baseline
2023-06-29 23:32:19 +02:00
} else if (theme === 'dark') {
return `${assetPath}/stylesheets/theme-dark.css`;
} else if (theme === "next") {
return `${assetPath}/stylesheets/theme-next.css`;
2023-06-29 23:32:19 +02:00
} else {
const themeNote = attributeService.getNoteWithLabel('appTheme', theme);
if (themeNote) {
return `api/notes/download/${themeNote.noteId}`;
2023-06-29 23:32:19 +02:00
} else {
return false; // baseline light theme
}
}
}
2020-06-20 12:31:38 +02:00
function getAppCssNoteIds() {
return attributeService.getNotesWithLabel('appCss').map(note => note.noteId);
}
export default {
index
};