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

Gutenberg Integration: load editor with a new post #27074

Merged
merged 25 commits into from
Sep 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
c05cfb9
Gutenberg Integration: Adds support to load Post contents using the w…
Sep 5, 2018
653455f
Adds selector to retrieve the current loaded post for Gutenberg.
Sep 7, 2018
4013910
Adds actions to create, fetch and process a new Post for Gutenberg
Sep 7, 2018
9640d4b
Adds a reducer to store Gutenberg's current post in state
Sep 7, 2018
8b2eb23
Removes unused file
Sep 7, 2018
3365818
Adds the wp/v2 API calls to support Gutenberg
Sep 7, 2018
493df76
Adds the query components so Gutenberg can be loaded with a new draft
Sep 7, 2018
0668100
Adds the necesary query components and checks to Gutenberg's post pag…
Sep 7, 2018
341979d
Renaming create post actions and handlers to make them Gutenberg spec…
Sep 7, 2018
b8b4219
Another rename, because naming things is hard...
Sep 7, 2018
dd75a65
Migrating tests to jest
Sep 8, 2018
2b0379c
Removes query components in favor of using plain data getters
Sep 10, 2018
e862e50
Removing all the boylerplate no longer needed to request draft and wp…
Sep 10, 2018
c460f8a
Removes test code (http -> rawHttp) on requestSiteAlerts.
Sep 10, 2018
aaa78e8
Adds empty action to http action creator to renive "missing onSuccess…
Sep 10, 2018
070b4cd
Fixes some missing imports after rebasing with master
Sep 11, 2018
df5abb4
Removes unused siteId prop from connect()
Sep 11, 2018
d51c397
Updates the request post draft method to use POST verb instead of GET
Sep 11, 2018
4ecc235
Extremely basic support for postId from the route
Sep 12, 2018
6029852
New component to inject a unique draft Id into the editor instance
Sep 12, 2018
1bb3de6
Request Gutenberg draft now accepts a draft Id
Sep 12, 2018
64e793e
wraps the Gutenberg editor connected component so it can access the u…
Sep 12, 2018
94869f6
Adds a unique draft Id on the controller
Sep 12, 2018
c1392de
Removes unused HOC component and refactors mapStateToProps to handle …
Sep 12, 2018
816e624
Reuses the p2-gutenberg create auto-draft endpoint
Sep 12, 2018
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
20 changes: 19 additions & 1 deletion client/gutenberg/editor/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
* External dependencies
*/
import React from 'react';
import { uniqueId } from 'lodash';

/**
* Internal dependencies
*/
import GutenbergEditor from 'gutenberg/editor/main';
import { getSelectedSiteId } from 'state/ui/selectors';

function determinePostType( context ) {
if ( context.path.startsWith( '/gutenberg/post/' ) ) {
Expand All @@ -20,9 +22,25 @@ function determinePostType( context ) {
return context.params.customPostType;
}

//duplicated from post-editor/controller.js. We should import it from there instead
function getPostID( context ) {
if ( ! context.params.post || 'new' === context.params.post ) {
return null;
}

// both post and site are in the path
return parseInt( context.params.post, 10 );
}

export const post = ( context, next ) => {
const state = context.store.getState();

const siteId = getSelectedSiteId( state );
const postId = getPostID( context );
const uniqueDraftKey = uniqueId( 'gutenberg-draft-' );
const postType = determinePostType( context );

//reserved space for adding the post to context see post-editor/controller.js
context.primary = <GutenbergEditor postType={ postType } />;
context.primary = <GutenbergEditor { ...{ siteId, postId, postType, uniqueDraftKey } } />;
next();
};
35 changes: 23 additions & 12 deletions client/gutenberg/editor/main.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/** @format */

/**
* External dependencies
*/
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { noop } from 'lodash';
import { get, noop } from 'lodash';
import { dispatch } from '@wordpress/data';
import '@wordpress/core-data'; // Initializes core data store
import { registerCoreBlocks } from '@wordpress/block-library';
Expand All @@ -15,27 +16,27 @@ import { registerCoreBlocks } from '@wordpress/block-library';
import Editor from './edit-post/editor.js';
import EditorPostTypeUnsupported from 'post-editor/editor-post-type-unsupported';
import QueryPostTypes from 'components/data/query-post-types';
import { getSelectedSiteId } from 'state/ui/selectors';
import { requestGutenbergDraftPost as createAutoDraft, requestSitePost } from 'state/data-getters';
import { getHttpData } from 'state/data-layer/http-data';
import { getSiteSlug } from 'state/sites/selectors';
import { WithAPIMiddleware } from './api-middleware/utils';

const editorSettings = {};

const mockPost = {
type: 'post',
content: { raw: 'test content' },
};

class GutenbergEditor extends Component {
componentDidMount() {
registerCoreBlocks();
// Prevent Guided tour from showing when editor loads.
dispatch( 'core/nux' ).disableTips();

const { siteId, postId, uniqueDraftKey } = this.props;
if ( ! postId ) {
createAutoDraft( siteId, uniqueDraftKey );
}
}

render() {
const { postType, siteId, siteSlug } = this.props;
const post = { ...mockPost, type: postType };
const { postType, siteId, siteSlug, post } = this.props;

return (
<WithAPIMiddleware siteSlug={ siteSlug }>
Expand All @@ -52,12 +53,22 @@ class GutenbergEditor extends Component {
}
}

const mapStateToProps = state => {
const siteId = getSelectedSiteId( state );
const getPost = ( siteId, postId ) => {
if ( siteId && postId ) {
const requestSitePostData = requestSitePost( siteId, postId );
return get( requestSitePostData, 'data', null );
}

return null;
};

const mapStateToProps = ( state, { siteId, postId, uniqueDraftKey } ) => {
const draftPostId = get( getHttpData( uniqueDraftKey ), 'data.ID', null );
const post = getPost( siteId, postId || draftPostId );

return {
siteId,
siteSlug: getSiteSlug( state, siteId ),
post,
};
};

Expand Down
30 changes: 30 additions & 0 deletions client/state/data-getters/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/** @format */

/**
* Internal dependencies
*/
Expand Down Expand Up @@ -100,3 +101,32 @@ export const requestSiteAlerts = siteId => {
}
);
};

export const requestGutenbergDraftPost = ( siteId, draftId ) =>
requestHttpData(
draftId,
http(
{
path: `/sites/${ siteId }/p2/post`,
method: 'GET', //this should be a POST, remember
apiNamespace: 'wpcom/v2',
body: {}, //this is for a POST verb.
},
{}
),
{ formApi: () => data => [ [ draftId, data ] ] }
);

export const requestSitePost = ( siteId, postId ) =>
requestHttpData(
`gutenberg-site-${ siteId }-post-${ postId }`,
http(
{
path: `/sites/${ siteId }/posts/${ postId }?context=edit`,
method: 'GET',
apiNamespace: 'wp/v2',
},
{}
),
{ fromApi: () => post => [ [ `gutenberg-site-${ siteId }-post-${ postId }`, post ] ] }
);