[Glitch] Add support for custom emojis in poll options

Fixes #956

Port 80f0910e21 to glitch-soc
This commit is contained in:
ThibG 2019-03-20 17:29:12 +01:00 committed by Thibaut Girka
parent cbf1d711ba
commit 8b5b686f08
2 changed files with 15 additions and 2 deletions

View File

@ -69,9 +69,11 @@ export function normalizeStatus(status, normalOldStatus) {
export function normalizePoll(poll) { export function normalizePoll(poll) {
const normalPoll = { ...poll }; const normalPoll = { ...poll };
const emojiMap = makeEmojiMap(normalPoll);
normalPoll.options = poll.options.map(option => ({ normalPoll.options = poll.options.map(option => ({
...option, ...option,
title_emojified: emojify(escapeTextContentForBrowser(option.title)), title_emojified: emojify(escapeTextContentForBrowser(option.title), emojiMap),
})); }));
return normalPoll; return normalPoll;

View File

@ -44,6 +44,11 @@ const timeRemainingString = (intl, date, now) => {
return relativeTime; return relativeTime;
}; };
const makeEmojiMap = record => record.get('emojis').reduce((obj, emoji) => {
obj[`:${emoji.get('shortcode')}:`] = emoji.toJS();
return obj;
}, {});
export default @injectIntl export default @injectIntl
class Poll extends ImmutablePureComponent { class Poll extends ImmutablePureComponent {
@ -99,6 +104,12 @@ class Poll extends ImmutablePureComponent {
const active = !!this.state.selected[`${optionIndex}`]; const active = !!this.state.selected[`${optionIndex}`];
const showResults = poll.get('voted') || poll.get('expired'); const showResults = poll.get('voted') || poll.get('expired');
let titleEmojified = option.get('title_emojified');
if (!titleEmojified) {
const emojiMap = makeEmojiMap(poll);
titleEmojified = emojify(escapeTextContentForBrowser(option.get('title')), emojiMap);
}
return ( return (
<li key={option.get('title')}> <li key={option.get('title')}>
{showResults && ( {showResults && (
@ -122,7 +133,7 @@ class Poll extends ImmutablePureComponent {
{!showResults && <span className={classNames('poll__input', { checkbox: poll.get('multiple'), active })} />} {!showResults && <span className={classNames('poll__input', { checkbox: poll.get('multiple'), active })} />}
{showResults && <span className='poll__number'>{Math.round(percent)}%</span>} {showResults && <span className='poll__number'>{Math.round(percent)}%</span>}
<span dangerouslySetInnerHTML={{ __html: option.get('title_emojified', emojify(escapeTextContentForBrowser(option.get('title')))) }} /> <span dangerouslySetInnerHTML={{ __html: titleEmojified }} />
</label> </label>
</li> </li>
); );