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

Add editor in reader behind a feature flag #76035

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
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
32 changes: 32 additions & 0 deletions client/data/posts/use-create-new-post.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { useMutation } from '@tanstack/react-query';
import { Post } from '@wordpress/core-data';
import { useCallback } from 'react';
import wpcom from 'calypso/lib/wp';
import { SiteId } from 'calypso/types';

export default function useCreateNewPost( mutationOptions = {} ) {
const mutation = useMutation( {
mutationFn: async ( { siteId, post }: { siteId: SiteId; post: Post } ) =>
wpcom.req.post(
`/sites/${ siteId }/posts`,
{
apiNamespace: 'wp/v2',
},
{
...post,
status: 'publish',
}
),

...mutationOptions,
} );

const { mutate } = mutation;

const createNewPost = useCallback(
( siteId: SiteId, post: Post ) => mutate( { siteId, post } ),
[ mutate ]
);

return { createNewPost, ...mutation };
}
2 changes: 2 additions & 0 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@automattic/global-styles": "workspace:^",
"@automattic/help-center": "workspace:^",
"@automattic/i18n-utils": "workspace:^",
"@automattic/isolated-block-editor": "2.24.x",
"@automattic/js-utils": "workspace:^",
"@automattic/language-picker": "workspace:^",
"@automattic/languages": "workspace:^",
Expand Down Expand Up @@ -83,6 +84,7 @@
"@wordpress/blocks": "^12.0.0",
"@wordpress/components": "^23.0.0",
"@wordpress/compose": "^6.0.0",
"@wordpress/core-data": "^6.11.0",
"@wordpress/data": "^8.0.0",
"@wordpress/dom": "^3.23.0",
"@wordpress/edit-post": "^7.0.0",
Expand Down
4 changes: 4 additions & 0 deletions client/reader/following/main.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import config from '@automattic/calypso-config';
import AsyncLoad from 'calypso/components/async-load';
import SuggestionProvider from 'calypso/reader/search-stream/suggestion-provider';
import Stream from 'calypso/reader/stream';
Expand All @@ -12,6 +13,9 @@ function FollowingStream( { ...props } ) {
<Stream
{ ...props }
className="following"
streamHeader={
config.isEnabled( 'reader/editor' ) && <AsyncLoad require="calypso/reader/post-editor" />
}
streamSidebar={ <ReaderListFollowedSites path={ window.location.pathname } /> }
>
<FollowingIntro />
Expand Down
79 changes: 79 additions & 0 deletions client/reader/post-editor/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import IsoloatedEditor, { ToolbarSlot } from '@automattic/isolated-block-editor';
import { serialize } from '@wordpress/blocks';
import { Button } from '@wordpress/components';
import { select, useDispatch } from '@wordpress/data';
import { useTranslate } from 'i18n-calypso';
import { useSelector, useDispatch as useReduxDispatch } from 'react-redux';
import useCreateNewPost from 'calypso/data/posts/use-create-new-post';
import { successNotice, errorNotice } from 'calypso/state/notices/actions';
import getPrimarySiteId from 'calypso/state/selectors/get-primary-site-id';
import '@automattic/isolated-block-editor/build-browser/core.css';
import './style.scss';

const noticeOptions = {
duration: 5000,
id: 'reader-post-editor-create-post-notice',
isDismissible: true,
};

function ReaderPostEditor() {
const dispatch = useDispatch();
const translate = useTranslate();

// Use global redux store for Calypso state and actions
const reduxDispatch = useReduxDispatch();
const primarySiteId = useSelector( getPrimarySiteId );

const { createNewPost, isLoading } = useCreateNewPost( {
onSuccess: () => {
reduxDispatch( successNotice( translate( 'Post published!' ), noticeOptions ) );
dispatch( 'core/block-editor' ).resetBlocks( [] );
},
onError: ( error ) => {
const errorMessage = error?.message || translate( 'Something went wrong, please try again.' );
reduxDispatch( errorNotice( errorMessage, noticeOptions ) );
},
} );

const editorSettings = {
iso: {
moreMenu: false,
footer: true,
},
editor: {
bodyPlaceholder: translate( "What's on your mind?" ),
hasFixedToolbar: true,
},
};

function publishPost() {
const blocks = select( 'core/block-editor' ).getBlocks();

if ( blocks && blocks.length > 0 ) {
createNewPost( primarySiteId, {
content: serialize( blocks ),
creativecoder marked this conversation as resolved.
Show resolved Hide resolved
} );
}
}

return (
<div className="reader-post-editor">
<div className="reader-post-editor__editor">
<IsoloatedEditor settings={ editorSettings }>
<ToolbarSlot>
<Button
className="reader-post-editor__publish-button"
isPrimary
onClick={ publishPost }
disabled={ isLoading }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it also be disabled when there are no blocks?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had the same idea, but found this surprisingly tricky because select( 'core/block-editor' ).getBlocks() seems to only work if run within the scope of IsolatedBlockEditor, rather than within the parent component.

I didn't see an immediate way to pass a function that the button component runs to determine if it's disabled, rather than passing it a predetermined value.

I'll plan to address this in a follow-up, though!

>
{ isLoading ? translate( 'Publishing…' ) : translate( 'Publish' ) }
</Button>
</ToolbarSlot>
</IsoloatedEditor>
</div>
</div>
);
}

export default ReaderPostEditor;
18 changes: 18 additions & 0 deletions client/reader/post-editor/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
.reader-post-editor {
margin-bottom: 80px;

.components-menu-items__item-icon {
// Make sure menu icons display at the intended size.
height: 24px;
width: 24px;
}
}

.reader-post-editor__editor {
margin-bottom: 12px;
width: 100%;
}

.reader-post-editor__publish-button {
float: right;
}
1 change: 1 addition & 0 deletions config/development.json
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@
"push-notifications": true,
"reader": true,
"reader/comment-polling": false,
"reader/editor": true,
"reader/full-errors": true,
"reader/list-management": true,
"reader/public-tag-pages": true,
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@
"@wordpress/jest-console": "6.6.0",
"@wordpress/jest-preset-default": "10.4.0",
"@wordpress/jest-puppeteer-axe": "5.6.0",
"@wordpress/keyboard-shortcuts": "4.0.0",
"@wordpress/keyboard-shortcuts": "4.9.0",
"@wordpress/keycodes": "3.23.0",
"@wordpress/lazy-import": "1.10.0",
"@wordpress/library-export-default-webpack-plugin": "2.9.0",
Expand Down Expand Up @@ -387,7 +387,8 @@
"@wordpress/viewport": "5.0.0",
"@wordpress/warning": "2.23.0",
"@wordpress/widgets": "3.0.0",
"@wordpress/wordcount": "3.23.0"
"@wordpress/wordcount": "3.23.0",
"framer-motion": "10.11.6"
},
"packageManager": "[email protected]",
"dependenciesMeta": {
Expand Down
Loading