2022-06-25 18:07:33 +08:00
|
|
|
import { mergeAttributes, Node, nodePasteRule } from '@tiptap/core'
|
2022-06-17 11:29:48 +08:00
|
|
|
|
2022-12-07 07:45:51 +08:00
|
|
|
import { getEmbedUrlFromYoutubeUrl, isValidYoutubeUrl, YOUTUBE_REGEX_GLOBAL } from './utils'
|
2022-06-17 11:29:48 +08:00
|
|
|
|
|
|
|
export interface YoutubeOptions {
|
2022-06-25 17:16:17 +08:00
|
|
|
addPasteHandler: boolean;
|
2022-06-17 11:29:48 +08:00
|
|
|
allowFullscreen: boolean;
|
2022-10-17 23:28:30 +08:00
|
|
|
autoplay: boolean;
|
2022-11-04 23:51:06 +08:00
|
|
|
ccLanguage?: string;
|
|
|
|
ccLoadPolicy?: boolean;
|
2022-06-25 17:16:17 +08:00
|
|
|
controls: boolean;
|
2022-10-17 23:28:30 +08:00
|
|
|
disableKBcontrols: boolean;
|
|
|
|
enableIFrameApi: boolean;
|
|
|
|
endTime: number;
|
2022-06-25 17:16:17 +08:00
|
|
|
height: number;
|
2022-11-04 23:51:06 +08:00
|
|
|
interfaceLanguage?: string;
|
2022-10-17 23:28:30 +08:00
|
|
|
ivLoadPolicy: number;
|
|
|
|
loop: boolean;
|
|
|
|
modestBranding: boolean;
|
|
|
|
HTMLAttributes: Record<string, any>;
|
2022-06-25 17:16:17 +08:00
|
|
|
inline: boolean;
|
|
|
|
nocookie: boolean;
|
2022-10-17 23:28:30 +08:00
|
|
|
origin: string;
|
|
|
|
playlist: string;
|
2022-11-04 23:51:06 +08:00
|
|
|
progressBarColor?: string;
|
2022-06-25 17:16:17 +08:00
|
|
|
width: number;
|
2022-06-17 11:29:48 +08:00
|
|
|
}
|
|
|
|
|
2023-03-23 16:54:58 +08:00
|
|
|
type SetYoutubeVideoOptions = { src: string, width?: number, height?: number, start?: number }
|
|
|
|
|
2022-06-17 11:29:48 +08:00
|
|
|
declare module '@tiptap/core' {
|
|
|
|
interface Commands<ReturnType> {
|
|
|
|
youtube: {
|
|
|
|
/**
|
|
|
|
* Insert a youtube video
|
|
|
|
*/
|
2023-03-23 16:54:58 +08:00
|
|
|
setYoutubeVideo: (options: SetYoutubeVideoOptions) => ReturnType,
|
2022-06-17 11:29:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const Youtube = Node.create<YoutubeOptions>({
|
|
|
|
name: 'youtube',
|
|
|
|
|
|
|
|
addOptions() {
|
|
|
|
return {
|
2022-06-25 17:16:17 +08:00
|
|
|
addPasteHandler: true,
|
2022-11-05 00:21:52 +08:00
|
|
|
allowFullscreen: true,
|
2022-10-17 23:28:30 +08:00
|
|
|
autoplay: false,
|
2022-11-04 23:51:06 +08:00
|
|
|
ccLanguage: undefined,
|
|
|
|
ccLoadPolicy: undefined,
|
2022-06-17 11:29:48 +08:00
|
|
|
controls: true,
|
2022-10-17 23:28:30 +08:00
|
|
|
disableKBcontrols: false,
|
|
|
|
enableIFrameApi: false,
|
|
|
|
endTime: 0,
|
2022-06-25 17:16:17 +08:00
|
|
|
height: 480,
|
2022-11-04 23:51:06 +08:00
|
|
|
interfaceLanguage: undefined,
|
|
|
|
ivLoadPolicy: 0,
|
2022-10-17 23:28:30 +08:00
|
|
|
loop: false,
|
|
|
|
modestBranding: false,
|
2022-06-17 11:29:48 +08:00
|
|
|
HTMLAttributes: {},
|
2022-06-25 17:16:17 +08:00
|
|
|
inline: false,
|
2022-06-17 11:29:48 +08:00
|
|
|
nocookie: false,
|
2022-10-17 23:28:30 +08:00
|
|
|
origin: '',
|
|
|
|
playlist: '',
|
2022-11-04 23:51:06 +08:00
|
|
|
progressBarColor: undefined,
|
2022-06-17 11:29:48 +08:00
|
|
|
width: 640,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
inline() {
|
|
|
|
return this.options.inline
|
|
|
|
},
|
|
|
|
|
|
|
|
group() {
|
|
|
|
return this.options.inline ? 'inline' : 'block'
|
|
|
|
},
|
|
|
|
|
|
|
|
draggable: true,
|
|
|
|
|
|
|
|
addAttributes() {
|
|
|
|
return {
|
|
|
|
src: {
|
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
start: {
|
|
|
|
default: 0,
|
|
|
|
},
|
|
|
|
width: {
|
|
|
|
default: this.options.width,
|
|
|
|
},
|
|
|
|
height: {
|
|
|
|
default: this.options.height,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
parseHTML() {
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
tag: 'div[data-youtube-video] iframe',
|
|
|
|
},
|
|
|
|
]
|
|
|
|
},
|
|
|
|
|
|
|
|
addCommands() {
|
|
|
|
return {
|
2023-03-23 16:54:58 +08:00
|
|
|
setYoutubeVideo: (options: SetYoutubeVideoOptions) => ({ commands }) => {
|
2022-06-17 11:29:48 +08:00
|
|
|
if (!isValidYoutubeUrl(options.src)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return commands.insertContent({
|
|
|
|
type: this.name,
|
|
|
|
attrs: options,
|
|
|
|
})
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2022-06-25 17:16:17 +08:00
|
|
|
addPasteRules() {
|
|
|
|
if (!this.options.addPasteHandler) {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
2022-06-25 18:07:33 +08:00
|
|
|
nodePasteRule({
|
2022-06-25 17:16:17 +08:00
|
|
|
find: YOUTUBE_REGEX_GLOBAL,
|
2022-06-25 18:07:33 +08:00
|
|
|
type: this.type,
|
|
|
|
getAttributes: match => {
|
|
|
|
return { src: match.input }
|
2022-06-25 17:16:17 +08:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
]
|
|
|
|
},
|
|
|
|
|
2022-06-17 11:29:48 +08:00
|
|
|
renderHTML({ HTMLAttributes }) {
|
2022-12-07 07:45:51 +08:00
|
|
|
const embedUrl = getEmbedUrlFromYoutubeUrl({
|
2022-06-17 11:29:48 +08:00
|
|
|
url: HTMLAttributes.src,
|
2022-10-17 23:28:30 +08:00
|
|
|
allowFullscreen: this.options.allowFullscreen,
|
|
|
|
autoplay: this.options.autoplay,
|
|
|
|
ccLanguage: this.options.ccLanguage,
|
|
|
|
ccLoadPolicy: this.options.ccLoadPolicy,
|
2022-06-17 11:29:48 +08:00
|
|
|
controls: this.options.controls,
|
2022-10-17 23:28:30 +08:00
|
|
|
disableKBcontrols: this.options.disableKBcontrols,
|
|
|
|
enableIFrameApi: this.options.enableIFrameApi,
|
|
|
|
endTime: this.options.endTime,
|
|
|
|
interfaceLanguage: this.options.interfaceLanguage,
|
|
|
|
ivLoadPolicy: this.options.ivLoadPolicy,
|
|
|
|
loop: this.options.loop,
|
|
|
|
modestBranding: this.options.modestBranding,
|
2022-06-17 11:29:48 +08:00
|
|
|
nocookie: this.options.nocookie,
|
2022-10-17 23:28:30 +08:00
|
|
|
origin: this.options.origin,
|
|
|
|
playlist: this.options.playlist,
|
|
|
|
progressBarColor: this.options.progressBarColor,
|
2022-06-17 11:29:48 +08:00
|
|
|
startAt: HTMLAttributes.start || 0,
|
|
|
|
})
|
|
|
|
|
|
|
|
HTMLAttributes.src = embedUrl
|
|
|
|
|
|
|
|
return [
|
|
|
|
'div',
|
|
|
|
{ 'data-youtube-video': '' },
|
|
|
|
[
|
|
|
|
'iframe',
|
|
|
|
mergeAttributes(
|
|
|
|
this.options.HTMLAttributes,
|
|
|
|
{
|
|
|
|
width: this.options.width,
|
|
|
|
height: this.options.height,
|
|
|
|
allowfullscreen: this.options.allowFullscreen,
|
2022-10-17 23:28:30 +08:00
|
|
|
autoplay: this.options.autoplay,
|
|
|
|
ccLanguage: this.options.ccLanguage,
|
|
|
|
ccLoadPolicy: this.options.ccLoadPolicy,
|
|
|
|
disableKBcontrols: this.options.disableKBcontrols,
|
|
|
|
enableIFrameApi: this.options.enableIFrameApi,
|
|
|
|
endTime: this.options.endTime,
|
|
|
|
interfaceLanguage: this.options.interfaceLanguage,
|
|
|
|
ivLoadPolicy: this.options.ivLoadPolicy,
|
|
|
|
loop: this.options.loop,
|
|
|
|
modestBranding: this.options.modestBranding,
|
|
|
|
origin: this.options.origin,
|
|
|
|
playlist: this.options.playlist,
|
|
|
|
progressBarColor: this.options.progressBarColor,
|
2022-06-17 11:29:48 +08:00
|
|
|
},
|
|
|
|
HTMLAttributes,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
]
|
|
|
|
},
|
|
|
|
})
|