1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-01-17 18:44:45 +02:00

Desktop: Allow passing arguments to commands in command palette

This commit is contained in:
Laurent Cozic 2021-06-10 11:46:41 +02:00
parent c37eb56ed7
commit 00dc1d881b

View File

@ -47,6 +47,12 @@ interface State {
listType: number; listType: number;
showHelp: boolean; showHelp: boolean;
resultsInBody: boolean; resultsInBody: boolean;
commandArgs: string[];
}
interface CommandQuery {
name: string;
args: string[];
} }
class GotoAnything { class GotoAnything {
@ -87,6 +93,7 @@ class Dialog extends React.PureComponent<Props, State> {
listType: BaseModel.TYPE_NOTE, listType: BaseModel.TYPE_NOTE,
showHelp: false, showHelp: false,
resultsInBody: false, resultsInBody: false,
commandArgs: [],
}; };
this.styles_ = {}; this.styles_ = {};
@ -250,6 +257,15 @@ class Dialog extends React.PureComponent<Props, State> {
return this.markupToHtml_; return this.markupToHtml_;
} }
private parseCommandQuery(query: string): CommandQuery {
const fullQuery = query;
const splitted = fullQuery.split(/\s+/);
return {
name: splitted.length ? splitted[0] : '',
args: splitted.slice(1),
};
}
async updateList() { async updateList() {
let resultsInBody = false; let resultsInBody = false;
@ -260,13 +276,16 @@ class Dialog extends React.PureComponent<Props, State> {
let listType = null; let listType = null;
let searchQuery = ''; let searchQuery = '';
let keywords = null; let keywords = null;
let commandArgs: string[] = [];
if (this.state.query.indexOf(':') === 0) { // COMMANDS if (this.state.query.indexOf(':') === 0) { // COMMANDS
const query = this.state.query.substr(1); const commandQuery = this.parseCommandQuery(this.state.query.substr(1));
listType = BaseModel.TYPE_COMMAND;
keywords = [query];
const commandResults = CommandService.instance().searchCommands(query, true); listType = BaseModel.TYPE_COMMAND;
keywords = [commandQuery.name];
commandArgs = commandQuery.args;
const commandResults = CommandService.instance().searchCommands(commandQuery.name, true);
results = commandResults.map((result: CommandSearchResult) => { results = commandResults.map((result: CommandSearchResult) => {
return { return {
@ -367,6 +386,7 @@ class Dialog extends React.PureComponent<Props, State> {
keywords: keywords ? keywords : await this.keywords(searchQuery), keywords: keywords ? keywords : await this.keywords(searchQuery),
selectedItemId: results.length === 0 ? null : results[0].id, selectedItemId: results.length === 0 ? null : results[0].id,
resultsInBody: resultsInBody, resultsInBody: resultsInBody,
commandArgs: commandArgs,
}); });
} }
} }
@ -379,7 +399,7 @@ class Dialog extends React.PureComponent<Props, State> {
}); });
if (item.type === BaseModel.TYPE_COMMAND) { if (item.type === BaseModel.TYPE_COMMAND) {
void CommandService.instance().execute(item.id); void CommandService.instance().execute(item.id, ...item.commandArgs);
void focusEditorIfEditorCommand(item.id, CommandService.instance()); void focusEditorIfEditorCommand(item.id, CommandService.instance());
return; return;
} }
@ -426,6 +446,7 @@ class Dialog extends React.PureComponent<Props, State> {
id: itemId, id: itemId,
parent_id: parentId, parent_id: parentId,
type: itemType, type: itemType,
commandArgs: this.state.commandArgs,
}); });
} }
@ -466,7 +487,7 @@ class Dialog extends React.PureComponent<Props, State> {
selectedItem() { selectedItem() {
const index = this.selectedItemIndex(); const index = this.selectedItemIndex();
if (index < 0) return null; if (index < 0) return null;
return this.state.results[index]; return { ...this.state.results[index], commandArgs: this.state.commandArgs };
} }
input_onKeyDown(event: any) { input_onKeyDown(event: any) {