Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QuillEditor: Use youtube embed URL if source is from youtube #1697

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 videoUrl = prompt('Enter the URL of the video:') // for a better UX find a way to use the Quill Tooltip or a Modal box
if (!videoUrl) 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 = videoUrl.match(
/(youtu.*be.*)\/(watch\?v=|embed\/|v|shorts|)(.*?((?=[&#?])|$))/,
)
//if video is from youtube, use youtube embed URL
if (isYoutubeLink) {
const videoId = isYoutubeLink[3]
videoUrl = 'https://youtube.com/embed/' + videoId
}

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

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
Loading