-
Notifications
You must be signed in to change notification settings - Fork 58
/
jetpack-editor-setup.js
197 lines (174 loc) · 5.06 KB
/
jetpack-editor-setup.js
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
/**
* WordPress dependencies
*/
import { dispatch, select } from '@wordpress/data';
import { store as editPostStore } from '@wordpress/edit-post';
import { addAction, addFilter } from '@wordpress/hooks';
import { store as blockEditorStore } from '@wordpress/block-editor';
/**
* Internal dependencies
*/
import { JETPACK_DATA_PATH } from '../jetpack/projects/js-packages/shared-extension-utils/src/get-jetpack-data';
import isActive from '../jetpack/projects/plugins/jetpack/extensions/shared/is-active';
import {
reactivateFacebookEmbedBlockVariation,
reactivateInstagramEmbedBlockVariation,
registerLoomVariation,
registerSmartframeVariation,
} from '../jetpack/projects/plugins/jetpack/extensions/extended-blocks/core-embed';
// When adding new blocks to this list please also consider updating `./block-support/supported-blocks.json`
const supportedJetpackBlocks = {
'contact-info': {
available: true,
},
paywall: {
available: true,
},
'tiled-gallery': {
available: __DEV__,
},
'videopress/video': {
available: true,
},
};
const setJetpackData = ( {
isJetpackActive = false,
userData = null,
siteFragment = null,
blogId,
} ) => {
const availableBlocks = supportedJetpackBlocks;
const jetpackEditorInitialState = {
available_blocks: availableBlocks,
jetpack: {
is_active: isJetpackActive,
},
siteFragment,
tracksUserData: userData,
wpcomBlogId: blogId,
};
global.window[ JETPACK_DATA_PATH ] = jetpackEditorInitialState;
return jetpackEditorInitialState;
};
const hideBlockByCapability = ( capability, blockName ) => {
if ( capability !== true ) {
dispatch( editPostStore ).hideBlockTypes( [ blockName ] );
} else {
dispatch( editPostStore ).showBlockTypes( [ blockName ] );
}
};
export function setupJetpackEditor( jetpackState ) {
if ( ! jetpackState.isJetpackActive ) {
return;
}
return setJetpackData( jetpackState );
}
export function registerJetpackBlocks( { capabilities } ) {
if ( ! isActive() || capabilities.onlyCoreBlocks ) {
return;
}
hideBlockByCapability(
capabilities.contactInfoBlock,
'jetpack/contact-info'
);
hideBlockByCapability(
capabilities.tiledGalleryBlock,
'jetpack/tiled-gallery'
);
hideBlockByCapability( capabilities.videoPressBlock, 'videopress/video' );
// Limit support to rendering the Paywall block, not inserting it.
dispatch( editPostStore ).hideBlockTypes( [ 'jetpack/paywall' ] );
// Register Jetpack blocks
require( '../jetpack/projects/plugins/jetpack/extensions/editor' );
// Register VideoPress block
require( '../jetpack/projects/packages/videopress/src/client/block-editor/editor' );
}
export function registerJetpackEmbedVariations( { capabilities } ) {
if ( ! isActive() || capabilities.onlyCoreBlocks ) {
return;
}
// Register Jetpack Embed variations
[
{
// Facebook embed
capability: capabilities.facebookEmbed,
registerFunc: reactivateFacebookEmbedBlockVariation,
},
{
// Instagram embed
capability: capabilities.instagramEmbed,
registerFunc: reactivateInstagramEmbedBlockVariation,
},
{
// Loom embed
capability: capabilities.loomEmbed,
registerFunc: registerLoomVariation,
},
{
// Smartframe embed
capability: capabilities.smartframeEmbed,
registerFunc: registerSmartframeVariation,
},
].forEach( ( { capability, registerFunc } ) => {
if ( capability === true ) {
registerFunc();
}
} );
}
export function enableVideoPressV5Support( { capabilities } ) {
if ( ! isActive() || ! capabilities.videoPressV5Support ) {
return;
}
require( '../jetpack/projects/plugins/jetpack/extensions/blocks/videopress/editor' );
}
const setupHooks = () => {
// Hook triggered before the editor is rendered
addAction( 'native.pre-render', 'gutenberg-mobile-jetpack', ( props ) => {
const { jetpackState } = props;
setupJetpackEditor(
jetpackState || { blogId: 1, isJetpackActive: true }
);
// Jetpack Embed variations use WP hooks that are attached to
// block type registration, so it’s required to add them before
// the core blocks are registered.
registerJetpackEmbedVariations( props );
// VideoPress v5 conversion also uses WP hooks that are attached to
// block type registration, so it’s required to add them before
// the core blocks are registered.
enableVideoPressV5Support( props );
} );
// Hook triggered after the editor is rendered
addAction(
'native.post-register-core-blocks',
'gutenberg-mobile-jetpack',
( props ) => {
registerJetpackBlocks( props );
}
);
};
const setupStringsOverrides = () => {
addFilter(
'native.missing_block_detail',
'native/missing_block',
( defaultValue, blockName ) => {
const { capabilities } = select( blockEditorStore ).getSettings();
const onlyCoreBlocks = capabilities?.onlyCoreBlocks === true;
const namespacedBlockNames = Object.keys(
supportedJetpackBlocks
).map( ( name ) =>
/^(\w|-)+$/.test( name ) ? `jetpack/${ name }` : name
);
if (
onlyCoreBlocks &&
namespacedBlockNames.includes( blockName )
) {
return null;
}
return defaultValue;
}
);
};
export default () => {
setupHooks();
setupStringsOverrides();
};