From 5dd9d5d749f7c6b14558e4ed216ae2e1b5961f36 Mon Sep 17 00:00:00 2001 From: swve Date: Wed, 26 Feb 2025 11:37:29 +0100 Subject: [PATCH] feat: Enhance EmbedObjectsComponent with YouTube support and responsive design, overall improvements --- .../EmbedObjects/EmbedObjectsComponent.tsx | 401 +++++++++++++++--- .../Objects/Editor/Toolbar/ToolbarButtons.tsx | 2 +- 2 files changed, 351 insertions(+), 52 deletions(-) diff --git a/apps/web/components/Objects/Editor/Extensions/EmbedObjects/EmbedObjectsComponent.tsx b/apps/web/components/Objects/Editor/Extensions/EmbedObjects/EmbedObjectsComponent.tsx index c993d4d4..385955e6 100644 --- a/apps/web/components/Objects/Editor/Extensions/EmbedObjects/EmbedObjectsComponent.tsx +++ b/apps/web/components/Objects/Editor/Extensions/EmbedObjects/EmbedObjectsComponent.tsx @@ -2,7 +2,7 @@ import { NodeViewWrapper } from '@tiptap/react' import React, { useState, useRef, useEffect, useMemo } from 'react' import { Upload, Link as LinkIcon, GripVertical, GripHorizontal, AlignCenter, Cuboid, Code } from 'lucide-react' import { useEditorProvider } from '@components/Contexts/Editor/EditorContext' -import { SiGithub, SiReplit, SiSpotify, SiLoom, SiGooglemaps, SiCodepen, SiCanva, SiNotion, SiGoogledocs, SiGitlab, SiX, SiFigma, SiGiphy } from '@icons-pack/react-simple-icons' +import { SiGithub, SiReplit, SiSpotify, SiLoom, SiGooglemaps, SiCodepen, SiCanva, SiNotion, SiGoogledocs, SiGitlab, SiX, SiFigma, SiGiphy, SiYoutube } from '@icons-pack/react-simple-icons' import { useRouter } from 'next/navigation' import DOMPurify from 'dompurify' @@ -14,6 +14,21 @@ const SCRIPT_BASED_EMBEDS = { // Add more platforms as needed }; +// Helper function to convert YouTube URLs to embed format +const getYouTubeEmbedUrl = (url: string): string => { + // Handle different YouTube URL formats + const youtubeRegex = /(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([^"&?\/\s]{11})/i; + const match = url.match(youtubeRegex); + + if (match && match[1]) { + // Return the embed URL with the video ID + return `https://www.youtube.com/embed/${match[1]}?autoplay=0&rel=0`; + } + + // If no match found, return the original URL + return url; +}; + // Add new memoized component for the embed content const MemoizedEmbed = React.memo(({ embedUrl, sanitizedEmbedCode, embedType }: { embedUrl: string; @@ -43,9 +58,14 @@ const MemoizedEmbed = React.memo(({ embedUrl, sanitizedEmbedCode, embedType }: { }, [embedType, sanitizedEmbedCode]); if (embedType === 'url' && embedUrl) { + // Process the URL if it's a YouTube URL + const processedUrl = embedUrl.includes('youtube.com') || embedUrl.includes('youtu.be') + ? getYouTubeEmbedUrl(embedUrl) + : embedUrl; + return (