Skip to content

Commit

Permalink
fix: save localstorage per vault
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinzent03 committed Sep 20, 2022
1 parent 86b4d5a commit a3c4e4f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 25 deletions.
62 changes: 37 additions & 25 deletions src/localStorageSettings.ts
Original file line number Diff line number Diff line change
@@ -1,72 +1,84 @@
import ObsidianGit from "./main";

export class LocalStorageSettings {
private prefix: string;
constructor(private readonly plugin: ObsidianGit) {
this.prefix = this.plugin.manifest.id;
this.prefix = this.plugin.manifest.id + ":";
}

migrate(): void {
const keys = ["password", "hostname", "conflict", "lastAutoPull", "lastAutoBackup", "lastAutoPush", "gitPath", "pluginDisabled"];
for (const key of keys) {
const old = app.loadLocalStorage(this.prefix + key);
if (app.loadLocalStorage(key) == null && old != null) {
if (old != null) {
app.saveLocalStorage(this.prefix + key, old);
localStorage.removeItem(this.prefix + key);
}
}
}
}

getPassword(): string {
return localStorage.getItem(this.prefix + ":password");
getPassword(): string | null {
return app.loadLocalStorage(this.prefix + "password");
}

setPassword(value: string): void {
return localStorage.setItem(this.prefix + ":password", value);
return app.saveLocalStorage(this.prefix + "password", value);
}

getHostname(): string {
return localStorage.getItem(this.prefix + ":hostname");
getHostname(): string | null {
return app.loadLocalStorage(this.prefix + "hostname");
}

setHostname(value: string): void {
return localStorage.setItem(this.prefix + ":hostname", value);
return app.saveLocalStorage(this.prefix + "hostname", value);
}

getConflict(): string {
return localStorage.getItem(this.prefix + ":conflict");
getConflict(): string | null {
return app.loadLocalStorage(this.prefix + "conflict");
}

setConflict(value: string): void {
return localStorage.setItem(this.prefix + ":conflict", value);
return app.saveLocalStorage(this.prefix + "conflict", value);
}

getLastAutoPull(): string {
return localStorage.getItem(this.prefix + ":lastAutoPull");
getLastAutoPull(): string | null {
return app.loadLocalStorage(this.prefix + "lastAutoPull");
}

setLastAutoPull(value: string): void {
return localStorage.setItem(this.prefix + ":lastAutoPull", value);
return app.saveLocalStorage(this.prefix + "lastAutoPull", value);
}

getLastAutoBackup(): string {
return localStorage.getItem(this.prefix + ":lastAutoBackup");
getLastAutoBackup(): string | null {
return app.loadLocalStorage(this.prefix + "lastAutoBackup");
}

setLastAutoBackup(value: string): void {
return localStorage.setItem(this.prefix + ":lastAutoBackup", value);
return app.saveLocalStorage(this.prefix + "lastAutoBackup", value);
}

getLastAutoPush(): string {
return localStorage.getItem(this.prefix + ":lastAutoPush");
getLastAutoPush(): string | null {
return app.loadLocalStorage(this.prefix + "lastAutoPush");
}

setLastAutoPush(value: string): void {
return localStorage.setItem(this.prefix + ":lastAutoPush", value);
return app.saveLocalStorage(this.prefix + "lastAutoPush", value);
}

getGitPath(): string {
return localStorage.getItem(this.prefix + ":gitPath");
getGitPath(): string | null {
return app.loadLocalStorage(this.prefix + "gitPath");
}

setGitPath(value: string): void {
return localStorage.setItem(this.prefix + ":gitPath", value);
return app.saveLocalStorage(this.prefix + "gitPath", value);
}

getPluginDisabled(): boolean {
return localStorage.getItem(this.prefix + ":pluginDisabled") == "true";
return app.loadLocalStorage(this.prefix + "pluginDisabled") == "true";
}

setPluginDisabled(value: boolean): void {
return localStorage.setItem(this.prefix + ":pluginDisabled", `${value}`);
return app.saveLocalStorage(this.prefix + "pluginDisabled", `${value}`);
}
}
2 changes: 2 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,11 @@ export default class ObsidianGit extends Plugin {
console.log('loading ' + this.manifest.name + " plugin");
this.localStorage = new LocalStorageSettings(this);

this.localStorage.migrate();
await this.loadSettings();
this.migrateSettings();


this.addSettingTab(new ObsidianGitSettingsTab(this.app, this));

if (!this.localStorage.getPluginDisabled()) {
Expand Down
7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,10 @@ export enum FileType {
changed,
pulled,
}

declare module "obsidian" {
interface App {
loadLocalStorage(key: string): string | null;
saveLocalStorage(key: string, value: string | undefined): void;
}
}

0 comments on commit a3c4e4f

Please sign in to comment.