import { DateTime } from "model/time" ; import moment from "moment" ; import type { Todo } from "./markdown" ; import { ReminderModel , TodoBasedReminderFormat } from "./reminder-base" ; import { escapeRegExpChars } from "./util" ; type KanbanSettingType = { dateTrigger : string ; dateFormat : string ; timeTrigger : string ; timeFormat : string ; linkDateToDailyNote : boolean ; } ; const kanbanSetting = new ( class KanbanSetting { get dateTrigger ( ) { return this . get ( "date-trigger" , "@" ) ; } get dateFormat ( ) { return this . get ( "date-format" , "YYYY-MM-DD" ) ; } get timeTrigger ( ) { return this . get ( "time-trigger" , "@@" ) ; } get timeFormat ( ) { return this . get ( "time-format" , "HH:mm" ) ; } get linkDateToDailyNote ( ) { return this . get ( "link-date-to-daily-note" , false ) ; } private get < E > ( key , defaultValue ) { if ( ! window ) { return defaultValue ; } const plugins = ( window as any ) ?. app ?. plugins ?. plugins ; if ( ! plugins ) { return defaultValue ; } const plugin = plugins [ "obsidian-kanban" ] ; if ( ! plugin ) { return defaultValue ; } const settings = plugin . settings ; if ( ! settings ) { return defaultValue ; } const value = plugin . settings [ key ] ; if ( value === null || value === undefined ) { return defaultValue ; } return value ; } } ) ; type KanbanSplitResult = { title : string ; time ? : DateTime ; } ; export class KanbanDateTimeFormat { static instance : KanbanDateTimeFormat = new KanbanDateTimeFormat ( kanbanSetting ) ; private dateRegExp : RegExp ; private timeRegExp : RegExp ; constructor ( private setting ) { let dateRegExpStr ; if ( setting . linkDateToDailyNote ) { dateRegExpStr = `${ escapeRegExpChars ( this . setting . dateTrigger ) }\\[\\[(?<date>.+?)\\]\\]` ; } else { dateRegExpStr = `${ escapeRegExpChars ( this . setting . dateTrigger ) }\\{(?<date>.+?)\\}` ; } const timeRegExpStr = `${ escapeRegExpChars ( this . setting . timeTrigger ) }\\{(?<time>.+?)\\}` ; this . dateRegExp = new RegExp ( dateRegExpStr ) ; this . timeRegExp = new RegExp ( timeRegExpStr ) ; } format ( time ) { let datePart ; if ( this . setting . linkDateToDailyNote ) { datePart = `${ this . setting . dateTrigger }[[${ time . format ( this . setting . dateFormat ) }]]` ; } else { datePart = `${ this . setting . dateTrigger }{${ time . format ( this . setting . dateFormat ) }}` ; } if ( ! time . hasTimePart ) { return datePart ; } return `${ datePart } ${ this . setting . timeTrigger }{${ time . format ( this . setting . timeFormat ) }}` ; } split ( text , strictDateFormat ? ) { const originalText = text ; let title ; let date ; let time ; const dateMatch = this . dateRegExp . exec ( text ) ; if ( dateMatch ) { date = dateMatch . groups ! [ "date" ] ! ; text = text . replace ( this . dateRegExp , "" ) ; } else { return { title : originalText } ; } const timeMatch = this . timeRegExp . exec ( text ) ; if ( timeMatch ) { time = timeMatch . groups ! [ "time" ] ! ; text = text . replace ( this . timeRegExp , "" ) ; } title = text . trim ( ) ; let parsedTime ; const strict = strictDateFormat ?? true ; if ( time ) { parsedTime = new DateTime ( moment ( `${ date } ${ time }` , `${ this . setting . dateFormat } ${ this . setting . timeFormat }` , strict ) , true ) ; } else { parsedTime = new DateTime ( moment ( date , this . setting . dateFormat , strict ) , false ) ; } if ( parsedTime . isValid ( ) ) { return { title , time : parsedTime } ; } return { title : originalText } ; } } export class KanbanReminderModel implements ReminderModel { static parse ( line , strictDateFormat ? ) { const splitted = KanbanDateTimeFormat . instance . split ( line , strictDateFormat ) ; if ( splitted . time == null ) { return null ; } return new KanbanReminderModel ( splitted . title , splitted . time ) ; } constructor ( public title , public time ) { } getTitle ( ) { return this . title . trim ( ) ; } getTime ( ) { if ( this . time ) { return this . time ; } return null ; } setTime ( time ) { this . time = time ; } setRawTime ( ) { return false ; } toMarkdown ( ) { return `${ this . title . trim ( ) } ${ KanbanDateTimeFormat . instance . format ( this . time ) }` ; } } export class KanbanReminderFormat extends TodoBasedReminderFormat < KanbanReminderModel > { public static readonly instance = new KanbanReminderFormat ( ) ; parseReminder ( todo ) { return KanbanReminderModel . parse ( todo . body , this . isStrictDateFormat ( ) ) ; } newReminder ( title , time ) { const parsed = new KanbanReminderModel ( title , time ) ; parsed . setTime ( time ) ; return parsed ; } }