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

SPT: Set template title in large preview #39831

Merged
merged 15 commits into from
Mar 4, 2020
Merged
Show file tree
Hide file tree
Changes from 14 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
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ const BlockFramePreview = ( {
blocks,
settings,
setTimeout = noop,
title,
} ) => {
const frameContainerRef = useRef();
const renderedBlocksRef = useRef();
Expand Down Expand Up @@ -121,6 +122,24 @@ const BlockFramePreview = ( {
} );
}, [ viewportWidth ] );

// Set template title.
useEffect( () => {
const iframeBody = get( iframeRef, [ 'current', 'contentDocument', 'body' ] );
if ( ! iframeBody ) {
return;
}

const templateTitle = iframeBody.querySelector(
'.editor-post-title .editor-post-title__input'
);

if ( ! templateTitle ) {
return;
}

templateTitle.value = title;
}, [ recomputeBlockListKey ] );

// Populate iFrame styles.
useEffect( () => {
setTimeout( () => {
Expand Down Expand Up @@ -183,17 +202,15 @@ const BlockFramePreview = ( {
style={ style }
/>

<div ref={ renderedBlocksRef } className="block-editor" id="rendered-blocks">
<div ref={ renderedBlocksRef } className="block-editor">
<div className="edit-post-visual-editor">
<div className="editor-styles-wrapper">
<div className="editor-writing-flow">
{ blocks && blocks.length ? (
<CustomBlockPreview
blocks={ renderedBlocks }
settings={ settings }
recomputeBlockListKey={ recomputeBlockListKey }
/>
) : null }
<CustomBlockPreview
blocks={ renderedBlocks }
settings={ settings }
recomputeBlockListKey={ recomputeBlockListKey }
/>
</div>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,24 @@
*/
import { BlockEditorProvider, BlockList } from '@wordpress/block-editor';
import { Disabled } from '@wordpress/components';
import { PostTitle } from '@wordpress/editor';

// Exists as a pass through component to simplify automatted testing of
// components which need to `BlockEditorProvider`. Setting up JSDom to handle
// and mock the entire Block Editor isn't useful and is difficult for testing.
// Therefore this component exists to simplify mocking out the Block Editor
// when under test conditions.
export default function( { blocks, settings, recomputeBlockListKey } ) {
/* eslint-disable wpcalypso/jsx-classname-namespace */
Copy link
Contributor

Choose a reason for hiding this comment

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

If we've updated to block-iframe-preview__template-title do we need the eslint disable? It's better to keep these kinds of warnings where possible as (despite the "noise") they are the first line of defence against bugs.

return (
<BlockEditorProvider value={ blocks } settings={ settings }>
<Disabled key={ recomputeBlockListKey }>
<div className="block-iframe-preview__template-title">
<PostTitle />
</div>
<BlockList />
</Disabled>
</BlockEditorProvider>
);
/* eslint-enable wpcalypso/jsx-classname-namespace */
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { __ } from '@wordpress/i18n';
*/
import BlockIframePreview from './block-iframe-preview';

const TemplateSelectorPreview = ( { blocks = [], viewportWidth } ) => {
const TemplateSelectorPreview = ( { blocks = [], viewportWidth, title } ) => {
const noBlocks = ! blocks.length;
return (
/* eslint-disable wpcalypso/jsx-classname-namespace */
Expand All @@ -26,7 +26,7 @@ const TemplateSelectorPreview = ( { blocks = [], viewportWidth } ) => {
{ /* Always render preview iframe to ensure it's ready to populate with Blocks. */
/* Without this some browsers will experience a noticavle delay
/* before Blocks are populated into the iframe. */ }
<BlockIframePreview blocks={ blocks } viewportWidth={ viewportWidth } />
<BlockIframePreview blocks={ blocks } viewportWidth={ viewportWidth } title={ title } />
</div>
/* eslint-enable wpcalypso/jsx-classname-namespace */
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { compose } from '@wordpress/compose';
import { Button, Modal, Spinner, IconButton } from '@wordpress/components';
import { withDispatch, withSelect } from '@wordpress/data';
import { Component } from '@wordpress/element';
import { parse as parseBlocks, createBlock } from '@wordpress/blocks';
import { parse as parseBlocks } from '@wordpress/blocks';

/**
* Internal dependencies
Expand Down Expand Up @@ -43,21 +43,9 @@ class PageTemplateModal extends Component {
getBlocksByTemplateSlugs = memoize( templates =>
reduce(
templates,
( prev, { slug, content, title } ) => {
( prev, { slug, content } ) => {
prev[ slug ] = content
? [
/*
* Let's add the page title as a heading block.
* It will be remove when inserting the template
* blocks into the editor.
*/
createBlock( 'core/heading', {
content: title,
align: 'center',
level: 1,
} ),
...parseBlocks( replacePlaceholders( content, this.props.siteInformation ) ),
]
? parseBlocks( replacePlaceholders( content, this.props.siteInformation ) )
: [];
return prev;
},
Expand Down Expand Up @@ -175,9 +163,6 @@ class PageTemplateModal extends Component {
// Load content.
const blocks = this.getBlocksForSelection( slug );

// Let's pull the title before to insert blocks in the editor.
blocks.shift();

// Only overwrite the page title if the template is not one of the Homepage Layouts
const title = isHomepageTemplate ? null : this.getTitleByTemplateSlug( slug );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ body.admin-bar:not( .is-fullscreen-mode ) .page-template-modal-screen-overlay {
.sidebar-modal-opener__warning-options {
float: right;
margin-top: 20px;

.components-button {
margin-left: 12px;
}
Expand Down Expand Up @@ -439,14 +439,14 @@ body.admin-bar:not( .is-fullscreen-mode ) .page-template-modal-screen-overlay {
}

// Manual CSS Overrides. Remove after better solutions are in place.

// Removes empty paragraph placeholders, i.e. "Write Title..."
[data-type='core/paragraph'] [data-rich-text-placeholder] {
display: none;
}

/*
* Fixes jetpack .wp-block-jetpack-slideshow styles, as the /wp-content/plugins/jetpack/_inc/blocks/vendors~swiper.[hash].css
* Fixes jetpack .wp-block-jetpack-slideshow styles, as the /wp-content/plugins/jetpack/_inc/blocks/vendors~swiper.[hash].css
* file is loaded on block insert, not on page load. After the iframe is grabbing these styles, we can remove this code.
*/
.swiper-wrapper {
Expand Down Expand Up @@ -475,9 +475,8 @@ body.admin-bar:not( .is-fullscreen-mode ) .page-template-modal-screen-overlay {
}
}

// Fixes a rendering bug where the margins of the top/bottom blocks weren't contained in the iframe body
// For details see issue #39799
.block-editor-block-list__layout {
overflow: hidden;
// Tweak template title (post-title) component.
.block-iframe-preview__template-title {
padding-top: 20px;
}
}