Repository navigation
Expand file tree
/
Copy pathmain.ts
More file actions
83 lines (67 loc) · 2.07 KB
/
Copy pathmain.ts
File metadata and controls
83 lines (67 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { Notice, Plugin, Platform } from 'obsidian';
import * as fs from 'fs';
import * as path from 'path';
import * as os from 'os';
interface VaultSelectorSettings {
// Add settings here if needed in the future
}
const DEFAULT_SETTINGS: VaultSelectorSettings = {
// Add default settings here if needed in the future
}
export default class VaultSelectorPlugin extends Plugin {
settings: VaultSelectorSettings;
async onload() {
await this.loadSettings();
// Add window unload handler
window.addEventListener('beforeunload', () => {
this.handleUnload();
});
}
async onunload() {
await this.handleUnload();
}
private getObsidianConfigPath(): string {
let configPath: string;
if (Platform.isWin) {
configPath = path.join(process.env.APPDATA || '', 'obsidian', 'obsidian.json');
} else if (Platform.isMacOS) {
configPath = path.join(os.homedir(), 'Library', 'Application Support', 'obsidian', 'obsidian.json');
} else if (Platform.isLinux) {
configPath = path.join(os.homedir(), '.config', 'obsidian', 'obsidian.json');
} else {
throw new Error('Unsupported platform');
}
return configPath;
}
private async handleUnload() {
try {
const configPath = this.getObsidianConfigPath();
// Check if file exists
if (!fs.existsSync(configPath)) {
return;
}
// Read the current config
const configContent = fs.readFileSync(configPath, 'utf8');
const config = JSON.parse(configContent);
// Remove "open": true from each vault
if (config.vaults) {
Object.keys(config.vaults).forEach(vaultId => {
if (config.vaults[vaultId].open) {
delete config.vaults[vaultId].open;
}
});
}
// Write the modified config back
fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
new Notice('Successfully updated vault configuration');
} catch (error) {
new Notice('Failed to update vault configuration: ' + error.message);
}
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}