Skip to content

Commit dfbd0a5

Browse files
committed
Refactor to add validateEditorState() by Cursor.
1 parent 40f9f54 commit dfbd0a5

1 file changed

Lines changed: 112 additions & 12 deletions

File tree

‎jgclark.EventHelpers/src/eventsToNotes.js‎

Lines changed: 112 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,91 @@ const DEFAULT_FORMAT_ALL_DAY = '- *|CAL|*: *|TITLE|**| with ATTENDEENAMES|**|\nL
3434
// Helper Functions (Private)
3535
// ----------------------------------------------------------------------------
3636

37+
/**
38+
* Validate Editor state based on requirements
39+
* @param {Object} options - Validation options
40+
* @param {boolean} options.requireEditor - Whether Editor must exist
41+
* @param {boolean} options.requireFilename - Whether Editor.filename must exist
42+
* @param {boolean} options.requireCalendarType - Whether Editor.type must be 'Calendar'
43+
* @param {boolean} options.requireDailyNote - Whether note must be a daily note
44+
* @param {boolean} options.requireWeeklyNote - Whether note must be a weekly note
45+
* @param {boolean} options.requireDailyOrWeekly - Whether note must be daily or weekly
46+
* @returns {{isValid: boolean, errorMessage: ?string, note: ?TNote}} Validation result
47+
*/
48+
function validateEditorState(options: {
49+
requireEditor?: boolean,
50+
requireFilename?: boolean,
51+
requireCalendarType?: boolean,
52+
requireDailyNote?: boolean,
53+
requireWeeklyNote?: boolean,
54+
requireDailyOrWeekly?: boolean,
55+
}): { isValid: boolean, errorMessage: ?string, note: ?TNote } {
56+
const {
57+
requireEditor = true,
58+
requireFilename = false,
59+
requireCalendarType = true,
60+
requireDailyNote = false,
61+
requireWeeklyNote = false,
62+
requireDailyOrWeekly = false,
63+
} = options
64+
65+
if (requireEditor && (!Editor || Editor.note == null)) {
66+
return {
67+
isValid: false,
68+
errorMessage: 'Please run again with a note open.',
69+
note: null,
70+
}
71+
}
72+
73+
if (!Editor || Editor.note == null) {
74+
return { isValid: false, errorMessage: 'No note is currently open.', note: null }
75+
}
76+
77+
const note: TNote = Editor.note
78+
79+
if (requireFilename && Editor.filename == null) {
80+
return {
81+
isValid: false,
82+
errorMessage: 'Note filename is not available.',
83+
note: null,
84+
}
85+
}
86+
87+
if (requireCalendarType && Editor.type !== 'Calendar') {
88+
return {
89+
isValid: false,
90+
errorMessage: 'Please run again with a calendar note open.',
91+
note: null,
92+
}
93+
}
94+
95+
if (requireDailyNote && !isDailyNote(note)) {
96+
return {
97+
isValid: false,
98+
errorMessage: 'Please run again with a daily calendar note open.',
99+
note: null,
100+
}
101+
}
102+
103+
if (requireWeeklyNote && !isWeeklyNote(note)) {
104+
return {
105+
isValid: false,
106+
errorMessage: 'Please run again with a weekly calendar note open.',
107+
note: null,
108+
}
109+
}
110+
111+
if (requireDailyOrWeekly && !isDailyNote(note) && !isWeeklyNote(note)) {
112+
return {
113+
isValid: false,
114+
errorMessage: 'Please run again with a daily or weekly calendar note open.',
115+
note: null,
116+
}
117+
}
118+
119+
return { isValid: true, errorMessage: null, note }
120+
}
121+
37122
/**
38123
* Normalize parameter string, handling null/undefined cases
39124
* @param {?string} paramStringIn - Input parameter string
@@ -187,11 +272,17 @@ function sortEvents(events: Array<{ cal: string, start: Date, text: string }>, s
187272
*/
188273
export async function listDaysEvents(paramStringIn: string = ''): Promise<string> {
189274
try {
190-
if (!Editor || Editor.note == null || Editor.filename == null || Editor.type !== 'Calendar' || (!isDailyNote(Editor.note) && !isWeeklyNote(Editor.note))) {
191-
await showMessage(`Please run again with a daily or weekly calendar note open.`, 'OK', 'List Events')
275+
const validation = validateEditorState({
276+
requireEditor: true,
277+
requireFilename: true,
278+
requireCalendarType: true,
279+
requireDailyOrWeekly: true,
280+
})
281+
if (!validation.isValid) {
282+
await showMessage(validation.errorMessage || 'Please run again with a daily or weekly calendar note open.', 'OK', 'List Events')
192283
return ''
193284
}
194-
const openNote: TNote = Editor.note
285+
const openNote: TNote = validation.note
195286
const paramString = normalizeParamString(paramStringIn, 'listDaysEvents')
196287

197288
const config = await getEventsSettings()
@@ -261,8 +352,13 @@ export async function listDaysEvents(paramStringIn: string = ''): Promise<string
261352
export async function insertDaysEvents(paramString: ?string): Promise<void> {
262353
try {
263354
logDebug(pluginJson, 'insertDaysEvents: Starting')
264-
if (!Editor || Editor.note == null || Editor.type !== 'Calendar' || !isDailyNote(Editor.note)) {
265-
await showMessage(`Please run again with a daily calendar note open.`, 'OK', 'Insert Events')
355+
const validation = validateEditorState({
356+
requireEditor: true,
357+
requireCalendarType: true,
358+
requireDailyNote: true,
359+
})
360+
if (!validation.isValid) {
361+
await showMessage(validation.errorMessage || 'Please run again with a daily calendar note open.', 'OK', 'Insert Events')
266362
return
267363
}
268364

@@ -278,7 +374,7 @@ export async function insertDaysEvents(paramString: ?string): Promise<void> {
278374
// ----------------------------------------------------------------------------
279375

280376
/**
281-
* Return string of matching events in the current day's note, from list in keys of config.addMatchingEvents, having applied placeholder formatting.
377+
* Return markdown-formatted list of matching events for the currently-open Calendar note, from list in keys of config.addMatchingEvents, having applied placeholder formatting.
282378
* Note: Parameters can be passed in as a JSON string, except for the complex 'format' which only comes from 'config.addMatchingEvents'.
283379
* @author @jgclark
284380
* @param {?string} paramStringIn Paramaters to use
@@ -288,11 +384,16 @@ export async function listMatchingDaysEvents(
288384
paramStringIn: string = '', // NB: the parameter isn't currently used, but is provided for future expansion.
289385
): Promise<string> {
290386
try {
291-
if (Editor.note == null || Editor.filename == null || Editor.type !== 'Calendar') {
292-
await showMessage(`Please run again with a calendar note open.`, 'OK', 'List Events')
387+
const validation = validateEditorState({
388+
requireEditor: false,
389+
requireFilename: true,
390+
requireCalendarType: true,
391+
})
392+
if (!validation.isValid) {
393+
await showMessage(validation.errorMessage || 'Please run again with a calendar note open.', 'OK', 'List Events')
293394
return ''
294395
}
295-
const openNote: TNote = Editor.note
396+
const openNote: TNote = validation.note
296397
const paramString = normalizeParamString(paramStringIn, 'listMatchingDaysEvents')
297398

298399
const config = await getEventsSettings()
@@ -426,8 +527,7 @@ export async function insertWeeksEvents(paramString: ?string): Promise<void> {
426527

427528
// ----------------------------------------------------------------------------
428529
/**
429-
* Insert list of matching events in the current day's note, from list
430-
* in keys of config.addMatchingEvents. Apply format too.
530+
* Insert list of matching events in the current day's note, from list in keys of config.addMatchingEvents. Apply format too.
431531
* @author @jgclark
432532
*
433533
* @param {?string} paramString Paramaters to use (to pass on to next function)
@@ -448,7 +548,7 @@ export async function insertMatchingDaysEvents(paramString: ?string): Promise<vo
448548

449549
// ----------------------------------------------------------------------------
450550
/**
451-
* Change the format placeholders to the actual values, using a Map
551+
* Change the format placeholders to the actual values, using a Map.
452552
* @author @jgclark
453553
*
454554
* @param {TCalendarItem} item Calendar item whose values to use

0 commit comments

Comments
 (0)