1
0
Fork 0
mirror of https://github.com/FrankerFaceZ/FrankerFaceZ.git synced 2025-08-07 23:00:54 +00:00

Fix tokenization of cheers in chat messages. Also fix cached token invalidation when changing how chat lines should render.

This commit is contained in:
SirStendec 2017-12-01 15:33:06 -05:00
parent 691c09eb18
commit aef58f24af
2 changed files with 52 additions and 43 deletions

View file

@ -328,65 +328,69 @@ export const CheerEmotes = {
return tokens; return tokens;
const actions = bitsConfig.indexedActions, const actions = bitsConfig.indexedActions,
matcher = new RegExp('\\b(' + Object.keys(actions).join('|') + ')(\\d+)\\b', 'ig'); matcher = new RegExp(`^(${Object.keys(actions).join('|')})(\\d+)$`, 'i');
const out = [], const out = [],
collected = {}, collected = {},
collect = this.context.get('chat.bits.stack'); collect = this.context.get('chat.bits.stack');
for(const token of tokens) { for(const token of tokens) {
if ( token.type !== 'text' ) { if ( ! token || token.type !== 'text' ) {
out.push(token); out.push(token);
continue; continue;
} }
matcher.lastIndex = 0; let text = [];
const text = token.text; for(const segment of token.text.split(/ +/)) {
let idx = 0, match; const match = matcher.exec(segment);
if ( match ) {
const prefix = match[1].toLowerCase(),
cheer = actions[prefix];
while((match = matcher.exec(text))) { if ( ! cheer ) {
const prefix = match[1].toLowerCase(), text.push(segment);
cheer = actions[prefix]; continue;
if ( ! cheer )
continue;
if ( idx !== match.index )
out.push({type: 'text', text: text.slice(idx, match.index)});
const amount = parseInt(match[2], 10),
tiers = cheer.orderedTiers;
let tier, token;
for(let i=0, l = tiers.length; i < l; i++)
if ( amount >= tiers[i].bits ) {
tier = i;
break;
} }
out.push(token = { const amount = parseInt(match[2], 10),
type: 'cheer', tiers = cheer.orderedTiers;
prefix,
tier,
amount: parseInt(match[2], 10),
text: match[0]
});
if ( collect ) { let tier, token;
const pref = collect === 2 ? 'cheer' : prefix, for(let i=0, l = tiers.length; i < l; i++)
group = collected[pref] = collected[pref] || {total: 0, individuals: []}; if ( amount >= tiers[i].bits ) {
tier = i;
break;
}
group.total += amount; if ( text.length ) {
group.individuals.push([amount, tier, prefix]); // We have pending text. Join it together, with an extra space.
token.hidden = true; out.push({type: 'text', text: `${text.join(' ')} `});
} text = [];
}
idx = match.index + match[0].length; out.push(token = {
type: 'cheer',
prefix,
tier,
amount,
text: match[0]
});
if ( collect ) {
const pref = collect === 2 ? 'cheer' : prefix,
group = collected[pref] = collected[pref] || {total: 0, individuals: []};
group.total += amount;
group.individuals.push([amount, tier, prefix]);
token.hidden = true;
}
} else
text.push(segment);
} }
if ( idx < text.length ) if ( text.length > 1 || (text.length === 1 && text[0] !== '') )
out.push({type: 'text', text: text.slice(idx)}); out.push({type: 'text', text: text.join(' ')});
} }
if ( collect ) { if ( collect ) {

View file

@ -75,7 +75,7 @@ export default class ChatLine extends Module {
if ( ! msg.message && msg.messageParts ) if ( ! msg.message && msg.messageParts )
detokenizeMessage(msg); detokenizeMessage(msg);
const tokens = msg.ffzTokens = msg.ffzTokens || t.chat.tokenizeMessage(msg, {login: this.props.currentUserLogin, display: this.props.currentUserDisplayName}); const tokens = msg.ffz_tokens = msg.ffz_tokens || t.chat.tokenizeMessage(msg, {login: this.props.currentUserLogin, display: this.props.currentUserDisplayName});
let cls = 'chat-line__message', let cls = 'chat-line__message',
out = tokens.length ? [ out = tokens.length ? [
@ -175,8 +175,13 @@ export default class ChatLine extends Module {
updateLines() { updateLines() {
for(const inst of this.ChatLine.instances) for(const inst of this.ChatLine.instances) {
const msg = inst.props.message;
if ( msg )
msg.ffz_tokens = null;
inst.forceUpdate(); inst.forceUpdate();
}
} }
} }