Skip to content

Commit aa0e651

Browse files
author
Georgii Rychko
committed
feat(node editing): add support of cancel and applying actions for the node value
For example: esc will cancel editing, enter will apply new value to the node, mouse click elsewhere cancels editing
1 parent d9dc8be commit aa0e651

3 files changed

Lines changed: 34 additions & 17 deletions

File tree

components/editable-node.directive.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,17 @@ export class EditableNodeDirective implements OnInit {
2626

2727
@HostListener('keyup', ['$event', '$event.target.value'])
2828
private editCompleted($event: any, newValue: any) {
29-
//13 - enter
30-
//27 - esc
31-
if ($event.keyCode === 13) {
32-
// http://stackoverflow.com/questions/35515254/what-is-a-dehydrated-detector-and-how-am-i-using-one-here
33-
setTimeout(() => this.valueChanged.emit({value: newValue}), 1);
29+
if ($event.keyCode === 13) {//enter
30+
return this.valueChanged.emit({type: 'keyup', value: newValue});
31+
}
32+
33+
if ($event.keyCode === 27) {//esc
34+
return this.valueChanged.emit({type: 'keyup', value: this.nodeValue});
3435
}
3536
}
3637

3738
@HostListener('blur', ['$event', '$event.target.value'])
3839
private editCompletedByMouse($event: any, newValue: any) {
39-
//13 - enter
40-
//27 - esc
41-
this.valueChanged.emit({value: newValue});
40+
this.valueChanged.emit({type: 'blur', value: this.nodeValue});
4241
}
4342
}

components/ng2-tree.component.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {EditableNodeDirective} from './editable-node.directive.ts';
66
@Component({
77
selector: 'ng2-tree',
88
template: `
9-
<ul *ngIf="tree" class="tree" (keyup)="cancelActions($event)">
9+
<ul *ngIf="tree" class="tree">
1010
<li>
1111
<div (contextmenu)="showMenu($event)">
1212
<span class="folding" (click)="switchFolding($event, tree)" [ngClass]="foldingType(tree)"></span>
@@ -38,13 +38,13 @@ export class Ng2Tree implements OnInit {
3838
private treeService: Ng2TreeService;
3939
private isMenuVisible: boolean = false;
4040
private edit: boolean = false;
41+
private previousEvent: any;
4142

4243
constructor(treeService: Ng2TreeService) {
4344
this.treeService = treeService;
4445
}
4546

4647
private switchFolding($event: any, tree: any): void {
47-
console.log('fold');
4848
this.handleFoldingType($event.target.parentNode.parentNode, tree);
4949
}
5050

@@ -91,14 +91,12 @@ export class Ng2Tree implements OnInit {
9191
private rename($event: any, node: any) {
9292
if ($event.which === 1) {
9393
this.edit = true;
94-
this.isMenuVisible = false;
9594
}
9695
}
9796

9897
private remove($event: any, node: any) {
9998
if ($event.which === 1) {
10099
this.treeService.emitRemoveEvent({node});
101-
this.isMenuVisible = false;
102100
}
103101
}
104102

@@ -110,11 +108,17 @@ export class Ng2Tree implements OnInit {
110108
$event.preventDefault();
111109
}
112110

113-
private cancelActions($event: any): void {
114-
115-
}
116-
117111
private applyNewValue($event: any, node: any): void {
112+
if (!this.previousEvent) {
113+
this.previousEvent = $event.type;
114+
}
115+
116+
if (this.previousEvent === 'keyup' && $event.type === 'blur') {
117+
this.previousEvent = $event.type;
118+
return;
119+
}
120+
121+
this.previousEvent = $event.type;
118122
node.value = $event.value;
119123
this.edit = false;
120124
}
@@ -126,7 +130,7 @@ export class Ng2Tree implements OnInit {
126130
this.isMenuVisible = false;
127131
}
128132
})
129-
133+
130134
this.treeService.removeNodeEventStream()
131135
.subscribe(removeEvent => {
132136
if (!this.tree || !this.tree.children) return;

components/ng2-tree.service.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,21 @@ export class Ng2TreeService {
66
private menuEvents$:EventEmitter<any> = new EventEmitter();
77
private cancelEvents$:EventEmitter<any> = new EventEmitter();
88
private removeEvents$:EventEmitter<any> = new EventEmitter();
9+
public root: any;
10+
11+
constructor() {
12+
window.addEventListener('keyup', (event: any) => {
13+
if (event.keyCode === 27) {
14+
this.emitMenuEvent({sender: null, action: 'close'});
15+
}
16+
});
17+
918

19+
window.addEventListener('click', (event: any) => {
20+
this.emitMenuEvent({sender: null, action: 'close'});
21+
});
22+
}
23+
1024
menuEventStream(): Observable<any> {
1125
return this.menuEvents$;
1226
}

0 commit comments

Comments
 (0)