2018-05-06 23:25:17 +00:00
|
|
|
import { store } from '../_store/store'
|
|
|
|
|
2020-06-29 06:12:14 +00:00
|
|
|
const emojiMapper = emoji => emoji.unicode ? emoji.unicode : `:${emoji.shortcodes[0]}:`
|
2019-10-13 01:06:04 +00:00
|
|
|
const hashtagMapper = hashtag => `#${hashtag.name}`
|
|
|
|
const accountMapper = account => `@${account.acct}`
|
|
|
|
|
|
|
|
async function insertTextAtPosition (realm, text, startIndex, endIndex) {
|
2019-08-03 20:49:37 +00:00
|
|
|
const { currentInstance } = store.get()
|
|
|
|
const oldText = store.getComposeData(realm, 'text')
|
|
|
|
const pre = oldText.substring(0, startIndex)
|
|
|
|
const post = oldText.substring(endIndex)
|
2019-10-13 01:06:04 +00:00
|
|
|
const newText = `${pre}${text} ${post}`
|
2018-08-30 04:42:57 +00:00
|
|
|
store.setComposeData(realm, { text: newText })
|
|
|
|
store.setForAutosuggest(currentInstance, realm, { autosuggestSearchResults: [] })
|
2018-05-06 23:25:17 +00:00
|
|
|
}
|
|
|
|
|
2019-10-13 01:06:04 +00:00
|
|
|
export async function insertUsername (realm, account, startIndex, endIndex) {
|
|
|
|
await insertTextAtPosition(realm, accountMapper(account), startIndex, endIndex)
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function insertHashtag (realm, hashtag, startIndex, endIndex) {
|
|
|
|
await insertTextAtPosition(realm, hashtagMapper(hashtag), startIndex, endIndex)
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function insertEmojiAtPosition (realm, emoji, startIndex, endIndex) {
|
|
|
|
await insertTextAtPosition(realm, emojiMapper(emoji), startIndex, endIndex)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function clickSelectedItem (realm, resultMapper) {
|
2019-08-03 20:49:37 +00:00
|
|
|
const {
|
2018-05-06 23:25:17 +00:00
|
|
|
composeSelectionStart,
|
|
|
|
autosuggestSearchText,
|
|
|
|
autosuggestSelected,
|
|
|
|
autosuggestSearchResults
|
|
|
|
} = store.get()
|
2019-10-13 01:06:04 +00:00
|
|
|
const result = autosuggestSearchResults[autosuggestSelected]
|
2019-08-03 20:49:37 +00:00
|
|
|
const startIndex = composeSelectionStart - autosuggestSearchText.length
|
|
|
|
const endIndex = composeSelectionStart
|
2019-10-13 01:06:04 +00:00
|
|
|
await insertTextAtPosition(realm, resultMapper(result), startIndex, endIndex)
|
2018-05-06 23:25:17 +00:00
|
|
|
}
|
|
|
|
|
2019-10-13 01:06:04 +00:00
|
|
|
export async function clickSelectedAutosuggestionUsername (realm) {
|
|
|
|
return clickSelectedItem(realm, accountMapper)
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function clickSelectedAutosuggestionHashtag (realm) {
|
|
|
|
return clickSelectedItem(realm, hashtagMapper)
|
2018-05-06 23:25:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function clickSelectedAutosuggestionEmoji (realm) {
|
2019-10-13 01:06:04 +00:00
|
|
|
return clickSelectedItem(realm, emojiMapper)
|
2018-05-06 23:25:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function selectAutosuggestItem (item) {
|
2019-08-03 20:49:37 +00:00
|
|
|
const {
|
2018-05-06 23:25:17 +00:00
|
|
|
currentComposeRealm,
|
|
|
|
composeSelectionStart,
|
|
|
|
autosuggestSearchText
|
|
|
|
} = store.get()
|
2019-08-03 20:49:37 +00:00
|
|
|
const startIndex = composeSelectionStart - autosuggestSearchText.length
|
|
|
|
const endIndex = composeSelectionStart
|
2018-05-06 23:25:17 +00:00
|
|
|
if (item.acct) {
|
2019-10-13 01:06:04 +00:00
|
|
|
/* no await */ insertUsername(currentComposeRealm, item, startIndex, endIndex)
|
2020-06-29 06:12:14 +00:00
|
|
|
} else if (item.shortcodes) {
|
2018-05-06 23:25:17 +00:00
|
|
|
/* no await */ insertEmojiAtPosition(currentComposeRealm, item, startIndex, endIndex)
|
2019-10-13 01:06:04 +00:00
|
|
|
} else { // hashtag
|
|
|
|
/* no await */ insertHashtag(currentComposeRealm, item, startIndex, endIndex)
|
2018-05-06 23:25:17 +00:00
|
|
|
}
|
|
|
|
}
|