PluginProbe
TablePress – Tables in WordPress made easy / 3.4
TablePress – Tables in WordPress made easy v3.4
3.4 3.3.4 3.3.3 3.3.2 3.3.1 trunk 1.12 1.14 1.9.2 2.0.4 2.1.7 2.1.8 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3 2.3.1 2.3.2 2.4 2.4.1 2.4.2 2.4.3 All 45 releases
tablepress / admin / js / common / keyboard-shortcut.js

keyboard-shortcut.js in TablePress – Tables in WordPress made easy 3.4, at admin/js/common/keyboard-shortcut.js

60 lines 1.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * Common functions that are used in TablePress JS.
3 *
4 * @package TablePress
5 * @subpackage Views JavaScript
6 * @author Tobias Bäthge
7 * @since 2.2.0
8 */
9
10 /**
11 * WordPress dependencies.
12 */
13 import { _x, sprintf } from '@wordpress/i18n';
14
15 /**
16 * Registers a "Save Changes" keyboard shortcut for a button.
17 *
18 * @since 2.2.0
19 *
20 * @param {HTMLElement} $button DOM element for the button.
21 */
22 export const registerSaveChangesKeyboardShortcut = ( $button ) => {
23 // Add keyboard shortcut as title attribute to the "Save Changes" button, with correct modifier key for Mac/non-Mac.
24 const modifierKey = ( window?.navigator?.platform?.includes( 'Mac' ) ) ?
25 _x( '⌘', 'keyboard shortcut modifier key on a Mac keyboard', 'tablepress' ) :
26 _x( 'Ctrl+', 'keyboard shortcut modifier key on a non-Mac keyboard', 'tablepress' );
27 $button.title = sprintf( $button.dataset.shortcut, modifierKey ); // eslint-disable-line @wordpress/valid-sprintf
28
29 /**
30 * Registers keyboard events and triggers corresponding actions by emulating button clicks.
31 *
32 * @since 2.2.0
33 *
34 * @param {Event} event Keyboard event.
35 */
36 const keyboardShortcuts = ( event ) => {
37 let action = '';
38
39 if ( event.ctrlKey || event.metaKey ) {
40 if ( 83 === event.keyCode ) {
41 // Save Changes: Ctrl/Cmd + S.
42 action = 'save-changes';
43 }
44 }
45
46 if ( 'save-changes' === action ) {
47 // Blur the focussed element to make sure that all change events were triggered.
48 document.activeElement.blur(); // eslint-disable-line @wordpress/no-global-active-element
49
50 // Emulate a click on the button corresponding to the action.
51 $button.click();
52
53 // Prevent the browser's native handling of the shortcut, i.e. showing the Save or Print dialogs.
54 event.preventDefault();
55 }
56 };
57 // Register keyboard shortcut handler.
58 window.addEventListener( 'keydown', keyboardShortcuts, true );
59 };
60