Skip to content

Commit 81ba357

Browse files
authored
Update article.md
1 parent db901b7 commit 81ba357

1 file changed

Lines changed: 51 additions & 42 deletions

File tree

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

Lines changed: 51 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -512,26 +512,32 @@ The exception is some selection methods, that replace the existing selection, su
512512
(رویداد ها)Events:
513513
- `input.onselect` -- هنگامی که چیزی انتخاب می شود فعال می شود.
514514
515-
Methods:
515+
متد ها:
516+
- `input.select()` -- همه چیز را در کنترل متن انتخاب می کند (می تواند `textarea` به جای `input` باشد)
517+
- `input.setSelectionRange(start, end, [direction])` -- انتخاب را تغییر دهید تا از موقعیت `شروع` تا `"پایان` در جهت معین (اختیاری) باشد.
518+
- `input.setRangeText(replacement, [start], [end], [selectionMode])` -- یک محدوده از متن را با متن جدید جایگزین کنید.
516519
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 new text.
520-
521-
Optional arguments `start` and `end`, if provided, set the range start and end, otherwise user selection is used.
520+
521+
آرگومان های اختیاری`«شروع` و`«پایان`، در صورت ارائه، محدوده شروع و پایان را تنظیم می کنند، در غیر این صورت از انتخاب کاربر استفاده می شود.
522522
523-
The last argument, `selectionMode`, determines how the selection will be set after the text has been replaced. The possible values are:
523+
آخرین آرگومان، `selectionMode`، نحوه تنظیم انتخاب پس از جایگزینی متن را تعیین می‌کند. مقادیر ممکن عبارتند از:
524524
525525
- `"select"` -- the newly inserted text will be selected.
526526
- `"start"` -- the selection range collapses just before the inserted text (the cursor will be immediately before it).
527527
- `"end"` -- the selection range collapses just after the inserted text (the cursor will be right after it).
528528
- `"preserve"` -- attempts to preserve the selection. This is the default.
529529
530530
Now let's see these methods in action.
531+
- `"select"` -- متن درج شده جدید انتخاب خواهد شد.
532+
- `"start"`-- محدوده انتخاب درست قبل از متن درج شده جمع می شود (مکان نما بلافاصله قبل از آن خواهد بود).
533+
- `"end"` -- محدوده انتخاب درست بعد از متن درج شده جمع می شود (مکان نما درست بعد از آن خواهد بود).
534+
- `"preserve"` - تلاش برای حفظ انتخاب. این پیش فرض است.
535+
536+
حال بیایید این روش ها را در عمل ببینیم.
531537
532538
### Example: tracking selection
533539
534-
For example, this code uses `onselect` event to track selection:
540+
رای مثال، این کد از رویداد `onselect` برای ردیابی انتخاب استفاده می‌کند:
535541
536542
```html run autorun
537543
<textarea id="area" style="width:80%;height:60px">
@@ -548,20 +554,19 @@ From <input id="from" disabled> – To <input id="to" disabled>
548554
</script>
549555
```
550556
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` و محدوده برخی از مرورگرها آن را تولید می کنند، اما ما نباید به آن تکیه کنیم.
556560
### Example: moving cursor
557561
558-
We can change `selectionStart` and `selectionEnd`, that sets the selection.
559562
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` را تغییر دهیم که انتخاب را تنظیم می‌کند.
561564
562-
So, by setting `selectionStart` and `selectionEnd` to the same value, we move the cursor.
565+
یک مورد مهم لبه زمانی است که `selectionStart` و `selectionEnd` با هم برابر باشند. سپس دقیقاً موقعیت مکان نما است. یا، برای بازنویسی، وقتی چیزی انتخاب نشده است، انتخاب در موقعیت مکان نما جمع می شود.
563566
564-
For example:
567+
بنابراین، با تنظیم `selectionStart` و `selectionEnd` روی یک مقدار، مکان‌نما را حرکت می‌دهیم.
568+
569+
مثلا:
565570
566571
```html run autorun
567572
<textarea id="area" style="width:80%;height:60px">
@@ -582,11 +587,11 @@ Focus on me, the cursor will be at position 10.
582587
583588
### Example: modifying selection
584589
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` قدرتمندتر و اغلب راحت‌تر است.
586591
587-
That's a somewhat complex method. In its simplest one-argument form it replaces the user selected range and removes the selection.
592+
این یک روش تا حدودی پیچیده است. در ساده ترین شکل تک آرگومان خود، جایگزین محدوده انتخابی کاربر می شود و انتخاب را حذف می کند.
588593
589-
For example, here the user selection will be wrapped by `*...*`:
594+
به عنوان مثال، در اینجا انتخاب کاربر با `*...*` پیچیده می شود:
590595
591596
```html run autorun
592597
<input id="input" style="width:200px" value="Select here and click the button">
@@ -604,9 +609,9 @@ button.onclick = () => {
604609
</script>
605610
```
606611
607-
With more arguments, we can set range `start` and `end`.
612+
با آرگومان های بیشتر، می توانیم محدوده `start` و `end` را تنظیم کنیم.
608613
609-
In this example we find `"THIS"` in the input text, replace it and keep the replacement selected:
614+
در این مثال، `"THIS"` را در متن ورودی پیدا می کنیم، آن را جایگزین می کنیم و جایگزین را انتخاب می کنیم:
610615
611616
```html run autorun
612617
<input id="input" style="width:200px" value="Replace THIS in text">
@@ -625,11 +630,12 @@ button.onclick = () => {
625630
626631
### Example: insert at cursor
627632
628-
If nothing is selected, or we use equal `start` and `end` in `setRangeText`, then the new text is just inserted, nothing is removed.
629633
630-
We can also insert something "at the cursor" using `setRangeText`.
634+
اگر چیزی انتخاب نشده باشد، یا از `start` و `end` مساوی در `setRangeText` استفاده کنیم، آنگاه متن جدید فقط درج می‌شود، چیزی حذف نمی‌شود.
635+
636+
همچنین می‌توانیم چیزی را «در مکان‌نما» با استفاده از `setRangeText` وارد کنیم.
631637
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» شناسایی کنیم و به جای آن کار دیگری انجام دهیم):
633639
634640
```html run autorun
635641
<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
646652
647653
## Making unselectable
648654
649-
To make something unselectable, there are three ways:
655+
2. برای غیرقابل انتخاب کردن چیزی، سه راه وجود دارد:
650656
651-
1. Use CSS property `user-select: none`.
657+
1. از ویژگی `user-select: none` استفاده کنید.
652658
653659
```html run
654660
<style>
@@ -658,13 +664,14 @@ To make something unselectable, there are three ways:
658664
</style>
659665
<div>Selectable <div id="elem">Unselectable</div> Selectable</div>
660666
```
667+
661668
662-
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` را در آن قرار دهد.
663670
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()` تبدیل می‌شود، بنابراین انتخاب در واقع اتفاق می‌افتد، اما محتوای آن معمولاً در کپی-پیست نادیده گرفته می‌شود.
665672
666673
667-
2. Prevent default action in `onselectstart` or `mousedown` events.
674+
4. 3. از اقدام پیش‌فرض در رویدادهای `onselectstart` یا `mousedown` جلوگیری کنید.
668675
669676
```html run
670677
<div>Selectable <div id="elem">Unselectable</div> Selectable</div>
@@ -674,11 +681,12 @@ To make something unselectable, there are three ways:
674681
</script>
675682
```
676683
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` کپی شود.
678687
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()` رخ داد پاک کنیم. این به ندرت استفاده می شود، زیرا باعث چشمک زدن ناخواسته با ظاهر شدن انتخاب می شود - ناپدید می شود.
680689
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.
682690
683691
## References
684692
@@ -689,16 +697,17 @@ To make something unselectable, there are three ways:
689697
690698
## Summary
691699
692-
We covered two different APIs for selections:
693700
694-
1. For document: `Selection` and `Range` objects.
695-
2. For `input`, `textarea`: additional methods and properties.
701+
ما دو API مختلف را برای انتخاب پوشش دادیم:
696702
697-
The second API is very simple, as it works with text.
703+
1. برای سند: اشیاء `Selection` و `Range`.
704+
2. برای `input`، `textarea`: روش‌ها و ویژگی‌های اضافی.
698705
699-
The most used recipes are probably:
706+
وAPI دوم بسیار ساده است، زیرا با متن کار می کند.
700707
701-
1. Getting the selection:
708+
دستور العمل های مورد استفاده احتمالاً عبارتند از:
709+
710+
1. دریافت انتخاب:
702711
```js
703712
let selection = document.getSelection();
704713
@@ -710,7 +719,7 @@ The most used recipes are probably:
710719
cloned.append(selection.getRangeAt(i).cloneContents());
711720
}
712721
```
713-
2. Setting the selection:
722+
3. تنظیمات selection:
714723
```js
715724
let selection = document.getSelection();
716725
@@ -721,5 +730,5 @@ The most used recipes are probably:
721730
selection.removeAllRanges();
722731
selection.addRange(range);
723732
```
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

Comments
 (0)