-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprompt.js
440 lines (353 loc) · 13.5 KB
/
prompt.js
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
// Validate configuration object size.
function validateConfigFormat(config) {
// If the config is empty.
if (Object.keys(config).length === 0) {
// Throw.
throw new Error("The configuration object is empty.");
}
}
// Validate the required config element properties.
function validateRequiredConfigElementProperties(config) {
// Ensure that each object on the config has the three required properties.
for (const key in config) {
// Ensure the config has the required `prompt`.
if (!config[key].hasOwnProperty("prompt")) {
// Throw.
throw new Error(`Missing required '${key}.prompt' property.`);
}
// If a prompt is required.
if (config[key].prompt && !config[key].hasOwnProperty("display")) {
// Ensure the config has the required `display`.
throw new Error(`Missing required '${key}.display' property.`);
}
// Ensure the config has the required `value`.
if (!config[key].hasOwnProperty("value")) {
// Throw.
throw new Error(`Missing required '${key}.value' property.`);
}
}
}
// Sort references array to ensure logical ordering of the prompts.
function sortReferences(references) {
// Sort.
references.sort((a, b) => {
// `a` should be prompted before `b`.
if (b.references.includes(a.key)) {
return -1;
// `a` should be prompted after `b`.
} else if (a.references.includes(b.key)) {
return 1;
// `a` and `b` are independent.
} else {
return 0;
}
})
}
// Get the reference from the config element value if it exists.
function getReferences(config, key) {
// Create the references array.
const references = [];
// If not string.
if (typeof config[key].value !== "string") {
// Return undefined.
return references;
}
// Perform the match.
const match = config[key].value.match(/{{\s*(.*?)\s*}}/g);
// Check if the match is not null.
if (match != null) {
// For each match.
for (const reference of match) {
// Remove the curly braces and spaces.
const referenceKey = reference.replace(/[{}]/g, "").trim();
// Add the reference to the array.
references.push(referenceKey);
}
}
// Otherwise return an empty array.
return references;
}
// Get the 'multiline' property of the config element if set or the default.
function getMultiLine(config, key) {
// Check if the element has the 'multiline' property.
if (config[key].hasOwnProperty("multiline")) {
// If the `multiline` property is not a boolean.
if (typeof config[key].multiline !== "boolean") {
// Throw.
throw new Error(`Property '${key}.multiline' must be a boolean.`);
}
// Return the value of the property.
return config[key].multiline;
} else {
// Return default.
return false;
}
}
// Get the 'limit' property of the config element if set or the default.
function getLimit(config, key) {
// Check if the element has the 'limit' property.
if (config[key].hasOwnProperty("limit")) {
// If the `limit` property is not a number.
if (typeof config[key].limit !== "number") {
// Throw.
throw new Error(`Property '${key}.limit' must be an integer.`);
}
// Return the value of the property.
return config[key].limit;
} else {
// Return default.
return undefined;
}
}
// Get the 'text' property of the config element if set or the default.
function getValueText(config, key) {
// Check if the element has the 'text' property.
if (config[key].hasOwnProperty("text")) {
// If the `text` property is not an array and not a function.
if (!Array.isArray(config[key].text) && typeof config[key].text !== "function") {
// Throw.
throw new Error(`Property '${key}.text' property must be an array or a function.`);
}
// Return the value of the property.
return config[key].text;
} else {
// Return default.
return config[key].value;
}
}
// Perform custom user checks on the config element values.
async function checkConfigElementValue(tp, config, key) {
// If the value has a custom user check.
if (config[key].hasOwnProperty("check")) {
// If the check is not a function.
if (typeof config[key].check !== "function") {
// Throw.
throw new Error("The 'check' property must be a function.");
}
// Assume the check fails.
let checkResult = false;
// Try to check the value.
try {
// Check the value.
checkResult = await Promise.resolve(config[key].check(config[key].value, tp, config));
} catch (error) {
// Throw if the checking failed.
throw new Error(`Failed checking '${key}' configuration value.`);
}
// If the check failed.
if (!checkResult) {
// Throw.
throw new Error(`Invalid value '${config[key].value}' for '${key}' config.`);
}
}
}
// Create a modal for displaying informational messages.
function makeInformationalModal(tp) {
// Define modal class.
class InformationalModal extends tp.obsidian.Modal {
// Modal text.
#content = "";
#title = "";
// Setter for the content.
set content(value) {
this.#content = value;
}
// Setter for the title.
set title(value) {
this.#title = value;
}
// Constructor.
constructor(app) {
super(app);
}
// On open event handler.
onOpen() {
// Extract the content container.
let { contentEl, titleEl } = this;
// Set the title.
titleEl.setText(this.#title);
// Set the content.
contentEl.setText(this.#content);
}
// On close event handler.
onClose() {
// Extract the content container.
let { contentEl, titleEl } = this;
// Clear the title.
titleEl.empty();
// Clear the content.
contentEl.empty();
}
}
// Return a modal instance.
return new InformationalModal(app);
}
// Perform customer user processing on the config element values.
async function processConfigElementValue(tp, config, key) {
// If the value has a custom user process.
if (config[key].hasOwnProperty("process")) {
// If the process is not a function.
if (typeof config[key].process !== "function") {
// Throw.
throw new Error(`Property '${key}.process' must be a function.`);
}
// Store the current value.
const originalValue = config[key].value;
// Get a modal class instance.
const modal = makeInformationalModal(tp);
// Set the modal title and content.
modal.title = `Processing '${key}' value.`;
modal.content = `Please wait while the '${key}' value is being processed...`;
// Try to process the value.
try {
// Open the modal.
modal.open();
// Process and update the value.
config[key].value = await Promise.resolve(config[key].process(originalValue, tp, config));
// Close the user modal.
modal.close();
// If a prompt is required.
if (config[key].prompt) {
// Give the user a chance to modify the result.
config[key].value = await issuePrompt(tp, config, key);
}
}
catch (error) {
// Restore the original value.
config[key].value = originalValue;
// Throw if the processing failed.
throw new Error(`Failed processing '${key}' configuration value.`);
}
}
}
// Issue the correct prompt baaed on value type.
async function issuePrompt(tp, config, key) {
// Define the value.
let value;
// If the config element is an array.
if (Array.isArray(config[key].value)) {
// Show the user a list of options to choose from.
value = await tp.system.suggester(
// The text representation of the items.
getValueText(config, key),
// The actual item values.
config[key].value,
// Throw on cancel.
true,
// The placeholder text.
config[key].display,
// Whether or not there is a limit.
getLimit(config, key)
);
} else {
// Capture the prompt promise.
const promptPromise = tp.system.prompt(
// The prompt message.
config[key].display,
// The default, prefilled value.
config[key].value,
// Throw on cancel.
true,
// Whether or not the prompt is multiline.
getMultiLine(config, key)
);
// Manually focus the input.
// See: https://github.com/SilentVoid13/Templater/issues/1120
document.getElementsByClassName("templater-prompt-input")[0].focus();
// Settle the promise.
value = await promptPromise;
}
// Return the value.
return value;
}
// Elicit answers from the user for the prompts.
async function elicitPromptAnswers(tp, config) {
// Configs that have references.
let references = [];
// Attempt to adjust the reference template config values.
try {
// For each config object in the template config.
for (const key in config) {
// If the value is expressed as a function.
if (typeof config[key].value === "function") {
// Evaluate the function.
config[key].value = await Promise.resolve(config[key].value(tp, config));
}
// Attempt to get the reference.
const elementReferences = getReferences(config, key);
// If the config element value has a reference.
if (elementReferences.length !== 0) {
// Merge the element references with the references array.
references.push(
{ key: key, references: elementReferences }
);
// For now continue with the prompts.
continue;
}
// If the config element requires a prompt.
if (config[key].prompt) {
// Update the config element value based on the prompt.
config[key].value = await issuePrompt(tp, config, key);
}
// Process the value if the config has a custom user process.
await processConfigElementValue(tp, config, key);
// Check the value if the config has a custom user check.
await checkConfigElementValue(tp, config, key);
}
// Sort the references to respect reference dependency.
sortReferences(references);
// For each configuration element that had a reference.
for (const reference of references) {
// For each reference in the configuration element.
for (let i = 0; i < reference.references.length; i++) {
// Update the element value with the reference value.
config[reference.key].value = config[reference.key].value.replace(
// The reference placeholder.
new RegExp(`{{\\s*${reference.references[i]}\\s*}}`, "g"),
// The reference value.
config[reference.references[i]].value
);
}
// If the value that referenced also requires prompting.
if (config[reference.key].prompt) {
// Prompt the user to modify the referenced value.
config[reference.key].value = await issuePrompt(tp, config, reference.key);
}
// Process the value if the config has a custom user process.
await processConfigElementValue(tp, config, reference.key);
// Check the value if the config has a custom user check.
await checkConfigElementValue(tp, config, reference.key);
}
} catch (error) {
// Set the default message to note creation cancelation.
let message = "Note creation canceled.";
// Decide on the error message.
if (error != null) {
// Set the error message.
message = error.message;
}
// Throw.
throw new Error(message);
}
}
/**
* Prompt the user based on a configuration object.
*
* @param {object} tp - The `Templater` object.
* @param {object} config - A configuration object containing objects used in the template.
*
* @returns {undefined} - The function returns `undefined` because the `config` object is passed by reference and modified in place.
*
* @throws {Error} - Throws a errors for various reasons. Error handling in the template is recommended.
*/
// Make prompt function.
async function prompt(tp, config) {
// Validate the config object format.
validateConfigFormat(config);
// Validate the properties of config elements.
validateRequiredConfigElementProperties(config);
// Adjust the passed by reference config object based on the user's input.
await elicitPromptAnswers(tp, config);
}
// Export the function.
module.exports = prompt