From b34d72f21ac2d61aac3b931e4736de5c432c02f8 Mon Sep 17 00:00:00 2001 From: Leo Lam Date: Wed, 3 May 2017 00:29:18 -0400 Subject: [PATCH] revamp handling of multi-key presses - use keyCodes instead of strings to detect keys - delete key from keyPressed array on key up - fix #540 --- browser/components/MarkdownEditor.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/browser/components/MarkdownEditor.js b/browser/components/MarkdownEditor.js index 78904226..43d1a10d 100644 --- a/browser/components/MarkdownEditor.js +++ b/browser/components/MarkdownEditor.js @@ -9,14 +9,16 @@ class MarkdownEditor extends React.Component { constructor (props) { super(props) - this.escapeFromEditor = ['Control', 'w'] + //char codes for ctrl + w + this.escapeFromEditor = [17, 87] - this.supportMdSelectionBold = ['Control', ':'] + //ctrl + shift + ; + this.supportMdSelectionBold = [16, 17, 186] this.state = { status: 'PREVIEW', renderValue: props.value, - keyPressed: {}, + keyPressed: new Set(), isLocked: false } @@ -87,7 +89,7 @@ class MarkdownEditor extends React.Component { handleBlur (e) { if (this.state.isLocked) return - this.setState({ keyPressed: [] }) + this.setState({ keyPressed: new Set()}) let { config } = this.props if (config.editor.switchPreview === 'BLUR') { let cursorPosition = this.refs.code.editor.getCursor() @@ -161,11 +163,10 @@ class MarkdownEditor extends React.Component { handleKeyDown (e) { if (this.state.status !== 'CODE') return false - const keyPressed = Object.assign(this.state.keyPressed, { - [e.key]: true - }) + const keyPressed = this.state.keyPressed + keyPressed.add(e.keyCode) this.setState({ keyPressed }) - let isNoteHandlerKey = (el) => { return this.state.keyPressed[el] } + let isNoteHandlerKey = (el) => { return keyPressed.has(el) } if (!this.state.isLocked && this.state.status === 'CODE' && this.escapeFromEditor.every(isNoteHandlerKey)) { document.activeElement.blur() } @@ -190,9 +191,8 @@ class MarkdownEditor extends React.Component { } handleKeyUp (e) { - const keyPressed = Object.assign(this.state.keyPressed, { - [e.key]: false - }) + const keyPressed = this.state.keyPressed + keyPressed.delete(e.keyCode) this.setState({ keyPressed }) }