1 | import keyString from '@yelloxing/core.js/tools/keyString';
|
2 | import xhtml from '../xhtml';
|
3 |
|
4 |
|
5 |
|
6 | export default function () {
|
7 |
|
8 |
|
9 | xhtml.bind(this._el, 'click', event => {
|
10 |
|
11 | let position = xhtml.position(this._el, event);
|
12 | let topIndex = Math.round((position.y - 20.5) / 21);
|
13 |
|
14 |
|
15 | if (topIndex < 0 || topIndex >= this._contentArray.length) return;
|
16 |
|
17 | this.__lineNum = topIndex;
|
18 | this.__leftNum = this.$$bestLeftNum(position.x);
|
19 |
|
20 | this.$$updateCursorPosition();
|
21 | this.$$updateView();
|
22 | });
|
23 |
|
24 | let update = text => {
|
25 |
|
26 |
|
27 | text = text || this.__focusDOM.value;
|
28 |
|
29 | this.__focusDOM.value = "";
|
30 |
|
31 |
|
32 | if (/^\n$/.test(text)) {
|
33 |
|
34 | if (this.__leftNum >= this._contentArray[this.__lineNum].length) {
|
35 | this._contentArray.splice(this.__lineNum + 1, 0, "");
|
36 | } else {
|
37 | this._contentArray.splice(this.__lineNum + 1, 0, this._contentArray[this.__lineNum].substring(this.__leftNum));
|
38 | this._contentArray[this.__lineNum] = this._contentArray[this.__lineNum].substring(0, this.__leftNum);
|
39 | }
|
40 | this.__lineNum += 1;
|
41 | this.__leftNum = 0;
|
42 |
|
43 | }
|
44 |
|
45 |
|
46 | else {
|
47 |
|
48 | let textArray = text.split(/\n/);
|
49 |
|
50 |
|
51 | if (textArray.length <= 1) {
|
52 | this._contentArray[this.__lineNum] = this._contentArray[this.__lineNum].substring(0, this.__leftNum) + text + this._contentArray[this.__lineNum].substring(this.__leftNum);
|
53 | this.__leftNum += text.length;
|
54 | }
|
55 |
|
56 |
|
57 | else {
|
58 |
|
59 |
|
60 | let leftText = this._contentArray[this.__lineNum].substring(0, this.__leftNum);
|
61 | let rightText = this._contentArray[this.__lineNum].substring(this.__leftNum);
|
62 |
|
63 |
|
64 | textArray[0] = leftText + textArray[0];
|
65 | textArray[textArray.length - 1] += rightText;
|
66 |
|
67 |
|
68 | this._contentArray.splice(this.__lineNum, 1, ...textArray);
|
69 |
|
70 | this.__lineNum += (textArray.length - 1);
|
71 | this.__leftNum = textArray[textArray.length - 1].length - rightText.length;
|
72 |
|
73 | }
|
74 |
|
75 | }
|
76 |
|
77 |
|
78 |
|
79 | this.__formatData = this.$shader(this._contentArray.join('\n'));
|
80 | this.$$updateCursorPosition();
|
81 | this.$$updateView();
|
82 |
|
83 | };
|
84 |
|
85 |
|
86 | xhtml.bind(this.__focusDOM, 'compositionstart', () => {
|
87 | this.__needUpdate = false;
|
88 | this.__focusDOM.style.color = "rgba(0,0,0,0)";
|
89 | this.__focusDOM.style.borderLeft = '1px solid ' + this._colorCursor;
|
90 | });
|
91 |
|
92 |
|
93 | xhtml.bind(this.__focusDOM, 'compositionend', () => {
|
94 | this.__needUpdate = true;
|
95 | this.__focusDOM.style.color = this._colorCursor;
|
96 | this.__focusDOM.style.borderLeft = "none";
|
97 | update();
|
98 | });
|
99 |
|
100 |
|
101 | xhtml.bind(this.__focusDOM, 'input', () => {
|
102 |
|
103 | if (this.__needUpdate) update();
|
104 | });
|
105 |
|
106 |
|
107 | xhtml.bind(this._el, 'keydown', event => {
|
108 |
|
109 |
|
110 |
|
111 | switch (keyString(event)) {
|
112 |
|
113 | case "tab": {
|
114 |
|
115 |
|
116 | xhtml.stopPropagation(event);
|
117 | xhtml.preventDefault(event);
|
118 |
|
119 |
|
120 | let blanks = "";
|
121 | for (let i = 0; i < this._tabSpace; i++) blanks += " ";
|
122 |
|
123 | update(blanks);
|
124 |
|
125 | break;
|
126 | }
|
127 |
|
128 | case "up": {
|
129 |
|
130 |
|
131 | if (this.__lineNum <= 0) return;
|
132 |
|
133 | this.__leftNum = this.$$bestLeftNum(this.$$textWidth("", true) + 40);
|
134 |
|
135 |
|
136 | this.__lineNum -= 1;
|
137 |
|
138 | this.$$updateCursorPosition();
|
139 | this.$$updateView();
|
140 |
|
141 | this._el.scrollTop -= 21;
|
142 |
|
143 | break;
|
144 | }
|
145 |
|
146 | case "down": {
|
147 |
|
148 | if (this.__lineNum >= this._contentArray.length - 1) return;
|
149 |
|
150 | this.__leftNum = this.$$bestLeftNum(this.$$textWidth("", true) + 40);
|
151 |
|
152 |
|
153 | this.__lineNum += 1;
|
154 |
|
155 | this.$$updateCursorPosition();
|
156 | this.$$updateView();
|
157 |
|
158 | this._el.scrollTop += 21;
|
159 |
|
160 | break;
|
161 | }
|
162 |
|
163 | case "left": {
|
164 |
|
165 | if (this.__leftNum <= 0) {
|
166 | if (this.__lineNum <= 0) return;
|
167 | this.__lineNum -= 1;
|
168 | this.__leftNum = this._contentArray[this.__lineNum].length;
|
169 | } else {
|
170 | this.__leftNum -= 1;
|
171 | }
|
172 |
|
173 | this.$$updateCursorPosition();
|
174 |
|
175 | break;
|
176 | }
|
177 |
|
178 | case "right": {
|
179 |
|
180 | if (this.__leftNum >= this._contentArray[this.__lineNum].length) {
|
181 | if (this.__lineNum >= this._contentArray.length - 1) return;
|
182 | this.__lineNum += 1;
|
183 | this.__leftNum = 0;
|
184 | } else {
|
185 | this.__leftNum += 1;
|
186 | }
|
187 |
|
188 | this.$$updateCursorPosition();
|
189 |
|
190 | break;
|
191 | }
|
192 |
|
193 | case "backspace": {
|
194 |
|
195 | if (this.__leftNum <= 0) {
|
196 | if (this.__lineNum <= 0) return;
|
197 |
|
198 | this.__lineNum -= 1;
|
199 | this.__leftNum = this._contentArray[this.__lineNum].length;
|
200 |
|
201 |
|
202 | this._contentArray[this.__lineNum] += this._contentArray[this.__lineNum + 1];
|
203 | this._contentArray.splice(this.__lineNum + 1, 1);
|
204 |
|
205 | } else {
|
206 | this.__leftNum -= 1;
|
207 | this._contentArray[this.__lineNum] = this._contentArray[this.__lineNum].substring(0, this.__leftNum) + this._contentArray[this.__lineNum].substring(this.__leftNum + 1);
|
208 | }
|
209 |
|
210 |
|
211 | this.__formatData = this.$shader(this._contentArray.join('\n'));
|
212 |
|
213 |
|
214 | this.$$updateCursorPosition();
|
215 | this.$$updateView();
|
216 |
|
217 | break;
|
218 | }
|
219 | }
|
220 |
|
221 | });
|
222 |
|
223 | }; |
\ | No newline at end of file |