Skip to content

Commit e992199

Browse files
committed
feat: commit changes with specified message (close #26)
1 parent 71d2a59 commit e992199

File tree

1 file changed

+38
-6
lines changed

1 file changed

+38
-6
lines changed

main.ts

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { spawnSync } from "child_process";
2-
import { FileSystemAdapter, Notice, Plugin, PluginSettingTab, Setting } from "obsidian";
2+
import { FileSystemAdapter, Notice, Plugin, PluginSettingTab, Setting, SuggestModal } from "obsidian";
33
import simpleGit, { CheckRepoActions, FileStatusResult, SimpleGit } from "simple-git";
44

55
enum PluginState {
@@ -59,7 +59,15 @@ export default class ObsidianGit extends Plugin {
5959
name: "Commit *all* changes and push to remote repository",
6060
callback: () => this.createBackup()
6161
});
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+
6269
this.init();
70+
6371
// init statusBar
6472
let statusBarEl = this.addStatusBarItem();
6573
this.statusBar = new StatusBar(statusBarEl, this);
@@ -133,8 +141,7 @@ export default class ObsidianGit extends Plugin {
133141
this.setState(PluginState.idle);
134142
}
135143

136-
async createBackup(): Promise<void> {
137-
144+
async createBackup(commitMessage?: string): Promise<void> {
138145
if (!this.gitReady) {
139146
await this.init();
140147
}
@@ -149,7 +156,7 @@ export default class ObsidianGit extends Plugin {
149156

150157
if (changedFiles.length !== 0) {
151158
await this.add();
152-
await this.commit();
159+
await this.commit(commitMessage);
153160
this.lastUpdate = Date.now();
154161
this.displayMessage(`Committed ${changedFiles.length} files`);
155162
} else {
@@ -210,9 +217,9 @@ export default class ObsidianGit extends Plugin {
210217
);
211218
}
212219

213-
async commit(): Promise<void> {
220+
async commit(message?: string): Promise<void> {
214221
this.setState(PluginState.commit);
215-
const commitMessage = await this.formatCommitMessage(this.settings.commitMessage);
222+
const commitMessage = message ?? await this.formatCommitMessage(this.settings.commitMessage);
216223
await this.git.commit(commitMessage);
217224
}
218225

@@ -548,3 +555,28 @@ class StatusBar {
548555
}
549556
}
550557
}
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

Comments
 (0)