1
Fork 0
mirror of https://github.com/jech/galene.git synced 2024-11-22 16:45:58 +01:00

Allow HTML elements in addToChatbox.

This commit is contained in:
Juliusz Chroboczek 2023-12-08 22:43:18 +01:00
parent 96e2db0347
commit 0fc9136774
3 changed files with 34 additions and 34 deletions

View file

@ -426,6 +426,10 @@ textarea.form-reply {
margin-right: 0.33em; margin-right: 0.33em;
} }
.message-me-content {
white-space: pre-wrap;
}
.video-container { .video-container {
height: calc(var(--vh, 1vh) * 100 - 56px); height: calc(var(--vh, 1vh) * 100 - 56px);
position: relative; position: relative;

View file

@ -2830,18 +2830,18 @@ function formatToken(token, details) {
const urlRegexp = /https?:\/\/[-a-zA-Z0-9@:%/._\\+~#&()=?]+[-a-zA-Z0-9@:%/_\\+~#&()=]/g; const urlRegexp = /https?:\/\/[-a-zA-Z0-9@:%/._\\+~#&()=?]+[-a-zA-Z0-9@:%/_\\+~#&()=]/g;
/** /**
* @param {string} line * @param {string} text
* @returns {Array.<Text|HTMLElement>} * @returns {HTMLDivElement}
*/ */
function formatLine(line) { function formatText(text) {
let r = new RegExp(urlRegexp); let r = new RegExp(urlRegexp);
let result = []; let result = [];
let pos = 0; let pos = 0;
while(true) { while(true) {
let m = r.exec(line); let m = r.exec(text);
if(!m) if(!m)
break; break;
result.push(document.createTextNode(line.slice(pos, m.index))); result.push(document.createTextNode(text.slice(pos, m.index)));
let a = document.createElement('a'); let a = document.createElement('a');
a.href = m[0]; a.href = m[0];
a.textContent = m[0]; a.textContent = m[0];
@ -2850,25 +2850,13 @@ function formatLine(line) {
result.push(a); result.push(a);
pos = m.index + m[0].length; pos = m.index + m[0].length;
} }
result.push(document.createTextNode(line.slice(pos))); result.push(document.createTextNode(text.slice(pos)));
return result;
}
/** let div = document.createElement('div');
* @param {string[]} lines result.forEach(e => {
* @returns {HTMLElement} div.appendChild(e);
*/ });
function formatLines(lines) { return div;
let elts = [];
if(lines.length > 0)
elts = formatLine(lines[0]);
for(let i = 1; i < lines.length; i++) {
elts.push(document.createElement('br'));
elts = elts.concat(formatLine(lines[i]));
}
let elt = document.createElement('p');
elts.forEach(e => elt.appendChild(e));
return elt;
} }
/** /**
@ -2902,7 +2890,7 @@ let lastMessage = {};
* @param {boolean} privileged * @param {boolean} privileged
* @param {boolean} history * @param {boolean} history
* @param {string} kind * @param {string} kind
* @param {unknown} message * @param {string|HTMLElement} message
*/ */
function addToChatbox(peerId, dest, nick, time, privileged, history, kind, message) { function addToChatbox(peerId, dest, nick, time, privileged, history, kind, message) {
let row = document.createElement('div'); let row = document.createElement('div');
@ -2919,8 +2907,17 @@ function addToChatbox(peerId, dest, nick, time, privileged, history, kind, messa
if(dest) if(dest)
container.classList.add('message-private'); container.classList.add('message-private');
/** @type{HTMLElement} */
let body;
if(message instanceof HTMLElement) {
body = message;
} else if(typeof message === 'string') {
body = formatText(message);
} else {
throw new Error('Cannot add element to chatbox');
}
if(kind !== 'me') { if(kind !== 'me') {
let p = formatLines(message.toString().split('\n'));
let doHeader = true; let doHeader = true;
if(lastMessage.nick !== (nick || null) || if(lastMessage.nick !== (nick || null) ||
lastMessage.peerId !== (peerId || null) || lastMessage.peerId !== (peerId || null) ||
@ -2952,6 +2949,8 @@ function addToChatbox(peerId, dest, nick, time, privileged, history, kind, messa
} }
} }
let p = document.createElement('p');
p.appendChild(body);
p.classList.add('message-content'); p.classList.add('message-content');
container.appendChild(p); container.appendChild(p);
lastMessage.nick = (nick || null); lastMessage.nick = (nick || null);
@ -2965,14 +2964,10 @@ function addToChatbox(peerId, dest, nick, time, privileged, history, kind, messa
let user = document.createElement('span'); let user = document.createElement('span');
user.textContent = nick || '(anon)'; user.textContent = nick || '(anon)';
user.classList.add('message-me-user'); user.classList.add('message-me-user');
let content = document.createElement('span'); body.classList.add('message-me-content');
formatLine(message.toString()).forEach(elt => {
content.appendChild(elt);
});
content.classList.add('message-me-content');
container.appendChild(asterisk); container.appendChild(asterisk);
container.appendChild(user); container.appendChild(user);
container.appendChild(content); container.appendChild(body);
container.classList.add('message-me'); container.classList.add('message-me');
lastMessage = {}; lastMessage = {};
} }
@ -2988,7 +2983,7 @@ function addToChatbox(peerId, dest, nick, time, privileged, history, kind, messa
} }
/** /**
* @param {string} message * @param {string|HTMLElement} message
*/ */
function localMessage(message) { function localMessage(message) {
return addToChatbox(null, null, null, new Date(), false, false, '', message); return addToChatbox(null, null, null, new Date(), false, false, '', message);

View file

@ -193,7 +193,7 @@ function ServerConnection() {
/** /**
* onchat is called whenever a new chat message is received. * onchat is called whenever a new chat message is received.
* *
* @type {(this: ServerConnection, id: string, dest: string, username: string, time: Date, privileged: boolean, history: boolean, kind: string, message: unknown) => void} * @type {(this: ServerConnection, id: string, dest: string, username: string, time: Date, privileged: boolean, history: boolean, kind: string, message: string) => void}
*/ */
this.onchat = null; this.onchat = null;
/** /**
@ -437,7 +437,8 @@ ServerConnection.prototype.connect = async function(url) {
if(sc.onchat) if(sc.onchat)
sc.onchat.call( sc.onchat.call(
sc, m.source, m.dest, m.username, parseTime(m.time), sc, m.source, m.dest, m.username, parseTime(m.time),
m.privileged, m.type === 'chathistory', m.kind, m.value, m.privileged, m.type === 'chathistory', m.kind,
'' + m.value,
); );
break; break;
case 'usermessage': case 'usermessage':