Skip to content

Commit 5358211

Browse files
Command for quickly inserting current date from default calendar (#192)
* 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. * chore(types): Update types --------- Co-authored-by: Jeremy Valentine <[email protected]>
1 parent a8a8611 commit 5358211

File tree

2 files changed

+46
-9
lines changed

2 files changed

+46
-9
lines changed

src/api/calendar.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ export class CalendarAPI {
116116
* @param {string} dateFormat Optional date format string
117117
* @returns formatted string
118118
*/
119-
toDisplayDate(date: CalDate, end?: CalDate, dateFormat?: string): string {
119+
toDisplayDate(date: CalDate, end?: CalDate | null, dateFormat?: string): string {
120120
return dateString(date, this.#object, end, dateFormat);
121121
}
122122

src/main.ts

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
1-
import { Platform, Plugin, WorkspaceLeaf, addIcon } from "obsidian";
1+
import {
2+
Editor,
3+
Platform,
4+
Plugin,
5+
WorkspaceLeaf,
6+
addIcon,
7+
} from "obsidian";
28

39
import CalendariumSettings from "./settings/settings.view";
410

5-
import type { Calendar, CalendariumData } from "./@types";
11+
import type {
12+
CalDate,
13+
Calendar,
14+
CalendariumData,
15+
StaticCalendarData,
16+
} from "./@types";
617
import CalendariumView from "./calendar/view";
718

819
import { Watcher } from "./watcher/watcher";
@@ -20,6 +31,8 @@ import { CalendariumNotice } from "./utils/notice";
2031

2132
import { ViewType } from "./calendar/view.types";
2233
import { AgendaView } from "./agenda/view.agenda";
34+
import type { YearStoreCache } from "./stores/years.store";
35+
import { get } from "svelte/store";
2336

2437
declare global {
2538
interface Window {
@@ -200,8 +213,12 @@ export default class Calendarium extends Plugin {
200213
id: "insert-calendarium-current-date",
201214
name: "Insert Current Date",
202215
/* 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);
216+
editorCallback: (editor: Editor) => {
217+
this.insertCurrentDate(
218+
this.settings$.getDefaultCalendar(),
219+
editor,
220+
this.api
221+
);
205222
},
206223
});
207224

@@ -210,6 +227,7 @@ export default class Calendarium extends Plugin {
210227
name: "Set Current Date to Next",
211228
callback: () => {
212229
const defaultCal = this.settings$.getDefaultCalendar();
230+
if (!defaultCal) return;
213231
const store = this.getStoreByCalendar(defaultCal);
214232
this.changeDay(defaultCal, store, incrementDay);
215233
},
@@ -220,6 +238,7 @@ export default class Calendarium extends Plugin {
220238
name: "Set Current Date to Previous",
221239
callback: () => {
222240
const defaultCal = this.settings$.getDefaultCalendar();
241+
if (!defaultCal) return;
223242
const store = this.getStoreByCalendar(defaultCal);
224243
this.changeDay(defaultCal, store, decrementDay);
225244
},
@@ -248,10 +267,19 @@ export default class Calendarium extends Plugin {
248267
return leaf;
249268
}
250269

251-
insertCurrentDate(calendar: Calendar, editor: Editor, api: API) {
270+
insertCurrentDate(
271+
calendar: Calendar | undefined,
272+
editor: Editor,
273+
api: API
274+
) {
275+
if (!calendar) return;
252276
const currentCalDate = calendar.current;
253277
const calendarApi = api.getAPI(calendar.name);
254-
const dateString = calendarApi.toDisplayDate(currentCalDate, calendar.dateFormat);
278+
const dateString = calendarApi.toDisplayDate(
279+
currentCalDate,
280+
null,
281+
calendar.dateFormat
282+
);
255283
const cursor = editor.getCursor();
256284
editor.replaceRange(dateString, cursor);
257285
cursor.ch += dateString.length;
@@ -261,9 +289,18 @@ export default class Calendarium extends Plugin {
261289
changeDay(
262290
calendar: Calendar,
263291
store: CalendarStore,
264-
changeFunction: (date: CalDate, yearCalculator: YearStoreCache, config: StaticCalendarData) => CalDate) {
292+
changeFunction: (
293+
date: CalDate,
294+
yearCalculator: YearStoreCache,
295+
config: StaticCalendarData
296+
) => CalDate
297+
) {
265298
const currentCalDate = calendar.current;
266-
const newDay = changeFunction(currentCalDate, store.yearCalculator, store.staticStore.staticData);
299+
const newDay = changeFunction(
300+
currentCalDate,
301+
store.yearCalculator,
302+
get(store.staticStore.staticData)
303+
);
267304
store.setCurrentDate(newDay);
268305
}
269306
}

0 commit comments

Comments
 (0)