Files
danak-admin/src/components/VideoBlot.ts
T
2025-09-02 17:00:37 +03:30

74 lines
2.0 KiB
TypeScript

import Quill from "quill";
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const BlockEmbed = Quill.import("blots/block/embed") as any;
interface VideoData {
url: string;
width?: string;
height?: string;
}
class VideoBlot extends BlockEmbed {
static blotName = "video";
static tagName = "div";
static className = "ql-video-wrapper";
static create(value: VideoData) {
const node = super.create();
node.classList.add("ql-video-wrapper");
const iframe = document.createElement("iframe");
iframe.setAttribute("src", value.url);
iframe.setAttribute("width", value.width || "100%");
iframe.setAttribute("height", value.height || "315");
iframe.setAttribute("frameborder", "0");
iframe.setAttribute("allowfullscreen", "true");
iframe.style.maxWidth = "100%";
iframe.style.borderRadius = "8px";
node.appendChild(iframe);
return node;
}
static value(node: HTMLElement) {
const iframe = node.querySelector("iframe");
if (!iframe) return null;
return {
url: iframe.getAttribute("src") || "",
width: iframe.getAttribute("width") || "100%",
height: iframe.getAttribute("height") || "315",
};
}
}
// تابع برای تشخیص و تبدیل URL های ویدیو
export const getEmbedUrl = (url: string): string | null => {
// YouTube
const youtubeRegex = /(?:youtube\.com\/(?:[^/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([^"&?/\s]{11})/;
const youtubeMatch = url.match(youtubeRegex);
if (youtubeMatch) {
return `https://www.youtube.com/embed/${youtubeMatch[1]}`;
}
// Aparat
const aparatRegex = /aparat\.com\/v\/([a-zA-Z0-9]+)/;
const aparatMatch = url.match(aparatRegex);
if (aparatMatch) {
return `https://www.aparat.com/video/video/embed/videohash/${aparatMatch[1]}/vt/frame`;
}
// اگر قبلاً embed URL است
if (
url.includes("youtube.com/embed/") ||
url.includes("aparat.com/video/video/embed/")
) {
return url;
}
return null;
};
export default VideoBlot;