Skip to content

Commit

Permalink
feat(keyboard): make function detection available via explicit utilities
Browse files Browse the repository at this point in the history
* expose dedicated utils for function detection via `KeyboardUtil`:
   * isCopy
   * isPaste
   * isUndo
   * isRedo
  • Loading branch information
nikku committed Aug 16, 2022
1 parent c1571c3 commit 92acdee
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 12 deletions.
13 changes: 8 additions & 5 deletions lib/features/keyboard/KeyboardBindings.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import {
isCmd,
isKey,
isShift
isCopy,
isPaste,
isUndo,
isRedo
} from './KeyboardUtil';

var LOW_PRIORITY = 500;
Expand Down Expand Up @@ -73,7 +76,7 @@ KeyboardBindings.prototype.registerBindings = function(keyboard, editorActions)

var event = context.keyEvent;

if (isCmd(event) && !isShift(event) && isKey(KEYS_UNDO, event)) {
if (isUndo(event)) {
editorActions.trigger('undo');

return true;
Expand All @@ -87,7 +90,7 @@ KeyboardBindings.prototype.registerBindings = function(keyboard, editorActions)

var event = context.keyEvent;

if (isCmd(event) && (isKey(KEYS_REDO, event) || (isKey(KEYS_UNDO, event) && isShift(event)))) {
if (isRedo(event)) {
editorActions.trigger('redo');

return true;
Expand All @@ -100,7 +103,7 @@ KeyboardBindings.prototype.registerBindings = function(keyboard, editorActions)

var event = context.keyEvent;

if (isCmd(event) && isKey(KEYS_COPY, event)) {
if (isCopy(event)) {
editorActions.trigger('copy');

return true;
Expand All @@ -113,7 +116,7 @@ KeyboardBindings.prototype.registerBindings = function(keyboard, editorActions)

var event = context.keyEvent;

if (isCmd(event) && isKey(KEYS_PASTE, event)) {
if (isPaste(event)) {
editorActions.trigger('paste');

return true;
Expand Down
30 changes: 30 additions & 0 deletions lib/features/keyboard/KeyboardUtil.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { isArray } from 'min-dash';

var KEYCODE_C = 67;
var KEYCODE_V = 86;
var KEYCODE_Y = 89;
var KEYCODE_Z = 90;

var KEYS_COPY = [ 'c', 'C', KEYCODE_C ];
var KEYS_PASTE = [ 'v', 'V', KEYCODE_V ];
var KEYS_REDO = [ 'y', 'Y', KEYCODE_Y ];
var KEYS_UNDO = [ 'z', 'Z', KEYCODE_Z ];

/**
* Returns true if event was triggered with any modifier
* @param {KeyboardEvent} event
Expand Down Expand Up @@ -39,4 +49,24 @@ export function isKey(keys, event) {
*/
export function isShift(event) {
return event.shiftKey;
}

export function isCopy(event) {
return isCmd(event) && isKey(KEYS_COPY, event);
}

export function isPaste(event) {
return isCmd(event) && isKey(KEYS_PASTE, event);
}

export function isUndo(event) {
return isCmd(event) && !isShift(event) && isKey(KEYS_UNDO, event);
}

export function isRedo(event) {
return isCmd(event) && (
isKey(KEYS_REDO, event) || (
isKey(KEYS_UNDO, event) && isShift(event)
)
);
}
2 changes: 1 addition & 1 deletion test/spec/features/keyboard/CopySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import editorActionsModule from 'lib/features/editor-actions';

import { createKeyEvent } from 'test/util/KeyEvents';

import { KEYS_COPY } from 'lib/features/keyboard/KeyboardBindings';
var KEYS_COPY = [ 'c', 'C', 67 ];


describe('features/keyboard - copy', function() {
Expand Down
2 changes: 1 addition & 1 deletion test/spec/features/keyboard/PasteSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import editorActionsModule from 'lib/features/editor-actions';

import { createKeyEvent } from 'test/util/KeyEvents';

import { KEYS_PASTE } from 'lib/features/keyboard/KeyboardBindings';
var KEYS_PASTE = [ 'v', 'V', 86 ];


describe('features/keyboard - paste', function() {
Expand Down
6 changes: 2 additions & 4 deletions test/spec/features/keyboard/RedoSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ import keyboardModule from 'lib/features/keyboard';

import { createKeyEvent } from 'test/util/KeyEvents';

import {
KEYS_REDO,
KEYS_UNDO
} from 'lib/features/keyboard/KeyboardBindings';
var KEYS_REDO = [ 'y', 'Y', 89 ];
var KEYS_UNDO = [ 'z', 'Z', 90 ];


describe('features/keyboard - redo', function() {
Expand Down
2 changes: 1 addition & 1 deletion test/spec/features/keyboard/UndoSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import keyboardModule from 'lib/features/keyboard';

import { createKeyEvent } from 'test/util/KeyEvents';

import { KEYS_UNDO } from 'lib/features/keyboard/KeyboardBindings';
var KEYS_UNDO = [ 'z', 'Z', 90 ];


describe('features/keyboard - undo', function() {
Expand Down

0 comments on commit 92acdee

Please sign in to comment.