Issue 3562: ] and [ shortcuts don't work on a page showing a unified diff.
Status:  Duplicate
Merged:  issue 3385
Owner:
Closed:  Dec 4
Reported by mat...@google.com, Sep 15, 2015
Affected Version: 2.11.3-1300-g1912561

What steps will reproduce the problem?
1. View a patch set containing multiple files.
2. View one changed file and select 'unified diff'. In the case observed, a binary file was added and so unified diff was the default for that file.
3. Try to move to previous/next files with the keyboard ([]). Nothing happens.
4. Change to side-by-side diff. I clicked the icon at the top right for this.
5. Try the same keyboard shortcuts. They work.


Nov 24, 2015
#2 joe.lencioni
As a workaround for this issue, you should be able to add the following to your GerritSiteHeader.html file:

<script>
<![CDATA[
// Fix `[` and `]` keyboard shortcuts when viewing unified diffs.
// https://code.google.com/p/gerrit/issues/detail?id=3562
(function() {
  var handleKeypress = function(event) {
    var which = event.which;
    if (which == null) {
      which = event.charCode != null ? event.charCode : event.keyCode;
    }

    if (which !== 91 && which !== 93) {
      return;
    }

    var navLinks = document.querySelector('.sideBySideScreenLinkTable')
      .querySelectorAll('.gwt-InlineHyperlink');
    var navLinksArr = [].slice.call(navLinks);

    var navLink;
    if (which === 91) { // [
      navLink = navLinksArr.find(function(link) {
        return /⇦/.test(link.innerText);
      });
    } else if (which === 93) { // ]
      navLink = navLinksArr.find(function(link) {
        return /⇨/.test(link.innerText);
      });
    }

    if (!navLink) {
      // There was no matching nav link, so we probably need to go "up".
      navLink = navLinksArr.find(function(link) {
        return /⇧/.test(link.innerText);
      });
    }

    if (navLink) {
      // We found a matching nav link, so lets click it.
      navLink.click();
    }
  };

  var bindOrUnbindKeyboardListener = function() {
    if (/,unified$/.test(window.location.hash)) {
      // This is a unified diff, so we want to add our custom keyboard listener.
      window.addEventListener('keypress', handleKeypress);
    } else {
      // This is not a unified diff, so we want to remove our custom keyboard
      // listener.
      window.removeEventListener('keypress', handleKeypress);
    }
  };

  window.addEventListener('hashchange', bindOrUnbindKeyboardListener);
  window.addEventListener('load', bindOrUnbindKeyboardListener);
})();
]]>
</script>
Dec 4, 2015
Project Member #3 dougk....@gmail.com
Duplicate of Issue 3385 -- thanks for the workaround, though!
Status: Duplicate
Owner: dougk....@gmail.com
Mergedinto: 3385