Skip to content

Commit

Permalink
feature(input): allow transforming input from input button before ins…
Browse files Browse the repository at this point in the history
…ert into editor

close #12
  • Loading branch information
Raiper34 committed Oct 29, 2022
1 parent f49d45d commit 3949c19
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,23 @@ describe('EditorInputComponent', () => {
expect(component.command.emit).not.toHaveBeenCalled();
});

it('should emit command with transformed value when transform method is defined', () => {
const command = ExecCommand.createLink;
component.value = 'www.example.com';
component.command.emit = jasmine.createSpy('emit');
component.button = {
type: ToolbarItemType.Input,
command: ExecCommand.createLink,
icon: 'link',
text: 'Url',
transform: (val: string) => val.replace('www', 'http://www')
};

component.onCommand();

expect(component.command.emit).toHaveBeenCalledWith({command, value: 'http://www.example.com'});
});

it('should close window when click outside', (done) => {
component.openInputWindow();
fixture.detectChanges();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class EditorInputComponent {

@Input() button: EditorInput;
@Input() state: string | number | boolean;
@Output() command = new EventEmitter<{command: ExecCommand, value: string}>();
@Output() command = new EventEmitter<{ command: ExecCommand, value: string }>();
@ViewChild('input', {static: false}) inputElement: ElementRef;
@ViewChild('window', {static: false}) windowElement: ElementRef;
showInputWindow = false;
Expand All @@ -28,7 +28,10 @@ export class EditorInputComponent {
onCommand(): void {
if (this.value) {
this.closeInputWindow();
this.command.emit({command: this.button.command, value: this.value});
this.command.emit({
command: this.button.command,
value: this.button.transform ? this.button.transform(this.value) : this.value
});
this.value = '';
}
}
Expand Down
1 change: 1 addition & 0 deletions projects/ngx-simple-text-editor/src/lib/models/button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface EditorInput extends ToolbarBaseItem {
text: string;
title?: string;
label?: string;
transform?: (val: string) => string;
}

export interface EditorSelect extends ToolbarBaseItem {
Expand Down

0 comments on commit 3949c19

Please sign in to comment.