Пример #1
0
 // Handles incoming new commands
 // Attached to dom by onInputFocus
 function onInputKeydown(event) {
   event.preventDefault();
   if (event.key === 'Escape') {
     event.currentTarget.blur();
     return;
   }
   const { id: parentId } = event.currentTarget.parentElement.parentElement;
   const command = keyboard.command(event);
   const isValid = keyboard.isValid(command);
   const { name } = store.getState().keyboard[parentId];
   if (isValid) {
     const { isDuplicate, key: duplicateKey } =
       isDuplicateCommand(store.getState().keyboard, command);
     if (isDuplicate && duplicateKey === parentId) {
       Flash.message(`<${kbString(command)}> is already ${name}'s shortcut.`, Flash.WARNING);
       event.currentTarget.blur();
     } else if (isDuplicate) {
       Flash.message(`Duplicate key! <${kbString(command)}> is ${name}'s shortcut.`, Flash.ERROR);
     } else {
       // Stop input reset race
       event.currentTarget.blur();
       Flash.close();
       // Actually update the store with the new binding here
       const updateBinding =
         event.currentTarget.dataset.key === 'secondaryCommand'
           ? updateSecondaryKeybinding
           : updateKeybinding;
       store.dispatch(updateBinding(parentId, command));
     }
   } else {
     // Then it's an error
     let flashType;
     let appendMsg;
     switch (command.error) {
       // Warning
       case ERROR_MSG_FINAL_KEY_IS_MODIFIER:
         flashType = Flash.WARNING;
         appendMsg = [HINT_MSG_NEED_FINAL_KEY];
         break;
       // Error
       case ERROR_MSG_NOT_VALID_FINAL_COMBO_KEY:
       case ERROR_MSG_NOT_VALID_SINGLE_KEY:
       default:
         flashType = Flash.ERROR;
         appendMsg = [
           HINT_MSG_SINGLE_KEYS,
           HINT_MSG_SHOULD_USE_MODIFIERS,
           HINT_MSG_TRY_PUNCTUATION,
         ];
         break;
     }
     Flash.message(
       `${kbString(command)} is ${lowerCaseSentence(command.error)}`,
       flashType,
     );
     Flash.append(appendMsg);
   }
 }
Пример #2
0
 return function handleKeyDown(event) {
   if (isModifierSingle(event)) {
     event.preventDefault();
   }
   // Handle preventing default key actions
   switch (event.key) {
     case 'Tab':
     //   // Change it so tab no longer focuses the entire popup window
     //   // and behaves like a TAB_NEXT command
     //   // This could break the tab-fix if the popup focus bug occurs
     //   // so remove this if someone complains
     //
     case 'ArrowUp':
     case 'ArrowDown':
     case 'Enter':
     case 'PageDown':
     case 'PageUp':
       event.preventDefault();
       break;
     default: break;
   }
   if (keyboard.isValid(event)) {
     const cmd = keyboard.command(event);
     const key = [...kbdControlMap.keys()].find(x => keyboard.isEqual(x, cmd));
     return navigate(kbdControlMap.get(key));
   }
   // Keys that require special behavior
   // In the case of 'End' or 'Home' this breaks the behavior of skipping to
   // the beginning of the end of the line in the searchInput.
   // Maybe we should only focus if the searchInput isn't active?
   switch (event.key) {
     case 'End':
       if (document.activeElement !== searchInput) {
         event.preventDefault();
         tabList.lastChild.focus();
       }
       break;
     case 'Home':
       if (document.activeElement !== searchInput) {
         event.preventDefault();
         tabList.firstChild.focus();
       }
       break;
     case 'PageDown':
       return navigate(TAB_NEXT);
     case 'PageUp':
       return navigate(TAB_PREV);
     case 'Tab':
       return navigate(event.shiftKey ? TAB_PREV : TAB_NEXT);
     default: break;
   }
   const shouldJustFocusSearchBar = (event.key === 'Backspace' && !isModifierSingle(event))
     || (/^([A-Za-z]|\d)$/.test(event.key) && !isModifierSingle(event));
   if (shouldJustFocusSearchBar) {
     searchInput.focus();
   }
   return noop();
 };