1
- import { Platform , Plugin , WorkspaceLeaf , addIcon } from "obsidian" ;
1
+ import {
2
+ Editor ,
3
+ Platform ,
4
+ Plugin ,
5
+ WorkspaceLeaf ,
6
+ addIcon ,
7
+ } from "obsidian" ;
2
8
3
9
import CalendariumSettings from "./settings/settings.view" ;
4
10
5
- import type { Calendar , CalendariumData } from "./@types" ;
11
+ import type {
12
+ CalDate ,
13
+ Calendar ,
14
+ CalendariumData ,
15
+ StaticCalendarData ,
16
+ } from "./@types" ;
6
17
import CalendariumView from "./calendar/view" ;
7
18
8
19
import { Watcher } from "./watcher/watcher" ;
@@ -20,6 +31,8 @@ import { CalendariumNotice } from "./utils/notice";
20
31
21
32
import { ViewType } from "./calendar/view.types" ;
22
33
import { AgendaView } from "./agenda/view.agenda" ;
34
+ import type { YearStoreCache } from "./stores/years.store" ;
35
+ import { get } from "svelte/store" ;
23
36
24
37
declare global {
25
38
interface Window {
@@ -200,8 +213,12 @@ export default class Calendarium extends Plugin {
200
213
id : "insert-calendarium-current-date" ,
201
214
name : "Insert Current Date" ,
202
215
/* 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
+ ) ;
205
222
} ,
206
223
} ) ;
207
224
@@ -210,6 +227,7 @@ export default class Calendarium extends Plugin {
210
227
name : "Set Current Date to Next" ,
211
228
callback : ( ) => {
212
229
const defaultCal = this . settings$ . getDefaultCalendar ( ) ;
230
+ if ( ! defaultCal ) return ;
213
231
const store = this . getStoreByCalendar ( defaultCal ) ;
214
232
this . changeDay ( defaultCal , store , incrementDay ) ;
215
233
} ,
@@ -220,6 +238,7 @@ export default class Calendarium extends Plugin {
220
238
name : "Set Current Date to Previous" ,
221
239
callback : ( ) => {
222
240
const defaultCal = this . settings$ . getDefaultCalendar ( ) ;
241
+ if ( ! defaultCal ) return ;
223
242
const store = this . getStoreByCalendar ( defaultCal ) ;
224
243
this . changeDay ( defaultCal , store , decrementDay ) ;
225
244
} ,
@@ -248,10 +267,19 @@ export default class Calendarium extends Plugin {
248
267
return leaf ;
249
268
}
250
269
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 ;
252
276
const currentCalDate = calendar . current ;
253
277
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
+ ) ;
255
283
const cursor = editor . getCursor ( ) ;
256
284
editor . replaceRange ( dateString , cursor ) ;
257
285
cursor . ch += dateString . length ;
@@ -261,9 +289,18 @@ export default class Calendarium extends Plugin {
261
289
changeDay (
262
290
calendar : Calendar ,
263
291
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
+ ) {
265
298
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
+ ) ;
267
304
store . setCurrentDate ( newDay ) ;
268
305
}
269
306
}
0 commit comments