Skip to content
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

DevDocs: Add block playground #44153

Open
wants to merge 10 commits into
base: trunk
Choose a base branch
from
6 changes: 6 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ const isBrowser = process.env.BROWSERSLIST_ENV !== 'server';
const babelConfig = {
presets: [ '@automattic/calypso-build/babel/default' ],
plugins: [ [ '@automattic/transform-wpcalypso-async', { async: isBrowser } ] ],
overrides: [
{
test: [ './apps/o2-blocks/src' ],
plugins: [ '@wordpress/import-jsx-pragma', '@babel/transform-react-jsx' ],
},
],
env: {
production: {
plugins: [ 'babel-plugin-transform-react-remove-prop-types' ],
Expand Down
66 changes: 66 additions & 0 deletions client/devdocs/block-playground/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* External dependencies
*/
import React, { FunctionComponent, useEffect, useState } from 'react';

/**
* Internal dependencies
*/
import Main from 'components/main';
import DocumentHead from 'components/data/document-head';

/**
* WordPress dependencies
*/
import {
BlockEditorProvider,
BlockList,
ObserveTyping,
WritingFlow,
} from '@wordpress/block-editor';
import { registerCoreBlocks } from '@wordpress/block-library';
import { BlockInstance, parse, serialize } from '@wordpress/blocks';

import '@wordpress/components/build-style/style.css';
import '@wordpress/block-editor/build-style/style.css';
import '@wordpress/block-library/build-style/style.css';
import '@wordpress/block-library/build-style/editor.css';
import '@wordpress/block-library/build-style/theme.css';

registerCoreBlocks();

export const BlockPlayground: FunctionComponent = () => {
useEffect( () => {
import( /* webpackChunkName: "o2-blocks" */ '../../../apps/o2-blocks/src/editor.js' );
}, [] );

const [ blocks, setBlocks ] = useState< BlockInstance[] >( [] );
const [ post, setPost ] = useState< string >( '' );
const update = ( newBlocks: BlockInstance[] ) => {
setBlocks( newBlocks );
setPost( serialize( newBlocks ) );
};

return (
<Main className="devdocs block-playground">
<DocumentHead title="Block Playground" />
<div>
<BlockEditorProvider value={ blocks } onInput={ update } onChange={ update }>
<WritingFlow>
<ObserveTyping>
<BlockList />
</ObserveTyping>
</WritingFlow>
</BlockEditorProvider>
</div>
<textarea
style={ { minHeight: '480px' } }
value={ post }
onChange={ ( event ) => setPost( event.target.value ) }
onBlur={ () => setBlocks( parse( post ) ) }
/>
</Main>
);
};

export default BlockPlayground;
7 changes: 7 additions & 0 deletions client/devdocs/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,13 @@ const devdocs = {
next();
},

blockPlayground: function ( context, next ) {
context.primary = (
<AsyncLoad require="./block-playground" component={ context.params.component } />
);
next();
},

typography: function ( context, next ) {
context.primary = (
<AsyncLoad component={ context.params.component } require="./design/typography" />
Expand Down
7 changes: 7 additions & 0 deletions client/devdocs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ export default function () {
makeLayout,
clientRender
);
page(
'/devdocs/block-playground',
controller.sidebar,
controller.blockPlayground,
makeLayout,
clientRender
);
page(
'/devdocs/typography',
controller.sidebar,
Expand Down
7 changes: 7 additions & 0 deletions client/devdocs/sidebar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@ export default class DevdocsSidebar extends React.PureComponent {
link="/devdocs/selectors"
selected={ this.isItemSelected( '/devdocs/selectors', false ) }
/>
<SidebarItem
className="devdocs__navigation-item"
icon="custom-post-type"
label="Block Playground"
link="/devdocs/block-playground"
selected={ this.isItemSelected( '/devdocs/block-playground', false ) }
/>
</ul>
</SidebarMenu>
</Sidebar>
Expand Down
1 change: 1 addition & 0 deletions packages/calypso-build/webpack/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ const nodeModulesToTranspile = [
'escape-string-regexp/',
'filesize/',
'gridicons/',
'new-github-issue-url/',
'prismjs/',
'punycode/',
'react-spring/',
Expand Down