element-web/src/autocomplete/CommandProvider.js

80 lines
1.8 KiB
JavaScript
Raw Normal View History

2016-06-01 14:24:21 +03:00
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'
}
];
let COMMAND_RE = /(^\/\w*)/g;
let instance = null;
2016-06-01 14:24:21 +03:00
export default class CommandProvider extends AutocompleteProvider {
constructor() {
super(COMMAND_RE);
2016-06-01 14:24:21 +03:00
this.fuse = new Fuse(COMMANDS, {
keys: ['command', 'args', 'description']
});
}
getCompletions(query: string, selection: {start: number, end: number}) {
2016-06-01 14:24:21 +03:00
let completions = [];
const command = this.getCurrentCommand(query, selection);
if(command) {
completions = this.fuse.search(command[0]).map(result => {
2016-06-01 14:24:21 +03:00
return {
title: result.command,
subtitle: result.args,
description: result.description
};
});
}
return Q.when(completions);
}
getName() {
return 'Commands';
}
static getInstance(): CommandProvider {
if(instance == null)
instance = new CommandProvider();
return instance;
}
2016-06-01 14:24:21 +03:00
}