pinafore/routes/_actions/emoji.js

53 lines
1.9 KiB
JavaScript
Raw Normal View History

2018-02-28 08:18:07 +01:00
import { cacheFirstUpdateAfter } from '../_utils/sync'
import {
getCustomEmoji as getCustomEmojiFromDatabase,
setCustomEmoji as setCustomEmojiInDatabase
} from '../_database/meta'
2018-02-28 08:18:07 +01:00
import { getCustomEmoji } from '../_api/emoji'
import { store } from '../_store/store'
export async function updateCustomEmojiForInstance (instanceName) {
await cacheFirstUpdateAfter(
() => getCustomEmoji(instanceName),
() => getCustomEmojiFromDatabase(instanceName),
emoji => setCustomEmojiInDatabase(instanceName, emoji),
2018-02-28 08:18:07 +01:00
emoji => {
let { customEmoji } = store.get()
2018-02-28 08:18:07 +01:00
customEmoji[instanceName] = emoji
store.set({customEmoji: customEmoji})
}
)
}
2018-03-03 23:51:48 +01:00
export function insertEmoji (realm, emoji) {
let { composeSelectionStart } = store.get()
let idx = composeSelectionStart || 0
let oldText = store.getComposeData(realm, 'text') || ''
let pre = oldText.substring(0, idx)
let post = oldText.substring(idx)
2018-03-01 03:45:29 +01:00
let newText = `${pre}:${emoji.shortcode}: ${post}`
2018-03-03 23:51:48 +01:00
store.setComposeData(realm, {text: newText})
2018-02-28 08:18:07 +01:00
}
2018-03-25 21:24:38 +02:00
export function insertEmojiAtPosition (realm, emoji, startIndex, endIndex) {
let oldText = store.getComposeData(realm, 'text') || ''
let pre = oldText.substring(0, startIndex)
let post = oldText.substring(endIndex)
2018-03-25 21:24:38 +02:00
let newText = `${pre}:${emoji.shortcode}: ${post}`
store.setComposeData(realm, {text: newText})
}
export async function clickSelectedAutosuggestionEmoji (realm) {
let {
composeSelectionStart,
composeAutosuggestionSearchText,
composeAutosuggestionSelected,
composeAutosuggestionSearchResults
} = store.get()
composeAutosuggestionSelected = composeAutosuggestionSelected || 0
let emoji = composeAutosuggestionSearchResults[composeAutosuggestionSelected]
let startIndex = composeSelectionStart - composeAutosuggestionSearchText.length
let endIndex = composeSelectionStart
2018-03-25 21:24:38 +02:00
await insertEmojiAtPosition(realm, emoji, startIndex, endIndex)
}