diff --git a/src/main.ts b/src/main.ts index b37395df..da5ba939 100644 --- a/src/main.ts +++ b/src/main.ts @@ -196,6 +196,15 @@ export default class ObsidianGit extends Plugin { } } + handleOnlineStatusChange() { + if (!this.state.offlineMode) return; + + if (navigator.onLine) { + this.setPluginState({ offlineMode: false }); + this.promiseQueue.addTask(() => this.pullChangesFromRemote()); + } + } + /** This method only registers events, views, commands and more. * * This only needs to be called once since the registered events are @@ -209,6 +218,7 @@ export default class ObsidianGit extends Plugin { this.refresh().catch((e) => this.displayError(e)); }) ); + this.registerEvent( this.app.workspace.on("obsidian-git:head-change", () => { this.refreshUpdatedHead(); @@ -226,6 +236,12 @@ export default class ObsidianGit extends Plugin { this.onActiveLeafChange(leaf); }) ); + + this.registerDomEvent(window, "online", () => { + this.handleOnlineStatusChange(); + } + ); + this.registerEvent( this.app.vault.on("modify", () => { this.debRefresh(); @@ -466,6 +482,12 @@ export default class ObsidianGit extends Plugin { return Platform.isDesktopApp; } + hasConnectivity() { + if (navigator.onLine) return true; + this.setPluginState({ offlineMode: true }); + return (new Notice('No Connectivity'), false) + } + async init({ fromReload = false }): Promise { if (this.settings.showStatusBar) { const statusBarEl = this.addStatusBarItem(); @@ -663,6 +685,7 @@ export default class ObsidianGit extends Plugin { ///Used for command async pullChangesFromRemote(): Promise { if (!(await this.isAllInitialized())) return; + if (!(this.hasConnectivity())) return; const filesUpdated = await this.pull(); await this.automaticsManager.setUpAutoCommitAndSync(); @@ -695,11 +718,13 @@ export default class ObsidianGit extends Plugin { commitMessage?: string ): Promise { if (!(await this.isAllInitialized())) return; + const isConnected = this.hasConnectivity(); if ( this.settings.syncMethod == "reset" && this.settings.pullBeforePush ) { + if (!isConnected) return; await this.pull(); } @@ -716,11 +741,13 @@ export default class ObsidianGit extends Plugin { this.settings.syncMethod != "reset" && this.settings.pullBeforePush ) { + if (!isConnected) return; await this.pull(); } if (!this.settings.disablePush) { // Prevent trying to push every time. Only if unpushed commits are present + if (!isConnected) return; if ( (await this.remotesAreSet()) && (await this.gitManager.canPush()) @@ -891,9 +918,9 @@ export default class ObsidianGit extends Plugin { */ async push(): Promise { if (!(await this.isAllInitialized())) return false; - if (!(await this.remotesAreSet())) { - return false; - } + if (!(await this.remotesAreSet())) return false; + if (!(this.hasConnectivity())) return false; + const hadConflict = this.localStorage.getConflict(); try { if (this.gitManager instanceof SimpleGit)