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

Commit b4b77a8

Browse files
divyanshiGuptajoshuawilson
authored andcommitted
fix(markdown): emit event on outside-click if no data changed (#157)
* fix(markdown): add emit event on outside-click * fix(test): add test for outside-click event
1 parent eeef189 commit b4b77a8

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/app/markdown/markdown.component.spec.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ describe('Markdown component - ', () => {
2020

2121
beforeEach(async(() => {
2222
TestBed.configureTestingModule({
23-
imports: [ FormsModule, MarkdownModule, BrowserModule ],
24-
declarations: [ ]
23+
imports: [ FormsModule, MarkdownModule, BrowserModule ]
2524
})
2625
.compileComponents()
2726
.then(() => {
@@ -116,4 +115,11 @@ describe('Markdown component - ', () => {
116115
comp.saveClick();
117116
expect(comp.onSaveClick.emit).toHaveBeenCalled();
118117
});
118+
119+
it('should emit onClickOut when clicked outside', () => {
120+
spyOn(comp.onClickOut, 'emit');
121+
comp.onClick(fixture.nativeElement);
122+
comp.onClick(document.body);
123+
expect(comp.onClickOut.emit).toHaveBeenCalledTimes(1);
124+
});
119125
});

src/app/markdown/markdown.component.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
Component,
55
ElementRef,
66
EventEmitter,
7+
HostListener,
78
Input,
89
OnChanges,
910
OnInit,
@@ -45,6 +46,23 @@ export class MarkdownComponent implements OnChanges, OnInit, AfterViewChecked {
4546
@Output() onSaveClick = new EventEmitter();
4647
@Output() showPreview = new EventEmitter();
4748
@Output() onCloseClick = new EventEmitter();
49+
@Output() onClickOut = new EventEmitter();
50+
51+
@HostListener('document:click', ['$event.target'])
52+
onClick(target: HTMLElement): void {
53+
if (!target) {
54+
return;
55+
}
56+
const clickedInside = this.elementRef.nativeElement.contains(target);
57+
58+
if (this.editorInput) {
59+
this.isNoDataChanged = this.editorInput.nativeElement.innerText.trim() === this.rawText;
60+
}
61+
62+
if (!clickedInside) {
63+
this.onClickOut.emit(this.isNoDataChanged);
64+
}
65+
}
4866

4967
@ViewChild('editorInput') editorInput: ElementRef;
5068
@ViewChild('editorBox') editorBox: ElementRef;
@@ -57,6 +75,7 @@ export class MarkdownComponent implements OnChanges, OnInit, AfterViewChecked {
5775
rawText = '';
5876
fieldEmpty: boolean = true;
5977
previousRawText = '';
78+
isNoDataChanged: boolean = false;
6079

6180
private markdownViewExpanded: boolean = false;
6281
private tabBarVisible: boolean = true;

0 commit comments

Comments
 (0)