This is a viewer only at the moment see the article on how this works.
To update the preview hit Ctrl-Alt-R (or ⌘-Alt-R on Mac) or Enter to refresh. The Save icon lets you save the markdown file to disk
This is a preview from the server running through my markdig pipeline
زر النسخ لتسليط الضوء.js
Saturday, 28 September 2024
المقدمة
في هذا الموقع أستخدم Hightlight.js لإظهار شفرات البرمجة على الجانب клиентي. أعجبني هذا لأنه يبقي برمجتي على الجانب серверي نظيفة وبسيطةM SK2 مع ذلك , أردت أن أضيف زر نسخ إلى كل شفرات برمجية بحيث يستطيع المستخدمين بسهولة نسخ الرمز إلى لوحتهم المدمجة . هذه مهمة بسيطة لكني فكرت أنني سأقوم بتوثيقها هنا لأي شخص آخر قد يرغب في القيام بنفس الشيءMSC5
الهدف
النقطة النهائية هي أن لدينا زر نسخ مثل هذا على الموقع

ملاحظة: كل الإحتمالات لهذا المقال تذهب إلى فراز باتانكار من "مسك0" المقال الذي استخدمته لصنع هذا "مسک1" أردت فقط أن أوثيق التغييرات التي قمت بها هنا لأجل مرجعي الخاص وأشاركها مع الآخرين
الخيارات
هناك ' هناك بعض الطرق لفعل هذا ; على سبيل المثال هناك برمجة زر النسخ لHiglight.js لكني قررت أن أرغب في المزيد من التحكم في الزر والتصميم هذا المقال لإضافة زر النسخ.
المشاكل
في حين أن هذا المقال هو نهج عظيم كان له بعض القضايا التي توقفت عن كونه مثالي بالنسبة لي
- إنها تستخدم الخطوط التي لا أستخدمها على موقعي 'لا أستعملها (لكن عندها يستعمل SVG يدويًا أيضا, لست متأكدة لماذا
// 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 نسخ الأيقون هناك بالفعل.
- إنها تستخدم مكتبة الخبز التي لا أملكها على هذا الموقع
// Notify user that the content has been copied
toast.success("Copied to clipboard", {
description: "The code block content has been copied to the clipboard.",
});
- إنه يضرب الـ y ــ - ــ التدفق الزائد على بلاك الشيفرة ويضع الأيقون في الأسفل الذي لم أرغب فيه ـــ ' ــ لا أريده ـــ. ــ لذا فإن الخاص بي في الأعلى على اليمين ــ.
تكيفاتي
الوظيفة الرئيسية
هذا प्लगइन يربط في after:highlightElement و يضيف زراً إلى بلاك الرمز.
لذلك قمت أولاً بتقليد شفرة فرااز ' ثم قمت بإجراء التغيرات التالية :
- بدلاً من دمجها في نهاية بلاك البرمجة أقوم بتحضيرها في البداية
- بدلاً من SVG استخدمت إصدار الألوان الصندوقية فقط بإضافة تلك الفئات إلى زر مدمج & يضبط حجم النص إلى
text-xl. - قمت بإزالة الإبلاغ عن الخبز و استبدلته بنموذج بسيط
showToastالوظيفة التي أملكها في موقعي (انظر إلى وقت لاحق) - لقد قمت بإضافة
aria-labelوtitleإلى زر إمكانية الوصول (و لإعطاء تأثير طائر جميل ).
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 لإظهار رسالة إلى المستخدم. أحب هذا المقاربة لأنها تبقي Javascript نظيفة وبسيطة وتسمح لي بتصميم رسالة الخبز بنفس الطريقة التي يتبعها باقي الموقع.
<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 بسيطة, تظهر رسالة الخبز فقط لوقت محدد ثم تخفيها مرة أخرى.
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 حدث.
showToast("The code block content has been copied to the clipboard.", 3000, "success");
لقد قمت بإضافة هذا الجزء في الأعلى _Layout.cshtml بحيث يكون ' متاحاً على كل صفحة
<partial name="_Toast" />``
الآن عندما نعرض ملصقات المدونات فإن بلاك البرمجة لديها

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