|
1 | | -import { map_display_pos_to_raw_pos } from '../../../shared/symbols' |
| 1 | +import { get_raw_selection_bounds } from '../../../shared/symbols' |
2 | 2 | import { HandlerContext } from './types' |
3 | 3 | import { get_symbol_ranges, set_caret_position_after_change } from './utils' |
4 | 4 |
|
@@ -243,43 +243,29 @@ export const create_handle_key_down = ( |
243 | 243 | return |
244 | 244 | } |
245 | 245 |
|
246 | | - const selection = window.getSelection() |
247 | | - if ( |
248 | | - selection && |
249 | | - selection.rangeCount > 0 && |
250 | | - !selection.isCollapsed && |
251 | | - params.input_ref.current?.contains(selection.getRangeAt(0).startContainer) |
252 | | - ) { |
253 | | - e.preventDefault() |
254 | | - const range = selection.getRangeAt(0) |
255 | | - const input_element = params.input_ref.current |
256 | | - |
257 | | - const pre_selection_range = document.createRange() |
258 | | - pre_selection_range.selectNodeContents(input_element) |
259 | | - pre_selection_range.setEnd(range.startContainer, range.startOffset) |
260 | | - const display_start = pre_selection_range.toString().length |
261 | | - |
262 | | - pre_selection_range.setEnd(range.endContainer, range.endOffset) |
263 | | - const display_end = pre_selection_range.toString().length |
264 | | - |
265 | | - const raw_start = map_display_pos_to_raw_pos({ |
266 | | - display_pos: display_start, |
267 | | - raw_text: props.value, |
268 | | - context_file_paths: props.selected_files ?? [] |
269 | | - }) |
270 | | - const raw_end = map_display_pos_to_raw_pos({ |
271 | | - display_pos: display_end, |
272 | | - raw_text: props.value, |
273 | | - context_file_paths: props.selected_files ?? [] |
274 | | - }) |
275 | | - |
276 | | - const new_value = |
277 | | - props.value.substring(0, raw_start) + props.value.substring(raw_end) |
| 246 | + const selection = window.getSelection() |
| 247 | + if ( |
| 248 | + selection && |
| 249 | + selection.rangeCount > 0 && |
| 250 | + !selection.isCollapsed && |
| 251 | + params.input_ref.current?.contains(selection.getRangeAt(0).startContainer) |
| 252 | + ) { |
| 253 | + e.preventDefault() |
| 254 | + const input_element = params.input_ref.current! |
| 255 | + const bounds = get_raw_selection_bounds({ |
| 256 | + input_element, |
| 257 | + raw_text: props.value, |
| 258 | + context_file_paths: props.selected_files ?? [] |
| 259 | + }) |
| 260 | + if (bounds) { |
| 261 | + const new_value = |
| 262 | + props.value.substring(0, bounds.raw_start) + props.value.substring(bounds.raw_end) |
278 | 263 |
|
279 | | - refs.has_modified_current_entry_ref.current = true |
280 | | - utils.update_value(new_value, raw_start) |
281 | | - return |
282 | | - } |
| 264 | + refs.has_modified_current_entry_ref.current = true |
| 265 | + utils.update_value(new_value, bounds.raw_start) |
| 266 | + } |
| 267 | + return |
| 268 | + } |
283 | 269 |
|
284 | 270 | if (selection?.isCollapsed && refs.raw_caret_pos_ref.current === 0) { |
285 | 271 | e.preventDefault() |
@@ -377,41 +363,22 @@ export const create_handle_key_down = ( |
377 | 363 | } |
378 | 364 | } else { |
379 | 365 | e.preventDefault() |
380 | | - const selection = window.getSelection() |
381 | | - if (!selection || !selection.rangeCount || !params.input_ref.current) |
382 | | - return |
383 | | - |
384 | | - const range = selection.getRangeAt(0) |
385 | 366 | const input_element = params.input_ref.current |
386 | | - |
387 | | - if (!input_element.contains(range.startContainer)) return |
388 | | - |
389 | | - const pre_selection_range = document.createRange() |
390 | | - pre_selection_range.selectNodeContents(input_element) |
391 | | - pre_selection_range.setEnd(range.startContainer, range.startOffset) |
392 | | - const display_start = pre_selection_range.toString().length |
393 | | - |
394 | | - pre_selection_range.setEnd(range.endContainer, range.endOffset) |
395 | | - const display_end = pre_selection_range.toString().length |
396 | | - |
397 | | - const raw_start = map_display_pos_to_raw_pos({ |
398 | | - display_pos: display_start, |
399 | | - raw_text: props.value, |
400 | | - context_file_paths: props.selected_files ?? [] |
401 | | - }) |
402 | | - const raw_end = map_display_pos_to_raw_pos({ |
403 | | - display_pos: display_end, |
| 367 | + if (!input_element) return |
| 368 | + const bounds = get_raw_selection_bounds({ |
| 369 | + input_element, |
404 | 370 | raw_text: props.value, |
405 | 371 | context_file_paths: props.selected_files ?? [] |
406 | 372 | }) |
| 373 | + if (!bounds) return |
407 | 374 |
|
408 | 375 | const new_value = |
409 | | - props.value.substring(0, raw_start) + |
| 376 | + props.value.substring(0, bounds.raw_start) + |
410 | 377 | '\n' + |
411 | | - props.value.substring(raw_end) |
| 378 | + props.value.substring(bounds.raw_end) |
412 | 379 |
|
413 | 380 | refs.has_modified_current_entry_ref.current = true |
414 | | - utils.update_value(new_value, raw_start + 1) |
| 381 | + utils.update_value(new_value, bounds.raw_start + 1) |
415 | 382 | } |
416 | 383 | } |
417 | 384 |
|
|
0 commit comments