Skip to content

Commit 3e276e9

Browse files
authored
Update article.md
1 parent 04633a9 commit 3e276e9

1 file changed

Lines changed: 28 additions & 26 deletions

File tree

2-ui/99-ui-misc/01-mutation-observer/article.md

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11

22
# Mutation observer
33

4-
همان طور که می دانیم `MutationObserver` یک شی داخلی است که یک عنصر DOM را مشاهده می کند و هنگامی که تغییری را تشخیص می دهد یک تماس برگشتی ایجاد می کند.
4+
همان طور که می دانیم `MutationObserver` یک inner object است که یک عنصر DOM را مشاهده می کند و هنگامی که تغییری را تشخیص می دهد یک تماس برگشتی ایجاد می کند.
55

66
ابتدا نگاهی به نحو می اندازیم و سپس یک مورد استفاده در دنیای واقعی را بررسی می کنیم تا ببینیم چنین چیزی در کجا ممکن است مفید باشد.
77

88
## Syntax
99

1010
استفاده از `MutationObserver` بسیار ساده است.
1111

12-
اول از ههمه، یک observer با استفاده از callback-function می سازیم:
12+
اول از همه، یک observer با استفاده از callback-function می سازیم:
1313

1414
```js
1515
let observer = new MutationObserver(callback);
@@ -32,19 +32,20 @@ observer.observe(node, config);
3232
- `attributeOldValue` --بود هم مقدار قذیم و هم مقدار جدید attribute به callback پاس داده می شود `true` اگر
3333
- `characterDataOldValue` --(needs `characterData` option) پاس می دهیم، در غیر این صورت فقط مقدار جدید را callback را یه `node.data` بود هم مقدار جدید و هم مقدار قدیم `true` اگر
3434

35-
Then after any changes, the `callback` is executed: changes are passed in the first argument as a list of [MutationRecord](https://dom.spec.whatwg.org/#mutationrecord) objects, and the observer itself as the second argument.
35+
سپس پس از هر تغییری، `callback` اجرا می‌شود: تغییرات در آرگومان اول به‌عنوان فهرستی از اشیاء [MutationRecord](https://dom.spec.whatwg.org/#mutationrecord) و خود مشاهده‌گر به‌عنوان استدلال دوم است.
3636

37-
[MutationRecord](https://dom.spec.whatwg.org/#mutationrecord) objects have properties:
37+
38+
شی (object) های [MutationRecord](https://dom.spec.whatwg.org/#mutationrecord) properties هایی دارند:
3839

3940
- `type` -- mutation type, یکی از موارد زیر
4041
- `"attributes"`: attribute modified
4142
- `"characterData"`: data modified, used for text nodes,
4243
- `"childList"`: child elements added/removed,
43-
- `target` -- where the change occurred: an element for `"attributes"`, or text node for `"characterData"`, or an element for a `"childList"` mutation,
44-
- `addedNodes/removedNodes` -- nodes that were added/removed,
45-
- `previousSibling/nextSibling` -- the previous and next sibling to added/removed nodes,
46-
- `attributeName/attributeNamespace` -- the name/namespace (for XML) of the changed attribute,
47-
- `oldValue` -- the previous value, only for attribute or text changes, if the corresponding option is set `attributeOldValue`/`characterDataOldValue`.
44+
- `target` -- `"childList"` mutation برای element و یا `"characterData"` برای text node یا `"attributes"` جایی که تغییر رخ داده است: یک عنصر برای
45+
- `addedNodes/removedNodes` -- شده اند added/removed هایی که (nodes)نود
46+
- `previousSibling/nextSibling` --added/removed nodes خواهر یا برادر قبلی یا جدید،
47+
- `attributeName/attributeNamespace` -- changed attribute (XML) برای namespace اسم یا
48+
- `oldValue` -- .تنظیم شود `attributeOldValue`/`characterDataOldValue` مقدار قبلی، فقط برای تغییر ویژگی یا متن می باشد، اگر گزینه مربوطه
4849

4950
برای مثال، در مثال زیر یک `<div>` با یک `contentEditable` وجود دارد. این attribute به ما اجازه میدهد روی آن و ادیت کردن آن تمرکز کنیم.
5051

@@ -137,20 +138,20 @@ mutationRecords = [{
137138
document.querySelectorAll('pre[class*="language"]').forEach(Prism.highlightElem);
138139
```
139140

140-
Everything's simple so far, right? We find code snippets in HTML and highlight them.
141141

142-
Now let's go on. Let's say we're going to dynamically fetch materials from a server. We'll study methods for that [later in the tutorial](info:fetch). For now it only matters that we fetch an HTML article from a webserver and display it on demand:
142+
حالا بیایید ادامه دهیم. فرض کنید می خواهیم مطالب را به صورت پویا از یک سرور واکشی کنیم. ما روش‌هایی را برای آن مطالعه خواهیم کرد [later in the tutorial](info:fetch). در حال حاضر فقط مهم است که یک مقاله HTML را از یک وب سرور واکشی کنیم و آن را در صورت درخواست نمایش دهیم:
143143

144144
```js
145145
let article = /* fetch new content from server */
146146
articleElem.innerHTML = article;
147147
```
148148

149-
The new `article` HTML may contain code snippets. We need to call `Prism.highlightElem` on them, otherwise they won't get highlighted.
150149

151-
**Where and when to call `Prism.highlightElem` for a dynamically loaded article?**
150+
در HTML `article` جدید ممکن است حاوی کدهایی باشد. باید`Prism.highlightElem` را روی آنها صدا کنیم، در غیر این صورت برجسته نمی‌شوند.
151+
152+
**کجا و چه زمانی برای یک مقاله بارگذاری شده پویا با `Prism.highlightElem` تماس بگیرید؟**
152153

153-
We could append that call to the code that loads an article, like this:
154+
می‌توانیم آن فراخوان را به کدی که یک مقاله را بارگیری می‌کند، اضافه کنیم، مانند این:
154155

155156
```js
156157
let article = /* fetch new content from server */
@@ -162,21 +163,22 @@ snippets.forEach(Prism.highlightElem);
162163
*/!*
163164
```
164165

165-
...But, imagine if we have many places in the code where we load our content - articles, quizzes, forum posts, etc. Do we need to put the highlighting call everywhere, to highlight the code in content after loading? That's not very convenient.
166166

167-
And what if the content is loaded by a third-party module? For example, we have a forum written by someone else, that loads content dynamically, and we'd like to add syntax highlighting to it. No one likes patching third-party scripts.
167+
...اما، تصور کنید اگر ما مکان های زیادی در کد داشته باشیم که در آن محتوای خود را بارگذاری می کنیم - مقالات، آزمون ها، پست های انجمن و غیره. این خیلی راحت نیست.
168+
169+
و اگر محتوا توسط یک ماژول شخص ثالث بارگذاری شود چه؟ به عنوان مثال، ما یک انجمن داریم که توسط شخص دیگری نوشته شده است، که محتوا را به صورت پویا بارگیری می کند، و مایلیم برجسته سازی نحوی را به آن اضافه کنیم. هیچ کس وصله اسکریپت های شخص ثالث را دوست ندارد.
168170

169-
Luckily, there's another option.
171+
خوشبختانه، گزینه دیگری وجود دارد.
170172

171-
We can use `MutationObserver` to automatically detect when code snippets are inserted into the page and highlight them.
173+
می‌توانیم از `MutationObserver` استفاده کنیم تا به‌طور خودکار زمانی که قطعه‌های کد در صفحه درج می‌شوند، شناسایی کرده و آن‌ها را برجسته کنیم.
172174

173-
So we'll handle the highlighting functionality in one place, relieving us from the need to integrate it.
175+
بنابراین ما عملکرد برجسته سازی را در یک مکان مدیریت می کنیم و ما را از نیاز به ادغام آن رها می کنیم.
174176

175177
### Dynamic highlight demo
176178

177-
Here's the working example.
179+
و مثالی که کار می کند.
178180

179-
If you run this code, it starts observing the element below and highlighting any code snippets that appear there:
181+
اگر این کد را اجرا کنید، شروع به مشاهده element زیر می کند و هر قطعه کدی را که در آنجا ظاهر می شود برجسته می کند:
180182

181183
```js run
182184
let observer = new MutationObserver(mutations => {
@@ -254,14 +256,14 @@ observer.disconnect();
254256
```
255257

256258

257-
```smart header="Records returned by `observer.takeRecords()` are removed from the processing queue"
258-
The callback won't be called for records, returned by `observer.takeRecords()`.
259+
``` پس smart header="سوابق برگردانده شده توسط `observer.takeRecords()` از صف پردازش حذف می شوند"
260+
تماس برگشتی برای رکوردها که توسط 'observer.takeRecords()' برگردانده شده است، فراخوانی نمی شود.
259261
```
260262
261-
```smart header="Garbage collection interaction"
262-
Observers use weak references to nodes internally. That is, if a node is removed from the DOM, and becomes unreachable, then it can be garbage collected.
263+
```smart header="تعامل جمع آوری زباله"
264+
ناظران از ارجاعات ضعیف به گره ها در داخل استفاده می کنند. به این معنا که اگر یک گره از DOM حذف شود و غیرقابل دسترسی شود، می‌توان زباله‌ها را جمع‌آوری کرد.
263265
264-
The mere fact that a DOM node is observed doesn't prevent the garbage collection.
266+
صرف این واقعیت که یک گره DOM مشاهده می شود مانع از جمع آوری زباله نمی شود.
265267
```
266268

267269
## Summary

0 commit comments

Comments
 (0)