Skip to content

Commit

Permalink
QuillEditor: Use youtube embed link is video is from youtube
Browse files Browse the repository at this point in the history
  • Loading branch information
sashko9807 committed Jan 3, 2024
1 parent 3762cde commit bc22e8f
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/components/common/form/QuillEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,30 @@ export function QuillEditor({ value, onChange }: QuillEditorProps) {
}
}

function handleVideoInsert() {
let imageUrl = prompt('Enter the URL of the video:') // for a better UX find a way to use the Quill Tooltip or a Modal box
if (!imageUrl) return

const editor = reactQuillRef.current?.getEditor()
if (!editor) return

const unprivilegedEditor = reactQuillRef.current?.makeUnprivilegedEditor(editor)
if (unprivilegedEditor) {
//check if the link is from google drive and if so, change the link to a direct link
const isYoutubeLink = imageUrl.match(
/(youtu.*be.*)\/(watch\?v=|embed\/|v|shorts|)(.*?((?=[&#?])|$))/,
)
//if video is from youtube, use youtube embed URL
if (isYoutubeLink) {
const videoId = isYoutubeLink[3]
imageUrl = 'https://youtube.com/embed/' + videoId
}

const range = unprivilegedEditor.getSelection(true)
editor.insertEmbed(range.index, 'video', imageUrl)
}
}

const modules = useMemo(
() => ({
toolbar: {
Expand All @@ -120,7 +144,7 @@ export function QuillEditor({ value, onChange }: QuillEditorProps) {
['link', 'video', 'image'],
['clean'],
],
handlers: { image: handleImageUrlInsert },
handlers: { image: handleImageUrlInsert, video: handleVideoInsert },
},
clipboard: {
// toggle to add extra line breaks when pasting HTML:
Expand Down

0 comments on commit bc22e8f

Please sign in to comment.