import { TemplaterError } from "utils/Error" ; import { App , Modal } from "obsidian" ; export class PromptModal extends Modal { private promptEl : HTMLInputElement ; private resolve : ( value : string ) => void ; private reject : ( reason ? : TemplaterError ) => void ; private submitted = false ; constructor ( app , private prompt_text , private default_value ) { super ( app ) ; } onOpen ( ) { this . titleEl . setText ( this . prompt_text ) ; this . createForm ( ) ; } onClose ( ) { this . contentEl . empty ( ) ; if ( ! this . submitted ) { this . reject ( new TemplaterError ( "Cancelled prompt" ) ) ; } } createForm ( ) { const div = this . contentEl . createDiv ( ) ; div . addClass ( "templater-prompt-div" ) ; const form = div . createEl ( "form" ) ; form . addClass ( "templater-prompt-form" ) ; form . type = "submit" ; form . onsubmit = ( e ) => { this . submitted = true ; e . preventDefault ( ) ; this . resolve ( this . promptEl . value ) ; this . close ( ) ; } ; this . promptEl = form . createEl ( "input" ) ; this . promptEl . type = "text" ; this . promptEl . placeholder = "Type text here..." ; this . promptEl . value = this . default_value ?? "" ; this . promptEl . addClass ( "templater-prompt-input" ) ; this . promptEl . select ( ) ; } async openAndGetValue ( resolve , reject ) { this . resolve = resolve ; this . reject = reject ; this . open ( ) ; } }