Skip to content

Instantly share code, notes, and snippets.

@kkarpieszuk
Created November 15, 2018 21:33
Show Gist options
  • Save kkarpieszuk/265a89d66d6fafe4cc3517a749178e56 to your computer and use it in GitHub Desktop.
Save kkarpieszuk/265a89d66d6fafe4cc3517a749178e56 to your computer and use it in GitHub Desktop.
/**
* The following styles get applied inside the editor only.
*
* Replace them with your own styles or remove the file completely.
*/
.wp-block-syntax-block-syntax-highliter {
border: 1px dotted #f00;
}
( function( wp ) {
/**
* Registers a new block provided a unique name and an object defining its behavior.
* @see https://github.com/WordPress/gutenberg/tree/master/blocks#api
*/
var registerBlockType = wp.blocks.registerBlockType;
/**
* Returns a new element of given type. Element is an abstraction layer atop React.
* @see https://github.com/WordPress/gutenberg/tree/master/element#element
*/
var el = wp.element.createElement;
/**
* Retrieves the translation of text.
* @see https://github.com/WordPress/gutenberg/tree/master/i18n#api
*/
var __ = wp.i18n.__;
/**
* Every block starts by registering a new block type definition.
* @see https://wordpress.org/gutenberg/handbook/block-api/
*/
registerBlockType( 'syntax-block/syntax-highliter', {
/**
* This is the display title for your block, which can be translated with `i18n` functions.
* The block inserter will show this name.
*/
title: __( 'Syntax Highliter' ),
/**
* Blocks are grouped into categories to help users browse and discover them.
* The categories provided by core are `common`, `embed`, `formatting`, `layout` and `widgets`.
*/
category: 'formatting',
/**
* Optional block extended support features.
*/
supports: {
// Removes support for an HTML mode.
html: false,
},
/**
* The edit function describes the structure of your block in the context of the editor.
* This represents what the editor will render when the block is used.
* @see https://wordpress.org/gutenberg/handbook/block-edit-save/#edit
*
* @param {Object} [props] Properties passed from the editor.
* @return {Element} Element to render.
*/
edit: function( props ) {
return el(
'p',
{ className: props.className },
__( 'Hello from the editor!' )
);
},
/**
* The save function defines the way in which the different attributes should be combined
* into the final markup, which is then serialized by Gutenberg into `post_content`.
* @see https://wordpress.org/gutenberg/handbook/block-edit-save/#save
*
* @return {Element} Element to render.
*/
save: function() {
return el(
'p',
{},
__( 'Hello from the saved content!' )
);
}
} );
} )(
window.wp
);
/**
* The following styles get applied both on the front of your site and in the editor.
*
* Replace them with your own styles or remove the file completely.
*/
.wp-block-syntax-block-syntax-highliter {
background-color: #000;
color: #fff;
padding: 2px;
}
<?php
/**
* Functions to register client-side assets (scripts and stylesheets) for the
* Gutenberg block.
*
* @package syntax-block
*/
/**
* Registers all block assets so that they can be enqueued through Gutenberg in
* the corresponding context.
*
* @see https://wordpress.org/gutenberg/handbook/blocks/writing-your-first-block-type/#enqueuing-block-scripts
*/
function syntax_highliter_block_init() {
// Skip block registration if Gutenberg is not enabled/merged.
if ( ! function_exists( 'register_block_type' ) ) {
return;
}
$dir = dirname( __FILE__ );
$index_js = 'syntax-highliter/index.js';
wp_register_script(
'syntax-highliter-block-editor',
plugins_url( $index_js, __FILE__ ),
array(
'wp-blocks',
'wp-i18n',
'wp-element',
),
filemtime( "$dir/$index_js" )
);
$editor_css = 'syntax-highliter/editor.css';
wp_register_style(
'syntax-highliter-block-editor',
plugins_url( $editor_css, __FILE__ ),
array(),
filemtime( "$dir/$editor_css" )
);
$style_css = 'syntax-highliter/style.css';
wp_register_style(
'syntax-highliter-block',
plugins_url( $style_css, __FILE__ ),
array(),
filemtime( "$dir/$style_css" )
);
register_block_type( 'syntax-block/syntax-highliter', array(
'editor_script' => 'syntax-highliter-block-editor',
'editor_style' => 'syntax-highliter-block-editor',
'style' => 'syntax-highliter-block',
) );
}
add_action( 'init', 'syntax_highliter_block_init' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment