-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathvideo.ts
203 lines (177 loc) · 4.84 KB
/
video.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
import type { GeneralOptions } from '@/type'
import { VIDEO_SIZE } from '@/constants/define'
import { getCssUnitWithDefault } from '@/utils/utils'
import { Node } from '@tiptap/core'
import VideoDialog from './components/video/VideoDialog.vue'
import VideoActionButton from './components/VideoActionButton.vue'
/**
* Represents the interface for video options, extending GeneralOptions.
*/
export interface VideoOptions extends GeneralOptions<VideoOptions> {
/**
* Indicates whether fullscreen play is allowed
*
* @default true
*/
allowFullscreen: boolean
/**
* Indicates whether to display the frameborder
*
* @default false
*/
frameborder: boolean
/**
* Width of the video, can be a number or string
*
* @default VIDEO_SIZE['size-medium']
*/
width: number | string
/** HTML attributes object for passing additional attributes */
HTMLAttributes: {
[key: string]: any
}
/** Component for the video dialog */
dialogComponent: any
}
/**
* Represents the type for setting video options
*/
type SetVideoOptions = {
/** The source URL of the video */
src: string
/** The width of the video */
width: string | number
}
declare module '@tiptap/core' {
interface Commands<ReturnType> {
video: {
/**
* Add an video
*/
setVideo: (options: Partial<SetVideoOptions>) => ReturnType
/**
* Update an video
*/
updateVideo: (options: Partial<SetVideoOptions>) => ReturnType
}
}
}
function linkConvert(src: string) {
// Convert youtube links
src = src.replace('https://youtu.be/', 'https://www.youtube.com/watch?v=').replace('watch?v=', 'embed/')
// Convert vimeo links
src = src.replace('https://vimeo.com/', 'https://player.vimeo.com/video/')
// Convert bilibili links
const isBilibiliLink = /^https?:\/\/www.bilibili.com\/video\/.*/i.test(src)
if (isBilibiliLink) {
src = src
.replace(/\?.*$/i, '')
.replace('https://www.bilibili.com/video/', 'https://player.bilibili.com/player.html?bvid=')
}
// Convert google drive links
if (src.includes('drive.google.com')) {
src = src.replace('/view', '/preview')
}
return src
}
export const Video = /* @__PURE__*/ Node.create<VideoOptions>({
name: 'video',
group: 'block',
atom: true,
draggable: true,
addAttributes() {
return {
src: {
default: null,
renderHTML: ({ src }) => ({
src: src ? linkConvert(src) : null
})
},
width: {
default: this.options.width,
renderHTML: ({ width }) => ({
width: getCssUnitWithDefault(width)
})
},
frameborder: {
default: this.options.frameborder ? 1 : 0,
parseHTML: () => (this.options.frameborder ? 1 : 0)
},
allowfullscreen: {
default: this.options.allowFullscreen,
parseHTML: () => this.options.allowFullscreen
}
}
},
parseHTML() {
return [
{
tag: 'div[data-video] iframe'
}
]
},
renderHTML({ HTMLAttributes }) {
const { width = '100%' } = HTMLAttributes ?? {}
const iframeHTMLAttributes = {
...HTMLAttributes,
width: '100%',
height: '100%'
}
const responsiveStyle = `position: relative;overflow: hidden;display: flex;flex: 1;max-width: ${width};`
const responsiveSizesStyle = `flex: 1;padding-bottom: ${(9 / 16) * 100}%;`
const iframeDOM = ['iframe', iframeHTMLAttributes]
const sizesDOM = ['div', { style: responsiveSizesStyle }]
const responsiveDOM = ['div', { style: responsiveStyle }, sizesDOM, iframeDOM]
const divAttrs = {
...this.options.HTMLAttributes,
'data-video': ''
}
return ['div', divAttrs, responsiveDOM]
},
addCommands() {
return {
setVideo:
options =>
({ commands }) => {
return commands.insertContent({
type: this.name,
attrs: options
})
},
updateVideo:
options =>
({ commands }) => {
return commands.updateAttributes(this.name, options)
}
}
},
addOptions() {
return {
divider: false,
spacer: false,
allowFullscreen: true,
frameborder: false,
width: VIDEO_SIZE['size-medium'],
HTMLAttributes: {
class: 'iframe-wrapper',
style: 'display: flex;justify-content: center;'
},
dialogComponent: () => VideoDialog,
button: ({ editor, extension, t }) => {
const { dialogComponent } = extension.options
return {
component: VideoActionButton,
componentProps: {
isActive: () => editor.isActive('video') || false,
icon: 'video',
tooltip: t('editor.video.tooltip')
},
componentSlots: {
dialog: dialogComponent()
},
disabled: !editor.can().setVideo({})
}
}
}
}
})