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

Commit

Permalink
fix(markdown): emit event on outside-click if no data changed (#157)
Browse files Browse the repository at this point in the history
* fix(markdown): add emit event on outside-click

* fix(test): add test for outside-click event
  • Loading branch information
divyanshiGupta authored and joshuawilson committed Sep 27, 2018
1 parent eeef189 commit b4b77a8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/app/markdown/markdown.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ describe('Markdown component - ', () => {

beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [ FormsModule, MarkdownModule, BrowserModule ],
declarations: [ ]
imports: [ FormsModule, MarkdownModule, BrowserModule ]
})
.compileComponents()
.then(() => {
Expand Down Expand Up @@ -116,4 +115,11 @@ describe('Markdown component - ', () => {
comp.saveClick();
expect(comp.onSaveClick.emit).toHaveBeenCalled();
});

it('should emit onClickOut when clicked outside', () => {
spyOn(comp.onClickOut, 'emit');
comp.onClick(fixture.nativeElement);
comp.onClick(document.body);
expect(comp.onClickOut.emit).toHaveBeenCalledTimes(1);
});
});
19 changes: 19 additions & 0 deletions src/app/markdown/markdown.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Component,
ElementRef,
EventEmitter,
HostListener,
Input,
OnChanges,
OnInit,
Expand Down Expand Up @@ -45,6 +46,23 @@ export class MarkdownComponent implements OnChanges, OnInit, AfterViewChecked {
@Output() onSaveClick = new EventEmitter();
@Output() showPreview = new EventEmitter();
@Output() onCloseClick = new EventEmitter();
@Output() onClickOut = new EventEmitter();

@HostListener('document:click', ['$event.target'])
onClick(target: HTMLElement): void {
if (!target) {
return;
}
const clickedInside = this.elementRef.nativeElement.contains(target);

if (this.editorInput) {
this.isNoDataChanged = this.editorInput.nativeElement.innerText.trim() === this.rawText;
}

if (!clickedInside) {
this.onClickOut.emit(this.isNoDataChanged);
}
}

@ViewChild('editorInput') editorInput: ElementRef;
@ViewChild('editorBox') editorBox: ElementRef;
Expand All @@ -57,6 +75,7 @@ export class MarkdownComponent implements OnChanges, OnInit, AfterViewChecked {
rawText = '';
fieldEmpty: boolean = true;
previousRawText = '';
isNoDataChanged: boolean = false;

private markdownViewExpanded: boolean = false;
private tabBarVisible: boolean = true;
Expand Down

0 comments on commit b4b77a8

Please sign in to comment.