import { Plugin , TAbstractFile , TFile } from 'obsidian' ; import { CommitProcessor } from './block' ; import { updateProjects , initializeFilesCheckpoint , getFileStats , updateFilesCheckpoint , SETTINGS , CommitsSettings } from './utils' ; import { CommitsSettingTab } from './settings' ; export default class CommitsPlugin extends Plugin { settings : CommitsSettings ; containerEl : HTMLElement ; async onload ( ) { await this . loadSettings ( ) ; console . log ( 'Loaded Commits Plugin' ) ; this . registerMarkdownCodeBlockProcessor ( 'commits-type' , async ( source , el ) => { const proc = new CommitProcessor ( ) ; await proc . run ( source , el , this . settings , 'type' ) ; } ) ; this . registerMarkdownCodeBlockProcessor ( 'commits-weekly' , async ( source , el ) => { const proc = new CommitProcessor ( ) ; await proc . run ( source , el , this . settings , 'weekly' ) ; } ) ; this . registerMarkdownCodeBlockProcessor ( 'commits-daily' , async ( source , el ) => { const proc = new CommitProcessor ( ) ; await proc . run ( source , el , this . settings , 'daily' ) ; } ) ; this . registerMarkdownCodeBlockProcessor ( 'commits-recents' , async ( source , el ) => { const proc = new CommitProcessor ( ) ; await proc . run ( source , el , this . settings , 'recents' ) ; } ) ; this . registerEvent ( this . app . workspace . on ( "layout-ready" , this . initialize . bind ( this ) ) ) ; this . registerEvent ( this . app . vault . on ( "delete" , this . commitsDelete . bind ( this ) ) ) ; this . registerEvent ( this . app . vault . on ( "rename" , this . commitsRename . bind ( this ) ) ) ; this . registerEvent ( this . app . vault . on ( 'create' , this . commitsCreate . bind ( this ) ) ) ; setInterval ( ( ) => { updateFilesCheckpoint ( this . app . vault . getMarkdownFiles ( ) , this . app . metadataCache , this . settings ) ; this . saveSettings ( ) ; } , 300000 ) ; this . addSettingTab ( new CommitsSettingTab ( this . app , this ) ) ; } onunload ( ) { console . log ( 'unloading Commits Plugin' ) ; } async loadSettings ( ) { this . settings = Object . assign ( { } , SETTINGS , await this . loadData ( ) ) ; } async saveSettings ( ) { await this . saveData ( this . settings ) ; } async commitsCreate ( file ) { if ( this . settings . initialized && file instanceof TFile && file . extension === 'md' && ! this . settings . filesCheckpoint [ file . path ] ) { let fileStats = getFileStats ( file , this . app . metadataCache ) ; this . settings . filesCheckpoint [ file . path ] = { size : fileStats [ 0 ] , tags : fileStats [ 1 ] , links : fileStats [ 2 ] } ; updateProjects ( file . path , this . settings , 'Create' , 'Created' , file . name ) ; } } async initialize ( ) { if ( ! this . settings . initialized ) { initializeFilesCheckpoint ( this . app . vault . getMarkdownFiles ( ) , this . app . metadataCache , this . settings ) ; this . settings . initialized = true ; await this . saveData ( this . settings ) ; } } async commitsDelete ( file ) { if ( this . settings . filesCheckpoint [ file . path ] ) { delete this . settings . filesCheckpoint [ file . path ] ; updateProjects ( file . path , this . settings , 'Refactor' , 'Deleted' , file . name ) ; await this . saveData ( this . settings ) ; } } async commitsRename ( file , oldPath ) { if ( this . settings . filesCheckpoint [ oldPath ] ) { let regex = new RegExp ( '.*Untitled.*' ) ; this . settings . filesCheckpoint [ file . path ] = this . settings . filesCheckpoint [ oldPath ] ; delete this . settings . filesCheckpoint [ oldPath ] ; if ( oldPath . match ( regex ) ) { return ; } updateProjects ( file . path , this . settings , 'Refactor' , 'Renamed' , file . name ) ; await this . saveData ( this . settings ) ; } } }