pinafore/src/routes/_actions/autosuggest.js

63 lines
2.3 KiB
JavaScript
Raw Normal View History

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}`
store.setComposeData(realm, { text: newText })
store.setForAutosuggest(currentInstance, realm, { autosuggestSearchResults: [] })
}
export async function clickSelectedAutosuggestionUsername (realm) {
2019-08-03 20:49:37 +00:00
const {
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
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}`
store.setComposeData(realm, { text: newText })
store.setForAutosuggest(currentInstance, realm, { autosuggestSearchResults: [] })
}
export async function clickSelectedAutosuggestionEmoji (realm) {
2019-08-03 20:49:37 +00:00
const {
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
await insertEmojiAtPosition(realm, emoji, startIndex, endIndex)
}
export function selectAutosuggestItem (item) {
2019-08-03 20:49:37 +00:00
const {
currentComposeRealm,
composeSelectionStart,
autosuggestSearchText
} = store.get()
2019-08-03 20:49:37 +00:00
const startIndex = composeSelectionStart - autosuggestSearchText.length
const endIndex = composeSelectionStart
if (item.acct) {
/* no await */ insertUsername(currentComposeRealm, item.acct, startIndex, endIndex)
} else {
/* no await */ insertEmojiAtPosition(currentComposeRealm, item, startIndex, endIndex)
}
}