Skip to content

Commit b6e5051

Browse files
committed
Merge branch 'feature/editor-ref' into develop
2 parents 2643723 + 5bc54d9 commit b6e5051

File tree

8 files changed

+93
-165
lines changed

8 files changed

+93
-165
lines changed

.prettierrc.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"insertPragma": false,
88
"jsxBracketSameLine": false,
99
"jsxSingleQuote": false,
10-
"printWidth": 80,
10+
"printWidth": 100,
1111
"proseWrap": "preserve",
1212
"quoteProps": "as-needed",
1313
"requirePragma": false,

src/main/actions/hosts/refresh.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ export default async (hosts_id: string): Promise<IOperationResult> => {
4848
try {
4949
console.log(`-> refreshHosts URL: "${url}"`)
5050
if (url.startsWith('file://')) {
51-
new_content = await fs.promises.readFile(new URL(url) ,'utf-8')
51+
new_content = await fs.promises.readFile(new URL(url), 'utf-8')
5252
} else {
5353
let resp = await GET(url)
5454
new_content = resp.data
5555
}
56-
} catch (e) {
56+
} catch (e: any) {
5757
console.error(e)
5858
return {
5959
success: false,

src/renderer/components/Editor/HostsEditor.tsx

+66-69
Original file line numberDiff line numberDiff line change
@@ -24,50 +24,47 @@ import styles from './HostsEditor.less'
2424

2525
modeHosts()
2626

27-
interface Props {
28-
hosts: {
29-
id: string
30-
content?: string
31-
}
32-
}
27+
interface Props {}
3328

3429
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('')
3933
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+
}
4944

5045
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)
5447
setContent(content)
5548
cm_editor.setValue(content)
56-
cm_editor.setOption('readOnly', isReadOnly(hosts))
49+
if (is_new) {
50+
cm_editor.clearHistory()
51+
}
5752
}
5853

5954
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)
6661
}
62+
}, [current_hosts])
6763

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])
7168

7269
const { run: toSave } = useDebounceFn(
7370
(id: string, content: string) => {
@@ -81,10 +78,11 @@ const HostsEditor = (props: Props) => {
8178

8279
const onChange = (content: string) => {
8380
setContent(content)
84-
toSave(hosts.id, content)
81+
toSave(hosts_id, content)
8582
}
8683

8784
const toggleComment = () => {
85+
let cm_editor = ref_cm.current
8886
if (is_read_only || !cm_editor) return
8987
cm_editor.toggleComment()
9088

@@ -95,6 +93,7 @@ const HostsEditor = (props: Props) => {
9593
}
9694

9795
const onGutterClick = (n: number) => {
96+
let cm_editor = ref_cm.current
9897
if (is_read_only || !cm_editor) return
9998

10099
let info = cm_editor.lineInfo(n)
@@ -110,22 +109,18 @@ const HostsEditor = (props: Props) => {
110109

111110
cm_editor
112111
.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 })
118113
}
119114

120115
useEffect(() => {
121-
if (!el_ref.current) return
116+
if (!ref_el.current) return
122117

123-
let cm = CodeMirror.fromTextArea(el_ref.current, {
118+
let cm = CodeMirror.fromTextArea(ref_el.current, {
124119
lineNumbers: true,
125120
readOnly: is_read_only,
126121
mode: 'hosts',
127122
})
128-
setCMEditor(cm)
123+
ref_cm.current = cm
129124

130125
cm.setSize('100%', '100%')
131126

@@ -140,59 +135,63 @@ const HostsEditor = (props: Props) => {
140135
}, [])
141136

142137
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))
145140
}
146-
}, [hosts, find_params])
141+
}, [hosts_id, find_params])
147142

148143
useOnBroadcast(
149144
events.editor_content_change,
150145
(new_content: string) => {
151146
if (new_content === content) return
152147
onChange(new_content)
153148
},
154-
[hosts, hosts_id, content],
149+
[hosts_id, content],
155150
)
156151

157-
useOnBroadcast(events.editor_gutter_click, onGutterClick, [
158-
cm_editor,
159-
is_read_only,
160-
])
161-
162152
useOnBroadcast(
163153
events.hosts_refreshed,
164154
(h: IHostsListObject) => {
165-
if (hosts.id !== '0' && h.id !== hosts.id) return
155+
if (hosts_id !== '0' && h.id !== hosts_id) return
166156
loadContent().catch((e) => console.error(e))
167157
},
168-
[hosts, hosts_data, cm_editor],
158+
[hosts_id],
169159
)
170160

171161
useOnBroadcast(
172162
events.hosts_refreshed_by_id,
173163
(id: string) => {
174-
if (hosts.id !== '0' && id !== hosts.id) return
164+
if (hosts_id !== '0' && hosts_id !== id) return
175165
loadContent().catch((e) => console.error(e))
176166
},
177-
[hosts, hosts_data, cm_editor],
167+
[hosts_id, hosts_data],
178168
)
179169

180-
useOnBroadcast(events.toggle_comment, toggleComment, [
181-
cm_editor,
182-
is_read_only,
183-
])
184-
185170
useOnBroadcast(
186171
events.set_hosts_on_status,
187172
() => {
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') {
189184
loadContent().catch((e) => console.error(e))
190185
}
191186
},
192-
[hosts, cm_editor],
187+
[hosts_id],
193188
)
194189

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
196195
if (!cm_editor) return
197196
let doc = cm_editor.getDoc()
198197

@@ -218,9 +217,9 @@ const HostsEditor = (props: Props) => {
218217
useOnBroadcast(
219218
events.show_source,
220219
async (params: IFindShowSourceParam) => {
221-
if (!cm_editor) return
220+
if (!ref_cm.current) return
222221

223-
if (params.item_id !== hosts.id) {
222+
if (params.item_id !== hosts_id) {
224223
setFindParams(params)
225224
setTimeout(() => {
226225
setFindParams(null)
@@ -230,16 +229,14 @@ const HostsEditor = (props: Props) => {
230229

231230
setSelection(params).catch((e) => console.error(e))
232231
},
233-
[hosts, cm_editor],
232+
[hosts_id],
234233
)
235234

236235
return (
237236
<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)}>
241238
<textarea
242-
ref={el_ref}
239+
ref={ref_el}
243240
defaultValue={content}
244241
// onChange={e => onChange(e.target.value)}
245242
// disabled={is_read_only}

src/renderer/components/List/ListItem.tsx

+5-18
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ interface Props {
2929
const ListItem = (props: Props) => {
3030
const { data, is_tray, selected_ids } = props
3131
const { lang, i18n } = useModel('useI18n')
32-
const { hosts_data, setList, current_hosts, setCurrentHosts } =
33-
useModel('useHostsData')
32+
const { hosts_data, setList, current_hosts, setCurrentHosts } = useModel('useHostsData')
3433
const [is_collapsed, setIsCollapsed] = useState(!!data.is_collapsed)
3534
const [is_on, setIsOn] = useState(data.on)
3635
const el = useRef<HTMLDivElement>(null)
@@ -86,11 +85,7 @@ const ListItem = (props: Props) => {
8685

8786
return (
8887
<div
89-
className={clsx(
90-
styles.root,
91-
is_selected && styles.selected,
92-
is_tray && styles.is_tray,
93-
)}
88+
className={clsx(styles.root, is_selected && styles.selected, is_tray && styles.is_tray)}
9489
// className={clsx(styles.item, is_selected && styles.selected, is_collapsed && styles.is_collapsed)}
9590
// style={{ paddingLeft: `${1.3 * level}em` }}
9691
onContextMenu={(e) => {
@@ -155,9 +150,7 @@ const ListItem = (props: Props) => {
155150
label:
156151
deal_count === 1
157152
? lang.move_to_trashcan
158-
: i18n.trans('move_items_to_trashcan', [
159-
deal_count.toLocaleString(),
160-
]),
153+
: i18n.trans('move_items_to_trashcan', [deal_count.toLocaleString()]),
161154
click() {
162155
let ids = deal_count === 1 ? [data.id] : selected_ids
163156
agent.broadcast(events.move_to_trashcan, ids)
@@ -184,14 +177,8 @@ const ListItem = (props: Props) => {
184177
}}
185178
>
186179
<div className={styles.title} onClick={onSelect}>
187-
<span
188-
className={clsx(styles.icon, is_folder && styles.folder)}
189-
onClick={toggleIsCollapsed}
190-
>
191-
<ItemIcon
192-
type={data.is_sys ? 'system' : data.type}
193-
is_collapsed={data.is_collapsed}
194-
/>
180+
<span className={clsx(styles.icon, is_folder && styles.folder)} onClick={toggleIsCollapsed}>
181+
<ItemIcon type={data.is_sys ? 'system' : data.type} is_collapsed={data.is_collapsed} />
195182
</span>
196183
{data.title || lang.untitled}
197184
</div>

0 commit comments

Comments
 (0)