2020-06-17 19:18:31 +00:00
|
|
|
import QtQuick 2.13
|
|
|
|
import QtQuick.Controls 2.13
|
|
|
|
import QtQuick.Layouts 1.13
|
2020-06-30 20:01:37 +00:00
|
|
|
import QtMultimedia 5.13
|
2020-07-20 17:04:33 +00:00
|
|
|
import QtQuick.Dialogs 1.0
|
2020-06-24 03:23:49 +00:00
|
|
|
import "../components"
|
2020-05-27 23:06:41 +00:00
|
|
|
import "../../../../shared"
|
|
|
|
import "../../../../imports"
|
|
|
|
|
|
|
|
Rectangle {
|
2020-07-09 19:10:28 +00:00
|
|
|
id: rectangle
|
2020-07-14 11:40:58 +00:00
|
|
|
property alias textInput: txtData
|
2020-05-27 23:06:41 +00:00
|
|
|
border.width: 0
|
2020-07-09 19:10:28 +00:00
|
|
|
height: 52
|
2020-07-13 18:45:54 +00:00
|
|
|
color: Style.current.transparent
|
2020-05-27 23:06:41 +00:00
|
|
|
|
2020-07-02 18:49:02 +00:00
|
|
|
visible: chatsModel.activeChannel.chatType !== Constants.chatTypePrivateGroupChat || chatsModel.activeChannel.isMember(profileModel.profile.pubKey)
|
2020-06-15 17:41:19 +00:00
|
|
|
|
2020-09-02 18:18:16 +00:00
|
|
|
property bool emojiEvent: false;
|
2020-09-04 14:06:50 +00:00
|
|
|
property bool paste: false;
|
|
|
|
property bool isColonPressed: false;
|
2020-09-02 18:18:16 +00:00
|
|
|
|
2020-07-10 19:02:57 +00:00
|
|
|
Audio {
|
|
|
|
id: sendMessageSound
|
|
|
|
source: "../../../../sounds/send_message.wav"
|
2020-08-19 19:56:41 +00:00
|
|
|
volume: 0.2
|
2020-07-10 19:02:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function interpretMessage(msg) {
|
|
|
|
if (msg === "/shrug") {
|
|
|
|
return "¯\\\\\\_(ツ)\\_/¯"
|
|
|
|
}
|
|
|
|
if (msg === "/tableflip") {
|
|
|
|
return "(╯°□°)╯︵ ┻━┻"
|
|
|
|
}
|
|
|
|
|
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
2020-08-03 14:01:47 +00:00
|
|
|
function sendMsg(event){
|
|
|
|
if(chatColumn.isImage){
|
|
|
|
chatsModel.sendImage(sendImageArea.image);
|
|
|
|
}
|
|
|
|
var msg = chatsModel.plainText(Emoji.deparse(txtData.text).trim()).trim()
|
|
|
|
if(msg.length > 0){
|
|
|
|
msg = interpretMessage(msg)
|
|
|
|
chatsModel.sendMessage(msg, chatColumn.isReply ? SelectedMessage.messageId : "", Utils.isOnlyEmoji(msg) ? Constants.emojiType : Constants.messageType);
|
|
|
|
txtData.text = "";
|
|
|
|
if(event) event.accepted = true
|
|
|
|
sendMessageSound.stop()
|
|
|
|
Qt.callLater(sendMessageSound.play);
|
|
|
|
}
|
|
|
|
chatColumn.hideExtendedArea();
|
|
|
|
}
|
2020-07-14 11:40:58 +00:00
|
|
|
|
2020-08-03 14:01:47 +00:00
|
|
|
function onEnter(event){
|
2020-07-02 18:49:02 +00:00
|
|
|
if (event.modifiers === Qt.NoModifier && (event.key === Qt.Key_Enter || event.key === Qt.Key_Return)) {
|
2020-08-03 14:01:47 +00:00
|
|
|
sendMsg(event);
|
2020-06-25 02:01:13 +00:00
|
|
|
}
|
2020-09-04 14:06:50 +00:00
|
|
|
|
|
|
|
if ((event.key == Qt.Key_V) && (event.modifiers & Qt.ControlModifier)) {
|
|
|
|
paste = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
isColonPressed = (event.key == Qt.Key_Colon) && (event.modifiers & Qt.ShiftModifier);
|
2020-09-03 00:47:55 +00:00
|
|
|
}
|
2020-09-02 18:18:16 +00:00
|
|
|
|
2020-09-03 00:47:55 +00:00
|
|
|
function onRelease(event) {
|
2020-09-04 14:06:50 +00:00
|
|
|
// the text doesn't get registered to the textarea fast enough
|
|
|
|
// we can only get it in the `released` event
|
|
|
|
if (paste) {
|
|
|
|
paste = false;
|
|
|
|
interrogateMessage();
|
|
|
|
}
|
|
|
|
|
|
|
|
emojiEvent = emojiHandler(event);
|
2020-09-02 18:18:16 +00:00
|
|
|
}
|
|
|
|
|
2020-09-03 00:47:55 +00:00
|
|
|
function onMouseClicked() {
|
2020-09-04 14:06:50 +00:00
|
|
|
emojiEvent = emojiHandler({key: null});
|
|
|
|
}
|
|
|
|
|
|
|
|
function interrogateMessage() {
|
|
|
|
const text = chatsModel.plainText(Emoji.deparse(txtData.text));
|
|
|
|
var words = text.split(' ');
|
|
|
|
|
|
|
|
for (var i = 0; i < words.length; i++) {
|
|
|
|
var transform = true;
|
|
|
|
if (words[i].charAt(0) === ':') {
|
|
|
|
for (var j = 0; j < words[i].length; j++) {
|
|
|
|
if (isSpace(words[i].charAt(j)) === true || isPunct(words[i].charAt(j)) === true) {
|
|
|
|
transform = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (transform) {
|
|
|
|
const codePoint = Emoji.getEmojiUnicode(words[i]);
|
|
|
|
words[i] = words[i].replace(words[i], (codePoint !== undefined) ? Emoji.fromCodePoint(codePoint) : words[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
txtData.remove(0, txtData.length);
|
|
|
|
txtData.insert(0, Emoji.parse(words.join(' '), '26x26'));
|
2020-09-03 00:47:55 +00:00
|
|
|
}
|
2020-09-02 18:18:16 +00:00
|
|
|
|
2020-09-04 14:06:50 +00:00
|
|
|
function emojiHandler(event) {
|
|
|
|
let message = extrapolateCursorPosition();
|
|
|
|
pollEmojiEvent(message);
|
2020-09-03 22:37:57 +00:00
|
|
|
|
2020-09-04 14:06:50 +00:00
|
|
|
// state machine to handle different forms of the emoji event state
|
|
|
|
if (!emojiEvent && isColonPressed) {
|
|
|
|
return (message.data.length <= 1 || isSpace(message.data.charAt(message.cursor - 1))) ? true : false;
|
|
|
|
} else if (emojiEvent && isColonPressed) {
|
|
|
|
const index = message.data.lastIndexOf(':', message.cursor - 2);
|
|
|
|
if (index >= 0 && message.cursor > 0) {
|
|
|
|
const shortname = message.data.substr(index, message.cursor);
|
|
|
|
const codePoint = Emoji.getEmojiUnicode(shortname);
|
|
|
|
const newMessage = message.data.replace(shortname, (codePoint !== undefined) ? Emoji.fromCodePoint(codePoint) : shortname);
|
|
|
|
txtData.remove(0, txtData.cursorPosition);
|
|
|
|
txtData.insert(0, Emoji.parse(newMessage, '26x26'));
|
|
|
|
return false;
|
2020-09-03 22:37:57 +00:00
|
|
|
}
|
2020-09-04 14:06:50 +00:00
|
|
|
return true;
|
|
|
|
} else if (emojiEvent && isKeyValid(event.key) && !isColonPressed) {
|
|
|
|
console.log('popup');
|
|
|
|
return true;
|
|
|
|
} else if (emojiEvent && !isKeyValid(event.key) && !isColonPressed) {
|
|
|
|
return false;
|
2020-09-03 22:37:57 +00:00
|
|
|
}
|
2020-09-04 14:06:50 +00:00
|
|
|
return false;
|
2020-09-03 22:37:57 +00:00
|
|
|
}
|
|
|
|
|
2020-09-04 14:06:50 +00:00
|
|
|
// since emoji length is not 1 we need to match that position that TextArea returns
|
|
|
|
// to the actual position in the string.
|
|
|
|
function extrapolateCursorPosition() {
|
|
|
|
// we need only the message part to be html
|
|
|
|
const text = chatsModel.plainText(Emoji.deparse(txtData.text));
|
|
|
|
const plainText = Emoji.parse(text, '26x26');
|
|
|
|
|
2020-09-03 22:37:57 +00:00
|
|
|
var bracketEvent = false;
|
|
|
|
var length = 0;
|
|
|
|
|
2020-09-04 14:06:50 +00:00
|
|
|
for (var i = 0; i < plainText.length;) {
|
|
|
|
if (length >= txtData.cursorPosition) break;
|
2020-09-03 22:37:57 +00:00
|
|
|
|
2020-09-04 14:06:50 +00:00
|
|
|
if (!bracketEvent && plainText.charAt(i) !== '<') {
|
2020-09-03 22:37:57 +00:00
|
|
|
i++;
|
|
|
|
length++;
|
2020-09-04 14:06:50 +00:00
|
|
|
} else if (!bracketEvent && plainText.charAt(i) === '<') {
|
2020-09-03 22:37:57 +00:00
|
|
|
bracketEvent = true;
|
|
|
|
i++;
|
2020-09-04 14:06:50 +00:00
|
|
|
} else if (bracketEvent && plainText.charAt(i) !== '>') {
|
2020-09-03 22:37:57 +00:00
|
|
|
i++;
|
2020-09-04 14:06:50 +00:00
|
|
|
} else if (bracketEvent && plainText.charAt(i) === '>') {
|
2020-09-03 22:37:57 +00:00
|
|
|
bracketEvent = false;
|
|
|
|
i++;
|
|
|
|
length++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-04 14:06:50 +00:00
|
|
|
let textBeforeCursor = Emoji.deparseFromParse(plainText.substr(0, i));
|
2020-09-03 22:37:57 +00:00
|
|
|
|
2020-09-04 14:06:50 +00:00
|
|
|
return {
|
|
|
|
cursor: countEmojiLengths(plainText.substr(0, i)) + txtData.cursorPosition,
|
|
|
|
data: chatsModel.plainText(Emoji.deparseFromParse(textBeforeCursor)),
|
|
|
|
};
|
2020-09-03 22:37:57 +00:00
|
|
|
}
|
|
|
|
|
2020-09-04 14:06:50 +00:00
|
|
|
function countEmojiLengths(value) {
|
|
|
|
const match = Emoji.getEmojis(value);
|
|
|
|
var length = 0;
|
2020-09-03 00:47:55 +00:00
|
|
|
|
2020-09-04 14:06:50 +00:00
|
|
|
if (match && match.length > 0) {
|
|
|
|
for (var i = 0; i < match.length; i++) {
|
|
|
|
length += Emoji.deparseFromParse(match[i]).length;
|
2020-09-03 00:47:55 +00:00
|
|
|
}
|
2020-09-04 14:06:50 +00:00
|
|
|
length = length - match.length;
|
2020-09-02 18:18:16 +00:00
|
|
|
}
|
2020-09-04 14:06:50 +00:00
|
|
|
return length;
|
|
|
|
}
|
2020-09-02 18:18:16 +00:00
|
|
|
|
2020-09-04 14:06:50 +00:00
|
|
|
// check if user has placed cursor near valid emoji colon token
|
|
|
|
function pollEmojiEvent(message) {
|
|
|
|
const index = message.data.lastIndexOf(':', message.cursor);
|
|
|
|
if (index >= 0) {
|
|
|
|
emojiEvent = validSubstr(message.data.substr(index, message.cursor - index));
|
|
|
|
}
|
2020-09-02 18:18:16 +00:00
|
|
|
}
|
|
|
|
|
2020-09-03 00:47:55 +00:00
|
|
|
function validSubstr(substr) {
|
|
|
|
for(var i = 0; i < substr.length; i++) {
|
|
|
|
var c = substr.charAt(i);
|
|
|
|
if (isSpace(c) === true || isPunct(c) === true)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-09-02 18:18:16 +00:00
|
|
|
function isKeyValid(key) {
|
2020-09-03 00:47:55 +00:00
|
|
|
if (key === Qt.Key_Space || key === Qt.Key_Tab ||
|
|
|
|
(key >= Qt.Key_Exclam && key <= Qt.Key_Slash) ||
|
|
|
|
(key >= Qt.Key_Semicolon && key <= Qt.Key_Question) ||
|
|
|
|
(key >= Qt.Key_BracketLeft && key <= Qt.Key_hyphen))
|
|
|
|
return false;
|
|
|
|
return true;
|
2020-09-02 18:18:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function isSpace(c) {
|
2020-09-03 00:47:55 +00:00
|
|
|
if (/( |\t|\n|\r)/.test(c))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-04 14:06:50 +00:00
|
|
|
function isPunct(c) {
|
|
|
|
if (/(!|\@|#|\$|%|\^|&|\*|\(|\)|_|\+|\||-|=|\\|{|}|[|]|"|;|'|<|>|\?|,|\.|\/)/.test(c))
|
2020-09-02 18:18:16 +00:00
|
|
|
return true;
|
|
|
|
return false;
|
2020-06-25 02:01:13 +00:00
|
|
|
}
|
|
|
|
|
2020-07-20 17:04:33 +00:00
|
|
|
FileDialog {
|
|
|
|
id: imageDialog
|
2020-08-26 15:52:26 +00:00
|
|
|
//% "Please choose an image"
|
|
|
|
title: qsTrId("please-choose-an-image")
|
2020-07-20 17:04:33 +00:00
|
|
|
folder: shortcuts.pictures
|
|
|
|
nameFilters: [
|
2020-08-26 15:52:26 +00:00
|
|
|
//% "Image files (*.jpg *.jpeg *.png)"
|
|
|
|
qsTrId("image-files----jpg---jpeg---png-")
|
2020-07-20 17:04:33 +00:00
|
|
|
]
|
|
|
|
onAccepted: {
|
|
|
|
chatColumn.showImageArea(imageDialog.fileUrls);
|
|
|
|
txtData.forceActiveFocus();
|
|
|
|
}
|
|
|
|
onRejected: {
|
|
|
|
chatColumn.hideExtendedArea();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-09 19:10:28 +00:00
|
|
|
ScrollView {
|
|
|
|
id: scrollView
|
|
|
|
anchors.bottom: parent.bottom
|
|
|
|
anchors.left: parent.left
|
|
|
|
anchors.top: parent.top
|
|
|
|
anchors.right: sendBtns.left
|
|
|
|
anchors.rightMargin: 0
|
2020-07-31 15:25:45 +00:00
|
|
|
ScrollBar.horizontal.policy: ScrollBar.AlwaysOff
|
|
|
|
topPadding: Style.current.padding
|
|
|
|
|
2020-07-09 19:10:28 +00:00
|
|
|
StyledTArea {
|
2020-07-31 21:30:55 +00:00
|
|
|
textFormat: Text.RichText
|
2020-07-09 19:10:28 +00:00
|
|
|
id: txtData
|
|
|
|
text: ""
|
|
|
|
selectByMouse: true
|
2020-07-31 15:25:45 +00:00
|
|
|
wrapMode: TextArea.Wrap
|
2020-07-09 19:10:28 +00:00
|
|
|
font.pixelSize: 15
|
|
|
|
//% "Type a message..."
|
|
|
|
placeholderText: qsTrId("type-a-message")
|
|
|
|
Keys.onPressed: onEnter(event)
|
2020-09-03 00:47:55 +00:00
|
|
|
Keys.onReleased: onRelease(event) // gives much more up to date cursorPosition
|
2020-07-09 19:10:28 +00:00
|
|
|
background: Rectangle {
|
2020-07-22 20:16:06 +00:00
|
|
|
color: Style.current.transparent
|
2020-07-02 18:49:02 +00:00
|
|
|
}
|
2020-09-03 00:47:55 +00:00
|
|
|
|
|
|
|
TapHandler {
|
|
|
|
id: mousearea
|
|
|
|
onTapped: onMouseClicked()
|
|
|
|
}
|
2020-06-24 03:23:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-09 19:10:28 +00:00
|
|
|
ChatButtons {
|
|
|
|
id: sendBtns
|
2020-07-16 06:23:00 +00:00
|
|
|
height: parent.height
|
2020-07-09 19:10:28 +00:00
|
|
|
anchors.verticalCenter: parent.verticalCenter
|
|
|
|
anchors.right: parent.right
|
|
|
|
addToChat: function (text) {
|
|
|
|
txtData.insert(txtData.length, text)
|
|
|
|
}
|
2020-08-03 14:01:47 +00:00
|
|
|
onSend: function(){
|
|
|
|
sendMsg(false)
|
|
|
|
}
|
2020-07-09 19:10:28 +00:00
|
|
|
}
|
2020-05-28 19:41:31 +00:00
|
|
|
}
|
|
|
|
/*##^##
|
|
|
|
Designer {
|
2020-07-10 21:47:31 +00:00
|
|
|
D{i:0;formeditorColor:"#ffffff"}
|
2020-05-28 19:41:31 +00:00
|
|
|
}
|
|
|
|
##^##*/
|