# زر النسخ لتسليط الضوء.js

<!--category-- DaisyUI, Tailwind, Highlight.js, Javascript -->
<datetime class="hidden">2024-09-28T14:15</datetime>

# المقدمة

في هذا الموقع أستخدم Hightlight.js لإظهار شفرات البرمجة على الجانب клиентي. أعجبني هذا لأنه يبقي برمجتي على الجانب серверي نظيفة وبسيطةM SK2 مع ذلك , أردت أن أضيف زر نسخ إلى كل شفرات برمجية بحيث يستطيع المستخدمين بسهولة نسخ الرمز إلى لوحتهم المدمجة . هذه مهمة بسيطة لكني فكرت أنني سأقوم بتوثيقها هنا لأي شخص آخر قد يرغب في القيام بنفس الشيءMSC5

# الهدف

النقطة النهائية هي أن لدينا زر نسخ مثل هذا على الموقع
![أضغط على زر النسخ](copybutton.png)

**ملاحظة: كل الإحتمالات لهذا المقال تذهب إلى [فراز باتانكار](https://dev.to/farazpatankar/highlightjs-copy-button-plugin-3dld) من "مسك0" المقال الذي استخدمته لصنع هذا "مسک1" أردت فقط أن أوثيق التغييرات التي قمت بها هنا لأجل مرجعي الخاص وأشاركها مع الآخرين**

[TOC]

# الخيارات

هناك ' هناك بعض الطرق لفعل هذا ; على سبيل المثال هناك [برمجة زر النسخ](https://github.com/arronhunt/highlightjs-copy) لHiglight.js لكني قررت أن أرغب في المزيد من التحكم في الزر والتصميم [هذا المقال](https://dev.to/farazpatankar/highlightjs-copy-button-plugin-3dld) لإضافة زر النسخ.

## المشاكل

في حين أن هذا المقال هو نهج عظيم كان له بعض القضايا التي توقفت عن كونه مثالي بالنسبة لي

1. إنها تستخدم الخطوط التي لا أستخدمها على موقعي 'لا أستعملها (لكن عندها يستعمل SVG يدويًا أيضا, لست متأكدة لماذا

```javascript
  // Lucide copy icon
    copyButton.innerHTML = `<svg class="h-4 w-4" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-copy"><rect width="14" height="14" x="8" y="8" rx="2" ry="2"/><path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2"/></svg>`;
```

بينما هذا يعمل, أنا أستخدم already BoxIcons on this site which has a [نسخ الأيقون هناك بالفعل](https://icon-sets.iconify.design/bx/copy/).

2. إنها تستخدم مكتبة الخبز التي لا أملكها على هذا الموقع

```javascript
  // Notify user that the content has been copied
      toast.success("Copied to clipboard", {
        description: "The code block content has been copied to the clipboard.",
      });
```

3. إنه يضرب الـ y ــ - ــ التدفق الزائد على بلاك الشيفرة ويضع الأيقون في الأسفل الذي لم أرغب فيه ـــ ' ــ لا أريده ـــ. ــ لذا فإن الخاص بي في الأعلى على اليمين ــ.

# تكيفاتي

## الوظيفة الرئيسية

هذا प्लगइन يربط في `after:highlightElement` و يضيف زراً إلى بلاك الرمز.

لذلك قمت أولاً بتقليد شفرة فرااز ' ثم قمت بإجراء التغيرات التالية :

1. بدلاً من دمجها في نهاية بلاك البرمجة أقوم بتحضيرها في البداية
2. بدلاً من SVG استخدمت إصدار الألوان الصندوقية فقط بإضافة تلك الفئات إلى زر مدمج & يضبط حجم النص إلى `text-xl`.
3. قمت بإزالة الإبلاغ عن الخبز و استبدلته بنموذج بسيط `showToast` الوظيفة التي أملكها في موقعي (انظر إلى وقت لاحق)
4. لقد قمت بإضافة `aria-label` و `title` إلى زر إمكانية الوصول (و لإعطاء تأثير طائر جميل ).

```javascript
hljs.addPlugin({
    "after:highlightElement": ({ el, text }) => {
        const wrapper = el.parentElement;
        if (wrapper == null) {
            return;
        }

        /**
         * Make the parent relative so we can absolutely
         * position the copy button
         */
        wrapper.classList.add("relative");
        const copyButton = document.createElement("button");
        copyButton.classList.add(
            "absolute",
            "top-2",
            "right-1",
            "p-2",
            "text-gray-500",
            "hover:text-gray-700",
            "bx",
            "bx-copy",
            "text-xl",
            "cursor-pointer"
        );
        copyButton.setAttribute("aria-label", "Copy code to clipboard");
        copyButton.setAttribute("title", "Copy code to clipboard");

        copyButton.onclick = () => {
            navigator.clipboard.writeText(text);

            // Notify user that the content has been copied
            showToast("The code block content has been copied to the clipboard.", 3000, "success");

        };
        // Append the copy button to the wrapper
        wrapper.prepend(copyButton);
    },
});
```

## `showToast` الوظيفة

هذا يعتمد على قطعة ال razor التي أضافتها إلى مشروعي.
هذا الجزء يستخدم [مكونات DaisyUI Toast](https://daisyui.com/components/toast/) لإظهار رسالة إلى المستخدم.
أحب هذا المقاربة لأنها تبقي Javascript نظيفة وبسيطة وتسمح لي بتصميم رسالة الخبز بنفس الطريقة التي يتبعها باقي الموقع.

```html
<div id="toast" class="toast toast-bottom fixed z-50 hidden overflow-y-hidden">
    <div id="toast-message" class="alert">
        <div>
            <span id="toast-text">Notification message</span>
        </div>
    </div>
    <p class="hidden right-1 bx-copy  cursor-pointer alert-success alert-warning alert-error alert-info"></p>
</div>
```

ستلاحظون أن هذا أيضاً مخفي غريب `p` tag at the bottom, this is just for Tailwind to parse these classes when it builds the site

وظيفة Javascript بسيطة, تظهر رسالة الخبز فقط لوقت محدد ثم تخفيها مرة أخرى.

```javascript
window.showToast = function(message, duration = 3000, type = 'success') {
    const toast = document.getElementById('toast');
    const toastText = document.getElementById('toast-text');
    const toastMessage = document.getElementById('toast-message');

    // Set message and type
    toastText.innerText = message;
    toastMessage.className = `alert alert-${type}`; // Change alert type (success, warning, error)

    // Show the toast
    toast.classList.remove('hidden');

    // Hide the toast after specified duration
    setTimeout(() => {
        toast.classList.add('hidden');
    }, duration);
}
```

ثم يمكننا أن نسمي هذا باستخدام `showToast` وظيفة `copyButton.onclick` حدث.

```javascript
showToast("The code block content has been copied to the clipboard.", 3000, "success");
```

لقد قمت بإضافة هذا الجزء في الأعلى `_Layout.cshtml` بحيث يكون ' متاحاً على كل صفحة

```html
<partial name="_Toast"  />``
```

الآن عندما نعرض ملصقات المدونات فإن بلاك البرمجة لديها
![أضغط على زر النسخ](copybutton.png)

# في الختام

لذلك 'is itM SK1 a simple change to Faraz's code to make it work for meMST3 I hope this helps someone else out thereMSC4