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 "More" block. #1440

Merged
merged 24 commits into from
Jul 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
f46ae7a
Add "More" block.
mtias Jun 26, 2017
e614f31
Polish the styles and description a bit.
jasmussen Jun 27, 2017
a085101
Add test files
ellatrix Jun 29, 2017
74d4d28
Use BlockDescription component
swissspidy Jul 5, 2017
bc25566
Rename test files
swissspidy Jul 5, 2017
a067bd2
Update block name
swissspidy Jul 5, 2017
b9f311b
Update grammar
swissspidy Jul 5, 2017
2c6b53b
Update tests
swissspidy Jul 5, 2017
d3ecbbb
Fix block description
swissspidy Jul 5, 2017
d7f3ef2
Use default class name
swissspidy Jul 5, 2017
567735e
Fix vertical alignment
swissspidy Jul 5, 2017
28ce6cb
Fix serializer
swissspidy Jul 5, 2017
a8b9062
Add ToggleControl for noTeaser option
swissspidy Jul 5, 2017
c642df3
Add trailing comma
swissspidy Jul 5, 2017
2c09b4d
Fix tests
swissspidy Jul 5, 2017
ca1d612
More Block: Avoid unnecessary space when no custom text is provided
youknowriad Jul 26, 2017
6bb1343
More Block: It should only be used once
youknowriad Jul 26, 2017
cd1d11c
More Block: Fix inspector controls (description position and drop the…
youknowriad Jul 26, 2017
260857f
More Block: Fix the text style (center vertically)
youknowriad Jul 26, 2017
26e3c38
More Block: Drop useless prop
youknowriad Jul 26, 2017
25cf370
More Block: Using Input instead of Editable
youknowriad Jul 27, 2017
0039599
Test: Checked the parsed block by checking the JSON file
youknowriad Jul 27, 2017
f9d9aed
More Block: Adding a test with no-teaser and a custom test
youknowriad Jul 27, 2017
e7f04b0
More Block: Adding a full featured unit test
youknowriad Jul 27, 2017
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
6 changes: 3 additions & 3 deletions blocks/api/post.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ WP_Tag_More
= "<!--" WS* "more" customText:(WS+ text:$((!(WS* "-->") .)+) { /** <?php return $text; ?> **/ return text })? WS* "-->" noTeaser:(WS* "<!--noteaser-->")?
{ /** <?php
return array(
'blockName' => 'wp:core/more',
'blockName' => 'core/more',
'attrs' => array(
'customText' => $customText,
'noTeaser' => (bool) $noTeaser
Expand All @@ -40,7 +40,7 @@ WP_Tag_More
);
?> **/
return {
blockName: 'wp:core/more',
blockName: 'core/more',
attrs: {
customText: customText,
noTeaser: !! noTeaser
Expand Down Expand Up @@ -95,7 +95,7 @@ WP_Block_Balanced
}

WP_Block_Html
= ts:(!WP_Block_Balanced !WP_Block_Void c:Any {
= ts:(!WP_Block_Balanced !WP_Block_Void !WP_Tag_More c:Any {
/** <?php return $c; ?> **/
return c;
})+
Expand Down
4 changes: 2 additions & 2 deletions blocks/api/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ export function serializeBlock( block ) {

const saveAttributes = getCommentAttributes( block.attributes, parseBlockAttributes( saveContent, blockType.attributes ) );

if ( 'wp:core/more' === blockName ) {
return `<!-- more ${ saveAttributes.customText ? `${ saveAttributes.customText } ` : '' }-->${ saveAttributes.noTeaser ? '\n<!--noteaser-->' : '' }`;
if ( 'core/more' === blockName ) {
return `<!--more${ saveAttributes.text ? ` ${ saveAttributes.text }` : '' }-->${ saveAttributes.noTeaser ? '\n<!--noteaser-->' : '' }`;
}

const serializedAttributes = ! isEmpty( saveAttributes )
Expand Down
1 change: 1 addition & 0 deletions blocks/library/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import './quote';
import './embed';
import './list';
import './separator';
import './more';
import './button';
import './pullquote';
import './table';
Expand Down
64 changes: 64 additions & 0 deletions blocks/library/more/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* WordPress dependencies
*/
import { __ } from 'i18n';

/**
* Internal dependencies
*/
import './style.scss';
import { registerBlockType } from '../../api';
import InspectorControls from '../../inspector-controls';
import BlockDescription from '../../block-description';
import ToggleControl from '../../inspector-controls/toggle-control';

registerBlockType( 'core/more', {
title: __( 'More' ),

icon: 'editor-insertmore',

category: 'layout',

useOnce: true,

className: false,

edit( { attributes, setAttributes, focus, setFocus } ) {
const { text, noTeaser } = attributes;

const toggleNoTeaser = () => setAttributes( { noTeaser: ! noTeaser } );
const defaultText = __( 'Read more' );
const value = text !== undefined ? text : defaultText;
const inputLength = value.length ? value.length + 1 : 1;

return [
focus && (
<InspectorControls key="inspector">
<BlockDescription>
<p>{ __( '"More" allows you to break your post into a part shown on index pages, and the subsequent after clicking a "Read More" link.' ) }</p>
</BlockDescription>
<ToggleControl
label={ __( 'Hide the teaser before the "More" tag' ) }
checked={ !! noTeaser }
onChange={ toggleNoTeaser }
/>
</InspectorControls>
),
<div key="more-tag" className="wp-block-more">
Copy link
Member

Choose a reason for hiding this comment

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

Should this be props.className instead of "wp-block-more" ?

Copy link
Contributor

Choose a reason for hiding this comment

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

no, when we set the className to false, there's no className prop

Copy link
Member

Choose a reason for hiding this comment

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

Why do we set the className to false if we clearly want one? Is this related to save auto-applying the class name?

Copy link
Contributor

Choose a reason for hiding this comment

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

it's more related to the className input showing in the inspector if the className is not set to false

<input
type="text"
value={ value }
size={ inputLength }
onChange={ ( event ) => setAttributes( { text: event.target.value } ) }
onFocus={ setFocus }
/>
</div>,
];
},

save( { attributes } ) {
const { text } = attributes;

return text;
},
} );
34 changes: 34 additions & 0 deletions blocks/library/more/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
.editor-visual-editor__block[data-type="core/more"] {
max-width: 100%;
text-align: center;
}

.editor-visual-editor__block .wp-block-more {
input {
font-size: 12px;
text-transform: uppercase;
font-weight: 600;
font-family: $default-font;
color: $dark-gray-300;
padding-left: 8px;
padding-right: 8px;
background: $white;
border: none;
box-shadow: none;
white-space: nowrap;

&:focus {
box-shadow: none;
}
}

&:before {
content: '';
position: absolute;
top: calc( 50% );
left: $block-mover-padding-visible + $block-padding;
right: $block-mover-padding-visible + $block-padding;
border-top: 3px dashed $light-gray-700;
z-index: z-index( '.editor-visual-editor__block .wp-block-more:before' );
}
}
1 change: 1 addition & 0 deletions blocks/test/fixtures/core__more.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!--more-->
11 changes: 11 additions & 0 deletions blocks/test/fixtures/core__more.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[
{
"uid": "_uid_0",
"name": "core/more",
"isValid": true,
"attributes": {
"customText": null,
"noTeaser": false
}
}
]
14 changes: 14 additions & 0 deletions blocks/test/fixtures/core__more.parsed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"blockName": "core/more",
"attrs": {
"customText": null,
"noTeaser": false
},
"rawContent": ""
},
{
"attrs": {},
"rawContent": "\n"
}
]
1 change: 1 addition & 0 deletions blocks/test/fixtures/core__more.serialized.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!--more-->
2 changes: 2 additions & 0 deletions blocks/test/fixtures/core__more__custom-text-teaser.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<!--more Continue Reading-->
<!--noteaser-->
11 changes: 11 additions & 0 deletions blocks/test/fixtures/core__more__custom-text-teaser.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[
{
"uid": "_uid_0",
"name": "core/more",
"isValid": true,
"attributes": {
"customText": "Continue Reading",
"noTeaser": true
}
}
]
14 changes: 14 additions & 0 deletions blocks/test/fixtures/core__more__custom-text-teaser.parsed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"blockName": "core/more",
"attrs": {
"customText": "Continue Reading",
"noTeaser": true
},
"rawContent": ""
},
{
"attrs": {},
"rawContent": "\n"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<!--more-->
<!--noteaser-->
7 changes: 3 additions & 4 deletions blocks/test/full-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,11 @@ describe( 'full post content fixture', () => {
) )
.map( basename => {
const filename = basename + '.html';
const parsedBlockFilename = basename + '.json';
return {
filename,
contents: readFixtureFile( filename ),
parsed: JSON.parse( readFixtureFile( parsedBlockFilename ) )[ 0 ],
};
} )
.filter( fixture => fixture.contents !== null );
Expand All @@ -221,10 +223,7 @@ describe( 'full post content fixture', () => {
}

foundFixtures.forEach( fixture => {
const delimiter = new RegExp(
'<!--\\s*wp:' + name + '(\\s+|\\s*-->)'
);
if ( ! delimiter.test( fixture.contents ) ) {
if ( name !== fixture.parsed.name ) {
errors.push( format(
'Expected fixture file \'%s\' to test the \'%s\' block.',
fixture.filename,
Expand Down
1 change: 1 addition & 0 deletions editor/assets/stylesheets/_z-index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
$z-layers: (
'.editor-block-switcher__arrow': 1,
'.editor-visual-editor__block:before': -1,
'.editor-visual-editor__block .wp-block-more:before': -1,
'.editor-visual-editor__block {core/image aligned left or right}': 10,
'.editor-visual-editor__block-controls': 1,
'.editor-visual-editor__invalid-block-warning': 1,
Expand Down
48 changes: 39 additions & 9 deletions lib/parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ private function peg_buildException($message, $expected, $pos) {
private function peg_f0($text) { return $text; }
private function peg_f1($customText, $noTeaser) {
return array(
'blockName' => 'wp:core/more',
'blockName' => 'core/more',
'attrs' => array(
'customText' => $customText,
'noTeaser' => (bool) $noTeaser
Expand Down Expand Up @@ -861,11 +861,26 @@ private function peg_parseWP_Block_Html() {
$s4 = $this->peg_FAILED;
}
if ($s4 !== $this->peg_FAILED) {
$s5 = $this->peg_parseAny();
$s5 = $this->peg_currPos;
$this->peg_silentFails++;
$s6 = $this->peg_parseWP_Tag_More();
$this->peg_silentFails--;
if ($s6 === $this->peg_FAILED) {
$s5 = null;
} else {
$this->peg_currPos = $s5;
$s5 = $this->peg_FAILED;
}
if ($s5 !== $this->peg_FAILED) {
$this->peg_reportedPos = $s2;
$s3 = $this->peg_f7($s5);
$s2 = $s3;
$s6 = $this->peg_parseAny();
if ($s6 !== $this->peg_FAILED) {
$this->peg_reportedPos = $s2;
$s3 = $this->peg_f7($s6);
$s2 = $s3;
} else {
$this->peg_currPos = $s2;
$s2 = $this->peg_FAILED;
}
} else {
$this->peg_currPos = $s2;
$s2 = $this->peg_FAILED;
Expand Down Expand Up @@ -904,11 +919,26 @@ private function peg_parseWP_Block_Html() {
$s4 = $this->peg_FAILED;
}
if ($s4 !== $this->peg_FAILED) {
$s5 = $this->peg_parseAny();
$s5 = $this->peg_currPos;
$this->peg_silentFails++;
$s6 = $this->peg_parseWP_Tag_More();
$this->peg_silentFails--;
if ($s6 === $this->peg_FAILED) {
$s5 = null;
} else {
$this->peg_currPos = $s5;
$s5 = $this->peg_FAILED;
}
if ($s5 !== $this->peg_FAILED) {
$this->peg_reportedPos = $s2;
$s3 = $this->peg_f7($s5);
$s2 = $s3;
$s6 = $this->peg_parseAny();
if ($s6 !== $this->peg_FAILED) {
$this->peg_reportedPos = $s2;
$s3 = $this->peg_f7($s6);
$s2 = $s3;
} else {
$this->peg_currPos = $s2;
$s2 = $this->peg_FAILED;
}
} else {
$this->peg_currPos = $s2;
$s2 = $this->peg_FAILED;
Expand Down
2 changes: 0 additions & 2 deletions post-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ window._wpGutenbergPost.content = {
'<p style="text-align:right">... like this one, which is right aligned.</p>',
'<!-- /wp:core/text -->',

'<!-- wp:core/more /-->',

'<!-- wp:core/text -->',
'<p>Headings are separate blocks as well, which helps with the outline and organization of your content.</p>',
'<!-- /wp:core/text -->',
Expand Down