You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 2-ui/99-ui-misc/02-selection-range/article.md
+10-12
Original file line number
Diff line number
Diff line change
@@ -445,8 +445,8 @@ Methods:
445
445
The last argument, `selectionMode`, determines how the selection will be set after the text has been replaced. The possible values are:
446
446
447
447
-`"select"` -- the newly inserted text will be selected.
448
-
-`"start"` -- the selection range collapses just before the inserted text.
449
-
-`"end"` -- the selection range collapses just after the inserted text.
448
+
-`"start"` -- the selection range collapses just before the inserted text (the cursor will be immediately before it).
449
+
-`"end"` -- the selection range collapses just after the inserted text (the cursor will be right after it).
450
450
-`"preserve"` -- attempts to preserve the selection. This is the default.
451
451
452
452
Now let's see these methods in action.
@@ -504,7 +504,7 @@ Focus on me, the cursor will be at position 10.
504
504
505
505
### Example: modifying selection
506
506
507
-
To modify the content of the selection, we can use `input.setRangeText`. Of course, we can read `selectionStart/End` and can just change `value`, but `setRangeText` is more powerful.
507
+
To modify the content of the selection, we can use `input.setRangeText`. Of course, we can read `selectionStart/End` and, with the knowledge of the selection, change the corresponding substring of `value`, but `setRangeText` is more powerful and often more convenient.
508
508
509
509
That's a somewhat complex method. In its simplest one-argument form it replaces the user selected range and removes the selection.
510
510
@@ -551,7 +551,7 @@ If nothing is selected, or we use equal `start` and `end` in `setRangeText`, the
551
551
552
552
We can also insert something "at the cursor" using `setRangeText`.
553
553
554
-
Here's an button that inserts `"HELLO"` at the cursor position and puts the cursor immediately after it. If the selection is not empty, then it gets replaced (we can do detect in by comparing `selectionStart=selectionEnd` and do something else instead):
554
+
Here's an button that inserts `"HELLO"` at the cursor position and puts the cursor immediately after it. If the selection is not empty, then it gets replaced (we can do detect in by comparing `selectionStart!=selectionEnd` and do something else instead):
555
555
556
556
```html run autorun
557
557
<inputid="input"style="width:200px"value="Text Text Text Text Text">
@@ -598,9 +598,7 @@ To make something unselectable, there are three ways:
598
598
599
599
This prevents starting the selection on `elem`, but the visitor may start it at another element, then extend to `elem`.
600
600
601
-
That's convenient when there's another event handler on the same action that triggers the select. So we disable the selection to avoid conflict.
602
-
603
-
And `elem` contents still be copied.
601
+
That's convenient when there's another event handler on the same action that triggers the select (e.g. `mousedown`). So we disable the selection to avoid conflict, still allowing `elem` contents to be copied.
604
602
605
603
3. We can also clear the selection post-factum after it happens with `document.getSelection().empty()`. That's rarely used, as this causes unwanted blinking as the selection appears-disappears.
606
604
@@ -626,8 +624,10 @@ The most used recipes are probably:
626
624
```js run
627
625
let selection = document.getSelection();
628
626
627
+
let cloned = /* element to clone the selected nodes to */;
628
+
629
629
// then apply Range methods to selection.getRangeAt(0)
630
-
// orto all ranges if supporting multi-select
630
+
// or, like here, to all ranges to support multi-select
@@ -639,11 +639,9 @@ The most used recipes are probably:
639
639
// directly:
640
640
selection.setBaseAndExtent(...from...to...);
641
641
642
-
// or create range and:
642
+
// or we can create a range and:
643
643
selection.removeAllRanges();
644
644
selection.addRange(range);
645
645
```
646
646
647
-
Another important thing to know about selection: the cursor position in editable elements, like `<textarea>` is always at the start or the end of the selection.
648
-
649
-
We can use it both to get cursor position and to move the cursor by setting `elem.selectionStart` and `elem.selectionEnd`.
647
+
And finally, about the cursor. The cursor position in editable elements, like `<textarea>` is always at the start or the end of the selection. We can use it to get cursor position or to move the cursor by setting `elem.selectionStart` and `elem.selectionEnd`.
0 commit comments