1
1
import { spawnSync } from "child_process" ;
2
- import { FileSystemAdapter , Notice , Plugin , PluginSettingTab , Setting } from "obsidian" ;
2
+ import { FileSystemAdapter , Notice , Plugin , PluginSettingTab , Setting , SuggestModal } from "obsidian" ;
3
3
import simpleGit , { CheckRepoActions , FileStatusResult , SimpleGit } from "simple-git" ;
4
4
5
5
enum PluginState {
@@ -59,7 +59,15 @@ export default class ObsidianGit extends Plugin {
59
59
name : "Commit *all* changes and push to remote repository" ,
60
60
callback : ( ) => this . createBackup ( )
61
61
} ) ;
62
+
63
+ this . addCommand ( {
64
+ id : "commit-push-specified-message" ,
65
+ name : "Commit and push all changes with specified message" ,
66
+ callback : ( ) => new CustomMessageModal ( this ) . open ( )
67
+ } ) ;
68
+
62
69
this . init ( ) ;
70
+
63
71
// init statusBar
64
72
let statusBarEl = this . addStatusBarItem ( ) ;
65
73
this . statusBar = new StatusBar ( statusBarEl , this ) ;
@@ -133,8 +141,7 @@ export default class ObsidianGit extends Plugin {
133
141
this . setState ( PluginState . idle ) ;
134
142
}
135
143
136
- async createBackup ( ) : Promise < void > {
137
-
144
+ async createBackup ( commitMessage ?: string ) : Promise < void > {
138
145
if ( ! this . gitReady ) {
139
146
await this . init ( ) ;
140
147
}
@@ -149,7 +156,7 @@ export default class ObsidianGit extends Plugin {
149
156
150
157
if ( changedFiles . length !== 0 ) {
151
158
await this . add ( ) ;
152
- await this . commit ( ) ;
159
+ await this . commit ( commitMessage ) ;
153
160
this . lastUpdate = Date . now ( ) ;
154
161
this . displayMessage ( `Committed ${ changedFiles . length } files` ) ;
155
162
} else {
@@ -210,9 +217,9 @@ export default class ObsidianGit extends Plugin {
210
217
) ;
211
218
}
212
219
213
- async commit ( ) : Promise < void > {
220
+ async commit ( message ?: string ) : Promise < void > {
214
221
this . setState ( PluginState . commit ) ;
215
- const commitMessage = await this . formatCommitMessage ( this . settings . commitMessage ) ;
222
+ const commitMessage = message ?? await this . formatCommitMessage ( this . settings . commitMessage ) ;
216
223
await this . git . commit ( commitMessage ) ;
217
224
}
218
225
@@ -548,3 +555,28 @@ class StatusBar {
548
555
}
549
556
}
550
557
}
558
+ class CustomMessageModal extends SuggestModal < string > {
559
+ plugin : ObsidianGit ;
560
+
561
+ constructor ( plugin : ObsidianGit ) {
562
+ super ( plugin . app ) ;
563
+ this . plugin = plugin ;
564
+ this . setPlaceholder ( "Type your message and select optional the version with the added date." ) ;
565
+ }
566
+
567
+
568
+ getSuggestions ( query : string ) : string [ ] {
569
+ const date = ( window as any ) . moment ( ) . format ( this . plugin . settings . commitDateFormat ) ;
570
+ if ( query == "" ) query = "..." ;
571
+ return [ query , `${ date } : ${ query } ` , `${ query } : ${ date } ` ] ;
572
+ }
573
+
574
+ renderSuggestion ( value : string , el : HTMLElement ) : void {
575
+ el . innerText = value ;
576
+ }
577
+
578
+ onChooseSuggestion ( item : string , _ : MouseEvent | KeyboardEvent ) : void {
579
+ this . plugin . createBackup ( item ) ;
580
+ }
581
+
582
+ }
0 commit comments