-
Notifications
You must be signed in to change notification settings - Fork 125
/
Copy pathoptions_init.ts
229 lines (206 loc) · 11 KB
/
options_init.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import optionService from "./options.js";
import type { OptionMap, OptionNames } from "./options_interface.js";
import appInfo from "./app_info.js";
import { randomSecureToken, isWindows } from "./utils.js";
import log from "./log.js";
import dateUtils from "./date_utils.js";
import keyboardActions from "./keyboard_actions.js";
import type { KeyboardShortcutWithRequiredActionName } from "./keyboard_actions_interface.js";
import { DEFAULT_ALLOWED_TAGS } from "./html_sanitizer.js";
function initDocumentOptions() {
optionService.createOption("documentId", randomSecureToken(16), false);
optionService.createOption("documentSecret", randomSecureToken(16), false);
}
/**
* Contains additional options to be initialized for a new database, containing the information entered by the user.
*/
interface NotSyncedOpts {
syncServerHost?: string;
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: OptionNames;
/**
* The value to initialize the option with, if the option is not already present in the database.
*
* If a function is passed Gin 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;
}
/**
* Initializes the default options for new databases only.
*
* @param initialized `true` if the database has been fully initialized (i.e. a new database was created), or `false` if the database is created for sync.
* @param opts additional options to be initialized, for example the sync configuration.
*/
async function initNotSyncedOptions(initialized: boolean, opts: NotSyncedOpts = {}) {
optionService.createOption(
"openNoteContexts",
JSON.stringify([
{
notePath: "root",
active: true
}
]),
false
);
optionService.createOption("lastDailyBackupDate", dateUtils.utcNowDateTime(), false);
optionService.createOption("lastWeeklyBackupDate", dateUtils.utcNowDateTime(), false);
optionService.createOption("lastMonthlyBackupDate", dateUtils.utcNowDateTime(), false);
optionService.createOption("dbVersion", appInfo.dbVersion.toString(), false);
optionService.createOption("initialized", initialized ? "true" : "false", false);
optionService.createOption("lastSyncedPull", "0", false);
optionService.createOption("lastSyncedPush", "0", false);
optionService.createOption("theme", "next", false);
optionService.createOption("textNoteEditorType", "ckeditor-classic", true);
optionService.createOption("syncServerHost", opts.syncServerHost || "", false);
optionService.createOption("syncServerTimeout", "120000", false);
optionService.createOption("syncProxy", opts.syncProxy || "", false);
}
/**
* Contains all the default options that must be initialized on new and existing databases (at startup). The value can also be determined based on other options, provided they have already been initialized.
*/
const defaultOptions: DefaultOption[] = [
{ name: "revisionSnapshotTimeInterval", value: "600", isSynced: true },
{ name: "revisionSnapshotTimeIntervalTimeScale", value: "60", isSynced: true }, // default to Minutes
{ name: "revisionSnapshotNumberLimit", value: "-1", isSynced: true },
{ name: "protectedSessionTimeout", value: "600", isSynced: true },
{ name: "protectedSessionTimeoutTimeScale", value: "60", isSynced: true },
{ name: "zoomFactor", value: isWindows ? "0.9" : "1.0", isSynced: false },
{ name: "overrideThemeFonts", value: "false", isSynced: false },
{ name: "mainFontFamily", value: "theme", isSynced: false },
{ name: "mainFontSize", value: "100", isSynced: false },
{ name: "treeFontFamily", value: "theme", isSynced: false },
{ name: "treeFontSize", value: "100", isSynced: false },
{ name: "detailFontFamily", value: "theme", isSynced: false },
{ name: "detailFontSize", value: "110", isSynced: false },
{ name: "monospaceFontFamily", value: "theme", isSynced: false },
{ name: "monospaceFontSize", value: "110", isSynced: false },
{ name: "spellCheckEnabled", value: "true", isSynced: false },
{ name: "spellCheckLanguageCode", value: "en-US", isSynced: false },
{ name: "imageMaxWidthHeight", value: "2000", isSynced: true },
{ name: "imageJpegQuality", value: "75", isSynced: true },
{ name: "autoFixConsistencyIssues", value: "true", isSynced: false },
{ name: "vimKeymapEnabled", value: "false", isSynced: false },
{ name: "codeLineWrapEnabled", value: "true", isSynced: false },
{
name: "codeNotesMimeTypes",
value: '["text/x-csrc","text/x-c++src","text/x-csharp","text/css","text/x-go","text/x-groovy","text/x-haskell","text/html","message/http","text/x-java","application/javascript;env=frontend","application/javascript;env=backend","application/json","text/x-kotlin","text/x-markdown","text/x-perl","text/x-php","text/x-python","text/x-ruby",null,"text/x-sql","text/x-sqlite;schema=trilium","text/x-swift","text/xml","text/x-yaml","text/x-sh","application/typescript"]',
isSynced: true
},
{ name: "leftPaneWidth", value: "25", isSynced: false },
{ name: "leftPaneVisible", value: "true", isSynced: false },
{ name: "rightPaneWidth", value: "25", isSynced: false },
{ name: "rightPaneVisible", value: "true", isSynced: false },
{ name: "nativeTitleBarVisible", value: "false", isSynced: false },
{ name: "eraseEntitiesAfterTimeInSeconds", value: "604800", isSynced: true }, // default is 7 days
{ name: "eraseEntitiesAfterTimeScale", value: "86400", isSynced: true }, // default 86400 seconds = Day
{ name: "hideArchivedNotes_main", value: "false", isSynced: false },
{ name: "debugModeEnabled", value: "false", isSynced: false },
{ name: "headingStyle", value: "underline", isSynced: true },
{ name: "autoCollapseNoteTree", value: "true", isSynced: true },
{ name: "autoReadonlySizeText", value: "10000", isSynced: false },
{ name: "autoReadonlySizeCode", value: "30000", isSynced: false },
{ name: "dailyBackupEnabled", value: "true", isSynced: false },
{ name: "weeklyBackupEnabled", value: "true", isSynced: false },
{ name: "monthlyBackupEnabled", value: "true", isSynced: false },
{ name: "maxContentWidth", value: "1200", isSynced: false },
{ name: "compressImages", value: "true", isSynced: true },
{ name: "downloadImagesAutomatically", value: "true", isSynced: true },
{ name: "minTocHeadings", value: "5", isSynced: true },
{ name: "highlightsList", value: '["bold","italic","underline","color","bgColor"]', isSynced: true },
{ name: "checkForUpdates", value: "true", isSynced: true },
{ name: "disableTray", value: "false", isSynced: false },
{ name: "eraseUnusedAttachmentsAfterSeconds", value: "2592000", isSynced: true }, // default 30 days
{ name: "eraseUnusedAttachmentsAfterTimeScale", value: "86400", isSynced: true }, // default 86400 seconds = Day
{ name: "customSearchEngineName", value: "DuckDuckGo", isSynced: true },
{ name: "customSearchEngineUrl", value: "https://duckduckgo.com/?q={keyword}", isSynced: true },
{ name: "promotedAttributesOpenInRibbon", value: "true", isSynced: true },
{ name: "editedNotesOpenInRibbon", value: "true", isSynced: true },
{ name: "mfaEnabled", value: "false", isSynced: false },
{ name: "mfaMethod", value: "totp", isSynced: false },
{ name: "encryptedRecoveryCodes", value: "false", isSynced: false },
{ name: "userSubjectIdentifierSaved", value: "false", isSynced: false },
// Appearance
{ name: "splitEditorOrientation", value: "horizontal", isSynced: true },
// Internationalization
{ name: "locale", value: "en", isSynced: true },
{ name: "formattingLocale", value: "en", isSynced: true },
{ name: "firstDayOfWeek", value: "1", isSynced: true },
{ name: "languages", value: "[]", isSynced: true },
// Code block configuration
{
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 },
// Text note configuration
{ name: "textNoteEditorType", value: "ckeditor-balloon", isSynced: true },
{ name: "textNoteEditorMultilineToolbar", value: "false", isSynced: true },
// HTML import configuration
{ name: "layoutOrientation", value: "vertical", isSynced: false },
{ name: "backgroundEffects", value: "false", isSynced: false },
{
name: "allowedHtmlTags",
value: JSON.stringify(DEFAULT_ALLOWED_TAGS),
isSynced: true
},
// Share settings
{ name: "redirectBareDomain", value: "false", isSynced: true },
{ name: "showLoginInShareTheme", value: "false", isSynced: true }
];
/**
* Initializes the options, by checking which options from {@link #allDefaultOptions()} are missing and registering them. It will also check some environment variables such as safe mode, to make any necessary adjustments.
*
* This method is called regardless of whether a new database is created, or an existing database is used.
*/
function initStartupOptions() {
const optionsMap = optionService.getOptionMap();
const allDefaultOptions = defaultOptions.concat(getKeyboardDefaultOptions());
for (const { name, value, isSynced } of allDefaultOptions) {
if (!(name in optionsMap)) {
let resolvedValue;
if (typeof value === "function") {
resolvedValue = value(optionsMap);
} else {
resolvedValue = value;
}
optionService.createOption(name, resolvedValue, isSynced);
log.info(`Created option "${name}" with default value "${resolvedValue}"`);
}
}
if (process.env.TRILIUM_START_NOTE_ID || process.env.TRILIUM_SAFE_MODE) {
optionService.setOption(
"openNoteContexts",
JSON.stringify([
{
notePath: process.env.TRILIUM_START_NOTE_ID || "root",
active: true
}
])
);
}
}
function getKeyboardDefaultOptions() {
return (keyboardActions.getDefaultKeyboardActions().filter((ka) => !!ka.actionName) as KeyboardShortcutWithRequiredActionName[]).map((ka) => ({
name: `keyboardShortcuts${ka.actionName.charAt(0).toUpperCase()}${ka.actionName.slice(1)}`,
value: JSON.stringify(ka.defaultShortcuts),
isSynced: false
})) as DefaultOption[];
}
export default {
initDocumentOptions,
initNotSyncedOptions,
initStartupOptions
};