import { App , TAbstractFile , TFile } from "obsidian" ; import { TextInputSuggest } from "./suggest" ; import { get_tfiles_from_folder } from "utils/Utils" ; import TemplaterPlugin from "main" ; import { errorWrapperSync } from "utils/Error" ; export enum FileSuggestMode { TemplateFiles , ScriptFiles } export class FileSuggest extends TextInputSuggest < TFile > { constructor ( public app , public inputEl , private plugin , private mode ) { super ( app , inputEl ) ; } get_folder ( mode ) { switch ( mode ) { case FileSuggestMode . TemplateFiles : return this . plugin . settings . templates_folder ; case FileSuggestMode . ScriptFiles : return this . plugin . settings . user_scripts_folder ; } } get_error_msg ( mode ) { switch ( mode ) { case FileSuggestMode . TemplateFiles : return `Templates folder doesn't exist` ; case FileSuggestMode . ScriptFiles : return `User Scripts folder doesn't exist` ; } } getSuggestions ( input_str ) { const all_files = errorWrapperSync ( ( ) => get_tfiles_from_folder ( this . app , this . get_folder ( this . mode ) ) , this . get_error_msg ( this . mode ) ) ; if ( ! all_files ) { return [ ] ; } const files = [ ] ; const lower_input_str = input_str . toLowerCase ( ) ; all_files . forEach ( ( file ) => { if ( file instanceof TFile && file . extension === "md" && file . path . toLowerCase ( ) . contains ( lower_input_str ) ) { files . push ( file ) ; } } ) ; return files ; } renderSuggestion ( file , el ) { el . setText ( file . path ) ; } selectSuggestion ( file ) { this . inputEl . value = file . path ; this . inputEl . trigger ( "input" ) ; this . close ( ) ; } }