Skip to content

Commit af9987e

Browse files
committed
notice: add progress notice for git operations
- Add ProgressNotice class with loading animation - Show spinning indicator during pull/push/commit - Transition to success/fail state when complete - Add i18n strings for progress messages
1 parent 7ae68ce commit af9987e

4 files changed

Lines changed: 66 additions & 18 deletions

File tree

main.js

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/i18n.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ type Translations = {
114114
conflictStatusDesc: string;
115115
resolveConflictButton: string;
116116

117+
// Notices - progress
118+
noticePulling: string;
119+
noticePushing: string;
120+
noticeCommitting: string;
121+
117122
// Notices
118123
noticeNoChanges: string;
119124
noticeCommitted: (count: number) => string;
@@ -242,6 +247,10 @@ const en: Translations = {
242247
conflictStatusDesc: "Please resolve conflicts manually, then click the button below.",
243248
resolveConflictButton: "Mark as resolved",
244249

250+
noticePulling: "GitAutoCommit: Pulling...",
251+
noticePushing: "GitAutoCommit: Pushing...",
252+
noticeCommitting: "GitAutoCommit: Committing...",
253+
245254
noticeNoChanges: "GitAutoCommit: No changes to commit.",
246255
noticeCommitted: (count) => `GitAutoCommit: Committed ${count} file(s).`,
247256
noticePushed: "GitAutoCommit: Pushed to remote.",
@@ -369,6 +378,10 @@ const zhCN: Translations = {
369378
conflictStatusDesc: "请手动解决冲突后,点击下方按钮。",
370379
resolveConflictButton: "标记为已解决",
371380

381+
noticePulling: "GitAutoCommit: 正在拉取...",
382+
noticePushing: "GitAutoCommit: 正在推送...",
383+
noticeCommitting: "GitAutoCommit: 正在提交...",
384+
372385
noticeNoChanges: "GitAutoCommit: 没有可提交的更改。",
373386
noticeCommitted: (count) => `GitAutoCommit: 已提交 ${count} 个文件。`,
374387
noticePushed: "GitAutoCommit: 已推送到远程仓库。",

src/main.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { renderTemplate } from "./template";
55
import { t } from "./i18n";
66
import { RevertConfirmModal } from "./modals";
77
import { GitStatusBadgeManager } from "./statusBadges";
8+
import { ProgressNotice } from "./notice";
89

910
export default class AutoGitPlugin extends Plugin {
1011
settings: AutoGitSettings = DEFAULT_SETTINGS;
@@ -235,6 +236,8 @@ export default class AutoGitPlugin extends Plugin {
235236
return false;
236237
}
237238

239+
const progress = new ProgressNotice(t().noticeCommitting);
240+
238241
const now = new Date();
239242
const subject = renderTemplate(this.settings.commitTemplate, {
240243
date: now.toISOString().slice(0, 10),
@@ -251,17 +254,22 @@ export default class AutoGitPlugin extends Plugin {
251254
message = subject + "\n\n" + fileList;
252255
}
253256

254-
await commitAll(cwd, gitPath, message);
255-
committed = true;
256-
new Notice(t().noticeCommitted(changedFiles.length));
257+
try {
258+
await commitAll(cwd, gitPath, message);
259+
committed = true;
260+
progress.succeed(t().noticeCommitted(changedFiles.length));
261+
} catch (e) {
262+
progress.fail(t().noticeAutoGitError((e as Error).message));
263+
throw e;
264+
}
257265

258266
if (this.settings.autoPush) {
259267
await this.doPush();
260268
}
261269

262270
void this.statusBadges?.refresh();
263-
} catch (e) {
264-
new Notice(t().noticeAutoGitError((e as Error).message));
271+
} catch {
272+
// Error already handled in progress.fail
265273
} finally {
266274
this.isCommitting = false;
267275
if (this.pendingRerun) {
@@ -273,12 +281,13 @@ export default class AutoGitPlugin extends Plugin {
273281
}
274282

275283
async doPush() {
284+
const progress = new ProgressNotice(t().noticePushing);
276285
try {
277286
const cwd = this.getVaultPath();
278287
await push(cwd, this.settings.gitPath);
279-
new Notice(t().noticePushed);
288+
progress.succeed(t().noticePushed);
280289
} catch (e) {
281-
new Notice(t().noticePushFailed((e as Error).message));
290+
progress.fail(t().noticePushFailed((e as Error).message));
282291
}
283292
}
284293

@@ -389,19 +398,20 @@ export default class AutoGitPlugin extends Plugin {
389398
return;
390399
}
391400

401+
const progress = new ProgressNotice(t().noticePulling);
392402
try {
393403
const cwd = this.getVaultPath();
394404
const result = await pull(cwd, this.settings.gitPath);
395405

396406
if (result.hasConflicts) {
397407
await this.checkConflicts();
398-
new Notice(t().noticeConflictDetected);
408+
progress.fail(t().noticeConflictDetected);
399409
} else if (result.success) {
400-
new Notice(t().noticePulled);
410+
progress.succeed(t().noticePulled);
401411
void this.statusBadges?.refresh();
402412
}
403413
} catch (e) {
404-
new Notice(t().noticePullFailed((e as Error).message));
414+
progress.fail(t().noticePullFailed((e as Error).message));
405415
}
406416
}
407417

src/notice.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { Notice } from "obsidian";
2+
3+
/** Notice with loading spinner that transitions to success/fail state */
4+
export class ProgressNotice {
5+
private notice: Notice;
6+
7+
constructor(message: string) {
8+
this.notice = new Notice(message, 0);
9+
this.notice.messageEl.parentElement?.addClass("is-loading");
10+
}
11+
12+
succeed(message: string, timeout = 3000) {
13+
const el = this.notice.messageEl.parentElement;
14+
el?.removeClass("is-loading");
15+
el?.addClass("mod-success");
16+
this.notice.setMessage(message);
17+
window.setTimeout(() => this.notice.hide(), timeout);
18+
}
19+
20+
fail(message: string, timeout = 5000) {
21+
this.notice.messageEl.parentElement?.removeClass("is-loading");
22+
this.notice.setMessage(message);
23+
window.setTimeout(() => this.notice.hide(), timeout);
24+
}
25+
}

0 commit comments

Comments
 (0)