1- import { EventRef , Notice , Platform , Plugin , TAbstractFile , TFile , FileSystemAdapter } from "obsidian" ;
1+ import { 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 , FileStatus } from "./git" ;
3+ import { getChangedFiles , commitAll , push , pull , getFileStatuses , getConflictFiles , markConflictsResolved , revertAll , FileStatus } from "./git" ;
44import { renderTemplate } from "./template" ;
55import { t } from "./i18n" ;
66
@@ -16,10 +16,12 @@ export default class AutoGitPlugin extends Plugin {
1616 private conflictFiles : Set < string > = new Set ( ) ;
1717 private _hasConflicts = false ;
1818 private resolveConflictCommand : { id : string } | null = null ;
19+ private ribbonIconEl : HTMLElement | null = null ;
1920
2021 async onload ( ) {
2122 await this . loadSettings ( ) ;
2223 this . addSettingTab ( new AutoGitSettingTab ( this . app , this ) ) ;
24+ this . updateRibbonButton ( ) ;
2325
2426 this . addCommand ( {
2527 id : "commit-now" ,
@@ -70,6 +72,10 @@ export default class AutoGitPlugin extends Plugin {
7072 if ( this . statusRefreshInterval ) {
7173 window . clearInterval ( this . statusRefreshInterval ) ;
7274 }
75+ if ( this . ribbonIconEl ) {
76+ this . ribbonIconEl . remove ( ) ;
77+ this . ribbonIconEl = null ;
78+ }
7379 }
7480
7581 async loadSettings ( ) {
@@ -230,6 +236,59 @@ export default class AutoGitPlugin extends Plugin {
230236 }
231237 }
232238
239+ updateRibbonButton ( ) {
240+ if ( this . settings . showRibbonButton && ! Platform . isMobileApp ) {
241+ if ( ! this . ribbonIconEl ) {
242+ this . ribbonIconEl = this . addRibbonIcon ( "git-branch" , "Git" , ( evt ) => {
243+ this . showRibbonMenu ( evt ) ;
244+ } ) ;
245+ }
246+ } else if ( this . ribbonIconEl ) {
247+ this . ribbonIconEl . remove ( ) ;
248+ this . ribbonIconEl = null ;
249+ }
250+ }
251+
252+ private showRibbonMenu ( evt : MouseEvent ) {
253+ const i18n = t ( ) ;
254+ const menu = new Menu ( ) ;
255+
256+ menu . addItem ( ( item ) =>
257+ item . setTitle ( i18n . ribbonMenuPull ) . setIcon ( "download" ) . onClick ( ( ) => void this . doPull ( ) )
258+ ) ;
259+ menu . addItem ( ( item ) =>
260+ item . setTitle ( i18n . ribbonMenuCommit ) . setIcon ( "check" ) . onClick ( ( ) => void this . runCommit ( "manual" ) )
261+ ) ;
262+ menu . addItem ( ( item ) =>
263+ item . setTitle ( i18n . ribbonMenuPush ) . setIcon ( "upload" ) . onClick ( ( ) => void this . doPush ( ) )
264+ ) ;
265+ menu . addItem ( ( item ) =>
266+ item . setTitle ( i18n . ribbonMenuCommitAndPush ) . setIcon ( "upload" ) . onClick ( async ( ) => {
267+ const committed = await this . runCommit ( "manual" ) ;
268+ if ( committed ) {
269+ await this . doPush ( ) ;
270+ }
271+ } )
272+ ) ;
273+ menu . addSeparator ( ) ;
274+ menu . addItem ( ( item ) =>
275+ item . setTitle ( i18n . ribbonMenuRevertAll ) . setIcon ( "rotate-ccw" ) . onClick ( ( ) => void this . doRevert ( ) )
276+ ) ;
277+
278+ menu . showAtMouseEvent ( evt ) ;
279+ }
280+
281+ private async doRevert ( ) {
282+ try {
283+ const cwd = this . getVaultPath ( ) ;
284+ await revertAll ( cwd , this . settings . gitPath ) ;
285+ new Notice ( t ( ) . noticeReverted ) ;
286+ this . refreshStatusBadges ( ) ;
287+ } catch ( e ) {
288+ new Notice ( t ( ) . noticeRevertFailed ( ( e as Error ) . message ) ) ;
289+ }
290+ }
291+
233292 private async doPull ( ) {
234293 if ( Platform . isMobileApp ) {
235294 new Notice ( t ( ) . noticeMobileNotSupported ) ;
0 commit comments