Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(keyboard): make function detection available via explicit utilities #664

Merged
merged 1 commit into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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