| 1 |
import { createBlock } from '@wordpress/blocks'; |
| 2 |
|
| 3 |
// parse youtube ID |
| 4 |
function getYoutubeID(ytUrl) { |
| 5 |
const regExp = |
| 6 |
/.*(?:youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=)([^#\&\?]*).*/; |
| 7 |
const match = ytUrl.match(regExp); |
| 8 |
return match && match[1].length === 11 ? match[1] : false; |
| 9 |
} |
| 10 |
|
| 11 |
// parse vimeo ID |
| 12 |
function getVimeoID(vmUrl) { |
| 13 |
const regExp = |
| 14 |
/https?:\/\/(?:www\.|player\.)?vimeo.com\/(?:channels\/(?:\w+\/)?|groups\/([^/]*)\/videos\/|album\/(\d+)\/video\/|video\/|)(\d+)(?:$|\/|\?)/; |
| 15 |
const match = vmUrl.match(regExp); |
| 16 |
return match && match[3] ? match[3] : false; |
| 17 |
} |
| 18 |
|
| 19 |
// prepare attributes for core embed |
| 20 |
function prepareAttributesForCoreEmbed(attributes) { |
| 21 |
let aspectRatio = '16-9'; |
| 22 |
const { video, videoAspectRatio } = attributes; |
| 23 |
let { className } = attributes; |
| 24 |
|
| 25 |
if (videoAspectRatio === '21:9') { |
| 26 |
aspectRatio = '21-9'; |
| 27 |
} else if (videoAspectRatio === '4:3') { |
| 28 |
aspectRatio = '4-3'; |
| 29 |
} else if (videoAspectRatio === '3:2') { |
| 30 |
aspectRatio = '3-2'; |
| 31 |
} |
| 32 |
|
| 33 |
if (aspectRatio) { |
| 34 |
className = `wp-embed-aspect-${aspectRatio} wp-has-aspect-ratio ${className}`; |
| 35 |
} |
| 36 |
|
| 37 |
return { |
| 38 |
url: video, |
| 39 |
className, |
| 40 |
}; |
| 41 |
} |
| 42 |
|
| 43 |
export default { |
| 44 |
from: [ |
| 45 |
{ |
| 46 |
type: 'block', |
| 47 |
blocks: ['core-embed/youtube', 'core-embed/vimeo'], |
| 48 |
isMatch(attributes) { |
| 49 |
return ( |
| 50 |
attributes && attributes.type && attributes.type === 'video' |
| 51 |
); |
| 52 |
}, |
| 53 |
transform(attributes) { |
| 54 |
let aspectRatio = '16:9'; |
| 55 |
const { url, className } = attributes; |
| 56 |
|
| 57 |
if (className && /wp-embed-aspect-21-9/.test(className)) { |
| 58 |
aspectRatio = '21:9'; |
| 59 |
} else if (className && /wp-embed-aspect-4-3/.test(className)) { |
| 60 |
aspectRatio = '4:3'; |
| 61 |
} else if (className && /wp-embed-aspect-3-2/.test(className)) { |
| 62 |
aspectRatio = '3:2'; |
| 63 |
} |
| 64 |
|
| 65 |
return createBlock('ghostkit/video', { |
| 66 |
video: url, |
| 67 |
videoAspectRatio: aspectRatio, |
| 68 |
}); |
| 69 |
}, |
| 70 |
}, |
| 71 |
{ |
| 72 |
type: 'block', |
| 73 |
blocks: ['core/video'], |
| 74 |
isMatch(attributes) { |
| 75 |
return attributes && attributes.id; |
| 76 |
}, |
| 77 |
transform(attributes) { |
| 78 |
const { id, src, muted } = attributes; |
| 79 |
|
| 80 |
const attrs = { |
| 81 |
type: 'video', |
| 82 |
videoVolume: muted ? 0 : 100, |
| 83 |
}; |
| 84 |
|
| 85 |
if (/\.ogv$|\.ogg$/.test(src)) { |
| 86 |
attrs.videoOgv = src; |
| 87 |
attrs.videoOgvId = id; |
| 88 |
} else if (/\.webm$/.test(src)) { |
| 89 |
attrs.videoWebm = src; |
| 90 |
attrs.videoWebmId = id; |
| 91 |
} else { |
| 92 |
attrs.videoMp4 = src; |
| 93 |
attrs.videoMp4Id = id; |
| 94 |
} |
| 95 |
|
| 96 |
return createBlock('ghostkit/video', attrs); |
| 97 |
}, |
| 98 |
}, |
| 99 |
], |
| 100 |
to: [ |
| 101 |
{ |
| 102 |
type: 'block', |
| 103 |
blocks: ['core-embed/youtube'], |
| 104 |
isMatch(attributes) { |
| 105 |
return ( |
| 106 |
attributes && |
| 107 |
attributes.type && |
| 108 |
attributes.type === 'yt_vm_video' && |
| 109 |
attributes.video && |
| 110 |
getYoutubeID(attributes.video) |
| 111 |
); |
| 112 |
}, |
| 113 |
transform(attributes) { |
| 114 |
return createBlock( |
| 115 |
'core-embed/youtube', |
| 116 |
prepareAttributesForCoreEmbed(attributes) |
| 117 |
); |
| 118 |
}, |
| 119 |
}, |
| 120 |
{ |
| 121 |
type: 'block', |
| 122 |
blocks: ['core-embed/vimeo'], |
| 123 |
isMatch(attributes) { |
| 124 |
return ( |
| 125 |
attributes && |
| 126 |
attributes.type && |
| 127 |
attributes.type === 'yt_vm_video' && |
| 128 |
attributes.video && |
| 129 |
getVimeoID(attributes.video) |
| 130 |
); |
| 131 |
}, |
| 132 |
transform(attributes) { |
| 133 |
return createBlock( |
| 134 |
'core-embed/vimeo', |
| 135 |
prepareAttributesForCoreEmbed(attributes) |
| 136 |
); |
| 137 |
}, |
| 138 |
}, |
| 139 |
{ |
| 140 |
type: 'block', |
| 141 |
blocks: ['core/video'], |
| 142 |
isMatch(attributes) { |
| 143 |
return ( |
| 144 |
attributes && |
| 145 |
attributes.type && |
| 146 |
attributes.type === 'video' && |
| 147 |
(attributes.videoMp4Id || |
| 148 |
attributes.videoOgvId || |
| 149 |
attributes.videoWebmId) && |
| 150 |
(attributes.videoMp4 || |
| 151 |
attributes.videoOgv || |
| 152 |
attributes.videoWebm) |
| 153 |
); |
| 154 |
}, |
| 155 |
transform(attributes) { |
| 156 |
return createBlock('core/video', { |
| 157 |
id: |
| 158 |
attributes.videoMp4Id || |
| 159 |
attributes.videoOgvId || |
| 160 |
attributes.videoWebmId, |
| 161 |
src: |
| 162 |
attributes.videoMp4 || |
| 163 |
attributes.videoOgv || |
| 164 |
attributes.videoWebm, |
| 165 |
muted: attributes.videoVolume === 0, |
| 166 |
}); |
| 167 |
}, |
| 168 |
}, |
| 169 |
], |
| 170 |
}; |
| 171 |
|