# हाइलाइट के लिए एक प्रतिलिपि बटन

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

# परिचय

इस साइट में मैं Hightlight का उपयोग करता हूँ।

# लक्ष्य

अंतिम बिंदु यह है कि हम साइट पर एक प्रतिलिपि बटन इस तरह है
![प्रतिलिपि बटन](copybutton.png)

**नोट: इस लेख के लिए सभी श्रेय [फराज़ पटनकर](https://dev.to/farazpatankar/highlightjs-copy-button-plugin-3dld) जिसको मैं इस एक को बनाने के लिए उपयोग किया करता हूँ-' का लेख-मैं सिर्फ अपने संदर्भ के लिए और दूसरों से साझा करने के लिए इसमें किए गए परिवर्तनों की दस्तावेज़ बनाना चाहता था**

[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>`;
```

जब यह काम करता है, मैं पहले से ही इस साइट पर BoxIcons का उपयोग कर रहा हूँ जो एक [वहाँ पहले से ही प्रतिमा को नक़ल करें](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` घटना और कोड ब्लॉक में एक बटन जोड़ता है

तो पहले मैं Faraz के कोड को प्रतिलिपिित किया और फिर निम्नलिखित परिवर्तन कर दिया

1. इसे कोड ब्लॉक के अंत में जोड़ने की बजाय मैं इसे शुरू करने के लिए पहले से जोड़ता हूँ
2. SVG के बजाय मैं सिर्फ इन वर्गों को घुसाया बटन & सेटिंग पाठ साइज में जोड़ कर BoxIcons संस्करण का उपयोग किया `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` प्रकार्य

यह मेरी परियोजना में जोड़ी गई एक रैसर आंशिक पर निर्भर करता है
यह आंशिक का उपयोग करता है [डेज़ीयूआई टोस्ट घटक](https://daisyui.com/components/toast/) उपयोगकर्ता को संदेश दिखाने के लिए
मैं इस दृष्टिकोण को पसंद करता हूँ क्योंकि यह जावास्क्रिप्ट साफ और सरल रखता है और मुझे साइट के बाकी की तरह टोस्ट संदेश शैली प्रदान करता है

```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)

# अंत में

तो यह है कि Faraz के कोड में एक सरल परिवर्तन है यह मेरे लिए काम करने के लिए ' को बनाता है। मैं आशा करता हूँ यह बाहर से किसी और की मदद करता है