import type { Reminders } from "model/reminder" ; import type { DateTime } from "model/time" ; import { App , KeymapEventHandler , Modal , Modifier , Platform } from "obsidian" ; import DateTimeChooser from "./components/DateTimeChooser.svelte" ; class DateTimeChooserModal extends Modal { private selected ? : DateTime ; private handlers : KeymapEventHandler [ ] = [ ] ; constructor ( app , private reminders , private onSelect , private onCancel ) { super ( app ) ; } override onOpen ( ) { let targetElement ; if ( Platform . isDesktop ) { this . modalEl . style . minWidth = "0px" ; this . modalEl . style . minHeight = "0px" ; targetElement = this . contentEl ; } else { targetElement = this . containerEl ; } const chooser = new DateTimeChooser ( { target : targetElement , props : { onClick : ( time ) => { this . select ( time ) ; } , reminders : this . reminders , undefined } , } ) ; this . registerKeymap ( [ "Ctrl" ] , "P" , ( ) => chooser [ "moveUp" ] ( ) ) ; this . registerKeymap ( [ ] , "ArrowUp" , ( ) => chooser [ "moveUp" ] ( ) ) ; this . registerKeymap ( [ "Ctrl" ] , "N" , ( ) => chooser [ "moveDown" ] ( ) ) ; this . registerKeymap ( [ ] , "ArrowDown" , ( ) => chooser [ "moveDown" ] ( ) ) ; this . registerKeymap ( [ "Ctrl" ] , "B" , ( ) => chooser [ "moveLeft" ] ( ) ) ; this . registerKeymap ( [ ] , "ArrowLeft" , ( ) => chooser [ "moveLeft" ] ( ) ) ; this . registerKeymap ( [ "Ctrl" ] , "F" , ( ) => chooser [ "moveRight" ] ( ) ) ; this . registerKeymap ( [ ] , "ArrowRight" , ( ) => chooser [ "moveRight" ] ( ) ) ; this . registerKeymap ( [ ] , "Enter" , ( ) => this . select ( chooser [ "selection" ] ( ) ) ) ; this . registerKeymap ( [ ] , "Escape" , ( ) => this . close ( ) ) ; } private registerKeymap ( modifier , key , action ) { this . handlers . push ( this . scope . register ( modifier , key , ( ) : boolean | void => { action ( ) ; return false ; } ) ) ; } private select ( time ) { this . selected = time ; this . close ( ) ; } override onClose ( ) { this . handlers . forEach ( handler => this . scope . unregister ( handler ) ) ; if ( this . selected != null ) { this . onSelect ( this . selected ) ; } else { this . onCancel ( ) ; } } } export function showDateTimeChooserModal ( app , reminders ) { return new Promise ( ( resolve , reject ) => { const modal = new DateTimeChooserModal ( app , reminders , resolve , reject ) ; modal . open ( ) ; } ) ; }