From c3c4f98c6682bb7ff8398ddff1ec4ca5b0221858 Mon Sep 17 00:00:00 2001 From: Pieter de Bie Date: Fri, 12 Sep 2008 22:11:25 +0200 Subject: [PATCH] Allow hunk navigation by using j/k keys --- English.lproj/RepositoryWindow.xib | 68 +++++++++++++++++++++++------- GitX.xcodeproj/project.pbxproj | 6 +++ PBCommitList.h | 2 + PBCommitList.m | 2 + PBWebGitController.h | 2 + PBWebGitController.m | 6 +++ html/commit.html | 5 ++- html/diffHighlighter.js | 16 ++++++- html/diff_style.css | 17 ++++++-- html/keyboardNavigation.js | 49 +++++++++++++++++++++ 10 files changed, 152 insertions(+), 21 deletions(-) create mode 100644 html/keyboardNavigation.js diff --git a/English.lproj/RepositoryWindow.xib b/English.lproj/RepositoryWindow.xib index f8d9173..1f1c686 100644 --- a/English.lproj/RepositoryWindow.xib +++ b/English.lproj/RepositoryWindow.xib @@ -8,7 +8,7 @@ 352.00 YES - + YES @@ -73,10 +73,9 @@ - + 265 {{0, 14}, {183, 22}} - YES 343014976 @@ -172,10 +171,9 @@ Branch - + 268 {{0, 14}, {134, 26}} - YES -2076049856 @@ -283,10 +281,9 @@ View selector - + 268 {{0, 14}, {114, 23}} - YES 67239424 @@ -1109,7 +1106,7 @@ ELIAAAAAAAgACAAIAAgAAQABAAEAAQ 2322 - {835, 70} + {835, 0} @@ -1473,7 +1470,7 @@ ELIAAAAAAAgACAAIAAgAAQABAAEAAQ 2322 - {543, 112} + {543, 0} @@ -2238,6 +2235,14 @@ ELIAAAAAAAgACAAIAAgAAQABAAEAAQ 218 + + + webController + + + + 233 + @@ -2825,6 +2830,7 @@ ELIAAAAAAAgACAAIAAgAAQABAAEAAQ 3.windowTemplate.hasMinSize 3.windowTemplate.maxSize 3.windowTemplate.minSize + 31.IBAttributePlaceholdersKey 31.IBPluginDependency 34.IBPluginDependency 35.IBPluginDependency @@ -2915,9 +2921,9 @@ ELIAAAAAAAgACAAIAAgAAQABAAEAAQ com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin - {{334, 465}, {850, 418}} + {{480, 439}, {850, 418}} com.apple.InterfaceBuilder.CocoaPlugin - {{334, 465}, {850, 418}} + {{480, 439}, {850, 418}} {{15, 196}, {850, 418}} @@ -2925,6 +2931,15 @@ ELIAAAAAAAgACAAIAAgAAQABAAEAAQ {3.40282e+38, 3.40282e+38} {213, 107} + + YES + + YES + + + YES + + com.apple.WebKitIBPlugin com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin @@ -2933,7 +2948,7 @@ ELIAAAAAAAgACAAIAAgAAQABAAEAAQ PBQLOutlineView com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin - {{451, 656}, {616, 227}} + {{451, 883}, {616, 0}} com.apple.InterfaceBuilder.CocoaPlugin {{132, 614}, {616, 0}} com.apple.InterfaceBuilder.CocoaPlugin @@ -2996,7 +3011,7 @@ ELIAAAAAAAgACAAIAAgAAQABAAEAAQ - 218 + 233 @@ -3005,8 +3020,17 @@ ELIAAAAAAAgACAAIAAgAAQABAAEAAQ PBCommitList NSTableView - webView - WebView + YES + + YES + webController + webView + + + YES + PBWebGitController + WebView + IBProjectSource @@ -3092,16 +3116,22 @@ ELIAAAAAAAgACAAIAAgAAQABAAEAAQ PBWebGitController NSObject + + doeHet: + id + YES YES detailController + source view YES PBDetailController + id WebView @@ -3110,6 +3140,14 @@ ELIAAAAAAAgACAAIAAgAAQABAAEAAQ PBWebGitController.h + + PBWebGitController + NSObject + + IBUserSource + + + 0 diff --git a/GitX.xcodeproj/project.pbxproj b/GitX.xcodeproj/project.pbxproj index 1f62e37..059fc07 100644 --- a/GitX.xcodeproj/project.pbxproj +++ b/GitX.xcodeproj/project.pbxproj @@ -26,6 +26,8 @@ F56173280E056ED2001DCD79 /* diffHighlighter.js in Resources */ = {isa = PBXBuildFile; fileRef = F56173270E056ED2001DCD79 /* diffHighlighter.js */; }; F56174570E058893001DCD79 /* PBGitTree.m in Sources */ = {isa = PBXBuildFile; fileRef = F56174560E058893001DCD79 /* PBGitTree.m */; }; F561777D0E05C88E001DCD79 /* DetailView.png in Resources */ = {isa = PBXBuildFile; fileRef = F561777C0E05C88E001DCD79 /* DetailView.png */; }; + F561E6CC0E7AFDD000521636 /* keyboardNavigation.js in Sources */ = {isa = PBXBuildFile; fileRef = F561E6CB0E7AFDD000521636 /* keyboardNavigation.js */; }; + F561E6CD0E7AFDDF00521636 /* keyboardNavigation.js in Resources */ = {isa = PBXBuildFile; fileRef = F561E6CB0E7AFDD000521636 /* keyboardNavigation.js */; }; F56524BB0E02D22D00F03B52 /* NSFileHandleExt.m in Sources */ = {isa = PBXBuildFile; fileRef = F56524B90E02D22D00F03B52 /* NSFileHandleExt.m */; }; F56524F00E02D45200F03B52 /* PBGitCommit.m in Sources */ = {isa = PBXBuildFile; fileRef = F56524EF0E02D45200F03B52 /* PBGitCommit.m */; }; F56526240E03D85900F03B52 /* WebKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F56526230E03D85900F03B52 /* WebKit.framework */; }; @@ -107,6 +109,7 @@ F56174550E058893001DCD79 /* PBGitTree.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBGitTree.h; sourceTree = ""; }; F56174560E058893001DCD79 /* PBGitTree.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBGitTree.m; sourceTree = ""; }; F561777C0E05C88E001DCD79 /* DetailView.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = DetailView.png; sourceTree = ""; }; + F561E6CB0E7AFDD000521636 /* keyboardNavigation.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; name = keyboardNavigation.js; path = html/keyboardNavigation.js; sourceTree = ""; }; F56524B90E02D22D00F03B52 /* NSFileHandleExt.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NSFileHandleExt.m; sourceTree = ""; }; F56524BA0E02D22D00F03B52 /* NSFileHandleExt.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NSFileHandleExt.h; sourceTree = ""; }; F56524EE0E02D45200F03B52 /* PBGitCommit.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBGitCommit.h; sourceTree = ""; }; @@ -328,6 +331,7 @@ children = ( F561727C0E056A11001DCD79 /* diff_style.css */, F56526590E03E71B00F03B52 /* commit.html */, + F561E6CB0E7AFDD000521636 /* keyboardNavigation.js */, F58A8F270E043698007E3FC0 /* commits.css */, F57ABDDE0E0441DE00A088B8 /* commit.js */, F57ABE180E04431D00A088B8 /* prototype.js */, @@ -415,6 +419,7 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( + F561E6CD0E7AFDDF00521636 /* keyboardNavigation.js in Resources */, 913D5E500E55645900CECEA2 /* gitx in Resources */, 8D11072B0486CEB800E47090 /* InfoPlist.strings in Resources */, F565265A0E03E71B00F03B52 /* commit.html in Resources */, @@ -460,6 +465,7 @@ F5C007750E731B48007B84B2 /* PBGitRef.m in Sources */, F5AD56790E79B78100EDAAFE /* PBCommitList.m in Sources */, F53FF2050E7ABB5300389171 /* PBGitRevSpecifier.m in Sources */, + F561E6CC0E7AFDD000521636 /* keyboardNavigation.js in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/PBCommitList.h b/PBCommitList.h index 43e0006..20cea8f 100644 --- a/PBCommitList.h +++ b/PBCommitList.h @@ -8,9 +8,11 @@ #import #import +#import "PBWebGitController.h" @interface PBCommitList : NSTableView { IBOutlet WebView* webView; + IBOutlet PBWebGitController* webController; } @end diff --git a/PBCommitList.m b/PBCommitList.m index a7068fb..cafe583 100644 --- a/PBCommitList.m +++ b/PBCommitList.m @@ -22,6 +22,8 @@ else [webView scrollPageDown: self]; } + else if ([character rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:@"jk"]].location == 0) + [webController sendKey: character]; else [super keyDown: event]; } diff --git a/PBWebGitController.h b/PBWebGitController.h index e59fa4f..42e870f 100644 --- a/PBWebGitController.h +++ b/PBWebGitController.h @@ -20,5 +20,7 @@ } - (void) changeContentTo: (PBGitCommit *) content; +- (void) sendKey: (NSString*) key; + @property (readonly) NSString* diff; @end diff --git a/PBWebGitController.m b/PBWebGitController.m index 5fd55c7..245c9af 100644 --- a/PBWebGitController.m +++ b/PBWebGitController.m @@ -67,6 +67,12 @@ [detailController selectCommit:sha]; } +- (void) sendKey: (NSString*) key +{ + id script = [view windowScriptObject]; + [script callWebScriptMethod:@"handleKeyFromCocoa" withArguments: [NSArray arrayWithObject:key]]; +} + + (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector { return NO; diff --git a/html/commit.html b/html/commit.html index 2bf34a6..9e3d472 100644 --- a/html/commit.html +++ b/html/commit.html @@ -1,12 +1,13 @@ -
+ Details for commit -
+ + diff --git a/html/diffHighlighter.js b/html/diffHighlighter.js index 3c17005..76151e5 100644 --- a/html/diffHighlighter.js +++ b/html/diffHighlighter.js @@ -14,12 +14,26 @@ var highlightDiffs = function() { l = l.gsub("\t", " "); + if (l.startsWith("diff")) { + newContent += "
" + l + "
"; + return; + } + if (l.startsWith("---")) { + newContent += "
" + l + "
"; + return; + } + + if (l.startsWith("+++")) { + newContent += "
" + l + "
"; + return; + } + if (l.startsWith("+")) newContent += "
" + l + "
"; else if (l.startsWith("-")) newContent += "
" + l + "
"; else if (l.startsWith("@")) - newContent += "
" + l + "
"; + newContent += "
" + l + "
"; else newContent += l + "\n"; }); diff --git a/html/diff_style.css b/html/diff_style.css index 3488f67..27c30f5 100644 --- a/html/diff_style.css +++ b/html/diff_style.css @@ -1,14 +1,21 @@ -code .delline { +code .delline, code .oldfile { background-color: #FEE; color: #B00; } -code .addline { +code .addline, code .newfile { background-color: #DFD; color: #080; } +code .fileline { + font-weight: bold; +} -code .meta { +code .fileheader { + margin-top: 20px; +} + +code .hunkheader { background-color: #f7f7f7; color: #bbb; } @@ -22,4 +29,8 @@ pre code { display: table-cell; padding: 9px; width: 100%; +} + +#CurrentHunk { + border-left: 5px solid black; } \ No newline at end of file diff --git a/html/keyboardNavigation.js b/html/keyboardNavigation.js new file mode 100644 index 0000000..6367e26 --- /dev/null +++ b/html/keyboardNavigation.js @@ -0,0 +1,49 @@ +var scrollToCenter = function(element) { + var pos = element.cumulativeOffset(); + window.scrollTo(pos[0], pos[1] - 100); +} + +var handleKeys = function(event) { + if (event.altKey || event.ctrlKey || event.metaKey || event.shiftKey) + return; + if (event.keyCode == 74) + return changeHunk(true); + else if (event.keyCode == 75) + return changeHunk(false); + + return true; +} + +var handleKeyFromCocoa = function(key) { + if (key == 'j') + changeHunk(true); + else if (key == 'k') + changeHunk(false); +} + +var changeHunk = function(next) { + var hunks = $A(document.getElementsByClassName("hunkheader")); + if (hunks.length == 0) + return; + + var currentHunk = document.getElementById("CurrentHunk"); + var newHunk; + + if (currentHunk && hunks.indexOf(currentHunk) >= 0) { + currentHunk.id = null; + if (next) + newHunk = hunks[hunks.indexOf(currentHunk) + 1]; + else + newHunk = hunks[hunks.indexOf(currentHunk) - 1]; + } + if (!newHunk) + newHunk = hunks[0]; + + newHunk.id = 'CurrentHunk'; + scrollToCenter(newHunk); + return false; +} + +document.onkeydown = function(event) { + return handleKeys(event); +}; \ No newline at end of file