-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Render generic fallback block for unknown block type #335
Changes from all commits
716811f
67adf4d
ba9635b
2b61305
a4e2994
9bf268c
8ff84e9
eaa5dfc
15db036
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const { html } = wp.blocks.query; | ||
|
||
wp.blocks.registerBlock( 'core/freeform', { | ||
title: 'Freeform', | ||
|
||
icon: 'text', | ||
|
||
category: 'common', | ||
|
||
attributes: { | ||
html: html() | ||
}, | ||
|
||
edit( { attributes } ) { | ||
return <div contentEditable>{ attributes.html }</div>; | ||
}, | ||
|
||
save( { attributes } ) { | ||
return attributes.html; | ||
} | ||
} ); | ||
|
||
wp.blocks.setUnknownTypeHandler( 'core/freeform' ); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
import './freeform'; | ||
import './text'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,13 +19,25 @@ function Blocks( { blocks, onChange } ) { | |
|
||
return ( | ||
<div className="editor-mode-visual"> | ||
<div> | ||
{ blocks.map( ( block, index ) => | ||
<div key={ index }> | ||
{ wp.blocks.getBlockSettings( block.blockType ).edit( block.attributes, onChangeBlock( index ) ) } | ||
</div> | ||
) } | ||
</div> | ||
{ blocks.map( ( block, index ) => { | ||
const settings = wp.blocks.getBlockSettings( block.blockType ); | ||
|
||
let BlockEdit; | ||
if ( settings ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can assume this is always defined, right! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I guess this depends if the editor implementation should trust itself to know that elsewhere it has assigned the unknown type handler. 😄 I might be erring too far on the side of safety here. Would certainly be more convenient to be able to assume that |
||
BlockEdit = settings.edit || settings.save; | ||
} | ||
|
||
if ( ! BlockEdit ) { | ||
return; | ||
} | ||
|
||
return ( | ||
<BlockEdit | ||
key={ index } | ||
attributes={ block.attributes } | ||
onChange={ onChangeBlock( index ) } /> | ||
); | ||
} ) } | ||
<InserterButton /> | ||
</div> | ||
); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we fallback to a different blockType as suggested above, all the changes here are not needed anymore.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I kept some changes mostly for consideration of:
save
implementation? Should we support this? With updated logic it will simply show the front-end UI within the editor.edit
norsave
implementations, which will throw if left uncheckedThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, could be good for separators and stuff like that.
I think we should add some validation to the
registerBlock
(category, save, title, icon are all mandatory to me) (but granted this is a separate task)