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

Try: Support block edit defined as tag name for web components interoperability #2791

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions blocks/api/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ export function registerBlockType( name, settings ) {
);
return;
}
if ( 'edit' in settings && ! isFunction( settings.edit ) ) {
if ( 'edit' in settings && ! isFunction( settings.edit ) && 'string' !== typeof settings.edit ) {
console.error(
'The "edit" property must be a valid function.'
'The "edit" property must be a valid component or tag name.'
);
return;
}
Expand Down
6 changes: 3 additions & 3 deletions blocks/api/test/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ describe( 'blocks', () => {
expect( block ).toBeUndefined();
} );

it( 'should reject blocks with an invalid edit function', () => {
const blockType = { save: noop, edit: 'not-a-function', category: 'common', title: 'block title' },
it( 'should reject blocks with an invalid edit value, must be web component tag name or function', () => {
const blockType = { save: noop, edit: { not: 'valid' }, category: 'common', title: 'block title' },
block = registerBlockType( 'my-plugin/fancy-block-6', blockType );
expect( console.error ).toHaveBeenCalledWith( 'The "edit" property must be a valid function.' );
expect( console.error ).toHaveBeenCalledWith( 'The "edit" property must be a valid component or tag name.' );
expect( block ).toBeUndefined();
} );

Expand Down
79 changes: 79 additions & 0 deletions editor/component-interop/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* External dependencies
*/
import { isString, includes, forOwn } from 'lodash';

/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';

function isWebComponent( tagName ) {
return isString( tagName ) && includes( tagName, '-' );
}

class ComponentInterop extends Component {
constructor() {
super( ...arguments );

this.bindNode = this.bindNode.bind( this );
}

componentDidMount() {
this.setWebComponentProps( this.props );
}

componentWillReceiveProps( nextProps ) {
this.setWebComponentProps( nextProps );
}

setWebComponentProps( props ) {
const { tagName, attributes, ...properties } = props;
if ( ! isWebComponent( tagName ) ) {
return;
}

let child = this.node.firstChild;
if ( ! child ) {
child = document.createElement( tagName );
this.node.appendChild( child );
}

forOwn( properties, ( value, key ) => {
child[ key ] = value;
} );

forOwn( attributes, ( value, key ) => {
child.setAttribute( key, value );
} );
}

shouldComponentUpdate() {
const { tagName } = this.props;
if ( isWebComponent( tagName ) ) {
return false;
}

if ( 'function' === typeof tagName &&
'function' === typeof tagName.prototype.shouldComponentUpdate ) {
return tagName.prototype.shouldComponentUpdate.apply( this, arguments );
}

return true;
}

bindNode( node ) {
this.node = node;
}

render() {
const { tagName: TagName, ...componentProps } = this.props;
if ( isWebComponent( TagName ) ) {
return <div ref={ this.bindNode } />;
}

return <TagName { ...componentProps } />;
}
}

export default ComponentInterop;
4 changes: 3 additions & 1 deletion editor/modes/visual-editor/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import BlockDropZone from './block-drop-zone';
import BlockMover from '../../block-mover';
import BlockRightMenu from '../../block-settings-menu';
import BlockSwitcher from '../../block-switcher';
import ComponentInterop from '../../component-interop';
import {
updateBlockAttributes,
focusBlock,
Expand Down Expand Up @@ -381,7 +382,8 @@ class VisualEditorBlock extends Component {
<BlockCrashBoundary onError={ this.onBlockError }>
{ isValid
? (
<BlockEdit
<ComponentInterop
tagName={ BlockEdit }
focus={ focus }
attributes={ block.attributes }
setAttributes={ this.setAttributes }
Expand Down