-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathresizableMedia.ts
183 lines (158 loc) Β· 4.04 KB
/
resizableMedia.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
import { mergeAttributes, Node, nodeInputRule } from '@tiptap/core'
import { VueNodeViewRenderer } from '@tiptap/vue-3'
import ResizableMediaNodeView from './ResizableMediaNodeView.vue'
declare module '@tiptap/core' {
interface Commands<ReturnType> {
resizableMedia: {
/**
* Set media
*/
setMedia: (options: {
'media-type': 'img' | 'video'
src: string
alt?: string
title?: string
width?: string
height?: string
}) => ReturnType;
};
}
}
export interface MediaOptions {
// inline: boolean, // we have floating support, so block is good enough
// allowBase64: boolean, // we're not going to allow this
HTMLAttributes: Record<string, any>,
}
export const IMAGE_INPUT_REGEX = /(?:^|\s)(!\[(.+|:?)]\((\S+)(?:(?:\s+)["'](\S+)["'])?\))$/
export const VIDEO_INPUT_REGEX = /!\[(.+|:?)]\((\S+)(?:(?:\s+)["'](\S+)["'])?\)/
export const ResizableMedia = Node.create<MediaOptions>({
name: 'resizableMedia',
addOptions () {
return {
inline: false,
allowBase64: false,
HTMLAttributes: {},
}
},
inline: false,
group: 'block',
draggable: true,
addAttributes () {
return {
src: {
default: null,
},
'media-type': {
default: null
},
alt: {
default: null,
},
title: {
default: null,
},
width: {
default: '100%',
},
height: {
default: 'auto'
},
dataAlign: {
default: 'left' // 'left' | 'center' | 'right'
},
dataFloat: {
default: null // 'left' | 'right'
},
}
},
selectable: true,
parseHTML () {
return [
{
tag: 'img[src]:not([src^="data:"])',
getAttrs: el => ({
src: (el as HTMLImageElement).getAttribute('src'),
'media-type': 'img'
})
},
{
tag: 'video',
getAttrs: el => ({
src: (el as HTMLVideoElement).getAttribute('src'),
'media-type': 'video'
}),
},
]
},
renderHTML ({ HTMLAttributes }) {
const { 'media-type': mediaType } = HTMLAttributes
if (mediaType === 'img') {
return [ 'img', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes) ]
} else if (mediaType === 'video') {
return [
'video',
{ controls: 'true', style: 'width: 100%', ...HTMLAttributes },
[ 'source', HTMLAttributes ]
]
}
if (!mediaType) console.error('TiptapMediaExtension-renderHTML method: Media Type not set, going default with image')
return [ 'img', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes) ]
},
addCommands () {
return {
setMedia: options => ({ commands }) => {
const { 'media-type': mediaType } = options
if (mediaType === 'img') {
return commands.insertContent({
type: this.name,
attrs: options,
})
} else if (mediaType === 'video') {
return commands.insertContent({
type: this.name,
attrs: {
...options,
controls: 'true'
},
})
}
if (!mediaType) console.error('TiptapMediaExtension-setMedia: Media Type not set, going default with image')
return commands.insertContent({
type: this.name,
attrs: options,
})
},
}
},
addNodeView () {
return VueNodeViewRenderer(ResizableMediaNodeView)
},
addInputRules () {
return [
nodeInputRule({
find: IMAGE_INPUT_REGEX,
type: this.type,
getAttributes: match => {
const [ , , alt, src, title ] = match
return {
src,
alt,
title,
'media-type': 'img'
}
},
}),
nodeInputRule({
find: VIDEO_INPUT_REGEX,
type: this.type,
getAttributes: (match) => {
const [ ,, src ] = match
return {
src,
'media-type': 'video'
}
},
})
]
},
})