-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
youtube.ts
325 lines (287 loc) · 7.24 KB
/
youtube.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import { mergeAttributes, Node, nodePasteRule } from '@tiptap/core'
import { getEmbedUrlFromYoutubeUrl, isValidYoutubeUrl, YOUTUBE_REGEX_GLOBAL } from './utils.js'
export interface YoutubeOptions {
/**
* Controls if the paste handler for youtube videos should be added.
* @default true
* @example false
*/
addPasteHandler: boolean;
/**
* Controls if the youtube video should be allowed to go fullscreen.
* @default true
* @example false
*/
allowFullscreen: boolean;
/**
* Controls if the youtube video should autoplay.
* @default false
* @example true
*/
autoplay: boolean;
/**
* The language of the captions shown in the youtube video.
* @default undefined
* @example 'en'
*/
ccLanguage?: string;
/**
* Controls if the captions should be shown in the youtube video.
* @default undefined
* @example true
*/
ccLoadPolicy?: boolean;
/**
* Controls if the controls should be shown in the youtube video.
* @default true
* @example false
*/
controls: boolean;
/**
* Controls if the keyboard controls should be disabled in the youtube video.
* @default false
* @example true
*/
disableKBcontrols: boolean;
/**
* Controls if the iframe api should be enabled in the youtube video.
* @default false
* @example true
*/
enableIFrameApi: boolean;
/**
* The end time of the youtube video.
* @default 0
* @example 120
*/
endTime: number;
/**
* The height of the youtube video.
* @default 480
* @example 720
*/
height: number;
/**
* The language of the youtube video.
* @default undefined
* @example 'en'
*/
interfaceLanguage?: string;
/**
* Controls if the video annotations should be shown in the youtube video.
* @default 0
* @example 1
*/
ivLoadPolicy: number;
/**
* Controls if the youtube video should loop.
* @default false
* @example true
*/
loop: boolean;
/**
* Controls if the youtube video should show a small youtube logo.
* @default false
* @example true
*/
modestBranding: boolean;
/**
* The HTML attributes for a youtube video node.
* @default {}
* @example { class: 'foo' }
*/
HTMLAttributes: Record<string, any>;
/**
* Controls if the youtube node should be inline or not.
* @default false
* @example true
*/
inline: boolean;
/**
* Controls if the youtube video should be loaded from youtube-nocookie.com.
* @default false
* @example true
*/
nocookie: boolean;
/**
* The origin of the youtube video.
* @default ''
* @example 'https://tiptap.dev'
*/
origin: string;
/**
* The playlist of the youtube video.
* @default ''
* @example 'PLQg6GaokU5CwiVmsZ0dZm6VeIg0V5z1tK'
*/
playlist: string;
/**
* The color of the youtube video progress bar.
* @default undefined
* @example 'red'
*/
progressBarColor?: string;
/**
* The width of the youtube video.
* @default 640
* @example 1280
*/
width: number;
}
/**
* The options for setting a youtube video.
*/
type SetYoutubeVideoOptions = { src: string, width?: number, height?: number, start?: number }
declare module '@tiptap/core' {
interface Commands<ReturnType> {
youtube: {
/**
* Insert a youtube video
* @param options The youtube video attributes
* @example editor.commands.setYoutubeVideo({ src: 'https://www.youtube.com/watch?v=dQw4w9WgXcQ' })
*/
setYoutubeVideo: (options: SetYoutubeVideoOptions) => ReturnType,
}
}
}
/**
* This extension adds support for youtube videos.
* @see https://www.tiptap.dev/api/nodes/youtube
*/
export const Youtube = Node.create<YoutubeOptions>({
name: 'youtube',
addOptions() {
return {
addPasteHandler: true,
allowFullscreen: true,
autoplay: false,
ccLanguage: undefined,
ccLoadPolicy: undefined,
controls: true,
disableKBcontrols: false,
enableIFrameApi: false,
endTime: 0,
height: 480,
interfaceLanguage: undefined,
ivLoadPolicy: 0,
loop: false,
modestBranding: false,
HTMLAttributes: {},
inline: false,
nocookie: false,
origin: '',
playlist: '',
progressBarColor: undefined,
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 {
setYoutubeVideo: (options: SetYoutubeVideoOptions) => ({ commands }) => {
if (!isValidYoutubeUrl(options.src)) {
return false
}
return commands.insertContent({
type: this.name,
attrs: options,
})
},
}
},
addPasteRules() {
if (!this.options.addPasteHandler) {
return []
}
return [
nodePasteRule({
find: YOUTUBE_REGEX_GLOBAL,
type: this.type,
getAttributes: match => {
return { src: match.input }
},
}),
]
},
renderHTML({ HTMLAttributes }) {
const embedUrl = getEmbedUrlFromYoutubeUrl({
url: HTMLAttributes.src,
allowFullscreen: this.options.allowFullscreen,
autoplay: this.options.autoplay,
ccLanguage: this.options.ccLanguage,
ccLoadPolicy: this.options.ccLoadPolicy,
controls: this.options.controls,
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,
nocookie: this.options.nocookie,
origin: this.options.origin,
playlist: this.options.playlist,
progressBarColor: this.options.progressBarColor,
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,
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,
},
HTMLAttributes,
),
],
]
},
})