@@ -12,79 +12,155 @@ const Task = memo(() => {
12
12
13
13
//添加input框
14
14
const createElement = ( ) => {
15
- //创建input元素(在input前创建一个空的div站位)
16
15
const newInput = document . createElement ( "input" ) ;
17
- // const blank = document.createElement("div");
18
- // blank.className = "blank";
16
+
19
17
addInput . current . before ( newInput ) ;
20
- // newInput.before(blank);
21
18
} ;
22
19
23
20
const moveElement = ( element , value ) => {
24
- const newInput = document . createElement ( "input" ) ;
25
- newInput . value = value ;
21
+ const newInput = document . createElement ( "div" ) ;
22
+ const deleteBtn = document . createElement ( "div" ) ;
23
+ //创建删除按钮并添加事件
24
+ deleteBtn . className = "delete" ;
25
+ deleteBtn . innerHTML = "x" ;
26
+ deleteBtn . addEventListener ( "click" , btnDelete ) ;
27
+
28
+ newInput . className = "input" ;
29
+ newInput . innerHTML = value ;
30
+ newInput . appendChild ( deleteBtn ) ;
26
31
element . current . appendChild ( newInput ) ;
27
32
} ;
28
33
34
+ const btnDelete = ( e ) => {
35
+ // console.log(e.path[2].className);
36
+ if ( e . path [ 2 ] . className === "learning" ) {
37
+ learningRef . current . removeChild ( e . path [ 1 ] ) ;
38
+ } else if ( e . path [ 2 ] . className === "complete" ) {
39
+ completeRef . current . removeChild ( e . path [ 1 ] ) ;
40
+ }
41
+ } ;
42
+
29
43
// 添加新的study
30
44
const add = ( ) => {
31
45
createElement ( ) ;
32
46
//给增加的input增加监听事件
33
47
const currentInput = studyRef . current . childNodes [ listIndex ] ;
48
+ if ( listIndex < 0 ) {
49
+ setlistIndex ( 0 ) ;
50
+ }
51
+ setlistIndex ( listIndex + 1 ) ;
52
+ let diffX ; //与外层的距离(input)
53
+ let diffY ;
54
+ let currentX ; //input框原始的位置
55
+ let currentY ;
56
+ let mouseX ; //鼠标的位置
57
+ let mouseY ;
58
+ let taskX ;
59
+ let taskY ;
60
+ //move监听函数
61
+ const moveFn = ( e ) => {
62
+ //设置拖动时的位置(还要减去margin的偏移量)
63
+ currentInput . style . left = e . clientX - 20 - diffX + "px" ;
64
+ currentInput . style . top = e . clientY - diffY + "px" ;
65
+ mouseX = e . clientX ;
66
+ mouseY = e . clientY ;
34
67
35
- if ( currentInput ) {
36
- setlistIndex ( listIndex + 1 ) ;
37
- let diffX ; //与外层的距离(input)
38
- let diffY ;
39
- let currentX ; //input框原始的位置
40
- let currentY ;
41
- let mouseX ; //鼠标的位置
42
- let mouseY ;
43
- let taskX ;
44
- let taskY ;
45
- //move监听函数
46
- const moveFn = ( e ) => {
47
- //设置拖动时的位置(还要减去margin的偏移量)
48
- currentInput . style . left = e . clientX - 20 - diffX + "px" ;
49
- currentInput . style . top = e . clientY - diffY + "px" ;
50
- mouseX = e . clientX ;
51
- mouseY = e . clientY ;
52
-
53
- //当鼠标进入学习或完成框中,边框发生变化
54
- taskX = taskRef . current . offsetLeft ;
55
- taskY = taskRef . current . offsetTop ;
56
- if (
57
- mouseX > taskX - 100 &&
58
- mouseX < taskX + 100 &&
59
- mouseY < taskY + learningRef . current . scrollHeight / 2 &&
60
- mouseY > taskY - learningRef . current . scrollHeight / 2
61
- ) {
62
- // 改变边框
63
- learningRef . current . style . border = "2px solid #85eaff" ;
64
- } else {
65
- learningRef . current . style . border = "" ;
66
- }
67
- if (
68
- mouseX > taskX - 100 + learningRef . current . scrollWidth &&
69
- mouseX < taskX + 100 + learningRef . current . scrollWidth &&
70
- mouseY < taskY + completeRef . current . scrollHeight / 2 &&
71
- mouseY > taskY - completeRef . current . scrollHeight / 2
72
- ) {
73
- // 改变边框
74
- completeRef . current . style . border = "2px solid #74787c" ;
75
- } else {
76
- completeRef . current . style . border = "" ;
77
- }
68
+ //当鼠标进入学习或完成框中,边框发生变化
69
+ taskX = taskRef . current . offsetLeft ;
70
+ taskY = taskRef . current . offsetTop ;
71
+ if (
72
+ mouseX > taskX - 100 &&
73
+ mouseX < taskX + 100 &&
74
+ mouseY < taskY + learningRef . current . scrollHeight / 2 &&
75
+ mouseY > taskY - learningRef . current . scrollHeight / 2
76
+ ) {
77
+ // 改变边框
78
+ learningRef . current . style . border = "2px solid #85eaff" ;
79
+ } else {
80
+ learningRef . current . style . border = "" ;
81
+ }
82
+ if (
83
+ mouseX > taskX - 100 + learningRef . current . scrollWidth &&
84
+ mouseX < taskX + 100 + learningRef . current . scrollWidth &&
85
+ mouseY < taskY + completeRef . current . scrollHeight / 2 &&
86
+ mouseY > taskY - completeRef . current . scrollHeight / 2
87
+ ) {
88
+ // 改变边框
89
+ completeRef . current . style . border = "2px solid #74787c" ;
90
+ } else {
91
+ completeRef . current . style . border = "" ;
92
+ }
78
93
79
- // console.log(taskRef);
94
+ // console.log(taskRef);
80
95
81
- // console.log(mouseX, mouseY);
82
- } ;
96
+ // console.log(mouseX, mouseY);
97
+ } ;
83
98
84
- //mousedown监听函数
85
- const mouseDownFn = ( e ) => {
86
- //按住
99
+ //mousedown监听函数
100
+ const mouseDownFn = ( e ) => {
101
+ //按住
102
+
103
+ currentInput . style . position = "absolute" ;
104
+
105
+ diffX = e . clientX - currentInput . offsetLeft ;
106
+ diffY = e . clientY - currentInput . offsetTop ;
107
+ currentX = currentInput . style . left ;
108
+ currentY = currentInput . style . top ;
109
+
110
+ //moving
111
+ currentInput . addEventListener ( "mousemove" , moveFn ) ;
112
+ } ;
113
+
114
+ //mouseup
115
+ const mouseupFn = ( e ) => {
116
+ //松开的逻辑
117
+ // console.log(1231, e);
118
+ if (
119
+ mouseX > taskX - 100 &&
120
+ mouseX < taskX + 100 &&
121
+ mouseY < taskY + learningRef . current . scrollHeight / 2 &&
122
+ mouseY > taskY - learningRef . current . scrollHeight / 2
123
+ ) {
124
+ moveElement ( learningRef , e . srcElement . value ) ;
125
+ //销毁原来的input,并做相应的操作
126
+ studyRef . current . removeChild ( e . srcElement ) ;
127
+ setlistIndex ( listIndex - 1 ) ;
128
+ learningRef . current . style . border = "" ;
129
+ // console.log(123, e);
130
+ console . log ( 1231 , e . srcElement . value ) ;
131
+ // console.log(12312, listIndex);
132
+ }
133
+ if (
134
+ mouseX > taskX - 100 + learningRef . current . scrollWidth &&
135
+ mouseX < taskX + 100 + learningRef . current . scrollWidth &&
136
+ mouseY < taskY + completeRef . current . scrollHeight / 2 &&
137
+ mouseY > taskY - completeRef . current . scrollHeight / 2
138
+ ) {
139
+ moveElement ( completeRef , e . srcElement . value ) ;
140
+ studyRef . current . removeChild ( e . srcElement ) ;
141
+ setlistIndex ( listIndex - 1 ) ;
142
+
143
+ completeRef . current . style . border = "" ;
144
+ }
145
+
146
+ currentInput . style . position = "" ;
147
+ currentInput . style . left = currentX ;
148
+ currentInput . style . top = currentY ;
149
+
150
+ currentInput . removeEventListener ( "mousemove" , moveFn ) ;
151
+ } ;
152
+
153
+ //兼容bug//没有获取到当前新创建的input时
154
+ if ( currentInput ) {
155
+ //添加监听器
87
156
157
+ currentInput . addEventListener ( "mousedown" , mouseDownFn ) ;
158
+ currentInput . addEventListener ( "mouseup" , mouseupFn ) ;
159
+ } else {
160
+ console . log ( 231 , studyRef ) ;
161
+ studyRef . current . childNodes [ 0 ] . addEventListener ( "mousedown" , ( e ) => {
162
+ //按住
163
+ const currentInput = studyRef . current . childNodes [ 0 ] ;
88
164
currentInput . style . position = "absolute" ;
89
165
90
166
diffX = e . clientX - currentInput . offsetLeft ;
@@ -93,13 +169,48 @@ const Task = memo(() => {
93
169
currentY = currentInput . style . top ;
94
170
95
171
//moving
96
- currentInput . addEventListener ( "mousemove" , moveFn ) ;
97
- } ;
172
+ studyRef . current . childNodes [ 0 ] . addEventListener ( "mousemove" , ( e ) => {
173
+ const currentInput = studyRef . current . childNodes [ 0 ] ;
174
+ //设置拖动时的位置(还要减去margin的偏移量)
175
+ currentInput . style . left = e . clientX - 20 - diffX + "px" ;
176
+ currentInput . style . top = e . clientY - diffY + "px" ;
177
+ mouseX = e . clientX ;
178
+ mouseY = e . clientY ;
179
+
180
+ //当鼠标进入学习或完成框中,边框发生变化
181
+ taskX = taskRef . current . offsetLeft ;
182
+ taskY = taskRef . current . offsetTop ;
183
+ if (
184
+ mouseX > taskX - 100 &&
185
+ mouseX < taskX + 100 &&
186
+ mouseY < taskY + learningRef . current . scrollHeight / 2 &&
187
+ mouseY > taskY - learningRef . current . scrollHeight / 2
188
+ ) {
189
+ // 改变边框
190
+ learningRef . current . style . border = "2px solid #85eaff" ;
191
+ } else {
192
+ learningRef . current . style . border = "" ;
193
+ }
194
+ if (
195
+ mouseX > taskX - 100 + learningRef . current . scrollWidth &&
196
+ mouseX < taskX + 100 + learningRef . current . scrollWidth &&
197
+ mouseY < taskY + completeRef . current . scrollHeight / 2 &&
198
+ mouseY > taskY - completeRef . current . scrollHeight / 2
199
+ ) {
200
+ // 改变边框
201
+ completeRef . current . style . border = "2px solid #74787c" ;
202
+ } else {
203
+ completeRef . current . style . border = "" ;
204
+ }
98
205
99
- //mouseup
100
- const mouseupFn = ( e ) => {
206
+ // console.log(taskRef);
207
+
208
+ // console.log(mouseX, mouseY);
209
+ } ) ;
210
+ } ) ;
211
+ studyRef . current . childNodes [ 0 ] . addEventListener ( "mouseup" , ( e ) => {
101
212
//松开的逻辑
102
- // console.log(1231, e) ;
213
+ const currentInput = studyRef . current . childNodes [ 0 ] ;
103
214
if (
104
215
mouseX > taskX - 100 &&
105
216
mouseX < taskX + 100 &&
@@ -132,16 +243,40 @@ const Task = memo(() => {
132
243
currentInput . style . left = currentX ;
133
244
currentInput . style . top = currentY ;
134
245
135
- currentInput . removeEventListener ( "mousemove" , moveFn ) ;
136
- } ;
246
+ currentInput . removeEventListener ( "mousemove" , ( e ) => {
247
+ //设置拖动时的位置(还要减去margin的偏移量)
248
+ currentInput . style . left = e . clientX - 20 - diffX + "px" ;
249
+ currentInput . style . top = e . clientY - diffY + "px" ;
250
+ mouseX = e . clientX ;
251
+ mouseY = e . clientY ;
137
252
138
- //添加监听器
139
- currentInput . addEventListener ( "mousedown" , mouseDownFn ) ;
140
-
141
- currentInput . addEventListener ( "mouseup" , mouseupFn ) ;
142
- //关闭监听
143
- // currentInput.removeEventListener("mousedown", mouseDownFn);
144
- // currentInput.removeEventListener("mouseup", mouseupFn);
253
+ //当鼠标进入学习或完成框中,边框发生变化
254
+ taskX = taskRef . current . offsetLeft ;
255
+ taskY = taskRef . current . offsetTop ;
256
+ if (
257
+ mouseX > taskX - 100 &&
258
+ mouseX < taskX + 100 &&
259
+ mouseY < taskY + learningRef . current . scrollHeight / 2 &&
260
+ mouseY > taskY - learningRef . current . scrollHeight / 2
261
+ ) {
262
+ // 改变边框
263
+ learningRef . current . style . border = "2px solid #85eaff" ;
264
+ } else {
265
+ learningRef . current . style . border = "" ;
266
+ }
267
+ if (
268
+ mouseX > taskX - 100 + learningRef . current . scrollWidth &&
269
+ mouseX < taskX + 100 + learningRef . current . scrollWidth &&
270
+ mouseY < taskY + completeRef . current . scrollHeight / 2 &&
271
+ mouseY > taskY - completeRef . current . scrollHeight / 2
272
+ ) {
273
+ // 改变边框
274
+ completeRef . current . style . border = "2px solid #74787c" ;
275
+ } else {
276
+ completeRef . current . style . border = "" ;
277
+ }
278
+ } ) ;
279
+ } ) ;
145
280
}
146
281
} ;
147
282
0 commit comments