2018-05-06 23:25:17 +00:00
|
|
|
import { store } from '../_store/store'
|
|
|
|
|
|
|
|
export async function insertUsername (realm, username, 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)
|
|
|
|
const newText = `${pre}@${username} ${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
|
|
|
}
|
|
|
|
|
|
|
|
export async function clickSelectedAutosuggestionUsername (realm) {
|
2019-08-03 20:49:37 +00:00
|
|
|
const {
|
2018-05-06 23:25:17 +00:00
|
|
|
composeSelectionStart,
|
|
|
|
autosuggestSearchText,
|
|
|
|
autosuggestSelected,
|
|
|
|
autosuggestSearchResults
|
|
|
|
} = store.get()
|
2019-08-03 20:49:37 +00:00
|
|
|
const account = autosuggestSearchResults[autosuggestSelected]
|
|
|
|
const startIndex = composeSelectionStart - autosuggestSearchText.length
|
|
|
|
const endIndex = composeSelectionStart
|
2018-05-06 23:25:17 +00:00
|
|
|
await insertUsername(realm, account.acct, startIndex, endIndex)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function insertEmojiAtPosition (realm, emoji, 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)
|
|
|
|
const newText = `${pre}:${emoji.shortcode}: ${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
|
|
|
}
|
|
|
|
|
|
|
|
export async function clickSelectedAutosuggestionEmoji (realm) {
|
2019-08-03 20:49:37 +00:00
|
|
|
const {
|
2018-05-06 23:25:17 +00:00
|
|
|
composeSelectionStart,
|
|
|
|
autosuggestSearchText,
|
|
|
|
autosuggestSelected,
|
|
|
|
autosuggestSearchResults
|
|
|
|
} = store.get()
|
2019-08-03 20:49:37 +00:00
|
|
|
const emoji = autosuggestSearchResults[autosuggestSelected]
|
|
|
|
const startIndex = composeSelectionStart - autosuggestSearchText.length
|
|
|
|
const endIndex = composeSelectionStart
|
2018-05-06 23:25:17 +00:00
|
|
|
await insertEmojiAtPosition(realm, emoji, startIndex, endIndex)
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
/* no await */ insertUsername(currentComposeRealm, item.acct, startIndex, endIndex)
|
|
|
|
} else {
|
|
|
|
/* no await */ insertEmojiAtPosition(currentComposeRealm, item, startIndex, endIndex)
|
|
|
|
}
|
|
|
|
}
|