2016-07-03 16:45:13 +00:00
|
|
|
import React from 'react';
|
2016-06-01 11:24:21 +00:00
|
|
|
import AutocompleteProvider from './AutocompleteProvider';
|
|
|
|
import Q from 'q';
|
|
|
|
import Fuse from 'fuse.js';
|
2016-07-03 16:45:13 +00:00
|
|
|
import {TextualCompletion} from './Components';
|
2016-06-01 11:24:21 +00:00
|
|
|
|
|
|
|
const COMMANDS = [
|
|
|
|
{
|
|
|
|
command: '/me',
|
|
|
|
args: '<message>',
|
2016-07-03 16:45:13 +00:00
|
|
|
description: 'Displays action',
|
2016-06-01 11:24:21 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
command: '/ban',
|
|
|
|
args: '<user-id> [reason]',
|
2016-07-03 16:45:13 +00:00
|
|
|
description: 'Bans user with given id',
|
2016-06-01 11:24:21 +00:00
|
|
|
},
|
|
|
|
{
|
2016-07-03 16:45:13 +00:00
|
|
|
command: '/deop',
|
|
|
|
args: '<user-id>',
|
|
|
|
description: 'Deops user with given id',
|
2016-06-01 11:24:21 +00:00
|
|
|
},
|
|
|
|
{
|
2016-07-03 16:45:13 +00:00
|
|
|
command: '/invite',
|
|
|
|
args: '<user-id>',
|
|
|
|
description: 'Invites user with given id to current room'
|
2016-06-01 11:24:21 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
command: '/join',
|
|
|
|
args: '<room-alias>',
|
2016-07-03 16:45:13 +00:00
|
|
|
description: 'Joins room with given alias',
|
2016-06-01 11:24:21 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
command: '/kick',
|
|
|
|
args: '<user-id> [reason]',
|
2016-07-03 16:45:13 +00:00
|
|
|
description: 'Kicks user with given id',
|
2016-06-01 11:24:21 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
command: '/nick',
|
|
|
|
args: '<display-name>',
|
2016-07-03 16:45:13 +00:00
|
|
|
description: 'Changes your display nickname',
|
|
|
|
},
|
2016-06-01 11:24:21 +00:00
|
|
|
];
|
|
|
|
|
2016-06-21 10:16:20 +00:00
|
|
|
let COMMAND_RE = /(^\/\w*)/g;
|
|
|
|
|
2016-06-20 08:22:55 +00:00
|
|
|
let instance = null;
|
|
|
|
|
2016-06-01 11:24:21 +00:00
|
|
|
export default class CommandProvider extends AutocompleteProvider {
|
|
|
|
constructor() {
|
2016-06-21 10:16:20 +00:00
|
|
|
super(COMMAND_RE);
|
2016-06-01 11:24:21 +00:00
|
|
|
this.fuse = new Fuse(COMMANDS, {
|
2016-07-03 16:45:13 +00:00
|
|
|
keys: ['command', 'args', 'description'],
|
2016-06-01 11:24:21 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-06-21 10:16:20 +00:00
|
|
|
getCompletions(query: string, selection: {start: number, end: number}) {
|
2016-06-01 11:24:21 +00:00
|
|
|
let completions = [];
|
2016-07-03 16:45:13 +00:00
|
|
|
let {command, range} = this.getCurrentCommand(query, selection);
|
|
|
|
if (command) {
|
2016-06-21 10:16:20 +00:00
|
|
|
completions = this.fuse.search(command[0]).map(result => {
|
2016-06-01 11:24:21 +00:00
|
|
|
return {
|
2016-07-03 16:45:13 +00:00
|
|
|
completion: result.command + ' ',
|
|
|
|
component: (<TextualCompletion
|
|
|
|
title={result.command}
|
|
|
|
subtitle={result.args}
|
|
|
|
description={result.description}
|
|
|
|
/>),
|
|
|
|
range,
|
2016-06-01 11:24:21 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return Q.when(completions);
|
|
|
|
}
|
2016-06-12 11:32:46 +00:00
|
|
|
|
|
|
|
getName() {
|
|
|
|
return 'Commands';
|
|
|
|
}
|
2016-06-20 08:22:55 +00:00
|
|
|
|
|
|
|
static getInstance(): CommandProvider {
|
2016-07-03 16:45:13 +00:00
|
|
|
if (instance == null)
|
2016-06-20 08:22:55 +00:00
|
|
|
instance = new CommandProvider();
|
|
|
|
|
|
|
|
return instance;
|
|
|
|
}
|
2016-06-01 11:24:21 +00:00
|
|
|
}
|