element-web/src/autocomplete/CommandProvider.js

67 lines
2.4 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
2017-11-02 18:01:28 +00:00
Copyright 2017 New Vector Ltd
Copyright 2018 Michael Telatynski <7t3chguy@gmail.com>
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.
*/
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';
import {CommandMap} from '../SlashCommands';
import type {SelectionRange} from "./Autocompleter";
2016-06-01 11:24:21 +00:00
const COMMANDS = Object.values(CommandMap);
2016-06-01 11:24:21 +00:00
const COMMAND_RE = /(^\/\w*)(?: .*)?/g;
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: SelectionRange, force?: boolean) {
const {command, range} = this.getCurrentCommand(query, selection);
if (!command) return [];
// if the query is just `/` (and the user hit TAB or waits), show them all COMMANDS otherwise FuzzyMatch them
const matches = query === '/' ? COMMANDS : this.matcher.match(command[1]);
return matches.map((result) => ({
// If the command is the same as the one they entered, we don't want to discard their arguments
completion: result.command === command[1] ? command[0] : (result.command + ' '),
component: <TextualCompletion
title={result.command}
subtitle={result.args}
description={_t(result.description)} />,
range,
}));
2016-06-01 11:24:21 +00:00
}
getName() {
return '*️⃣ ' + _t('Commands');
}
renderCompletions(completions: [React.Component]): ?React.Component {
2016-08-22 19:06:31 +00:00
return <div className="mx_Autocomplete_Completion_container_block">
{ completions }
2016-08-22 19:06:31 +00:00
</div>;
}
2016-06-01 11:24:21 +00:00
}