@@ -11,6 +11,8 @@ import { SettingsService } from "./settings/settings.service";
11
11
import {
12
12
type CalendarStore ,
13
13
createCalendarStore ,
14
+ incrementDay ,
15
+ decrementDay ,
14
16
} from "./stores/calendar.store" ;
15
17
import { CodeBlockService } from "./calendar/codeblock" ;
16
18
import { DEFAULT_FORMAT } from "./utils/constants" ;
@@ -193,6 +195,35 @@ export default class Calendarium extends Plugin {
193
195
this . addCalendarView ( ) ;
194
196
} ,
195
197
} ) ;
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
+ } ) ;
196
227
}
197
228
198
229
addCalendarView ( params : { full ?: boolean ; startup ?: boolean } = { } ) {
@@ -216,4 +247,23 @@ export default class Calendarium extends Plugin {
216
247
217
248
return leaf ;
218
249
}
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
+ }
219
269
}
0 commit comments