Some basic reply support.

This commit is contained in:
Black Hat 2018-09-17 11:58:02 +08:00
parent d02c3f6e90
commit 5ca03fdea8
7 changed files with 131 additions and 89 deletions

View File

@ -82,7 +82,8 @@ RowLayout {
selectByMouse: true selectByMouse: true
readOnly: true readOnly: true
wrapMode: Label.Wrap wrapMode: Label.Wrap
selectionColor: Material.accent selectedTextColor: highlighted ? Material.accent : "white"
selectionColor: highlighted ? "white" : Material.accent
textFormat: Text.RichText textFormat: Text.RichText
onLinkActivated: Qt.openUrlExternally(link) onLinkActivated: Qt.openUrlExternally(link)

View File

@ -332,108 +332,119 @@ Item {
onClicked: currentRoom.chooseAndUploadFile() onClicked: currentRoom.chooseAndUploadFile()
} }
TextField { ScrollView {
property real progress: 0
Layout.fillWidth: true Layout.fillWidth: true
Layout.preferredHeight: 48 Layout.preferredHeight: 48
id: inputField clip: true
placeholderText: "Send a Message" TextArea {
leftPadding: 16 property real progress: 0
topPadding: 0
bottomPadding: 0
selectByMouse: true
text: currentRoom ? currentRoom.cachedInput : "" id: inputField
onTextChanged: { placeholderText: "Send a Message"
timeoutTimer.restart() leftPadding: 16
repeatTimer.start() topPadding: 0
currentRoom.cachedInput = text bottomPadding: 0
} selectByMouse: true
verticalAlignment: TextEdit.AlignVCenter
Keys.onReturnPressed: { text: currentRoom ? currentRoom.cachedInput : ""
if (inputField.text) {
inputField.postMessage(inputField.text) onTextChanged: {
inputField.text = "" timeoutTimer.restart()
repeatTimer.start()
currentRoom.cachedInput = text
} }
}
background: Rectangle { background: Rectangle { color: MSettings.darkTheme ? "#282828" : "#eaeaea" }
color: MSettings.darkTheme ? "#282828" : "#eaeaea"
}
ToolTip.visible: currentRoom && currentRoom.hasUsersTyping ToolTip.visible: currentRoom && currentRoom.hasUsersTyping
ToolTip.text: currentRoom ? currentRoom.usersTyping : "" ToolTip.text: currentRoom ? currentRoom.usersTyping : ""
Timer { Shortcut {
id: timeoutTimer sequence: "Ctrl+Return"
onActivated: {
repeat: false inputField.postMessage(inputField.text)
interval: 2000 inputField.text = ""
onTriggered: {
repeatTimer.stop()
currentRoom.sendTypingNotification(false)
}
}
Timer {
id: repeatTimer
repeat: true
interval: 5000
triggeredOnStart: true
onTriggered: currentRoom.sendTypingNotification(true)
}
function postMessage(text) {
if (text.trim().length === 0) { return }
if(!currentRoom) { return }
var PREFIX_ME = '/me '
var PREFIX_NOTICE = '/notice '
var PREFIX_RAINBOW = '/rainbow '
var PREFIX_HTML = '/html '
var PREFIX_MARKDOWN = '/md '
if (text.indexOf(PREFIX_ME) === 0) {
text = text.substr(PREFIX_ME.length)
currentRoom.postMessage(text, RoomMessageEvent.Emote)
return
}
if (text.indexOf(PREFIX_NOTICE) === 0) {
text = text.substr(PREFIX_NOTICE.length)
currentRoom.postMessage(text, RoomMessageEvent.Notice)
return
}
if (text.indexOf(PREFIX_RAINBOW) === 0) {
text = text.substr(PREFIX_RAINBOW.length)
var parsedText = ""
var rainbowColor = ["#ff2b00", "#ff5500", "#ff8000", "#ffaa00", "#ffd500", "#ffff00", "#d4ff00", "#aaff00", "#80ff00", "#55ff00", "#2bff00", "#00ff00", "#00ff2b", "#00ff55", "#00ff80", "#00ffaa", "#00ffd5", "#00ffff", "#00d4ff", "#00aaff", "#007fff", "#0055ff", "#002bff", "#0000ff", "#2a00ff", "#5500ff", "#7f00ff", "#aa00ff", "#d400ff", "#ff00ff", "#ff00d4", "#ff00aa", "#ff0080", "#ff0055", "#ff002b", "#ff0000"]
for (var i = 0; i < text.length; i++) {
parsedText = parsedText + "<font color='" + rainbowColor[i % rainbowColor.length] + "'>" + text.charAt(i) + "</font>"
} }
currentRoom.postHtmlMessage(text, parsedText, RoomMessageEvent.Text)
return
}
if (text.indexOf(PREFIX_HTML) === 0) {
text = text.substr(PREFIX_HTML.length)
var re = new RegExp("<.*?>")
var plainText = text.replace(re, "")
currentRoom.postHtmlMessage(plainText, text, RoomMessageEvent.Text)
return
}
if (text.indexOf(PREFIX_MARKDOWN) === 0) {
text = text.substr(PREFIX_MARKDOWN.length)
var parsedText = Markdown.markdown_parser(text)
currentRoom.postHtmlMessage(text, parsedText, RoomMessageEvent.Text)
return
} }
currentRoom.postPlainText(text) Timer {
id: timeoutTimer
repeat: false
interval: 2000
onTriggered: {
repeatTimer.stop()
currentRoom.sendTypingNotification(false)
}
}
Timer {
id: repeatTimer
repeat: true
interval: 5000
triggeredOnStart: true
onTriggered: currentRoom.sendTypingNotification(true)
}
function postMessage(text) {
if (text.trim().length === 0) { return }
if(!currentRoom) { return }
var PREFIX_ME = '/me '
var PREFIX_NOTICE = '/notice '
var PREFIX_RAINBOW = '/rainbow '
var PREFIX_HTML = '/html '
var PREFIX_MARKDOWN = '/md '
var replyRe = new RegExp("^> <(.*)><(.*)> (.*)\n\n(.*)")
if (text.match(replyRe)) {
var matches = text.match(replyRe)
currentRoom.sendReply(matches[1], matches[2], matches[3], matches[4])
return
}
if (text.indexOf(PREFIX_ME) === 0) {
text = text.substr(PREFIX_ME.length)
currentRoom.postMessage(text, RoomMessageEvent.Emote)
return
}
if (text.indexOf(PREFIX_NOTICE) === 0) {
text = text.substr(PREFIX_NOTICE.length)
currentRoom.postMessage(text, RoomMessageEvent.Notice)
return
}
if (text.indexOf(PREFIX_RAINBOW) === 0) {
text = text.substr(PREFIX_RAINBOW.length)
var parsedText = ""
var rainbowColor = ["#ff2b00", "#ff5500", "#ff8000", "#ffaa00", "#ffd500", "#ffff00", "#d4ff00", "#aaff00", "#80ff00", "#55ff00", "#2bff00", "#00ff00", "#00ff2b", "#00ff55", "#00ff80", "#00ffaa", "#00ffd5", "#00ffff", "#00d4ff", "#00aaff", "#007fff", "#0055ff", "#002bff", "#0000ff", "#2a00ff", "#5500ff", "#7f00ff", "#aa00ff", "#d400ff", "#ff00ff", "#ff00d4", "#ff00aa", "#ff0080", "#ff0055", "#ff002b", "#ff0000"]
for (var i = 0; i < text.length; i++) {
parsedText = parsedText + "<font color='" + rainbowColor[i % rainbowColor.length] + "'>" + text.charAt(i) + "</font>"
}
currentRoom.postHtmlMessage(text, parsedText, RoomMessageEvent.Text)
return
}
if (text.indexOf(PREFIX_HTML) === 0) {
text = text.substr(PREFIX_HTML.length)
var re = new RegExp("<.*?>")
var plainText = text.replace(re, "")
currentRoom.postHtmlMessage(plainText, text, RoomMessageEvent.Text)
return
}
if (text.indexOf(PREFIX_MARKDOWN) === 0) {
text = text.substr(PREFIX_MARKDOWN.length)
var parsedText = Markdown.markdown_parser(text)
currentRoom.postHtmlMessage(text, parsedText, RoomMessageEvent.Text)
return
}
currentRoom.postPlainText(text)
}
} }
} }

View File

@ -41,6 +41,16 @@ Menu {
onTriggered: row.saveFileAs() onTriggered: row.saveFileAs()
} }
MenuItem {
visible: model && model.author !== currentRoom.localUser
height: visible ? undefined : 0
text: "Reply"
onTriggered: {
inputField.clear()
inputField.insert(0, "> <" + model.author.id + "><" + model.eventId + "> " + model.message + "\n\n")
}
}
MenuItem { MenuItem {
visible: model && model.author === currentRoom.localUser visible: model && model.author === currentRoom.localUser
height: visible ? undefined : 0 height: visible ? undefined : 0

View File

@ -124,3 +124,18 @@ void MatriqueRoom::countChanged() {
resetHighlightCount(); resetHighlightCount();
} }
} }
void MatriqueRoom::sendReply(QString userId, QString eventId,
QString replyContent, QString sendContent) {
QJsonObject json{
{"msgtype", "m.text"},
{"body", "> <" + userId + "> " + replyContent + "\n\n" + sendContent},
{"format", "org.matrix.custom.html"},
{"m.relates_to", QJsonObject{{"m.in_reply_to", QJsonObject{{"event_id", eventId}}}}},
{"formatted_body",
"<mx-reply><blockquote><a href=\"https://matrix.to/#/" + id() + "/" +
eventId + "\">In reply to</a> <a href=\"https://matrix.to/#/" +
userId + "\">" + userId + "</a><br>" + replyContent +
"</blockquote></mx-reply>" + sendContent}};
postJson("m.room.message", json);
}

View File

@ -59,6 +59,7 @@ class MatriqueRoom : public Room {
void acceptInvitation(); void acceptInvitation();
void forget(); void forget();
void sendTypingNotification(bool isTyping); void sendTypingNotification(bool isTyping);
void sendReply(QString userId, QString eventId, QString replyContent, QString sendContent);
}; };
#endif // MATRIQUEROOM_H #endif // MATRIQUEROOM_H

View File

@ -15,6 +15,7 @@
QHash<int, QByteArray> MessageEventModel::roleNames() const { QHash<int, QByteArray> MessageEventModel::roleNames() const {
QHash<int, QByteArray> roles = QAbstractItemModel::roleNames(); QHash<int, QByteArray> roles = QAbstractItemModel::roleNames();
roles[EventTypeRole] = "eventType"; roles[EventTypeRole] = "eventType";
roles[MessageRole] = "message";
roles[AboveEventTypeRole] = "aboveEventType"; roles[AboveEventTypeRole] = "aboveEventType";
roles[EventIdRole] = "eventId"; roles[EventIdRole] = "eventId";
roles[TimeRole] = "time"; roles[TimeRole] = "time";
@ -415,6 +416,8 @@ QVariant MessageEventModel::data(const QModelIndex& idx, int role) const {
tr("Unknown Event")); tr("Unknown Event"));
} }
if (role == MessageRole) return evt.contentJson().value("body");
if (role == Qt::ToolTipRole) { if (role == Qt::ToolTipRole) {
return evt.originalJson(); return evt.originalJson();
} }

View File

@ -13,6 +13,7 @@ class MessageEventModel : public QAbstractListModel {
public: public:
enum EventRoles { enum EventRoles {
EventTypeRole = Qt::UserRole + 1, EventTypeRole = Qt::UserRole + 1,
MessageRole,
AboveEventTypeRole, AboveEventTypeRole,
EventIdRole, EventIdRole,
TimeRole, TimeRole,