standartized autocomplete panel suggesions format, fixed some bugs

This commit is contained in:
Henry Jameson 2019-06-08 16:23:58 +03:00
parent ca3140fd3e
commit 8872b4802e
7 changed files with 129 additions and 112 deletions

View file

@ -809,14 +809,10 @@ nav {
.autocomplete { .autocomplete {
&-panel { &-panel {
position: relative;
&-body { &-body {
margin: 0 0.5em 0 0.5em; margin: 0 0.5em 0 0.5em;
border-radius: $fallback--tooltipRadius; border-radius: $fallback--tooltipRadius;
border-radius: var(--tooltipRadius, $fallback--tooltipRadius); border-radius: var(--tooltipRadius, $fallback--tooltipRadius);
position: absolute;
z-index: 1;
box-shadow: 1px 2px 4px rgba(0, 0, 0, 0.5); box-shadow: 1px 2px 4px rgba(0, 0, 0, 0.5);
// this doesn't match original but i don't care, making it uniform. // this doesn't match original but i don't care, making it uniform.
box-shadow: var(--popupShadow); box-shadow: var(--popupShadow);

View file

@ -154,9 +154,9 @@ const getStaticEmoji = async ({ store }) => {
const values = await res.json() const values = await res.json()
const emoji = Object.keys(values).map((key) => { const emoji = Object.keys(values).map((key) => {
return { return {
shortcode: key, displayText: key,
image_url: false, imageUrl: false,
'replacement': values[key] replacement: values[key]
} }
}) })
store.dispatch('setInstanceOption', { name: 'emoji', value: emoji }) store.dispatch('setInstanceOption', { name: 'emoji', value: emoji })
@ -178,9 +178,10 @@ const getCustomEmoji = async ({ store }) => {
const result = await res.json() const result = await res.json()
const values = Array.isArray(result) ? Object.assign({}, ...result) : result const values = Array.isArray(result) ? Object.assign({}, ...result) : result
const emoji = Object.keys(values).map((key) => { const emoji = Object.keys(values).map((key) => {
const imageUrl = values[key].image_url
return { return {
shortcode: key, displayText: key,
image_url: values[key].image_url || values[key], imageUrl: imageUrl ? store.state.instance.server + imageUrl : values[key],
replacement: `:${key}: ` replacement: `:${key}: `
} }
}) })

View file

@ -13,7 +13,19 @@ const EmojiInput = {
return { return {
input: undefined, input: undefined,
highlighted: 0, 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: { computed: {
@ -24,13 +36,17 @@ const EmojiInput = {
if (matchedSuggestions.length <= 0) { if (matchedSuggestions.length <= 0) {
return false return false
} }
return take(matchedSuggestions, 5).map(({shortcode, image_url, replacement}, index) => ({ return take(matchedSuggestions, 5)
shortcode, .map(({ displayText, imageUrl, replacement }, index) => ({
replacement, displayText,
// eslint-disable-next-line camelcase replacement,
img: !image_url ? '' : this.$store.state.instance.server + image_url, // eslint-disable-next-line camelcase
highlighted: index === this.highlighted img: imageUrl || '',
})) highlighted: index === this.highlighted
}))
},
showPopup () {
return this.focused && this.suggestions && this.suggestions.length > 0
}, },
textAtCaret () { textAtCaret () {
return (this.wordAtCaret || {}).word || '' return (this.wordAtCaret || {}).word || ''
@ -40,25 +56,29 @@ const EmojiInput = {
const word = Completion.wordAtPosition(this.value, this.caret - 1) || {} const word = Completion.wordAtPosition(this.value, this.caret - 1) || {}
return word return word
} }
}, }
}, },
mounted () { mounted () {
const slots = this.$slots.default 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)) const input = slots.find(slot => ['input', 'textarea'].includes(slot.tag))
if (!input) return if (!input) return
this.input = input this.input = input
input.elm.addEventListener('keyup', this.setCaret) this.resize()
input.elm.addEventListener('paste', this.setCaret) input.elm.addEventListener('blur', this.onBlur)
input.elm.addEventListener('focus', this.setCaret) 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('keydown', this.onKeyDown)
}, },
unmounted () { unmounted () {
if (this.input) { const { input } = this
this.input.elm.removeEventListener('keyup', this.setCaret) if (input) {
this.input.elm.removeEventListener('paste', this.setCaret) input.elm.removeEventListener('blur', this.onBlur)
this.input.elm.removeEventListener('focus', this.setCaret) input.elm.removeEventListener('focus', this.onFocus)
this.input.elm.removeEventListener('keydown', this.onKeyDown) input.elm.removeEventListener('paste', this.onPaste)
input.elm.removeEventListener('keyup', this.onKeyUp)
input.elm.removeEventListener('keydown', this.onKeyDown)
} }
}, },
methods: { methods: {
@ -101,26 +121,49 @@ const EmojiInput = {
this.highlighted = 0 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) { onKeyDown (e) {
this.setCaret(e) this.setCaret(e)
e.stopPropagation() this.resize(e)
const { ctrlKey, shiftKey, key } = e const { ctrlKey, shiftKey, key } = e
if (key === 'Tab') { if (key === 'Tab') {
if (shiftKey) { if (shiftKey) {
this.cycleBackward() this.cycleBackward()
e.preventDefault()
} else { } else {
this.cycleForward() this.cycleForward()
e.preventDefault()
} }
} }
if (key === 'ArrowUp') { if (key === 'ArrowUp') {
this.cycleBackward() this.cycleBackward()
e.preventDefault()
} else if (key === 'ArrowDown') { } else if (key === 'ArrowDown') {
this.cycleForward() this.cycleForward()
e.preventDefault()
} }
if (key === 'Enter') { if (key === 'Enter') {
if (!ctrlKey) { if (!ctrlKey) {
this.replaceText() this.replaceText()
e.preventDefault()
} }
} }
}, },
@ -129,6 +172,12 @@ const EmojiInput = {
}, },
setCaret ({ target: { selectionStart, value } }) { setCaret ({ target: { selectionStart, value } }) {
this.caret = selectionStart 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'
} }
} }
} }

View file

@ -1,38 +1,25 @@
<template> <template>
<div class="emoji-input"> <div class="emoji-input">
<slot <slot></slot>
:class="classname" <div ref="panel" class="autocomplete-panel" :class="{ hide: suggestions}">
:value="value" <div class="autocomplete-panel-body">
:placeholder="placeholder" <div
@input="onInput" v-for="(suggestion, index) in suggestions"
@click="setCaret" :key="index"
@keyup="setCaret" @click.stop.prevent="replace(suggestion.replacement)"
@keydown="onKeydown" class="autocomplete-item"
@keydown.down="cycleForward" :class="{ highlighted: suggestion.highlighted }"
@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 }"
> >
<span v-if="suggestion.img"> <span v-if="suggestion.img">
<img :src="suggestion.img" /> <img :src="suggestion.img" />
</span> </span>
<span v-else>{{suggestion.replacement}}</span> <span v-else>{{suggestion.replacement}}</span>
<span>{{suggestion.shortcode}}</span> <span>{{suggestion.displayText}}</span>
<span>{{suggestion.detailText}}</span>
</div> </div>
</div>
</div> </div>
</div> </div>
</div>
</template> </template>
<script src="./emoji-input.js"></script> <script src="./emoji-input.js"></script>
@ -41,8 +28,21 @@
@import '../../_variables.scss'; @import '../../_variables.scss';
.emoji-input { .emoji-input {
.form-control { display: flex;
width: 100%; flex-direction: column;
&.hide {
display: none
}
.autocomplete-panel {
position: absolute;
z-index: 9;
margin-top: 2px;
}
input, textarea {
flex: 1;
} }
} }
</style> </style>

View file

@ -15,24 +15,26 @@ export default function suggest (data) {
function suggestEmoji (emojis) { function suggestEmoji (emojis) {
return input => { return input => {
const shortcode = input.toLowerCase().substr(1) const noPrefix = input.toLowerCase().substr(1)
console.log(shortcode) return emojis
return emojis.filter(emoji => emoji.shortcode.toLowerCase().startsWith(shortcode)) .filter(({ displayText }) => displayText.toLowerCase().startsWith(noPrefix))
} }
} }
function suggestUsers (users) { function suggestUsers (users) {
return input => { return input => {
const shortcode = input.toLowerCase().substr(1) const noPrefix = input.toLowerCase().substr(1)
return users.filter( return users.filter(
user => user =>
user.screen_name.toLowerCase().startsWith('@' + shortcode) || user.screen_name.toLowerCase().startsWith(noPrefix) ||
user.name.toLowerCase().startsWith(shortcode) user.name.toLowerCase().startsWith(noPrefix)
/* eslint-disable camelcase */
).map(({ screen_name, name, profile_image_url_original }) => ({ ).map(({ screen_name, name, profile_image_url_original }) => ({
shortcode: screen_name, displayText: screen_name,
detail: name, detailText: name,
image_url: profile_image_url_original, imageUrl: profile_image_url_original,
replacement: '@' + screen_name replacement: '@' + screen_name
})) }))
/* eslint-enable camelcase */
} }
} }

View file

@ -83,43 +83,6 @@ const PostStatusForm = {
} }
}, },
computed: { computed: {
candidates () {
const firstchar = this.textAtCaret.charAt(0)
if (firstchar === '@') {
const query = this.textAtCaret.slice(1).toUpperCase()
const matchedUsers = filter(this.users, (user) => {
return user.screen_name.toUpperCase().startsWith(query) ||
user.name && user.name.toUpperCase().startsWith(query)
})
if (matchedUsers.length <= 0) {
return false
}
// eslint-disable-next-line camelcase
return map(take(matchedUsers, 5), ({screen_name, name, profile_image_url_original}, index) => ({
// eslint-disable-next-line camelcase
screen_name: `@${screen_name}`,
name: name,
img: profile_image_url_original,
highlighted: index === this.highlighted
}))
} else if (firstchar === ':') {
if (this.textAtCaret === ':') { return }
const matchedEmoji = filter(this.emoji.concat(this.customEmoji), (emoji) => emoji.shortcode.startsWith(this.textAtCaret.slice(1)))
if (matchedEmoji.length <= 0) {
return false
}
return map(take(matchedEmoji, 5), ({shortcode, image_url, utf}, index) => ({
screen_name: `:${shortcode}:`,
name: '',
utf: utf || '',
// eslint-disable-next-line camelcase
img: utf ? '' : this.$store.state.instance.server + image_url,
highlighted: index === this.highlighted
}))
} else {
return false
}
},
users () { users () {
return this.$store.state.users.users return this.$store.state.users.users
}, },

View file

@ -32,24 +32,29 @@
<span v-else>{{ $t('post_status.direct_warning_to_all') }}</span> <span v-else>{{ $t('post_status.direct_warning_to_all') }}</span>
</p> </p>
<emoji-input <emoji-input
:suggest="emojiSuggestor" v-model="newStatus.spoilerText"
v-if="newStatus.spoilerText || alwaysShowSubject" v-if="newStatus.spoilerText || alwaysShowSubject"
:suggest="emojiSuggestor"
v-model="newStatus.spoilerText"
class="form-control"
> >
<input <input
type="text" type="text"
:placeholder="$t('post_status.content_warning')" :placeholder="$t('post_status.content_warning')"
v-model="newStatus.spoilerText" v-model="newStatus.spoilerText"
classname="form-control" class="form-post-subject"
/> />
</emoji-input> </emoji-input>
<emoji-input :suggest="emojiUserSuggestor" v-model="newStatus.status"> <emoji-input
:suggest="emojiUserSuggestor"
v-model="newStatus.status"
class="form-control"
>
<textarea <textarea
ref="textarea" ref="textarea"
v-model="newStatus.status" v-model="newStatus.status"
:placeholder="$t('post_status.default')" :placeholder="$t('post_status.default')"
rows="1" rows="1"
class="form-control"
@keydown.meta.enter="postStatus(newStatus)" @keydown.meta.enter="postStatus(newStatus)"
@keyup.ctrl.enter="postStatus(newStatus)" @keyup.ctrl.enter="postStatus(newStatus)"
@drop="fileDrop" @drop="fileDrop"
@ -57,6 +62,7 @@
@input="resize" @input="resize"
@paste="paste" @paste="paste"
:disabled="posting" :disabled="posting"
class="form-post-body"
> >
</textarea> </textarea>
</emoji-input> </emoji-input>
@ -251,7 +257,7 @@
min-height: 1px; min-height: 1px;
} }
form textarea.form-control { .form-post-body {
line-height:16px; line-height:16px;
resize: none; resize: none;
overflow: hidden; overflow: hidden;
@ -260,7 +266,7 @@
box-sizing: content-box; box-sizing: content-box;
} }
form textarea.form-control:focus { .form-post-body:focus {
min-height: 48px; min-height: 48px;
} }