-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Packages: Add common utilities to WP.com block editor package #32294
Changes from all commits
6d786f4
eaa1ace
b6d1102
a12f1c8
6d963c8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
module.exports = { | ||
rules: { | ||
'import/no-extraneous-dependencies': [ 'error', { packageDir: __dirname } ], | ||
'react/react-in-jsx-scope': 0, | ||
}, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,19 +16,25 @@ | |
}, | ||
"homepage": "https://github.com/Automattic/wp-calypso", | ||
"scripts": { | ||
"clean": "npx rimraf dist", | ||
"prebuild": "npm run clean", | ||
"bundle": "webpack --config='../../packages/calypso-build/webpack.config.js' --env.WP='true' iframe-bridge-server='./src/iframe-bridge-server.js' tinymce='./src/tinymce.js'", | ||
"build": "npm run bundle -- --output-path=./dist" | ||
"bundle": "webpack --env.WP='true'", | ||
"build:dev": "npm run bundle --", | ||
"build:prod": "NODE_ENV=production npm run bundle --", | ||
"build": "npm-run-all --parallel \"build:* -- {@}\" --" | ||
}, | ||
"dependencies": { | ||
"@wordpress/data": "4.4.0", | ||
"@wordpress/blocks": "6.0.7", | ||
"@wordpress/compose": "3.2.0", | ||
"@wordpress/editor": "9.2.2", | ||
"@wordpress/hooks": "2.0.5", | ||
"@wordpress/rich-text": "3.2.2", | ||
"@wordpress/url": "2.3.3", | ||
"jquery": "^1.12", | ||
"lodash": "4.17.11", | ||
"react": "16.8.6", | ||
"tinymce": "4.8.5" | ||
}, | ||
"devDependencies": { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A peculiarity of monorepo: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
"@automattic/calypso-build": "file:../../packages/calypso-build" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import './rich-text'; | ||
import './switch-to-classic'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* global wpcomGutenberg */ | ||
|
||
/** | ||
* External dependencies | ||
*/ | ||
import { compose, ifCondition } from '@wordpress/compose'; | ||
import { withSelect, withDispatch } from '@wordpress/data'; | ||
import { RichTextToolbarButton } from '@wordpress/editor'; | ||
import { toggleFormat, registerFormatType } from '@wordpress/rich-text'; | ||
import { get } from 'lodash'; | ||
|
||
registerFormatType( 'wpcom/underline', { | ||
title: wpcomGutenberg.richTextToolbar.underline, | ||
tagName: 'span', | ||
className: null, | ||
attributes: { style: 'style' }, | ||
edit( { isActive, value, onChange } ) { | ||
const onToggle = () => | ||
onChange( | ||
toggleFormat( value, { | ||
type: 'wpcom/underline', | ||
attributes: { | ||
style: 'text-decoration: underline;', | ||
}, | ||
} ) | ||
); | ||
|
||
return ( | ||
<RichTextToolbarButton | ||
icon="editor-underline" | ||
title={ wpcomGutenberg.richTextToolbar.underline } | ||
onClick={ onToggle } | ||
isActive={ isActive } | ||
/> | ||
); | ||
}, | ||
} ); | ||
|
||
const RichTextJustifyButton = ( { blockId, isBlockJustified, updateBlockAttributes } ) => { | ||
const onToggle = () => | ||
updateBlockAttributes( blockId, { align: isBlockJustified ? null : 'justify' } ); | ||
|
||
return ( | ||
<RichTextToolbarButton | ||
icon="editor-justify" | ||
title={ wpcomGutenberg.richTextToolbar.justify } | ||
onClick={ onToggle } | ||
isActive={ isBlockJustified } | ||
/> | ||
); | ||
}; | ||
|
||
const ConnectedRichTextJustifyButton = compose( | ||
withSelect( select => { | ||
const selectedBlock = select( 'core/editor' ).getSelectedBlock(); | ||
if ( ! selectedBlock ) { | ||
return {}; | ||
} | ||
return { | ||
blockId: selectedBlock.clientId, | ||
blockName: selectedBlock.name, | ||
isBlockJustified: 'justify' === get( selectedBlock, 'attributes.align' ), | ||
}; | ||
} ), | ||
withDispatch( dispatch => ( { | ||
updateBlockAttributes: dispatch( 'core/editor' ).updateBlockAttributes, | ||
} ) ), | ||
ifCondition( props => 'core/paragraph' === props.blockName ) | ||
)( RichTextJustifyButton ); | ||
|
||
registerFormatType( 'wpcom/justify', { | ||
title: wpcomGutenberg.richTextToolbar.justify, | ||
tagName: 'p', | ||
className: null, | ||
attributes: { style: 'style' }, | ||
edit: ConnectedRichTextJustifyButton, | ||
} ); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* global _currentSiteId, wpcomGutenberg */ | ||
|
||
/** | ||
* External dependencies | ||
*/ | ||
import $ from 'jquery'; | ||
|
||
function addSwitchToClassicButton() { | ||
if ( ! wpcomGutenberg.switchToClassic.isVisible ) { | ||
return; | ||
} | ||
|
||
$( '#editor' ).on( 'click', '.edit-post-more-menu .components-button', () => { | ||
// We need to wait a few ms until the menu content is rendered | ||
setTimeout( () => { | ||
$( '.edit-post-more-menu__content .components-menu-group:last-child > div[role=menu]' ) | ||
.append( ` | ||
<button type="button" aria-label="${ wpcomGutenberg.switchToClassic.label }" role="menuitem" | ||
class="components-button components-menu-item__button components-menu-item__button-switch"> | ||
${ wpcomGutenberg.switchToClassic.label } | ||
</button>; | ||
` ); | ||
$( '.components-menu-item__button-switch' ).on( 'click', () => { | ||
$.wpcom_proxy_request( { | ||
method: 'POST', | ||
path: `/sites/${ _currentSiteId }/gutenberg`, | ||
apiNamespace: 'wpcom/v2', | ||
query: { | ||
platform: 'web', | ||
editor: 'classic', | ||
}, | ||
} ).done( () => { | ||
if ( wpcomGutenberg.isCalypsoify ) { | ||
top.window.location.replace( wpcomGutenberg.switchToClassic.url ); | ||
} else { | ||
top.window.location.reload(); | ||
} | ||
} ); | ||
} ); | ||
}, 0 ); | ||
} ); | ||
} | ||
|
||
$( () => { | ||
addSwitchToClassicButton(); | ||
} ); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** | ||
**** WARNING: No ES6 modules here. Not transpiled! **** | ||
*/ | ||
/* eslint-disable import/no-nodejs-modules */ | ||
|
||
/** | ||
* External dependencies | ||
*/ | ||
const getBaseWebpackConfig = require( '@automattic/calypso-build/webpack.config.js' ); | ||
const path = require( 'path' ); | ||
|
||
/** | ||
* Internal variables | ||
*/ | ||
const isDevelopment = process.env.NODE_ENV !== 'production'; | ||
|
||
/** | ||
* Return a webpack config object | ||
* | ||
* Arguments to this function replicate webpack's so this config can be used on the command line, | ||
* with individual options overridden by command line args. | ||
* | ||
* @see {@link https://webpack.js.org/configuration/configuration-types/#exporting-a-function} | ||
* @see {@link https://webpack.js.org/api/cli/} | ||
* | ||
* @param {object} env environment options | ||
* @param {object} argv options map | ||
* @param {object} argv.entry Entry point(s) | ||
* @param {string} argv.'output-path' Output path | ||
* @param {string} argv.'output-filename' Output filename pattern | ||
* @param {string} argv.'output-library-target' Output library target | ||
* @return {object} webpack config | ||
*/ | ||
function getWebpackConfig( | ||
env = {}, | ||
{ | ||
entry = { | ||
common: path.join( __dirname, 'src', 'common' ), | ||
'calypso-iframe-bridge-server': path.join( | ||
__dirname, | ||
'src', | ||
'calypso', | ||
'iframe-bridge-server.js' | ||
), | ||
'calypso-tinymce': path.join( __dirname, 'src', 'calypso', 'tinymce.js' ), | ||
}, | ||
'output-path': outputPath = path.join( __dirname, 'dist' ), | ||
'output-filename': outputFilename = isDevelopment ? '[name].js' : '[name].min.js', | ||
} | ||
) { | ||
const webpackConfig = getBaseWebpackConfig( env, { | ||
entry, | ||
'output-filename': outputFilename, | ||
'output-path': outputPath, | ||
} ); | ||
|
||
return { | ||
...webpackConfig, | ||
devtool: isDevelopment ? 'inline-cheap-source-map' : false, | ||
}; | ||
} | ||
|
||
module.exports = getWebpackConfig; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this gets too funky at some point, you could also create a simple Webpack config for this package that inherits and extends the one from Calypso-build, similarly to
client/notifications/
.