-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditorInputBase.js
More file actions
120 lines (100 loc) · 2.43 KB
/
EditorInputBase.js
File metadata and controls
120 lines (100 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
class EditorInputBase extends EditorEntryBase {
constructor(id, label) {
super(id);
this.input_ = document.createElement('input');
this.input_.type = 'text';
this.listen(this.input_, 'keydown', (e) => this.onInputKeyDown(e));
this.listen(this.input_, 'input', () => this.onInput());
this.listen(this.input_, 'blur', (e) => this.onBlur(e));
this.elem_.appendChild(this.input_);
this.lastLabel_ = '';
this.lastSnapshotLabel_ = '';
if (label) {
this.setLabel(label);
}
}
afterDomAdd() {
this.input_.focus();
}
serialize(base) {
super.serialize(base);
base.label = this.getLabel();
return base;
}
getLabel() {
return this.input_.value;
}
setLabel(label) {
this.input_.value = label;
this.lastSnapshotLabel_ = label;
this.updateLabel();
}
wantFocus() {
return this.getLabel() == '';
}
updateLabel() {
this.lastLabel_ = this.getLabel();
let objs = document.getElementsByClassName('grid-' + this.getId());
if (objs.length == 1) {
objs[0].innerText = this.getLabel();
objs[0].xArchFixSize();
}
}
onInput() {
this.updateLabel();
this.requestRender();
}
onBlur() {
if (this.getLabel() != this.lastSnapshotLabel_) {
this.lastSnapshotLabel_ = this.getLabel();
this.elem_.dispatchEvent(
new CustomEvent('snapshotRequest', { bubbles: true }));
}
}
onInputKeyDown(e) {
switch (e.key) {
case 'Enter':
e.preventDefault();
e.stopPropagation();
if (this.elem_.nextElementSibling &&
this.elem_.nextElementSibling.xArchObj &&
this.elem_.nextElementSibling.xArchObj.wantFocus()) {
this.elem_.nextElementSibling.xArchObj.startEdit();
} else {
this.stopEdit();
}
break;
case 'Escape':
case '`':
this.stopEdit();
e.preventDefault();
e.stopPropagation();
break;
case 'PageUp':
case 'PageDown':
this.stopEdit();
break;
default:
e.stopPropagation();
break;
}
}
onKeyDown(e) {
super.onKeyDown(e);
switch (e.key) {
case 'ArrowRight':
case 'Enter':
this.ctrlKey_ = e.ctrlKey;
this.startEdit();
e.stopPropagation();
e.preventDefault();
break;
}
}
startEdit() {
this.input_.focus();
}
stopEdit() {
this.elem_.focus();
}
}