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
This commit is contained in:
Dan Stillman 2013-07-02 02:43:53 -04:00
parent 4d9191ccd8
commit 30a0bbcca2
3 changed files with 137 additions and 134 deletions

View File

@ -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;

View File

@ -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();
}

View File

@ -94,6 +94,7 @@
<vbox id="zotero-pane"
onkeydown="ZoteroPane_Local.handleKeyDown(event, this.id)"
onkeyup="ZoteroPane_Local.handleKeyUp(event, this.id)"
onkeypress="ZoteroPane_Local.handleKeyPress(event)"
chromedir="&locale.dir;">
<toolbar id="zotero-toolbar" class="toolbar toolbar-primary">
@ -299,7 +300,6 @@
the tag selector to max height -->
<tree id="zotero-collections-tree" hidecolumnpicker="true" context="zotero-collectionmenu"
onmouseover="ZoteroPane_Local.collectionsView.setHighlightedRows();"
onkeypress="ZoteroPane_Local.handleKeyPress(event, this.id)"
onselect="ZoteroPane_Local.onCollectionSelected();"
ondragstart="if (event.target.localName == 'treechildren') { ZoteroPane_Local.collectionsView.onDragStart(event); }"
ondragenter="return ZoteroPane_Local.collectionsView.onDragEnter(event)"
@ -337,7 +337,6 @@
disableKeyNavigation="true"
onfocus="if (ZoteroPane_Local.itemsView.rowCount &amp;&amp; !ZoteroPane_Local.itemsView.selection.count) { ZoteroPane_Local.itemsView.selection.select(0); }"
onkeydown="ZoteroPane_Local.handleKeyDown(event, this.id)"
onkeypress="ZoteroPane_Local.handleKeyPress(event, this.id)"
onselect="ZoteroPane_Local.itemSelected(event)"
ondragstart="if (event.target.localName == 'treechildren') { ZoteroPane_Local.itemsView.onDragStart(event); }"
ondragenter="return ZoteroPane_Local.itemsView.onDragEnter(event)"