Skip to content

Commit

Permalink
fix: open file with custom base path
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinzent03 committed Jun 9, 2022
1 parent 57108b7 commit 8a11666
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 25 deletions.
32 changes: 23 additions & 9 deletions src/simpleGit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,22 @@ export class SimpleGit extends GitManager {
return {
changed: status.files.filter((e) => e.working_dir !== " ").map((e) => {
const res = this.formatPath(e);
e.path = res.path;
e.from = res.from;
e.working_dir = e.working_dir === "?" ? "U" : e.working_dir;
return e;

return <FileStatusResult>{
path: res.path,
from: res.from,
working_dir: e.working_dir === "?" ? "U" : e.working_dir,
vault_path: this.getVaultPath(res.path),
};
}),
staged: status.files.filter((e) => e.index !== " " && e.index != "?").map((e) => {
const res = this.formatPath(e, e.index === "R");
e.path = res.path;
e.from = res.from;
return e;
return <FileStatusResult>{
path: res.path,
from: res.from,
index: e.index,
vault_path: this.getVaultPath(res.path),
};
}),
conflicted: status.conflicted.map((e) => this.formatPath({
path: e,
Expand All @@ -70,6 +76,14 @@ export class SimpleGit extends GitManager {
};
}

getVaultPath(path: string): String {
if (this.plugin.settings.basePath) {
return this.plugin.settings.basePath + "/" + path;
} else {
return path;
}
}

//Remove wrong `"` like "My file.md"
formatPath(path: simple.FileStatusResult, renamed: boolean = false): { path: string, from?: string; } {
function format(path?: string): string {
Expand Down Expand Up @@ -105,7 +119,7 @@ export class SimpleGit extends GitManager {
if (!(args.contains("submodule") && args.contains("foreach"))) return;

let body = "";
let root = (this.app.vault.adapter as FileSystemAdapter).getBasePath() + (this.plugin.settings.basePath ? sep + this.plugin.settings.basePath : "");
let root = (this.app.vault.adapter as FileSystemAdapter).getBasePath() + (this.plugin.settings.basePath ? "/" + this.plugin.settings.basePath : "");
stdout.on('data', (chunk) => {
body += chunk.toString('utf8');
});
Expand All @@ -116,7 +130,7 @@ export class SimpleGit extends GitManager {
submods = submods.map(i => {
let submod = i.match(/'([^']*)'/);
if (submod != undefined) {
return root + sep + submod[1] + sep;
return root + "/" + submod[1] + sep;
}
});

Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export interface Status {
}
export interface FileStatusResult {
path: string;
vault_path: string;
from?: string;
index: string;
working_dir: string;
Expand Down
10 changes: 5 additions & 5 deletions src/ui/modals/changedFilesModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class ChangedFilesModal extends FuzzySuggestModal<FileStatusResult> {

getItemText(item: FileStatusResult): string {
if (item.index == "?" && item.working_dir == "U") {
return `Untracked | ${item.path}`;
return `Untracked | ${item.vault_path}`;
}

let working_dir = "";
Expand All @@ -28,14 +28,14 @@ export class ChangedFilesModal extends FuzzySuggestModal<FileStatusResult> {
if (item.working_dir != " ") working_dir = `Working dir: ${item.working_dir} `;
if (item.index != " ") index = `Index: ${item.index}`;

return `${working_dir}${index} | ${item.path}`;
return `${working_dir}${index} | ${item.vault_path}`;
}

onChooseItem(item: FileStatusResult, _: MouseEvent | KeyboardEvent): void {
if (this.plugin.app.metadataCache.getFirstLinkpathDest(item.path, "") == null) {
(this.app as any).openWithDefaultApp(item.path);
if (this.plugin.app.metadataCache.getFirstLinkpathDest(item.vault_path, "") == null) {
(this.app as any).openWithDefaultApp(item.vault_path);
} else {
this.plugin.app.workspace.openLinkText(item.path, "/");
this.plugin.app.workspace.openLinkText(item.vault_path, "/");
}
}
}
14 changes: 7 additions & 7 deletions src/ui/sidebar/components/fileComponent.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
hoverPreview(
event,
view as any,
change.path.split("/").last().replace(".md", "")
change.vault_path.split("/").last().replace(".md", "")
);
}
}
Expand All @@ -40,7 +40,7 @@
change.working_dir === "D"
)
) {
openOrSwitch(view.app as any, change.path, event);
openOrSwitch(view.app as any, change.vault_path, event);
}
}
Expand Down Expand Up @@ -79,12 +79,12 @@
function discard() {
const deleteFile = change.working_dir == "U";
new DiscardModal(view.app, deleteFile, change.path)
new DiscardModal(view.app, deleteFile, change.vault_path)
.myOpen()
.then((shouldDiscard) => {
if (shouldDiscard === true) {
if (deleteFile) {
view.app.vault.adapter.remove(change.path).finally(() => {
view.app.vault.adapter.remove(change.vault_path).finally(() => {
dispatchEvent(new CustomEvent("git-refresh"));
});
} else {
Expand All @@ -102,14 +102,14 @@
<span
class="path"
aria-label-position={side}
aria-label={change.path.split("/").last() != change.path ? change.path : ""}
aria-label={change.vault_path.split("/").last() != change.vault_path ? change.vault_path : ""}
on:click|self={showDiff}
>
{change.path.split("/").last().replace(".md", "")}
{change.vault_path.split("/").last().replace(".md", "")}
</span>
<div class="tools">
<div class="buttons">
{#if view.app.vault.getAbstractFileByPath(change.path)}
{#if view.app.vault.getAbstractFileByPath(change.vault_path)}
<div
data-icon="go-to-file"
aria-label="Open File"
Expand Down
9 changes: 5 additions & 4 deletions src/ui/sidebar/components/stagedFileComponent.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script lang="ts">
import { setIcon } from "obsidian";
import { hoverPreview, openOrSwitch } from "obsidian-community-lib";
import { format } from "path";
import { DIFF_VIEW_CONFIG } from "src/constants";
import { GitManager } from "src/gitManager";
import { FileStatusResult } from "src/types";
Expand All @@ -10,7 +11,7 @@
export let view: GitView;
export let manager: GitManager;
let buttons: HTMLElement[] = [];
$: formattedPath = change.path;
$: formattedPath = change.vault_path;
$: side = (view.leaf.getRoot() as any).side == "left" ? "right" : "left";
setImmediate(() =>
Expand Down Expand Up @@ -73,7 +74,7 @@
}
function unstage() {
manager.unstage(formattedPath).finally(() => {
manager.unstage(change.path).finally(() => {
dispatchEvent(new CustomEvent("git-refresh"));
});
}
Expand All @@ -83,14 +84,14 @@
<span
class="path"
aria-label-position={side}
aria-label={change.path.split("/").last() != change.path ? change.path : ""}
aria-label={formattedPath.split("/").last() != formattedPath ? formattedPath : ""}
on:click={showDiff}
>
{formattedPath.split("/").last().replace(".md", "")}
</span>
<div class="tools">
<div class="buttons">
{#if view.app.vault.getAbstractFileByPath(change.path)}
{#if view.app.vault.getAbstractFileByPath(formattedPath)}
<div
data-icon="go-to-file"
aria-label="Open File"
Expand Down

0 comments on commit 8a11666

Please sign in to comment.