Merge pull request #1803 from thelounge/xpaw/merge-condensed

Merge condensed messages when loading more history
This commit is contained in:
Jérémie Astori 2017-12-06 09:07:33 -05:00 committed by GitHub
commit 6ec1a2ee14
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 5 deletions

View File

@ -1,21 +1,29 @@
"use strict";
const _ = require("lodash");
const constants = require("./constants");
const templates = require("../views");
module.exports = {
updateText,
getStoredTypes,
};
function updateText(condensed, addedTypes) {
function getStoredTypes(condensed) {
const obj = {};
constants.condensedTypes.forEach((type) => {
obj[type] = condensed.data(type) || 0;
});
addedTypes.forEach((type) => {
obj[type]++;
return obj;
}
function updateText(condensed, addedTypes) {
const obj = getStoredTypes(condensed);
_.forOwn(addedTypes, (count, type) => {
obj[type] += count;
condensed.data(type, obj[type]);
});

View File

@ -61,18 +61,21 @@ function appendMessage(container, chanId, chanType, msg) {
return;
}
const obj = {};
obj[msg.type] = 1;
// If the previous message is already condensed,
// we just append to it and update text
if (lastChild.hasClass("condensed")) {
lastChild.append(renderedMessage);
condensed.updateText(lastChild, [msg.type]);
condensed.updateText(lastChild, obj);
return;
}
// Always create a condensed container
const newCondensed = $(templates.msg_condensed({time: msg.time}));
condensed.updateText(newCondensed, [msg.type]);
condensed.updateText(newCondensed, obj);
newCondensed.append(renderedMessage);
container.append(newCondensed);
}

View File

@ -3,6 +3,7 @@
const $ = require("jquery");
const socket = require("../socket");
const render = require("../render");
const condensed = require("../condensed");
const chat = $("#chat");
socket.on("more", function(data) {
@ -58,6 +59,21 @@ socket.on("more", function(data) {
}
}
// Join duplicate condensed messages together
const condensedDuplicate = chan.find(".msg.condensed + .msg.condensed");
if (condensedDuplicate) {
const condensedCorrect = condensedDuplicate.prev();
condensed.updateText(condensedCorrect, condensed.getStoredTypes(condensedDuplicate));
condensedCorrect
.append(condensedDuplicate.find(".msg"))
.toggleClass("closed", condensedDuplicate.hasClass("closed"));
condensedDuplicate.remove();
}
// restore scroll position
const position = chan.height() - heightOld;
scrollable.finish().scrollTop(position);