2019-03-26 13:38:15 +11:00
|
|
|
import Completion from '../../services/completion/completion.js'
|
2019-08-12 20:18:37 +10:00
|
|
|
import EmojiPicker from '../emoji_picker/emoji_picker.vue'
|
2019-06-07 07:17:49 +10:00
|
|
|
import { take } from 'lodash'
|
2019-03-30 02:49:32 +11:00
|
|
|
|
2019-06-11 01:57:53 +10: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-19 05:13:03 +10:00
|
|
|
* <EmojiInput v-model="something">
|
2019-06-11 01:57:53 +10:00
|
|
|
* <input v-model="something"/>
|
2019-06-19 04:30:35 +10:00
|
|
|
* </EmojiInput>
|
2019-06-11 01:57:53 +10: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 13:38:15 +11:00
|
|
|
|
|
|
|
const EmojiInput = {
|
2019-06-11 01:57:53 +10: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)
|
2019-06-12 04:18:09 +10:00
|
|
|
* replacement: string. Text that should replace the textAtCaret
|
2019-06-11 01:57:53 +10:00
|
|
|
* detailText: string, optional. Subtitle text, providing additional info
|
|
|
|
* if present (user's nickname)
|
|
|
|
* imageUrl: string, optional. Image to display alongside with suggestion,
|
2019-06-12 04:18:09 +10:00
|
|
|
* currently if no image is provided, replacement will be used (for
|
2019-06-11 01:57:53 +10: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-28 23:07:01 +10:00
|
|
|
},
|
|
|
|
emojiPicker: {
|
|
|
|
required: false,
|
|
|
|
type: Boolean,
|
|
|
|
default: false
|
2019-08-13 03:01:38 +10:00
|
|
|
},
|
|
|
|
emojiPickerExternalTrigger: {
|
|
|
|
required: false,
|
|
|
|
type: Boolean,
|
|
|
|
default: false
|
|
|
|
},
|
|
|
|
stickerPicker: {
|
|
|
|
required: false,
|
|
|
|
type: Boolean,
|
|
|
|
default: false
|
2019-06-11 01:57:53 +10:00
|
|
|
}
|
2019-07-05 17:02:14 +10:00
|
|
|
},
|
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,
|
2019-06-26 04:31:43 +10:00
|
|
|
focused: false,
|
2019-07-28 23:07:01 +10:00
|
|
|
blurTimeout: null,
|
2019-09-08 22:51:17 +10:00
|
|
|
showPicker: false,
|
2019-09-13 03:14:35 +10:00
|
|
|
temporarilyHideSuggestions: false,
|
2019-09-08 22:51:17 +10:00
|
|
|
spamMode: false,
|
|
|
|
disableClickOutside: false
|
2019-03-26 13:38:15 +11:00
|
|
|
}
|
|
|
|
},
|
2019-03-30 02:49:32 +11:00
|
|
|
components: {
|
2019-07-28 20:56:08 +10:00
|
|
|
EmojiPicker
|
2019-03-30 02:49:32 +11:00
|
|
|
},
|
2019-03-26 13:38:15 +11:00
|
|
|
computed: {
|
|
|
|
suggestions () {
|
|
|
|
const firstchar = this.textAtCaret.charAt(0)
|
2019-06-10 04:17:24 +10:00
|
|
|
if (this.textAtCaret === firstchar) { return [] }
|
2019-06-07 07:17:49 +10:00
|
|
|
const matchedSuggestions = this.suggest(this.textAtCaret)
|
|
|
|
if (matchedSuggestions.length <= 0) {
|
2019-06-10 04:17:24 +10:00
|
|
|
return []
|
2019-03-26 13:38:15 +11:00
|
|
|
}
|
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-03-26 13:38:15 +11:00
|
|
|
// eslint-disable-next-line camelcase
|
2019-06-08 23:23:58 +10:00
|
|
|
img: imageUrl || '',
|
2019-03-26 13:38:15 +11:00
|
|
|
highlighted: index === this.highlighted
|
|
|
|
}))
|
2019-06-08 23:23:58 +10:00
|
|
|
},
|
2019-07-28 23:07:01 +10:00
|
|
|
showSuggestions () {
|
2019-09-13 03:14:35 +10:00
|
|
|
return this.focused &&
|
|
|
|
this.suggestions &&
|
|
|
|
this.suggestions.length > 0 &&
|
|
|
|
!this.showPicker &&
|
|
|
|
!this.temporarilyHideSuggestions
|
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()
|
|
|
|
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)
|
2019-06-12 04:18:09 +10:00
|
|
|
input.elm.addEventListener('transitionend', this.onTransition)
|
2019-07-04 06:08:51 +10:00
|
|
|
input.elm.addEventListener('compositionupdate', this.onCompositionUpdate)
|
2019-06-07 07:17:49 +10:00
|
|
|
},
|
|
|
|
unmounted () {
|
2019-06-08 23:23:58 +10:00
|
|
|
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)
|
2019-06-12 04:18:09 +10:00
|
|
|
input.elm.removeEventListener('transitionend', this.onTransition)
|
2019-07-04 06:08:51 +10:00
|
|
|
input.elm.removeEventListener('compositionupdate', this.onCompositionUpdate)
|
2019-03-26 13:38:15 +11:00
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
2019-08-13 03:01:38 +10:00
|
|
|
triggerShowPicker () {
|
|
|
|
this.showPicker = true
|
2019-09-08 22:51:17 +10:00
|
|
|
// This temporarily disables "click outside" handler
|
|
|
|
// since external trigger also means click originates
|
|
|
|
// from outside, thus preventing picker from opening
|
|
|
|
this.disableClickOutside = true
|
|
|
|
setTimeout(() => {
|
|
|
|
this.disableClickOutside = false
|
|
|
|
}, 0)
|
2019-08-13 03:01:38 +10:00
|
|
|
},
|
2019-07-28 23:07:01 +10:00
|
|
|
togglePicker () {
|
2019-09-13 03:14:35 +10:00
|
|
|
this.input.elm.focus()
|
2019-07-28 23:07:01 +10:00
|
|
|
this.showPicker = !this.showPicker
|
|
|
|
},
|
2019-03-26 13:38:15 +11:00
|
|
|
replace (replacement) {
|
|
|
|
const newValue = Completion.replaceWord(this.value, this.wordAtCaret, replacement)
|
|
|
|
this.$emit('input', newValue)
|
|
|
|
this.caret = 0
|
|
|
|
},
|
2019-09-08 22:51:17 +10:00
|
|
|
insert ({ insertion, spamMode }) {
|
2019-07-28 23:07:01 +10:00
|
|
|
const newValue = [
|
|
|
|
this.value.substring(0, this.caret),
|
|
|
|
insertion,
|
|
|
|
this.value.substring(this.caret)
|
|
|
|
].join('')
|
2019-09-08 22:51:17 +10:00
|
|
|
this.spamMode = spamMode
|
2019-07-28 23:07:01 +10:00
|
|
|
this.$emit('input', newValue)
|
2019-08-13 03:01:38 +10:00
|
|
|
const position = this.caret + insertion.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-07-28 23:07:01 +10:00
|
|
|
},
|
2019-06-26 04:31:43 +10:00
|
|
|
replaceText (e, suggestion) {
|
2019-03-26 13:38:15 +11:00
|
|
|
const len = this.suggestions.length || 0
|
2019-06-07 07:17:49 +10:00
|
|
|
if (this.textAtCaret.length === 1) { return }
|
2019-06-26 04:31:43 +10:00
|
|
|
if (len > 0 || suggestion) {
|
|
|
|
const chosenSuggestion = suggestion || this.suggestions[this.highlighted]
|
|
|
|
const replacement = chosenSuggestion.replacement
|
2019-03-26 13:38:15 +11:00
|
|
|
const newValue = Completion.replaceWord(this.value, this.wordAtCaret, replacement)
|
|
|
|
this.$emit('input', newValue)
|
|
|
|
this.highlighted = 0
|
2019-06-18 04:24:10 +10:00
|
|
|
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-10 03:40:46 +10:00
|
|
|
e.preventDefault()
|
2019-03-26 13:38:15 +11:00
|
|
|
}
|
|
|
|
},
|
|
|
|
cycleBackward (e) {
|
|
|
|
const len = this.suggestions.length || 0
|
2019-09-08 22:51:17 +10:00
|
|
|
if (len > 1) {
|
2019-03-26 13:38:15 +11:00
|
|
|
this.highlighted -= 1
|
|
|
|
if (this.highlighted < 0) {
|
|
|
|
this.highlighted = this.suggestions.length - 1
|
|
|
|
}
|
2019-06-10 04:17:24 +10:00
|
|
|
e.preventDefault()
|
2019-03-26 13:38:15 +11:00
|
|
|
} else {
|
|
|
|
this.highlighted = 0
|
|
|
|
}
|
|
|
|
},
|
|
|
|
cycleForward (e) {
|
|
|
|
const len = this.suggestions.length || 0
|
2019-09-08 22:51:17 +10:00
|
|
|
if (len > 1) {
|
2019-03-26 13:38:15 +11:00
|
|
|
this.highlighted += 1
|
|
|
|
if (this.highlighted >= len) {
|
|
|
|
this.highlighted = 0
|
|
|
|
}
|
2019-06-10 04:17:24 +10:00
|
|
|
e.preventDefault()
|
2019-03-26 13:38:15 +11:00
|
|
|
} else {
|
|
|
|
this.highlighted = 0
|
|
|
|
}
|
|
|
|
},
|
2019-06-10 03:40:46 +10:00
|
|
|
onTransition (e) {
|
2019-06-19 03:49:43 +10:00
|
|
|
this.resize()
|
2019-03-26 13:38:15 +11:00
|
|
|
},
|
2019-06-08 23:23:58 +10:00
|
|
|
onBlur (e) {
|
2019-06-12 04:18:09 +10:00
|
|
|
// Clicking on any suggestion removes focus from autocomplete,
|
|
|
|
// preventing click handler ever executing.
|
2019-06-26 04:31:43 +10:00
|
|
|
this.blurTimeout = setTimeout(() => {
|
2019-06-12 04:18:09 +10:00
|
|
|
this.focused = false
|
|
|
|
this.setCaret(e)
|
2019-06-19 03:49:43 +10:00
|
|
|
this.resize()
|
2019-06-12 04:18:09 +10:00
|
|
|
}, 200)
|
2019-03-26 13:38:15 +11:00
|
|
|
},
|
2019-06-26 04:31:43 +10:00
|
|
|
onClick (e, suggestion) {
|
|
|
|
this.replaceText(e, suggestion)
|
2019-03-30 06:56:50 +11:00
|
|
|
},
|
2019-06-08 23:23:58 +10:00
|
|
|
onFocus (e) {
|
2019-06-26 04:31:43 +10:00
|
|
|
if (this.blurTimeout) {
|
|
|
|
clearTimeout(this.blurTimeout)
|
2019-06-26 07:34:09 +10:00
|
|
|
this.blurTimeout = null
|
2019-06-26 04:31:43 +10:00
|
|
|
}
|
|
|
|
|
2019-09-08 22:51:17 +10:00
|
|
|
if (!this.spamMode) {
|
|
|
|
this.showPicker = false
|
|
|
|
}
|
2019-06-08 23:23:58 +10:00
|
|
|
this.focused = true
|
|
|
|
this.setCaret(e)
|
2019-06-19 03:49:43 +10:00
|
|
|
this.resize()
|
2019-09-13 03:14:35 +10:00
|
|
|
this.temporarilyHideSuggestions = false
|
2019-06-08 23:23:58 +10:00
|
|
|
},
|
|
|
|
onKeyUp (e) {
|
2019-09-13 03:14:35 +10:00
|
|
|
const { key } = e
|
2019-06-08 23:23:58 +10:00
|
|
|
this.setCaret(e)
|
2019-06-19 03:49:43 +10:00
|
|
|
this.resize()
|
2019-09-13 03:14:35 +10:00
|
|
|
|
|
|
|
// Setting hider in keyUp to prevent suggestions from blinking
|
|
|
|
// when moving away from suggested spot
|
|
|
|
if (key === 'Escape') {
|
|
|
|
this.temporarilyHideSuggestions = true
|
|
|
|
} else {
|
|
|
|
this.temporarilyHideSuggestions = false
|
|
|
|
}
|
2019-06-08 23:23:58 +10:00
|
|
|
},
|
|
|
|
onPaste (e) {
|
|
|
|
this.setCaret(e)
|
2019-06-19 03:49:43 +10:00
|
|
|
this.resize()
|
2019-06-08 23:23:58 +10:00
|
|
|
},
|
2019-06-07 07:17:49 +10:00
|
|
|
onKeyDown (e) {
|
|
|
|
const { ctrlKey, shiftKey, key } = e
|
2019-09-13 03:14:35 +10:00
|
|
|
// Disable suggestions hotkeys if suggestions are hidden
|
|
|
|
if (!this.temporarilyHideSuggestions) {
|
|
|
|
if (key === 'Tab') {
|
|
|
|
if (shiftKey) {
|
|
|
|
this.cycleBackward(e)
|
|
|
|
} else {
|
|
|
|
this.cycleForward(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (key === 'ArrowUp') {
|
2019-06-10 03:40:46 +10:00
|
|
|
this.cycleBackward(e)
|
2019-09-13 03:14:35 +10:00
|
|
|
} else if (key === 'ArrowDown') {
|
2019-06-10 03:40:46 +10:00
|
|
|
this.cycleForward(e)
|
2019-04-09 01:10:26 +10:00
|
|
|
}
|
2019-09-13 03:14:35 +10:00
|
|
|
if (key === 'Enter') {
|
|
|
|
if (!ctrlKey) {
|
|
|
|
this.replaceText(e)
|
|
|
|
}
|
|
|
|
}
|
2019-04-09 01:10:26 +10:00
|
|
|
}
|
2019-09-13 03:14:35 +10:00
|
|
|
// Probably add optional keyboard controls for emoji picker?
|
|
|
|
|
|
|
|
// Escape hides suggestions, if suggestions are hidden it
|
|
|
|
// de-focuses the element (i.e. default browser behavior)
|
|
|
|
if (key === 'Escape') {
|
|
|
|
if (!this.temporarilyHideSuggestions) {
|
|
|
|
this.input.elm.focus()
|
2019-06-07 07:17:49 +10:00
|
|
|
}
|
|
|
|
}
|
2019-09-13 03:14:35 +10:00
|
|
|
|
|
|
|
this.showPicker = false
|
|
|
|
this.resize()
|
2019-03-26 13:38:15 +11:00
|
|
|
},
|
|
|
|
onInput (e) {
|
2019-07-28 23:07:01 +10:00
|
|
|
this.showPicker = false
|
2019-07-04 06:08:51 +10:00
|
|
|
this.setCaret(e)
|
2019-09-13 03:14:35 +10:00
|
|
|
this.resize()
|
2019-07-04 06:08:51 +10:00
|
|
|
this.$emit('input', e.target.value)
|
|
|
|
},
|
|
|
|
onCompositionUpdate (e) {
|
2019-09-13 03:14:35 +10:00
|
|
|
this.showPicker = false
|
2019-07-04 06:08:51 +10:00
|
|
|
this.setCaret(e)
|
|
|
|
this.resize()
|
2019-03-26 13:38:15 +11:00
|
|
|
this.$emit('input', e.target.value)
|
|
|
|
},
|
2019-09-08 22:51:17 +10:00
|
|
|
onClickOutside (e) {
|
|
|
|
if (this.disableClickOutside) return
|
2019-07-28 23:07:01 +10:00
|
|
|
this.showPicker = false
|
|
|
|
},
|
2019-08-13 03:01:38 +10:00
|
|
|
onStickerUploaded (e) {
|
|
|
|
this.showPicker = false
|
|
|
|
this.$emit('sticker-uploaded', e)
|
|
|
|
},
|
|
|
|
onStickerUploadFailed (e) {
|
|
|
|
this.showPicker = false
|
|
|
|
this.$emit('sticker-upload-Failed', e)
|
|
|
|
},
|
2019-06-11 01:57:53 +10:00
|
|
|
setCaret ({ target: { selectionStart } }) {
|
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-07-28 23:07:01 +10:00
|
|
|
this.$refs.picker.$el.style.top = (offsetTop + offsetHeight) + 'px'
|
2019-03-26 13:38:15 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default EmojiInput
|