Skip to content

Commit c4a6c4f

Browse files
committed
minor
1 parent bd9eb92 commit c4a6c4f

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

2-ui/99-ui-misc/02-selection-range/article.md

+10-12
Original file line numberDiff line numberDiff line change
@@ -445,8 +445,8 @@ Methods:
445445
The last argument, `selectionMode`, determines how the selection will be set after the text has been replaced. The possible values are:
446446

447447
- `"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).
450450
- `"preserve"` -- attempts to preserve the selection. This is the default.
451451

452452
Now let's see these methods in action.
@@ -504,7 +504,7 @@ Focus on me, the cursor will be at position 10.
504504

505505
### Example: modifying selection
506506

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.
508508

509509
That's a somewhat complex method. In its simplest one-argument form it replaces the user selected range and removes the selection.
510510

@@ -551,7 +551,7 @@ If nothing is selected, or we use equal `start` and `end` in `setRangeText`, the
551551

552552
We can also insert something "at the cursor" using `setRangeText`.
553553

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):
555555

556556
```html run autorun
557557
<input id="input" style="width:200px" value="Text Text Text Text Text">
@@ -598,9 +598,7 @@ To make something unselectable, there are three ways:
598598

599599
This prevents starting the selection on `elem`, but the visitor may start it at another element, then extend to `elem`.
600600

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.
604602

605603
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.
606604

@@ -626,8 +624,10 @@ The most used recipes are probably:
626624
```js run
627625
let selection = document.getSelection();
628626

627+
let cloned = /* element to clone the selected nodes to */;
628+
629629
// then apply Range methods to selection.getRangeAt(0)
630-
// or to all ranges if supporting multi-select
630+
// or, like here, to all ranges to support multi-select
631631
for (let i = 0; i < selection; i++) {
632632
cloned.append(selection.getRangeAt(i).cloneContents());
633633
}
@@ -639,11 +639,9 @@ The most used recipes are probably:
639639
// directly:
640640
selection.setBaseAndExtent(...from...to...);
641641

642-
// or create range and:
642+
// or we can create a range and:
643643
selection.removeAllRanges();
644644
selection.addRange(range);
645645
```
646646

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

Comments
 (0)