-
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
Proposal for Block API #48
Conversation
Merge in changes.
Fixes #27. Here is my intial proposal for a blocks API for Gutenberg. If es6 were available for use this could be cleaned up big time. I still need to add some polyfills for compatability, but overall, I think this came out decently well. I would love some feedback!
In areas where I thought this was flexible, it was instead limiting. Closing it out. Going to take a different approach. |
Can you expand on this? Curious if you have any specifics on what you thought to be limiting. |
This API mostly served as a sort of One hangup is that // Define the properties of a text block. T
const textBlockType = blockType( properties )
// You could compose a quote out of potentially shared properties form the textType or not, it would be up to you.
const quoteBlockType = blockType( textBlockType, blockType( properties ) )
// Create a poetry type.
const poetryBlockType = blockType( textBlockType, blockType( otherTextProperties ), blockType( poetryProperties ) )
// List Type. Calling it on itself won't matter.
const listType = blockType( blockType( listProperties ) )
// Ordered List Type. blockTypes should also support something like this.
let orderedListType = listType( orderedListProperties )
// So they can be overridden to create a different type, depending on your needs, with other properties.
unorderedListType = listType( orderedListProperties )( oopsUseTheseProperties, iWantedUnorderedProperties )
// More concrete example:
const text = blockType( {
name: 'Text',
type: 'wp-text',
controls: [
leftAlignControlType,
centerAlignControl,
rightAlignControl,
],
// Create a textBlock that will by default match this type.
create: ( blockType = this ) => textBlock( blockType )
} )
// Example text block.
const textBlock = blockType => {
return block ( {
/**
* State and props would be objects that would be passed in via data held somewhere. i.e. the UI
*
* The render method is defined by you, so you can make it work the way you want it to.
*/
render: ( state = {}, props = {} ) => {
// Do some stuff relating to the blockType for this block.
// Render something. Could be DOM, vDOM, React, TinyMCE, JSON, XML, HTML, whatever you want.
// Here is DOM:
if ( typeof document !== 'undefined' ) {
pElement = document.createElement( 'p' )
textNode = document.createTextNode( props.text )
pElement.appendChild( textNode )
return pElement
}
// Here is vDOM:
if ( typeof h !== 'undefined' ) {
return h( 'p', '.wp-text', [
props.text
] )
}
}
} )
} Another hangup is that rather than // blockProperties would include the render() method.
const textBlock = block( blockProperties )
// textTypeProperties could contain a create method returning textBlock().
const textType = blockType( textTypeProperties )
const aTextBlock = textType.create()
const anotherTextBlock = textType.create( textType( withDifferentPropertiesIfYouWant ) )
// containerTypeProperties's create() method would create the actual block.
const containerType = blockType( containerTypeProperties )
// containerProperties's render() method could provide a way to render both aTextBlock and anotherTextBlock inside of it.
const blockContainer = block( containerProperties )
let container = containerType(
{
blocks: [ aTextBlock, anotherTextBlock ],
create: ( blockType = this ) => {
return blockContainer( { blockType } )
}
}
).create()
// If all of the block render methods are React Components, creating a react version of the editor is simple as this.
import 'render' from 'react-dom'
render(
container.render(),
document.getElementById( 'editor' )
) Not sure whether this would be a good approach, but it at least seems like a cool idea to me. |
…interface Improve callback for contentSize.
Fixes #27. Here is my intial proposal for a blocks API for Gutenberg. If es6 were available for use this could be cleaned up big time. I still need to add some polyfills for compatability, but overall, I think this came out decently well. I would love some feedback!
How to use:
gutenberg()
is currently used as a namespace for three different things:gutenberg()
.Using factories:
In the above we are creating an instance of
gutenberg()
ascreate
. We use theblock()
andcontrol
factories to create a list block with one control. All you need to do now is associate a dom element with the block and the Oila, a block element is already created.Using the interface for registering blocks:
You can chain a set of registered blocks using
registerBlock()
,registerBlocks()
,unregisterBlock()
, andunregisterBlocks()
The return of the methods will act like an array but it is not an array. It is also import to understand that the blocks are encapsulated and the reference you store is meant to be generated in one go and not changed later on during run time.
Using the editor interface:
There is a lot more that the API can currently handle and I have replaced block level controls with the API in its current standing.
Let me know what you think.