element-web/src/autocomplete/CommandProvider.js

99 lines
2.7 KiB
JavaScript
Raw Normal View History

import React from 'react';
import _t from 'counterpart-riot';
2016-06-01 14:24:21 +03:00
import AutocompleteProvider from './AutocompleteProvider';
import Fuse from 'fuse.js';
import {TextualCompletion} from './Components';
2016-06-01 14:24:21 +03:00
// Warning: Since the description string will be translated in _t(result.description), all these strings below must be in i18n/strings/en_EN.json file
2016-06-01 14:24:21 +03:00
const COMMANDS = [
{
command: '/me',
args: '<message>',
description: 'Displays action',
2016-06-01 14:24:21 +03:00
},
{
command: '/ban',
args: '<user-id> [reason]',
description: 'Bans user with given id',
2016-06-01 14:24:21 +03:00
},
{
command: '/deop',
args: '<user-id>',
description: 'Deops user with given id',
2016-06-01 14:24:21 +03:00
},
{
command: '/invite',
args: '<user-id>',
description: 'Invites user with given id to current room',
2016-06-01 14:24:21 +03:00
},
{
command: '/join',
args: '<room-alias>',
description: 'Joins room with given alias',
2016-06-01 14:24:21 +03:00
},
{
command: '/kick',
args: '<user-id> [reason]',
description: 'Kicks user with given id',
2016-06-01 14:24:21 +03:00
},
{
command: '/nick',
args: '<display-name>',
description: 'Changes your display nickname',
},
{
command: '/ddg',
args: '<query>',
description: 'Searches DuckDuckGo for results',
}
2016-06-01 14:24:21 +03:00
];
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'],
2016-06-01 14:24:21 +03:00
});
}
async getCompletions(query: string, selection: {start: number, end: number}) {
2016-06-01 14:24:21 +03:00
let completions = [];
let {command, range} = this.getCurrentCommand(query, selection);
if (command) {
completions = this.fuse.search(command[0]).map(result => {
2016-06-01 14:24:21 +03:00
return {
completion: result.command + ' ',
component: (<TextualCompletion
title={result.command}
subtitle={result.args}
description={ t_(result.description) }
/>),
range,
2016-06-01 14:24:21 +03:00
};
});
}
return completions;
2016-06-01 14:24:21 +03:00
}
getName() {
return '*️⃣ ' + _t('Commands');
}
static getInstance(): CommandProvider {
if (instance == null)
{instance = new CommandProvider();}
return instance;
}
renderCompletions(completions: [React.Component]): ?React.Component {
2016-08-22 22:06:31 +03:00
return <div className="mx_Autocomplete_Completion_container_block">
{completions}
</div>;
}
2016-06-01 14:24:21 +03:00
}