standartized autocomplete panel suggesions format, fixed some bugs
This commit is contained in:
parent
ca3140fd3e
commit
8872b4802e
7 changed files with 129 additions and 112 deletions
|
@ -13,7 +13,19 @@ const EmojiInput = {
|
|||
return {
|
||||
input: undefined,
|
||||
highlighted: 0,
|
||||
caret: 0
|
||||
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' }
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
computed: {
|
||||
|
@ -24,13 +36,17 @@ const EmojiInput = {
|
|||
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
|
||||
}))
|
||||
return take(matchedSuggestions, 5)
|
||||
.map(({ displayText, imageUrl, replacement }, index) => ({
|
||||
displayText,
|
||||
replacement,
|
||||
// eslint-disable-next-line camelcase
|
||||
img: imageUrl || '',
|
||||
highlighted: index === this.highlighted
|
||||
}))
|
||||
},
|
||||
showPopup () {
|
||||
return this.focused && this.suggestions && this.suggestions.length > 0
|
||||
},
|
||||
textAtCaret () {
|
||||
return (this.wordAtCaret || {}).word || ''
|
||||
|
@ -40,25 +56,29 @@ const EmojiInput = {
|
|||
const word = Completion.wordAtPosition(this.value, this.caret - 1) || {}
|
||||
return word
|
||||
}
|
||||
},
|
||||
}
|
||||
},
|
||||
mounted () {
|
||||
const slots = this.$slots.default
|
||||
if (slots.length === 0) return
|
||||
if (!slots || 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)
|
||||
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)
|
||||
},
|
||||
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)
|
||||
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)
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
|
@ -101,26 +121,49 @@ const EmojiInput = {
|
|||
this.highlighted = 0
|
||||
}
|
||||
},
|
||||
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)
|
||||
},
|
||||
onKeyDown (e) {
|
||||
this.setCaret(e)
|
||||
e.stopPropagation()
|
||||
this.resize(e)
|
||||
|
||||
const { ctrlKey, shiftKey, key } = e
|
||||
if (key === 'Tab') {
|
||||
if (shiftKey) {
|
||||
this.cycleBackward()
|
||||
e.preventDefault()
|
||||
} else {
|
||||
this.cycleForward()
|
||||
e.preventDefault()
|
||||
}
|
||||
}
|
||||
if (key === 'ArrowUp') {
|
||||
this.cycleBackward()
|
||||
e.preventDefault()
|
||||
} else if (key === 'ArrowDown') {
|
||||
this.cycleForward()
|
||||
e.preventDefault()
|
||||
}
|
||||
if (key === 'Enter') {
|
||||
if (!ctrlKey) {
|
||||
this.replaceText()
|
||||
e.preventDefault()
|
||||
}
|
||||
}
|
||||
},
|
||||
|
@ -129,6 +172,12 @@ const EmojiInput = {
|
|||
},
|
||||
setCaret ({ target: { selectionStart, value } }) {
|
||||
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'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,38 +1,25 @@
|
|||
<template>
|
||||
<div class="emoji-input">
|
||||
<slot
|
||||
:class="classname"
|
||||
:value="value"
|
||||
:placeholder="placeholder"
|
||||
@input="onInput"
|
||||
@click="setCaret"
|
||||
@keyup="setCaret"
|
||||
@keydown="onKeydown"
|
||||
@keydown.down="cycleForward"
|
||||
@keydown.up="cycleBackward"
|
||||
@keydown.shift.tab="cycleBackward"
|
||||
@keydown.tab="cycleForward"
|
||||
@keydown.enter="replaceEmoji"
|
||||
>
|
||||
</slot>
|
||||
<div class="autocomplete-panel" v-if="suggestions">
|
||||
<div class="autocomplete-panel-body">
|
||||
<div
|
||||
v-for="(suggestion, index) in suggestions"
|
||||
:key="index"
|
||||
@click="replace(suggestion.replacement)"
|
||||
class="autocomplete-item"
|
||||
:class="{ highlighted: suggestion.highlighted }"
|
||||
<div class="emoji-input">
|
||||
<slot></slot>
|
||||
<div ref="panel" class="autocomplete-panel" :class="{ hide: suggestions}">
|
||||
<div class="autocomplete-panel-body">
|
||||
<div
|
||||
v-for="(suggestion, index) in suggestions"
|
||||
:key="index"
|
||||
@click.stop.prevent="replace(suggestion.replacement)"
|
||||
class="autocomplete-item"
|
||||
:class="{ highlighted: suggestion.highlighted }"
|
||||
>
|
||||
<span v-if="suggestion.img">
|
||||
<img :src="suggestion.img" />
|
||||
</span>
|
||||
<span v-else>{{suggestion.replacement}}</span>
|
||||
<span>{{suggestion.shortcode}}</span>
|
||||
<span v-if="suggestion.img">
|
||||
<img :src="suggestion.img" />
|
||||
</span>
|
||||
<span v-else>{{suggestion.replacement}}</span>
|
||||
<span>{{suggestion.displayText}}</span>
|
||||
<span>{{suggestion.detailText}}</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script src="./emoji-input.js"></script>
|
||||
|
@ -41,8 +28,21 @@
|
|||
@import '../../_variables.scss';
|
||||
|
||||
.emoji-input {
|
||||
.form-control {
|
||||
width: 100%;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
|
||||
&.hide {
|
||||
display: none
|
||||
}
|
||||
|
||||
.autocomplete-panel {
|
||||
position: absolute;
|
||||
z-index: 9;
|
||||
margin-top: 2px;
|
||||
}
|
||||
|
||||
input, textarea {
|
||||
flex: 1;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
|
|
|
@ -15,24 +15,26 @@ export default function suggest (data) {
|
|||
|
||||
function suggestEmoji (emojis) {
|
||||
return input => {
|
||||
const shortcode = input.toLowerCase().substr(1)
|
||||
console.log(shortcode)
|
||||
return emojis.filter(emoji => emoji.shortcode.toLowerCase().startsWith(shortcode))
|
||||
const noPrefix = input.toLowerCase().substr(1)
|
||||
return emojis
|
||||
.filter(({ displayText }) => displayText.toLowerCase().startsWith(noPrefix))
|
||||
}
|
||||
}
|
||||
|
||||
function suggestUsers (users) {
|
||||
return input => {
|
||||
const shortcode = input.toLowerCase().substr(1)
|
||||
const noPrefix = input.toLowerCase().substr(1)
|
||||
return users.filter(
|
||||
user =>
|
||||
user.screen_name.toLowerCase().startsWith('@' + shortcode) ||
|
||||
user.name.toLowerCase().startsWith(shortcode)
|
||||
user.screen_name.toLowerCase().startsWith(noPrefix) ||
|
||||
user.name.toLowerCase().startsWith(noPrefix)
|
||||
/* eslint-disable camelcase */
|
||||
).map(({ screen_name, name, profile_image_url_original }) => ({
|
||||
shortcode: screen_name,
|
||||
detail: name,
|
||||
image_url: profile_image_url_original,
|
||||
displayText: screen_name,
|
||||
detailText: name,
|
||||
imageUrl: profile_image_url_original,
|
||||
replacement: '@' + screen_name
|
||||
}))
|
||||
/* eslint-enable camelcase */
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue