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
+51-42Lines changed: 51 additions & 42 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -512,26 +512,32 @@ The exception is some selection methods, that replace the existing selection, su
512
512
(رویداد ها)Events:
513
513
-`input.onselect`-- هنگامی که چیزی انتخاب می شود فعال می شود.
514
514
515
-
Methods:
515
+
متد ها:
516
+
-`input.select()`-- همه چیز را در کنترل متن انتخاب می کند (می تواند `textarea` به جای `input` باشد)
517
+
-`input.setSelectionRange(start, end, [direction])`-- انتخاب را تغییر دهید تا از موقعیت `شروع` تا `"پایان` در جهت معین (اختیاری) باشد.
518
+
-`input.setRangeText(replacement, [start], [end], [selectionMode])`-- یک محدوده از متن را با متن جدید جایگزین کنید.
516
519
517
-
-`input.select()`-- selects everything in the text control (can be `textarea` instead of`input`),
518
-
-`input.setSelectionRange(start, end, [direction])`-- change the selection to span from position `start` till `end`, in the given direction (optional).
519
-
-`input.setRangeText(replacement, [start], [end], [selectionMode])`-- replace a range of text with the newtext.
520
-
521
-
Optional arguments`start` and `end`, if provided, set the range start and end, otherwise user selection is used.
520
+
521
+
آرگومان های اختیاری`«شروع` و`«پایان`، در صورت ارائه، محدوده شروع و پایان را تنظیم می کنند، در غیر این صورت از انتخاب کاربر استفاده می شود.
522
522
523
-
The last argument, `selectionMode`, determines how the selection will be set after the text has been replaced. The possible values are:
523
+
آخرین آرگومان، `selectionMode`، نحوه تنظیم انتخاب پس از جایگزینی متن را تعیین میکند. مقادیر ممکن عبارتند از:
524
524
525
525
-`"select"`-- the newly inserted text will be selected.
526
526
-`"start"`-- the selection range collapses just before the inserted text (the cursor will be immediately before it).
527
527
-`"end"`-- the selection range collapses just after the inserted text (the cursor will be right after it).
528
528
-`"preserve"`-- attempts to preserve the selection. This is the default.
529
529
530
530
Now let's see these methods in action.
531
+
- `"select"` -- متن درج شده جدید انتخاب خواهد شد.
532
+
- `"start"`-- محدوده انتخاب درست قبل از متن درج شده جمع می شود (مکان نما بلافاصله قبل از آن خواهد بود).
533
+
- `"end"` -- محدوده انتخاب درست بعد از متن درج شده جمع می شود (مکان نما درست بعد از آن خواهد بود).
534
+
- `"preserve"` - تلاش برای حفظ انتخاب. این پیش فرض است.
535
+
536
+
حال بیایید این روش ها را در عمل ببینیم.
531
537
532
538
### Example: tracking selection
533
539
534
-
For example, this code uses `onselect` event to track selection:
540
+
رای مثال، این کد از رویداد `onselect` برای ردیابی انتخاب استفاده میکند:
@@ -548,20 +554,19 @@ From <input id="from" disabled> – To <input id="to" disabled>
548
554
</script>
549
555
```
550
556
551
-
Please note:
552
-
- `onselect` triggers when something is selected, but not when the selection is removed.
553
-
- `document.onselectionchange` event should not trigger for selections inside a form control, according to the [spec](https://w3c.github.io/selection-api/#dfn-selectionchange), as it's not related to `document` selection and ranges. Some browsers generate it, but we shouldn't rely on it.
554
-
555
-
557
+
لطفا توجه داشته باشید:
558
+
- وقتی چیزی انتخاب میشود، `onselect` فعال میشود، اما وقتی انتخاب حذف میشود نه.
559
+
- طبق [spec](https://w3c.github.io/selection-api/#dfn-selectionchange)، رویداد `document.onselectionchange` نباید برای انتخابهای داخل یک کنترل فرم فعال شود، زیرا به مرتبط نیست انتخاب `document` و محدوده برخی از مرورگرها آن را تولید می کنند، اما ما نباید به آن تکیه کنیم.
556
560
### Example: moving cursor
557
561
558
-
We can change `selectionStart` and `selectionEnd`, that sets the selection.
559
562
560
-
An important edge case is when `selectionStart` and `selectionEnd` equal each other. Then it's exactly the cursor position. Or, to rephrase, when nothing is selected, the selection is collapsed at the cursor position.
563
+
میتوانیم `selectionStart` و `selectionEnd` را تغییر دهیم که انتخاب را تنظیم میکند.
561
564
562
-
So, by setting `selectionStart`and`selectionEnd`to the same value, we move the cursor.
565
+
یک مورد مهم لبه زمانی است که `selectionStart` و `selectionEnd` با هم برابر باشند. سپس دقیقاً موقعیت مکان نما است. یا، برای بازنویسی، وقتی چیزی انتخاب نشده است، انتخاب در موقعیت مکان نما جمع می شود.
563
566
564
-
For example:
567
+
بنابراین، با تنظیم `selectionStart` و `selectionEnd` روی یک مقدار، مکاننما را حرکت میدهیم.
@@ -582,11 +587,11 @@ Focus on me, the cursor will be at position 10.
582
587
583
588
### Example: modifying selection
584
589
585
-
To modify the content of the selection, we can use `input.setRangeText()`method. 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.
590
+
برای تغییر محتوای انتخابی، میتوانیم از روش `input.setRangeText()` استفاده کنیم. البته، میتوانیم `selectionStart/End` را بخوانیم و با آگاهی از انتخاب، زیررشته مربوط به `value` را تغییر دهیم، اما `setRangeText` قدرتمندتر و اغلب راحتتر است.
586
591
587
-
That's a somewhat complex method. In its simplest one-argument form it replaces the user selected range and removes the selection.
592
+
این یک روش تا حدودی پیچیده است. در ساده ترین شکل تک آرگومان خود، جایگزین محدوده انتخابی کاربر می شود و انتخاب را حذف می کند.
588
593
589
-
For example, here the user selection will be wrapped by `*...*`:
594
+
به عنوان مثال، در اینجا انتخاب کاربر با `*...*` پیچیده می شود:
590
595
591
596
```html run autorun
592
597
<input id="input" style="width:200px" value="Select here and click the button">
@@ -604,9 +609,9 @@ button.onclick = () => {
604
609
</script>
605
610
```
606
611
607
-
With more arguments, we can set range `start` and `end`.
612
+
با آرگومان های بیشتر، می توانیم محدوده `start` و `end` را تنظیم کنیم.
608
613
609
-
In this example we find `"THIS"` in the input text, replace it and keep the replacement selected:
614
+
در این مثال، `"THIS"` را در متن ورودی پیدا می کنیم، آن را جایگزین می کنیم و جایگزین را انتخاب می کنیم:
610
615
611
616
```html run autorun
612
617
<input id="input" style="width:200px" value="Replace THIS in text">
@@ -625,11 +630,12 @@ button.onclick = () => {
625
630
626
631
### Example: insert at cursor
627
632
628
-
If nothing is selected, or we use equal `start` and `end` in `setRangeText`, then the new text is just inserted, nothing is removed.
629
633
630
-
We can also insert something "at the cursor" using `setRangeText`.
634
+
اگر چیزی انتخاب نشده باشد، یا از `start` و `end` مساوی در `setRangeText` استفاده کنیم، آنگاه متن جدید فقط درج میشود، چیزی حذف نمیشود.
635
+
636
+
همچنین میتوانیم چیزی را «در مکاننما» با استفاده از `setRangeText` وارد کنیم.
631
637
632
-
Here's a 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 detect it by comparing `selectionStart!=selectionEnd` and do something else instead):
638
+
در اینجا دکمه ای وجود دارد که «HELLO» را در موقعیت مکان نما قرار می دهد و مکان نما را بلافاصله بعد از آن قرار می دهد. اگر انتخاب خالی نباشد، جایگزین میشود (میتوانیم آن را با مقایسه «selectionStart!=selectionEnd» شناسایی کنیم و به جای آن کار دیگری انجام دهیم):
633
639
634
640
```html run autorun
635
641
<input id="input" style="width:200px" value="Text Text Text Text Text">
@@ -646,9 +652,9 @@ Here's a button that inserts `"HELLO"` at the cursor position and puts the curso
646
652
647
653
## Making unselectable
648
654
649
-
To make something unselectable, there are three ways:
655
+
2. برای غیرقابل انتخاب کردن چیزی، سه راه وجود دارد:
650
656
651
-
1.Use CSS property `user-select: none`.
657
+
1. از ویژگی `user-select: none` استفاده کنید.
652
658
653
659
```html run
654
660
<style>
@@ -658,13 +664,14 @@ To make something unselectable, there are three ways:
This doesn't allow the selection to start at `elem`. But the user may start the selection elsewhere and include `elem` into it.
669
+
این اجازه نمیدهد انتخاب از «elem» شروع شود. اما کاربر ممکن است انتخاب را از جای دیگری شروع کند و `elem` را در آن قرار دهد.
663
670
664
-
Then `elem` will become a part of `document.getSelection()`, so the selection actually happens, but its content is usually ignored in copy-paste.
671
+
سپس `elem` به بخشی از `document.getSelection()` تبدیل میشود، بنابراین انتخاب در واقع اتفاق میافتد، اما محتوای آن معمولاً در کپی-پیست نادیده گرفته میشود.
665
672
666
673
667
-
2. Prevent default action in `onselectstart` or `mousedown` events.
674
+
4. 3. از اقدام پیشفرض در رویدادهای `onselectstart` یا `mousedown` جلوگیری کنید.
@@ -674,11 +681,12 @@ To make something unselectable, there are three ways:
674
681
</script>
675
682
```
676
683
677
-
This prevents starting the selection on `elem`, but the visitor may start it at another element, then extend to `elem`.
684
+
این کار از شروع انتخاب روی `elem` جلوگیری میکند، اما بازدیدکننده ممکن است آن را از عنصر دیگری شروع کند و سپس به `elem` گسترش دهد.
685
+
686
+
زمانی که کنترلکننده رویداد دیگری در همان عملکرد وجود دارد که انتخاب را فعال میکند، راحت است (e.g. `mousedown`). بنابراین، برای جلوگیری از تضاد، انتخاب را غیرفعال میکنیم و همچنان اجازه میدهیم محتوای `elem` کپی شود.
678
687
679
-
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.
688
+
5. همچنین میتوانیم انتخاب post-factum را بعد از اینکه با `document.getSelection().empty()` رخ داد پاک کنیم. این به ندرت استفاده می شود، زیرا باعث چشمک زدن ناخواسته با ظاهر شدن انتخاب می شود - ناپدید می شود.
680
689
681
-
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.
682
690
683
691
## References
684
692
@@ -689,16 +697,17 @@ To make something unselectable, there are three ways:
689
697
690
698
## Summary
691
699
692
-
We covered two different APIs for selections:
693
700
694
-
1. For document:`Selection` and `Range` objects.
695
-
2. For `input`, `textarea`: additional methods and properties.
701
+
ما دو API مختلف را برای انتخاب پوشش دادیم:
696
702
697
-
The second API is very simple, as it works with text.
703
+
1. برای سند: اشیاء `Selection` و `Range`.
704
+
2. برای `input`، `textarea`: روشها و ویژگیهای اضافی.
698
705
699
-
The most used recipes are probably:
706
+
وAPI دوم بسیار ساده است، زیرا با متن کار می کند.
700
707
701
-
1. Getting the selection:
708
+
دستور العمل های مورد استفاده احتمالاً عبارتند از:
709
+
710
+
1. دریافت انتخاب:
702
711
```js
703
712
let selection = document.getSelection();
704
713
@@ -710,7 +719,7 @@ The most used recipes are probably:
@@ -721,5 +730,5 @@ The most used recipes are probably:
721
730
selection.removeAllRanges();
722
731
selection.addRange(range);
723
732
```
724
-
725
-
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`.
733
+
734
+
و در نهایت، در مورد مکان نما. موقعیت مکان نما در عناصر قابل ویرایش، مانند `<textarea>` همیشه در ابتدا یا انتهای انتخاب است. میتوانیم با تنظیم `elem.selectionStart` و `elem.selectionEnd` از آن برای به دست آوردن موقعیت مکاننما یا حرکت مکاننما استفاده کنیم.
0 commit comments