Skip to content

Build your personal knowledge base with Trilium Notes

server: Implement color theme migration based on existing theme

Elian Doran92c588dc989f
diff --git a/src/services/options.ts b/src/services/options.ts
index a8b7500fcd..3ad56f04c5 100644
--- a/src/services/options.ts
+++ b/src/services/options.ts
@@ -3,6 +3,11 @@ import BOption from "../becca/entities/boption.js";
import { OptionRow } from '../becca/entities/rows.js';
import sql from "./sql.js";
+/**
+ * A dictionary where the keys are the option keys (e.g. `theme`) and their corresponding values.
+ */
+export type OptionMap = Record<string | number, string>;
+
function getOptionOrNull(name: string): string | null {
let option;
@@ -82,7 +87,7 @@ function getOptions() {
}
function getOptionMap() {
- const map: Record<string | number, string> = {};
+ const map: OptionMap = {};
for (const option of Object.values(becca.options)) {
map[option.name] = option.value;
diff --git a/src/services/options_init.ts b/src/services/options_init.ts
index 11ed44b9f8..71eb44b867 100644
--- a/src/services/options_init.ts
+++ b/src/services/options_init.ts
@@ -1,4 +1,4 @@
-import optionService from "./options.js";
+import optionService, { OptionMap } from "./options.js";
import appInfo from "./app_info.js";
import utils from "./utils.js";
import log from "./log.js";
@@ -19,9 +19,17 @@ interface NotSyncedOpts {
syncProxy?: string;
}
+/**
+ * Represents a correspondence between an option and its default value, to be initialized when the database is missing that particular option (after a migration from an older version, or when creating a new database).
+ */
interface DefaultOption {
name: string;
- value: string;
+ /**
+ * The value to initialize the option with, if the option is not already present in the database.
+ *
+ * If a function is passed in instead, the function is called if the option does not exist (with access to the current options) and the return value is used instead. Useful to migrate a new option with a value depending on some other option that might be initialized.
+ */
+ value: string | ((options: OptionMap) => string);
isSynced: boolean;
}
@@ -111,7 +119,13 @@ const defaultOptions: DefaultOption[] = [
{ name: 'locale', value: 'en', isSynced: true },
{ name: 'firstDayOfWeek', value: '1', isSynced: true },
- { name: "codeBlockTheme", value: "default:atom-one-dark", isSynced: false },
+ { name: "codeBlockTheme", value: (optionsMap) => {
+ if (optionsMap.theme === "light") {
+ return "default:stackoverflow-light";
+ } else {
+ return "default:stackoverflow-dark";
+ }
+ }, isSynced: false },
{ name: "codeBlockWordWrap", value: "false", isSynced: true }
];
@@ -122,9 +136,15 @@ function initStartupOptions() {
for (const {name, value, isSynced} of allDefaultOptions) {
if (!(name in optionsMap)) {
- optionService.createOption(name, value, isSynced);
+ let resolvedValue;
+ if (typeof value === "function") {
+ resolvedValue = value(optionsMap);
+ } else {
+ resolvedValue = value;
+ }
- log.info(`Created option "${name}" with default value "${value}"`);
+ optionService.createOption(name, resolvedValue, isSynced);
+ log.info(`Created option "${name}" with default value "${resolvedValue}"`);
}
}