-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Save history command in local storage. Some refactor to make the flow more reactive
- Loading branch information
Showing
6 changed files
with
247 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export enum SESSIONSTORAGEKEYS { | ||
HISTORYCOMMAND = 'HistoryCommand', | ||
} | ||
export const getFromLocalStorage = (key: SESSIONSTORAGEKEYS) => | ||
sessionStorage.getItem(key); | ||
|
||
export const setToLocalStorage = (key: SESSIONSTORAGEKEYS, value: string) => | ||
sessionStorage.setItem(key, value); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const isNil = <T>(value: T): value is null => value === null; | ||
export const isNotNil = <T>(value: T): value is T => value !== null; |
30 changes: 13 additions & 17 deletions
30
src/app/features/command/command-line/command-line.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,18 @@ | ||
<div class="command-line-container"> | ||
<input type="text" | ||
class="form-control" | ||
placeholder="Insert command" | ||
[formControl]="commandLine" | ||
(keyup.enter)="executeCommand(commandLine.value)" | ||
(keydown)="getHistory($event)"> | ||
<input #input type="text" class="form-control" placeholder="Insert command" [formControl]="commandLine" | ||
(keyup.enter)="executeCommandSubj$.next(commandLine.value)" (keydown.arrowup)="arrowUpClickSubj$.next($event)" | ||
(keydown.arrowdown)="arrowDownClickSubj$.next($event)" /> | ||
|
||
<div class="font-italic text-muted suggestions pl-2 mb-0 h-auto"> | ||
<small *ngIf="!commandLine.dirty || commandLine.value.length < 3">no command enter (min. 3 char)</small> | ||
<small *ngIf="commandLine.valid" class="suggestion"> {{ activeCommand?.suggestion }}</small> | ||
<small | ||
class="text-danger" | ||
*ngIf="!commandLine.valid | ||
&& commandLine.errors | ||
&& commandLine.errors.allowedCommand | ||
&& commandLine.errors.allowedCommand.value | ||
&& commandLine.errors.allowedCommand.value.length >= 3"> | ||
command not found | ||
<small *ngIf=" | ||
!commandLine.dirty || commandLine.value.length < commandLineMinLetter | ||
"> | ||
no command enter (min. 3 char) | ||
</small> | ||
<small *ngIf="commandLine.valid" class="suggestion"> | ||
{{ activeCommand?.suggestion }}</small> | ||
<small class="text-danger" *ngIf="isValidCommand()"> | ||
command not found | ||
</small> | ||
</div> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
src/app/features/command/command-line/command-line.interface.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface CommandsHistory { | ||
commandsHistory: Array<string>; | ||
commandsHistoryCursor: number; | ||
} |
108 changes: 108 additions & 0 deletions
108
src/app/features/command/command-line/command-line.utilities.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { ReturnStatement } from '@angular/compiler'; | ||
import { ElementRef } from '@angular/core'; | ||
import { FormControl } from '@angular/forms'; | ||
import { | ||
getFromLocalStorage, | ||
SESSIONSTORAGEKEYS, | ||
setToLocalStorage, | ||
} from '@app/core/utilities/local-storage.utilities'; | ||
import { isNotNil } from '@app/core/utilities/logic.utilities'; | ||
import { BehaviorSubject, merge, Observable } from 'rxjs'; | ||
import { | ||
withLatestFrom, | ||
tap, | ||
map, | ||
mapTo, | ||
filter, | ||
switchMapTo, | ||
takeUntil, | ||
} from 'rxjs/operators'; | ||
import { CommandsHistory } from './command-line.interface'; | ||
|
||
export const setValueFn = (commandLine: FormControl) => (value: string) => | ||
commandLine.setValue(value); | ||
|
||
export type SetValueFn = ReturnType<typeof setValueFn>; | ||
export const createHistoryPipe = ( | ||
arrowUp$: Observable<Event>, | ||
arrowDown$: Observable<Event>, | ||
commandsHistorySubj$: BehaviorSubject<CommandsHistory>, | ||
commandsHistory$: Observable<CommandsHistory>, | ||
inputElementRef$: Observable<ElementRef<HTMLInputElement> | null>, | ||
destroy$: Observable<void>, | ||
setValue: SetValueFn | ||
) => { | ||
const setValueArrowUpClick$ = arrowUp$.pipe( | ||
tap((event) => event.preventDefault()), | ||
withLatestFrom(commandsHistory$), | ||
tap(([, { commandsHistory, commandsHistoryCursor }]) => | ||
commandsHistorySubj$.next({ | ||
commandsHistoryCursor: | ||
commandsHistory.length > commandsHistoryCursor + 1 | ||
? commandsHistoryCursor + 1 | ||
: commandsHistoryCursor, | ||
commandsHistory, | ||
}) | ||
), | ||
map( | ||
([, { commandsHistory, commandsHistoryCursor }]) => | ||
commandsHistory[commandsHistoryCursor] | ||
), | ||
filter(isNotNil), | ||
tap(setValue) | ||
); | ||
|
||
const setValueArrowDownClick$ = arrowDown$.pipe( | ||
tap((event) => event.preventDefault()), | ||
withLatestFrom(commandsHistory$), | ||
tap(([, { commandsHistory, commandsHistoryCursor }]) => | ||
commandsHistorySubj$.next({ | ||
commandsHistoryCursor: | ||
commandsHistoryCursor - 1 <= 0 ? 0 : commandsHistoryCursor - 1, | ||
commandsHistory, | ||
}) | ||
), | ||
map(([, { commandsHistory, commandsHistoryCursor }]) => | ||
commandsHistoryCursor <= 0 | ||
? '' | ||
: commandsHistory[commandsHistoryCursor - 1] | ||
), | ||
filter(isNotNil), | ||
tap(setValue) | ||
); | ||
|
||
return merge(setValueArrowUpClick$, setValueArrowDownClick$).pipe( | ||
switchMapTo(inputElementRef$), | ||
filter(isNotNil), | ||
tap((mutableElementRef) => { | ||
const selectionEnd = mutableElementRef.nativeElement.selectionEnd; | ||
mutableElementRef.nativeElement.selectionStart = selectionEnd; | ||
mutableElementRef.nativeElement.selectionEnd = selectionEnd; | ||
}), | ||
mapTo(void 0), | ||
takeUntil(destroy$) | ||
); | ||
}; | ||
|
||
export const setNewCommandsHistoryInLocalStorage = ( | ||
commandsHistory: Array<string> | ||
) => { | ||
const commandsHistoryStringify = JSON.stringify(commandsHistory); | ||
setToLocalStorage( | ||
SESSIONSTORAGEKEYS.HISTORYCOMMAND, | ||
commandsHistoryStringify | ||
); | ||
}; | ||
|
||
export const getHistoryFromLocalStorage = () => { | ||
const commandsHistory = getFromLocalStorage( | ||
SESSIONSTORAGEKEYS.HISTORYCOMMAND | ||
); | ||
|
||
try { | ||
const history = JSON.parse(commandsHistory) as Array<string>; | ||
return history ? history : []; | ||
} catch (_) { | ||
return []; | ||
} | ||
}; |