Skip to content

Commit

Permalink
feat: add context menu to git views
Browse files Browse the repository at this point in the history
close #615
  • Loading branch information
Vinzent03 committed Jul 11, 2024
1 parent f204f7c commit 115c4ba
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 69 deletions.
131 changes: 74 additions & 57 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,18 +298,7 @@ export default class ObsidianGit extends Plugin {
if (checking) {
return file !== null;
} else {
this.app.vault.adapter
.append(
this.gitManager.getRelativeVaultPath(".gitignore"),
"\n" +
this.gitManager.getRelativeRepoPath(
file!.path,
true
)
)
.then(() => {
this.refresh();
});
this.addFileToGitignore(file!);
}
},
});
Expand Down Expand Up @@ -625,55 +614,83 @@ export default class ObsidianGit extends Plugin {
}
}

async addFileToGitignore(file: TAbstractFile): Promise<void> {
await this.app.vault.adapter.append(
this.gitManager.getRelativeVaultPath(".gitignore"),
"\n" + this.gitManager.getRelativeRepoPath(file!.path, true)
);
this.refresh();
}

handleFileMenu(menu: Menu, file: TAbstractFile, source: string): void {
if (!this.settings.showFileMenu) return;
if (source !== "file-explorer-context-menu") {
return;
}
if (!file) {
return;
}
if (!this.gitReady) return;
menu.addItem((item) => {
item.setTitle(`Git: Stage`)
.setIcon("plus-circle")
.setSection("action")
.onClick((_) => {
this.promiseQueue.addTask(async () => {
if (file instanceof TFile) {
await this.gitManager.stage(file.path, true);
} else {
await this.gitManager.stageAll({
dir: this.gitManager.getRelativeRepoPath(
file.path,
true
),
});
}
this.displayMessage(`Staged ${file.path}`);
if (!this.settings.showFileMenu) return;
if (!file) return;

if (
this.settings.showFileMenu &&
source == "file-explorer-context-menu"
) {
menu.addItem((item) => {
item.setTitle(`Git: Stage`)
.setIcon("plus-circle")
.setSection("action")
.onClick((_) => {
this.promiseQueue.addTask(async () => {
if (file instanceof TFile) {
await this.gitManager.stage(file.path, true);
} else {
await this.gitManager.stageAll({
dir: this.gitManager.getRelativeRepoPath(
file.path,
true
),
});
}
this.displayMessage(`Staged ${file.path}`);
});
});
});
});
menu.addItem((item) => {
item.setTitle(`Git: Unstage`)
.setIcon("minus-circle")
.setSection("action")
.onClick((_) => {
this.promiseQueue.addTask(async () => {
if (file instanceof TFile) {
await this.gitManager.unstage(file.path, true);
} else {
await this.gitManager.unstageAll({
dir: this.gitManager.getRelativeRepoPath(
file.path,
true
),
});
}
this.displayMessage(`Unstaged ${file.path}`);
});
menu.addItem((item) => {
item.setTitle(`Git: Unstage`)
.setIcon("minus-circle")
.setSection("action")
.onClick((_) => {
this.promiseQueue.addTask(async () => {
if (file instanceof TFile) {
await this.gitManager.unstage(file.path, true);
} else {
await this.gitManager.unstageAll({
dir: this.gitManager.getRelativeRepoPath(
file.path,
true
),
});
}
this.displayMessage(`Unstaged ${file.path}`);
});
});
});
});
});
menu.addItem((item) => {
item.setTitle(`Git: Add to .gitignore`)
.setIcon("file-x")
.setSection("action")
.onClick((_) => {
this.addFileToGitignore(file);
});
});
}

if (source == "git-source-control") {
menu.addItem((item) => {
item.setTitle(`Git: Add to .gitignore`)
.setIcon("file-x")
.setSection("action")
.onClick((_) => {
this.addFileToGitignore(file);
});
});
}
}

async migrateSettings(): Promise<void> {
Expand Down
13 changes: 10 additions & 3 deletions src/ui/history/components/logFileComponent.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { setIcon, TFile } from "obsidian";
import { DIFF_VIEW_CONFIG } from "src/constants";
import type { DiffFile } from "src/types";
import { getDisplayPath, getNewLeaf } from "src/utils";
import { getDisplayPath, getNewLeaf, mayTriggerFileMenu } from "src/utils";
import type HistoryView from "../historyView";
export let diff: DiffFile;
Expand Down Expand Up @@ -42,7 +42,14 @@
<!-- svelte-ignore a11y-no-noninteractive-element-interactions -->
<main
on:click|stopPropagation={showDiff}
on:auxclick|stopPropagation={showDiff}
on:auxclick|stopPropagation={(event) =>
mayTriggerFileMenu(
view.app,
event,
diff.vault_path,
view.leaf,
"git-history"
)}
on:focus
class="tree-item nav-file"
>
Expand All @@ -59,7 +66,7 @@
</div>
<div class="git-tools">
<div class="buttons">
{#if view.app.vault.getAbstractFileByPath(diff.vault_path)}
{#if view.app.vault.getAbstractFileByPath(diff.vault_path) instanceof TFile}
<div
data-icon="go-to-file"
aria-label="Open File"
Expand Down
12 changes: 9 additions & 3 deletions src/ui/sourceControl/components/fileComponent.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import type { GitManager } from "src/gitManager/gitManager";
import type { FileStatusResult } from "src/types";
import { DiscardModal } from "src/ui/modals/discardModal";
import { getDisplayPath, getNewLeaf } from "src/utils";
import { getDisplayPath, getNewLeaf, mayTriggerFileMenu } from "src/utils";
import type GitView from "../sourceControl";
export let change: FileStatusResult;
Expand All @@ -28,7 +28,6 @@
function open(event: MouseEvent) {
const file = view.app.vault.getAbstractFileByPath(change.vault_path);
console.log(event);
if (file instanceof TFile) {
getNewLeaf(event)?.openFile(file);
Expand Down Expand Up @@ -82,7 +81,14 @@
<main
on:mouseover={hover}
on:click|stopPropagation={showDiff}
on:auxclick|stopPropagation={showDiff}
on:auxclick|stopPropagation={(event) =>
mayTriggerFileMenu(
view.app,
event,
change.vault_path,
view.leaf,
"git-source-control"
)}
on:focus
class="tree-item nav-file"
>
Expand Down
11 changes: 9 additions & 2 deletions src/ui/sourceControl/components/pulledFileComponent.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { TFile } from "obsidian";
import { hoverPreview } from "obsidian-community-lib";
import type { FileStatusResult } from "src/types";
import { getDisplayPath, getNewLeaf } from "src/utils";
import { getDisplayPath, getNewLeaf, mayTriggerFileMenu } from "src/utils";
import type GitView from "../sourceControl";
export let change: FileStatusResult;
Expand All @@ -29,7 +29,14 @@
<main
on:mouseover={hover}
on:click|stopPropagation={open}
on:auxclick|stopPropagation={open}
on:auxclick|stopPropagation={(event) =>
mayTriggerFileMenu(
view.app,
event,
change.vault_path,
view.leaf,
"git-source-control"
)}
on:focus
class="tree-item nav-file"
>
Expand Down
11 changes: 9 additions & 2 deletions src/ui/sourceControl/components/stagedFileComponent.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { DIFF_VIEW_CONFIG } from "src/constants";
import type { GitManager } from "src/gitManager/gitManager";
import type { FileStatusResult } from "src/types";
import { getDisplayPath, getNewLeaf } from "src/utils";
import { getDisplayPath, getNewLeaf, mayTriggerFileMenu } from "src/utils";
import type GitView from "../sourceControl";
export let change: FileStatusResult;
Expand Down Expand Up @@ -57,7 +57,14 @@
on:mouseover={hover}
on:focus
on:click|stopPropagation={showDiff}
on:auxclick|stopPropagation={showDiff}
on:auxclick|stopPropagation={(event) =>
mayTriggerFileMenu(
view.app,
event,
change.vault_path,
view.leaf,
"git-source-control"
)}
class="tree-item nav-file"
>
<div
Expand Down
9 changes: 9 additions & 0 deletions src/ui/sourceControl/components/treeComponent.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import FileComponent from "./fileComponent.svelte";
import PulledFileComponent from "./pulledFileComponent.svelte";
import StagedFileComponent from "./stagedFileComponent.svelte";
import { mayTriggerFileMenu } from "src/utils";
export let hierarchy: StatusRootTreeItem;
export let plugin: ObsidianGit;
export let view: GitView;
Expand Down Expand Up @@ -73,6 +74,14 @@
{:else}
<div
on:click|stopPropagation={() => fold(entity)}
on:auxclick|stopPropagation={(event) =>
mayTriggerFileMenu(
view.app,
event,
entity.vaultPath,
view.leaf,
"git-source-control"
)}
class="tree-item nav-folder"
class:is-collapsed={closed[entity.title]}
>
Expand Down
21 changes: 19 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as cssColorConverter from "css-color-converter";
import deepEqual from "deep-equal";
import type { RGB, WorkspaceLeaf } from "obsidian";
import { Keymap, moment } from "obsidian";
import type { App, RGB, WorkspaceLeaf } from "obsidian";
import { Keymap, Menu, moment } from "obsidian";

export const worthWalking = (filepath: string, root?: string) => {
if (filepath === "." || root == null || root.length === 0 || root === ".") {
Expand All @@ -27,6 +27,23 @@ export function getNewLeaf(event?: MouseEvent): WorkspaceLeaf | undefined {
return leaf;
}

export function mayTriggerFileMenu(
app: App,
event: MouseEvent,
filePath: string,
view: WorkspaceLeaf,
source: string
) {
if (event.button == 2) {
const file = app.vault.getAbstractFileByPath(filePath);
if (file != null) {
const fileMenu = new Menu();
app.workspace.trigger("file-menu", fileMenu, file, source, view);
fileMenu.showAtPosition({ x: event.pageX, y: event.pageY });
}
}
}

/**
* Creates a type-error, if this function is in a possible branch.
*
Expand Down

0 comments on commit 115c4ba

Please sign in to comment.