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

[staging] Fix keyboard shortcuts on Edge and non-English layouts #2334

Merged
merged 2 commits into from
Sep 14, 2020
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
5 changes: 3 additions & 2 deletions lib/app-layout/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,12 @@ export class AppLayout extends Component<Props> {
keyboardShortcutsAreOpen,
showKeyboardShortcuts,
} = this.props;
const { ctrlKey, code, metaKey } = event;
const { ctrlKey, metaKey } = event;
const key = event.key.toLowerCase();

const cmdOrCtrl = ctrlKey || metaKey;

if (cmdOrCtrl && code === 'Slash') {
if (cmdOrCtrl && key === '/') {
keyboardShortcutsAreOpen
? hideKeyboardShortcuts()
: showKeyboardShortcuts();
Expand Down
17 changes: 9 additions & 8 deletions lib/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,14 @@ class AppComponent extends Component<Props> {
if (!hotkeysEnabled) {
return;
}
const { code, ctrlKey, metaKey, shiftKey } = event;
const { ctrlKey, metaKey, shiftKey } = event;
const key = event.key.toLowerCase();

// Is either cmd or ctrl pressed? (But not both)
const cmdOrCtrl = (ctrlKey || metaKey) && ctrlKey !== metaKey;

// open tag list
if (cmdOrCtrl && shiftKey && 'KeyU' === code) {
if (cmdOrCtrl && shiftKey && 'u' === key) {
this.props.toggleTagList();

event.stopPropagation();
Expand All @@ -92,8 +93,8 @@ class AppComponent extends Component<Props> {
}

if (
(cmdOrCtrl && shiftKey && 'KeyS' === code) ||
(cmdOrCtrl && !shiftKey && 'KeyF' === code)
(cmdOrCtrl && shiftKey && 's' === key) ||
(cmdOrCtrl && !shiftKey && 'f' === key)
) {
this.props.focusSearchField();

Expand All @@ -102,19 +103,19 @@ class AppComponent extends Component<Props> {
return false;
}

if (('Escape' === code || 'Esc' === code) && this.props.isSearchActive) {
if (('Escape' === key || 'Esc' === key) && this.props.isSearchActive) {
this.props.clearSearch();
}

if (cmdOrCtrl && shiftKey && 'KeyF' === code) {
if (cmdOrCtrl && shiftKey && 'f' === key) {
this.props.toggleFocusMode();

event.stopPropagation();
event.preventDefault();
return false;
}

if (cmdOrCtrl && shiftKey && 'KeyI' === code) {
if (cmdOrCtrl && shiftKey && 'i' === key) {
this.props.createNote();

event.stopPropagation();
Expand All @@ -123,7 +124,7 @@ class AppComponent extends Component<Props> {
}

// prevent default browser behavior for search and find
if (cmdOrCtrl && ('KeyG' === code || 'KeyF' === code)) {
if (cmdOrCtrl && ('g' === key || 'f' === key)) {
event.stopPropagation();
event.preventDefault();
}
Expand Down
7 changes: 4 additions & 3 deletions lib/note-content-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -161,17 +161,18 @@ class NoteContentEditor extends Component<Props> {
};

handleShortcut = (event: KeyboardEvent) => {
const { ctrlKey, code, metaKey, shiftKey } = event;
const { ctrlKey, metaKey, shiftKey } = event;
const key = event.key.toLowerCase();

const cmdOrCtrl = ctrlKey || metaKey;
if (cmdOrCtrl && shiftKey && code === 'KeyG') {
if (cmdOrCtrl && shiftKey && key === 'g') {
this.setPrevSearchSelection();
event.stopPropagation();
event.preventDefault();
return false;
}

if (cmdOrCtrl && !shiftKey && code === 'KeyG') {
if (cmdOrCtrl && !shiftKey && key === 'g') {
this.setNextSearchSelection();
event.stopPropagation();
event.preventDefault();
Expand Down
10 changes: 6 additions & 4 deletions lib/note-editor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,15 @@ export class NoteEditor extends Component<Props> {
if (!this.props.keyboardShortcuts) {
return;
}
const { code, ctrlKey, metaKey, shiftKey } = event;

const { ctrlKey, metaKey, shiftKey } = event;
const key = event.key.toLowerCase();
const { note, noteId, toggleMarkdown } = this.props;

const cmdOrCtrl = ctrlKey || metaKey;

// toggle Markdown enabled
if (note && cmdOrCtrl && shiftKey && 'KeyM' === code) {
if (note && cmdOrCtrl && shiftKey && 'm' === key) {
console.log('toggling markdown');
toggleMarkdown(noteId, !this.markdownEnabled());
event.stopPropagation();
Expand All @@ -64,15 +66,15 @@ export class NoteEditor extends Component<Props> {
}

// toggle editor mode
if (cmdOrCtrl && shiftKey && 'KeyP' === code && this.markdownEnabled()) {
if (cmdOrCtrl && shiftKey && 'p' === key && this.markdownEnabled()) {
this.props.toggleEditMode();
event.stopPropagation();
event.preventDefault();
return false;
}

// toggle between tag editor and note editor
if (shiftKey && cmdOrCtrl && 'KeyY' === code && this.props.isEditorActive) {
if (shiftKey && cmdOrCtrl && 'y' === key && this.props.isEditorActive) {
// prefer focusing the edit field first
if (!this.editFieldHasFocus() || this.props.isSearchActive) {
this.focusNoteEditor?.();
Expand Down
11 changes: 6 additions & 5 deletions lib/note-list/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -216,35 +216,36 @@ export class NoteList extends Component<Props> {
if (!this.props.keyboardShortcuts) {
return;
}
const { ctrlKey, code, metaKey, shiftKey } = event;
const { ctrlKey, metaKey, shiftKey } = event;
const key = event.key.toLowerCase();
const { isSmallScreen, showNoteList } = this.props;

const cmdOrCtrl = ctrlKey || metaKey;
if (cmdOrCtrl && shiftKey && code === 'KeyK') {
if (cmdOrCtrl && shiftKey && key === 'k') {
this.props.selectNoteAbove();

event.stopPropagation();
event.preventDefault();
return false;
}

if (cmdOrCtrl && shiftKey && code === 'KeyJ') {
if (cmdOrCtrl && shiftKey && key === 'j') {
this.props.selectNoteBelow();

event.stopPropagation();
event.preventDefault();
return false;
}

if (isSmallScreen && cmdOrCtrl && shiftKey && code === 'KeyL') {
if (isSmallScreen && cmdOrCtrl && shiftKey && key === 'l') {
this.props.toggleNoteList();

event.stopPropagation();
event.preventDefault();
return false;
}

if (isSmallScreen && showNoteList && code === 'Enter') {
if (isSmallScreen && showNoteList && key === 'Enter') {
this.props.openNote();

event.stopPropagation();
Expand Down
4 changes: 2 additions & 2 deletions lib/tag-field/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export class TagField extends Component<Props, OwnState> {

preventStealingFocus = ({
ctrlKey,
code,
key,
metaKey,
shiftKey,
}: KeyboardEvent) => {
Expand All @@ -163,7 +163,7 @@ export class TagField extends Component<Props, OwnState> {
}
const cmdOrCtrl = ctrlKey || metaKey;

if (cmdOrCtrl && shiftKey && 'KeyY' === code) {
if (cmdOrCtrl && shiftKey && 'y' === key.toLowerCase()) {
this.setState({ selectedTag: '' as T.TagName });
}

Expand Down