generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
55 lines (45 loc) · 1.98 KB
/
main.ts
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
import { Notice, Plugin, TextFileView, WorkspaceLeaf } from 'obsidian';
export default class RecentTabSwitcher extends Plugin {
private previousLeaf: WorkspaceLeaf | null = null;
private currentLeaf: WorkspaceLeaf | null = null;
async onload() {
console.log("RecentTabSwitcher Plugin loaded");
this.registerEvent(
this.app.workspace.on('active-leaf-change', () => {
// console.log("Active leaf changed");
this.updatePreviousLeaf();
})
);
// console.log("Event listener for 'active-leaf-change' registered");
this.updatePreviousLeaf();
this.addCommand({
id: 'switch-to-previous-leaf',
name: 'Switch to Previous Leaf',
callback: () => this.switchToPreviousLeaf()
});
// console.log("Command for switching to previous leaf registered");
}
private updatePreviousLeaf() {
const newLeaf = this.app.workspace.activeLeaf; // couldn't find a suitable
// console.log("Updating Previous Leaf. New Leaf:", newLeaf);
if (newLeaf && newLeaf !== this.currentLeaf) {
this.previousLeaf = this.currentLeaf;
this.currentLeaf = newLeaf;
// console.log("Previous Leaf updated to:", this.previousLeaf);
// console.log("Current Leaf updated to:", this.currentLeaf);
}
}
private switchToPreviousLeaf() {
const activeLeaf = this.app.workspace.activeLeaf;
// console.log("Active Leaf:", activeLeaf);
if (activeLeaf) {
if (this.previousLeaf && this.previousLeaf !== activeLeaf) {
// console.log("Switching to Previous Leaf:", this.previousLeaf);
this.app.workspace.setActiveLeaf(this.previousLeaf); // Set the previous leaf as active
} else {
console.log("No previous tab to switch to.");
new Notice("No previous tab to switch to.");
}
}
}
}