2018-05-02 00:05:36 +00:00
|
|
|
<div class="compose-autosuggest {shown ? 'shown' : ''} {realm === 'dialog' ? 'is-dialog' : ''}"
|
2018-04-06 04:35:22 +00:00
|
|
|
aria-hidden="true" >
|
2018-03-25 01:04:54 +00:00
|
|
|
<ComposeAutosuggestionList
|
2018-05-02 00:05:36 +00:00
|
|
|
items={searchResults}
|
2018-03-25 19:24:38 +00:00
|
|
|
on:click="onClick(event)"
|
2018-05-02 00:05:36 +00:00
|
|
|
{type}
|
|
|
|
{selected}
|
2018-03-25 01:04:54 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<style>
|
|
|
|
.compose-autosuggest {
|
2018-04-06 04:35:22 +00:00
|
|
|
position: absolute;
|
|
|
|
left: 5px;
|
|
|
|
top: 0;
|
2018-03-25 01:04:54 +00:00
|
|
|
pointer-events: none;
|
2018-04-06 04:35:22 +00:00
|
|
|
opacity: 0;
|
2018-03-25 01:04:54 +00:00
|
|
|
transition: opacity 0.1s linear;
|
|
|
|
min-width: 400px;
|
|
|
|
max-width: calc(100vw - 20px);
|
2018-04-06 04:35:22 +00:00
|
|
|
z-index: 7000;
|
|
|
|
}
|
|
|
|
.compose-autosuggest.is-dialog {
|
|
|
|
z-index: 11000;
|
2018-03-25 01:04:54 +00:00
|
|
|
}
|
|
|
|
.compose-autosuggest.shown {
|
|
|
|
pointer-events: auto;
|
2018-04-06 04:35:22 +00:00
|
|
|
opacity: 1;
|
2018-03-25 01:04:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@media (max-width: 479px) {
|
|
|
|
.compose-autosuggest {
|
|
|
|
/* hack: move this over to the left on mobile so it's easier to see */
|
|
|
|
transform: translateX(-58px); /* avatar size 48px + 10px padding */
|
2018-04-06 04:35:22 +00:00
|
|
|
min-width: 0;
|
2018-03-25 01:04:54 +00:00
|
|
|
width: calc(100vw - 20px);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
</style>
|
|
|
|
<script>
|
|
|
|
import { store } from '../../_store/store'
|
|
|
|
import { insertUsername } from '../../_actions/compose'
|
2018-03-25 19:24:38 +00:00
|
|
|
import { insertEmojiAtPosition } from '../../_actions/emoji'
|
2018-03-25 01:04:54 +00:00
|
|
|
import { scheduleIdleTask } from '../../_utils/scheduleIdleTask'
|
|
|
|
import { once } from '../../_utils/once'
|
|
|
|
import ComposeAutosuggestionList from './ComposeAutosuggestionList.html'
|
2018-04-17 03:56:21 +00:00
|
|
|
import {
|
|
|
|
searchAccountsByUsername as searchAccountsByUsernameInDatabase
|
|
|
|
} from '../../_database/accountsAndRelationships'
|
2018-04-30 15:29:04 +00:00
|
|
|
import { observe } from 'svelte-extras'
|
2018-03-25 01:04:54 +00:00
|
|
|
|
|
|
|
const SEARCH_RESULTS_LIMIT = 4
|
|
|
|
const DATABASE_SEARCH_RESULTS_LIMIT = 30
|
|
|
|
const MIN_PREFIX_LENGTH = 1
|
2018-03-25 19:24:38 +00:00
|
|
|
const ACCOUNT_SEARCH_REGEX = new RegExp(`(?:\\s|^)(@\\S{${MIN_PREFIX_LENGTH},})$`)
|
|
|
|
const EMOJI_SEARCH_REGEX = new RegExp(`(?:\\s|^)(:[^:]{${MIN_PREFIX_LENGTH},})$`)
|
2018-03-25 01:04:54 +00:00
|
|
|
|
|
|
|
export default {
|
2018-04-20 04:38:01 +00:00
|
|
|
oncreate () {
|
2018-03-25 01:04:54 +00:00
|
|
|
// perf improves for input responsiveness
|
|
|
|
this.observe('composeSelectionStart', () => {
|
|
|
|
scheduleIdleTask(() => {
|
2018-05-05 03:09:20 +00:00
|
|
|
let { composeSelectionStart } = this.get()
|
2018-04-19 16:37:05 +00:00
|
|
|
this.set({composeSelectionStartDeferred: composeSelectionStart})
|
2018-03-25 01:04:54 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
this.observe('composeFocused', (composeFocused) => {
|
|
|
|
let updateFocusedState = () => {
|
|
|
|
scheduleIdleTask(() => {
|
2018-05-05 03:09:20 +00:00
|
|
|
let { composeFocused } = this.get()
|
2018-04-19 16:37:05 +00:00
|
|
|
this.set({composeFocusedDeferred: composeFocused})
|
2018-03-25 01:04:54 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: hack so that when the user clicks the button, and the textarea blurs,
|
|
|
|
// we don't immediately hide the dropdown which would cause the click to get lost
|
|
|
|
if (composeFocused) {
|
|
|
|
updateFocusedState()
|
|
|
|
} else {
|
|
|
|
Promise.race([
|
|
|
|
new Promise(resolve => setTimeout(resolve, 200)),
|
2018-03-25 19:24:38 +00:00
|
|
|
new Promise(resolve => this.once('autosuggestItemSelected', resolve))
|
2018-03-25 01:04:54 +00:00
|
|
|
]).then(updateFocusedState)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
this.observe('searchText', async searchText => {
|
2018-04-21 21:57:02 +00:00
|
|
|
let { thisComposeFocused } = this.get()
|
|
|
|
if (!thisComposeFocused || !searchText) {
|
2018-03-25 01:04:54 +00:00
|
|
|
return
|
|
|
|
}
|
2018-03-25 19:24:38 +00:00
|
|
|
let type = searchText.startsWith('@') ? 'account' : 'emoji'
|
|
|
|
let results = (type === 'account')
|
|
|
|
? await this.searchAccounts(searchText)
|
|
|
|
: await this.searchEmoji(searchText)
|
2018-03-25 01:04:54 +00:00
|
|
|
this.store.set({
|
|
|
|
composeAutosuggestionSelected: 0,
|
|
|
|
composeAutosuggestionSearchText: searchText,
|
2018-03-25 19:24:38 +00:00
|
|
|
composeAutosuggestionSearchResults: results,
|
2018-04-20 04:38:01 +00:00
|
|
|
composeAutosuggestionType: type
|
2018-03-25 01:04:54 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
this.observe('shown', shown => {
|
2018-04-21 21:57:02 +00:00
|
|
|
let { thisComposeFocused } = this.get()
|
|
|
|
if (!thisComposeFocused) {
|
|
|
|
return
|
|
|
|
}
|
2018-03-25 01:04:54 +00:00
|
|
|
this.store.set({composeAutosuggestionShown: shown})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
methods: {
|
2018-04-30 15:29:04 +00:00
|
|
|
observe,
|
2018-04-30 05:13:41 +00:00
|
|
|
once,
|
2018-04-20 04:38:01 +00:00
|
|
|
onClick (item) {
|
2018-03-25 19:24:38 +00:00
|
|
|
this.fire('autosuggestItemSelected')
|
2018-04-19 16:37:05 +00:00
|
|
|
let { realm } = this.get()
|
|
|
|
let { composeSelectionStart, composeAutosuggestionSearchText } = this.store.get()
|
|
|
|
let startIndex = composeSelectionStart - composeAutosuggestionSearchText.length
|
|
|
|
let endIndex = composeSelectionStart
|
2018-03-25 19:24:38 +00:00
|
|
|
if (item.acct) {
|
|
|
|
/* no await */ insertUsername(realm, item.acct, startIndex, endIndex)
|
|
|
|
} else {
|
|
|
|
/* no await */ insertEmojiAtPosition(realm, item, startIndex, endIndex)
|
|
|
|
}
|
2018-03-25 01:04:54 +00:00
|
|
|
},
|
2018-04-20 04:38:01 +00:00
|
|
|
async searchAccounts (searchText) {
|
2018-03-25 19:24:38 +00:00
|
|
|
searchText = searchText.substring(1)
|
2018-04-19 16:37:05 +00:00
|
|
|
let { currentInstance } = this.store.get()
|
2018-04-17 03:56:21 +00:00
|
|
|
let results = await searchAccountsByUsernameInDatabase(
|
2018-03-25 19:24:38 +00:00
|
|
|
currentInstance, searchText, DATABASE_SEARCH_RESULTS_LIMIT)
|
2018-03-25 01:04:54 +00:00
|
|
|
return results.slice(0, SEARCH_RESULTS_LIMIT)
|
2018-03-25 19:24:38 +00:00
|
|
|
},
|
2018-04-20 04:38:01 +00:00
|
|
|
searchEmoji (searchText) {
|
2018-03-25 19:24:38 +00:00
|
|
|
searchText = searchText.toLowerCase().substring(1)
|
2018-04-19 16:37:05 +00:00
|
|
|
let { currentCustomEmoji } = this.store.get()
|
|
|
|
let results = currentCustomEmoji.filter(emoji => emoji.shortcode.toLowerCase().startsWith(searchText))
|
2018-03-25 19:24:38 +00:00
|
|
|
.sort((a, b) => a.shortcode.toLowerCase() < b.shortcode.toLowerCase() ? -1 : 1)
|
|
|
|
.slice(0, SEARCH_RESULTS_LIMIT)
|
|
|
|
return results
|
2018-03-25 01:04:54 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
2018-05-02 00:05:36 +00:00
|
|
|
composeSelectionStart: ({ $composeSelectionStart }) => $composeSelectionStart,
|
|
|
|
composeFocused: ({ $composeFocused }) => $composeFocused,
|
|
|
|
thisComposeFocused: ({ composeFocusedDeferred, realm }) => composeFocusedDeferred === realm,
|
|
|
|
searchResults: ({ $composeAutosuggestionSearchResults }) => $composeAutosuggestionSearchResults || [],
|
|
|
|
type: ({ $composeAutosuggestionType }) => $composeAutosuggestionType || 'account',
|
|
|
|
selected: ({ $composeAutosuggestionSelected }) => $composeAutosuggestionSelected || 0,
|
|
|
|
searchText: ({ text, composeSelectionStartDeferred, thisComposeFocused }) => {
|
2018-04-21 21:57:02 +00:00
|
|
|
if (!thisComposeFocused) {
|
|
|
|
return
|
|
|
|
}
|
2018-04-30 05:13:41 +00:00
|
|
|
let selectionStart = composeSelectionStartDeferred
|
2018-03-25 01:04:54 +00:00
|
|
|
if (!text || selectionStart < MIN_PREFIX_LENGTH) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-03-25 19:24:38 +00:00
|
|
|
let textUpToCursor = text.substring(0, selectionStart)
|
|
|
|
let match = textUpToCursor.match(ACCOUNT_SEARCH_REGEX) || textUpToCursor.match(EMOJI_SEARCH_REGEX)
|
2018-03-25 01:04:54 +00:00
|
|
|
return match && match[1]
|
|
|
|
},
|
2018-05-02 00:05:36 +00:00
|
|
|
shown: ({ thisComposeFocused, searchText, searchResults }) => {
|
2018-04-20 13:26:36 +00:00
|
|
|
return !!(thisComposeFocused &&
|
2018-03-25 01:04:54 +00:00
|
|
|
searchText &&
|
|
|
|
searchResults.length)
|
|
|
|
}
|
|
|
|
},
|
2018-04-30 05:13:41 +00:00
|
|
|
data: () => ({
|
|
|
|
composeFocusedDeferred: void 0,
|
|
|
|
composeSelectionStartDeferred: 0
|
|
|
|
}),
|
2018-03-25 01:04:54 +00:00
|
|
|
store: () => store,
|
|
|
|
components: {
|
|
|
|
ComposeAutosuggestionList
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|