Skip to content

Commit

Permalink
fix: middle click to open file/diff in new tab
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinzent03 committed Sep 21, 2022
1 parent 7b2c1da commit ddb1164
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 28 deletions.
3 changes: 2 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,9 @@ export default class ObsidianGit extends Plugin {
const file = this.app.workspace.getActiveFile();
if (checking) {
return file !== null;

} else {
getNewLeaf().setViewState({ type: DIFF_VIEW_CONFIG.type, state: { staged: false, file: file!.path } });
getNewLeaf()?.setViewState({ type: DIFF_VIEW_CONFIG.type, state: { staged: false, file: file!.path } });
}
}

Expand Down
8 changes: 6 additions & 2 deletions src/ui/sidebar/components/fileComponent.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@
function open(event: MouseEvent) {
const file = view.app.vault.getAbstractFileByPath(change.vault_path);
console.log(event);
if (file instanceof TFile) {
getNewLeaf(event).openFile(file);
getNewLeaf(event)?.openFile(file);
}
}
Expand All @@ -47,7 +49,7 @@
}
function showDiff(event: MouseEvent) {
getNewLeaf(event).setViewState({
getNewLeaf(event)?.setViewState({
type: DIFF_VIEW_CONFIG.type,
active: true,
state: {
Expand Down Expand Up @@ -89,6 +91,7 @@
? change.vault_path
: ""}
on:click|self={showDiff}
on:auxclick|self={showDiff}
>
{change.vault_path.split("/").last()?.replace(".md", "")}
</span>
Expand All @@ -99,6 +102,7 @@
data-icon="go-to-file"
aria-label="Open File"
bind:this={buttons[1]}
on:auxclick={open}
on:click={open}
/>
{/if}
Expand Down
2 changes: 1 addition & 1 deletion src/ui/sidebar/components/pulledFileComponent.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
function open(event: MouseEvent) {
const file = view.app.vault.getAbstractFileByPath(change.vault_path);
if (file instanceof TFile) {
getNewLeaf(event).openFile(file);
getNewLeaf(event)?.openFile(file);
}
}
</script>
Expand Down
4 changes: 2 additions & 2 deletions src/ui/sidebar/components/stagedFileComponent.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@
function open(event: MouseEvent) {
const file = view.app.vault.getAbstractFileByPath(change.vault_path);
if (file instanceof TFile) {
getNewLeaf(event).openFile(file);
getNewLeaf(event)?.openFile(file);
}
}
function showDiff(event: MouseEvent) {
getNewLeaf(event).setViewState({
getNewLeaf(event)?.setViewState({
type: DIFF_VIEW_CONFIG.type,
active: true,
state: {
Expand Down
32 changes: 10 additions & 22 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { requireApiVersion, WorkspaceLeaf } from "obsidian";
import { Keymap, WorkspaceLeaf } from "obsidian";

export const worthWalking = (filepath: string, root?: string) => {
if (filepath === '.' || root == null || root.length === 0 || root === '.') {
Expand All @@ -12,28 +12,16 @@ export const worthWalking = (filepath: string, root?: string) => {
};


export function getNewLeaf(event?: MouseEvent | KeyboardEvent): WorkspaceLeaf {
let leaf: WorkspaceLeaf;
if (!event) {
leaf = app.workspace.getLeaf(false);
} else {
if (requireApiVersion("0.16.0")) {
if (event.ctrlKey && event.altKey && event.shiftKey) {
leaf = app.workspace.getLeaf("window");
} else if (event.ctrlKey && event.altKey) {
leaf = app.workspace.getLeaf("split");
} else if (event.ctrlKey) {
leaf = app.workspace.getLeaf("tab");
} else {
leaf = app.workspace.getLeaf(false);
}
} else {
if (event.ctrlKey) {
leaf = app.workspace.getLeaf(true);
} else {
leaf = app.workspace.getLeaf(false);
}
export function getNewLeaf(event?: MouseEvent): WorkspaceLeaf | undefined {
let leaf: WorkspaceLeaf | undefined;
if (event) {
if ((event.button === 0 || event.button === 1)) {
const type = Keymap.isModEvent(event);
leaf = app.workspace.getLeaf(type);
}
} else {
leaf = app.workspace.getLeaf(false);

}
return leaf;
}

0 comments on commit ddb1164

Please sign in to comment.