| 1 |
/** |
| 2 |
* JavaScript code for the keyboard shortcuts integration on the "Edit" screen. |
| 3 |
* |
| 4 |
* @package TablePress |
| 5 |
* @subpackage Views JavaScript |
| 6 |
* @author Tobias Bäthge |
| 7 |
* @since 3.0.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
/* globals tp */ |
| 11 |
|
| 12 |
/** |
| 13 |
* Internal dependencies. |
| 14 |
*/ |
| 15 |
import { $ } from '../common/functions'; |
| 16 |
|
| 17 |
/** |
| 18 |
* Registers keyboard events and triggers corresponding actions by emulating button clicks. |
| 19 |
* |
| 20 |
* @param {Event} event Keyboard event. |
| 21 |
*/ |
| 22 |
const handleKeyboardShortcuts = ( event ) => { |
| 23 |
let action = ''; |
| 24 |
let move_direction = ''; |
| 25 |
let move_type = ''; |
| 26 |
|
| 27 |
if ( event.ctrlKey || event.metaKey ) { |
| 28 |
if ( 76 === event.keyCode ) { |
| 29 |
// Insert Link: Ctrl/Cmd + L. |
| 30 |
action = 'insert_link'; |
| 31 |
} else if ( 73 === event.keyCode ) { |
| 32 |
// Insert Image: Ctrl/Cmd + I. |
| 33 |
action = 'insert_image'; |
| 34 |
} else if ( 69 === event.keyCode ) { |
| 35 |
// Advanced Editor: Ctrl/Cmd + E. |
| 36 |
action = 'advanced_editor'; |
| 37 |
} else if ( event.shiftKey ) { |
| 38 |
if ( 38 === event.keyCode ) { |
| 39 |
action = 'move'; |
| 40 |
move_type = 'rows'; |
| 41 |
if ( event.altKey ) { |
| 42 |
move_direction = 'top'; // Move to top: Ctrl/Cmd + Alt/Option + Shift + ↑. |
| 43 |
} else { |
| 44 |
move_direction = 'up'; // Move up: Ctrl/Cmd + Shift + ↑. |
| 45 |
} |
| 46 |
} else if ( 40 === event.keyCode ) { |
| 47 |
action = 'move'; |
| 48 |
move_type = 'rows'; |
| 49 |
if ( event.altKey ) { |
| 50 |
move_direction = 'bottom'; // Move to bottom: Ctrl/Cmd + Alt/Option + Shift + ↓. |
| 51 |
} else { |
| 52 |
move_direction = 'down'; // Move down: Ctrl/Cmd + Shift + ↓. |
| 53 |
} |
| 54 |
} else if ( 37 === event.keyCode ) { |
| 55 |
action = 'move'; |
| 56 |
move_type = 'columns'; |
| 57 |
if ( event.altKey ) { |
| 58 |
move_direction = 'first'; // Move to left end: Ctrl/Cmd + Alt/Option + Shift + ←. |
| 59 |
} else { |
| 60 |
move_direction = 'left'; // Move left: Ctrl/Cmd + Shift + ←. |
| 61 |
} |
| 62 |
} else if ( 39 === event.keyCode ) { |
| 63 |
action = 'move'; |
| 64 |
move_type = 'columns'; |
| 65 |
if ( event.altKey ) { |
| 66 |
move_direction = 'last'; // Move to right end: Ctrl/Cmd + Alt/Option + Shift + →. |
| 67 |
} else { |
| 68 |
move_direction = 'right'; // Move right: Ctrl/Cmd + Shift + →. |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
// Return early if no action was triggered. |
| 75 |
if ( '' === action ) { |
| 76 |
return; |
| 77 |
} |
| 78 |
|
| 79 |
if ( 'insert_link' === action || 'insert_image' === action || 'advanced_editor' === action ) { |
| 80 |
// Only open the dialogs if an element in the table editor is focussed, to e.g. prevent multiple dialogs to be opened. |
| 81 |
if ( $( '#table-editor' ).contains( document.activeElement ) ) { // eslint-disable-line @wordpress/no-global-active-element |
| 82 |
const $active_textarea = ( 'TEXTAREA' === document.activeElement.tagName ) ? document.activeElement : null; // eslint-disable-line @wordpress/no-global-active-element |
| 83 |
// Blur the active textarea to make sure that all change events were triggered. |
| 84 |
$active_textarea?.blur(); |
| 85 |
// Open the "Insert Link", "Insert Image", or Advanced Editor" dialog. |
| 86 |
tp.callbacks[ action ].open_dialog( $active_textarea ); |
| 87 |
} |
| 88 |
|
| 89 |
// Prevent the browser's native handling of the shortcut. |
| 90 |
event.preventDefault(); |
| 91 |
} else if ( 'move' === action ) { |
| 92 |
// Only move rows or columns if an element in the table editor is focussed, but not if the cell is being edited (to not prevent the browser's original shortcuts). |
| 93 |
if ( $( '#table-editor' ).contains( document.activeElement ) && 'TEXTAREA' !== document.activeElement.tagName ) { // eslint-disable-line @wordpress/no-global-active-element |
| 94 |
// Move the selected rows or columns. |
| 95 |
if ( tp.helpers.move_allowed( move_type, move_direction ) ) { |
| 96 |
tp.callbacks.move( move_direction, move_type ); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
// Stop the event propagation so that Jspreadsheet doesn't understand the arrow key as movement of the cursor, and prevent the browser's native handling of the shortcut. |
| 101 |
event.stopImmediatePropagation(); |
| 102 |
} |
| 103 |
}; |
| 104 |
|
| 105 |
// Register keyboard shortcut handler. |
| 106 |
window.addEventListener( 'keydown', handleKeyboardShortcuts, true ); |
| 107 |
|