@@ -24,50 +24,47 @@ import styles from './HostsEditor.less'
24
24
25
25
modeHosts ( )
26
26
27
- interface Props {
28
- hosts : {
29
- id : string
30
- content ?: string
31
- }
32
- }
27
+ interface Props { }
33
28
34
29
const HostsEditor = ( props : Props ) => {
35
- const { hosts } = props
36
- const { hosts_data, isReadOnly } = useModel ( 'useHostsData' )
37
- const [ hosts_id , setHostsId ] = useState ( hosts . id )
38
- const [ content , setContent ] = useState ( hosts . content || '' )
30
+ const { current_hosts, hosts_data, isReadOnly } = useModel ( 'useHostsData' )
31
+ const [ hosts_id , setHostsId ] = useState ( current_hosts ?. id || '0' )
32
+ const [ content , setContent ] = useState ( '' )
39
33
const [ is_read_only , setIsReadOnly ] = useState ( true )
40
- const [ cm_editor , setCMEditor ] =
41
- useState < CodeMirror . EditorFromTextArea | null > ( null )
42
- const el_ref = useRef < HTMLTextAreaElement > ( null )
43
- const [ find_params , setFindParams ] = useState < IFindShowSourceParam | null > (
44
- null ,
45
- )
46
-
47
- const loadContent = async ( ) => {
48
- if ( ! cm_editor ) return
34
+ const [ find_params , setFindParams ] = useState < IFindShowSourceParam | null > ( null )
35
+ const ref_el = useRef < HTMLTextAreaElement > ( null )
36
+ const ref_cm = useRef < CodeMirror . EditorFromTextArea | null > ( null )
37
+
38
+ const loadContent = async ( is_new = false ) => {
39
+ let cm_editor = ref_cm . current
40
+ if ( ! cm_editor ) {
41
+ setTimeout ( loadContent , 100 )
42
+ return
43
+ }
49
44
50
45
let content =
51
- hosts . id === '0'
52
- ? await actions . getSystemHosts ( )
53
- : await actions . getHostsContent ( hosts . id )
46
+ hosts_id === '0' ? await actions . getSystemHosts ( ) : await actions . getHostsContent ( hosts_id )
54
47
setContent ( content )
55
48
cm_editor . setValue ( content )
56
- cm_editor . setOption ( 'readOnly' , isReadOnly ( hosts ) )
49
+ if ( is_new ) {
50
+ cm_editor . clearHistory ( )
51
+ }
57
52
}
58
53
59
54
useEffect ( ( ) => {
60
- loadContent ( ) . catch ( ( e ) => console . error ( e ) )
61
- } , [ hosts_id , cm_editor ] )
62
-
63
- useEffect ( ( ) => {
64
- if ( cm_editor && hosts_id !== hosts . id ) {
65
- setTimeout ( ( ) => cm_editor . clearHistory ( ) , 300 )
55
+ // console.log(current_hosts )
56
+ setHostsId ( current_hosts ?. id || '0' )
57
+ let is_readonly = isReadOnly ( current_hosts )
58
+ setIsReadOnly ( is_readonly )
59
+ if ( ref_cm . current ) {
60
+ ref_cm . current . setOption ( 'readOnly' , is_readonly )
66
61
}
62
+ } , [ current_hosts ] )
67
63
68
- setHostsId ( hosts . id )
69
- setIsReadOnly ( isReadOnly ( hosts ) )
70
- } , [ hosts ] )
64
+ useEffect ( ( ) => {
65
+ console . log ( hosts_id )
66
+ loadContent ( true ) . catch ( ( e ) => console . error ( e ) )
67
+ } , [ hosts_id ] )
71
68
72
69
const { run : toSave } = useDebounceFn (
73
70
( id : string , content : string ) => {
@@ -81,10 +78,11 @@ const HostsEditor = (props: Props) => {
81
78
82
79
const onChange = ( content : string ) => {
83
80
setContent ( content )
84
- toSave ( hosts . id , content )
81
+ toSave ( hosts_id , content )
85
82
}
86
83
87
84
const toggleComment = ( ) => {
85
+ let cm_editor = ref_cm . current
88
86
if ( is_read_only || ! cm_editor ) return
89
87
cm_editor . toggleComment ( )
90
88
@@ -95,6 +93,7 @@ const HostsEditor = (props: Props) => {
95
93
}
96
94
97
95
const onGutterClick = ( n : number ) => {
96
+ let cm_editor = ref_cm . current
98
97
if ( is_read_only || ! cm_editor ) return
99
98
100
99
let info = cm_editor . lineInfo ( n )
@@ -110,22 +109,18 @@ const HostsEditor = (props: Props) => {
110
109
111
110
cm_editor
112
111
. getDoc ( )
113
- . replaceRange (
114
- new_line ,
115
- { line : info . line , ch : 0 } ,
116
- { line : info . line , ch : line . length } ,
117
- )
112
+ . replaceRange ( new_line , { line : info . line , ch : 0 } , { line : info . line , ch : line . length } )
118
113
}
119
114
120
115
useEffect ( ( ) => {
121
- if ( ! el_ref . current ) return
116
+ if ( ! ref_el . current ) return
122
117
123
- let cm = CodeMirror . fromTextArea ( el_ref . current , {
118
+ let cm = CodeMirror . fromTextArea ( ref_el . current , {
124
119
lineNumbers : true ,
125
120
readOnly : is_read_only ,
126
121
mode : 'hosts' ,
127
122
} )
128
- setCMEditor ( cm )
123
+ ref_cm . current = cm
129
124
130
125
cm . setSize ( '100%' , '100%' )
131
126
@@ -140,59 +135,63 @@ const HostsEditor = (props: Props) => {
140
135
} , [ ] )
141
136
142
137
useEffect ( ( ) => {
143
- if ( find_params && find_params . item_id === hosts . id ) {
144
- setSelection ( find_params ) . catch ( ( e ) => console . error ( e ) )
138
+ if ( find_params && find_params . item_id === hosts_id ) {
139
+ setSelection ( find_params , true ) . catch ( ( e ) => console . error ( e ) )
145
140
}
146
- } , [ hosts , find_params ] )
141
+ } , [ hosts_id , find_params ] )
147
142
148
143
useOnBroadcast (
149
144
events . editor_content_change ,
150
145
( new_content : string ) => {
151
146
if ( new_content === content ) return
152
147
onChange ( new_content )
153
148
} ,
154
- [ hosts , hosts_id , content ] ,
149
+ [ hosts_id , content ] ,
155
150
)
156
151
157
- useOnBroadcast ( events . editor_gutter_click , onGutterClick , [
158
- cm_editor ,
159
- is_read_only ,
160
- ] )
161
-
162
152
useOnBroadcast (
163
153
events . hosts_refreshed ,
164
154
( h : IHostsListObject ) => {
165
- if ( hosts . id !== '0' && h . id !== hosts . id ) return
155
+ if ( hosts_id !== '0' && h . id !== hosts_id ) return
166
156
loadContent ( ) . catch ( ( e ) => console . error ( e ) )
167
157
} ,
168
- [ hosts , hosts_data , cm_editor ] ,
158
+ [ hosts_id ] ,
169
159
)
170
160
171
161
useOnBroadcast (
172
162
events . hosts_refreshed_by_id ,
173
163
( id : string ) => {
174
- if ( hosts . id !== '0' && id !== hosts . id ) return
164
+ if ( hosts_id !== '0' && hosts_id !== id ) return
175
165
loadContent ( ) . catch ( ( e ) => console . error ( e ) )
176
166
} ,
177
- [ hosts , hosts_data , cm_editor ] ,
167
+ [ hosts_id , hosts_data ] ,
178
168
)
179
169
180
- useOnBroadcast ( events . toggle_comment , toggleComment , [
181
- cm_editor ,
182
- is_read_only ,
183
- ] )
184
-
185
170
useOnBroadcast (
186
171
events . set_hosts_on_status ,
187
172
( ) => {
188
- if ( hosts . id === '0' ) {
173
+ if ( hosts_id === '0' ) {
174
+ loadContent ( ) . catch ( ( e ) => console . error ( e ) )
175
+ }
176
+ } ,
177
+ [ hosts_id ] ,
178
+ )
179
+
180
+ useOnBroadcast (
181
+ events . system_hosts_updated ,
182
+ ( ) => {
183
+ if ( hosts_id === '0' ) {
189
184
loadContent ( ) . catch ( ( e ) => console . error ( e ) )
190
185
}
191
186
} ,
192
- [ hosts , cm_editor ] ,
187
+ [ hosts_id ] ,
193
188
)
194
189
195
- const setSelection = async ( params : IFindShowSourceParam ) => {
190
+ useOnBroadcast ( events . editor_gutter_click , onGutterClick , [ is_read_only ] )
191
+ useOnBroadcast ( events . toggle_comment , toggleComment , [ is_read_only ] )
192
+
193
+ const setSelection = async ( params : IFindShowSourceParam , repeat = false ) => {
194
+ let cm_editor = ref_cm . current
196
195
if ( ! cm_editor ) return
197
196
let doc = cm_editor . getDoc ( )
198
197
@@ -218,9 +217,9 @@ const HostsEditor = (props: Props) => {
218
217
useOnBroadcast (
219
218
events . show_source ,
220
219
async ( params : IFindShowSourceParam ) => {
221
- if ( ! cm_editor ) return
220
+ if ( ! ref_cm . current ) return
222
221
223
- if ( params . item_id !== hosts . id ) {
222
+ if ( params . item_id !== hosts_id ) {
224
223
setFindParams ( params )
225
224
setTimeout ( ( ) => {
226
225
setFindParams ( null )
@@ -230,16 +229,14 @@ const HostsEditor = (props: Props) => {
230
229
231
230
setSelection ( params ) . catch ( ( e ) => console . error ( e ) )
232
231
} ,
233
- [ hosts , cm_editor ] ,
232
+ [ hosts_id ] ,
234
233
)
235
234
236
235
return (
237
236
< div className = { styles . root } >
238
- < div
239
- className = { clsx ( styles . editor , is_read_only && styles . read_only_tag ) }
240
- >
237
+ < div className = { clsx ( styles . editor , is_read_only && styles . read_only ) } >
241
238
< textarea
242
- ref = { el_ref }
239
+ ref = { ref_el }
243
240
defaultValue = { content }
244
241
// onChange={e => onChange(e.target.value)}
245
242
// disabled={is_read_only}
0 commit comments