Skip to content

Commit

Permalink
#299 Navigation between commits on a particular branch using CTRL + U…
Browse files Browse the repository at this point in the history
…P / CTRL + DOWN.
  • Loading branch information
mhutchie committed May 2, 2020
1 parent b998aad commit c36023d
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 31 deletions.
24 changes: 24 additions & 0 deletions web/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,30 @@ class Graph {
return muted;
}

public getFirstParentIndex(i: number) {
const parents = this.vertices[i].getParents();
return parents.length > 0 ? parents[0].id : -1;
}

public getFirstChildIndex(i: number) {
const children = this.vertices[i].getChildren();
if (children.length > 0) {
// The vertex has children
const branch = this.vertices[i].getBranch();
let childOnSameBranch;
if (branch !== null && (childOnSameBranch = children.find((child) => child.isOnThisBranch(branch)))) {
// If a child could be found on the same branch as the vertex
return childOnSameBranch.id;
} else {
// No child could be found on the same branch as the vertex
return Math.max(...children.map((child) => child.id));
}
} else {
// The vertex has no children
return -1;
}
}


/* Width Adjustment Methods */

Expand Down
72 changes: 41 additions & 31 deletions web/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1772,41 +1772,51 @@ class GitGraphView {
if (e.key === 'Escape') {
contextMenu.close();
}
} else {
} else if (this.expandedCommit !== null && (e.key === 'ArrowUp' || e.key === 'ArrowDown')) {
let curHashIndex = this.commitLookup[this.expandedCommit.commitHash], newHashIndex = -1;

if (e.ctrlKey || e.metaKey) {
if (e.key === 'r') {
this.refresh(true);
handledEvent(e);
} else if (e.key === 'f') {
this.findWidget.show(true);
handledEvent(e);
} else if (e.key === 'h' && this.commitHead !== null) {
this.scrollToCommit(this.commitHead, true, true);
handledEvent(e);
} else if (e.key.toLowerCase() === 's') {
this.scrollToStash(!e.shiftKey);
handledEvent(e);
// Up / Down navigates according to the order of commits on the branch
if (e.key === 'ArrowUp') {
newHashIndex = this.graph.getFirstChildIndex(curHashIndex);
} else if (e.key === 'ArrowDown') {
newHashIndex = this.graph.getFirstParentIndex(curHashIndex);
}
} else {
// Up / Down navigates according to the order of commits in the table
if (e.key === 'ArrowUp' && curHashIndex > 0) {
newHashIndex = curHashIndex - 1;
} else if (e.key === 'ArrowDown' && curHashIndex < this.commits.length - 1) {
newHashIndex = curHashIndex + 1;
}
} else if (e.key === 'Escape' && this.settingsWidget.isVisible()) {
}

if (newHashIndex > -1) {
handledEvent(e);
const elem = findCommitElemWithId(getCommitElems(), newHashIndex);
if (elem !== null) this.loadCommitDetails(elem);
}
} else if (e.ctrlKey || e.metaKey) {
if (e.key === 'r') {
this.refresh(true);
handledEvent(e);
} else if (e.key === 'f') {
this.findWidget.show(true);
handledEvent(e);
} else if (e.key === 'h' && this.commitHead !== null) {
this.scrollToCommit(this.commitHead, true, true);
handledEvent(e);
} else if (e.key.toLowerCase() === 's') {
this.scrollToStash(!e.shiftKey);
handledEvent(e);
}
} else if (e.key === 'Escape') {
if (this.settingsWidget.isVisible()) {
this.settingsWidget.close();
} else if (e.key === 'Escape' && this.findWidget.isVisible()) {
} else if (this.findWidget.isVisible()) {
this.findWidget.close();
} else if (this.expandedCommit !== null) { // Commit Details View is open
if (e.key === 'Escape') {
this.closeCommitDetails(true);
} else if ((e.key === 'ArrowUp' || e.key === 'ArrowDown')) {
let curHashIndex = this.commitLookup[this.expandedCommit.commitHash], newHashIndex = -1;
if (e.key === 'ArrowUp' && curHashIndex > 0) {
newHashIndex = curHashIndex - 1;
} else if (e.key === 'ArrowDown' && curHashIndex < this.commits.length - 1) {
newHashIndex = curHashIndex + 1;
}
if (newHashIndex > -1) {
e.preventDefault();
let elem = findCommitElemWithId(getCommitElems(), newHashIndex);
if (elem !== null) this.loadCommitDetails(elem);
}
}
} else if (this.expandedCommit !== null) {
this.closeCommitDetails(true);
}
}
});
Expand Down

0 comments on commit c36023d

Please sign in to comment.