mirror of
https://github.com/zadam/trilium.git
synced 2025-11-13 08:45:50 +01:00
refactor(server): typed options
This commit is contained in:
@@ -15,14 +15,108 @@
|
||||
import becca from "../becca/becca.js";
|
||||
import BOption from "../becca/entities/boption.js";
|
||||
import { OptionRow } from '../becca/entities/rows.js';
|
||||
import { KeyboardActionNames } from "./keyboard_actions_interface.js";
|
||||
import sql from "./sql.js";
|
||||
|
||||
/**
|
||||
* For each keyboard action, there is a corresponding option which identifies the key combination defined by the user.
|
||||
*/
|
||||
type KeyboardShortcutsOptions<T extends KeyboardActionNames> = {
|
||||
[key in T as `keyboardShortcuts${Capitalize<key>}`]: string
|
||||
};
|
||||
|
||||
interface OptionDefinitions extends KeyboardShortcutsOptions<KeyboardActionNames> {
|
||||
"openNoteContexts": string;
|
||||
"lastDailyBackupDate": string;
|
||||
"lastWeeklyBackupDate": string;
|
||||
"lastMonthlyBackupDate": string;
|
||||
"dbVersion": string;
|
||||
"theme": string;
|
||||
"syncServerHost": string;
|
||||
"syncServerTimeout": string;
|
||||
"syncProxy": string;
|
||||
"mainFontFamily": string;
|
||||
"treeFontFamily": string;
|
||||
"detailFontFamily": string;
|
||||
"monospaceFontFamily": string;
|
||||
"spellCheckLanguageCode": string;
|
||||
"codeNotesMimeTypes": string;
|
||||
"headingStyle": string;
|
||||
"highlightsList": string;
|
||||
"customSearchEngineName": string;
|
||||
"customSearchEngineUrl": string;
|
||||
"locale": string;
|
||||
"codeBlockTheme": string;
|
||||
"textNoteEditorType": string;
|
||||
"layoutOrientation": string;
|
||||
"allowedHtmlTags": string;
|
||||
"documentId": string;
|
||||
"documentSecret": string;
|
||||
"passwordVerificationHash": string;
|
||||
"passwordVerificationSalt": string;
|
||||
"passwordDerivedKeySalt": string;
|
||||
"encryptedDataKey": string;
|
||||
|
||||
"lastSyncedPull": number;
|
||||
"lastSyncedPush": number;
|
||||
"revisionSnapshotTimeInterval": number;
|
||||
"revisionSnapshotNumberLimit": number;
|
||||
"protectedSessionTimeout": number;
|
||||
"zoomFactor": number;
|
||||
"mainFontSize": number;
|
||||
"treeFontSize": number;
|
||||
"detailFontSize": number;
|
||||
"monospaceFontSize": number;
|
||||
"imageMaxWidthHeight": number;
|
||||
"imageJpegQuality": number;
|
||||
"leftPaneWidth": number;
|
||||
"rightPaneWidth": number;
|
||||
"eraseEntitiesAfterTimeInSeconds": number;
|
||||
"autoReadonlySizeText": number;
|
||||
"autoReadonlySizeCode": number;
|
||||
"maxContentWidth": number;
|
||||
"minTocHeadings": number;
|
||||
"eraseUnusedAttachmentsAfterSeconds": number;
|
||||
"firstDayOfWeek": number;
|
||||
|
||||
"initialized": boolean;
|
||||
"overrideThemeFonts": boolean;
|
||||
"spellCheckEnabled": boolean;
|
||||
"autoFixConsistencyIssues": boolean;
|
||||
"vimKeymapEnabled": boolean;
|
||||
"codeLineWrapEnabled": boolean;
|
||||
"leftPaneVisible": boolean;
|
||||
"rightPaneVisible": boolean;
|
||||
"nativeTitleBarVisible": boolean;
|
||||
"hideArchivedNotes_main": boolean;
|
||||
"debugModeEnabled": boolean;
|
||||
"autoCollapseNoteTree": boolean;
|
||||
"dailyBackupEnabled": boolean;
|
||||
"weeklyBackupEnabled": boolean;
|
||||
"monthlyBackupEnabled": boolean;
|
||||
"compressImages": boolean;
|
||||
"downloadImagesAutomatically": boolean;
|
||||
"checkForUpdates": boolean;
|
||||
"disableTray": boolean;
|
||||
"promotedAttributesOpenInRibbon": boolean;
|
||||
"editedNotesOpenInRibbon": boolean;
|
||||
"codeBlockWordWrap": boolean;
|
||||
"textNoteEditorMultilineToolbar": boolean;
|
||||
"backgroundEffects": boolean;
|
||||
};
|
||||
|
||||
export type OptionNames = keyof OptionDefinitions;
|
||||
|
||||
type FilterOptionsByType<U> = {
|
||||
[K in keyof OptionDefinitions]: OptionDefinitions[K] extends U ? K : never;
|
||||
}[keyof OptionDefinitions];
|
||||
|
||||
/**
|
||||
* A dictionary where the keys are the option keys (e.g. `theme`) and their corresponding values.
|
||||
*/
|
||||
export type OptionMap = Record<string | number, string>;
|
||||
export type OptionMap = Record<OptionNames, string>;
|
||||
|
||||
function getOptionOrNull(name: string): string | null {
|
||||
function getOptionOrNull(name: OptionNames): string | null {
|
||||
let option;
|
||||
|
||||
if (becca.loaded) {
|
||||
@@ -35,7 +129,7 @@ function getOptionOrNull(name: string): string | null {
|
||||
return option ? option.value : null;
|
||||
}
|
||||
|
||||
function getOption(name: string) {
|
||||
function getOption(name: OptionNames) {
|
||||
const val = getOptionOrNull(name);
|
||||
|
||||
if (val === null) {
|
||||
@@ -45,7 +139,7 @@ function getOption(name: string) {
|
||||
return val;
|
||||
}
|
||||
|
||||
function getOptionInt(name: string, defaultValue?: number): number {
|
||||
function getOptionInt(name: FilterOptionsByType<number>, defaultValue?: number): number {
|
||||
const val = getOption(name);
|
||||
|
||||
const intVal = parseInt(val);
|
||||
@@ -61,7 +155,7 @@ function getOptionInt(name: string, defaultValue?: number): number {
|
||||
return intVal;
|
||||
}
|
||||
|
||||
function getOptionBool(name: string): boolean {
|
||||
function getOptionBool(name: FilterOptionsByType<boolean>): boolean {
|
||||
const val = getOption(name);
|
||||
|
||||
if (typeof val !== "string" || !['true', 'false'].includes(val)) {
|
||||
@@ -71,15 +165,11 @@ function getOptionBool(name: string): boolean {
|
||||
return val === 'true';
|
||||
}
|
||||
|
||||
function setOption(name: string, value: string | number | boolean) {
|
||||
if (value === true || value === false || typeof value === "number") {
|
||||
value = value.toString();
|
||||
}
|
||||
|
||||
function setOption<T extends OptionNames>(name: T, value: string | OptionDefinitions[T]) {
|
||||
const option = becca.getOption(name);
|
||||
|
||||
if (option) {
|
||||
option.value = value;
|
||||
option.value = value as string;
|
||||
|
||||
option.save();
|
||||
}
|
||||
@@ -95,10 +185,10 @@ function setOption(name: string, value: string | number | boolean) {
|
||||
* @param value the value of the option, as a string. It can then be interpreted as other types such as a number of boolean.
|
||||
* @param isSynced `true` if the value should be synced across multiple instances (e.g. locale) or `false` if it should be local-only (e.g. theme).
|
||||
*/
|
||||
function createOption(name: string, value: string, isSynced: boolean) {
|
||||
function createOption<T extends OptionNames>(name: T, value: string | OptionDefinitions[T], isSynced: boolean) {
|
||||
new BOption({
|
||||
name: name,
|
||||
value: value,
|
||||
value: value as string,
|
||||
isSynced: isSynced
|
||||
}).save();
|
||||
}
|
||||
@@ -108,13 +198,13 @@ function getOptions() {
|
||||
}
|
||||
|
||||
function getOptionMap() {
|
||||
const map: OptionMap = {};
|
||||
const map: Record<string, string> = {};
|
||||
|
||||
for (const option of Object.values(becca.options)) {
|
||||
map[option.name] = option.value;
|
||||
}
|
||||
|
||||
return map;
|
||||
return map as OptionMap;
|
||||
}
|
||||
|
||||
export default {
|
||||
|
||||
Reference in New Issue
Block a user