From 30a0bbcca2094cb79fd7084d5e8b94ffe52b0512 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Tue, 2 Jul 2013 02:43:53 -0400 Subject: [PATCH] Fix conflict with Cmd-Shift-A, and probably other third-party shortcuts The Zotero shortcut keys, and their event.preventDefault(), were bound to keydown, so shortcuts bound to keypress were still be called. This moves most of the shortcut handling code into the keypress handler. Fixes #344 --- chrome/content/zotero/xpcom/itemTreeView.js | 26 ++- chrome/content/zotero/zoteroPane.js | 242 ++++++++++---------- chrome/content/zotero/zoteroPane.xul | 3 +- 3 files changed, 137 insertions(+), 134 deletions(-) diff --git a/chrome/content/zotero/xpcom/itemTreeView.js b/chrome/content/zotero/xpcom/itemTreeView.js index cf2c1fc16..04ff52950 100644 --- a/chrome/content/zotero/xpcom/itemTreeView.js +++ b/chrome/content/zotero/xpcom/itemTreeView.js @@ -167,24 +167,28 @@ Zotero.ItemTreeView.prototype._setTreeGenerator = function(treebox) return; } + var key = String.fromCharCode(event.which); + if (key == '+' && !(event.ctrlKey || event.altKey || event.metaKey)) { + self.expandAllRows(); + event.preventDefault(); + return; + } + else if (key == '-' && !(event.shiftKey || event.ctrlKey || event.altKey || event.metaKey)) { + self.collapseAllRows(); + event.preventDefault(); + return; + } + // Ignore other non-character keypresses - if (!event.charCode) { + if (!event.charCode || event.shiftKey || event.ctrlKey || + event.altKey || event.metaKey) { return; } event.preventDefault(); Q.fcall(function () { - var key = String.fromCharCode(event.which); - if (key == '+' && !(event.ctrlKey || event.altKey || event.metaKey)) { - self.expandAllRows(); - return false; - } - else if (key == '-' && !(event.shiftKey || event.ctrlKey || event.altKey || event.metaKey)) { - self.collapseAllRows(); - return false; - } - else if (coloredTagsRE.test(key)) { + if (coloredTagsRE.test(key)) { let libraryID = self._itemGroup.libraryID; libraryID = libraryID ? parseInt(libraryID) : 0; let position = parseInt(key) - 1; diff --git a/chrome/content/zotero/zoteroPane.js b/chrome/content/zotero/zoteroPane.js index 558a01900..d6f5a0092 100644 --- a/chrome/content/zotero/zoteroPane.js +++ b/chrome/content/zotero/zoteroPane.js @@ -510,126 +510,7 @@ var ZoteroPane = new function() } ZoteroPane_Local.collectionsView.setHighlightedRows(); } - - return; } - else if (from == 'zotero-items-tree') { - // Focus TinyMCE explicitly on tab key, since the normal focusing - // doesn't work right - if (!event.shiftKey && event.keyCode == event.DOM_VK_TAB) { - var deck = document.getElementById('zotero-item-pane-content'); - if (deck.selectedPanel.id == 'zotero-view-note') { - setTimeout(function () { - document.getElementById('zotero-note-editor').focus(); - }, 0); - } - } - return; - } - - // Ignore keystrokes if Zotero pane is closed - var zoteroPane = document.getElementById('zotero-pane-stack'); - if (zoteroPane.getAttribute('hidden') == 'true' || - zoteroPane.getAttribute('collapsed') == 'true') { - return; - } - - var useShift = Zotero.isMac; - - var key = String.fromCharCode(event.which); - if (!key) { - Zotero.debug('No key'); - return; - } - - // Ignore modifiers other than Ctrl-Alt or Cmd-Shift - if (!((Zotero.isMac ? event.metaKey : event.ctrlKey) && - (useShift ? event.shiftKey : event.altKey))) { - return; - } - - var command = Zotero.Keys.getCommand(key); - if (!command) { - return; - } - - Zotero.debug(command); - - // Errors don't seem to make it out otherwise - try { - - switch (command) { - case 'openZotero': - try { - // Ignore Cmd-Shift-Z keystroke in text areas - if (Zotero.isMac && key == 'Z' && - event.originalTarget.localName == 'textarea') { - Zotero.debug('Ignoring keystroke in text area'); - return; - } - } - catch (e) { - Zotero.debug(e); - } - if(window.ZoteroOverlay) window.ZoteroOverlay.toggleDisplay() - break; - case 'library': - document.getElementById('zotero-collections-tree').focus(); - break; - case 'quicksearch': - document.getElementById('zotero-tb-search').select(); - break; - case 'newItem': - ZoteroPane_Local.newItem(2); // book - var menu = document.getElementById('zotero-editpane-item-box').itemTypeMenu; - menu.focus(); - document.getElementById('zotero-editpane-item-box').itemTypeMenu.menupopup.openPopup(menu, "before_start", 0, 0); - break; - case 'newNote': - // If a regular item is selected, use that as the parent. - // If a child item is selected, use its parent as the parent. - // Otherwise create a standalone note. - var parent = false; - var items = ZoteroPane_Local.getSelectedItems(); - if (items.length == 1) { - if (items[0].isRegularItem()) { - parent = items[0].id; - } - else { - parent = items[0].getSource(); - } - } - // Use key that's not the modifier as the popup toggle - ZoteroPane_Local.newNote( - useShift ? event.altKey : event.shiftKey, parent - ); - break; - case 'toggleTagSelector': - ZoteroPane_Local.toggleTagSelector(); - break; - case 'toggleFullscreen': - ZoteroPane_Local.toggleTab(); - break; - case 'copySelectedItemCitationsToClipboard': - ZoteroPane_Local.copySelectedItemsToClipboard(true) - break; - case 'copySelectedItemsToClipboard': - ZoteroPane_Local.copySelectedItemsToClipboard(); - break; - case 'importFromClipboard': - Zotero_File_Interface.importFromClipboard(); - break; - default: - throw ('Command "' + command + '" not found in ZoteroPane_Local.handleKeyDown()'); - } - - } - catch (e) { - Zotero.debug(e, 1); - Components.utils.reportError(e); - } - - event.preventDefault(); } @@ -662,7 +543,21 @@ var ZoteroPane = new function() } - function handleKeyPress(event, from) { + function handleKeyPress(event) { + var from = event.originalTarget.id; + + // Ignore keystrokes if Zotero pane is closed + var zoteroPane = document.getElementById('zotero-pane-stack'); + if (zoteroPane.getAttribute('hidden') == 'true' || + zoteroPane.getAttribute('collapsed') == 'true') { + return; + } + + if (Zotero.locked) { + event.preventDefault(); + return; + } + if (from == 'zotero-collections-tree') { if ((event.keyCode == event.DOM_VK_BACK_SPACE && Zotero.isMac) || event.keyCode == event.DOM_VK_DELETE) { @@ -673,7 +568,17 @@ var ZoteroPane = new function() } } else if (from == 'zotero-items-tree') { - if ((event.keyCode == event.DOM_VK_BACK_SPACE && Zotero.isMac) || + // Focus TinyMCE explicitly on tab key, since the normal focusing + // doesn't work right + if (!event.shiftKey && event.keyCode == event.DOM_VK_TAB) { + var deck = document.getElementById('zotero-item-pane-content'); + if (deck.selectedPanel.id == 'zotero-view-note') { + setTimeout(function () { + document.getElementById('zotero-note-editor').focus(); + }, 0); + } + } + else if ((event.keyCode == event.DOM_VK_BACK_SPACE && Zotero.isMac) || event.keyCode == event.DOM_VK_DELETE) { // If Cmd/Ctrl delete, use forced mode, which does different // things depending on the context @@ -696,6 +601,101 @@ var ZoteroPane = new function() return; } } + + var useShift = Zotero.isMac; + + var key = String.fromCharCode(event.which); + if (!key) { + Zotero.debug('No key'); + return; + } + + // Ignore modifiers other than Ctrl-Alt or Cmd-Shift + if (!((Zotero.isMac ? event.metaKey : event.ctrlKey) && + (useShift ? event.shiftKey : event.altKey))) { + return; + } + + var command = Zotero.Keys.getCommand(key); + if (!command) { + return; + } + + Zotero.debug(command); + + // Errors don't seem to make it out otherwise + try { + switch (command) { + case 'openZotero': + try { + // Ignore Cmd-Shift-Z keystroke in text areas + if (Zotero.isMac && key == 'Z' && + event.originalTarget.localName == 'textarea') { + Zotero.debug('Ignoring keystroke in text area'); + return; + } + } + catch (e) { + Zotero.debug(e); + } + if (window.ZoteroOverlay) window.ZoteroOverlay.toggleDisplay() + break; + case 'library': + document.getElementById('zotero-collections-tree').focus(); + break; + case 'quicksearch': + document.getElementById('zotero-tb-search').select(); + break; + case 'newItem': + ZoteroPane_Local.newItem(2); // book + var menu = document.getElementById('zotero-editpane-item-box').itemTypeMenu; + menu.focus(); + document.getElementById('zotero-editpane-item-box').itemTypeMenu.menupopup.openPopup(menu, "before_start", 0, 0); + break; + case 'newNote': + // If a regular item is selected, use that as the parent. + // If a child item is selected, use its parent as the parent. + // Otherwise create a standalone note. + var parent = false; + var items = ZoteroPane_Local.getSelectedItems(); + if (items.length == 1) { + if (items[0].isRegularItem()) { + parent = items[0].id; + } + else { + parent = items[0].getSource(); + } + } + // Use key that's not the modifier as the popup toggle + ZoteroPane_Local.newNote( + useShift ? event.altKey : event.shiftKey, parent + ); + break; + case 'toggleTagSelector': + ZoteroPane_Local.toggleTagSelector(); + break; + case 'toggleFullscreen': + ZoteroPane_Local.toggleTab(); + break; + case 'copySelectedItemCitationsToClipboard': + ZoteroPane_Local.copySelectedItemsToClipboard(true) + break; + case 'copySelectedItemsToClipboard': + ZoteroPane_Local.copySelectedItemsToClipboard(); + break; + case 'importFromClipboard': + Zotero_File_Interface.importFromClipboard(); + break; + default: + throw ('Command "' + command + '" not found in ZoteroPane_Local.handleKeyDown()'); + } + } + catch (e) { + Zotero.debug(e, 1); + Components.utils.reportError(e); + } + + event.preventDefault(); } diff --git a/chrome/content/zotero/zoteroPane.xul b/chrome/content/zotero/zoteroPane.xul index a04f2d0a2..f14e5204a 100644 --- a/chrome/content/zotero/zoteroPane.xul +++ b/chrome/content/zotero/zoteroPane.xul @@ -94,6 +94,7 @@ @@ -299,7 +300,6 @@ the tag selector to max height -->