refactor: Migrate UndoRedoKeyListeners to typescript (#30654)

This commit is contained in:
Enzo Martellucci 2024-10-24 18:53:16 +02:00 committed by GitHub
parent e4d8f7af61
commit ec2d3307fd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 13 additions and 13 deletions

View File

@ -17,15 +17,15 @@
* under the License.
*/
import { PureComponent } from 'react';
import PropTypes from 'prop-types';
import { HeaderProps } from '../Header/types';
const propTypes = {
onUndo: PropTypes.func.isRequired,
onRedo: PropTypes.func.isRequired,
type UndoRedoKeyListenersProps = {
onUndo: HeaderProps['onUndo'];
onRedo: HeaderProps['onRedo'];
};
class UndoRedoKeyListeners extends PureComponent {
constructor(props) {
class UndoRedoKeyListeners extends PureComponent<UndoRedoKeyListenersProps> {
constructor(props: UndoRedoKeyListenersProps) {
super(props);
this.handleKeydown = this.handleKeydown.bind(this);
}
@ -38,15 +38,17 @@ class UndoRedoKeyListeners extends PureComponent {
document.removeEventListener('keydown', this.handleKeydown);
}
handleKeydown(event) {
handleKeydown(event: KeyboardEvent) {
const controlOrCommand = event.ctrlKey || event.metaKey;
if (controlOrCommand) {
const isZChar = event.key === 'z' || event.keyCode === 90;
const isYChar = event.key === 'y' || event.keyCode === 89;
const isEditingMarkdown =
document && document.querySelector('.dashboard-markdown--editing');
const isEditingTitle =
document && document.querySelector('.editable-title--editing');
const isEditingMarkdown = document?.querySelector(
'.dashboard-markdown--editing',
);
const isEditingTitle = document?.querySelector(
'.editable-title--editing',
);
if (!isEditingMarkdown && !isEditingTitle && (isZChar || isYChar)) {
event.preventDefault();
@ -61,6 +63,4 @@ class UndoRedoKeyListeners extends PureComponent {
}
}
UndoRedoKeyListeners.propTypes = propTypes;
export default UndoRedoKeyListeners;