-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Format API example * Use destructuring, proper linting
- Loading branch information
Showing
5 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": [ "plugin:@wordpress/eslint-plugin/recommended" ] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
/** | ||
* Plugin Name: Gutenberg Examples Format API | ||
* Plugin URI: https://github.com/WordPress/gutenberg-examples | ||
* Description: This is a plugin demonstrating how to use Format API in the Gutenberg editor. | ||
* Version: 1.0.0 | ||
* Author: the Gutenberg Team | ||
* | ||
* @package gutenberg-examples | ||
*/ | ||
|
||
defined( 'ABSPATH' ) || exit; | ||
|
||
/** | ||
* Registers all block assets so that they can be enqueued through Gutenberg in | ||
* the corresponding context. | ||
*/ | ||
add_action( 'init', function() { | ||
// Automatically load dependencies and version. | ||
$asset_file = include plugin_dir_path( __FILE__ ) . 'build/index.asset.php'; | ||
|
||
wp_register_script( | ||
'gutenberg-examples-format-api', | ||
plugins_url( 'build/index.js', __FILE__ ), | ||
$asset_file['dependencies'], | ||
$asset_file['version'], | ||
true | ||
); | ||
} ); | ||
|
||
/** | ||
* Enqueue block editor assets for this example. | ||
*/ | ||
add_action( 'enqueue_block_editor_assets', function() { | ||
wp_enqueue_script( 'gutenberg-examples-format-api' ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"name": "format-api", | ||
"version": "1.0.0", | ||
"description": "Example: Format API", | ||
"author": "The WordPress Contributors", | ||
"license": "GPL-2.0-or-later", | ||
"keywords": [ | ||
"WordPress", | ||
"block" | ||
], | ||
"homepage": "https://github.com/WordPress/gutenberg-examples/", | ||
"repository": "git+https://github.com/WordPress/gutenberg-examples.git", | ||
"bugs": { | ||
"url": "https://github.com/WordPress/gutenberg-examples/issues" | ||
}, | ||
"main": "build/index.js", | ||
"devDependencies": { | ||
"@wordpress/scripts": "^18.0.1" | ||
}, | ||
"scripts": { | ||
"build": "wp-scripts build", | ||
"format:js": "wp-scripts format", | ||
"lint:js": "wp-scripts lint-js", | ||
"packages-update": "wp-scripts packages-update", | ||
"start": "wp-scripts start" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* WordPress dependencies. | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { registerFormatType, toggleFormat } from "@wordpress/rich-text"; | ||
import { RichTextToolbarButton } from "@wordpress/block-editor"; | ||
|
||
const MyCustomButton = ({ isActive, onChange, value }) => { | ||
return ( | ||
<RichTextToolbarButton | ||
icon="editor-code" | ||
title={ __( "Sample output" ) } | ||
onClick={ () => { | ||
onChange( | ||
toggleFormat(value, { | ||
type: "my-custom-format/sample-output", | ||
}) | ||
); | ||
} } | ||
isActive={ isActive } | ||
/> | ||
); | ||
}; | ||
|
||
registerFormatType( "my-custom-format/sample-output", { | ||
title: __( "Sample output" ), | ||
tagName: "samp", | ||
className: null, | ||
edit: MyCustomButton, | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters