import { App , Command , Editor , MarkdownView , TFile , WorkspaceLeaf } from "obsidian" ; import { findLeafByFile } from "./leaf" ; class AbstractCommand { constructor ( protected command ) { } protected tryCheckCallback ( ) { if ( ! this . command . checkCallback ) { return false ; } const checkResult = this . command . checkCallback ( true ) ; if ( checkResult !== undefined && ! checkResult ) { console . info ( "The command is not available by checking: %o" , this . command ) ; return true ; } this . command . checkCallback ( false ) ; return true ; } protected tryCallback ( ) { if ( ! this . command . callback ) { return false ; } this . command . callback ( ) ; return true ; } protected tryEditorCheckCallback ( editor , view ) { if ( ! this . command . editorCheckCallback ) { return false ; } const checkResult = this . command . editorCheckCallback ( true , editor , view ) ; if ( checkResult !== undefined && ! checkResult ) { console . info ( "The editor command is not available by checking: %o" , this . command ) ; return true ; } this . command . editorCheckCallback ( false , editor , view ) ; } protected tryEditorCallback ( editor , view ) { if ( ! this . command . editorCallback ) { return false ; } this . command . editorCallback ( editor , view ) ; return true ; } } class RunnableCommand extends AbstractCommand { run ( ) { if ( this . tryCheckCallback ( ) ) { return ; } if ( this . tryCallback ( ) ) { return ; } console . warn ( "Expected command callback doens't exist: %o" , this . command ) ; } } class RunnableEditorCommand extends AbstractCommand { constructor ( private app , command ) { super ( command ) ; } runInCurrentLeaf ( ) { const leaf = this . app . workspace . activeLeaf ! ; this . runInLeaf ( leaf ) ; } async openAndRun ( file ) { const leaf = await findLeafByFile ( this . app , file , true ) ; this . runInLeaf ( leaf ! ) ; } runInLeaf ( leaf ) { if ( ! ( leaf . view instanceof MarkdownView ) ) { console . warn ( leaf . view ) ; throw `invalid state.  should be markdown view` ; } const markdownView = leaf . view ; this . run ( markdownView . editor , markdownView ) ; } run ( editor , view ) { if ( this . tryEditorCheckCallback ( editor , view ) ) { return ; } if ( this . tryEditorCallback ( editor , view ) ) { return ; } if ( this . tryCheckCallback ( ) ) { return ; } if ( this . tryCallback ( ) ) { return ; } } } export function findCommand ( app , id ) { const command = ( app as any ) . commands . commands [ id ] ; if ( command ) { return new RunnableCommand ( command as Command ) ; } return null ; } export function findEditorCommand ( app , id ) { const command = ( app as any ) . commands . commands [ id ] ; if ( command ) { return new RunnableEditorCommand ( app , command as Command ) ; } return null ; }