11import { EventRef , Menu , Notice , Platform , Plugin , TAbstractFile , TFile , FileSystemAdapter } from "obsidian" ;
22import { AutoGitSettings , AutoGitSettingTab , DEFAULT_SETTINGS } from "./settings" ;
3- import { getChangedFiles , commitAll , push , pull , getFileStatuses , getConflictFiles , markConflictsResolved , revertAll , FileStatus } from "./git" ;
3+ import { getChangedFiles , commitAll , push , pull , getFileStatuses , getConflictFiles , markConflictsResolved , revertAll , FileStatus , getChangedFilesSync , commitAndPushSync } from "./git" ;
44import { renderTemplate } from "./template" ;
55import { t } from "./i18n" ;
66
@@ -17,6 +17,7 @@ export default class AutoGitPlugin extends Plugin {
1717 private _hasConflicts = false ;
1818 private resolveConflictCommand : { id : string } | null = null ;
1919 private ribbonIconEl : HTMLElement | null = null ;
20+ private beforeUnloadHandler : ( ( ) => void ) | null = null ;
2021
2122 async onload ( ) {
2223 await this . loadSettings ( ) ;
@@ -61,6 +62,33 @@ export default class AutoGitPlugin extends Plugin {
6162 window . setTimeout ( ( ) => { void this . doPull ( ) ; } , 1000 ) ;
6263 }
6364
65+ // Setup beforeunload handler for commit on close
66+ if ( ! Platform . isMobileApp ) {
67+ this . beforeUnloadHandler = ( ) => {
68+ if ( this . settings . commitOnClose ) {
69+ const cwd = this . getVaultPathSafe ( ) ;
70+ if ( cwd ) {
71+ const changedFiles = getChangedFilesSync ( cwd , this . settings . gitPath ) ;
72+ if ( changedFiles . length > 0 ) {
73+ const now = new Date ( ) ;
74+ const subject = renderTemplate ( this . settings . commitTemplate , {
75+ date : now . toISOString ( ) . slice ( 0 , 10 ) ,
76+ time : now . toTimeString ( ) . slice ( 0 , 8 ) ,
77+ files : changedFiles . slice ( 0 , 5 ) . join ( ", " ) + ( changedFiles . length > 5 ? "..." : "" ) ,
78+ count : String ( changedFiles . length ) ,
79+ } ) ;
80+ let message = subject ;
81+ if ( this . settings . includeFileList ) {
82+ message += "\n\n" + changedFiles . join ( "\n" ) ;
83+ }
84+ commitAndPushSync ( cwd , this . settings . gitPath , message ) ;
85+ }
86+ }
87+ }
88+ } ;
89+ window . addEventListener ( "beforeunload" , this . beforeUnloadHandler ) ;
90+ }
91+
6492 // Check for existing conflicts on load
6593 void this . checkConflicts ( ) ;
6694 }
@@ -76,6 +104,10 @@ export default class AutoGitPlugin extends Plugin {
76104 this . ribbonIconEl . remove ( ) ;
77105 this . ribbonIconEl = null ;
78106 }
107+ if ( this . beforeUnloadHandler ) {
108+ window . removeEventListener ( "beforeunload" , this . beforeUnloadHandler ) ;
109+ this . beforeUnloadHandler = null ;
110+ }
79111 }
80112
81113 async loadSettings ( ) {
0 commit comments