PluginProbe
Block Animations, Motion & Scroll Effects – Ghost Kit / 3.7.2
Block Animations, Motion & Scroll Effects – Ghost Kit v3.7.2
3.7.2 3.7.1 3.7.0 3.6.1 trunk 1.6.3 2.25.0 3.3.0 3.3.1 3.3.2 3.3.3 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.6 3.5.0 3.5.1 3.6.0
ghostkit / gutenberg / blocks / video / transforms.js

transforms.js in Block Animations, Motion & Scroll Effects – Ghost Kit 3.7.2, at gutenberg/blocks/video/transforms.js

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