akkoma-fe/src/components/emoji-input/emoji-input.js

255 lines
7.6 KiB
JavaScript
Raw Normal View History

2019-03-26 02:38:15 +00:00
import Completion from '../../services/completion/completion.js'
2019-07-28 10:33:05 +00:00
import EmojiSelector from '../emoji-selector/emoji-selector.vue'
import { take } from 'lodash'
2019-03-29 15:49:32 +00:00
2019-06-10 15:57:53 +00:00
/**
* EmojiInput - augmented inputs for emoji and autocomplete support in inputs
* without having to give up the comfort of <input/> and <textarea/> elements
*
* Intended usage is:
2019-06-18 19:13:03 +00:00
* <EmojiInput v-model="something">
2019-06-10 15:57:53 +00:00
* <input v-model="something"/>
2019-06-18 18:30:35 +00:00
* </EmojiInput>
2019-06-10 15:57:53 +00:00
*
* Works only with <input> and <textarea>. Intended to use with only one nested
* input. It will find first input or textarea and work with that, multiple
* nested children not tested. You HAVE TO duplicate v-model for both
* <emoji-input> and <input>/<textarea> otherwise it will not work.
*
* Be prepared for CSS troubles though because it still wraps component in a div
* while TRYING to make it look like nothing happened, but it could break stuff.
*/
2019-03-26 02:38:15 +00:00
const EmojiInput = {
2019-06-10 15:57:53 +00:00
props: {
suggest: {
/**
* suggest: function (input: String) => Suggestion[]
*
* Function that takes input string which takes string (textAtCaret)
* and returns an array of Suggestions
*
* Suggestion is an object containing following properties:
* displayText: string. Main display text, what actual suggestion
* represents (user's screen name/emoji shortcode)
* replacement: string. Text that should replace the textAtCaret
2019-06-10 15:57:53 +00:00
* detailText: string, optional. Subtitle text, providing additional info
* if present (user's nickname)
* imageUrl: string, optional. Image to display alongside with suggestion,
* currently if no image is provided, replacement will be used (for
2019-06-10 15:57:53 +00:00
* unicode emojis)
*
* TODO: make it asynchronous when adding proper server-provided user
* suggestions
*
* For commonly used suggestors (emoji, users, both) use suggestor.js
*/
required: true,
type: Function
},
value: {
/**
* Used for v-model
*/
required: true,
type: String
}
2019-07-05 07:02:14 +00:00
},
2019-03-26 02:38:15 +00:00
data () {
return {
input: undefined,
2019-03-26 02:38:15 +00:00
highlighted: 0,
caret: 0,
focused: false,
2019-06-25 21:34:09 +00:00
blurTimeout: null
2019-03-26 02:38:15 +00:00
}
},
2019-03-29 15:49:32 +00:00
components: {
EmojiSelector
},
2019-03-26 02:38:15 +00:00
computed: {
suggestions () {
const firstchar = this.textAtCaret.charAt(0)
if (this.textAtCaret === firstchar) { return [] }
const matchedSuggestions = this.suggest(this.textAtCaret)
if (matchedSuggestions.length <= 0) {
return []
2019-03-26 02:38:15 +00:00
}
return take(matchedSuggestions, 5)
2019-06-08 14:15:42 +00:00
.map(({ imageUrl, ...rest }, index) => ({
...rest,
2019-03-26 02:38:15 +00:00
// eslint-disable-next-line camelcase
img: imageUrl || '',
2019-03-26 02:38:15 +00:00
highlighted: index === this.highlighted
}))
},
showPopup () {
return this.focused && this.suggestions && this.suggestions.length > 0
2019-03-26 02:38:15 +00:00
},
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 || slots.length === 0) return
const input = slots.find(slot => ['input', 'textarea'].includes(slot.tag))
if (!input) return
this.input = input
this.resize()
input.elm.addEventListener('blur', this.onBlur)
input.elm.addEventListener('focus', this.onFocus)
input.elm.addEventListener('paste', this.onPaste)
input.elm.addEventListener('keyup', this.onKeyUp)
input.elm.addEventListener('keydown', this.onKeyDown)
input.elm.addEventListener('transitionend', this.onTransition)
input.elm.addEventListener('compositionupdate', this.onCompositionUpdate)
},
unmounted () {
const { input } = this
if (input) {
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)
input.elm.removeEventListener('transitionend', this.onTransition)
input.elm.removeEventListener('compositionupdate', this.onCompositionUpdate)
2019-03-26 02:38:15 +00:00
}
},
methods: {
replace (replacement) {
const newValue = Completion.replaceWord(this.value, this.wordAtCaret, replacement)
this.$emit('input', newValue)
this.caret = 0
},
replaceText (e, suggestion) {
2019-03-26 02:38:15 +00:00
const len = this.suggestions.length || 0
if (this.textAtCaret.length === 1) { return }
if (len > 0 || suggestion) {
const chosenSuggestion = suggestion || this.suggestions[this.highlighted]
const replacement = chosenSuggestion.replacement
2019-03-26 02:38:15 +00:00
const newValue = Completion.replaceWord(this.value, this.wordAtCaret, replacement)
this.$emit('input', newValue)
this.highlighted = 0
const position = this.wordAtCaret.start + replacement.length
this.$nextTick(function () {
// Re-focus inputbox after clicking suggestion
this.input.elm.focus()
// Set selection right after the replacement instead of the very end
this.input.elm.setSelectionRange(position, position)
this.caret = position
})
2019-06-09 17:40:46 +00:00
e.preventDefault()
2019-03-26 02:38:15 +00:00
}
},
cycleBackward (e) {
const len = this.suggestions.length || 0
if (len > 0) {
this.highlighted -= 1
if (this.highlighted < 0) {
this.highlighted = this.suggestions.length - 1
}
e.preventDefault()
2019-03-26 02:38:15 +00:00
} else {
this.highlighted = 0
}
},
cycleForward (e) {
const len = this.suggestions.length || 0
if (len > 0) {
this.highlighted += 1
if (this.highlighted >= len) {
this.highlighted = 0
}
e.preventDefault()
2019-03-26 02:38:15 +00:00
} else {
this.highlighted = 0
}
},
2019-06-09 17:40:46 +00:00
onTransition (e) {
2019-06-18 17:49:43 +00:00
this.resize()
2019-03-26 02:38:15 +00:00
},
onBlur (e) {
// Clicking on any suggestion removes focus from autocomplete,
// preventing click handler ever executing.
this.blurTimeout = setTimeout(() => {
this.focused = false
this.setCaret(e)
2019-06-18 17:49:43 +00:00
this.resize()
}, 200)
2019-03-26 02:38:15 +00:00
},
onClick (e, suggestion) {
this.replaceText(e, suggestion)
},
onFocus (e) {
if (this.blurTimeout) {
clearTimeout(this.blurTimeout)
2019-06-25 21:34:09 +00:00
this.blurTimeout = null
}
this.focused = true
this.setCaret(e)
2019-06-18 17:49:43 +00:00
this.resize()
},
onKeyUp (e) {
this.setCaret(e)
2019-06-18 17:49:43 +00:00
this.resize()
},
onPaste (e) {
this.setCaret(e)
2019-06-18 17:49:43 +00:00
this.resize()
},
onKeyDown (e) {
this.setCaret(e)
2019-06-18 17:49:43 +00:00
this.resize()
const { ctrlKey, shiftKey, key } = e
if (key === 'Tab') {
if (shiftKey) {
2019-06-09 17:40:46 +00:00
this.cycleBackward(e)
} else {
2019-06-09 17:40:46 +00:00
this.cycleForward(e)
}
}
if (key === 'ArrowUp') {
2019-06-09 17:40:46 +00:00
this.cycleBackward(e)
} else if (key === 'ArrowDown') {
2019-06-09 17:40:46 +00:00
this.cycleForward(e)
}
if (key === 'Enter') {
if (!ctrlKey) {
2019-06-09 17:40:46 +00:00
this.replaceText(e)
}
}
2019-03-26 02:38:15 +00:00
},
onInput (e) {
this.setCaret(e)
this.$emit('input', e.target.value)
},
onCompositionUpdate (e) {
this.setCaret(e)
this.resize()
2019-03-26 02:38:15 +00:00
this.$emit('input', e.target.value)
},
2019-06-10 15:57:53 +00:00
setCaret ({ target: { selectionStart } }) {
2019-03-26 02:38:15 +00:00
this.caret = selectionStart
},
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 02:38:15 +00:00
}
}
}
export default EmojiInput