Skip to content
This repository has been archived by the owner on May 21, 2019. It is now read-only.

Commit

Permalink
Use addCommand for historyNavigation.
Browse files Browse the repository at this point in the history
  • Loading branch information
vlad-shatskyi committed Aug 21, 2017
1 parent 05bf084 commit dd7df85
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 34 deletions.
42 changes: 30 additions & 12 deletions src/views/PromptComponent.tsx
Expand Up @@ -40,7 +40,7 @@ export class PromptComponent extends React.Component<Props, State> {
fontSize: services.font.size * 1.2,
fontFamily: services.font.family,
suggestFontSize: services.font.size,
minimap: { enabled: false },
minimap: {enabled: false},
scrollbar: {
vertical: "hidden",
horizontal: "hidden",
Expand All @@ -49,8 +49,30 @@ export class PromptComponent extends React.Component<Props, State> {
quickSuggestions: true,
quickSuggestionsDelay: 0,
parameterHints: true,
fontLigatures: true,
});

this.editor.addCommand(
monaco.KeyCode.UpArrow,
() => this.setPreviousHistoryItem(),
"!suggestWidgetVisible",
);
this.editor.addCommand(
monaco.KeyMod.WinCtrl | monaco.KeyCode.KEY_P,
() => this.setPreviousHistoryItem(),
"!suggestWidgetVisible",
);
this.editor.addCommand(
monaco.KeyCode.DownArrow,
() => this.setNextHistoryItem(),
"!suggestWidgetVisible",
);
this.editor.addCommand(
monaco.KeyMod.WinCtrl | monaco.KeyCode.KEY_N,
() => this.setNextHistoryItem(),
"!suggestWidgetVisible",
);

this.focus();
}

Expand Down Expand Up @@ -124,11 +146,14 @@ export class PromptComponent extends React.Component<Props, State> {
}
}

shouldNavigateHistory() {
return this.state.displayedHistoryRecordID || this.editor.getValue() === "";
setValue(value: string): void {
this.editor.setValue(value);
this.editor.setPosition({lineNumber: 1, column: value.length + 1});
this.prompt.setValue(value);
this.focus();
}

setPreviousHistoryItem(): void {
private setPreviousHistoryItem(): void {
const currentID = this.state.displayedHistoryRecordID;
if (currentID) {
const currentRecord = services.history.get(currentID);
Expand All @@ -150,7 +175,7 @@ export class PromptComponent extends React.Component<Props, State> {
}
}

setNextHistoryItem(): void {
private setNextHistoryItem(): void {
const currentID = this.state.displayedHistoryRecordID;
if (currentID) {
const currentRecord = services.history.get(currentID);
Expand All @@ -168,13 +193,6 @@ export class PromptComponent extends React.Component<Props, State> {
}
}

setValue(value: string): void {
this.editor.setValue(value);
this.editor.setPosition({lineNumber: 1, column: value.length + 1});
this.prompt.setValue(value);
this.focus();
}

private get promptContentNode(): HTMLDivElement {
/* tslint:disable:no-string-literal */
return this.refs["prompt-content"] as HTMLDivElement;
Expand Down
26 changes: 5 additions & 21 deletions src/views/keyevents/Keybindings.ts
Expand Up @@ -356,37 +356,21 @@ export function handleUserEvent(application: ApplicationComponent, search: Searc
return;
}

if (event.keyCode === KeyCode.Tab && promptComponent.isInHistorySearch()) {
promptComponent.applyHistorySearch();
return;
}

if (event.keyCode === KeyCode.Escape && promptComponent.isInHistorySearch()) {
promptComponent.cancelHistorySearch();
return;
}

if (event.ctrlKey && event.keyCode === KeyCode.R) {
if (event.ctrlKey && event.keyCode === KeyCode.R && !promptComponent.isInHistorySearch()) {
promptComponent.startHistorySearch();

event.stopPropagation();
event.preventDefault();
return;
}

if (promptComponent.shouldNavigateHistory() && isKeybindingForEvent(event, KeyboardAction.cliHistoryPrevious)) {
promptComponent.setPreviousHistoryItem();

event.stopPropagation();
event.preventDefault();
if (event.keyCode === KeyCode.Tab && promptComponent.isInHistorySearch()) {
promptComponent.applyHistorySearch();
return;
}

if (promptComponent.shouldNavigateHistory() && isKeybindingForEvent(event, KeyboardAction.cliHistoryNext)) {
promptComponent.setNextHistoryItem();

event.stopPropagation();
event.preventDefault();
if (event.keyCode === KeyCode.Escape && promptComponent.isInHistorySearch()) {
promptComponent.cancelHistorySearch();
return;
}
}
2 changes: 1 addition & 1 deletion tslint.json
Expand Up @@ -37,7 +37,7 @@
],
"no-any": false,
"no-arg": true,
"no-bitwise": true,
"no-bitwise": false,
"no-conditional-assignment": true,
"no-consecutive-blank-lines": false,
"no-console": [
Expand Down

0 comments on commit dd7df85

Please sign in to comment.