aboutsummaryrefslogtreecommitdiff
path: root/src/components/emoji-input/emoji-input.js
blob: 466341c07eb70dd1f174a79a1ea67f9dae55eec0 (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
127
128
129
130
131
132
133
134
135
136
import Completion from '../../services/completion/completion.js'
import { take } from 'lodash'

const EmojiInput = {
  props: [
    'placeholder',
    'suggest',
    'value',
    'type',
    'classname'
  ],
  data () {
    return {
      input: undefined,
      highlighted: 0,
      caret: 0
    }
  },
  computed: {
    suggestions () {
      const firstchar = this.textAtCaret.charAt(0)
      if (this.textAtCaret === firstchar) { return }
      const matchedSuggestions = this.suggest(this.textAtCaret)
      if (matchedSuggestions.length <= 0) {
        return false
      }
      return take(matchedSuggestions, 5).map(({shortcode, image_url, replacement}, index) => ({
        shortcode,
        replacement,
        // eslint-disable-next-line camelcase
        img: !image_url ? '' : this.$store.state.instance.server + image_url,
        highlighted: index === this.highlighted
      }))
    },
    textAtCaret () {
      return (this.wordAtCaret || {}).word || ''
    },
    wordAtCaret () {
      if (this.value && this.caret) {
        const word = Completion.wordAtPosition(this.value, this.caret - 1) || {}
        return word
      }
    },
  },
  mounted () {
    const slots = this.$slots.default
    if (slots.length === 0) return
    const input = slots.find(slot => ['input', 'textarea'].includes(slot.tag))
    if (!input) return
    this.input = input
    input.elm.addEventListener('keyup', this.setCaret)
    input.elm.addEventListener('paste', this.setCaret)
    input.elm.addEventListener('focus', this.setCaret)
    input.elm.addEventListener('keydown', this.onKeyDown)
  },
  unmounted () {
    if (this.input) {
      this.input.elm.removeEventListener('keyup', this.setCaret)
      this.input.elm.removeEventListener('paste', this.setCaret)
      this.input.elm.removeEventListener('focus', this.setCaret)
      this.input.elm.removeEventListener('keydown', this.onKeyDown)
    }
  },
  methods: {
    replace (replacement) {
      const newValue = Completion.replaceWord(this.value, this.wordAtCaret, replacement)
      this.$emit('input', newValue)
      this.caret = 0
    },
    replaceText () {
      const len = this.suggestions.length || 0
      if (this.textAtCaret.length === 1) { return }
      if (len > 0) {
        const suggestion = this.suggestions[this.highlighted]
        const replacement = suggestion.replacement
        const newValue = Completion.replaceWord(this.value, this.wordAtCaret, replacement)
        this.$emit('input', newValue)
        this.caret = 0
        this.highlighted = 0
      }
    },
    cycleBackward () {
      const len = this.suggestions.length || 0
      if (len > 0) {
        this.highlighted -= 1
        if (this.highlighted < 0) {
          this.highlighted = this.suggestions.length - 1
        }
      } else {
        this.highlighted = 0
      }
    },
    cycleForward () {
      const len = this.suggestions.length || 0
      if (len > 0) {
        this.highlighted += 1
        if (this.highlighted >= len) {
          this.highlighted = 0
        }
      } else {
        this.highlighted = 0
      }
    },
    onKeyDown (e) {
      this.setCaret(e)
      e.stopPropagation()

      const { ctrlKey, shiftKey, key } = e
      if (key === 'Tab') {
        if (shiftKey) {
          this.cycleBackward()
        } else {
          this.cycleForward()
        }
      }
      if (key === 'ArrowUp') {
        this.cycleBackward()
      } else if (key === 'ArrowDown') {
        this.cycleForward()
      }
      if (key === 'Enter') {
        if (!ctrlKey) {
          this.replaceText()
        }
      }
    },
    onInput (e) {
      this.$emit('input', e.target.value)
    },
    setCaret ({ target: { selectionStart, value } }) {
      this.caret = selectionStart
    }
  }
}

export default EmojiInput