Skip to content

Commit 645bae4

Browse files
author
Lukas Petry
committed
Merge branch 'feature-20' into release
2 parents 50708ee + 18f6fdf commit 645bae4

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
@@ -19,6 +19,7 @@ import { OpenerSetting } from './types';
1919
export default class Opener extends Plugin {
2020
settings: OpenerSetting;
2121
isMetaKeyHeld: boolean | null = null;
22+
sameTabOnce: boolean = false;
2223
uninstallMonkeyPatchOpenFile: () => void;
2324
uninstallMonkeyPatchOpenLinkText: () => void;
2425

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

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

46132
onunload(): void {
47133
this.uninstallMonkeyPatchOpenFile && this.uninstallMonkeyPatchOpenFile();
@@ -142,6 +228,12 @@ export default class Opener extends Plugin {
142228
return;
143229
}
144230

231+
if (parentThis.sameTabOnce) {
232+
parentThis.sameTabOnce = false;
233+
oldopenFile && oldopenFile.apply(this, [file, openState]);
234+
return;
235+
}
236+
145237
else if (parentThis.settings.newTab && !sameFile) {
146238
// console.log("not same file");
147239
// else if already open in another tab, switch to that tab

0 commit comments

Comments
 (0)