Skip to content

Commit

Permalink
Add a welcome guide to the template editor. (#31330)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Richards <[email protected]>
  • Loading branch information
2 people authored and jffng committed May 19, 2021
1 parent 3fffffa commit da43d81
Show file tree
Hide file tree
Showing 6 changed files with 245 additions and 138 deletions.
100 changes: 64 additions & 36 deletions packages/e2e-tests/specs/experiments/post-editor-template-mode.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,68 @@ const openSidebarPanelWithTitle = async ( title ) => {
await panel.click();
};

const disableTemplateWelcomeGuide = async () => {
// Turn off the welcome guide if it's visible.
const isWelcomeGuideActive = await page.evaluate( () =>
wp.data
.select( 'core/edit-post' )
.isFeatureActive( 'welcomeGuideTemplate' )
);
if ( isWelcomeGuideActive ) {
await page.evaluate( () =>
wp.data
.dispatch( 'core/edit-post' )
.toggleFeature( 'welcomeGuideTemplate' )
);
}
};

const switchToTemplateMode = async () => {
// Switch to template mode.
await openDocumentSettingsSidebar();
await openSidebarPanelWithTitle( 'Template' );
const editTemplateXPath =
"//*[contains(@class, 'edit-post-template__actions')]//button[contains(text(), 'Edit')]";
const switchLink = await page.waitForXPath( editTemplateXPath );
await switchLink.click();

// Check that we switched properly to edit mode.
await page.waitForXPath(
'//*[contains(@class, "components-snackbar")]/*[text()="Editing template. Changes made here affect all posts and pages that use the template."]'
);
const title = await page.$eval(
'.edit-post-template-title',
( el ) => el.innerText
);
expect( title ).toContain( 'Editing template:' );

await disableTemplateWelcomeGuide();
};

const createNewTemplate = async ( templateName ) => {
// Create a new custom template.
await openDocumentSettingsSidebar();
await openSidebarPanelWithTitle( 'Template' );
const newTemplateXPath =
"//*[contains(@class, 'edit-post-template__actions')]//button[contains(text(), 'New')]";
const newButton = await page.waitForXPath( newTemplateXPath );
await newButton.click();

// Fill the template title and submit.
const templateNameInputSelector =
'.edit-post-template__modal .components-text-control__input';
await page.click( templateNameInputSelector );
await page.keyboard.type( templateName );
await page.keyboard.press( 'Enter' );

// Check that we switched properly to edit mode.
await page.waitForXPath(
'//*[contains(@class, "components-snackbar")]/*[text()="Custom template created. You\'re in template mode now."]'
);

await disableTemplateWelcomeGuide();
};

describe( 'Post Editor Template mode', () => {
beforeAll( async () => {
await trashAllPosts( 'wp_template' );
Expand Down Expand Up @@ -47,23 +109,7 @@ describe( 'Post Editor Template mode', () => {
await saveDraft();
await page.reload();

// Switch to template mode.
await openDocumentSettingsSidebar();
await openSidebarPanelWithTitle( 'Template' );
const editTemplateXPath =
"//*[contains(@class, 'edit-post-template__actions')]//button[contains(text(), 'Edit')]";
const switchLink = await page.waitForXPath( editTemplateXPath );
await switchLink.click();

// Check that we switched properly to edit mode.
await page.waitForXPath(
'//*[contains(@class, "components-snackbar")]/*[text()="Editing template. Changes made here affect all posts and pages that use the template."]'
);
const title = await page.$eval(
'.edit-post-template-title',
( el ) => el.innerText
);
expect( title ).toContain( 'Editing template:' );
await switchToTemplateMode();

// Edit the template
await insertBlock( 'Paragraph' );
Expand Down Expand Up @@ -107,25 +153,7 @@ describe( 'Post Editor Template mode', () => {
await saveDraft();
await page.reload();

// Create a new custom template.
await openDocumentSettingsSidebar();
await openSidebarPanelWithTitle( 'Template' );
const newTemplateXPath =
"//*[contains(@class, 'edit-post-template__actions')]//button[contains(text(), 'New')]";
const newButton = await page.waitForXPath( newTemplateXPath );
await newButton.click();

// Fill the template title and submit.
const templateNameInputSelector =
'.edit-post-template__modal .components-text-control__input';
await page.click( templateNameInputSelector );
await page.keyboard.type( 'Blank Template' );
await page.keyboard.press( 'Enter' );

// Check that we switched properly to edit mode.
await page.waitForXPath(
'//*[contains(@class, "components-snackbar")]/*[text()="Custom template created. You\'re in template mode now."]'
);
await createNewTemplate( 'Blank Template' );

// Edit the template
await insertBlock( 'Paragraph' );
Expand Down
111 changes: 111 additions & 0 deletions packages/edit-post/src/components/welcome-guide/default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/**
* WordPress dependencies
*/
import { useDispatch } from '@wordpress/data';
import { ExternalLink, Guide } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { createInterpolateElement } from '@wordpress/element';

/**
* Internal dependencies
*/
/**
* Internal dependencies
*/
import {
CanvasImage,
EditorImage,
BlockLibraryImage,
DocumentationImage,
InserterIconImage,
} from './images';
import { store as editPostStore } from '../../store';

export default function WelcomeGuideDefault() {
const { toggleFeature } = useDispatch( editPostStore );

return (
<Guide
className="edit-post-welcome-guide"
contentLabel={ __( 'Welcome to the block editor' ) }
finishButtonText={ __( 'Get started' ) }
onFinish={ () => toggleFeature( 'welcomeGuide' ) }
pages={ [
{
image: <CanvasImage />,
content: (
<>
<h1 className="edit-post-welcome-guide__heading">
{ __( 'Welcome to the block editor' ) }
</h1>
<p className="edit-post-welcome-guide__text">
{ __(
'In the WordPress editor, each paragraph, image, or video is presented as a distinct “block” of content.'
) }
</p>
</>
),
},
{
image: <EditorImage />,
content: (
<>
<h1 className="edit-post-welcome-guide__heading">
{ __( 'Make each block your own' ) }
</h1>
<p className="edit-post-welcome-guide__text">
{ __(
'Each block comes with its own set of controls for changing things like color, width, and alignment. These will show and hide automatically when you have a block selected.'
) }
</p>
</>
),
},
{
image: <BlockLibraryImage />,
content: (
<>
<h1 className="edit-post-welcome-guide__heading">
{ __( 'Get to know the block library' ) }
</h1>
<p className="edit-post-welcome-guide__text">
{ createInterpolateElement(
__(
'All of the blocks available to you live in the block library. You’ll find it wherever you see the <InserterIconImage /> icon.'
),
{
InserterIconImage: (
<InserterIconImage className="edit-post-welcome-guide__inserter-icon" />
),
}
) }
</p>
</>
),
},
{
image: <DocumentationImage />,
content: (
<>
<h1 className="edit-post-welcome-guide__heading">
{ __( 'Learn how to use the block editor' ) }
</h1>
<p className="edit-post-welcome-guide__text">
{ __(
'New to the block editor? Want to learn more about using it? '
) }
<ExternalLink
href={ __(
'https://wordpress.org/support/article/wordpress-editor/'
) }
>
{ __( "Here's a detailed guide." ) }
</ExternalLink>
</p>
</>
),
},
] }
/>
);
}
115 changes: 15 additions & 100 deletions packages/edit-post/src/components/welcome-guide/index.js
Original file line number Diff line number Diff line change
@@ -1,117 +1,32 @@
/**
* WordPress dependencies
*/
import { useSelect, useDispatch } from '@wordpress/data';
import { ExternalLink, Guide } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { createInterpolateElement } from '@wordpress/element';
import { useSelect } from '@wordpress/data';

/**
* Internal dependencies
*/
import {
CanvasImage,
EditorImage,
BlockLibraryImage,
DocumentationImage,
InserterIconImage,
} from './images';
import WelcomeGuideDefault from './default';
import WelcomeGuideTemplate from './template';
import { store as editPostStore } from '../../store';

export default function WelcomeGuide() {
const isActive = useSelect(
( select ) => select( editPostStore ).isFeatureActive( 'welcomeGuide' ),
[]
);
const { isActive, isTemplateMode } = useSelect( ( select ) => {
const { isFeatureActive, isEditingTemplate } = select( editPostStore );
const _isTemplateMode = isEditingTemplate();
const feature = _isTemplateMode
? 'welcomeGuideTemplate'
: 'welcomeGuide';

const { toggleFeature } = useDispatch( editPostStore );
return {
isActive: isFeatureActive( feature ),
isTemplateMode: _isTemplateMode,
};
}, [] );

if ( ! isActive ) {
return null;
}

return (
<Guide
className="edit-post-welcome-guide"
contentLabel={ __( 'Welcome to the block editor' ) }
finishButtonText={ __( 'Get started' ) }
onFinish={ () => toggleFeature( 'welcomeGuide' ) }
pages={ [
{
image: <CanvasImage />,
content: (
<>
<h1 className="edit-post-welcome-guide__heading">
{ __( 'Welcome to the block editor' ) }
</h1>
<p className="edit-post-welcome-guide__text">
{ __(
'In the WordPress editor, each paragraph, image, or video is presented as a distinct “block” of content.'
) }
</p>
</>
),
},
{
image: <EditorImage />,
content: (
<>
<h1 className="edit-post-welcome-guide__heading">
{ __( 'Make each block your own' ) }
</h1>
<p className="edit-post-welcome-guide__text">
{ __(
'Each block comes with its own set of controls for changing things like color, width, and alignment. These will show and hide automatically when you have a block selected.'
) }
</p>
</>
),
},
{
image: <BlockLibraryImage />,
content: (
<>
<h1 className="edit-post-welcome-guide__heading">
{ __( 'Get to know the block library' ) }
</h1>
<p className="edit-post-welcome-guide__text">
{ createInterpolateElement(
__(
'All of the blocks available to you live in the block library. You’ll find it wherever you see the <InserterIconImage /> icon.'
),
{
InserterIconImage: (
<InserterIconImage className="edit-post-welcome-guide__inserter-icon" />
),
}
) }
</p>
</>
),
},
{
image: <DocumentationImage />,
content: (
<>
<h1 className="edit-post-welcome-guide__heading">
{ __( 'Learn how to use the block editor' ) }
</h1>
<p className="edit-post-welcome-guide__text">
{ __(
'New to the block editor? Want to learn more about using it? '
) }
<ExternalLink
href={ __(
'https://wordpress.org/support/article/wordpress-editor/'
) }
>
{ __( "Here's a detailed guide." ) }
</ExternalLink>
</p>
</>
),
},
] }
/>
);
return isTemplateMode ? <WelcomeGuideTemplate /> : <WelcomeGuideDefault />;
}
Loading

0 comments on commit da43d81

Please sign in to comment.