mirror of
https://github.com/element-hq/element-web
synced 2024-11-27 19:56:47 +03:00
bf891c85e8
Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
97 lines
3.6 KiB
TypeScript
97 lines
3.6 KiB
TypeScript
/*
|
|
Copyright 2016 Aviral Dasgupta
|
|
Copyright 2017 Vector Creations Ltd
|
|
Copyright 2017 New Vector Ltd
|
|
Copyright 2018 Michael Telatynski <7t3chguy@gmail.com>
|
|
|
|
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';
|
|
import AutocompleteProvider from './AutocompleteProvider';
|
|
import QueryMatcher from './QueryMatcher';
|
|
import {TextualCompletion} from './Components';
|
|
import {ICompletion, ISelectionRange} from "./Autocompleter";
|
|
import {Command, Commands, CommandMap} from '../SlashCommands';
|
|
|
|
const COMMAND_RE = /(^\/\w*)(?: .*)?/g;
|
|
|
|
export default class CommandProvider extends AutocompleteProvider {
|
|
matcher: QueryMatcher<Command>;
|
|
|
|
constructor() {
|
|
super(COMMAND_RE);
|
|
this.matcher = new QueryMatcher(Commands, {
|
|
keys: ['command', 'args', 'description'],
|
|
funcs: [({aliases}) => aliases.join(" ")], // aliases
|
|
});
|
|
}
|
|
|
|
async getCompletions(query: string, selection: ISelectionRange, force?: boolean): Promise<ICompletion[]> {
|
|
const {command, range} = this.getCurrentCommand(query, selection);
|
|
if (!command) return [];
|
|
|
|
let matches = [];
|
|
// check if the full match differs from the first word (i.e. returns false if the command has args)
|
|
if (command[0] !== command[1]) {
|
|
// The input looks like a command with arguments, perform exact match
|
|
const name = command[1].substr(1); // strip leading `/`
|
|
if (CommandMap.has(name)) {
|
|
// some commands, namely `me` and `ddg` don't suit having the usage shown whilst typing their arguments
|
|
if (CommandMap.get(name).hideCompletionAfterSpace) return [];
|
|
matches = [CommandMap.get(name)];
|
|
}
|
|
} else {
|
|
if (query === '/') {
|
|
// If they have just entered `/` show everything
|
|
matches = Commands;
|
|
} else {
|
|
// otherwise fuzzy match against all of the fields
|
|
matches = this.matcher.match(command[1]);
|
|
}
|
|
}
|
|
|
|
|
|
return matches.map((result) => {
|
|
let completion = result.getCommand() + ' ';
|
|
const usedAlias = result.aliases.find(alias => `/${alias}` === command[1]);
|
|
// If the command (or an alias) is the same as the one they entered, we don't want to discard their arguments
|
|
if (usedAlias || result.getCommand() === command[1]) {
|
|
completion = command[0];
|
|
}
|
|
|
|
return {
|
|
completion,
|
|
type: "command",
|
|
component: <TextualCompletion
|
|
title={`/${usedAlias || result.command}`}
|
|
subtitle={result.args}
|
|
description={_t(result.description)} />,
|
|
range,
|
|
};
|
|
});
|
|
}
|
|
|
|
getName() {
|
|
return '*️⃣ ' + _t('Commands');
|
|
}
|
|
|
|
renderCompletions(completions: React.ReactNode[]): React.ReactNode {
|
|
return (
|
|
<div className="mx_Autocomplete_Completion_container_block" role="listbox" aria-label={_t("Command Autocomplete")}>
|
|
{ completions }
|
|
</div>
|
|
);
|
|
}
|
|
}
|