Add rainbow support.

This commit is contained in:
Black Hat 2018-07-11 16:07:04 +08:00
parent 65ff905a7c
commit afc901b017
2 changed files with 37 additions and 29 deletions

View File

@ -193,36 +193,42 @@ Item {
return
}
var type = "m.text"
var PREFIX_ME = '/me '
var PREFIX_RAINBOW = '/rainbow '
var PREFIX_HTML = '/html '
var PREFIX_MARKDOWN = '/md '
if (text.indexOf(PREFIX_ME) === 0) {
text = text.substr(PREFIX_ME.length)
type = "m.emote"
currentRoom.postMessage("m.emote", text)
return
}
if (text.indexOf(PREFIX_RAINBOW) === 0) {
text = text.substr(PREFIX_RAINBOW.length)
var parsedText = ""
var rainbowColor = ["#ee0000", "#ff7700", "#eeee00", "#00bb00", "#0000ee", "#dd00dd", "#880088"]
for (var i = 0; i < text.length; i++) {
parsedText = parsedText + "<font color='" + rainbowColor[i % 7] + "'>" + text.charAt(i) + "</font>"
}
var result = parse(text)
var parsedText = result[0]
var isMarkdown = result [1]
if (isMarkdown) currentRoom.postHtmlMessage(text, parsedText, type)
else currentRoom.postMessage(type, text)
currentRoom.postHtmlMessage(text, parsedText, "m.text")
return
}
function parse(text) {
if (!testHTML(text)) {
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, "m.text")
return
}
if (text.indexOf(PREFIX_MARKDOWN) === 0) {
text = text.substr(PREFIX_MARKDOWN.length)
var parsedText = Markdown.markdown_parser(text)
if (testHTML(parsedText)) return [parsedText, true]
}
return [text, false]
currentRoom.postHtmlMessage(text, parsedText, "m.text")
return
}
function testHTML(text) {
var re = new RegExp("(<([^>]+)>)")
return re.test(text)
currentRoom.postMessage("m.text", text)
}
}

View File

@ -29,6 +29,8 @@ ApplicationWindow {
}
// Platform.SystemTrayIcon {
// id: trayIcon
// visible: true
// iconSource: "qrc:/asset/img/icon.png"