From 97268314fedf27e4d3896059ac4e22a519160791 Mon Sep 17 00:00:00 2001 From: Jori Lallo Date: Sun, 28 Jan 2018 17:57:45 -0800 Subject: [PATCH] Map keyboard shortcuts on windows (#526) * Map keyboard shortcuts on windows * Fixed also saving commands * Fix keyboard shortcut for keyboard shortcuts * Fixed guide * Cmd -> Mod --- app/components/Editor/Editor.js | 3 ++- app/components/Editor/plugins/KeyboardShortcuts.js | 3 ++- app/components/Editor/utils.js | 11 +++++++++++ app/scenes/KeyboardShortcuts/KeyboardShortcuts.js | 2 +- 4 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 app/components/Editor/utils.js diff --git a/app/components/Editor/Editor.js b/app/components/Editor/Editor.js index 41d5627f8..98ef5fbcc 100644 --- a/app/components/Editor/Editor.js +++ b/app/components/Editor/Editor.js @@ -19,6 +19,7 @@ import { insertImageFile } from './changes'; import renderMark from './marks'; import createRenderNode from './nodes'; import schema from './schema'; +import { isModKey } from './utils'; import styled from 'styled-components'; type Props = { @@ -136,7 +137,7 @@ class MarkdownEditor extends Component { // Handling of keyboard shortcuts within editor focus onKeyDown = (ev: SyntheticKeyboardEvent, change: Change) => { - if (!ev.metaKey) return; + if (!isModKey(ev)) return; switch (ev.key) { case 's': diff --git a/app/components/Editor/plugins/KeyboardShortcuts.js b/app/components/Editor/plugins/KeyboardShortcuts.js index 7ec48efca..5cc6dd732 100644 --- a/app/components/Editor/plugins/KeyboardShortcuts.js +++ b/app/components/Editor/plugins/KeyboardShortcuts.js @@ -1,10 +1,11 @@ // @flow import { Change } from 'slate'; +import { isModKey } from '../utils'; export default function KeyboardShortcuts() { return { onKeyDown(ev: SyntheticKeyboardEvent, change: Change) { - if (!ev.metaKey) return null; + if (!isModKey(ev)) return null; switch (ev.key) { case 'b': diff --git a/app/components/Editor/utils.js b/app/components/Editor/utils.js new file mode 100644 index 000000000..1425838af --- /dev/null +++ b/app/components/Editor/utils.js @@ -0,0 +1,11 @@ +// @flow + +/** + * Detect Cmd or Ctrl by platform for keyboard shortcuts + */ +export function isModKey(event: SyntheticKeyboardEvent) { + const isMac = + typeof window !== 'undefined' && + /Mac|iPod|iPhone|iPad/.test(window.navigator.platform); + return isMac ? event.metaKey : event.ctrlKey; +} diff --git a/app/scenes/KeyboardShortcuts/KeyboardShortcuts.js b/app/scenes/KeyboardShortcuts/KeyboardShortcuts.js index 7212bebc0..9c9c5db17 100644 --- a/app/scenes/KeyboardShortcuts/KeyboardShortcuts.js +++ b/app/scenes/KeyboardShortcuts/KeyboardShortcuts.js @@ -38,7 +38,7 @@ function KeyboardShortcuts() { - + / + ?