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'
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2016-06-21 13:16:20 +03:00
|
|
|
let COMMAND_RE = /(^\/\w*)/g;
|
|
|
|
|
2016-06-20 11:22:55 +03:00
|
|
|
let instance = null;
|
|
|
|
|
2016-06-01 14:24:21 +03:00
|
|
|
export default class CommandProvider extends AutocompleteProvider {
|
|
|
|
constructor() {
|
2016-06-21 13:16:20 +03:00
|
|
|
super(COMMAND_RE);
|
2016-06-01 14:24:21 +03:00
|
|
|
this.fuse = new Fuse(COMMANDS, {
|
|
|
|
keys: ['command', 'args', 'description']
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-06-21 13:16:20 +03:00
|
|
|
getCompletions(query: string, selection: {start: number, end: number}) {
|
2016-06-01 14:24:21 +03:00
|
|
|
let completions = [];
|
2016-06-21 13:16:20 +03:00
|
|
|
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);
|
|
|
|
}
|
2016-06-12 14:32:46 +03:00
|
|
|
|
|
|
|
getName() {
|
|
|
|
return 'Commands';
|
|
|
|
}
|
2016-06-20 11:22:55 +03:00
|
|
|
|
|
|
|
static getInstance(): CommandProvider {
|
|
|
|
if(instance == null)
|
|
|
|
instance = new CommandProvider();
|
|
|
|
|
|
|
|
return instance;
|
|
|
|
}
|
2016-06-01 14:24:21 +03:00
|
|
|
}
|