Files
ThreadNet-Web/src/autocomplete/CommandProvider.js
T

97 lines
2.5 KiB
JavaScript
Raw Normal View History

2016-07-03 22:15:13 +05:30
import React from 'react';
2016-06-01 16:54:21 +05:30
import AutocompleteProvider from './AutocompleteProvider';
import Fuse from 'fuse.js';
2016-07-03 22:15:13 +05:30
import {TextualCompletion} from './Components';
2016-06-01 16:54:21 +05:30
const COMMANDS = [
{
command: '/me',
args: '<message>',
2016-07-03 22:15:13 +05:30
description: 'Displays action',
2016-06-01 16:54:21 +05:30
},
{
command: '/ban',
args: '<user-id> [reason]',
2016-07-03 22:15:13 +05:30
description: 'Bans user with given id',
2016-06-01 16:54:21 +05:30
},
{
2016-07-03 22:15:13 +05:30
command: '/deop',
args: '<user-id>',
description: 'Deops user with given id',
2016-06-01 16:54:21 +05:30
},
{
2016-07-03 22:15:13 +05:30
command: '/invite',
args: '<user-id>',
2016-09-13 15:41:52 +05:30
description: 'Invites user with given id to current room',
2016-06-01 16:54:21 +05:30
},
{
command: '/join',
args: '<room-alias>',
2016-07-03 22:15:13 +05:30
description: 'Joins room with given alias',
2016-06-01 16:54:21 +05:30
},
{
command: '/kick',
args: '<user-id> [reason]',
2016-07-03 22:15:13 +05:30
description: 'Kicks user with given id',
2016-06-01 16:54:21 +05:30
},
{
command: '/nick',
args: '<display-name>',
2016-07-03 22:15:13 +05:30
description: 'Changes your display nickname',
},
2016-09-13 15:41:52 +05:30
{
command: '/ddg',
args: '<query>',
description: 'Searches DuckDuckGo for results',
}
2016-06-01 16:54:21 +05:30
];
let COMMAND_RE = /(^\/\w*)/g;
let instance = null;
2016-06-01 16:54:21 +05:30
export default class CommandProvider extends AutocompleteProvider {
constructor() {
super(COMMAND_RE);
2016-06-01 16:54:21 +05:30
this.fuse = new Fuse(COMMANDS, {
2016-07-03 22:15:13 +05:30
keys: ['command', 'args', 'description'],
2016-06-01 16:54:21 +05:30
});
}
2016-09-13 15:41:52 +05:30
async getCompletions(query: string, selection: {start: number, end: number}) {
2016-06-01 16:54:21 +05:30
let completions = [];
2016-07-03 22:15:13 +05:30
let {command, range} = this.getCurrentCommand(query, selection);
if (command) {
completions = this.fuse.search(command[0]).map(result => {
2016-06-01 16:54:21 +05:30
return {
2016-07-03 22:15:13 +05:30
completion: result.command + ' ',
component: (<TextualCompletion
title={result.command}
subtitle={result.args}
description={result.description}
/>),
range,
2016-06-01 16:54:21 +05:30
};
});
}
2016-09-13 15:41:52 +05:30
return completions;
2016-06-01 16:54:21 +05:30
}
getName() {
return '*️⃣ Commands';
}
static getInstance(): CommandProvider {
2016-07-03 22:15:13 +05:30
if (instance == null)
2017-01-20 14:22:27 +00:00
{instance = new CommandProvider();}
return instance;
}
renderCompletions(completions: [React.Component]): ?React.Component {
2016-08-23 00:36:31 +05:30
return <div className="mx_Autocomplete_Completion_container_block">
{completions}
</div>;
}
2016-06-01 16:54:21 +05:30
}