Skip to content

Commit

Permalink
feat: ✨ Add Diff View
Browse files Browse the repository at this point in the history
fix #158
  • Loading branch information
phibr0 committed Dec 27, 2021
1 parent ca59a2d commit 78cd43f
Show file tree
Hide file tree
Showing 11 changed files with 505 additions and 23 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"typescript": "4.3.5"
},
"dependencies": {
"diff2html": "^3.4.13",
"obsidian-community-lib": "^1.0.1",
"simple-git": "^2.21.0",
"supports-color": "^7.2.0"
Expand Down
10 changes: 8 additions & 2 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@ export const DEFAULT_SETTINGS: ObsidianGitSettings = {
mergeOnPull: true,
};

export const VIEW_CONFIG = {
export const GIT_VIEW_CONFIG = {
type: 'git-view',
name: 'Source Control',
icon: 'feather-git-pull-request'
};
};

export const DIFF_VIEW_CONFIG = {
type: 'diff-view',
name: 'Diff View',
icon: 'feather-git-pull-request'
}
2 changes: 2 additions & 0 deletions src/gitManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ export abstract class GitManager {

abstract updateGitPath(gitPath: string): void;

abstract getDiffString(filePath: string): string;

async formatCommitMessage(message?: string): Promise<string> {
let template = message ?? this.plugin.settings.commitMessage;
let status: Status | undefined;
Expand Down
32 changes: 23 additions & 9 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import { ObsidianGitSettingsTab } from "src/settings";
import { StatusBar } from "src/statusBar";
import { ChangedFilesModal } from "src/ui/modals/changedFilesModal";
import { CustomMessageModal } from "src/ui/modals/customMessageModal";
import { DEFAULT_SETTINGS, VIEW_CONFIG } from "./constants";
import { DEFAULT_SETTINGS, DIFF_VIEW_CONFIG, GIT_VIEW_CONFIG } from "./constants";
import { GitManager } from "./gitManager";
import openInGitHub from "./openInGitHub";
import { SimpleGit } from "./simpleGit";
import { ObsidianGitSettings, PluginState } from "./types";
import DiffView from "./ui/diff/diffView";
import addIcons from "./ui/icons";
import { GeneralModal } from "./ui/modals/generalModal";
import GitView from "./ui/sidebar/sidebarView";
Expand All @@ -36,13 +37,18 @@ export default class ObsidianGit extends Plugin {
async onload() {
console.log('loading ' + this.manifest.name + " plugin");
await this.loadSettings();

addIcons();

this.registerView(VIEW_CONFIG.type, (leaf) => {
this.registerView(GIT_VIEW_CONFIG.type, (leaf) => {
return new GitView(leaf, this);
});

(this.app.workspace as any).registerHoverLinkSource(VIEW_CONFIG.type, {
this.registerView(DIFF_VIEW_CONFIG.type, (leaf) => {
return new DiffView(leaf, this);
});

(this.app.workspace as any).registerHoverLinkSource(GIT_VIEW_CONFIG.type, {
display: 'Git View',
defaultMod: true,
});
Expand All @@ -53,19 +59,28 @@ export default class ObsidianGit extends Plugin {
id: 'open-git-view',
name: 'Open Source Control View',
callback: async () => {
if (this.app.workspace.getLeavesOfType(VIEW_CONFIG.type).length === 0) {
if (this.app.workspace.getLeavesOfType(GIT_VIEW_CONFIG.type).length === 0) {
await this.app.workspace.getRightLeaf(false).setViewState({
type: VIEW_CONFIG.type,
type: GIT_VIEW_CONFIG.type,
});
}
this.app.workspace.revealLeaf(this.app.workspace.getLeavesOfType(VIEW_CONFIG.type).first());
this.app.workspace.revealLeaf(this.app.workspace.getLeavesOfType(GIT_VIEW_CONFIG.type).first());
},
});

this.addCommand({
id: 'open-diff-view',
name: 'Open Diff View',
editorCallback: async (editor, view) => {
this.app.workspace.createLeafBySplit(view.leaf).setViewState({ type: DIFF_VIEW_CONFIG.type });
dispatchEvent(new CustomEvent('diff-update', { detail: { path: view.file.path } }));
},
});

this.addCommand({
id: 'view-file-in-github',
name: 'Open File in GitHub',
editorCallback: (editor, {file}) => openInGitHub(editor, file, this.gitManager),
editorCallback: (editor, { file }) => openInGitHub(editor, file, this.gitManager),
});

this.addCommand({
Expand Down Expand Up @@ -151,8 +166,7 @@ export default class ObsidianGit extends Plugin {
}

async onunload() {
(this.app.workspace as any).unregisterHoverLinkSource(VIEW_CONFIG.type);
this.app.workspace.detachLeavesOfType(VIEW_CONFIG.type);
(this.app.workspace as any).unregisterHoverLinkSource(GIT_VIEW_CONFIG.type);
this.clearAutoPull();
this.clearAutoBackup();
console.log('unloading ' + this.manifest.name + " plugin");
Expand Down
11 changes: 10 additions & 1 deletion src/simpleGit.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { spawnSync } from "child_process";
import { spawn, spawnSync } from "child_process";
import { FileSystemAdapter } from "obsidian";
import * as path from "path";
import simpleGit, * as simple from "simple-git";
Expand Down Expand Up @@ -299,6 +299,15 @@ export class SimpleGit extends GitManager {
this.setGitInstance();
}

getDiffString(filePath: string): string {
const command = spawnSync(this.plugin.settings.gitPath || 'git', ['diff', filePath], {
//@ts-ignore
cwd: this.plugin.app.vault.adapter.basePath
});
this.git.diffSummary()
return command.output.toString();
}

private isGitInstalled(): boolean {
// https://github.com/steveukx/git-js/issues/402
const command = spawnSync(this.plugin.settings.gitPath || 'git', ['--version'], {
Expand Down
76 changes: 76 additions & 0 deletions src/ui/diff/diffView.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { html } from "diff2html";
import { HoverParent, HoverPopover, ItemView, MarkdownView, WorkspaceLeaf } from "obsidian";
import { DIFF_VIEW_CONFIG } from "src/constants";
import ObsidianGit from "src/main";


export default class DiffView extends ItemView {

plugin: ObsidianGit;
filePath: string;
parser: DOMParser;

constructor(leaf: WorkspaceLeaf, plugin: ObsidianGit) {
super(leaf);
this.plugin = plugin;
this.parser = new DOMParser();

this.registerEvent(this.app.workspace.on('active-leaf-change', (leaf) => {
if (leaf.view instanceof MarkdownView) {
this.filePath = leaf.view.file.path;
} else {
this.filePath = null;
}
this.refresh();
}));
this.firstOpen = this.firstOpen.bind(this);
addEventListener('diff-update', this.firstOpen);
this.registerInterval(window.setInterval(() => this.refresh(), 10000));
}

firstOpen(event: CustomEvent) {
this.filePath = event.detail.path;
this.refresh();
}

getViewType(): string {
return DIFF_VIEW_CONFIG.type;
}

getDisplayText(): string {
return DIFF_VIEW_CONFIG.name;
}

getIcon(): string {
return DIFF_VIEW_CONFIG.icon;
}

onClose(): Promise<void> {
removeEventListener('diff-update', this.firstOpen)
return super.onClose();
}

onOpen(): Promise<void> {
this.refresh();
return super.onOpen();
}

refresh(): void {
if (this.filePath) {
this.contentEl.empty();
const diff = this.parser.parseFromString(
html(this.plugin.gitManager.getDiffString(this.filePath)),
'text/html')
.querySelector('.d2h-file-diff');
if (diff) {
this.contentEl.append(diff);
} else {
const div = this.contentEl.createDiv({ cls: 'diff-err' });
div.createSpan({ text: '⚠️', cls: 'diff-err-sign' });
div.createEl('br');
div.createSpan({ text: 'No changes to this file.' });
}
}
}

}
1 change: 1 addition & 0 deletions src/ui/icons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ export default function addIcons(): void {
addFeatherIcon('alert-circle');
addFeatherIcon('alert-triangle');
addFeatherIcon('git-commit');
addFeatherIcon('edit')
}
17 changes: 15 additions & 2 deletions src/ui/sidebar/components/fileComponent.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script lang="ts">
import { setIcon } from "obsidian";
import { setIcon, Workspace } from "obsidian";
import { hoverPreview, openOrSwitch } from "obsidian-community-lib";
import { DIFF_VIEW_CONFIG } from "src/constants";
import { GitManager } from "src/gitManager";
import { FileStatusResult } from "src/types";
import { DiscardModal } from "src/ui/modals/discardModal";
Expand All @@ -10,6 +11,7 @@
export let change: FileStatusResult;
export let view: GitView;
export let manager: GitManager;
export let workspace: Workspace;
let buttons: HTMLElement[] = [];
$: side = (view.leaf.getRoot() as any).side == "left" ? "right" : "left";
Expand Down Expand Up @@ -51,6 +53,11 @@
});
}
function showDiff() {
workspace.createLeafInParent(workspace.rootSplit, 0).setViewState({ type: DIFF_VIEW_CONFIG.type });
dispatchEvent(new CustomEvent('diff-update', { detail: { path: change.path } }));
}
function discard() {
const deleteFile = change.working_dir == "U";
new DiscardModal(view.app, deleteFile, change.path)
Expand Down Expand Up @@ -91,10 +98,16 @@
bind:this={buttons[0]}
on:click={discard}
/>
<div
data-icon="feather-edit"
aria-label="Show changes"
bind:this={buttons[1]}
on:click={showDiff}
/>
<div
data-icon="feather-plus"
aria-label="Stage"
bind:this={buttons[1]}
bind:this={buttons[2]}
on:click={stage}
/>
</div>
Expand Down
7 changes: 5 additions & 2 deletions src/ui/sidebar/gitView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
loading = false;
});
}
function stageAll() {
loading = true;
plugin.gitManager.stageAll().then(() => {
Expand Down Expand Up @@ -229,6 +230,7 @@
{change}
{view}
manager={plugin.gitManager}
workspace={plugin.app.workspace}
on:git-refresh={refresh}
/>
{/each}
Expand Down Expand Up @@ -276,11 +278,12 @@
}
}
.git-view-body {
height: calc(100% - 5rem);
overflow-y: scroll;
overflow-y: auto;
padding-left: 10px;
}
main {
display: flex;
flex-direction: column;
height: 100%;
overflow-y: hidden;
}
Expand Down
8 changes: 4 additions & 4 deletions src/ui/sidebar/sidebarView.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { HoverParent, HoverPopover, ItemView, WorkspaceLeaf } from "obsidian";
import { VIEW_CONFIG } from "src/constants";
import { GIT_VIEW_CONFIG } from "src/constants";
import ObsidianGit from "src/main";
import GitViewComponent from './gitView.svelte';

Expand All @@ -17,15 +17,15 @@ export default class GitView extends ItemView implements HoverParent {
}

getViewType(): string {
return VIEW_CONFIG.type;
return GIT_VIEW_CONFIG.type;
}

getDisplayText(): string {
return VIEW_CONFIG.name;
return GIT_VIEW_CONFIG.name;
}

getIcon(): string {
return VIEW_CONFIG.icon;
return GIT_VIEW_CONFIG.icon;
}

onClose(): Promise<void> {
Expand Down

0 comments on commit 78cd43f

Please sign in to comment.