aboutsummaryrefslogtreecommitdiff
path: root/src/components/react_button/react_button.js
blob: 58fbb61bdab530467392ff9668fe9138dc1a3a00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import Popover from '../popover/popover.vue'
import { ensureFinalFallback } from '../../i18n/languages.js'
import { library } from '@fortawesome/fontawesome-svg-core'
import { faPlus, faTimes } from '@fortawesome/free-solid-svg-icons'
import { faSmileBeam } from '@fortawesome/free-regular-svg-icons'
import { trim } from 'lodash'

library.add(
  faPlus,
  faTimes,
  faSmileBeam
)

const ReactButton = {
  props: ['status'],
  data () {
    return {
      filterWord: '',
      expanded: false
    }
  },
  components: {
    Popover
  },
  methods: {
    addReaction (event, emoji, close) {
      const existingReaction = this.status.emoji_reactions.find(r => r.name === emoji)
      if (existingReaction && existingReaction.me) {
        this.$store.dispatch('unreactWithEmoji', { id: this.status.id, emoji })
      } else {
        this.$store.dispatch('reactWithEmoji', { id: this.status.id, emoji })
      }
      close()
    },
    onShow () {
      this.expanded = true
      this.focusInput()
    },
    onClose () {
      this.expanded = false
    },
    focusInput () {
      this.$nextTick(() => {
        const input = document.querySelector('.popover.ReactButton').querySelector('input')
        if (input) input.focus()
      })
    },
    // Vaguely adjusted copypaste from emoji_input and emoji_picker!
    maybeLocalizedEmojiNamesAndKeywords (emoji) {
      const names = [emoji.displayText]
      const keywords = []

      if (emoji.displayTextI18n) {
        names.push(this.$t(emoji.displayTextI18n.key, emoji.displayTextI18n.args))
      }

      if (emoji.annotations) {
        this.languages.forEach(lang => {
          names.push(emoji.annotations[lang]?.name)

          keywords.push(...(emoji.annotations[lang]?.keywords || []))
        })
      }

      return {
        names: names.filter(k => k),
        keywords: keywords.filter(k => k)
      }
    },
    maybeLocalizedEmojiName (emoji) {
      if (!emoji.annotations) {
        return emoji.displayText
      }

      if (emoji.displayTextI18n) {
        return this.$t(emoji.displayTextI18n.key, emoji.displayTextI18n.args)
      }

      for (const lang of this.languages) {
        if (emoji.annotations[lang]?.name) {
          return emoji.annotations[lang].name
        }
      }

      return emoji.displayText
    }
  },
  computed: {
    commonEmojis () {
      const hardcodedSet = new Set(['👍', '😠', '👀', '😂', '🔥'])
      return this.$store.getters.standardEmojiList.filter(emoji => hardcodedSet.has(emoji.replacement))
    },
    languages () {
      return ensureFinalFallback(this.$store.getters.mergedConfig.interfaceLanguage)
    },
    emojis () {
      if (this.filterWord !== '') {
        const keywordLowercase = trim(this.filterWord.toLowerCase())

        const orderedEmojiList = []
        for (const emoji of this.$store.getters.standardEmojiList) {
          const indices = this.maybeLocalizedEmojiNamesAndKeywords(emoji)
            .keywords
            .map(k => k.toLowerCase().indexOf(keywordLowercase))
            .filter(k => k > -1)

          const indexOfKeyword = indices.length ? Math.min(...indices) : -1

          if (indexOfKeyword > -1) {
            if (!Array.isArray(orderedEmojiList[indexOfKeyword])) {
              orderedEmojiList[indexOfKeyword] = []
            }
            orderedEmojiList[indexOfKeyword].push(emoji)
          }
        }
        return orderedEmojiList.flat()
      }
      return this.$store.getters.standardEmojiList || []
    },
    mergedConfig () {
      return this.$store.getters.mergedConfig
    }
  }
}

export default ReactButton