2019-03-26 02:38:15 +00:00
|
|
|
import Completion from '../../services/completion/completion.js'
|
2019-08-12 10:18:37 +00:00
|
|
|
import EmojiPicker from '../emoji_picker/emoji_picker.vue'
|
2019-06-06 21:17:49 +00:00
|
|
|
import { take } from 'lodash'
|
2019-09-25 16:58:15 +00:00
|
|
|
import { findOffset } from '../../services/offset_finder/offset_finder.service.js'
|
2019-03-29 15:49:32 +00:00
|
|
|
|
2020-10-19 16:38:49 +00:00
|
|
|
import { library } from '@fortawesome/fontawesome-svg-core'
|
|
|
|
import {
|
|
|
|
faSmileBeam
|
|
|
|
} from '@fortawesome/free-regular-svg-icons'
|
|
|
|
|
|
|
|
library.add(
|
|
|
|
faSmileBeam
|
|
|
|
)
|
|
|
|
|
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 = {
|
2022-03-29 22:24:53 +00:00
|
|
|
emits: ['update:modelValue', 'shown'],
|
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)
|
2019-06-11 18:18:09 +00:00
|
|
|
* 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,
|
2019-06-11 18:18:09 +00:00
|
|
|
* 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
|
|
|
|
},
|
2021-04-25 10:23:16 +00:00
|
|
|
modelValue: {
|
2019-06-10 15:57:53 +00:00
|
|
|
/**
|
|
|
|
* Used for v-model
|
|
|
|
*/
|
|
|
|
required: true,
|
|
|
|
type: String
|
2019-07-28 13:07:01 +00:00
|
|
|
},
|
2019-09-12 17:36:43 +00:00
|
|
|
enableEmojiPicker: {
|
|
|
|
/**
|
|
|
|
* Enables emoji picker support, this implies that custom emoji are supported
|
|
|
|
*/
|
2019-07-28 13:07:01 +00:00
|
|
|
required: false,
|
|
|
|
type: Boolean,
|
|
|
|
default: false
|
2019-08-12 17:01:38 +00:00
|
|
|
},
|
2019-09-12 17:36:43 +00:00
|
|
|
hideEmojiButton: {
|
|
|
|
/**
|
|
|
|
* intended to use with external picker trigger, i.e. you have a button outside
|
|
|
|
* input that will open up the picker, see triggerShowPicker()
|
|
|
|
*/
|
2019-08-12 17:01:38 +00:00
|
|
|
required: false,
|
|
|
|
type: Boolean,
|
|
|
|
default: false
|
|
|
|
},
|
2019-09-12 17:36:43 +00:00
|
|
|
enableStickerPicker: {
|
|
|
|
/**
|
|
|
|
* Enables sticker picker support, only makes sense when enableEmojiPicker=true
|
|
|
|
*/
|
2019-08-12 17:01:38 +00:00
|
|
|
required: false,
|
|
|
|
type: Boolean,
|
|
|
|
default: false
|
2020-05-07 13:10:53 +00:00
|
|
|
},
|
|
|
|
placement: {
|
|
|
|
/**
|
|
|
|
* Forces the panel to take a specific position relative to the input element.
|
|
|
|
* The 'auto' placement chooses either bottom or top depending on which has the available space (when both have available space, bottom is preferred).
|
|
|
|
*/
|
|
|
|
required: false,
|
|
|
|
type: String, // 'auto', 'top', 'bottom'
|
|
|
|
default: 'auto'
|
2020-06-21 14:13:29 +00:00
|
|
|
},
|
|
|
|
newlineOnCtrlEnter: {
|
|
|
|
required: false,
|
|
|
|
type: Boolean,
|
|
|
|
default: false
|
2019-06-10 15:57:53 +00:00
|
|
|
}
|
2019-07-05 07:02:14 +00:00
|
|
|
},
|
2019-03-26 02:38:15 +00:00
|
|
|
data () {
|
|
|
|
return {
|
2019-06-06 21:17:49 +00:00
|
|
|
input: undefined,
|
2019-03-26 02:38:15 +00:00
|
|
|
highlighted: 0,
|
2019-06-08 13:23:58 +00:00
|
|
|
caret: 0,
|
2019-06-25 18:31:43 +00:00
|
|
|
focused: false,
|
2019-07-28 13:07:01 +00:00
|
|
|
blurTimeout: null,
|
2019-09-08 12:51:17 +00:00
|
|
|
showPicker: false,
|
2019-09-12 17:14:35 +00:00
|
|
|
temporarilyHideSuggestions: false,
|
2019-09-23 17:29:01 +00:00
|
|
|
keepOpen: false,
|
2020-11-17 15:46:26 +00:00
|
|
|
disableClickOutside: false,
|
|
|
|
suggestions: []
|
2019-03-26 02:38:15 +00:00
|
|
|
}
|
|
|
|
},
|
2019-03-29 15:49:32 +00:00
|
|
|
components: {
|
2019-07-28 10:56:08 +00:00
|
|
|
EmojiPicker
|
2019-03-29 15:49:32 +00:00
|
|
|
},
|
2019-03-26 02:38:15 +00:00
|
|
|
computed: {
|
2019-09-23 17:29:01 +00:00
|
|
|
padEmoji () {
|
2019-10-06 20:28:30 +00:00
|
|
|
return this.$store.getters.mergedConfig.padEmoji
|
2019-09-23 17:29:01 +00:00
|
|
|
},
|
2019-07-28 13:07:01 +00:00
|
|
|
showSuggestions () {
|
2019-09-12 17:14:35 +00:00
|
|
|
return this.focused &&
|
|
|
|
this.suggestions &&
|
|
|
|
this.suggestions.length > 0 &&
|
|
|
|
!this.showPicker &&
|
|
|
|
!this.temporarilyHideSuggestions
|
2019-03-26 02:38:15 +00:00
|
|
|
},
|
|
|
|
textAtCaret () {
|
|
|
|
return (this.wordAtCaret || {}).word || ''
|
|
|
|
},
|
|
|
|
wordAtCaret () {
|
2021-04-25 10:23:16 +00:00
|
|
|
if (this.modelValue && this.caret) {
|
|
|
|
const word = Completion.wordAtPosition(this.modelValue, this.caret - 1) || {}
|
2019-06-06 21:17:49 +00:00
|
|
|
return word
|
|
|
|
}
|
2019-06-08 13:23:58 +00:00
|
|
|
}
|
2019-06-06 21:17:49 +00:00
|
|
|
},
|
|
|
|
mounted () {
|
2021-04-18 14:03:31 +00:00
|
|
|
const { root } = this.$refs
|
|
|
|
const input = root.querySelector('.emoji-input > input') || root.querySelector('.emoji-input > textarea')
|
2019-06-06 21:17:49 +00:00
|
|
|
if (!input) return
|
|
|
|
this.input = input
|
2019-06-08 13:23:58 +00:00
|
|
|
this.resize()
|
2021-04-18 14:03:31 +00:00
|
|
|
input.addEventListener('blur', this.onBlur)
|
|
|
|
input.addEventListener('focus', this.onFocus)
|
|
|
|
input.addEventListener('paste', this.onPaste)
|
|
|
|
input.addEventListener('keyup', this.onKeyUp)
|
|
|
|
input.addEventListener('keydown', this.onKeyDown)
|
|
|
|
input.addEventListener('click', this.onClickInput)
|
|
|
|
input.addEventListener('transitionend', this.onTransition)
|
|
|
|
input.addEventListener('input', this.onInput)
|
2019-06-06 21:17:49 +00:00
|
|
|
},
|
|
|
|
unmounted () {
|
2019-06-08 13:23:58 +00:00
|
|
|
const { input } = this
|
|
|
|
if (input) {
|
2021-04-18 14:03:31 +00:00
|
|
|
input.removeEventListener('blur', this.onBlur)
|
|
|
|
input.removeEventListener('focus', this.onFocus)
|
|
|
|
input.removeEventListener('paste', this.onPaste)
|
|
|
|
input.removeEventListener('keyup', this.onKeyUp)
|
|
|
|
input.removeEventListener('keydown', this.onKeyDown)
|
|
|
|
input.removeEventListener('click', this.onClickInput)
|
|
|
|
input.removeEventListener('transitionend', this.onTransition)
|
|
|
|
input.removeEventListener('input', this.onInput)
|
2019-03-26 02:38:15 +00:00
|
|
|
}
|
|
|
|
},
|
2020-05-07 13:10:53 +00:00
|
|
|
watch: {
|
|
|
|
showSuggestions: function (newValue) {
|
|
|
|
this.$emit('shown', newValue)
|
2020-11-17 15:46:26 +00:00
|
|
|
},
|
2020-11-19 10:35:21 +00:00
|
|
|
textAtCaret: async function (newWord) {
|
|
|
|
const firstchar = newWord.charAt(0)
|
2020-11-17 15:46:26 +00:00
|
|
|
this.suggestions = []
|
2022-10-29 20:50:31 +00:00
|
|
|
if (newWord === firstchar && firstchar !== '$') return
|
2020-11-19 10:35:21 +00:00
|
|
|
const matchedSuggestions = await this.suggest(newWord)
|
|
|
|
// Async: cancel if textAtCaret has changed during wait
|
|
|
|
if (this.textAtCaret !== newWord) return
|
2020-11-17 15:46:26 +00:00
|
|
|
if (matchedSuggestions.length <= 0) return
|
2020-11-18 16:43:24 +00:00
|
|
|
this.suggestions = take(matchedSuggestions, 5)
|
2020-11-19 09:37:06 +00:00
|
|
|
.map(({ imageUrl, ...rest }) => ({
|
2020-11-17 15:46:26 +00:00
|
|
|
...rest,
|
2020-11-19 09:37:06 +00:00
|
|
|
img: imageUrl || ''
|
2020-11-17 15:46:26 +00:00
|
|
|
}))
|
2020-11-18 16:43:24 +00:00
|
|
|
},
|
2022-03-22 16:14:56 +00:00
|
|
|
suggestions: {
|
|
|
|
handler (newValue) {
|
|
|
|
this.$nextTick(this.resize)
|
|
|
|
},
|
|
|
|
deep: true
|
2020-05-07 13:10:53 +00:00
|
|
|
}
|
|
|
|
},
|
2019-03-26 02:38:15 +00:00
|
|
|
methods: {
|
2021-02-25 15:27:29 +00:00
|
|
|
focusPickerInput () {
|
|
|
|
const pickerEl = this.$refs.picker.$el
|
|
|
|
if (!pickerEl) return
|
|
|
|
const pickerInput = pickerEl.querySelector('input')
|
|
|
|
if (pickerInput) pickerInput.focus()
|
|
|
|
},
|
2019-08-12 17:01:38 +00:00
|
|
|
triggerShowPicker () {
|
|
|
|
this.showPicker = true
|
2019-10-08 17:54:30 +00:00
|
|
|
this.$refs.picker.startEmojiLoad()
|
2019-09-25 16:58:15 +00:00
|
|
|
this.$nextTick(() => {
|
|
|
|
this.scrollIntoView()
|
2021-02-25 15:27:29 +00:00
|
|
|
this.focusPickerInput()
|
2019-09-25 16:58:15 +00:00
|
|
|
})
|
2019-09-08 12:51:17 +00: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-12 17:01:38 +00:00
|
|
|
},
|
2019-07-28 13:07:01 +00:00
|
|
|
togglePicker () {
|
2021-04-18 14:03:31 +00:00
|
|
|
this.input.focus()
|
2019-07-28 13:07:01 +00:00
|
|
|
this.showPicker = !this.showPicker
|
2019-09-25 16:58:15 +00:00
|
|
|
if (this.showPicker) {
|
|
|
|
this.scrollIntoView()
|
2019-10-08 17:54:30 +00:00
|
|
|
this.$refs.picker.startEmojiLoad()
|
2021-02-25 15:32:54 +00:00
|
|
|
this.$nextTick(this.focusPickerInput)
|
2019-09-25 16:58:15 +00:00
|
|
|
}
|
2019-07-28 13:07:01 +00:00
|
|
|
},
|
2019-03-26 02:38:15 +00:00
|
|
|
replace (replacement) {
|
2021-04-25 10:23:16 +00:00
|
|
|
const newValue = Completion.replaceWord(this.modelValue, this.wordAtCaret, replacement)
|
|
|
|
this.$emit('update:modelValue', newValue)
|
2019-03-26 02:38:15 +00:00
|
|
|
this.caret = 0
|
|
|
|
},
|
2020-06-21 14:13:29 +00:00
|
|
|
insert ({ insertion, keepOpen, surroundingSpace = true }) {
|
2021-04-25 10:23:16 +00:00
|
|
|
const before = this.modelValue.substring(0, this.caret) || ''
|
|
|
|
const after = this.modelValue.substring(this.caret) || ''
|
2019-09-15 09:09:19 +00:00
|
|
|
|
|
|
|
/* Using a bit more smart approach to padding emojis with spaces:
|
|
|
|
* - put a space before cursor if there isn't one already, unless we
|
|
|
|
* are at the beginning of post or in spam mode
|
|
|
|
* - put a space after emoji if there isn't one already unless we are
|
|
|
|
* in spam mode
|
|
|
|
*
|
|
|
|
* The idea is that when you put a cursor somewhere in between sentence
|
|
|
|
* inserting just ' :emoji: ' will add more spaces to post which might
|
|
|
|
* break the flow/spacing, as well as the case where user ends sentence
|
|
|
|
* with a space before adding emoji.
|
|
|
|
*
|
|
|
|
* Spam mode is intended for creating multi-part emojis and overall spamming
|
|
|
|
* them, masto seem to be rendering :emoji::emoji: correctly now so why not
|
|
|
|
*/
|
|
|
|
const isSpaceRegex = /\s/
|
2020-06-21 14:13:29 +00:00
|
|
|
const spaceBefore = (surroundingSpace && !isSpaceRegex.exec(before.slice(-1)) && before.length && this.padEmoji > 0) ? ' ' : ''
|
|
|
|
const spaceAfter = (surroundingSpace && !isSpaceRegex.exec(after[0]) && this.padEmoji) ? ' ' : ''
|
2019-09-15 09:09:19 +00:00
|
|
|
|
2019-07-28 13:07:01 +00:00
|
|
|
const newValue = [
|
2019-09-15 09:09:19 +00:00
|
|
|
before,
|
|
|
|
spaceBefore,
|
2019-07-28 13:07:01 +00:00
|
|
|
insertion,
|
2019-09-15 09:09:19 +00:00
|
|
|
spaceAfter,
|
|
|
|
after
|
2019-07-28 13:07:01 +00:00
|
|
|
].join('')
|
2019-09-23 17:29:01 +00:00
|
|
|
this.keepOpen = keepOpen
|
2021-04-25 10:23:16 +00:00
|
|
|
this.$emit('update:modelValue', newValue)
|
2019-09-15 09:09:19 +00:00
|
|
|
const position = this.caret + (insertion + spaceAfter + spaceBefore).length
|
2019-09-23 19:12:25 +00:00
|
|
|
if (!keepOpen) {
|
2021-04-18 14:03:31 +00:00
|
|
|
this.input.focus()
|
2019-09-23 19:12:25 +00:00
|
|
|
}
|
2019-08-12 17:01:38 +00:00
|
|
|
|
|
|
|
this.$nextTick(function () {
|
|
|
|
// Re-focus inputbox after clicking suggestion
|
|
|
|
// Set selection right after the replacement instead of the very end
|
2021-05-31 10:59:44 +00:00
|
|
|
this.input.setSelectionRange(position, position)
|
2019-08-12 17:01:38 +00:00
|
|
|
this.caret = position
|
|
|
|
})
|
2019-07-28 13:07:01 +00:00
|
|
|
},
|
2019-06-25 18:31:43 +00:00
|
|
|
replaceText (e, suggestion) {
|
2019-03-26 02:38:15 +00:00
|
|
|
const len = this.suggestions.length || 0
|
2019-06-25 18:31:43 +00:00
|
|
|
if (len > 0 || suggestion) {
|
|
|
|
const chosenSuggestion = suggestion || this.suggestions[this.highlighted]
|
|
|
|
const replacement = chosenSuggestion.replacement
|
2021-04-25 10:23:16 +00:00
|
|
|
const newValue = Completion.replaceWord(this.modelValue, this.wordAtCaret, replacement)
|
|
|
|
this.$emit('update:modelValue', newValue)
|
2019-03-26 02:38:15 +00:00
|
|
|
this.highlighted = 0
|
2019-06-17 18:24:10 +00:00
|
|
|
const position = this.wordAtCaret.start + replacement.length
|
|
|
|
|
|
|
|
this.$nextTick(function () {
|
|
|
|
// Re-focus inputbox after clicking suggestion
|
2021-04-18 14:03:31 +00:00
|
|
|
this.input.focus()
|
2019-06-17 18:24:10 +00:00
|
|
|
// Set selection right after the replacement instead of the very end
|
2021-04-18 14:03:31 +00:00
|
|
|
this.input.setSelectionRange(position, position)
|
2019-06-17 18:24:10 +00:00
|
|
|
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
|
2019-09-08 12:51:17 +00:00
|
|
|
if (len > 1) {
|
2019-03-26 02:38:15 +00:00
|
|
|
this.highlighted -= 1
|
|
|
|
if (this.highlighted < 0) {
|
|
|
|
this.highlighted = this.suggestions.length - 1
|
|
|
|
}
|
2019-06-09 18:17:24 +00:00
|
|
|
e.preventDefault()
|
2019-03-26 02:38:15 +00:00
|
|
|
} else {
|
|
|
|
this.highlighted = 0
|
|
|
|
}
|
|
|
|
},
|
|
|
|
cycleForward (e) {
|
|
|
|
const len = this.suggestions.length || 0
|
2019-09-08 12:51:17 +00:00
|
|
|
if (len > 1) {
|
2019-03-26 02:38:15 +00:00
|
|
|
this.highlighted += 1
|
|
|
|
if (this.highlighted >= len) {
|
|
|
|
this.highlighted = 0
|
|
|
|
}
|
2019-06-09 18:17:24 +00:00
|
|
|
e.preventDefault()
|
2019-03-26 02:38:15 +00:00
|
|
|
} else {
|
|
|
|
this.highlighted = 0
|
|
|
|
}
|
|
|
|
},
|
2019-09-25 16:58:15 +00:00
|
|
|
scrollIntoView () {
|
|
|
|
const rootRef = this.$refs['picker'].$el
|
|
|
|
/* Scroller is either `window` (replies in TL), sidebar (main post form,
|
|
|
|
* replies in notifs) or mobile post form. Note that getting and setting
|
|
|
|
* scroll is different for `Window` and `Element`s
|
|
|
|
*/
|
|
|
|
const scrollerRef = this.$el.closest('.sidebar-scroller') ||
|
|
|
|
this.$el.closest('.post-form-modal-view') ||
|
|
|
|
window
|
|
|
|
const currentScroll = scrollerRef === window
|
|
|
|
? scrollerRef.scrollY
|
|
|
|
: scrollerRef.scrollTop
|
|
|
|
const scrollerHeight = scrollerRef === window
|
|
|
|
? scrollerRef.innerHeight
|
|
|
|
: scrollerRef.offsetHeight
|
|
|
|
|
|
|
|
const scrollerBottomBorder = currentScroll + scrollerHeight
|
|
|
|
// We check where the bottom border of root element is, this uses findOffset
|
|
|
|
// to find offset relative to scrollable container (scroller)
|
|
|
|
const rootBottomBorder = rootRef.offsetHeight + findOffset(rootRef, scrollerRef).top
|
|
|
|
|
|
|
|
const bottomDelta = Math.max(0, rootBottomBorder - scrollerBottomBorder)
|
|
|
|
// could also check top delta but there's no case for it
|
|
|
|
const targetScroll = currentScroll + bottomDelta
|
|
|
|
|
|
|
|
if (scrollerRef === window) {
|
|
|
|
scrollerRef.scroll(0, targetScroll)
|
|
|
|
} else {
|
|
|
|
scrollerRef.scrollTop = targetScroll
|
|
|
|
}
|
2019-10-08 18:30:27 +00:00
|
|
|
|
|
|
|
this.$nextTick(() => {
|
2021-04-18 14:03:31 +00:00
|
|
|
const { offsetHeight } = this.input
|
2019-10-08 18:30:27 +00:00
|
|
|
const { picker } = this.$refs
|
|
|
|
const pickerBottom = picker.$el.getBoundingClientRect().bottom
|
|
|
|
if (pickerBottom > window.innerHeight) {
|
|
|
|
picker.$el.style.top = 'auto'
|
|
|
|
picker.$el.style.bottom = offsetHeight + 'px'
|
|
|
|
}
|
|
|
|
})
|
2019-09-25 16:58:15 +00:00
|
|
|
},
|
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
|
|
|
},
|
2019-06-08 13:23:58 +00:00
|
|
|
onBlur (e) {
|
2019-06-11 18:18:09 +00:00
|
|
|
// Clicking on any suggestion removes focus from autocomplete,
|
|
|
|
// preventing click handler ever executing.
|
2019-06-25 18:31:43 +00:00
|
|
|
this.blurTimeout = setTimeout(() => {
|
2019-06-11 18:18:09 +00:00
|
|
|
this.focused = false
|
|
|
|
this.setCaret(e)
|
2019-06-18 17:49:43 +00:00
|
|
|
this.resize()
|
2019-06-11 18:18:09 +00:00
|
|
|
}, 200)
|
2019-03-26 02:38:15 +00:00
|
|
|
},
|
2019-06-25 18:31:43 +00:00
|
|
|
onClick (e, suggestion) {
|
|
|
|
this.replaceText(e, suggestion)
|
2019-03-29 19:56:50 +00:00
|
|
|
},
|
2019-06-08 13:23:58 +00:00
|
|
|
onFocus (e) {
|
2019-06-25 18:31:43 +00:00
|
|
|
if (this.blurTimeout) {
|
|
|
|
clearTimeout(this.blurTimeout)
|
2019-06-25 21:34:09 +00:00
|
|
|
this.blurTimeout = null
|
2019-06-25 18:31:43 +00:00
|
|
|
}
|
|
|
|
|
2019-09-23 17:29:01 +00:00
|
|
|
if (!this.keepOpen) {
|
2019-09-08 12:51:17 +00:00
|
|
|
this.showPicker = false
|
|
|
|
}
|
2019-06-08 13:23:58 +00:00
|
|
|
this.focused = true
|
|
|
|
this.setCaret(e)
|
2019-06-18 17:49:43 +00:00
|
|
|
this.resize()
|
2019-09-12 17:14:35 +00:00
|
|
|
this.temporarilyHideSuggestions = false
|
2019-06-08 13:23:58 +00:00
|
|
|
},
|
|
|
|
onKeyUp (e) {
|
2019-09-12 17:14:35 +00:00
|
|
|
const { key } = e
|
2019-06-08 13:23:58 +00:00
|
|
|
this.setCaret(e)
|
2019-06-18 17:49:43 +00:00
|
|
|
this.resize()
|
2019-09-12 17:14:35 +00: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 13:23:58 +00:00
|
|
|
},
|
|
|
|
onPaste (e) {
|
|
|
|
this.setCaret(e)
|
2019-06-18 17:49:43 +00:00
|
|
|
this.resize()
|
2019-06-08 13:23:58 +00:00
|
|
|
},
|
2019-06-06 21:17:49 +00:00
|
|
|
onKeyDown (e) {
|
|
|
|
const { ctrlKey, shiftKey, key } = e
|
2020-06-21 14:13:29 +00:00
|
|
|
if (this.newlineOnCtrlEnter && ctrlKey && key === 'Enter') {
|
|
|
|
this.insert({ insertion: '\n', surroundingSpace: false })
|
|
|
|
// Ensure only one new line is added on macos
|
|
|
|
e.stopPropagation()
|
|
|
|
e.preventDefault()
|
|
|
|
|
|
|
|
// Scroll the input element to the position of the cursor
|
|
|
|
this.$nextTick(() => {
|
2021-04-18 14:03:31 +00:00
|
|
|
this.input.blur()
|
|
|
|
this.input.focus()
|
2020-06-21 14:13:29 +00:00
|
|
|
})
|
|
|
|
}
|
2019-09-12 17:14:35 +00: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-09 17:40:46 +00:00
|
|
|
this.cycleBackward(e)
|
2019-09-12 17:14:35 +00:00
|
|
|
} else if (key === 'ArrowDown') {
|
2019-06-09 17:40:46 +00:00
|
|
|
this.cycleForward(e)
|
2019-04-08 15:10:26 +00:00
|
|
|
}
|
2019-09-12 17:14:35 +00:00
|
|
|
if (key === 'Enter') {
|
|
|
|
if (!ctrlKey) {
|
|
|
|
this.replaceText(e)
|
|
|
|
}
|
|
|
|
}
|
2019-04-08 15:10:26 +00:00
|
|
|
}
|
2019-09-12 17:14:35 +00: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) {
|
2021-04-18 14:03:31 +00:00
|
|
|
this.input.focus()
|
2019-06-06 21:17:49 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-12 17:14:35 +00:00
|
|
|
|
|
|
|
this.showPicker = false
|
|
|
|
this.resize()
|
2019-03-26 02:38:15 +00:00
|
|
|
},
|
|
|
|
onInput (e) {
|
2019-07-28 13:07:01 +00:00
|
|
|
this.showPicker = false
|
2019-07-03 20:08:51 +00:00
|
|
|
this.setCaret(e)
|
2019-09-12 17:14:35 +00:00
|
|
|
this.resize()
|
2021-04-25 10:23:16 +00:00
|
|
|
this.$emit('update:modelValue', e.target.value)
|
2019-07-03 20:08:51 +00:00
|
|
|
},
|
2019-09-25 16:58:15 +00:00
|
|
|
onClickInput (e) {
|
2019-09-25 17:23:55 +00:00
|
|
|
this.showPicker = false
|
2019-09-25 16:58:15 +00:00
|
|
|
},
|
2019-09-08 12:51:17 +00:00
|
|
|
onClickOutside (e) {
|
|
|
|
if (this.disableClickOutside) return
|
2019-07-28 13:07:01 +00:00
|
|
|
this.showPicker = false
|
|
|
|
},
|
2019-08-12 17:01:38 +00:00
|
|
|
onStickerUploaded (e) {
|
|
|
|
this.showPicker = false
|
|
|
|
this.$emit('sticker-uploaded', e)
|
|
|
|
},
|
|
|
|
onStickerUploadFailed (e) {
|
|
|
|
this.showPicker = false
|
|
|
|
this.$emit('sticker-upload-Failed', e)
|
|
|
|
},
|
2019-06-10 15:57:53 +00:00
|
|
|
setCaret ({ target: { selectionStart } }) {
|
2019-03-26 02:38:15 +00:00
|
|
|
this.caret = selectionStart
|
2019-06-08 13:23:58 +00:00
|
|
|
},
|
|
|
|
resize () {
|
2020-05-07 13:10:53 +00:00
|
|
|
const panel = this.$refs.panel
|
2019-06-08 13:23:58 +00:00
|
|
|
if (!panel) return
|
2020-05-07 13:10:53 +00:00
|
|
|
const picker = this.$refs.picker.$el
|
|
|
|
const panelBody = this.$refs['panel-body']
|
2021-04-18 14:03:31 +00:00
|
|
|
const { offsetHeight, offsetTop } = this.input
|
2019-10-08 18:30:27 +00:00
|
|
|
const offsetBottom = offsetTop + offsetHeight
|
|
|
|
|
2020-05-07 13:10:53 +00:00
|
|
|
this.setPlacement(panelBody, panel, offsetBottom)
|
|
|
|
this.setPlacement(picker, picker, offsetBottom)
|
|
|
|
},
|
|
|
|
setPlacement (container, target, offsetBottom) {
|
|
|
|
if (!container || !target) return
|
2022-08-06 00:01:18 +00:00
|
|
|
if (this.placement === 'bottom' || (this.placement === 'auto' && !this.overflowsBottom(container))) {
|
|
|
|
target.style.top = offsetBottom + 'px'
|
|
|
|
target.style.bottom = 'auto'
|
|
|
|
} else {
|
2020-05-07 13:10:53 +00:00
|
|
|
target.style.top = 'auto'
|
2021-04-18 14:03:31 +00:00
|
|
|
target.style.bottom = this.input.offsetHeight + 'px'
|
2020-05-07 13:10:53 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
overflowsBottom (el) {
|
|
|
|
return el.getBoundingClientRect().bottom > window.innerHeight
|
2019-03-26 02:38:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default EmojiInput
|