Skip to content

Commit

Permalink
#383 Shift Key Modifier on the Commit Details View topological naviga…
Browse files Browse the repository at this point in the history
…tion keystrokes.
  • Loading branch information
mhutchie committed Sep 19, 2020
1 parent 808878f commit 7355d83
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 8 deletions.
66 changes: 62 additions & 4 deletions web/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -587,24 +587,82 @@ class Graph {
return muted;
}


/**
* Get the index of the first parent of the commit at the specified index.
* @param i The index of the commit.
* @returns The index of the first parent, or -1 if there is no parent.
*/
public getFirstParentIndex(i: number) {
const parents = this.vertices[i].getParents();
return parents.length > 0 ? parents[0].id : -1;
return parents.length > 0
? parents[0].id
: -1;
}

/**
* Get the index of the alternative parent of the commit at the specified index.
* @param i The index of the commit.
* @returns The index of the alternative parent, or -1 if there is no parent.
*/
public getAlternativeParentIndex(i: number) {
const parents = this.vertices[i].getParents();
return parents.length > 1
? parents[1].id
: parents.length === 1
? parents[0].id
: -1;
}

/**
* Get the index of the first child of the commit at the specified index.
* @param i The index of the commit.
* @returns The index of the first child, or -1 if there is no child.
*/
public getFirstChildIndex(i: number) {
const children = this.vertices[i].getChildren();
if (children.length > 0) {
// The vertex has children
if (children.length > 1) {
// The vertex has multiple children
const branch = this.vertices[i].getBranch();
let childOnSameBranch;
let childOnSameBranch: Vertex | undefined;
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 if (children.length === 1) {
// The vertex has a single child
return children[0].id;
} else {
// The vertex has no children
return -1;
}
}

/**
* Get the index of the alternative child of the commit at the specified index.
* @param i The index of the commit.
* @returns The index of the alternative child, or -1 if there is no child.
*/
public getAlternativeChildIndex(i: number) {
const children = this.vertices[i].getChildren();
if (children.length > 1) {
// The vertex has multiple children
const branch = this.vertices[i].getBranch();
let childOnSameBranch: Vertex | undefined;
if (branch !== null && (childOnSameBranch = children.find((child) => child.isOnThisBranch(branch)))) {
// If a child could be found on the same branch as the vertex
return Math.max(...children.filter(child => child !== childOnSameBranch).map((child) => child.id));
} else {
// No child could be found on the same branch as the vertex
const childIndexes = children.map((child) => child.id).sort();
return childIndexes[childIndexes.length - 2];
}
} else if (children.length === 1) {
// The vertex has a single child
return children[0].id;
} else {
// The vertex has no children
return -1;
Expand Down
18 changes: 14 additions & 4 deletions web/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1819,10 +1819,20 @@ class GitGraphView {

if (e.ctrlKey || e.metaKey) {
// 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);
if (e.shiftKey) {
// Follow commits on alternative branches when possible
if (e.key === 'ArrowUp') {
newHashIndex = this.graph.getAlternativeChildIndex(curHashIndex);
} else if (e.key === 'ArrowDown') {
newHashIndex = this.graph.getAlternativeParentIndex(curHashIndex);
}
} else {
// Follow commits on the same 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
Expand Down

0 comments on commit 7355d83

Please sign in to comment.