Skip to content

Commit 18f6fdf

Browse files
author
Lukas Petry
committed
✨ Add same tab features
1 parent 451bcee commit 18f6fdf

1 file changed

Lines changed: 92 additions & 0 deletions

File tree

main.ts

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { OpenerSetting } from './types';
1818

1919
export default class Opener extends Plugin {
2020
settings: OpenerSetting;
21+
sameTabOnce: boolean = false;
2122
uninstallMonkeyPatchOpenFile: () => void;
2223
uninstallMonkeyPatchOpenLinkText: () => void;
2324

@@ -28,6 +29,73 @@ export default class Opener extends Plugin {
2829
this.addSettingTab(new OpenerSettingTab(this.app, this));
2930
this.monkeyPatchopenFile();
3031
this.monkeyPatchopenLinkText();
32+
this.addCommands();
33+
this.addMenuItem();
34+
}
35+
36+
addCommands() {
37+
this.addCommand({
38+
id: "same-tab-once",
39+
name: "Open next file in same tab (Obsidian default behavior)",
40+
checkCallback: (checking: boolean) => {
41+
if (checking) {
42+
return this.settings.newTab;
43+
}
44+
this.sameTabOnce = true;
45+
new Notice("Next file will open in same tab");
46+
}
47+
});
48+
49+
this.addCommand({
50+
id: "enable-new-tab",
51+
name: "Enable new tab for all files",
52+
checkCallback: (checking: boolean) => {
53+
if (checking) {
54+
return !this.settings.newTab;
55+
}
56+
this.settings.newTab = true;
57+
this.saveSettings();
58+
new Notice("Opener: New tab for all files enabled");
59+
},
60+
});
61+
this.addCommand({
62+
id: "disable-new-tab",
63+
name: "Disable new tab for all files",
64+
checkCallback: (checking: boolean) => {
65+
if (checking) {
66+
return this.settings.newTab;
67+
}
68+
this.settings.newTab = false;
69+
this.saveSettings();
70+
new Notice("Opener: New tab for all files disabled");
71+
}
72+
});
73+
74+
this.addCommand({
75+
id: "enable-pdf",
76+
name: "Enable open all PDFs with default app",
77+
checkCallback: (checking: boolean) => {
78+
if (checking) {
79+
return !this.settings.PDFApp;
80+
}
81+
this.settings.PDFApp = true;
82+
this.saveSettings();
83+
new Notice("Opener: Open all PDFs with default app enabled");
84+
}
85+
});
86+
this.addCommand({
87+
id: "disable-pdf",
88+
name: "Disable open all PDFs with default app",
89+
checkCallback: (checking: boolean) => {
90+
if (checking) {
91+
return this.settings.PDFApp;
92+
}
93+
this.settings.PDFApp = false;
94+
this.saveSettings();
95+
new Notice("Opener: Open all PDFs with default app disabled");
96+
}
97+
});
98+
3199
this.addCommand({
32100
id: "open-graph-view-in-new-tab",
33101
name: "Open Graph View in new tab",
@@ -40,6 +108,24 @@ export default class Opener extends Plugin {
40108
});
41109
}
42110

111+
// add command to right-click menu
112+
addMenuItem() {
113+
this.registerEvent(
114+
this.app.workspace.on("file-menu", (menu, file, source, leaf) => {
115+
if (file instanceof TFile) {
116+
menu.addItem((item) => {
117+
item.setSection("open");
118+
item.setTitle("Open in same tab")
119+
.onClick(() => {
120+
this.sameTabOnce = true;
121+
this.app.workspace.getLeaf().openFile(file);
122+
});
123+
});
124+
}
125+
})
126+
);
127+
}
128+
43129

44130
onunload(): void {
45131
this.uninstallMonkeyPatchOpenFile && this.uninstallMonkeyPatchOpenFile();
@@ -88,6 +174,12 @@ export default class Opener extends Plugin {
88174
return;
89175
}
90176

177+
if (parentThis.sameTabOnce) {
178+
parentThis.sameTabOnce = false;
179+
oldopenFile && oldopenFile.apply(this, [file, openState]);
180+
return;
181+
}
182+
91183
else if (parentThis.settings.newTab && !sameFile) {
92184
// console.log("not same file");
93185
// else if already open in another tab, switch to that tab

0 commit comments

Comments
 (0)