import Dexie from 'dexie' ; import { emoji } from 'node-emoji' ; import Action , { ThunkAction } from './type' ; import { AutoCompleteLabel } from '../components/editor/auto_complete_decorator' ; import { SuggestionItem } from '../components/editor/suggestions' ; import DB from '../database/db' ; const Promise = Dexie . Promise ; const MaxSuggestions = 5 ; function searchEmojiSuggestionItems ( query ) { if ( query . endsWith ( ':' ) ) { return Promise . resolve ( [ ] ) ; } let count = 0 ; let suggestions = [ ] as SuggestionItem [ ] ; const input = query . slice ( 1 ) ; for ( const name in emoji ) { if ( name . startsWith ( input ) ) { suggestions . push ( { code : emoji [ name ] , description : name , } ) ; count += 1 ; } if ( count > MaxSuggestions ) { return Promise . resolve ( suggestions ) ; } } return Promise . resolve ( suggestions ) ; } const RE_QUERY_END = /\s$/ ; function searchScreenNameSuggestionItems ( query ) { if ( RE_QUERY_END . test ( query ) ) { return Promise . resolve ( [ ] ) ; } const input = query . slice ( 1 ) ; return DB . accounts . getScreenNameSuggestions ( input ) ; } function hashtagTextToSuggestion ( text ) { return { description : '#' + text } as SuggestionItem ; } function searchHashtagSuggestionItems ( query ) { if ( RE_QUERY_END . test ( query ) ) { return Promise . resolve ( [ ] as SuggestionItem [ ] ) ; } const input = query . slice ( 1 ) ; if ( input . length === 0 ) { const stored = DB . hashtag_completion_history . getHashtags ( ) ; const from_history = stored . map ( hashtagTextToSuggestion ) ; if ( from_history . length >= MaxSuggestions ) { return Promise . resolve ( from_history ) ; } return DB . hashtags . getHashtagsExceptFor ( stored , MaxSuggestions - from_history . length ) . then ( hs => from_history . concat ( hs . map ( hashtagTextToSuggestion ) ) ) . catch ( ( ) => from_history ) ; } return DB . hashtags . getHashtagsStartWith ( input , MaxSuggestions ) . then ( hs => hs . map ( hashtagTextToSuggestion ) ) . catch ( ( ) => [ ] as SuggestionItem [ ] ) ; } export function searchSuggestionItems ( query , label ) { switch ( label ) { case 'EMOJI' : return searchEmojiSuggestionItems ( query ) ; case 'SCREENNAME' : return searchScreenNameSuggestionItems ( query ) ; case 'HASHTAG' : return searchHashtagSuggestionItems ( query ) ; default : log . error ( 'Unimplemented auto complete type:' , label ) ; return Promise . resolve ( [ ] ) ; } } export function updateAutoCompletion ( left , top , query , label ) { return dispatch => { searchSuggestionItems ( query , label ) . then ( suggestions => dispatch ( { type : 'UpdateAutoCompletion' , left , top , query , suggestions , completion_label : label , } ) ) . catch ( ( e ) => log . error ( 'updateAutoCompletion():' , e ) ) ; } ; } export function downAutoCompletionFocus ( ) { return { type : 'DownAutoCompletionFocus' , } ; } export function upAutoCompletionFocus ( ) { return { type : 'UpAutoCompletionFocus' , } ; } export function stopAutoCompletion ( ) { return { type : 'StopAutoCompletion' , } ; }