Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Embed block] Include Jetpack embed variants #4008

Merged
merged 29 commits into from
Oct 6, 2021
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
1010343
Refactor jetpack editor setup
fluiddot Sep 22, 2021
659c578
Add register jetpack embed variations
fluiddot Sep 22, 2021
ad6e101
Update Gutenberg ref
fluiddot Sep 22, 2021
91b2360
Update Gutenberg ref
fluiddot Sep 22, 2021
68791c9
Use capabilities from root props in registerJetpackBlocks
fluiddot Sep 23, 2021
7a4cd98
Update Jetpack blocks unit test
fluiddot Sep 23, 2021
3e90cd2
Remove unused import
fluiddot Sep 23, 2021
1c70106
Update Gutenberg ref
fluiddot Sep 23, 2021
3a27228
Update Jetpack ref
fluiddot Sep 23, 2021
1fcb5d8
Update Jetpack ref
fluiddot Sep 27, 2021
07019f2
Merge branch 'develop' into embed-block-jetpack-variants
fluiddot Sep 27, 2021
ac8409f
Update Jetpack ref
fluiddot Sep 27, 2021
f6f0300
Register Jetpack embed variations by capabilities
fluiddot Sep 27, 2021
5251571
Update Gutenberg ref
fluiddot Sep 27, 2021
b0e85b8
Update Gutenberg ref
fluiddot Sep 28, 2021
ea24895
Update bundle
fluiddot Sep 28, 2021
5d68177
Update Jetpack ref
fluiddot Sep 28, 2021
428728e
Add embed variations to jetpack setup unit test
fluiddot Sep 28, 2021
1b69ad9
Update jetpack setup unit test
fluiddot Sep 28, 2021
6455b21
Update Jetpack ref
fluiddot Sep 28, 2021
2e7fc08
Update Jetpack ref
fluiddot Sep 29, 2021
7cad013
Update Gutenberg ref
fluiddot Sep 29, 2021
274492b
Update Jetpack ref
fluiddot Oct 4, 2021
ec72204
Update Gutenberg ref
fluiddot Oct 4, 2021
18ee9a3
Merge branch 'develop' into embed-block-jetpack-variants
fluiddot Oct 4, 2021
1511ae1
Update Jetpack ref with merge commit
fluiddot Oct 6, 2021
3c23b9b
Update Gutenberg ref with merge commit
fluiddot Oct 6, 2021
bb6458f
Update bundle
fluiddot Oct 6, 2021
32c9abd
Merge branch 'develop' into embed-block-jetpack-variants
fluiddot Oct 6, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,965 changes: 1,482 additions & 1,483 deletions bundle/ios/App.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion bundle/ios/App.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion gutenberg
Submodule gutenberg updated 50 files
+2 −2 docs/getting-started/tutorials/devenv/README.md
+0 −2 package-lock.json
+15 −5 packages/api-fetch/src/middlewares/media-upload.js
+35 −0 packages/api-fetch/src/middlewares/test/media-upload.js
+5 −0 packages/block-editor/src/components/block-alignment-control/style.scss
+15 −1 packages/block-editor/src/components/block-alignment-control/test/__snapshots__/index.js.snap
+94 −27 packages/block-editor/src/components/block-alignment-control/ui.js
+16 −10 packages/block-editor/src/components/block-alignment-control/use-available-alignments.js
+7 −4 packages/block-editor/src/components/editor-styles/index.js
+3 −1 packages/block-editor/src/hooks/align.js
+50 −7 packages/block-editor/src/layouts/flow.js
+1 −0 packages/block-editor/src/style.scss
+11 −1 packages/block-library/src/column/save.js
+1 −0 packages/block-library/src/gallery/edit.js
+2 −1 packages/block-library/src/reset.scss
+8 −1 packages/block-library/src/site-title/block.json
+35 −8 packages/block-library/src/site-title/edit/index.js
+12 −2 packages/block-library/src/site-title/index.php
+53 −0 packages/components/CONTRIBUTING.md
+8 −0 packages/components/src/menu-item/README.md
+6 −2 packages/components/src/menu-item/index.js
+5 −3 packages/components/src/menu-item/style.scss
+2 −2 packages/components/src/menu-item/test/__snapshots__/index.js.snap
+12 −2 packages/components/src/mobile/bottom-sheet/range-cell.native.js
+116 −105 packages/e2e-tests/specs/editor/plugins/align-hook.test.js
+2 −2 packages/e2e-tests/specs/editor/various/block-grouping.test.js
+4 −2 packages/e2e-tests/specs/editor/various/writing-flow.test.js
+0 −1 packages/edit-navigation/package.json
+0 −55 packages/edit-navigation/src/store/controls.js
+0 −2 packages/edit-navigation/src/store/index.js
+27 −20 packages/edit-navigation/src/store/resolvers.js
+68 −0 packages/edit-navigation/src/store/test/__snapshots__/resolvers.js.snap
+114 −271 packages/edit-navigation/src/store/test/resolvers.js
+0 −1 packages/edit-widgets/package.json
+109 −145 packages/edit-widgets/src/store/actions.js
+0 −181 packages/edit-widgets/src/store/controls.js
+1 −2 packages/edit-widgets/src/store/index.js
+21 −27 packages/edit-widgets/src/store/resolvers.js
+1 −0 packages/icons/src/index.js
+12 −0 packages/icons/src/library/align-none.js
+1 −1 packages/icons/src/library/position-center.js
+5 −25 packages/interface/src/store/actions.js
+2 −0 packages/interface/src/store/index.js
+3 −3 packages/interface/src/store/reducer.js
+15 −4 packages/media-utils/src/components/media-upload/index.js
+12 −0 ...ative-bridge/android/react-native-bridge/src/main/java/org/wordpress/mobile/WPAndroidGlue/GutenbergProps.kt
+4 −0 packages/react-native-bridge/ios/GutenbergBridgeDelegate.swift
+4 −0 packages/react-native-editor/android/app/src/main/java/com/gutenberg/MainActivity.java
+4 −0 packages/react-native-editor/ios/GutenbergDemo/GutenbergViewController.swift
+3 −1 test/integration/fixtures/blocks/core__site-title.json
2 changes: 1 addition & 1 deletion jetpack
Submodule jetpack updated 537 files
19 changes: 15 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,31 @@ import {
* Internal dependencies
*/
import correctTextFontWeight from './text-font-weight-correct';
import setupJetpackEditor from './jetpack-editor-setup';
import {
registerJetpackBlocks,
registerJetpackEmbedVariations,
setupJetpackEditor,
} from './jetpack-editor-setup';
import setupBlockExperiments from './block-experiments-setup';
import initialHtml from './initial-html';

addAction( 'native.pre-render', 'gutenberg-mobile', () => {
addAction( 'native.pre-render', 'gutenberg-mobile', ( props ) => {
require( './strings-overrides' );
correctTextFontWeight();
} );

addAction( 'native.render', 'gutenberg-mobile', ( props ) => {
setupJetpackEditor(
props.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 );
} );

addAction( 'native.render', 'gutenberg-mobile', ( props ) => {
const capabilities = props.capabilities ?? {};
registerJetpackBlocks( props );
setupBlockExperiments( capabilities );
} );

Expand Down
96 changes: 68 additions & 28 deletions src/jetpack-editor-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@
* Internal dependencies
*/
import { JETPACK_DATA_PATH } from '../jetpack/projects/plugins/jetpack/extensions/shared/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';

/**
* WordPress dependencies
*/
import { dispatch, select } from '@wordpress/data';
import { dispatch } from '@wordpress/data';
import { store as editPostStore } from '@wordpress/edit-post';

// When adding new blocks to this list please also consider updating ./block-support/supported-blocks.json
const supportedJetpackBlocks = {
Expand Down Expand Up @@ -37,39 +46,70 @@ const setJetpackData = ( {
return jetpackEditorInitialState;
};

export default ( jetpackState ) => {
const hideBlockByCapability = ( capability, blockName ) => {
if ( capability !== true ) {
dispatch( editPostStore ).hideBlockTypes( [ blockName ] );
} else {
dispatch( editPostStore ).showBlockTypes( [ blockName ] );
}
};

export function setupJetpackEditor( jetpackState ) {
if ( ! jetpackState.isJetpackActive ) {
return;
}

const jetpackData = setJetpackData( jetpackState );
return setJetpackData( jetpackState );
}

const toggleBlock = ( capability, blockName ) => {
if ( capability !== true ) {
dispatch( 'core/edit-post' ).hideBlockTypes( [ blockName ] );
} else {
dispatch( 'core/edit-post' ).showBlockTypes( [ blockName ] );
}
};

// Note on the use of setTimeout() here:
// We observed the settings may not be ready exactly when the native.render hooks get run but rather
// right after that execution cycle (because state hasn't changed yet). Hence, we're only checking for
// the actual settings to be loaded by using setTimeout without a delay parameter. This ensures the
// settings are loaded onto the store and we can use the core/block-editor selector by the time we do
// the actual check.

// eslint-disable-next-line @wordpress/react-no-unsafe-timeout
setTimeout( () => {
const capabilities = select( 'core/block-editor' ).getSettings(
'capabilities'
);
export function registerJetpackBlocks( { capabilities } ) {
if ( ! isActive() ) {
return;
}

toggleBlock( capabilities.mediaFilesCollectionBlock, 'jetpack/story' );
toggleBlock( capabilities.contactInfoBlock, 'jetpack/contact-info' );
} );
hideBlockByCapability(
capabilities.mediaFilesCollectionBlock,
'jetpack/story'
);
hideBlockByCapability(
capabilities.contactInfoBlock,
'jetpack/contact-info'
);

// Register Jetpack blocks
require( '../jetpack/projects/plugins/jetpack/extensions/editor' );
}

return jetpackData;
};
export function registerJetpackEmbedVariations( { capabilities } ) {
if ( ! isActive() ) {
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();
}
} );
}
207 changes: 185 additions & 22 deletions src/test/index.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,191 @@
describe( 'Test Jetpack blocks', () => {
it( 'should setup the editor for jetpack without failing', () => {
const mockRegisterBlockCollection = jest.fn();
jest.mock( '@wordpress/blocks', () => {
return {
getCategories: () => [ { slug: 'media' } ],
setCategories: jest.fn(),
registerBlockCollection: mockRegisterBlockCollection,
withBlockContentContext: jest.fn(),
};
/**
* WordPress dependencies
*/
import {
getBlockTypes,
getBlockVariations,
unregisterBlockType,
unregisterBlockVariation,
} from '@wordpress/blocks';
import { select } from '@wordpress/data';
import { store as editPostStore } from '@wordpress/edit-post';
import { registerCoreBlocks } from '@wordpress/block-library';
import { removeAllFilters } from '@wordpress/hooks';

/**
* Internal dependencies
*/
import getJetpackData, {
JETPACK_DATA_PATH,
} from '../../jetpack/projects/plugins/jetpack/extensions/shared/get-jetpack-data';
import {
registerJetpackBlocks,
registerJetpackEmbedVariations,
setupJetpackEditor,
} from '../jetpack-editor-setup';

const defaultJetpackData = { blogId: 1, isJetpackActive: true };
const defaultProps = {
capabilities: {
mediaFilesCollectionBlock: true,
contactInfoBlock: true,
facebookEmbed: true,
instagramEmbed: true,
loomEmbed: true,
smartframeEmbed: true,
},
};
const jetpackBlocks = [ 'jetpack/contact-info', 'jetpack/story' ];
const jetpackEmbedVariations = [
'facebook',
'instagram',
'loom',
'smartframe',
];

// Jetpack blocks are registered when importing the editor extension module.
// Since we need to register the blocks multiple times for testing,
// it's required to isolate modules for re-importing the editor extension multiple times.
const registerJetpackBlocksIsolated = ( props ) => {
jest.isolateModules( () => {
registerJetpackBlocks( props );
} );
};
// Similarly to Jetpack blocks, Jetpack embed variations also require to isolate modules.
const registerJetpackEmbedVariationsIsolated = ( props ) => {
jest.isolateModules( () => {
registerJetpackEmbedVariations( props );
} );
};

describe( 'Jetpack blocks', () => {
afterEach( () => {
// Reset Jetpack data
delete global.window[ JETPACK_DATA_PATH ];

// Clean up registered blocks
getBlockTypes().forEach( ( block ) => {
unregisterBlockType( block.name );
} );
jest.mock(
'../../jetpack/projects/plugins/jetpack/extensions/blocks/contact-info/editor.js',
() => jest.fn()
);
jest.mock(
'../../jetpack/projects/plugins/jetpack/extensions/blocks/story/editor.js',
() => jest.fn()
);
} );

it( 'should set up Jetpack data', () => {
const expectedJetpackData = {
available_blocks: {
'contact-info': { available: true },
story: { available: true },
},
jetpack: { is_active: true },
siteFragment: null,
tracksUserData: null,
wpcomBlogId: 1,
};
setupJetpackEditor( defaultJetpackData );

const setupJetpackEditor = require( '../jetpack-editor-setup' ).default;
setupJetpackEditor( { blogId: 1, isJetpackActive: true } );
expect( getJetpackData() ).toEqual( expectedJetpackData );
} );

expect( mockRegisterBlockCollection.mock.calls[ 0 ][ 0 ] ).toBe(
'jetpack'
it( 'should register Jetpack blocks if Jetpack is active', () => {
setupJetpackEditor( defaultJetpackData );
registerJetpackBlocksIsolated( defaultProps );

const registeredBlocks = getBlockTypes().map( ( block ) => block.name );
expect( registeredBlocks ).toEqual(
expect.arrayContaining( jetpackBlocks )
);
} );

it( 'should not register Jetpack blocks if Jetpack is not active', () => {
setupJetpackEditor( { ...defaultJetpackData, isJetpackActive: false } );
registerJetpackBlocksIsolated( defaultProps );

const registeredBlocks = getBlockTypes().map( ( block ) => block.name );
expect( registeredBlocks ).toEqual( [] );
} );

it( 'should hide Jetpack blocks by capabilities', () => {
setupJetpackEditor( defaultJetpackData );
registerJetpackBlocksIsolated( {
capabilities: {
mediaFilesCollectionBlock: true,
contactInfoBlock: false,
},
} );

const { hiddenBlockTypes } = select( editPostStore ).getPreferences();
expect( hiddenBlockTypes ).toEqual( [ 'jetpack/contact-info' ] );
} );

describe( 'Jetpack embed variations', () => {
afterEach( () => {
// Clean up embed variations
getBlockVariations( 'core/embed' ).forEach( ( variation ) => {
unregisterBlockVariation( 'core/embed', variation.name );
} );

// Embed variations can use the register block type WP hook to reactivate
// already registered variations, so we need to remove all hooks after the tests.
removeAllFilters( 'blocks.registerBlockType' );
} );

it( 'should not register Jetpack embed variations if Jetpack is not active', () => {
setupJetpackEditor( {
...defaultJetpackData,
isJetpackActive: false,
} );
registerJetpackEmbedVariationsIsolated( defaultProps );
// Embed variations require the Embed block to be registered,
// so we need to register the core blocks to include it.
registerCoreBlocks();

const embedVariations = getBlockVariations(
'core/embed',
'inserter'
).map( ( block ) => block.name );

jetpackEmbedVariations.forEach( ( variation ) =>
expect( embedVariations ).not.toContain( variation )
);
} );

it( 'should not register Jetpack embed variations if capabilities are falsey', () => {
setupJetpackEditor( defaultJetpackData );
registerJetpackEmbedVariationsIsolated( {
capabilities: {
facebookEmbed: false,
instagramEmbed: null,
loomEmbed: undefined,
},
} );
// Embed variations require the Embed block to be registered,
// so we need to register the core blocks to include it.
registerCoreBlocks();

const embedVariations = getBlockVariations(
'core/embed',
'inserter'
).map( ( block ) => block.name );

jetpackEmbedVariations.forEach( ( variation ) =>
expect( embedVariations ).not.toContain( variation )
);
} );

it( 'should register Jetpack embed variations if capabilities are true', () => {
setupJetpackEditor( defaultJetpackData );
registerJetpackEmbedVariationsIsolated( defaultProps );
// Embed variations require the Embed block to be registered,
// so we need to register the core blocks to include it.
registerCoreBlocks();

const embedVariations = getBlockVariations(
'core/embed',
'inserter'
).map( ( block ) => block.name );

expect( embedVariations ).toEqual(
expect.arrayContaining( jetpackEmbedVariations )
);
} );
} );
} );