Last active
October 9, 2019 07:36
-
-
Save avillegasn/9253525fb8beda6afaa767a9cb2fa3a4 to your computer and use it in GitHub Desktop.
Fragments of code to add a custom button to Gutenberg rich text blocks
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
import ElementIcon from '../images/logo.svg'; | |
const { Fragment } = wp.element; | |
const { __ } = window.wp.i18n; | |
const { registerFormatType, unregisterFormatType } = window.wp.richText; | |
const { RichTextToolbarButton } = window.wp.blockEditor; | |
unregisterFormatType( 'nelio/button' ); | |
registerFormatType( 'nelio/button', { | |
title: __( 'Nelio', 'nelio-content' ), | |
tagName: 'span', | |
className: 'nelio', | |
edit({ value }) { | |
const onClick = () => doTheJob( value ); | |
return ( | |
<Fragment> | |
<RichTextToolbarButton | |
icon={ <ElementIcon /> } | |
title={ __( 'Nelio', 'nelio-content' ) } | |
onClick={ onClick } | |
/> | |
</Fragment> | |
); | |
} | |
}); | |
function doTheJob( value ) { | |
let selectedText = value.text.substring( value.start, value.end ); | |
if ( 0 === selectedText.length ) { | |
return; | |
}//end if | |
// Do things with the selected text here... | |
console.log( selectedText ); | |
}//end openDialogToCreateAMessage() |
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
const path = require( 'path' ); | |
const webpack = require( 'webpack' ); | |
const CopyWebpackPlugin = require( 'copy-webpack-plugin' ); | |
const ExtractTextPlugin = require( 'extract-text-webpack-plugin' ); | |
const ImageminPlugin = require( 'imagemin-webpack-plugin' ).default; | |
const CleanWebpackPlugin = require( 'clean-webpack-plugin' ); | |
const WebpackRTLPlugin = require( 'webpack-rtl-plugin' ); | |
const wpPot = require( 'wp-pot' ); | |
const inProduction = ( 'production' === process.env.NODE_ENV ); | |
const config = { | |
// Ensure modules like magnific know jQuery is external (loaded via WP). | |
externals: { | |
$: 'jQuery', | |
jquery: 'jQuery', | |
lodash: 'lodash', | |
react: 'React', | |
}, | |
devtool: 'source-map', | |
module: { | |
rules: [ | |
// Use Babel to compile JS. | |
{ | |
test: /\.js$/, | |
exclude: /node_modules/, | |
loaders: [ | |
'babel-loader', | |
], | |
}, | |
// SVG files. | |
{ | |
test: /.svg$/, | |
use: [ | |
{ | |
loader: 'svg-react-loader', | |
}, | |
], | |
}, | |
], | |
}, | |
// Plugins. Gotta have em'. | |
plugins: [ | |
// Removes the "dist" folder before building. | |
new CleanWebpackPlugin( [ 'dist' ] ), | |
// Copy index.php to all dist directories. | |
new CopyWebpackPlugin( [ { from: 'index.php', to: '.' } ] ), | |
], | |
}; | |
module.exports = [ | |
Object.assign( { | |
entry: { | |
gutenberg: [ './src/js/gutenberg.js' ], | |
}, | |
output: { | |
path: path.join( __dirname, './dist/' ), | |
filename: 'js/[name].js', | |
library: 'NelioBlocks', | |
libraryTarget: 'umd', | |
}, | |
}, config ), | |
]; | |
// inProd? | |
if ( inProduction ) { | |
// POT file. | |
wpPot( { | |
package: 'Nelio', | |
domain: 'nelio', | |
destFile: 'languages/nelio.pot', | |
relativeTo: './', | |
team: 'Nelio Software <[email protected]>', | |
} ); | |
// Uglify JS. | |
config.plugins.push( new webpack.optimize.UglifyJsPlugin( { sourceMap: true } ) ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment