1
+ import dayjs , { Dayjs } from 'dayjs' ;
1
2
import { App , MarkdownRenderer , Notice , Platform , Plugin , PluginSettingTab , Setting , TFile } from 'obsidian' ;
3
+
2
4
import { YesterdayMedia , ImageModal } from "./media"
3
5
import { YesterdayDialog } from "./dialogs"
4
6
import { mediaExtensions } from './constants' ;
@@ -9,14 +11,18 @@ interface YesterdaySettings {
9
11
showTodoCount : boolean ;
10
12
showMediaGrid : boolean ;
11
13
maximizeMedia : boolean ;
14
+ datePropFormat : string ;
15
+ startOfNextDay : number ;
12
16
}
13
17
14
18
const DEFAULT_SETTINGS : YesterdaySettings = {
15
19
colorMarkdownFiles : true ,
16
20
hideMediaFiles : false ,
17
21
showTodoCount : false ,
18
22
showMediaGrid : true ,
19
- maximizeMedia : true
23
+ maximizeMedia : true ,
24
+ datePropFormat : 'YYYY-MM-DD HH:mm:ss Z' ,
25
+ startOfNextDay : 5 ,
20
26
}
21
27
22
28
let todoCount = 0 ;
@@ -70,8 +76,8 @@ export default class Yesterday extends Plugin {
70
76
let target = event . target as HTMLElement ;
71
77
72
78
if (
73
- target . tagName === 'IMG' &&
74
- target . closest ( '.media-embed' ) &&
79
+ target . tagName === 'IMG' &&
80
+ target . closest ( '.media-embed' ) &&
75
81
this . settings . maximizeMedia
76
82
) {
77
83
let imgSrc = ( target as HTMLImageElement ) . src ;
@@ -205,7 +211,6 @@ export default class Yesterday extends Plugin {
205
211
todoEventListenersAdded : boolean = false ;
206
212
207
213
registerFileOperations ( ) {
208
-
209
214
if ( this . todoEventListenersAdded ) return
210
215
211
216
this . registerEvent (
@@ -255,27 +260,12 @@ export default class Yesterday extends Plugin {
255
260
}
256
261
257
262
async createEntry ( ) : Promise < void > {
258
-
259
- const now = new Date ( ) ;
260
-
261
- const year = now . getFullYear ( ) ;
262
- const month = ( "0" + ( now . getMonth ( ) + 1 ) ) . slice ( - 2 ) ;
263
- const day = ( "0" + now . getDate ( ) ) . slice ( - 2 ) ;
264
- const date = [ year , month , day ] . join ( "-" ) ;
265
-
266
- const hours = ( "0" + now . getHours ( ) ) . slice ( - 2 ) ;
267
- const minutes = ( "0" + now . getMinutes ( ) ) . slice ( - 2 ) ;
268
- const seconds = ( "0" + now . getSeconds ( ) ) . slice ( - 2 ) ;
269
- const time = [ hours , minutes , seconds ] . join ( "-" ) ;
270
-
271
- const timezoneOffsetNumber = now . getTimezoneOffset ( ) ;
272
- const timezoneOffset = ( ( timezoneOffsetNumber < 0 ? '+' : '-' ) + pad ( Math . abs ( timezoneOffsetNumber / 60 ) , 2 ) + ":" + pad ( Math . abs ( timezoneOffsetNumber % 60 ) , 2 ) ) ;
273
-
274
- const path = getPath ( ) ;
275
- const fileName = path + "/" + date + " - " + time + ".md" ;
276
-
263
+ const path = getPath ( this . settings . startOfNextDay ) ;
264
+ const now = dayjs ( ) ;
265
+ const fileName = path + "/" + now . format ( 'YYYY-MM-DD - HH-mm-ss' ) + ".md" ;
277
266
new Notice ( "Creating " + fileName ) ;
278
- const frontmatter = await createFrontmatter ( date + " " + hours + ":" + minutes + ":" + seconds + " " + timezoneOffset , this ) ;
267
+
268
+ const frontmatter = await createFrontmatter ( now . format ( this . settings . datePropFormat || DEFAULT_SETTINGS . datePropFormat ) , this ) ;
279
269
new Notice ( frontmatter ) ;
280
270
281
271
try {
@@ -349,59 +339,40 @@ export default class Yesterday extends Plugin {
349
339
}
350
340
}
351
341
352
- function getPath ( ) {
353
- const now = new Date ( ) ;
354
- if ( now . getHours ( ) < 5 ) {
355
- const yesterday = new Date ( )
356
- yesterday . setDate ( now . getDate ( ) - 1 ) ;
357
- return pathFromDate ( yesterday ) ;
342
+ function getPath ( startOfNextDay : number ) {
343
+ const now = dayjs ( ) ;
344
+ if ( now . hour ( ) < startOfNextDay ) {
345
+ return pathFromDate ( now . subtract ( 1 , 'day' ) ) ;
358
346
} else {
359
347
return pathFromDate ( now ) ;
360
348
}
361
349
}
362
350
363
- function pathFromDate ( date : Date ) {
351
+ function pathFromDate ( date : Dayjs ) {
364
352
const root = this . app . vault . getRoot ( ) . path ;
365
353
366
- const year = date . getFullYear ( ) ;
367
- const decade = year . toString ( ) . substring ( 0 , 3 ) + "0s" ;
368
-
369
- const month = ( "0" + ( date . getMonth ( ) + 1 ) ) . slice ( - 2 ) ;
370
- const day = ( "0" + date . getDate ( ) ) . slice ( - 2 ) ;
371
-
372
- const fullMonth = [ year , month ] . join ( "-" ) ;
373
- const fullDate = [ year , month , day ] . join ( "-" ) ;
374
-
375
- const components = [ decade , year , fullMonth , fullDate ] . join ( "/" ) ;
354
+ const components = [
355
+ date . year ( ) . toString ( ) . substring ( 0 , 3 ) + '0s' ,
356
+ date . format ( 'YYYY' ) ,
357
+ date . format ( 'YYYY-MM' ) ,
358
+ date . format ( 'YYYY-MM-DD' ) ,
359
+ ] . join ( "/" ) ;
376
360
377
361
return root + components ;
378
362
}
379
363
380
- async function createFrontmatter ( datetime : string , plugin : Yesterday ) : Promise < string > {
381
-
382
- return `---
383
- date: ${ datetime }
384
- ---
385
-
386
- `
364
+ async function createFrontmatter ( datetime : string , _plugin : Yesterday ) : Promise < string > {
365
+ return `---\ndate: ${ datetime } \n---\n\n`
387
366
}
388
367
389
368
async function runCommand ( command : string ) {
390
369
const util = require ( 'util' ) ;
391
370
const exec = util . promisify ( require ( 'child_process' ) . exec ) ;
392
- const { stdout, stderr } = await exec ( command ) ;
371
+ const { stdout } = await exec ( command ) ;
393
372
394
373
return stdout
395
374
}
396
375
397
- function pad ( number : number , length : number ) {
398
- let str = "" + number
399
- while ( str . length < length ) {
400
- str = '0' + str
401
- }
402
- return str
403
- }
404
-
405
376
class YesterdaySettingTab extends PluginSettingTab {
406
377
plugin : Yesterday ;
407
378
@@ -448,10 +419,23 @@ class YesterdaySettingTab extends PluginSettingTab {
448
419
this . plugin . setStatusBar ( ) ;
449
420
} ) ) ;
450
421
422
+ new Setting ( containerEl )
423
+ . setName ( 'Start of next day' )
424
+ . setDesc ( 'In hours after midnight' )
425
+ . addSlider ( toggle => toggle
426
+ . setLimits ( 0 , 23 , 1 )
427
+ . setValue ( this . plugin . settings . startOfNextDay )
428
+ . setDynamicTooltip ( )
429
+ . onChange ( async ( value ) => {
430
+ this . plugin . settings . startOfNextDay = value ;
431
+ await this . plugin . saveSettings ( ) ;
432
+ this . plugin . setStatusBar ( ) ;
433
+ } ) ) ;
434
+
451
435
containerEl . createEl ( 'br' ) ;
452
- const mediaSection = containerEl . createEl ( 'div' , { cls : 'setting-item setting-item-heading' } ) ;
453
- const mediaSectionInfo = mediaSection . createEl ( 'div' , { cls : 'setting-item-info' } ) ;
454
- mediaSectionInfo . createEl ( 'div' , { text : 'Media' , cls : 'setting-item-name' } ) ;
436
+ const mediaSection = containerEl . createEl ( 'div' , { cls : 'setting-item setting-item-heading' } ) ;
437
+ const mediaSectionInfo = mediaSection . createEl ( 'div' , { cls : 'setting-item-info' } ) ;
438
+ mediaSectionInfo . createEl ( 'div' , { text : 'Media' , cls : 'setting-item-name' } ) ;
455
439
456
440
new Setting ( containerEl )
457
441
. setName ( 'Show media files in a grid' )
@@ -473,5 +457,47 @@ class YesterdaySettingTab extends PluginSettingTab {
473
457
this . plugin . settings . maximizeMedia = value ;
474
458
await this . plugin . saveSettings ( ) ;
475
459
} ) ) ;
460
+
461
+ const timeFormatSection = containerEl . createEl ( 'div' , { cls : 'setting-item setting-item-heading' } ) ;
462
+ const timeFormatSectionInfo = timeFormatSection . createEl ( 'div' , { cls : 'setting-item-info' } ) ;
463
+ timeFormatSectionInfo . createEl ( 'div' , { text : 'Time format' , cls : 'setting-item-name' } ) ;
464
+
465
+ const timeFormatDescription = timeFormatSectionInfo . createEl ( 'div' , { cls : 'setting-item-description' } ) ;
466
+
467
+ const timeFormatText = createEl ( 'span' , {
468
+ text : 'If you change the time format your journal will not work with the '
469
+ } ) ;
470
+ timeFormatDescription . appendChild ( timeFormatText ) ;
471
+
472
+ const appLink = createEl ( 'a' , {
473
+ text : 'Yesterday app' ,
474
+ href : 'https://www.yesterday.md'
475
+ } ) ;
476
+ timeFormatText . appendChild ( appLink ) ;
477
+ timeFormatText . appendChild ( document . createTextNode ( '.' ) ) ;
478
+
479
+ const additionalText = createEl ( 'span' , {
480
+ text : ' See the '
481
+ } ) ;
482
+ timeFormatDescription . appendChild ( additionalText ) ;
483
+
484
+ const docLink = createEl ( 'a' , {
485
+ text : 'format documentation' ,
486
+ href : 'https://day.js.org/docs/en/display/format'
487
+ } ) ;
488
+ additionalText . appendChild ( docLink ) ;
489
+ additionalText . appendChild ( document . createTextNode ( ' for details.' ) ) ;
490
+
491
+ new Setting ( containerEl )
492
+ . setName ( 'Frontmatter \'date\'' )
493
+ . setDesc ( 'The format of the \'date\' property in the frontmatter of newly created entries' )
494
+ . addMomentFormat ( text => text . setPlaceholder ( DEFAULT_SETTINGS . datePropFormat )
495
+ . setValue ( ( this . plugin . settings . datePropFormat || '' ) + '' )
496
+ . setDefaultFormat ( DEFAULT_SETTINGS . datePropFormat )
497
+ . onChange ( async ( v ) => {
498
+ let value = v . trim ( )
499
+ this . plugin . settings . datePropFormat = value ;
500
+ await this . plugin . saveSettings ( ) ;
501
+ } ) ) ;
476
502
}
477
- }
503
+ }
0 commit comments