feat(extension/youtube): add paste handlers for youtube extension
This commit is contained in:
@@ -1,5 +1,8 @@
|
|||||||
|
export const YOUTUBE_REGEX = /^(https?:\/\/)?(www\.|music\.)?(youtube\.com|youtu\.be)(.+)?$/
|
||||||
|
export const YOUTUBE_REGEX_GLOBAL = /^(https?:\/\/)?(www\.|music\.)?(youtube\.com|youtu\.be)(.+)?$/g
|
||||||
|
|
||||||
export const isValidYoutubeUrl = (url: string) => {
|
export const isValidYoutubeUrl = (url: string) => {
|
||||||
return url.match(/^(https?:\/\/)?(www\.|music\.)?(youtube\.com|youtu\.be)(.+)?$/)
|
return url.match(YOUTUBE_REGEX)
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface GetEmbedUrlOptions {
|
export interface GetEmbedUrlOptions {
|
||||||
|
|||||||
@@ -1,15 +1,16 @@
|
|||||||
import { mergeAttributes, Node } from '@tiptap/core'
|
import { mergeAttributes, Node, PasteRule } from '@tiptap/core'
|
||||||
|
|
||||||
import { getEmbedURLFromYoutubeURL, isValidYoutubeUrl } from './utils'
|
import { getEmbedURLFromYoutubeURL, isValidYoutubeUrl, YOUTUBE_REGEX_GLOBAL } from './utils'
|
||||||
|
|
||||||
export interface YoutubeOptions {
|
export interface YoutubeOptions {
|
||||||
inline: boolean;
|
addPasteHandler: boolean;
|
||||||
width: number;
|
|
||||||
height: number;
|
|
||||||
controls: boolean;
|
|
||||||
nocookie: boolean;
|
|
||||||
allowFullscreen: boolean;
|
allowFullscreen: boolean;
|
||||||
|
controls: boolean;
|
||||||
|
height: number;
|
||||||
HTMLAttributes: Record<string, any>,
|
HTMLAttributes: Record<string, any>,
|
||||||
|
inline: boolean;
|
||||||
|
nocookie: boolean;
|
||||||
|
width: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
declare module '@tiptap/core' {
|
declare module '@tiptap/core' {
|
||||||
@@ -28,13 +29,14 @@ export const Youtube = Node.create<YoutubeOptions>({
|
|||||||
|
|
||||||
addOptions() {
|
addOptions() {
|
||||||
return {
|
return {
|
||||||
inline: false,
|
addPasteHandler: true,
|
||||||
controls: true,
|
|
||||||
HTMLAttributes: {},
|
|
||||||
nocookie: false,
|
|
||||||
allowFullscreen: false,
|
allowFullscreen: false,
|
||||||
width: 640,
|
controls: true,
|
||||||
height: 480,
|
height: 480,
|
||||||
|
HTMLAttributes: {},
|
||||||
|
inline: false,
|
||||||
|
nocookie: false,
|
||||||
|
width: 640,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -88,6 +90,28 @@ export const Youtube = Node.create<YoutubeOptions>({
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
addPasteRules() {
|
||||||
|
if (!this.options.addPasteHandler) {
|
||||||
|
return []
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
new PasteRule({
|
||||||
|
find: YOUTUBE_REGEX_GLOBAL,
|
||||||
|
|
||||||
|
handler({ match, chain, range }) {
|
||||||
|
if (match.input) {
|
||||||
|
chain()
|
||||||
|
.deleteRange(range)
|
||||||
|
.setYoutubeVideo({
|
||||||
|
src: match.input,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
]
|
||||||
|
},
|
||||||
|
|
||||||
renderHTML({ HTMLAttributes }) {
|
renderHTML({ HTMLAttributes }) {
|
||||||
const embedUrl = getEmbedURLFromYoutubeURL({
|
const embedUrl = getEmbedURLFromYoutubeURL({
|
||||||
url: HTMLAttributes.src,
|
url: HTMLAttributes.src,
|
||||||
|
|||||||
Reference in New Issue
Block a user