Skip to content

Commit

Permalink
Desktop: Allow passing arguments to commands in command palette
Browse files Browse the repository at this point in the history
  • Loading branch information
laurent22 committed Jun 10, 2021
1 parent c37eb56 commit 00dc1d8
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions packages/app-desktop/plugins/GotoAnything.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ interface State {
listType: number;
showHelp: boolean;
resultsInBody: boolean;
commandArgs: string[];
}

interface CommandQuery {
name: string;
args: string[];
}

class GotoAnything {
Expand Down Expand Up @@ -87,6 +93,7 @@ class Dialog extends React.PureComponent<Props, State> {
listType: BaseModel.TYPE_NOTE,
showHelp: false,
resultsInBody: false,
commandArgs: [],
};

this.styles_ = {};
Expand Down Expand Up @@ -250,6 +257,15 @@ class Dialog extends React.PureComponent<Props, State> {
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() {
let resultsInBody = false;

Expand All @@ -260,13 +276,16 @@ class Dialog extends React.PureComponent<Props, State> {
let listType = null;
let searchQuery = '';
let keywords = null;
let commandArgs: string[] = [];

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];
keywords = [commandQuery.name];
commandArgs = commandQuery.args;

const commandResults = CommandService.instance().searchCommands(query, true);
const commandResults = CommandService.instance().searchCommands(commandQuery.name, true);

results = commandResults.map((result: CommandSearchResult) => {
return {
Expand Down Expand Up @@ -367,6 +386,7 @@ class Dialog extends React.PureComponent<Props, State> {
keywords: keywords ? keywords : await this.keywords(searchQuery),
selectedItemId: results.length === 0 ? null : results[0].id,
resultsInBody: resultsInBody,
commandArgs: commandArgs,
});
}
}
Expand All @@ -379,7 +399,7 @@ class Dialog extends React.PureComponent<Props, State> {
});

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());
return;
}
Expand Down Expand Up @@ -426,6 +446,7 @@ class Dialog extends React.PureComponent<Props, State> {
id: itemId,
parent_id: parentId,
type: itemType,
commandArgs: this.state.commandArgs,
});
}

Expand Down Expand Up @@ -466,7 +487,7 @@ class Dialog extends React.PureComponent<Props, State> {
selectedItem() {
const index = this.selectedItemIndex();
if (index < 0) return null;
return this.state.results[index];
return { ...this.state.results[index], commandArgs: this.state.commandArgs };
}

input_onKeyDown(event: any) {
Expand Down

0 comments on commit 00dc1d8

Please sign in to comment.