Giter VIP home page Giter VIP logo

gutenberg-fields-middleware's Introduction

Gutenberg Fields Middleware

Project Status: Inactive – The project has reached a stable, usable state but is no longer being actively developed; support/maintenance will be provided as time allows.


IMPORTANT

The work on Gutenberg Fields Middleware had started during the initial development phase of Gutenberg before it was merged into core. Gutenberg has evolved a lot since then and with the introduction of InnerBlocks and block templates, Gutenberg Fields Middleware has become less useful because the core blocks can be used as fields for creating blocks using block templates. Therefore we have stopped the development of this project and would archive this repo.


Register fields for Gutenberg blocks with a simple, declarative API.

This project is in its early stages. Please open an issue with questions, feedback, suggestions, and bug reports.

Using | Available Fields

Using

Gutenberg fields middleware requires only two files dist/middleware.min.js and dist/middleware.min.css as dependency.

There are two ways of using fields middleware.

  1. As a Plugin: Install the Gutenberg Fields Middleware as a standalone WordPress plugin which will register a gutenberg-fields-middleware handle you can add as a dependency for your block script.
  2. Using JS and CSS files: Or you can use middleware.min.js and middleware.min.css and enqueue them as dependency for your block script. Be sure to use array( 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-date', 'wp-hooks' ) handles as your dependency when enqueing middleware js file.

Fields are now registered as attribute configuration details. Here's how you might register image, text, color and range fields:

wp.blocks.registerBlockType( 'example-namespace/example-block', {
	title: 'Example Block',
	category: 'common',
	attributes: {
		image: {
			type: 'object',
			field: {
				type: 'image',
			},
		},
		text: {
			type: 'string',
			field: {
				type: 'text',
				placeholder: 'Enter text..',
			},
		},
		color: {
			type: 'string',
			field: {
				type: 'color',
				placement: 'inspector',
			},
		},
		range: {
			type: 'string',
			field: {
				type: 'range',
				label: 'Columns',
				placement: 'inspector', // To show in sidebar.
			},
			default: 20,
		},
	},

	edit: function( props, middleware ) {
		return [
			middleware.inspectorControls, // Contains ALL inspector controls.
			middleware.fields.image,
			middleware.fields.text,
		];
	},

	save: function( props ) {}
});

Which will create a block like this

image

✔️ Fields can also be used in the same way when using register_block_type in PHP.

register_block_type( 'example-namespace/example-block', array(
	'attributes' => array(
		'image' => array(
			'type' => 'object',
			'field' => array(
				'type' => 'image',
				'buttonText' => 'Upload',
				'imagePlaceholder' => true,
				'removeButtonText' => 'Remove',
			),
		),
		'color' => array(
			'type' => 'string',
			'field' => array(
				'type' => 'color'
			)
		)
	),
	'render_callback' => 'example_callback',
) );

✔️ Gutenberg fields middleware is also available as npm package.

npm i gutenberg-fields-middleware

Available Fields

Gutenberg Fields Middleware supports the following field types and type configuration.

Returning field in edit method:

  • middleware.fields.arrtibuteKeyName for a single field when placement property is not defined.
  • middleware.blockControls for all block-control fields. ( where placement is block-control )
  • middleware.inspectorControls for all inspector fields. ( where placement is inspector )

Getting more control over fields:

There are two ways of getting a field, one is simply use middleware.fields.arrtibuteKeyName or middleware.getField( props, 'arrtibuteKeyName', config ) when you need more control over a field, here you can use all configuration options in config parameter given in the fields doc.

The same can be done for block controls and inspector controls as middleware.getBlockControls( props, fields ) and middleware.getInspectorControls( props, fields ) where fields can be defined as an array of fields.

See example usage of alignment-toolbar.

Example Usage:

  • See examples
    • You can set GUTENBERG_FIELDS_MIDDLEWARE_IS_DEV to true during development in wp-config.php which will also enable example blocks.
  • Check gutenberg-supplements plugin where we have created some actual blocks using middleware.

Does this interest you?

Join us at rtCamp, we specialize in providing high performance enterprise WordPress solutions

gutenberg-fields-middleware's People

Contributors

ajitbohra avatar danielbachhuber avatar dependabot-preview[bot] avatar dharmin avatar maitreyie-chavan avatar manishsongirkar avatar mohdsayed avatar pradeep910 avatar rahul286 avatar sanketio avatar thrijith avatar vaishaliagola27 avatar yahilmadakiya avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

gutenberg-fields-middleware's Issues

Feature request: post selection

Hi

Nice stuff, was looking for a package like this.
I'd like to propose a field type which is extremely useful from my experience - post selection

Thanks

Add basic test coverage

As a Gutenberg Fields Middleware user, it would give me more confidence in using this library if there was some basic test coverage around the generated fields.

Is this project still active?

Looks interesting. Certainly what I was expecting from Gutenberg in the first place. But...Is this project still active?

Image field doesn't have a way to remove/replace when in the inspector

When using the image field in the inspector, once an image has been added, there's no option to remove it.

'attributes'      => [
		'backgroundImage' => [
			'type'  => 'object',
			'field' => [
				'type'             => 'image',
				'label'            => __( 'Background Image', 'oshpd' ),
				'placement'        => 'inspector',
				'removeButtonText' => 'Remove',
			],
		],
],

screen shot 2018-04-27 at 2 11 25 pm

Server Side Registration not working

Hey, I've got things working well for Client Side registration, but I'm not able to see blocks registered server side.

Using your example:

register_block_type( 'example-namespace/example-block', array(
        'title' => 'Example Block',
	'attributes' => array(
		'image' => array(
			'type' => 'object',
			'field' => array(
				'type' => 'image',
				'buttonText' => 'Upload',
				'imagePlaceholder' => true,
				'removeButtonText' => 'Remove',
			),
		),
		'color' => array(
			'type' => 'string',
			'field' => array(
				'type' => 'color'
			)
		)
	),
	'render_callback' => 'example_callback',
) );

I don't have an example block in my list of blocks.

I know Gutenberg support for Server Side registry is pretty weak. . .not sure if this is still a limitation of Gutenberg, an issue with this plugin, or just something I'm doing wrong?

What version of Gutenberg does this work with?

I've tried this with Gutenberg versions 2.2-2.7 and can't get it to work with any of them.

Steps to Reproduce

  • activate Gutenberg v2.2 - 2.7
  • activate gutenberg-fields-middleware
  • create new post
  • try to add "Simple Example Block" to the post
  • check your console

This is what I'm seeing, for the most part. . .varies a bit based on the version of Gutenberg:

screen shot 2018-04-23 at 11 55 18 am
screen shot 2018-04-23 at 11 55 23 am

Match all fields available in the WordPress Customizer

As a Gutenberg Fields Middleware user, I'd like to be able to many generic fields. It'd be great of the middleware gave me the following:

  • Media upload / select from media library
  • <select> field.
  • <textarea> field.
  • TinyMCE editor field.
  • Checkboxes / radio buttons
  • All HTML5 <input> fields.

Kirki is good for inspiration on which controls could be included:

image

Error in wordpress 5.0.0 beta

Hi
I download github project and set active development mode with set IS_GUTENBERG_FIELDS_MIDDLEWARE_DEV to true to active example.
But I have just "Simple Example Block" block and have not advanced settings fields in advanced sidebar:
image

I have error in console:
image

inspector attributes not passed to php render_callback

For some reason $attributes in the PHP render_callback function doesn't have the inspector attributes from the JS side.

I also logged prepare_attributes_for_render() and the inspector attributes are not there.

From below, 'image' is in $attributes, 'amount' is not.

Am I missing something? Is it a bug?

Code excerpt:

attributes: {
	image: {
		type: 'object',
		field: {
			type: 'image',
		},
	},
	amount: {
		type: 'number',
		field: {
			type: 'range',
			label: 'Amount',
			placement: 'inspector', // To show in sidebar.
			min: 1,
			max: 10
		},
		default: 2,
	},
},
edit: function( props, middleware ) {
	return [
		middleware.blockControls,
		middleware.inspectorControls,
		middleware.fields.image,
	];

},
save: function( props ) {
	// Rendering in PHP
	return null;
},

Why not use the hooks provided by core?

I had a brief chat with @danielbachhuber and he suggested I open an issue.

It's more a feedback than a call for action. Core provides a few hooks which allow to modify the block which is about to be registered. See: https://wordpress.org/gutenberg/handbook/extensibility/#modifying-blocks-experimental. In particular blocks.registerBlockType might be another way to go. It probably would have to be set with the top priority to make sure it is executed first.

This is an interesting idea, the more plugins like that, the more ideas for the core part to incorporate it by default. Once it is ready, we should see if it is something that would bring expected benefits to a wider audience. In fact, we use a similar approach in core in a few cases. Examples: https://github.com/WordPress/gutenberg/blob/master/blocks/hooks/align.js, which adds align support for the block with supports: align property.

Radio - Not able to set default value

Currently there is no option to set default value for radio field, if its possible we can add support in Middleware to add default selected value.

For eg.

radio: {
	type: 'string',
	field: {
		type: 'radio',
		label: __( 'Type' ),
		placement: 'inspector',
		default: 'major',
		options: [
			{
				value: 'major',
				label: __( 'Major' ),
			},
			{
				value: 'minor',
				label: __( 'Minor' ),
			},
		],
	},
},

Ensure generated fields meet accessibility standards

A big push for Gutenberg right now is to ensure the entire user experience is WCAG-compliant.

We should make sure our generated fields meet accessibility standards.

When we take this issue on, we should also develop some automated way for enforcing field compliance and share those learnings upstream on WordPress/gutenberg#6098

Licensing?

Howdy!

What license is this code released under? I see a reference to GPLv2 in the package.json file, but none of the usual text is present.

Thanks!

Issue with button-editable

  1. Not able to set dynamic background color and text color using editable button. I also checked with this example . This only works with button but not button-editable.
  2. If user navigate using tab, then not able to add text or link.

Input type support

While creating custom block from Gutenberg we can use valid input types using TextControl.

In Middleware we have support for url, text, range, date-time etc. But missing some other valid input types. For eg. date, time, week, month etc.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.