Skip to content

Commit 1511236

Browse files
committed
Simplify caret position restoration logic in PromptField and remove redundant task forwarding handler
1 parent a728d3f commit 1511236

3 files changed

Lines changed: 8 additions & 28 deletions

File tree

‎packages/ui/src/components/editor/panel/PromptField/PromptField.tsx‎

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -170,19 +170,10 @@ export const PromptField: React.FC<PromptFieldProps> = (props) => {
170170

171171
useEffect(() => {
172172
if (input_ref.current && input_ref.current.innerHTML !== highlighted_html) {
173-
const selection = window.getSelection()
174-
const should_restore_caret =
175-
document.activeElement === input_ref.current &&
176-
selection &&
177-
selection.rangeCount > 0 &&
178-
selection.anchorNode &&
179-
input_ref.current.contains(selection.anchorNode)
180-
181-
const selection_start = should_restore_caret
182-
? get_caret_position_from_div(input_ref.current)
183-
: 0
173+
const is_focused = document.activeElement === input_ref.current
174+
const selection_start = get_caret_position_from_div(input_ref.current)
184175
input_ref.current.innerHTML = highlighted_html
185-
if (should_restore_caret) {
176+
if (is_focused) {
186177
set_caret_position_for_div(input_ref.current, selection_start)
187178
}
188179
}

‎packages/vscode/src/views/panel/frontend/Home/Home.tsx‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,8 @@ export const Home: React.FC<Props> = (props) => {
5555
handle_change,
5656
handle_add,
5757
handle_add_subtask,
58-
handle_delete,
59-
handle_forward
60-
} = use_tasks(
61-
props.on_tasks_change,
62-
props.on_task_delete,
63-
props.on_task_forward
64-
)
58+
handle_delete
59+
} = use_tasks(props.on_tasks_change, props.on_task_delete)
6560

6661
const { is_timeline_reached, timeline_ref, handle_scroll_to_timeline } =
6762
use_timeline_scroll()
@@ -247,7 +242,7 @@ export const Home: React.FC<Props> = (props) => {
247242
handle_delete(workspace_root_folder, timestamp)
248243
}}
249244
on_forward={(text) => {
250-
handle_forward(text)
245+
props.on_task_forward(text)
251246
}}
252247
/>
253248
)}

‎packages/vscode/src/views/panel/frontend/Home/hooks/use-tasks.ts‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ const add_subtask_to_tree = (
3737

3838
export const use_tasks = (
3939
on_tasks_change: (root: string, tasks: Task[]) => void,
40-
on_task_delete: (root: string, timestamp: number) => void,
41-
on_task_forward: (text: string) => void
40+
on_task_delete: (root: string, timestamp: number) => void
4241
) => {
4342
const handle_reorder = (root: string, new_tasks: Task[]) => {
4443
on_tasks_change(root, new_tasks)
@@ -82,16 +81,11 @@ export const use_tasks = (
8281
on_task_delete(root, timestamp)
8382
}
8483

85-
const handle_forward = (text: string) => {
86-
on_task_forward(text)
87-
}
88-
8984
return {
9085
handle_reorder,
9186
handle_change,
9287
handle_add,
9388
handle_add_subtask,
94-
handle_delete,
95-
handle_forward
89+
handle_delete
9690
}
9791
}

0 commit comments

Comments
 (0)