-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
101 lines (87 loc) · 2.85 KB
/
main.js
File metadata and controls
101 lines (87 loc) · 2.85 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
const fs = require("fs");
const path = require("path");
const os = require("os");
const obsidian = require("obsidian");
const DEFAULT_SETTINGS = {
path: ".obsidian-current-file.json",
};
module.exports = class CurrentFilePlugin extends obsidian.Plugin {
async onload() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
if (!this.app.isMobile) {
// navigation
this.registerEvent(
this.app.workspace.on(
"active-leaf-change",
this.writeCurrentFilename.bind(this)
)
);
// opening a new file
this.registerEvent(
this.app.workspace.on("file-open", this.writeCurrentFilename.bind(this))
);
// renaming a file (needed in case someone renames the current file they're editing!)
this.registerEvent(
this.app.vault.on("rename", this.writeCurrentFilename.bind(this))
);
this.addSettingTab(new CurrentFileSettingTab(this.app, this));
}
}
writeCurrentFilename() {
// get the vault path
let adapter = app.vault.adapter;
if (!adapter instanceof obsidian.FileSystemAdapter) {
// this isn't a filesystem adapter, so we can't get the path of the
// file we're viewing. Do not update the file
return;
}
const vaultPath = adapter.getBasePath();
// get the current active file path
const activeFile = this.app.workspace.getActiveFile();
if (!activeFile) {
// we're not looking at an active file, don't update the path of the
// file we're viewing
return;
}
const filePath = activeFile.path;
// create the output
const encodedJSON = JSON.stringify({
file: filePath,
vault: vaultPath,
fullpath: path.join(vaultPath, filePath),
});
// write the file out
// by using resolve instead of join, we can handle the case where the
// filename is a filename is an absolute path
const destFilename = path.resolve(os.homedir(), this.settings.path);
fs.writeFile(destFilename, encodedJSON, (err) => {
if (err) {
console.error("Error writing filename to file", err);
}
});
}
};
// Settings tab
class CurrentFileSettingTab extends obsidian.PluginSettingTab {
constructor(app, plugin) {
super(app, plugin);
this.plugin = plugin;
}
display() {
const { containerEl } = this;
containerEl.empty();
new obsidian.Setting(containerEl)
.setName("Path")
.setDesc(
"Where to save the current file information. " +
"If this is not an absolute path then it will be treated as " +
"relative to your home directory / profile directory."
)
.addText((text) =>
text.setValue(this.plugin.settings.path).onChange(async (value) => {
this.plugin.settings.path = value;
await this.plugin.saveData(this.plugin.settings);
})
);
}
}