Coder Social home page Coder Social logo

zackify / gutenblock Goto Github PK

View Code? Open in Web Editor NEW
238.0 18.0 15.0 203 KB

The easiest way to develop and release Gutenberg blocks (components) for WordPress

Home Page: https://www.youtube.com/watch?v=apZwd0FqQu4

License: MIT License

JavaScript 96.03% PHP 3.97%
gutenberg gutenberg-blocks wordpress react reactjs

gutenblock's Introduction

Contents

Install

npm install gutenblock -g

This is a Gutenberg plugin creator + reusable inspector components with hot loading and code splits built in.

Quickstart

If you have never done WordPress development, getting started couldn't be easier.

When you add docker on the end of the watch command, it will bring up WordPress for you. Simply create an account, install the Gutenberg plugin, and activate the blocks plugin. You're all set.

Comparison with other tooling

Currently, there is only one tool out there to help create blocks (that I have found so far). It's called Create Guten Block. This library was inspired by it. I've added what I consider to be good defaults that everyone would want when creating blocks. These features are not included in other libraries by default:

  • Auto Block registration
  • Helper utlities
  • Automatic code splitting
  • Hot reloading (without page reload)
  • Custom webpack config without ejection

Auto Block registration

No need to call registerBlockType for WordPress. Our loader does this for you.

Helper utilities

Currently, when editing things in gutenberg you make components like this:

const { RichText } = wp.editor;

export default ({ setAttributes, attributes }) => (
  <div>
    <RichText
      tagName="h1"
      value={attributes.title}
      placeholder="Title"
      onChange={title => setAttributes({ title })}
    />
  </div>
);

With Gutenblock, we created a higher order context that is loaded into all edit components. This means you can import our abstracted inputs:

import { RichText } from 'gutenblock-controls';

const Edit = () => (
  <div>
    <RichText name="description" />
  </div>
);

We've included a Select MediaSelect Input Inspector Repeat and other form fields to help you build blocks faster. A repeat component will handle the hard work of letting users add infinite items to an array of form fields, replacing items, and deleting them.

The name field is the key in your gutenberg attributes defined in block.js. You can create your own inputs that connect and get access to setAttributes and attributes, no longer needing to pass them all over in your components. See the example

Code splitting

If you have many blocks, you don't want Gutenberg to load all of that JS when it initializes. With this plugin, your edit blocks will only be loaded in once they are dragged out to the canvas.

Hot reloading

Every edit block is hooked into react-hot-loader with our loader so that updates won't need a full page load. Full reloads can make development much slower when Gutenberg has a lot of content on the page at once.

Custom Webpack

Add a gutenblock.config.js file in your blocks folder. It looks like this:

const path = require('path');

module.exports = webpack => ({
  //customize gutenblock options if needed
  gutenblock: {
    devHost: 'localhost',
    devPort: 8080,
    //build asset output path relative to the plugin directory
    outputPath: '/test',
    //when building the plugin, gutenblock will default to the folder name inside wp-content, if you have a different wp-content folder you can change it here
    publicPath: `/app/plugins/blocks/test/`,
  },
  resolve: {
    alias: {
      shared: path.resolve(__dirname, '../src/shared'),
    },
  },
  plugins: [new webpack.EnvironmentPlugin(['NODE_ENV'])],
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [require.resolve('style-loader'), require.resolve('css-loader')],
      },
    ],
  },
});

The configuration is the exact same as webpack with one extra piece: pass babelOptions with plugins and presets like a babelrc has to customize the babel loader.

If you choose to extend the configuration, down the road a future webpack release may require you to make changes and update your configuration. If you do not extend anything, you'll never have to update any configuration in order to upgrade gutenblock!

Future plans

  • Automatic i18n
  • Complicated examples (tabs component, loading in data from WordPress)
  • Test coverage
  • Batch updates when updating nested tabs that cause lots of rerenders in Gutenberg

Usage

gutenblock init will scaffold out a WordPress plugin for you.

gutenblock watch inside the folder will start development mode. Copy the blocks folder into your plugins directory, or use docker

gutenblock build production build the plugin. After, edit line 35 of yourplugin/src/init.php to point to the production assets. All set to publish!

Creating a block

Inside src you will create blocks matching the example one.

All blocks need a block.js and edit.js.

./src/paragraph/block.js

//Optionally use a save block for static rendering on the WordPress frontend

import Save from './save';

const Block = {
  title: 'Paragraph',
  icon: 'shield-alt',
  category: 'common',
  attributes: {
    body: {
      type: 'string',
    },
  },
  save: Save,
};

./src/paragraph/edit.js

import { RichText } from 'gutenblock-controls';

const Edit = () => (
  <RichText tagName="p" name="body" style={{ color: 'black' }} />
);

./src/paragraph/save.js

export default ({ attributes }) => <p>{attributes.body}</p>;

Side note: We don't use save blocks at Crossfield. This is because we fetch WordPress pages and posts via the api and render the blocks using a custom react frontend. Sadly, if you use save blocks, they will not be code split. This is a limitation of the gutenberg editor not supporting awaiting to render the save method.

No registering blocks, importing them into a root folder. It's all done for you.

Now we can run gutenblock watch inside our plugin folder. Inside WordPress the components will hot reload as you edit, thanks to react-hot-loader

You can read more about the Block API

gutenblock's People

Contributors

robertdevore avatar timarney avatar zackify 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

gutenblock's Issues

Use with npx?

First, thanks for working on this. Looks like a great project that will be super useful!

Lately I've been using npx to run scripts instead of installing globally. I tried with this (npx gutenblock init), and it didn't work. I did install globally and run and it's working.

Not sure what it takes to work with npx but would be cool if it worked.

Uncaught Error: only one instance of @babel/polyfill is allowed

Hey, I've not been able to get this up and running. With both the gutenblock watch and gutenblock build I get:

index.js:8 Uncaught Error: only one instance of @babel/polyfill is allowed
    at Object.eval (index.js:8)
    at eval (index.js:12)
    at Object.../node_modules/@babel/polyfill/lib/index.js (main.js?ver=4.9.5:1253)
    at __webpack_require__ (main.js?ver=4.9.5:753)
    at fn (main.js?ver=4.9.5:122)
    at eval (client:2)
    at Object.1 (main.js?ver=4.9.5:5093)
    at __webpack_require__ (main.js?ver=4.9.5:753)
    at main.js?ver=4.9.5:873
    at main.js?ver=4.9.5:876

Thoughts?

How to translate strings with wp.i18n

I've read in the readme that you are working on an automated translation system with i18n but what about current situation? How can i manage I18n translation in gutenblock? Even a manual and slow way would be good to go

MySql error on starting Docker

When running gutenblock watch docker I get the following error and cant seem to find a solution.

wordpress_1 | Warning: mysqli::__construct(): The server requested authentication method unknown to the client [caching_sha2_password] in Standard input code on line 22 wordpress_1 | wordpress_1 | Warning: mysqli::__construct(): (HY000/2054): The server requested authentication method unknown to the client in Standard input code on line 22

I'm on osx High sierra. Any idea?

Child/attribute based repeating inputs

This would be a big deal if possible:

 const CustomBlock = ({ title }) => <div>{title}</div>

edit.js

        <Repeat>
          <CustomBlock
             title={<Input name="title" />}
          />
        </Repeat>

save.js

   attributes.thing.map(thing => <CustomBlock title={thing.title} />)

It would mean that you could have repeating custom components and edit them in place. The base component wouldn't need to have any knowledge of Wordpress.

This is MAJOR missing piece from Gutenberg and it should probably be in core, IMO.

Am I right in thinking this isn't possible with the current patterns you're using? None of the child iteration / cloning you're doing will work, unfortunately, unless the parent CustomBlock was modified.

Missing loader?

This is by far the best option or building blocks I have come across. I like the features but can't get it to work.

During watch I get the error

WARNING in ./src/notes/block.js 34:19
Module parse failed: Unexpected token (34:19)
You may need an appropriate loader to handle this file type.
| Block.edit = function (props) {
|     return wp.element.createElement(_gutenblockControls.Import, _extends({}, props, { load: function load() {
>             return import('./edit');
|         } }));
| };
 @ ./src sync block.js$ ./notes/block.js
 @ ./index.js
 @ multi ././index.js

I get the same when I build and I have not edited any files yet its a stock install.

I have run npm install so all dependencies are there.

Any ideas?

Repeat Field doesn't seem to work inside editor

Repeat works great in the example file using the inspector panel but doesn't seem to work in the editor.

Am I missing something?

Block.js

const Block = {
    title: 'Repeat1',
    icon: 'shield-alt',
    category: 'common',
    attributes: {
        notes: {
            type: 'array',
            default: [{ title: "one", sub: "two" }],
        },
    }
};

Edit.js

import { Input, Repeat } from 'gutenblock-controls';

const Edit = ({ attributes }) => {
  return (<Repeat title="Notes" max="50" addNew="Add Field" attribute="notes">
    {attributes.notes.map(tab => {
      return <Input name="title" />
    })}
  </Repeat>)

};

Screen Capture

screenflow

Add ability to change ports

When running the docker watch, I am getting this error ERROR: for wordpress Cannot start service wordpress: driver failed programming external connectivity on endpoint blocks_wordpress_1 (92276f168396b71072f54fc7b359a72669273720199ba52e27ea71be1ac3cd1c): Error starting userland proxy: Bind for 0.0.0.0:80: unexpected error (Failure EADDRINUSE)

That would indicate that port 80 is in use, which is common. It would be great to have an environment variable for port or to automatically find an open one.

Fix project prefix

Left a hard coded project prefix when registering inside gutenberg. :P

Updating plugin in docker WP erases folder and fails

Steps:

  1. Run docker
  2. Go into WP plugins – "an update is available" – update this plugin

The content of the blocks folder is deleted, and even when creating a new folder, WP is ignoring the plugin.

This is shown in console:

ERROR in ./index.js
Module build failed (from ./loader.js):
Error: ENOENT: no such file or directory, open '/private/var/www/temp/blocks/index.js'
 @ multi ././index.js main[0]
ℹ 「wdm」: Failed to compile.

WP Admin - Plugin update screen

Just a heads up -


Admin says update available -

screen shot 2018-05-05 at 7 35 33 pm

Plugin details shows "Blocks" plugin


screen shot 2018-05-05 at 7 35 52 pm


Might want to change the name of the blocks dir.

 ncp(__dirname + '/../../plugin', `${process.cwd()}/blocks`, err => {

InspectorControls

Looks like InspectorControls is now under wp.editor . Not sure when that changed.

wp.editor.InspectorControls

Breaks:

const { InspectorControls } = wp.blocks;

How to add css or scss?

Hi Zack,

Looking at the examples in this repo I couldn't find any blocks with styles.

I would like to enqueue some structure styles to my block both on the editor and front-end, how is that be done?

Thanks in advance.

Gutenburg 4.x support

Just getting back to updating some components I built on top of gutenblock controls. Looks like things break at the 4.x release of Gutenburg. I didn't dig very deep but looks like an issue with importing the Inspector Controls + the Richtext field. Have you run into this?

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.