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

[FIX] Handle all events for enter key in message box #12507

Merged
merged 2 commits into from
Nov 1, 2018
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
9 changes: 7 additions & 2 deletions packages/rocketchat-ui-message/client/messageBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -425,19 +425,24 @@ Template.messageBox.events({
return instance.isMessageFieldEmpty.set(false);
}
},

'keydown .js-input-message': firefoxPasteUpload(function(event, t) {
if ((navigator.platform.indexOf('Mac') !== -1 && event.metaKey) || (navigator.platform.indexOf('Mac') === -1 && event.ctrlKey)) {
const action = markdownButtons.find((action) => action.command === event.key.toLowerCase() && (!action.condition || action.condition()));
const isMacOS = navigator.platform.indexOf('Mac') !== -1;
if (isMacOS && (event.metaKey || event.ctrlKey)) {
const action = markdownButtons.find(
(action) => action.command === event.key.toLowerCase() && (!action.condition || action.condition()));
if (action) {
applyMd.apply(action, [event, t]);
}
}
return chatMessages[this._id].keydown(this._id, event, Template.instance());
}),

'input .js-input-message'(event, instance) {
instance.sendIcon.set(event.target.value !== '');
return chatMessages[this._id].valueChanged(this._id, event, Template.instance());
},

'propertychange .js-input-message'(event) {
if (event.originalEvent.propertyName === 'value') {
return chatMessages[this._id].valueChanged(this._id, event, Template.instance());
Expand Down
53 changes: 32 additions & 21 deletions packages/rocketchat-ui/client/lib/chatMessages.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,23 @@ this.ChatMessages = class ChatMessages {
}
}

insertNewLine(input) {
if (document.selection) {
input.focus();
const sel = document.selection.createRange();
sel.text = '\n';
} else if (input.selectionStart || input.selectionStart === 0) {
const before = input.value.substring(0, input.selectionStart);
const after = input.value.substring(input.selectionEnd, input.value.length);
input.value = `${ before }\n${ after }`;
} else {
input.value += '\n';
}
input.blur();
input.focus();
typeof input.updateAutogrow === 'function' && input.updateAutogrow();
}

restoreText(rid) {
const text = localStorage.getItem(`messagebox_${ rid }`);
if (typeof text === 'string' && this.input) {
Expand Down Expand Up @@ -475,30 +492,24 @@ this.ChatMessages = class ChatMessages {
}

keydown(rid, event) {
const input = event.currentTarget;
// const $input = $(input);
const k = event.which;
const { currentTarget: input, which: k } = event;

if (k === 13) {
if ((sendOnEnter == null || sendOnEnter === 'normal' || sendOnEnter === 'desktop') && Meteor.Device.isDesktop()) {
if (!event.shiftKey && !event.ctrlKey && !event.altKey && !event.metaKey) { // Enter without shift/ctrl/alt
event.preventDefault();
event.stopPropagation();
this.send(rid, input);
return;
} else if (!event.shiftKey) {
return input.value += '\n';
}
} else if (sendOnEnter === 'alternative') {
if (event.shiftKey || event.ctrlKey || event.altKey || event.metaKey) { // Enter with shift/ctrl/alt
event.preventDefault();
event.stopPropagation();
this.send(rid, input);
return;
}
if (k === 13 || k === 10) { // New line or carriage return
const sendOnEnterActive = sendOnEnter == null || sendOnEnter === 'normal' ||
(sendOnEnter === 'desktop' && Meteor.Device.isDesktop());
const withModifier = event.shiftKey || event.ctrlKey || event.altKey || event.metaKey;
const isSending = (sendOnEnterActive && !withModifier) || (!sendOnEnterActive && withModifier);

event.preventDefault();
event.stopPropagation();
if (isSending) {
this.send(rid, input);
} else {
this.insertNewLine(input);
}
}

return;
}

if (k === 9) { // Tab
event.preventDefault();
Expand Down