2019-03-26 13:38:15 +11:00
|
|
|
import Completion from '../../services/completion/completion.js'
|
2019-06-07 07:17:49 +10:00
|
|
|
import { take } from 'lodash'
|
2019-03-26 13:38:15 +11:00
|
|
|
|
|
|
|
const EmojiInput = {
|
|
|
|
props: [
|
|
|
|
'placeholder',
|
2019-06-07 07:17:49 +10:00
|
|
|
'suggest',
|
|
|
|
'value',
|
2019-03-27 04:40:37 +11:00
|
|
|
'type',
|
|
|
|
'classname'
|
2019-03-26 13:38:15 +11:00
|
|
|
],
|
|
|
|
data () {
|
|
|
|
return {
|
2019-06-07 07:17:49 +10:00
|
|
|
input: undefined,
|
2019-03-26 13:38:15 +11:00
|
|
|
highlighted: 0,
|
2019-06-08 23:23:58 +10:00
|
|
|
caret: 0,
|
|
|
|
focused: false,
|
|
|
|
popupOptions: {
|
|
|
|
placement: 'bottom-start',
|
|
|
|
trigger: 'hover',
|
|
|
|
// See: https://github.com/RobinCK/vue-popper/issues/63
|
|
|
|
'delay-on-mouse-over': 9999999,
|
|
|
|
'delay-on-mouse-out': 9999999,
|
|
|
|
modifiers: {
|
|
|
|
arrow: { enabled: true },
|
|
|
|
offset: { offset: '0, 5px' }
|
|
|
|
}
|
|
|
|
}
|
2019-03-26 13:38:15 +11:00
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
suggestions () {
|
|
|
|
const firstchar = this.textAtCaret.charAt(0)
|
2019-06-07 07:17:49 +10:00
|
|
|
if (this.textAtCaret === firstchar) { return }
|
|
|
|
const matchedSuggestions = this.suggest(this.textAtCaret)
|
|
|
|
if (matchedSuggestions.length <= 0) {
|
2019-03-26 13:38:15 +11:00
|
|
|
return false
|
|
|
|
}
|
2019-06-08 23:23:58 +10:00
|
|
|
return take(matchedSuggestions, 5)
|
2019-06-09 00:15:42 +10:00
|
|
|
.map(({ imageUrl, ...rest }, index) => ({
|
|
|
|
...rest,
|
2019-06-08 23:23:58 +10:00
|
|
|
// eslint-disable-next-line camelcase
|
|
|
|
img: imageUrl || '',
|
|
|
|
highlighted: index === this.highlighted
|
|
|
|
}))
|
|
|
|
},
|
|
|
|
showPopup () {
|
|
|
|
return this.focused && this.suggestions && this.suggestions.length > 0
|
2019-03-26 13:38:15 +11:00
|
|
|
},
|
|
|
|
textAtCaret () {
|
|
|
|
return (this.wordAtCaret || {}).word || ''
|
|
|
|
},
|
|
|
|
wordAtCaret () {
|
2019-06-07 07:17:49 +10:00
|
|
|
if (this.value && this.caret) {
|
|
|
|
const word = Completion.wordAtPosition(this.value, this.caret - 1) || {}
|
|
|
|
return word
|
|
|
|
}
|
2019-06-08 23:23:58 +10:00
|
|
|
}
|
2019-06-07 07:17:49 +10:00
|
|
|
},
|
|
|
|
mounted () {
|
|
|
|
const slots = this.$slots.default
|
2019-06-08 23:23:58 +10:00
|
|
|
if (!slots || slots.length === 0) return
|
2019-06-07 07:17:49 +10:00
|
|
|
const input = slots.find(slot => ['input', 'textarea'].includes(slot.tag))
|
|
|
|
if (!input) return
|
|
|
|
this.input = input
|
2019-06-08 23:23:58 +10:00
|
|
|
this.resize()
|
2019-06-10 03:40:46 +10:00
|
|
|
input.elm.addEventListener('transitionend', this.onTransition)
|
2019-06-08 23:23:58 +10:00
|
|
|
input.elm.addEventListener('blur', this.onBlur)
|
|
|
|
input.elm.addEventListener('focus', this.onFocus)
|
|
|
|
input.elm.addEventListener('paste', this.onPaste)
|
|
|
|
input.elm.addEventListener('keyup', this.onKeyUp)
|
2019-06-07 07:17:49 +10:00
|
|
|
input.elm.addEventListener('keydown', this.onKeyDown)
|
|
|
|
},
|
|
|
|
unmounted () {
|
2019-06-08 23:23:58 +10:00
|
|
|
const { input } = this
|
|
|
|
if (input) {
|
2019-06-10 03:40:46 +10:00
|
|
|
input.elm.removeEventListener('transitionend', this.onTransition)
|
2019-06-08 23:23:58 +10:00
|
|
|
input.elm.removeEventListener('blur', this.onBlur)
|
|
|
|
input.elm.removeEventListener('focus', this.onFocus)
|
|
|
|
input.elm.removeEventListener('paste', this.onPaste)
|
|
|
|
input.elm.removeEventListener('keyup', this.onKeyUp)
|
|
|
|
input.elm.removeEventListener('keydown', this.onKeyDown)
|
2019-03-26 13:38:15 +11:00
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
replace (replacement) {
|
|
|
|
const newValue = Completion.replaceWord(this.value, this.wordAtCaret, replacement)
|
|
|
|
this.$emit('input', newValue)
|
|
|
|
this.caret = 0
|
|
|
|
},
|
2019-06-10 03:40:46 +10:00
|
|
|
replaceText (e) {
|
|
|
|
const len = (this.suggestions && this.suggestions.length) || 0
|
2019-06-07 07:17:49 +10:00
|
|
|
if (this.textAtCaret.length === 1) { return }
|
2019-03-26 13:38:15 +11:00
|
|
|
if (len > 0) {
|
2019-06-07 07:17:49 +10:00
|
|
|
const suggestion = this.suggestions[this.highlighted]
|
|
|
|
const replacement = suggestion.replacement
|
2019-03-26 13:38:15 +11:00
|
|
|
const newValue = Completion.replaceWord(this.value, this.wordAtCaret, replacement)
|
|
|
|
this.$emit('input', newValue)
|
|
|
|
this.caret = 0
|
|
|
|
this.highlighted = 0
|
2019-06-10 03:40:46 +10:00
|
|
|
e.preventDefault()
|
2019-03-26 13:38:15 +11:00
|
|
|
}
|
|
|
|
},
|
2019-06-10 03:40:46 +10:00
|
|
|
cycleBackward (e) {
|
2019-03-26 13:38:15 +11:00
|
|
|
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
|
|
|
|
}
|
|
|
|
},
|
2019-06-10 03:40:46 +10:00
|
|
|
cycleForward (e) {
|
2019-03-26 13:38:15 +11:00
|
|
|
const len = this.suggestions.length || 0
|
|
|
|
if (len > 0) {
|
|
|
|
this.highlighted += 1
|
|
|
|
if (this.highlighted >= len) {
|
|
|
|
this.highlighted = 0
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.highlighted = 0
|
|
|
|
}
|
|
|
|
},
|
2019-06-10 03:40:46 +10:00
|
|
|
onTransition (e) {
|
|
|
|
this.resize(e)
|
|
|
|
},
|
2019-06-08 23:23:58 +10:00
|
|
|
onBlur (e) {
|
|
|
|
this.focused = false
|
|
|
|
this.setCaret(e)
|
|
|
|
this.resize(e)
|
|
|
|
},
|
|
|
|
onFocus (e) {
|
|
|
|
this.focused = true
|
|
|
|
this.setCaret(e)
|
|
|
|
this.resize(e)
|
|
|
|
},
|
|
|
|
onKeyUp (e) {
|
|
|
|
this.setCaret(e)
|
|
|
|
this.resize(e)
|
|
|
|
},
|
|
|
|
onPaste (e) {
|
|
|
|
this.setCaret(e)
|
|
|
|
this.resize(e)
|
|
|
|
},
|
2019-06-07 07:17:49 +10:00
|
|
|
onKeyDown (e) {
|
|
|
|
this.setCaret(e)
|
2019-06-08 23:23:58 +10:00
|
|
|
this.resize(e)
|
2019-06-07 07:17:49 +10:00
|
|
|
|
|
|
|
const { ctrlKey, shiftKey, key } = e
|
|
|
|
if (key === 'Tab') {
|
|
|
|
if (shiftKey) {
|
2019-06-10 03:40:46 +10:00
|
|
|
this.cycleBackward(e)
|
2019-06-07 07:17:49 +10:00
|
|
|
} else {
|
2019-06-10 03:40:46 +10:00
|
|
|
this.cycleForward(e)
|
2019-06-07 07:17:49 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (key === 'ArrowUp') {
|
2019-06-10 03:40:46 +10:00
|
|
|
this.cycleBackward(e)
|
2019-06-08 23:23:58 +10:00
|
|
|
e.preventDefault()
|
2019-06-07 07:17:49 +10:00
|
|
|
} else if (key === 'ArrowDown') {
|
2019-06-10 03:40:46 +10:00
|
|
|
this.cycleForward(e)
|
2019-06-07 07:17:49 +10:00
|
|
|
}
|
|
|
|
if (key === 'Enter') {
|
|
|
|
if (!ctrlKey) {
|
2019-06-10 03:40:46 +10:00
|
|
|
this.replaceText(e)
|
2019-06-07 07:17:49 +10:00
|
|
|
}
|
|
|
|
}
|
2019-03-26 13:38:15 +11:00
|
|
|
},
|
|
|
|
onInput (e) {
|
|
|
|
this.$emit('input', e.target.value)
|
|
|
|
},
|
2019-06-07 07:17:49 +10:00
|
|
|
setCaret ({ target: { selectionStart, value } }) {
|
2019-03-26 13:38:15 +11:00
|
|
|
this.caret = selectionStart
|
2019-06-08 23:23:58 +10:00
|
|
|
},
|
|
|
|
resize () {
|
|
|
|
const { panel } = this.$refs
|
|
|
|
if (!panel) return
|
|
|
|
const { offsetHeight, offsetTop } = this.input.elm
|
|
|
|
this.$refs.panel.style.top = (offsetTop + offsetHeight) + 'px'
|
2019-03-26 13:38:15 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default EmojiInput
|