Add option to sync workspace files

This commit is contained in:
tiagoarroz 2026-05-08 12:09:41 +01:00
parent 27cbe81185
commit 570b2f846b
4 changed files with 63 additions and 16 deletions

View file

@ -135,16 +135,21 @@ export default class EventsListener {
}
}
private isWorkspaceFile(filePath: string): boolean {
return (
filePath === `${this.vault.configDir}/workspace.json` ||
filePath === `${this.vault.configDir}/workspace-mobile.json`
);
}
private isSyncable(filePath: string) {
if (filePath === `${this.vault.configDir}/${MANIFEST_FILE_NAME}`) {
// Manifest file must always be synced
return true;
} else if (
filePath === `${this.vault.configDir}/workspace.json` ||
filePath === `${this.vault.configDir}/workspace-mobile.json`
) {
// Obsidian recommends not syncing the workspace files
return false;
} else if (this.isWorkspaceFile(filePath)) {
// Workspace files are synced only when config directory sync is enabled
// and the dedicated workspace-files option is enabled too.
return this.settings.syncConfigDir && this.settings.syncWorkspaceFiles;
} else if (filePath === `${this.vault.configDir}/${LOG_FILE_NAME}`) {
// Don't sync the log file, doesn't make sense
return false;

View file

@ -8,6 +8,7 @@ export interface GitHubSyncSettings {
syncInterval: number;
syncOnStartup: boolean;
syncConfigDir: boolean;
syncWorkspaceFiles: boolean;
conflictHandling: "overwriteLocal" | "ask" | "overwriteRemote";
conflictViewMode: "default" | "unified" | "split";
showStatusBarItem: boolean;
@ -26,6 +27,7 @@ export const DEFAULT_SETTINGS: GitHubSyncSettings = {
syncInterval: 1,
syncOnStartup: false,
syncConfigDir: false,
syncWorkspaceFiles: false,
conflictHandling: "ask",
conflictViewMode: "default",
showStatusBarItem: true,

View file

@ -168,6 +168,23 @@ export default class GitHubSyncSettingsTab extends PluginSettingTab {
});
});
new Setting(containerEl)
.setName("Sync workspace files")
.setDesc("Sync workspace.json and workspace-mobile.json files")
.addToggle((toggle) => {
toggle
.setValue(this.plugin.settings.syncWorkspaceFiles)
.onChange(async (value) => {
this.plugin.settings.syncWorkspaceFiles = value;
if (value && this.plugin.settings.syncConfigDir) {
await this.plugin.syncManager.addConfigDirToMetadata();
} else if (!value) {
await this.plugin.syncManager.removeWorkspaceFilesFromMetadata();
}
await this.plugin.saveSettings();
});
});
const conflictHandlingOptions = {
overwriteLocal: "Overwrite local file",
ask: "Ask",

View file

@ -233,11 +233,8 @@ export default class SyncManager {
return;
}
if (targetPath === `${this.vault.configDir}/${LOG_FILE_NAME}`) {
// We don't want to download the log file if the user synced it in the past.
// This is necessary because in the past we forgot to ignore the log file
// from syncing if the user enabled configs sync.
// To avoid downloading it we ignore it if still present in the remote repo.
if (this.isVolatileSyncArtifact(targetPath)) {
await this.logger.info("Skipping volatile sync artifact", targetPath);
return;
}
@ -588,8 +585,23 @@ export default class SyncManager {
return filePath === `${this.vault.configDir}/${LOG_FILE_NAME}`;
}
private isWorkspaceFile(filePath: string): boolean {
return (
filePath === `${this.vault.configDir}/workspace.json` ||
filePath === `${this.vault.configDir}/workspace-mobile.json`
);
}
private shouldSyncWorkspaceFiles(): boolean {
return this.settings.syncConfigDir && this.settings.syncWorkspaceFiles;
}
private isVolatileSyncArtifact(filePath: string): boolean {
return this.isLogFile(filePath);
// Keep these files out of metadata when they should not be synced.
return (
this.isLogFile(filePath) ||
(this.isWorkspaceFile(filePath) && !this.shouldSyncWorkspaceFiles())
);
}
private filterRemoteMetadataFiles(filesMetadata: {
@ -1143,10 +1155,6 @@ export default class SyncManager {
folders.push(...res.folders);
}
files.forEach((filePath: string) => {
if (filePath === `${this.vault.configDir}/workspace.json`) {
// Obsidian recommends not syncing the workspace file
return;
}
if (this.isVolatileSyncArtifact(filePath)) {
return;
}
@ -1245,6 +1253,21 @@ export default class SyncManager {
this.metadataStore.save();
}
/**
* Removes workspace files from local metadata.
* Useful when the user disables workspace files synchronization.
*/
async removeWorkspaceFilesFromMetadata() {
await this.logger.info("Removing workspace files from metadata");
[
`${this.vault.configDir}/workspace.json`,
`${this.vault.configDir}/workspace-mobile.json`,
].forEach((filePath: string) => {
delete this.metadataStore.data.files[filePath];
});
this.metadataStore.save();
}
getFileMetadata(filePath: string): FileMetadata {
return this.metadataStore.data.files[filePath];
}