Skip to content

Commit ad00796

Browse files
authored
Commands to increment/decrement current date (#193)
* Adds a command for quickly inserting the current date from the default calendar. * Add comments describing new function. * Fix insert command so that cursor moves to end of inserted text. * Brute force working increment/decrement. * Cleaned up implementation of new commands.
1 parent 044e5ab commit ad00796

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

src/main.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import { SettingsService } from "./settings/settings.service";
1111
import {
1212
type CalendarStore,
1313
createCalendarStore,
14+
incrementDay,
15+
decrementDay,
1416
} from "./stores/calendar.store";
1517
import { CodeBlockService } from "./calendar/codeblock";
1618
import { DEFAULT_FORMAT } from "./utils/constants";
@@ -193,6 +195,35 @@ export default class Calendarium extends Plugin {
193195
this.addCalendarView();
194196
},
195197
});
198+
199+
this.addCommand({
200+
id: "insert-calendarium-current-date",
201+
name: "Insert Current Date",
202+
/* Insert the current date from the default calendar at the current cursor location. */
203+
editorCallback: (editor: Editor, view: MarkdownView) => {
204+
this.insertCurrentDate(this.settings$.getDefaultCalendar(), editor, this.api);
205+
},
206+
});
207+
208+
this.addCommand({
209+
id: "advance-calendarium-current-date",
210+
name: "Set Current Date to Next",
211+
callback: () => {
212+
const defaultCal = this.settings$.getDefaultCalendar();
213+
const store = this.getStoreByCalendar(defaultCal);
214+
this.changeDay(defaultCal, store, incrementDay);
215+
},
216+
});
217+
218+
this.addCommand({
219+
id: "previous-calendarium-current-date",
220+
name: "Set Current Date to Previous",
221+
callback: () => {
222+
const defaultCal = this.settings$.getDefaultCalendar();
223+
const store = this.getStoreByCalendar(defaultCal);
224+
this.changeDay(defaultCal, store, decrementDay);
225+
},
226+
});
196227
}
197228

198229
addCalendarView(params: { full?: boolean; startup?: boolean } = {}) {
@@ -216,4 +247,23 @@ export default class Calendarium extends Plugin {
216247

217248
return leaf;
218249
}
250+
251+
insertCurrentDate(calendar: Calendar, editor: Editor, api: API) {
252+
const currentCalDate = calendar.current;
253+
const calendarApi = api.getAPI(calendar.name);
254+
const dateString = calendarApi.toDisplayDate(currentCalDate, calendar.dateFormat);
255+
const cursor = editor.getCursor();
256+
editor.replaceRange(dateString, cursor);
257+
cursor.ch += dateString.length;
258+
editor.setCursor(cursor);
259+
}
260+
261+
changeDay(
262+
calendar: Calendar,
263+
store: CalendarStore,
264+
changeFunction: (date: CalDate, yearCalculator: YearStoreCache, config: StaticCalendarData) => CalDate) {
265+
const currentCalDate = calendar.current;
266+
const newDay = changeFunction(currentCalDate, store.yearCalculator, store.staticStore.staticData);
267+
store.setCurrentDate(newDay);
268+
}
219269
}

src/stores/calendar.store.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ function decrementMonth(
609609
return next;
610610
}
611611

612-
function incrementDay(
612+
export function incrementDay(
613613
date: CalDate,
614614
yearCalculator: YearStoreCache,
615615
config: StaticCalendarData
@@ -627,7 +627,7 @@ function incrementDay(
627627
}
628628
return next;
629629
}
630-
function decrementDay(
630+
export function decrementDay(
631631
date: CalDate,
632632
yearCalculator: YearStoreCache,
633633
config: StaticCalendarData

0 commit comments

Comments
 (0)