initial version of autocomplete

This commit is contained in:
Aviral Dasgupta 2016-06-01 16:54:21 +05:30
parent a145ab7e28
commit b979a16199
7 changed files with 164 additions and 1 deletions

View file

@ -0,0 +1,3 @@
export default class AutocompleteProvider {
}

View file

@ -0,0 +1,7 @@
import CommandProvider from './CommandProvider';
const COMPLETERS = [CommandProvider].map(completer => new completer());
export function getCompletions(query: String) {
return COMPLETERS.map(completer => completer.getCompletions(query));
}

View file

@ -0,0 +1,65 @@
import AutocompleteProvider from './AutocompleteProvider';
import Q from 'q';
import Fuse from 'fuse.js';
const COMMANDS = [
{
command: '/me',
args: '<message>',
description: 'Displays action'
},
{
command: '/ban',
args: '<user-id> [reason]',
description: 'Bans user with given id'
},
{
command: '/deop'
},
{
command: '/encrypt'
},
{
command: '/invite'
},
{
command: '/join',
args: '<room-alias>',
description: 'Joins room with given alias'
},
{
command: '/kick',
args: '<user-id> [reason]',
description: 'Kicks user with given id'
},
{
command: '/nick',
args: '<display-name>',
description: 'Changes your display nickname'
}
];
export default class CommandProvider extends AutocompleteProvider {
constructor() {
super();
this.fuse = new Fuse(COMMANDS, {
keys: ['command', 'args', 'description']
});
}
getCompletions(query: String) {
let completions = [];
const matches = query.match(/(^\/\w+)/);
if(!!matches) {
const command = matches[0];
completions = this.fuse.search(command).map(result => {
return {
title: result.command,
subtitle: result.args,
description: result.description
};
});
}
return Q.when(completions);
}
}