2024-11-02 00:55:45 +02:00
|
|
|
/**
|
|
|
|
* @module
|
2024-12-22 15:42:15 +02:00
|
|
|
*
|
2024-11-02 00:55:45 +02:00
|
|
|
* Options are key-value pairs that are used to store information such as user preferences (for example
|
|
|
|
* the current theme, sync server information), but also information about the state of the application.
|
2024-12-22 15:42:15 +02:00
|
|
|
*
|
2024-11-02 00:55:45 +02:00
|
|
|
* Although options internally are represented as strings, their value can be interpreted as a number or
|
|
|
|
* boolean by calling the appropriate methods from this service (e.g. {@link #getOptionInt}).\
|
2024-12-22 15:42:15 +02:00
|
|
|
*
|
2024-11-02 00:55:45 +02:00
|
|
|
* Generally options are shared across multiple instances of the application via the sync mechanism,
|
|
|
|
* however it is possible to have options that are local to an instance. For example, the user can select
|
|
|
|
* a theme on a device and it will not affect other devices.
|
|
|
|
*/
|
|
|
|
|
2024-07-18 21:35:17 +03:00
|
|
|
import becca from "../becca/becca.js";
|
2024-07-18 22:30:16 +03:00
|
|
|
import BOption from "../becca/entities/boption.js";
|
2024-07-24 20:19:27 +03:00
|
|
|
import { OptionRow } from '../becca/entities/rows.js';
|
2025-01-03 17:54:05 +02:00
|
|
|
import { KeyboardActionNames } from "./keyboard_actions_interface.js";
|
2024-07-18 21:35:17 +03:00
|
|
|
import sql from "./sql.js";
|
2021-05-01 21:52:22 +02:00
|
|
|
|
2025-01-03 17:54:05 +02:00
|
|
|
/**
|
|
|
|
* 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];
|
|
|
|
|
2024-11-02 00:39:22 +02:00
|
|
|
/**
|
|
|
|
* A dictionary where the keys are the option keys (e.g. `theme`) and their corresponding values.
|
|
|
|
*/
|
2025-01-03 17:54:05 +02:00
|
|
|
export type OptionMap = Record<OptionNames, string>;
|
2024-11-02 00:39:22 +02:00
|
|
|
|
2025-01-03 17:54:05 +02:00
|
|
|
function getOptionOrNull(name: OptionNames): string | null {
|
2022-01-02 21:20:56 +01:00
|
|
|
let option;
|
|
|
|
|
|
|
|
if (becca.loaded) {
|
|
|
|
option = becca.getOption(name);
|
2022-01-12 19:32:23 +01:00
|
|
|
} else {
|
2022-01-02 21:20:56 +01:00
|
|
|
// e.g. in initial sync becca is not loaded because DB is not initialized
|
2024-02-17 01:24:37 +02:00
|
|
|
option = sql.getRow<OptionRow>("SELECT * FROM options WHERE name = ?", [name]);
|
2022-01-02 21:20:56 +01:00
|
|
|
}
|
2023-05-04 22:16:18 +02:00
|
|
|
|
2022-01-12 19:32:23 +01:00
|
|
|
return option ? option.value : null;
|
|
|
|
}
|
|
|
|
|
2025-01-03 17:54:05 +02:00
|
|
|
function getOption(name: OptionNames) {
|
2022-01-12 19:32:23 +01:00
|
|
|
const val = getOptionOrNull(name);
|
2017-11-02 20:48:02 -04:00
|
|
|
|
2022-01-12 19:32:23 +01:00
|
|
|
if (val === null) {
|
2023-05-05 23:17:23 +02:00
|
|
|
throw new Error(`Option '${name}' doesn't exist`);
|
2017-11-02 20:48:02 -04:00
|
|
|
}
|
|
|
|
|
2022-01-12 19:32:23 +01:00
|
|
|
return val;
|
2017-11-02 20:48:02 -04:00
|
|
|
}
|
|
|
|
|
2025-01-03 17:54:05 +02:00
|
|
|
function getOptionInt(name: FilterOptionsByType<number>, defaultValue?: number): number {
|
2020-06-20 12:31:38 +02:00
|
|
|
const val = getOption(name);
|
2019-11-03 11:43:04 +01:00
|
|
|
|
|
|
|
const intVal = parseInt(val);
|
|
|
|
|
|
|
|
if (isNaN(intVal)) {
|
2023-05-09 23:32:06 +02:00
|
|
|
if (defaultValue === undefined) {
|
2023-05-20 20:55:57 +02:00
|
|
|
throw new Error(`Could not parse '${val}' into integer for option '${name}'`);
|
2023-05-09 23:32:06 +02:00
|
|
|
} else {
|
|
|
|
return defaultValue;
|
|
|
|
}
|
2019-11-03 11:43:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return intVal;
|
|
|
|
}
|
|
|
|
|
2025-01-03 17:54:05 +02:00
|
|
|
function getOptionBool(name: FilterOptionsByType<boolean>): boolean {
|
2020-06-20 12:31:38 +02:00
|
|
|
const val = getOption(name);
|
2019-11-10 11:25:41 +01:00
|
|
|
|
2024-04-03 23:28:26 +03:00
|
|
|
if (typeof val !== "string" || !['true', 'false'].includes(val)) {
|
2023-05-04 22:16:18 +02:00
|
|
|
throw new Error(`Could not parse '${val}' into boolean for option '${name}'`);
|
2019-11-10 11:25:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return val === 'true';
|
|
|
|
}
|
|
|
|
|
2025-01-03 17:54:05 +02:00
|
|
|
function setOption<T extends OptionNames>(name: T, value: string | OptionDefinitions[T]) {
|
2022-01-02 21:20:56 +01:00
|
|
|
const option = becca.getOption(name);
|
|
|
|
|
2020-03-01 20:49:11 +01:00
|
|
|
if (option) {
|
2025-01-03 17:54:05 +02:00
|
|
|
option.value = value as string;
|
2018-01-11 22:45:25 -05:00
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
option.save();
|
2020-03-01 20:49:11 +01:00
|
|
|
}
|
|
|
|
else {
|
2020-06-20 12:31:38 +02:00
|
|
|
createOption(name, value, false);
|
2020-03-01 20:49:11 +01:00
|
|
|
}
|
2018-01-11 22:45:25 -05:00
|
|
|
}
|
|
|
|
|
2024-11-02 00:55:45 +02:00
|
|
|
/**
|
|
|
|
* Creates a new option in the database, with the given name, value and whether it should be synced.
|
2024-12-22 15:42:15 +02:00
|
|
|
*
|
2024-11-02 00:55:45 +02:00
|
|
|
* @param name the name of the option to be created.
|
|
|
|
* @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).
|
|
|
|
*/
|
2025-01-03 17:54:05 +02:00
|
|
|
function createOption<T extends OptionNames>(name: T, value: string | OptionDefinitions[T], isSynced: boolean) {
|
2023-01-03 13:52:37 +01:00
|
|
|
new BOption({
|
2018-01-28 19:30:14 -05:00
|
|
|
name: name,
|
2025-01-03 17:54:05 +02:00
|
|
|
value: value as string,
|
2018-05-22 00:22:43 -04:00
|
|
|
isSynced: isSynced
|
|
|
|
}).save();
|
2017-11-02 20:48:02 -04:00
|
|
|
}
|
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
function getOptions() {
|
2021-05-01 21:52:22 +02:00
|
|
|
return Object.values(becca.options);
|
2018-09-06 11:54:04 +02:00
|
|
|
}
|
|
|
|
|
2023-06-29 22:10:13 +02:00
|
|
|
function getOptionMap() {
|
2025-01-03 17:54:05 +02:00
|
|
|
const map: Record<string, string> = {};
|
2021-05-01 21:52:22 +02:00
|
|
|
|
|
|
|
for (const option of Object.values(becca.options)) {
|
|
|
|
map[option.name] = option.value;
|
|
|
|
}
|
|
|
|
|
2025-01-03 17:54:05 +02:00
|
|
|
return map as OptionMap;
|
2018-09-06 11:54:04 +02:00
|
|
|
}
|
|
|
|
|
2024-07-18 21:47:30 +03:00
|
|
|
export default {
|
2017-11-02 20:48:02 -04:00
|
|
|
getOption,
|
2019-11-03 11:43:04 +01:00
|
|
|
getOptionInt,
|
2019-11-10 11:25:41 +01:00
|
|
|
getOptionBool,
|
2017-11-02 20:48:02 -04:00
|
|
|
setOption,
|
2018-09-06 11:54:04 +02:00
|
|
|
createOption,
|
|
|
|
getOptions,
|
2023-06-29 22:10:13 +02:00
|
|
|
getOptionMap,
|
2022-01-12 19:32:23 +01:00
|
|
|
getOptionOrNull
|
2020-06-20 12:31:38 +02:00
|
|
|
};
|