Skip to content

Commit

Permalink
refactor: Migrate UndoRedoKeyListeners to typescript (apache#30654)
Browse files Browse the repository at this point in the history
  • Loading branch information
EnxDev authored Oct 24, 2024
1 parent e4d8f7a commit ec2d330
Showing 1 changed file with 13 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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();
Expand All @@ -61,6 +63,4 @@ class UndoRedoKeyListeners extends PureComponent {
}
}

UndoRedoKeyListeners.propTypes = propTypes;

export default UndoRedoKeyListeners;

0 comments on commit ec2d330

Please sign in to comment.