| @@ -11,14 +11,15 @@ | ||
| 11 | 11 | |
| 12 | 12 | /** |
| 13 | 13 | * WordPress dependencies. |
| 14 | 14 | */ |
| 15 | -import { __, _x, sprintf } from '@wordpress/i18n'; | |
| 15 | +import { __ } from '@wordpress/i18n'; | |
| 16 | 16 | |
| 17 | 17 | /** |
| 18 | 18 | * Internal dependencies. |
| 19 | 19 | */ |
| 20 | -import { $ } from './_common-functions'; | |
| 20 | +import { $ } from './common/functions'; | |
| 21 | +import { registerSaveChangesKeyboardShortcut } from './common/keyboard-shortcut'; | |
| 21 | 22 | |
| 22 | 23 | /** |
| 23 | 24 | * Enable/disable the regular textarea according to state of "Load Custom CSS" checkbox. |
| 24 | 25 | * |
| @@ -36,60 +37,26 @@ | ||
| 36 | 37 | * On form submit: Enable disabled fields, so that they are sent in the HTTP POST request. |
| 37 | 38 | * |
| 38 | 39 | * @since 1.0.0 |
| 39 | 40 | */ |
| 40 | -document.querySelector( '#tablepress-page form' ).addEventListener( 'submit', function () { | |
| 41 | +$( '#tablepress-page-form' ).addEventListener( 'submit', function () { | |
| 41 | 42 | this.querySelectorAll( ':scope input, :scope select, :scope textarea' ).forEach( ( field ) => ( field.disabled = false ) ); |
| 42 | 43 | } ); |
| 43 | 44 | |
| 44 | -// Add keyboard shortcut as title attribute to the "Save Changes" button, with correct modifier key for Mac/non-Mac. | |
| 45 | -const modifier_key = ( window?.navigator?.platform?.includes( 'Mac' ) ) ? | |
| 46 | - _x( '⌘', 'keyboard shortcut modifier key on a Mac keyboard', 'tablepress' ) : | |
| 47 | - _x( 'Ctrl+', 'keyboard shortcut modifier key on a non-Mac keyboard', 'tablepress' ); | |
| 48 | -const $save_changes_button = $( '#tablepress-options-save-changes' ); | |
| 49 | -const shortcut = sprintf( $save_changes_button.dataset.shortcut, modifier_key ); // eslint-disable-line @wordpress/valid-sprintf | |
| 50 | -$save_changes_button.title = sprintf( __( 'Keyboard Shortcut: %s', 'tablepress' ), shortcut ); | |
| 45 | +registerSaveChangesKeyboardShortcut( $( '#tablepress-options-save-changes' ) ); | |
| 51 | 46 | |
| 52 | 47 | /** |
| 53 | - * Registers keyboard events and triggers corresponding actions by emulating button clicks. | |
| 54 | - * | |
| 55 | - * @since 2.1.1 | |
| 56 | - * | |
| 57 | - * @param {Event} event Keyboard event. | |
| 58 | - */ | |
| 59 | -const keyboard_shortcuts = function ( event ) { | |
| 60 | - let action = ''; | |
| 61 | - | |
| 62 | - if ( event.ctrlKey || event.metaKey ) { | |
| 63 | - if ( 83 === event.keyCode ) { | |
| 64 | - // Save Changes: Ctrl/Cmd + S. | |
| 65 | - action = 'save-changes'; | |
| 66 | - } | |
| 67 | - } | |
| 68 | - | |
| 69 | - if ( 'save-changes' === action ) { | |
| 70 | - // Blur the focussed element to make sure that all change events were triggered. | |
| 71 | - document.activeElement.blur(); // eslint-disable-line @wordpress/no-global-active-element | |
| 72 | - | |
| 73 | - // Emulate a click on the button corresponding to the action. | |
| 74 | - $save_changes_button.click(); | |
| 75 | - | |
| 76 | - // Prevent the browser's native handling of the shortcut, i.e. showing the Save or Print dialogs. | |
| 77 | - event.preventDefault(); | |
| 78 | - } | |
| 79 | -}; | |
| 80 | -// Register keyboard shortcut handler. | |
| 81 | -window.addEventListener( 'keydown', keyboard_shortcuts, true ); | |
| 82 | - | |
| 83 | -/** | |
| 84 | 48 | * Require double confirmation when wanting to uninstall TablePress. |
| 85 | 49 | * |
| 86 | 50 | * @since 1.0.0 |
| 87 | 51 | */ |
| 88 | -$( '#uninstall-tablepress' ).addEventListener( 'click', ( event ) => { | |
| 89 | - if ( | |
| 90 | - ! confirm( __( 'Do you really want to uninstall TablePress and delete ALL data?', 'tablepress' ) ) || | |
| 91 | - ! confirm( __( 'Are you really sure?', 'tablepress' ) ) ) | |
| 92 | - { | |
| 93 | - event.preventDefault(); | |
| 94 | - } | |
| 95 | -} ); | |
| 52 | +const $button_uninstall = $( '#uninstall-tablepress' ); | |
| 53 | +if ( $button_uninstall ) { // The button only exists for admins! | |
| 54 | + $button_uninstall.addEventListener( 'click', ( event ) => { | |
| 55 | + if ( | |
| 56 | + ! confirm( __( 'Do you really want to uninstall TablePress and delete ALL data?', 'tablepress' ) ) || | |
| 57 | + ! confirm( __( 'Are you really sure?', 'tablepress' ) ) | |
| 58 | + ) { | |
| 59 | + event.preventDefault(); | |
| 60 | + } | |
| 61 | + } ); | |
| 62 | +} | |