1
- import React , { useMemo , useState , useRef } from 'react' ;
1
+ import React , { useMemo , useState , useRef , useEffect } from 'react' ;
2
2
import type { CSSProperties } from 'react' ;
3
3
import classnames from 'classnames' ;
4
- import type { ElementType , ElementOperations } from 'idraw' ;
4
+ import type { ElementType , ElementOperations , ElementPosition } from 'idraw' ;
5
5
import { Input } from 'antd' ;
6
+ import type { InputRef } from 'antd' ;
6
7
import IconVisible from '../../icons/visible' ;
7
8
import IconInvisible from '../../icons/invisible' ;
8
9
// import IconUnlock from '../../icons/unlock';
9
10
// import IconLock from '../../icons/lock';
10
- import IconCheck from '../../icons/check' ;
11
+ // import IconCheck from '../../icons/check';
11
12
import IconRect from '../../icons/rect' ;
12
13
import IconCircle from '../../icons/circle' ;
13
14
import IconText from '../../icons/text' ;
@@ -16,28 +17,31 @@ import IconStar from '../../icons/star';
16
17
import IconImage from '../../icons/image' ;
17
18
import IconPath from '../../icons/path' ;
18
19
import IconHTML from '../../icons/html' ;
19
- import IconEdit from '../../icons/edit' ;
20
+ // import IconEdit from '../../icons/edit';
20
21
// import IconDelete from '../../icons/delete';
21
22
22
23
import IconCloseCircle from '../../icons/close-circle' ;
23
24
24
25
const modName = 'node' ;
25
26
26
27
export interface TreeNodeProps {
28
+ uuid : string ;
27
29
nodeKey : string ;
28
30
title : string ;
29
- operatinos : ElementOperations ;
31
+ operations : ElementOperations ;
32
+ position : ElementPosition ;
30
33
className ?: string ;
31
34
type ?: ElementType ;
32
35
style ?: CSSProperties ;
33
36
getPrefixName : ( ...args : string [ ] ) => string ;
34
37
onTitleChange ?: ( e : { uuid : string ; value : string } ) => void ;
35
38
onOperationToggle ?: ( e : { uuid : string ; operations : ElementOperations } ) => void ;
36
39
onDelete ?: ( e : { uuid : string } ) => void ;
40
+ onSelect ?: ( e : { uuids : string [ ] ; positions : ElementPosition [ ] } ) => void ;
37
41
}
38
42
39
43
export const TreeNode = ( props : TreeNodeProps ) => {
40
- const { className, style, type, nodeKey, title, getPrefixName, onTitleChange, onOperationToggle, onDelete, operatinos } = props ;
44
+ const { className, style, type, uuid , nodeKey, title, position , getPrefixName, onTitleChange, onOperationToggle, onDelete, onSelect , operations } = props ;
41
45
const [ isEdit , setIsEdit ] = useState < boolean > ( false ) ;
42
46
const [ showAction , setShowAction ] = useState < boolean > ( false ) ;
43
47
const refTitle = useRef < string > ( title ) ;
@@ -48,6 +52,8 @@ export const TreeNode = (props: TreeNodeProps) => {
48
52
const titleInputClassName = getPrefixName ( modName , 'title' , 'input' ) ;
49
53
const titleIconClassName = getPrefixName ( modName , 'title' , 'icon' ) ;
50
54
const actionClassName = getPrefixName ( modName , 'action' ) ;
55
+ const clickTime = useRef < number > ( 0 ) ;
56
+ const refInput = useRef < InputRef | null > ( null ) ;
51
57
52
58
// useEffect(() => {
53
59
// refTitle.current = title;
@@ -71,15 +77,21 @@ export const TreeNode = (props: TreeNodeProps) => {
71
77
// setIsEdit(true);
72
78
// };
73
79
80
+ useEffect ( ( ) => {
81
+ if ( isEdit === true ) {
82
+ refInput . current ?. focus ( ) ;
83
+ }
84
+ } , [ isEdit ] ) ;
85
+
74
86
const onTitleInputBlur = ( e : React . FocusEvent < HTMLInputElement , Element > ) => {
75
87
setIsEdit ( false ) ;
76
88
onTitleChange ?.( { uuid : nodeKey , value : e . target . value || '' } ) ;
77
89
} ;
78
- const onTitleInputOk = ( e : any ) => {
79
- e . stopPropagation ( ) ;
80
- setIsEdit ( false ) ;
81
- onTitleChange ?.( { uuid : nodeKey , value : refTitle . current || '' } ) ;
82
- } ;
90
+ // const onTitleInputOk = (e: any) => {
91
+ // e.stopPropagation();
92
+ // setIsEdit(false);
93
+ // onTitleChange?.({ uuid: nodeKey, value: refTitle.current || '' });
94
+ // };
83
95
const onTitleInputChange = ( e : React . ChangeEvent < HTMLInputElement > ) => {
84
96
refTitle . current = e . target . value || '' ;
85
97
} ;
@@ -105,10 +117,26 @@ export const TreeNode = (props: TreeNodeProps) => {
105
117
setShowAction ( false ) ;
106
118
} ;
107
119
108
- const onClickToEdit = ( e : React . MouseEvent < HTMLElement , MouseEvent > ) => {
109
- e . stopPropagation ( ) ;
110
- e . preventDefault ( ) ;
111
- setIsEdit ( true ) ;
120
+ // const onClickToEdit = (e: React.MouseEvent<HTMLElement, MouseEvent>) => {
121
+ // e.stopPropagation();
122
+ // e.preventDefault();
123
+ // setIsEdit(true);
124
+ // };
125
+
126
+ const onClickTitle = ( e : React . MouseEvent < HTMLElement , MouseEvent > ) => {
127
+ const nowTime = Date . now ( ) ;
128
+ const countTime = nowTime - clickTime . current ;
129
+ clickTime . current = nowTime ;
130
+
131
+ onSelect ?.( {
132
+ uuids : [ uuid ] ,
133
+ positions : [ position ]
134
+ } ) ;
135
+ if ( countTime <= 300 && countTime > 0 ) {
136
+ e . stopPropagation ( ) ;
137
+ e . preventDefault ( ) ;
138
+ setIsEdit ( true ) ;
139
+ }
112
140
} ;
113
141
114
142
const onClickToDelete = ( e : React . MouseEvent < HTMLElement , MouseEvent > ) => {
@@ -125,7 +153,7 @@ export const TreeNode = (props: TreeNodeProps) => {
125
153
onOperationToggle ?.( {
126
154
uuid : nodeKey ,
127
155
operations : {
128
- invisible : ! operatinos . invisible
156
+ invisible : ! operations . invisible
129
157
}
130
158
} ) ;
131
159
} ;
@@ -136,7 +164,7 @@ export const TreeNode = (props: TreeNodeProps) => {
136
164
// onOperationToggle?.({
137
165
// uuid: nodeKey,
138
166
// operations: {
139
- // lock: !operatinos .lock
167
+ // lock: !operations .lock
140
168
// }
141
169
// });
142
170
// };
@@ -165,42 +193,50 @@ export const TreeNode = (props: TreeNodeProps) => {
165
193
}
166
194
167
195
return (
168
- < span key = { nodeKey } style = { style } className = { classnames ( rootClassName , className ) } onMouseOver = { onNodeMouseOver } onMouseLeave = { onNodeMouseLeave } >
196
+ < span
197
+ key = { nodeKey }
198
+ style = { style }
199
+ className = { classnames ( rootClassName , className ) }
200
+ onClick = { onClickTitle }
201
+ onMouseOver = { onNodeMouseOver }
202
+ onMouseLeave = { onNodeMouseLeave }
203
+ >
169
204
< span className = { titleClassName } >
170
205
{ getIcon ( type ) }
171
206
< span > { title } </ span >
172
207
</ span >
173
208
{ showAction && (
174
209
< span className = { actionClassName } >
175
- { operatinos . invisible ? (
210
+ { operations . invisible ? (
176
211
< IconInvisible className = { iconClassName } onClick = { onClickToggleVisible } />
177
212
) : (
178
213
< IconVisible className = { iconClassName } onClick = { onClickToggleVisible } />
179
214
) }
180
- { /* {operatinos .lock ? (
215
+ { /* {operations .lock ? (
181
216
<IconLock className={iconClassName} onClick={onClickToggleLock} />
182
217
) : (
183
218
<IconUnlock className={iconClassName} onClick={onClickToggleLock} />
184
219
)} */ }
185
- < IconEdit className = { iconClassName } onClick = { onClickToEdit } />
220
+ { /* <IconEdit className={iconClassName} onClick={onClickToEdit} /> */ }
186
221
< IconCloseCircle className = { iconClassName } onClick = { onClickToDelete } />
187
222
</ span >
188
223
) }
189
224
190
225
{ isEdit && (
191
226
< span className = { titleInputClassName } >
192
227
< Input
228
+ ref = { refInput }
193
229
size = "small"
194
230
defaultValue = { title }
195
231
onBlur = { onTitleInputBlur }
196
232
onClick = { onTitleInputClick }
197
233
onKeyDown = { onTitleInputKeyDown }
198
234
onChange = { onTitleInputChange }
199
- addonAfter = { < IconCheck onClick = { onTitleInputOk } /> }
235
+ // addonAfter={<IconCheck onClick={onTitleInputOk} />}
200
236
/>
201
237
</ span >
202
238
) }
203
239
</ span >
204
240
) ;
205
- } , [ nodeKey , title , isEdit , type , showAction , operatinos ] ) ;
241
+ } , [ nodeKey , title , isEdit , type , showAction , operations ] ) ;
206
242
} ;
0 commit comments