]> cat aescling's git repositories - mastodon.git/commitdiff
[Glitch] Add support for custom emojis in poll options
authorThibG <thib@sitedethib.com>
Wed, 20 Mar 2019 16:29:12 +0000 (17:29 +0100)
committerThibaut Girka <thib@sitedethib.com>
Wed, 20 Mar 2019 16:34:32 +0000 (17:34 +0100)
Fixes #956

Port 80f0910e2141b24082b9143266a9a6cf1ef6a516 to glitch-soc

app/javascript/flavours/glitch/actions/importer/normalizer.js
app/javascript/flavours/glitch/components/poll.js

index ccd84364e834bb4acdd79c7ca9fb997db8319f52..a8c3fe16a2a946ed8616a0adbd16fe72359c55ed 100644 (file)
@@ -69,9 +69,11 @@ export function normalizeStatus(status, normalOldStatus) {
 export function normalizePoll(poll) {
   const normalPoll = { ...poll };
 
+  const emojiMap = makeEmojiMap(normalPoll);
+
   normalPoll.options = poll.options.map(option => ({
     ...option,
-    title_emojified: emojify(escapeTextContentForBrowser(option.title)),
+    title_emojified: emojify(escapeTextContentForBrowser(option.title), emojiMap),
   }));
 
   return normalPoll;
index a1b297ce750f1b32229636523f99cc5297bf5bfc..56331cb29031fdf83a6a031b32b93084ab7161c9 100644 (file)
@@ -44,6 +44,11 @@ const timeRemainingString = (intl, date, now) => {
   return relativeTime;
 };
 
+const makeEmojiMap = record => record.get('emojis').reduce((obj, emoji) => {
+  obj[`:${emoji.get('shortcode')}:`] = emoji.toJS();
+  return obj;
+}, {});
+
 export default @injectIntl
 class Poll extends ImmutablePureComponent {
 
@@ -99,6 +104,12 @@ class Poll extends ImmutablePureComponent {
     const active             = !!this.state.selected[`${optionIndex}`];
     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 (
       <li key={option.get('title')}>
         {showResults && (
@@ -122,7 +133,7 @@ class Poll extends ImmutablePureComponent {
           {!showResults && <span className={classNames('poll__input', { checkbox: poll.get('multiple'), active })} />}
           {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>
       </li>
     );