Skip to content

Commit

Permalink
Add editor color palette support (#4)
Browse files Browse the repository at this point in the history
* feat(poet): Add editor color palette support
* chore(readme): Add editor color palette examples to README
* chore(readme): Small nagging edits
* chore(composer): update description
  • Loading branch information
Log1x authored Apr 30, 2020
1 parent bfed556 commit f6f7d8d
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## v1.0.9 (04/30/20)

- feat(poet): Add editor color palette support
- chore(readme): Add editor color palette examples to README
- chore(readme): General clean up

## v1.0.8 (04/30/20)

- feat(poet): Add block category support
Expand Down
29 changes: 27 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
![Build Status](https://img.shields.io/circleci/build/github/Log1x/poet?style=flat-square)
![Total Downloads](https://img.shields.io/packagist/dt/log1x/poet?style=flat-square)

Poet provides simple configuration-based post type, taxonomy, block category, and block registration and modification.
Poet provides simple configuration-based post type, taxonomy, editor color palette, block category, and block registration/modification.

Blocks registered with Poet are rendered using Laravel Blade on the frontend giving you full control over rendered block markup.

Expand Down Expand Up @@ -90,7 +90,7 @@ Please note that some built-in post types (e.g. Post) can not be conventionally
For additional configuration options for post types, please see:

- [`register_post_type()`](https://developer.wordpress.org/reference/functions/register_post_type/)
- [`register_extended_post_type()`](https://github.com/johnbillion/extended-cpts/wiki/Registering-Post-Types).
- [`register_extended_post_type()`](https://github.com/johnbillion/extended-cpts/wiki/Registering-Post-Types)

> **Note**: Do not nest configuration in a `config` key like shown in the Extended CPTs documentation.
Expand Down Expand Up @@ -146,6 +146,31 @@ For additional configuration options for taxonomies, please see:

> **Note**: Do not nest configuration in a `config` key like shown in the Extended CPTs documentation.
### Registering an Editor Color Palette

Poet attempts to simplify registering a color palette with the editor a bit by not requiring such strict, fragile array markup.

While you can of course pass said array directly, you are also able to register colors by simply passing a slug along with a color and letting Poet handle the rest.

```php
'palette' => [
'red' => '#ff0000',
'blue' => '#0000ff',
],
```

Alternatively to passing an array, Poet also accepts a `JSON` file containing your color palette. Poet will generally look for this file in `dist/` by default.

```php
'palette' => 'colors.json',
```

If you are using the [Palette Webpack Plugin](https://github.com/roots/palette-webpack-plugin), you may also simply pass `true` to automatically use the generated `palette.json` during build.

```php
'palette' => true,
```

### Registering a Block Category

Poet provides an easy to way register, modify, and unregister Gutenberg block categories. Looking in the config, you will see a commented out example for a Call to Action category:
Expand Down
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@
"name": "log1x/poet",
"type": "package",
"license": "MIT",
"description": "Configuration-based post types and taxonomies for Sage 10.",
"description": "Configuration-based post type, taxonomy, editor color palette, block category, and block registration for Sage 10.",
"authors": [
{
"name": "Brandon Nifong",
"email": "[email protected]"
}
],
"keywords": [
"wordpress"
"wordpress",
"gutenberg",
"cpt"
],
"support": {
"issues": "https://github.com/log1x/poet/issues"
Expand Down
25 changes: 25 additions & 0 deletions config/poet.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,29 @@
// ],
],

/*
|--------------------------------------------------------------------------
| Editor Palette
|--------------------------------------------------------------------------
|
| Here you may specify the color palette registered in the Gutenberg
| editor.
|
| A color palette can be passed as an array or by passing the filename of
| a JSON file containing the palette.
|
| If a color is passed a value directly, the slug will automatically be
| converted to Title Case and used as the color name.
|
| If the palette is explicitly set to `true` – Poet will attempt to
| register the palette using the default `palette.json` filename generated
| by <https://github.com/roots/palette-webpack-plugin>
|
*/

'palette' => [
// 'red' => '#ff0000',
// 'blue' => '#0000ff',
],

];
54 changes: 54 additions & 0 deletions src/Poet.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Illuminate\Support\Str;
use Illuminate\Support\Arr;

use function Roots\asset;

class Poet
{
/**
Expand All @@ -31,6 +33,7 @@ public function __construct($config = [])
$this->registerTaxonomies();
$this->registerBlocks();
$this->registerCategories();
$this->registerPalette();
}, 20);
}

Expand Down Expand Up @@ -223,6 +226,57 @@ protected function registerCategories()
});
}

/**
* Register the configured color palette with the editor.
*
* @link https://developer.wordpress.org/block-editor/developers/themes/theme-support/#block-color-palettes
* @return void
*/
protected function registerPalette()
{
if (
(is_bool($palette = $this->config->get('palette')) && $palette === true) ||
is_string($palette)
) {
$palette = json_decode(
asset(Str::finish(is_string($palette) ? $palette : 'palette', '.json'))->contents(),
true
);

if (empty($palette)) {
return;
}

return add_theme_support('editor-color-palette', $palette);
}

$palette = $this->config
->only('palette')
->collapse()
->map(function ($value, $key) {
if (! is_array($value)) {
return [
'name' => Str::title($key),
'slug' => Str::slug($key),
'color' => $value,
];
}

return array_merge([
'name' => Str::title($key),
'slug' => Str::slug($key),
], $value ?? []);
})
->values()
->filter();

if ($palette->isEmpty()) {
return;
}

return add_theme_support('editor-color-palette', $palette->all());
}

/**
* Checks if a post type or taxonomy already exists.
*
Expand Down

0 comments on commit f6f7d8d

Please sign in to comment.