element-web/src/autocomplete/CommandProvider.js

157 lines
4.5 KiB
JavaScript
Raw Normal View History

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
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.
*/
import React from 'react';
import { _t } from '../languageHandler';
2016-06-01 11:24:21 +00:00
import AutocompleteProvider from './AutocompleteProvider';
import FuzzyMatcher from './FuzzyMatcher';
import {TextualCompletion} from './Components';
2016-06-01 11:24:21 +00:00
// TODO merge this with the factory mechanics of SlashCommands?
// 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>',
description: 'Displays action',
2016-06-01 11:24:21 +00:00
},
{
command: '/ban',
args: '<user-id> [reason]',
description: 'Bans user with given id',
2016-06-01 11:24:21 +00:00
},
{
command: '/unban',
args: '<user-id>',
description: 'Unbans user with given id',
},
{
command: '/op',
args: '<user-id> [<power-level>]',
description: 'Define the power level of a user',
},
2016-06-01 11:24:21 +00:00
{
command: '/deop',
args: '<user-id>',
description: 'Deops user with given id',
2016-06-01 11:24:21 +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>',
description: 'Joins room with given alias',
2016-06-01 11:24:21 +00:00
},
{
command: '/part',
args: '[<room-alias>]',
description: 'Leave room',
},
{
command: '/topic',
args: '<topic>',
description: 'Sets the room topic',
},
2016-06-01 11:24:21 +00:00
{
command: '/kick',
args: '<user-id> [reason]',
description: 'Kicks user with given id',
2016-06-01 11:24:21 +00:00
},
{
command: '/nick',
args: '<display-name>',
description: 'Changes your display nickname',
},
{
command: '/ddg',
args: '<query>',
description: 'Searches DuckDuckGo for results',
},
{
command: '/tint',
args: '<color1> [<color2>]',
description: 'Changes colour scheme of current room',
},
{
command: '/verify',
args: '<user-id> <device-id> <device-signing-key>',
description: 'Verifies a user, device, and pubkey tuple',
},
{
command: '/ignore',
args: '<user-id>',
description: 'Ignores a user, hiding their messages from you',
},
{
command: '/unignore',
args: '<user-id>',
description: 'Stops ignoring a user, showing their messages going forward',
},
// Omitting `/markdown` as it only seems to apply to OldComposer
2016-06-01 11:24:21 +00:00
];
const COMMAND_RE = /(^\/\w*)/g;
let instance = null;
2016-06-01 11:24:21 +00:00
export default class CommandProvider extends AutocompleteProvider {
constructor() {
super(COMMAND_RE);
this.matcher = new FuzzyMatcher(COMMANDS, {
keys: ['command', 'args', 'description'],
2016-06-01 11:24:21 +00:00
});
}
async getCompletions(query: string, selection: {start: number, end: number}) {
2016-06-01 11:24:21 +00:00
let completions = [];
const {command, range} = this.getCurrentCommand(query, selection);
if (command) {
2017-06-23 16:35:07 +00:00
completions = this.matcher.match(command[0]).map((result) => {
2016-06-01 11:24:21 +00:00
return {
completion: result.command + ' ',
component: (<TextualCompletion
title={result.command}
subtitle={result.args}
description={ _t(result.description) }
/>),
range,
2016-06-01 11:24:21 +00:00
};
});
}
return completions;
2016-06-01 11:24:21 +00: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 19:06:31 +00:00
return <div className="mx_Autocomplete_Completion_container_block">
{completions}
</div>;
}
2016-06-01 11:24:21 +00:00
}