Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
} ]
],
"plugins": [
"lodash",
"transform-runtime",
"transform-object-rest-spread",
[ "transform-react-jsx", {
Expand Down
2 changes: 2 additions & 0 deletions blocks/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* External dependencies
*/
import * as query from 'hpq';
import uuid from 'uuid/v4';

/**
* Internal dependencies
Expand Down Expand Up @@ -54,6 +55,7 @@ export default function parse( content ) {
if ( settings ) {
memo.push( {
blockType,
uid: uuid(),
rawContent: blockNode.rawContent,
attributes: getBlockAttributes( blockNode, settings )
} );
Expand Down
14 changes: 7 additions & 7 deletions blocks/test/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,13 @@ describe( 'block parser', () => {
'<!-- wp:core/unknown-block -->Ribs<!-- /wp:core/unknown-block -->'
);

expect( parsed ).to.eql( [ {
blockType: 'core/test-block',
attributes: {
content: 'Ribs & Chicken'
},
rawContent: 'Ribs'
} ] );
expect( parsed ).to.have.lengthOf( 1 );
expect( parsed[ 0 ].blockType ).to.equal( 'core/test-block' );
expect( parsed[ 0 ].attributes ).to.eql( {
content: 'Ribs & Chicken'
} );
expect( parsed[ 0 ].rawContent ).to.equal( 'Ribs' );
expect( parsed[ 0 ].uid ).to.be.a( 'string' );
} );

it( 'should parse the post content, using unknown block handler', () => {
Expand Down
6 changes: 2 additions & 4 deletions editor/header/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
*/
import ModeSwitcher from './mode-switcher';

function Header( { mode, onSwitchMode } ) {
function Header() {
return (
<header className="editor-header">
<ModeSwitcher
mode={ mode }
onSwitch={ onSwitchMode } />
<ModeSwitcher />
</header>
);
}
Expand Down
19 changes: 18 additions & 1 deletion editor/header/mode-switcher/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* External dependencies
*/
import { connect } from 'react-redux';

class ModeSwitcher extends wp.element.Component {
constructor() {
super( ...arguments );
Expand Down Expand Up @@ -50,4 +55,16 @@ class ModeSwitcher extends wp.element.Component {
}
}

export default ModeSwitcher;
export default connect(
( state ) => ( {
mode: state.mode
} ),
( dispatch ) => ( {
onSwitch( mode ) {
dispatch( {
type: 'SWITCH_MODE',
mode: mode
} );
}
} )
)( ModeSwitcher );
16 changes: 15 additions & 1 deletion editor/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
/**
* External dependencies
*/
import { Provider } from 'react-redux';

/**
* Internal dependencies
*/
import './assets/stylesheets/main.scss';
import './blocks';
import Layout from './layout';
import { createReduxStore } from './state';

/**
* Initializes and returns an instance of Editor.
Expand All @@ -12,8 +18,16 @@ import Layout from './layout';
* @param {Object} post API entity for post to edit
*/
export function createEditorInstance( id, post ) {
const store = createReduxStore();
store.dispatch( {
type: 'SET_HTML',
html: post.content.raw
} );

wp.element.render(
<Layout initialContent={ post.content.raw } />,
<Provider store={ store }>
<Layout />
</Provider>,
document.getElementById( id )
);
}
55 changes: 16 additions & 39 deletions editor/layout/index.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,25 @@
/**
* External dependencies
*/
import { connect } from 'react-redux';

/**
* Internal dependencies
*/
import Header from '../header';
import TextEditor from '../modes/text-editor';
import VisualEditor from '../modes/visual-editor';

class Layout extends wp.element.Component {
constructor( props ) {
super( ...arguments );
this.switchMode = this.switchMode.bind( this );
this.state = {
mode: 'visual',
html: props.initialContent,
blocks: wp.blocks.parse( this.props.initialContent )
};
}

switchMode( newMode ) {
// TODO: we need a serializer from blocks here
const html = this.state.html;
const blocks = this.mode === 'visual' ? this.state.blocks : wp.blocks.parse( this.state.html );
this.setState( {
mode: newMode,
html,
blocks
} );
}

createChangeHandler( type ) {
return ( value ) => this.setState( { [ type ]: value } );
}

render() {
const { mode, html, blocks } = this.state;
return (
<div>
<Header
mode={ mode }
onSwitchMode={ this.switchMode } />
{ mode === 'text' && <TextEditor html={ html } onChange={ this.createChangeHandler( 'html' ) } /> }
{ mode === 'visual' && <VisualEditor blocks={ blocks } onChange={ this.createChangeHandler( 'blocks' ) } /> }
</div>
);
}
function Layout( { mode } ) {
return (
<div>
<Header />
{ mode === 'text' && <TextEditor /> }
{ mode === 'visual' && <VisualEditor /> }
</div>
);
}

export default Layout;
export default connect( ( state ) => ( {
mode: state.mode
} ) )( Layout );
15 changes: 14 additions & 1 deletion editor/modes/text-editor/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* External dependencies
*/
import { connect } from 'react-redux';
import Textarea from 'react-textarea-autosize';

function TextEditor( { html, onChange } ) {
Expand All @@ -18,4 +19,16 @@ function TextEditor( { html, onChange } ) {
);
}

export default TextEditor;
export default connect(
( state ) => ( {
html: state.html
} ),
( dispatch ) => ( {
onChange( value ) {
dispatch( {
type: 'SET_HTML',
html: value
} );
}
} )
)( TextEditor );
47 changes: 47 additions & 0 deletions editor/modes/visual-editor/block.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* External dependencies
*/
import { connect } from 'react-redux';

function VisualEditorBlock( { block, onChange } ) {
const settings = wp.blocks.getBlockSettings( block.blockType );

let BlockEdit;
if ( settings ) {
BlockEdit = settings.edit || settings.save;
}

if ( ! BlockEdit ) {
return null;
}

function onAttributesChange( attributes ) {
onChange( {
attributes: {
...block.attributes,
...attributes
}
} );
}

return (
<BlockEdit
attributes={ block.attributes }
onChange={ onAttributesChange } />
);
}

export default connect(
( state, ownProps ) => ( {
block: state.blocks.byUid[ ownProps.uid ]
} ),
( dispatch, ownProps ) => ( {
onChange( updates ) {
dispatch( {
type: 'UPDATE_BLOCK',
uid: ownProps.uid,
updates
} );
}
} )
)( VisualEditorBlock );
47 changes: 13 additions & 34 deletions editor/modes/visual-editor/index.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,25 @@
/**
* External dependencies
*/
import { connect } from 'react-redux';

/**
* Internal dependencies
*/
import InserterButton from '../../inserter/button';
import VisualEditorBlock from './block';

function VisualEditor( { blocks, onChange } ) {
const onChangeBlock = ( index ) => ( changes ) => {
const newBlock = {
...blocks[ index ],
changes
};

onChange( [
...blocks.slice( 0, index ),
newBlock,
...blocks.slice( index + 1 )
] );
};

function VisualEditor( { blocks } ) {
return (
<div className="editor-visual-editor">
{ blocks.map( ( block, index ) => {
const settings = wp.blocks.getBlockSettings( block.blockType );

let BlockEdit;
if ( settings ) {
BlockEdit = settings.edit || settings.save;
}

if ( ! BlockEdit ) {
return;
}

return (
<BlockEdit
key={ index }
attributes={ block.attributes }
onChange={ onChangeBlock( index ) } />
);
} ) }
{ blocks.map( ( uid ) => (
<VisualEditorBlock key={ uid } uid={ uid } />
) ) }
<InserterButton />
</div>
);
}

export default VisualEditor;
export default connect( ( state ) => ( {
blocks: state.blocks.order
} ) )( VisualEditor );
Loading