2017-06-01 17:18:06 +03:00
|
|
|
/*
|
2017-06-01 19:29:40 +03:00
|
|
|
Copyright 2016 Aviral Dasgupta
|
2017-06-01 17:18:06 +03:00
|
|
|
Copyright 2017 Vector Creations Ltd
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2016-07-03 19:45:13 +03:00
|
|
|
import React from 'react';
|
2017-05-25 13:39:08 +03:00
|
|
|
import { _t } from '../languageHandler';
|
2016-06-01 14:24:21 +03:00
|
|
|
import AutocompleteProvider from './AutocompleteProvider';
|
2016-12-01 09:36:57 +03:00
|
|
|
import FuzzyMatcher from './FuzzyMatcher';
|
2016-07-03 19:45:13 +03:00
|
|
|
import {TextualCompletion} from './Components';
|
2016-06-01 14:24:21 +03:00
|
|
|
|
2017-05-23 17:16:31 +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>',
|
2016-07-03 19:45:13 +03:00
|
|
|
description: 'Displays action',
|
2016-06-01 14:24:21 +03:00
|
|
|
},
|
2017-03-07 02:16:55 +03:00
|
|
|
{
|
|
|
|
command: '/part',
|
|
|
|
args: '[#alias:domain]',
|
|
|
|
description: 'Leave room',
|
|
|
|
},
|
2016-06-01 14:24:21 +03:00
|
|
|
{
|
|
|
|
command: '/ban',
|
|
|
|
args: '<user-id> [reason]',
|
2016-07-03 19:45:13 +03:00
|
|
|
description: 'Bans user with given id',
|
2016-06-01 14:24:21 +03:00
|
|
|
},
|
2017-03-07 02:16:55 +03:00
|
|
|
{
|
|
|
|
command: '/unban',
|
|
|
|
args: '<user-id>',
|
|
|
|
description: 'Unbans user with given id',
|
|
|
|
},
|
2016-06-01 14:24:21 +03:00
|
|
|
{
|
2016-07-03 19:45:13 +03:00
|
|
|
command: '/deop',
|
|
|
|
args: '<user-id>',
|
|
|
|
description: 'Deops user with given id',
|
2016-06-01 14:24:21 +03:00
|
|
|
},
|
|
|
|
{
|
2016-07-03 19:45:13 +03:00
|
|
|
command: '/invite',
|
|
|
|
args: '<user-id>',
|
2016-09-13 13:11:52 +03:00
|
|
|
description: 'Invites user with given id to current room',
|
2016-06-01 14:24:21 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
command: '/join',
|
|
|
|
args: '<room-alias>',
|
2016-07-03 19:45:13 +03:00
|
|
|
description: 'Joins room with given alias',
|
2016-06-01 14:24:21 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
command: '/kick',
|
|
|
|
args: '<user-id> [reason]',
|
2016-07-03 19:45:13 +03:00
|
|
|
description: 'Kicks user with given id',
|
2016-06-01 14:24:21 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
command: '/nick',
|
|
|
|
args: '<display-name>',
|
2016-07-03 19:45:13 +03:00
|
|
|
description: 'Changes your display nickname',
|
|
|
|
},
|
2016-09-13 13:11:52 +03:00
|
|
|
{
|
|
|
|
command: '/ddg',
|
|
|
|
args: '<query>',
|
|
|
|
description: 'Searches DuckDuckGo for results',
|
2017-03-07 02:16:55 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
command: '/op',
|
|
|
|
args: '<userId> [<power level>]',
|
|
|
|
description: 'Define the power level of a user',
|
2017-06-23 19:35:07 +03:00
|
|
|
},
|
2016-06-01 14:24:21 +03:00
|
|
|
];
|
|
|
|
|
2017-05-30 01:52:55 +03:00
|
|
|
const COMMAND_RE = /(^\/\w*)/g;
|
2016-06-21 13:16:20 +03:00
|
|
|
|
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-12-01 09:36:57 +03:00
|
|
|
this.matcher = new FuzzyMatcher(COMMANDS, {
|
2016-07-03 19:45:13 +03:00
|
|
|
keys: ['command', 'args', 'description'],
|
2016-06-01 14:24:21 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-09-13 13:11:52 +03:00
|
|
|
async getCompletions(query: string, selection: {start: number, end: number}) {
|
2016-06-01 14:24:21 +03:00
|
|
|
let completions = [];
|
2017-05-30 01:52:55 +03:00
|
|
|
const {command, range} = this.getCurrentCommand(query, selection);
|
2016-07-03 19:45:13 +03:00
|
|
|
if (command) {
|
2017-06-23 19:35:07 +03:00
|
|
|
completions = this.matcher.match(command[0]).map((result) => {
|
2016-06-01 14:24:21 +03:00
|
|
|
return {
|
2016-07-03 19:45:13 +03:00
|
|
|
completion: result.command + ' ',
|
|
|
|
component: (<TextualCompletion
|
|
|
|
title={result.command}
|
|
|
|
subtitle={result.args}
|
2017-05-30 01:51:05 +03:00
|
|
|
description={ _t(result.description) }
|
2016-07-03 19:45:13 +03:00
|
|
|
/>),
|
|
|
|
range,
|
2016-06-01 14:24:21 +03:00
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
2016-09-13 13:11:52 +03:00
|
|
|
return completions;
|
2016-06-01 14:24:21 +03:00
|
|
|
}
|
2016-06-12 14:32:46 +03:00
|
|
|
|
|
|
|
getName() {
|
2017-05-23 17:16:31 +03:00
|
|
|
return '*️⃣ ' + _t('Commands');
|
2016-06-12 14:32:46 +03:00
|
|
|
}
|
2016-06-20 11:22:55 +03:00
|
|
|
|
|
|
|
static getInstance(): CommandProvider {
|
2017-05-30 01:52:55 +03:00
|
|
|
if (instance === null) instance = new CommandProvider();
|
2016-06-20 11:22:55 +03:00
|
|
|
|
|
|
|
return instance;
|
|
|
|
}
|
2016-08-17 14:57:19 +03:00
|
|
|
|
|
|
|
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-08-17 14:57:19 +03:00
|
|
|
}
|
2016-06-01 14:24:21 +03:00
|
|
|
}
|