initial version of autocomplete
This commit is contained in:
parent
a145ab7e28
commit
b979a16199
7 changed files with 164 additions and 1 deletions
3
src/autocomplete/AutocompleteProvider.js
Normal file
3
src/autocomplete/AutocompleteProvider.js
Normal file
|
@ -0,0 +1,3 @@
|
|||
export default class AutocompleteProvider {
|
||||
|
||||
}
|
7
src/autocomplete/Autocompleter.js
Normal file
7
src/autocomplete/Autocompleter.js
Normal 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));
|
||||
}
|
65
src/autocomplete/CommandProvider.js
Normal file
65
src/autocomplete/CommandProvider.js
Normal 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);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue