Skip to content

Commit

Permalink
refactor: Migrate UndoRedoKeyListeners to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
EnxDev committed Oct 22, 2024
1 parent 47c5334 commit d5dfcad
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 d5dfcad

Please sign in to comment.