Cancel message correction by pressing escape.

Also fixes #1153
This commit is contained in:
JC Brand 2018-08-10 11:48:07 +02:00
parent 4c86cca2cb
commit 45d1440684
2 changed files with 83 additions and 19 deletions

51
dist/converse.js vendored
View File

@ -69428,9 +69428,15 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_
const u = converse.env.utils; const u = converse.env.utils;
const KEY = { const KEY = {
ENTER: 13, ENTER: 13,
SHIFT: 17,
CTRL: 17,
ALT: 18,
ESCAPE: 27,
UP_ARROW: 38, UP_ARROW: 38,
DOWN_ARROW: 40, DOWN_ARROW: 40,
FORWARD_SLASH: 47 FORWARD_SLASH: 47,
META: 91,
META_RIGHT: 93
}; };
converse.plugins.add('converse-chatview', { converse.plugins.add('converse-chatview', {
/* Plugin dependencies are other plugins which might be /* Plugin dependencies are other plugins which might be
@ -70296,17 +70302,31 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_
keyPressed(ev) { keyPressed(ev) {
/* Event handler for when a key is pressed in a chat box textarea. /* Event handler for when a key is pressed in a chat box textarea.
*/ */
if (ev.shiftKey) { if (ev.ctrlKey) {
// When ctrl is pressed, no chars are entered into the textarea.
return; return;
} }
if (ev.keyCode === KEY.ENTER) { if (!ev.shiftKey && !ev.altKey) {
this.onFormSubmitted(ev); if (ev.keyCode === KEY.FORWARD_SLASH) {
} else if (ev.keyCode === KEY.UP_ARROW && !ev.target.selectionEnd) { // Forward slash is used to run commands. Nothing to do here.
this.editEarlierMessage(); return;
} else if (ev.keyCode === KEY.DOWN_ARROW && ev.target.selectionEnd === ev.target.value.length) { } else if (ev.keyCode === KEY.ESCAPE) {
this.editLaterMessage(); return this.onEscapePressed(ev);
} else if (ev.keyCode !== KEY.FORWARD_SLASH && this.model.get('chat_state') !== _converse.COMPOSING) { } else if (ev.keyCode === KEY.ENTER) {
return this.onFormSubmitted(ev);
} else if (ev.keyCode === KEY.UP_ARROW && !ev.target.selectionEnd) {
return this.editEarlierMessage();
} else if (ev.keyCode === KEY.DOWN_ARROW && ev.target.selectionEnd === ev.target.value.length) {
return this.editLaterMessage();
}
}
if (_.includes([KEY.SHIFT, KEY.META, KEY.META_RIGHT, KEY.ESCAPE, KEY.ALT], ev.keyCode)) {
return;
}
if (this.model.get('chat_state') !== _converse.COMPOSING) {
// Set chat state to composing if keyCode is not a forward-slash // Set chat state to composing if keyCode is not a forward-slash
// (which would imply an internal command and not a message). // (which would imply an internal command and not a message).
this.setChatState(_converse.COMPOSING); this.setChatState(_converse.COMPOSING);
@ -70319,7 +70339,20 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_
})); }));
}, },
onEscapePressed(ev) {
ev.preventDefault();
const idx = this.model.messages.findLastIndex('correcting'),
message = idx >= 0 ? this.model.messages.at(idx) : null;
if (message) {
message.save('correcting', false);
}
this.insertIntoTextArea('', true, false);
},
onMessageEditButtonClicked(ev) { onMessageEditButtonClicked(ev) {
ev.preventDefault();
const idx = this.model.messages.findLastIndex('correcting'), const idx = this.model.messages.findLastIndex('correcting'),
currently_correcting = idx >= 0 ? this.model.messages.at(idx) : null, currently_correcting = idx >= 0 ? this.model.messages.at(idx) : null,
message_el = u.ancestor(ev.target, '.chat-msg'), message_el = u.ancestor(ev.target, '.chat-msg'),

View File

@ -52,9 +52,15 @@
const u = converse.env.utils; const u = converse.env.utils;
const KEY = { const KEY = {
ENTER: 13, ENTER: 13,
SHIFT: 17,
CTRL: 17,
ALT: 18,
ESCAPE: 27,
UP_ARROW: 38, UP_ARROW: 38,
DOWN_ARROW: 40, DOWN_ARROW: 40,
FORWARD_SLASH: 47 FORWARD_SLASH: 47,
META: 91,
META_RIGHT: 93
}; };
converse.plugins.add('converse-chatview', { converse.plugins.add('converse-chatview', {
@ -915,15 +921,28 @@
keyPressed (ev) { keyPressed (ev) {
/* Event handler for when a key is pressed in a chat box textarea. /* Event handler for when a key is pressed in a chat box textarea.
*/ */
if (ev.shiftKey) { return; } if (ev.ctrlKey) {
// When ctrl is pressed, no chars are entered into the textarea.
if (ev.keyCode === KEY.ENTER) { return;
this.onFormSubmitted(ev); }
} else if (ev.keyCode === KEY.UP_ARROW && !ev.target.selectionEnd) { if (!ev.shiftKey && !ev.altKey) {
this.editEarlierMessage(); if (ev.keyCode === KEY.FORWARD_SLASH) {
} else if (ev.keyCode === KEY.DOWN_ARROW && ev.target.selectionEnd === ev.target.value.length) { // Forward slash is used to run commands. Nothing to do here.
this.editLaterMessage(); return;
} else if (ev.keyCode !== KEY.FORWARD_SLASH && this.model.get('chat_state') !== _converse.COMPOSING) { } else if (ev.keyCode === KEY.ESCAPE) {
return this.onEscapePressed(ev);
} else if (ev.keyCode === KEY.ENTER) {
return this.onFormSubmitted(ev);
} else if (ev.keyCode === KEY.UP_ARROW && !ev.target.selectionEnd) {
return this.editEarlierMessage();
} else if (ev.keyCode === KEY.DOWN_ARROW && ev.target.selectionEnd === ev.target.value.length) {
return this.editLaterMessage();
}
}
if (_.includes([KEY.SHIFT, KEY.META, KEY.META_RIGHT, KEY.ESCAPE, KEY.ALT], ev.keyCode)) {
return;
}
if (this.model.get('chat_state') !== _converse.COMPOSING) {
// Set chat state to composing if keyCode is not a forward-slash // Set chat state to composing if keyCode is not a forward-slash
// (which would imply an internal command and not a message). // (which would imply an internal command and not a message).
this.setChatState(_converse.COMPOSING); this.setChatState(_converse.COMPOSING);
@ -934,7 +953,19 @@
return f(this.model.messages.filter({'sender': 'me'})); return f(this.model.messages.filter({'sender': 'me'}));
}, },
onEscapePressed (ev) {
ev.preventDefault();
const idx = this.model.messages.findLastIndex('correcting'),
message = idx >=0 ? this.model.messages.at(idx) : null;
if (message) {
message.save('correcting', false);
}
this.insertIntoTextArea('', true, false);
},
onMessageEditButtonClicked (ev) { onMessageEditButtonClicked (ev) {
ev.preventDefault();
const idx = this.model.messages.findLastIndex('correcting'), const idx = this.model.messages.findLastIndex('correcting'),
currently_correcting = idx >=0 ? this.model.messages.at(idx) : null, currently_correcting = idx >=0 ? this.model.messages.at(idx) : null,
message_el = u.ancestor(ev.target, '.chat-msg'), message_el = u.ancestor(ev.target, '.chat-msg'),