-
-
Notifications
You must be signed in to change notification settings - Fork 356
Expand file tree
/
Copy pathSettings.ts
More file actions
215 lines (190 loc) · 7.01 KB
/
Settings.ts
File metadata and controls
215 lines (190 loc) · 7.01 KB
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
import { DEFAULT_MAX_GENERIC_SUGGESTIONS, makeDefaultSuggestionBuilder } from '../Suggestor/Suggestor';
import { DEFAULT_SYMBOLS } from '../TaskSerializer/DefaultTaskSerializer';
import { DATAVIEW_SYMBOLS } from '../TaskSerializer/DataviewTaskSerializer';
import { StatusConfiguration } from '../StatusConfiguration';
import { Status } from '../Status';
import { DefaultTaskSerializer, type TaskSerializer } from '../TaskSerializer';
import type { SuggestionBuilder } from '../Suggestor';
import { DataviewTaskSerializer } from '../TaskSerializer/DataviewTaskSerializer';
import { DebugSettings } from './DebugSettings';
import { StatusSettings } from './StatusSettings';
import { Feature } from './Feature';
import type { FeatureFlag } from './Feature';
import { GlobalFilter } from './GlobalFilter';
interface SettingsMap {
[key: string]: string | boolean;
}
export type HeadingState = {
[id: string]: boolean;
};
/**
* Interface encapsulating how a Task is written to and read from text
*
*/
interface TaskFormat {
/** User facing name of the {@link TaskFormat} */
displayName: string;
/** {@link TaskSerializer} responsible for reading Tasks from text and writing them back into text */
taskSerializer: TaskSerializer;
/** Function that generates Intellisense-like suggestions as a user is typing a Task */
buildSuggestions?: SuggestionBuilder;
}
/** Map of all defined {@link TaskFormat}s */
export const TASK_FORMATS = {
tasksPluginEmoji: {
displayName: 'Tasks Emoji Format',
taskSerializer: new DefaultTaskSerializer(DEFAULT_SYMBOLS),
buildSuggestions: makeDefaultSuggestionBuilder(DEFAULT_SYMBOLS, DEFAULT_MAX_GENERIC_SUGGESTIONS),
},
dataview: {
displayName: 'Dataview',
taskSerializer: new DataviewTaskSerializer(),
buildSuggestions: makeDefaultSuggestionBuilder(DATAVIEW_SYMBOLS, DEFAULT_MAX_GENERIC_SUGGESTIONS),
},
} as const;
export type TASK_FORMATS = typeof TASK_FORMATS; // For convenience to make some typing easier
export interface Settings {
globalQuery: string;
globalFilter: string;
autoInsertGlobalFilter: boolean;
removeGlobalFilter: boolean;
taskFormat: keyof TASK_FORMATS;
setCreatedDate: boolean;
setDoneDate: boolean;
autoSuggestInEditor: boolean;
autoSuggestMinMatch: number;
autoSuggestMaxItems: number;
provideAccessKeys: boolean;
useFilenameAsScheduledDate: boolean;
filenameAsDateFolders: string[];
recurrenceOnNextLine: boolean;
// The custom status states.
statusSettings: StatusSettings;
// Collection of feature flag IDs and their state.
features: FeatureFlag;
// Settings are moved to a more general map to allow the settings UI to be
// dynamically generated.
generalSettings: SettingsMap;
// Tracks the stage of the headings in the settings UI.
headingOpened: HeadingState;
debugSettings: DebugSettings;
}
const defaultSettings: Settings = {
globalQuery: '',
globalFilter: GlobalFilter.empty,
autoInsertGlobalFilter: false,
removeGlobalFilter: false,
taskFormat: 'tasksPluginEmoji',
setCreatedDate: false,
setDoneDate: true,
autoSuggestInEditor: true,
autoSuggestMinMatch: 0,
autoSuggestMaxItems: 6,
provideAccessKeys: true,
useFilenameAsScheduledDate: false,
filenameAsDateFolders: [],
recurrenceOnNextLine: false,
statusSettings: new StatusSettings(),
features: Feature.settingsFlags,
generalSettings: {
/* Prevent duplicate values in user settings for now,
at least until I start porting the pre-1.23.0 settings
code to be generated from settingsConfiguration.json.
*/
// globalFilter: '',
// removeGlobalFilter: false,
// setDoneDate: true,
},
headingOpened: {},
debugSettings: new DebugSettings(),
};
let settings: Settings = { ...defaultSettings };
/**
* Returns the current settings as a object, it will also check and
* update the flags to make sure they are all shown in the data.json
* file. Exposure via the settings UI is optional.
*
* @export
* @returns true if the feature is enabled.
*/
export const getSettings = (): Settings => {
// Check to see if there is a new flag and if so add it to the users settings.
for (const flag in Feature.settingsFlags) {
if (settings.features[flag] === undefined) {
settings.features[flag] = Feature.settingsFlags[flag];
}
}
// In case saves pre-dated StatusConfiguration.type
// TODO Special case for symbol 'X' or 'x' (just in case)
settings.statusSettings.customStatuses.forEach((s, index, array) => {
const newType = Status.getTypeFromStatusTypeString(s.type);
array[index] = new StatusConfiguration(
s.symbol ?? ' ',
s.name,
s.nextStatusSymbol ?? 'x',
s.availableAsCommand,
newType,
);
});
return { ...settings };
};
export const updateSettings = (newSettings: Partial<Settings>): Settings => {
settings = { ...settings, ...newSettings };
return getSettings();
};
export const resetSettings = (): Settings => {
return updateSettings(defaultSettings);
};
export const updateGeneralSetting = (name: string, value: string | boolean): Settings => {
settings.generalSettings[name] = value;
/* Prevent duplicate values in user settings for now,
at least until I start porting the pre-1.23.0 settings
code to be generated from settingsConfiguration.json.
*/
// sync the old settings for the moment so a larger change is not needed.
// updateSettings({
// globalFilter: <string>settings.generalSettings['globalFilter'],
// removeGlobalFilter: <boolean>settings.generalSettings['removeGlobalFilter'],
// setDoneDate: <boolean>settings.generalSettings['setDoneDate'],
// });
return getSettings();
};
/**
* Returns the enabled state of the feature from settings.
*
* @export
* @param internalName the internal name of the feature.
* @returns true if the feature is enabled.
*/
export const isFeatureEnabled = (internalName: string): boolean => {
return settings.features[internalName] ?? false;
};
/**
* enables toggling the feature and returning the current collection with state.
*
* @export
* @param internalName the internal name of the feature.
* @param enabled the expected state of the feature.
* @returns the features with the specified feature toggled.
*/
export const toggleFeature = (internalName: string, enabled: boolean): FeatureFlag => {
settings.features[internalName] = enabled;
return settings.features;
};
/**
* Retrieves the {@link TaskFormat} that corresponds to user's selection ({@link Settings.taskFormat})
*
* @exports
* @returns {TaskFormat}
*/
export function getUserSelectedTaskFormat(): TaskFormat {
return TASK_FORMATS[getSettings().taskFormat];
}
/**
* Retrieves the source of the global {@link Query}
*
* @exports
*/
export function getGlobalQuerySource(): { source: string } {
return { source: getSettings().globalQuery };
}