refs #517 Suggest emojis in new toot

This commit is contained in:
AkiraFukushima 2018-08-19 12:45:22 +09:00
parent 8902a74bfe
commit d8b81281d6
1 changed files with 48 additions and 13 deletions

View File

@ -16,10 +16,10 @@
v-model="openSuggest"> v-model="openSuggest">
<ul class="suggest-list"> <ul class="suggest-list">
<li <li
v-for="(item, index) in filteredAccounts" v-for="(item, index) in filteredSuggestion"
:key="index" :key="index"
@click="insertAccount(item)" @click="insertItem(item)"
@shortkey="insertAccount(item)" @shortkey="insertItem(item)"
@mouseover="highlightedIndex = index" @mouseover="highlightedIndex = index"
:class="{'highlighted': highlightedIndex === index}"> :class="{'highlighted': highlightedIndex === index}">
{{ item }} {{ item }}
@ -49,7 +49,13 @@ export default {
openSuggest: false, openSuggest: false,
highlightedIndex: 0, highlightedIndex: 0,
startIndex: null, startIndex: null,
matchWord: null matchWord: null,
filteredSuggestion: [],
emojis: [
':python:',
':slack:',
':nodejs'
]
} }
}, },
computed: { computed: {
@ -82,10 +88,16 @@ export default {
// Start suggest after user stop writing // Start suggest after user stop writing
setTimeout(() => { setTimeout(() => {
if (currentValue === this.status) { if (currentValue === this.status) {
this.suggestAccount(e) this.suggest(e)
} }
}, 500) }, 500)
}, },
async suggest (e) {
const ac = await this.suggestAccount(e)
if (!ac) {
this.suggestEmoji(e)
}
},
async suggestAccount (e) { async suggestAccount (e) {
// e.target.sectionStart: Cursor position // e.target.sectionStart: Cursor position
// e.target.value: current value of the textarea // e.target.value: current value of the textarea
@ -99,34 +111,57 @@ export default {
this.openSuggest = true this.openSuggest = true
this.startIndex = start this.startIndex = start
this.matchWord = word this.matchWord = word
this.filteredSuggestion = this.filteredAccounts
return true
} catch (err) { } catch (err) {
console.log(err) console.log(err)
return false
} }
}, },
suggestEmoji (e) {
// e.target.sectionStart: Cursor position
// e.target.value: current value of the textarea
const [start, word] = suggestText(e.target.value, e.target.selectionStart, ':')
if (!start || !word) {
this.closeSuggest()
return false
}
const filtered = this.emojis.filter(emoji => emoji.includes(word))
if (filtered.length > 0) {
this.openSuggest = true
this.startIndex = start
this.matchWord = word
this.filteredSuggestion = filtered
} else {
this.openSuggest = false
}
return true
},
closeSuggest () { closeSuggest () {
this.openSuggest = false this.openSuggest = false
this.startIndex = null this.startIndex = null
this.matchWord = null this.matchWord = null
this.highlightedIndex = 0 this.highlightedIndex = 0
this.filteredSuggestion = []
this.$store.commit('TimelineSpace/Modals/NewToot/Status/clearFilteredAccounts') this.$store.commit('TimelineSpace/Modals/NewToot/Status/clearFilteredAccounts')
}, },
suggestHighlight (index) { suggestHighlight (index) {
if (index < 0) { if (index < 0) {
this.highlightedIndex = 0 this.highlightedIndex = 0
} else if (index >= this.filteredAccounts.length) { } else if (index >= this.filteredSuggestion.length) {
this.highlightedIndex = this.filteredAccounts.length - 1 this.highlightedIndex = this.filteredSuggestion.length - 1
} else { } else {
this.highlightedIndex = index this.highlightedIndex = index
} }
}, },
insertAccount (account) { insertItem (item) {
const str = `${this.status.slice(0, this.startIndex)}${account} ${this.status.slice(this.startIndex + this.matchWord.length)}` const str = `${this.status.slice(0, this.startIndex)}${item} ${this.status.slice(this.startIndex + this.matchWord.length)}`
this.status = str this.status = str
this.closeSuggest() this.closeSuggest()
}, },
selectCurrentAccount () { selectCurrentItem () {
const account = this.filteredAccounts[this.highlightedIndex] const item = this.filteredSuggestion[this.highlightedIndex]
this.insertAccount(account) this.insertItem(item)
}, },
handleKey (event) { handleKey (event) {
const current = event.target.selectionStart const current = event.target.selectionStart
@ -138,7 +173,7 @@ export default {
this.suggestHighlight(this.highlightedIndex + 1) this.suggestHighlight(this.highlightedIndex + 1)
break break
case 'enter': case 'enter':
this.selectCurrentAccount() this.selectCurrentItem()
break break
case 'esc': case 'esc':
this.closeSuggest() this.closeSuggest()