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: { computed: {
...mapState({ ...mapState({
filteredAccounts: state => state.TimelineSpace.Modals.NewToot.Status.filteredAccounts,
customEmojis: state => state.TimelineSpace.emojis customEmojis: state => state.TimelineSpace.emojis
}), }),
...mapState('TimelineSpace/Modals/NewToot/Status', {
filteredAccounts: state => state.filteredAccounts,
filteredHashtags: state => state.filteredHashtags
}),
status: { status: {
get: function () { get: function () {
return this.value return this.value
@ -102,10 +105,16 @@ export default {
}, 500) }, 500)
}, },
async suggest (e) { async suggest (e) {
const ac = await this.suggestAccount(e) const emoji = this.suggestEmoji(e)
if (!ac) { if (emoji) {
this.suggestEmoji(e) return true
} }
const ac = await this.suggestAccount(e)
if (ac) {
return true
}
const tag = await this.suggestHashtag(e)
return tag
}, },
async suggestAccount (e) { async suggestAccount (e) {
// e.target.sectionStart: Cursor position // e.target.sectionStart: Cursor position
@ -127,6 +136,24 @@ export default {
return false 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) { suggestEmoji (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
@ -164,6 +191,7 @@ export default {
this.highlightedIndex = 0 this.highlightedIndex = 0
this.filteredSuggestion = [] this.filteredSuggestion = []
this.$store.commit('TimelineSpace/Modals/NewToot/Status/clearFilteredAccounts') this.$store.commit('TimelineSpace/Modals/NewToot/Status/clearFilteredAccounts')
this.$store.commit('TimelineSpace/Modals/NewToot/Status/clearFilteredHashtags')
}, },
suggestHighlight (index) { suggestHighlight (index) {
if (index < 0) { if (index < 0) {

View File

@ -3,7 +3,8 @@ import Mastodon from 'megalodon'
const Status = { const Status = {
namespaced: true, namespaced: true,
state: { state: {
filteredAccounts: [] filteredAccounts: [],
filteredHashtags: []
}, },
mutations: { mutations: {
updateFilteredAccounts (state, accounts) { updateFilteredAccounts (state, accounts) {
@ -16,6 +17,17 @@ const Status = {
}, },
clearFilteredAccounts (state) { clearFilteredAccounts (state) {
state.filteredAccounts = [] state.filteredAccounts = []
},
updateFilteredHashtags (state, tags) {
state.filteredHashtags = tags.map((t) => {
return {
name: `#${t}`,
image: null
}
})
},
clearFilteredHashtags (state) {
state.filteredHashtags = []
} }
}, },
actions: { actions: {
@ -28,6 +40,16 @@ const Status = {
commit('updateFilteredAccounts', res.data.accounts) commit('updateFilteredAccounts', res.data.accounts)
if (res.data.accounts.length === 0) throw new Error('Empty') if (res.data.accounts.length === 0) throw new Error('Empty')
return res.data.accounts 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
} }
} }
} }