refs #609 Suggest hashtags in new toot

This commit is contained in:
AkiraFukushima 2018-09-18 15:49:29 +09:00
parent efc6c63450
commit 0f93e7ec32
2 changed files with 55 additions and 5 deletions

View File

@ -63,9 +63,12 @@ export default {
},
computed: {
...mapState({
filteredAccounts: state => state.TimelineSpace.Modals.NewToot.Status.filteredAccounts,
customEmojis: state => state.TimelineSpace.emojis
}),
...mapState('TimelineSpace/Modals/NewToot/Status', {
filteredAccounts: state => state.filteredAccounts,
filteredHashtags: state => state.filteredHashtags
}),
status: {
get: function () {
return this.value
@ -102,10 +105,16 @@ export default {
}, 500)
},
async suggest (e) {
const ac = await this.suggestAccount(e)
if (!ac) {
this.suggestEmoji(e)
const emoji = this.suggestEmoji(e)
if (emoji) {
return true
}
const ac = await this.suggestAccount(e)
if (ac) {
return true
}
const tag = await this.suggestHashtag(e)
return tag
},
async suggestAccount (e) {
// e.target.sectionStart: Cursor position
@ -127,6 +136,24 @@ export default {
return false
}
},
async suggestHashtag (e) {
const [start, word] = suggestText(e.target.value, e.target.selectionStart, '#')
if (!start || !word) {
this.closeSuggest()
return false
}
try {
await this.$store.dispatch('TimelineSpace/Modals/NewToot/Status/searchHashtag', word)
this.openSuggest = true
this.startIndex = start
this.matchWord = word
this.filteredSuggestion = this.filteredHashtags
return true
} catch (err) {
console.log(err)
return false
}
},
suggestEmoji (e) {
// e.target.sectionStart: Cursor position
// e.target.value: current value of the textarea
@ -164,6 +191,7 @@ export default {
this.highlightedIndex = 0
this.filteredSuggestion = []
this.$store.commit('TimelineSpace/Modals/NewToot/Status/clearFilteredAccounts')
this.$store.commit('TimelineSpace/Modals/NewToot/Status/clearFilteredHashtags')
},
suggestHighlight (index) {
if (index < 0) {

View File

@ -3,7 +3,8 @@ import Mastodon from 'megalodon'
const Status = {
namespaced: true,
state: {
filteredAccounts: []
filteredAccounts: [],
filteredHashtags: []
},
mutations: {
updateFilteredAccounts (state, accounts) {
@ -16,6 +17,17 @@ const Status = {
},
clearFilteredAccounts (state) {
state.filteredAccounts = []
},
updateFilteredHashtags (state, tags) {
state.filteredHashtags = tags.map((t) => {
return {
name: `#${t}`,
image: null
}
})
},
clearFilteredHashtags (state) {
state.filteredHashtags = []
}
},
actions: {
@ -28,6 +40,16 @@ const Status = {
commit('updateFilteredAccounts', res.data.accounts)
if (res.data.accounts.length === 0) throw new Error('Empty')
return res.data.accounts
},
async searchHashtag ({ commit, rootState }, word) {
const client = new Mastodon(
rootState.TimelineSpace.account.accessToken,
rootState.TimelineSpace.account.baseURL + '/api/v1'
)
const res = await client.get('/search', { q: word })
commit('updateFilteredHashtags', res.data.hashtags)
if (res.data.hashtags.length === 0) throw new Error('Empty')
return res.data.hashtags
}
}
}