Skip to content

Commit 2c463ca

Browse files
committed
Fix changing sync config settings
1 parent 8e65f27 commit 2c463ca

File tree

3 files changed

+82
-9
lines changed

3 files changed

+82
-9
lines changed

src/events-listener.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { Vault, TAbstractFile, TFolder, TFile } from "obsidian";
1+
import { Vault, TAbstractFile, TFolder } from "obsidian";
22
import MetadataStore from "./metadata-store";
3+
import { GitHubSyncSettings } from "./settings/settings";
34

45
/**
56
* Tracks changes to local sync directory and updates files metadata.
@@ -8,8 +9,7 @@ export default class EventsListener {
89
constructor(
910
private vault: Vault,
1011
private metadataStore: MetadataStore,
11-
private localContentDir: string,
12-
private repoContentDir: string,
12+
private settings: GitHubSyncSettings,
1313
) {}
1414

1515
start() {
@@ -39,8 +39,11 @@ export default class EventsListener {
3939
}
4040

4141
let remotePath: string;
42-
if (file.path.startsWith(this.localContentDir)) {
43-
remotePath = file.path.replace(this.localContentDir, this.repoContentDir);
42+
if (file.path.startsWith(this.settings.localContentDir)) {
43+
remotePath = file.path.replace(
44+
this.settings.localContentDir,
45+
this.settings.repoContentDir,
46+
);
4447
} else if (
4548
file.path.startsWith(`${this.vault.configDir}/github-sync-metadata.json`)
4649
) {
@@ -129,8 +132,9 @@ export default class EventsListener {
129132

130133
private isSyncable(filePath: string) {
131134
return (
132-
filePath.startsWith(this.localContentDir) ||
133-
filePath.startsWith(`${this.vault.configDir}/github-sync-metadata.json`)
135+
filePath.startsWith(this.settings.localContentDir) ||
136+
filePath === `${this.vault.configDir}/github-sync-metadata.json` ||
137+
(this.settings.syncConfigDir && filePath.startsWith(this.vault.configDir))
134138
);
135139
}
136140
}

src/settings/tab.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,11 @@ export default class GitHubSyncSettingsTab extends PluginSettingTab {
189189
.setValue(this.plugin.settings.syncConfigDir)
190190
.onChange(async (value) => {
191191
this.plugin.settings.syncConfigDir = value;
192+
if (value) {
193+
await this.plugin.syncManager.addConfigDirToMetadata();
194+
} else {
195+
await this.plugin.syncManager.removeConfigDirFromMetadata();
196+
}
192197
await this.plugin.saveSettings();
193198
});
194199
});

src/sync-manager.ts

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ export default class SyncManager {
3939
this.eventsListener = new EventsListener(
4040
this.vault,
4141
this.metadataStore,
42-
this.settings.localContentDir,
43-
this.settings.repoContentDir,
42+
this.settings,
4443
);
4544
}
4645

@@ -660,6 +659,71 @@ export default class SyncManager {
660659
}
661660
}
662661

662+
/**
663+
* Add all the files in the config dir in the metadata store.
664+
* This is mainly useful when the user changes the sync config settings
665+
* as we need to add those files to the metadata store or they would never be synced.
666+
*/
667+
async addConfigDirToMetadata() {
668+
// Get all the files in the config dir
669+
let files = [];
670+
let folders = [this.vault.configDir];
671+
while (folders.length > 0) {
672+
const folder = folders.pop();
673+
if (folder === undefined) {
674+
continue;
675+
}
676+
const res = await this.vault.adapter.list(folder);
677+
files.push(...res.files);
678+
folders.push(...res.folders);
679+
}
680+
// Add them to the metadata store
681+
files.forEach((filePath: string) => {
682+
this.metadataStore.data.files[filePath] = {
683+
localPath: filePath,
684+
// Remote path is the same for config dir files
685+
remotePath: filePath,
686+
sha: null,
687+
dirty: false,
688+
justDownloaded: false,
689+
lastModified: Date.now(),
690+
};
691+
});
692+
this.metadataStore.save();
693+
}
694+
695+
/**
696+
* Remove all the files in the config dir from the metadata store.
697+
* The metadata file is not removed as it must always be present.
698+
* This is mainly useful when the user changes the sync config settings
699+
* as we need to remove those files to the metadata store or they would
700+
* keep being synced.
701+
*/
702+
async removeConfigDirFromMetadata() {
703+
// Get all the files in the config dir
704+
let files = [];
705+
let folders = [this.vault.configDir];
706+
while (folders.length > 0) {
707+
const folder = folders.pop();
708+
if (folder === undefined) {
709+
continue;
710+
}
711+
const res = await this.vault.adapter.list(folder);
712+
files.push(...res.files);
713+
folders.push(...res.folders);
714+
}
715+
716+
// Remove all them from the metadata store
717+
files.forEach((filePath: string) => {
718+
if (filePath === `${this.vault.configDir}/github-sync-metadata.json`) {
719+
// We don't want to remove the metadata file even if it's in the config dir
720+
return;
721+
}
722+
delete this.metadataStore.data.files[filePath];
723+
});
724+
this.metadataStore.save();
725+
}
726+
663727
getFileMetadata(filePath: string): FileMetadata {
664728
return this.metadataStore.data.files[filePath];
665729
}

0 commit comments

Comments
 (0)