2017-06-01 14:18:06 +00:00
|
|
|
/*
|
2017-06-01 16:29:40 +00:00
|
|
|
Copyright 2016 Aviral Dasgupta
|
2017-06-01 14:18:06 +00:00
|
|
|
Copyright 2017 Vector Creations Ltd
|
2017-11-02 18:01:28 +00:00
|
|
|
Copyright 2017 New Vector Ltd
|
2017-06-01 14:18:06 +00:00
|
|
|
|
|
|
|
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 16:45:13 +00:00
|
|
|
import React from 'react';
|
2017-09-22 19:43:27 +00:00
|
|
|
import { _t, _td } from '../languageHandler';
|
2016-06-01 11:24:21 +00:00
|
|
|
import AutocompleteProvider from './AutocompleteProvider';
|
2016-12-01 06:36:57 +00:00
|
|
|
import FuzzyMatcher from './FuzzyMatcher';
|
2016-07-03 16:45:13 +00:00
|
|
|
import {TextualCompletion} from './Components';
|
2018-05-13 02:04:40 +00:00
|
|
|
import type {SelectionRange} from './Autocompleter';
|
2016-06-01 11:24:21 +00:00
|
|
|
|
2017-07-01 11:55:43 +00:00
|
|
|
// TODO merge this with the factory mechanics of SlashCommands?
|
2017-05-23 14:16:31 +00: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 11:24:21 +00:00
|
|
|
const COMMANDS = [
|
|
|
|
{
|
|
|
|
command: '/me',
|
|
|
|
args: '<message>',
|
2017-09-22 19:43:27 +00:00
|
|
|
description: _td('Displays action'),
|
2016-06-01 11:24:21 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
command: '/ban',
|
|
|
|
args: '<user-id> [reason]',
|
2017-09-22 19:43:27 +00:00
|
|
|
description: _td('Bans user with given id'),
|
2016-06-01 11:24:21 +00:00
|
|
|
},
|
2017-07-01 11:55:43 +00:00
|
|
|
{
|
|
|
|
command: '/unban',
|
2017-03-06 23:16:55 +00:00
|
|
|
args: '<user-id>',
|
2017-09-22 19:43:27 +00:00
|
|
|
description: _td('Unbans user with given id'),
|
2017-07-01 11:55:43 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
command: '/op',
|
2017-07-03 18:24:18 +00:00
|
|
|
args: '<user-id> [<power-level>]',
|
2017-09-22 19:43:27 +00:00
|
|
|
description: _td('Define the power level of a user'),
|
2017-07-01 11:55:43 +00:00
|
|
|
},
|
2016-06-01 11:24:21 +00:00
|
|
|
{
|
2016-07-03 16:45:13 +00:00
|
|
|
command: '/deop',
|
|
|
|
args: '<user-id>',
|
2017-09-22 19:43:27 +00:00
|
|
|
description: _td('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>',
|
2017-09-22 19:43:27 +00:00
|
|
|
description: _td('Invites user with given id to current room'),
|
2016-06-01 11:24:21 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
command: '/join',
|
|
|
|
args: '<room-alias>',
|
2017-09-22 19:43:27 +00:00
|
|
|
description: _td('Joins room with given alias'),
|
2016-06-01 11:24:21 +00:00
|
|
|
},
|
2017-07-01 11:55:43 +00:00
|
|
|
{
|
|
|
|
command: '/part',
|
2017-07-01 12:07:18 +00:00
|
|
|
args: '[<room-alias>]',
|
2017-09-22 19:43:27 +00:00
|
|
|
description: _td('Leave room'),
|
2017-07-01 11:55:43 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
command: '/topic',
|
|
|
|
args: '<topic>',
|
2017-09-22 19:43:27 +00:00
|
|
|
description: _td('Sets the room topic'),
|
2017-07-01 11:55:43 +00:00
|
|
|
},
|
2016-06-01 11:24:21 +00:00
|
|
|
{
|
|
|
|
command: '/kick',
|
|
|
|
args: '<user-id> [reason]',
|
2017-09-22 19:43:27 +00:00
|
|
|
description: _td('Kicks user with given id'),
|
2016-06-01 11:24:21 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
command: '/nick',
|
|
|
|
args: '<display-name>',
|
2017-09-22 19:43:27 +00:00
|
|
|
description: _td('Changes your display nickname'),
|
2016-07-03 16:45:13 +00:00
|
|
|
},
|
2016-09-13 10:11:52 +00:00
|
|
|
{
|
|
|
|
command: '/ddg',
|
|
|
|
args: '<query>',
|
2017-09-22 19:43:27 +00:00
|
|
|
description: _td('Searches DuckDuckGo for results'),
|
2017-05-29 22:52:55 +00:00
|
|
|
},
|
2017-07-01 11:55:43 +00:00
|
|
|
{
|
|
|
|
command: '/tint',
|
|
|
|
args: '<color1> [<color2>]',
|
2017-09-22 19:43:27 +00:00
|
|
|
description: _td('Changes colour scheme of current room'),
|
2017-07-01 11:55:43 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
command: '/verify',
|
2017-07-03 18:24:18 +00:00
|
|
|
args: '<user-id> <device-id> <device-signing-key>',
|
2017-09-22 19:43:27 +00:00
|
|
|
description: _td('Verifies a user, device, and pubkey tuple'),
|
2017-07-01 11:55:43 +00:00
|
|
|
},
|
2017-09-15 02:30:40 +00:00
|
|
|
{
|
|
|
|
command: '/ignore',
|
|
|
|
args: '<user-id>',
|
2017-09-22 19:43:27 +00:00
|
|
|
description: _td('Ignores a user, hiding their messages from you'),
|
2017-09-15 02:30:40 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
command: '/unignore',
|
|
|
|
args: '<user-id>',
|
2017-09-22 19:43:27 +00:00
|
|
|
description: _td('Stops ignoring a user, showing their messages going forward'),
|
2017-09-15 02:30:40 +00:00
|
|
|
},
|
2018-02-28 00:57:14 +00:00
|
|
|
{
|
|
|
|
command: '/devtools',
|
|
|
|
args: '',
|
2018-03-21 11:00:07 +00:00
|
|
|
description: _td('Opens the Developer Tools dialog'),
|
2018-02-28 00:57:14 +00:00
|
|
|
},
|
2017-07-01 11:55:43 +00:00
|
|
|
// Omitting `/markdown` as it only seems to apply to OldComposer
|
2016-06-01 11:24:21 +00:00
|
|
|
];
|
|
|
|
|
2017-05-29 22:52:55 +00:00
|
|
|
const COMMAND_RE = /(^\/\w*)/g;
|
2016-06-21 10:16:20 +00:00
|
|
|
|
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-12-01 06:36:57 +00:00
|
|
|
this.matcher = new FuzzyMatcher(COMMANDS, {
|
2016-07-03 16:45:13 +00:00
|
|
|
keys: ['command', 'args', 'description'],
|
2016-06-01 11:24:21 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-05-13 02:04:40 +00:00
|
|
|
async getCompletions(query: string, selection: SelectionRange) {
|
2016-06-01 11:24:21 +00:00
|
|
|
let completions = [];
|
2018-05-13 02:04:40 +00:00
|
|
|
if (!selection.beginning) return completions;
|
2017-05-29 22:52:55 +00:00
|
|
|
const {command, range} = this.getCurrentCommand(query, selection);
|
2016-07-03 16:45:13 +00:00
|
|
|
if (command) {
|
2018-05-13 02:18:41 +00:00
|
|
|
let results;
|
|
|
|
if (command[0] == '/') {
|
|
|
|
results = COMMANDS;
|
2018-05-20 22:43:42 +00:00
|
|
|
} else {
|
2018-05-13 02:18:41 +00:00
|
|
|
results = this.matcher.match(command[0]);
|
|
|
|
}
|
|
|
|
completions = results.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}
|
2017-09-28 10:21:06 +00:00
|
|
|
description={_t(result.description)}
|
2016-07-03 16:45:13 +00:00
|
|
|
/>),
|
|
|
|
range,
|
2016-06-01 11:24:21 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
2016-09-13 10:11:52 +00:00
|
|
|
return completions;
|
2016-06-01 11:24:21 +00:00
|
|
|
}
|
2016-06-12 11:32:46 +00:00
|
|
|
|
|
|
|
getName() {
|
2017-05-23 14:16:31 +00:00
|
|
|
return '*️⃣ ' + _t('Commands');
|
2016-06-12 11:32:46 +00:00
|
|
|
}
|
2016-06-20 08:22:55 +00:00
|
|
|
|
2016-08-17 11:57:19 +00:00
|
|
|
renderCompletions(completions: [React.Component]): ?React.Component {
|
2016-08-22 19:06:31 +00:00
|
|
|
return <div className="mx_Autocomplete_Completion_container_block">
|
2017-09-28 10:21:06 +00:00
|
|
|
{ completions }
|
2016-08-22 19:06:31 +00:00
|
|
|
</div>;
|
2016-08-17 11:57:19 +00:00
|
|
|
}
|
2016-06-01 11:24:21 +00:00
|
|
|
}
|