From afa0b5bab2fa846af97b9fb494f110a5aa4a1910 Mon Sep 17 00:00:00 2001 From: Jonas Dellinger Date: Fri, 26 May 2017 00:51:34 +0200 Subject: [PATCH] Added styles support, fixed messages with no author, removed JQuery dep --- resources/[system]/chat/cl_chat.lua | 7 ++++--- resources/[system]/chat/html/App.js | 6 +++--- resources/[system]/chat/html/Message.js | 17 +++++++++++++++-- resources/[system]/chat/html/config.default.js | 4 +++- resources/[system]/chat/html/index.css | 8 ++++++++ resources/[system]/chat/html/index.html | 10 +++++++--- 6 files changed, 40 insertions(+), 12 deletions(-) diff --git a/resources/[system]/chat/cl_chat.lua b/resources/[system]/chat/cl_chat.lua index 25ce6be..8848f3e 100644 --- a/resources/[system]/chat/cl_chat.lua +++ b/resources/[system]/chat/cl_chat.lua @@ -13,15 +13,16 @@ RegisterNetEvent('_chat:messageEntered') --deprecated, use chat:addMessage AddEventHandler('chatMessage', function(author, color, text) - if author == "" then - author = false + local args = { text } + if author ~= "" then + table.insert(args, 1, author) end SendNUIMessage({ type = 'ON_MESSAGE', message = { color = color, multiline = true, - args = { author, text } + args = args } }) end) diff --git a/resources/[system]/chat/html/App.js b/resources/[system]/chat/html/App.js index e0552ae..8c509da 100644 --- a/resources/[system]/chat/html/App.js +++ b/resources/[system]/chat/html/App.js @@ -19,7 +19,7 @@ window.APP = { window.removeEventListener('message', this.listener); }, mounted() { - $.post('http://chat/loaded', JSON.stringify({})); + post('http://chat/loaded', JSON.stringify({})); this.listener = window.addEventListener('message', (event) => { const item = event.data || event.detail; //'detail' is for debuging via browsers if (this[item.type]) { @@ -135,7 +135,7 @@ window.APP = { this.resize(); } else { if(this.message !== '') { - $.post('http://chat/chatResult', JSON.stringify({ + post('http://chat/chatResult', JSON.stringify({ message: this.message, })); this.oldMessages.unshift(this.message); @@ -149,7 +149,7 @@ window.APP = { }, hideInput(canceled = false) { if (canceled) { - $.post('http://chat/chatResult', JSON.stringify({ canceled })); + post('http://chat/chatResult', JSON.stringify({ canceled })); } this.showInput = false; clearInterval(this.focusTimer); diff --git a/resources/[system]/chat/html/Message.js b/resources/[system]/chat/html/Message.js index a83e65e..f1a72a1 100644 --- a/resources/[system]/chat/html/Message.js +++ b/resources/[system]/chat/html/Message.js @@ -6,6 +6,13 @@ Vue.component('message', { computed: { textEscaped() { let s = this.template ? this.template : this.templates[this.templateId]; + + //This hack is required to preserve backwards compatability + if (this.templateId == CONFIG.defaultTemplateId + && this.args.length == 1) { + s = this.templates[CONFIG.defaultAltTemplateId] //Swap out default template :/ + } + s = s.replace(/{(\d+)}/g, (match, number) => { const argEscaped = this.args[number] != undefined ? this.escape(this.args[number]) : match if (number == 0 && this.color) { @@ -19,10 +26,16 @@ Vue.component('message', { }, methods: { colorizeOld(str) { - return `${str}` + return `${str}` }, colorize(str) { - const s = "" + (str.replace(/\^([0-9]+)/g, (str, color) => ``)) + ""; + const s = "" + (str.replace(/\^([0-9]+)([busr])|\^([0-9]+)|\^([busr])/g, + (match, color1, style1, color2, style2) => { + const color = (color1 || color2) ? `color-${color1 || color2} ` : ''; + const style = (style1 || style2) ? `style-${style1 || style2} ` : ''; + return ``; + } + )) + ""; return s.replace(/]*><\/span[^>]*>/g, ''); }, escape(unsafe) { diff --git a/resources/[system]/chat/html/config.default.js b/resources/[system]/chat/html/config.default.js index a517381..7e57105 100644 --- a/resources/[system]/chat/html/config.default.js +++ b/resources/[system]/chat/html/config.default.js @@ -1,9 +1,11 @@ // DO NOT EDIT THIS FILE // Copy it to `config.js` and edit it window.CONFIG = { - defaultTemplateId: 'default', //This template will be used for normal chat messages + defaultTemplateId: 'default', //This is the default template for 2 args1 + defaultAltTemplateId: 'defaultAlt', //This one for 1 arg templates: { //You can add static templates here 'default': '{0}: {1}', + 'defaultAlt': '{0}', 'example:important': '

^2{0}

' }, fadeTimeout: 7000, diff --git a/resources/[system]/chat/html/index.css b/resources/[system]/chat/html/index.css index 3357e56..b7ea222 100644 --- a/resources/[system]/chat/html/index.css +++ b/resources/[system]/chat/html/index.css @@ -8,6 +8,14 @@ .color-8{color: #cc0000;} .color-9{color: #cc0068;} +.style-b{font-weight: bold;} +.style-u{text-decoration: underline;} +.style-s{text-decoration: line-through;} +.style-r { + text-decoration: none; + font-weight: normal; +} + * { font-family: 'Lato', sans-serif; margin: 0; diff --git a/resources/[system]/chat/html/index.html b/resources/[system]/chat/html/index.html index de5ced8..64ee96e 100644 --- a/resources/[system]/chat/html/index.html +++ b/resources/[system]/chat/html/index.html @@ -8,9 +8,6 @@ - - - @@ -91,6 +88,13 @@