Skip to content

Commit

Permalink
Text Editor: Adding new lines between blocks and inside block content (
Browse files Browse the repository at this point in the history
…#663)

* Text Editor: Adding new lines between blocks and inside block content
* Text Editor: Beautify the HTML output
  • Loading branch information
youknowriad authored May 5, 2017
1 parent 6f5deec commit 1cb599d
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 8 deletions.
7 changes: 4 additions & 3 deletions blocks/api/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,21 @@ export function createBlockWithFallback( blockType, rawContent, attributes ) {

// Try finding settings for known block type, else again fall back
let blockSettings = getBlockSettings( blockType );
const fallbackBlockType = getUnknownTypeHandler();
if ( ! blockSettings ) {
blockType = getUnknownTypeHandler();
blockType = fallbackBlockType;
blockSettings = getBlockSettings( blockType );
}

// Include in set only if settings were determined
// TODO do we ever expect there to not be an unknown type handler?
if ( blockSettings ) {
if ( blockSettings && ( rawContent.trim() || blockType !== fallbackBlockType ) ) {
// TODO allow blocks to opt-in to receiving a tree instead of a string.
// Gradually convert all blocks to this new format, then remove the
// string serialization.
const block = createBlock(
blockType,
getBlockAttributes( blockSettings, rawContent, attributes )
getBlockAttributes( blockSettings, rawContent.trim(), attributes )
);
return block;
}
Expand Down
9 changes: 7 additions & 2 deletions blocks/api/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* External dependencies
*/
import { difference } from 'lodash';
import { html as beautifyHtml } from 'js-beautify';

/**
* Internal dependencies
Expand Down Expand Up @@ -73,6 +74,10 @@ export default function serialize( blocks ) {
const blockType = block.blockType;
const settings = getBlockSettings( blockType );
const saveContent = getSaveContent( settings.save, block.attributes );
const beautifyOptions = {
indent_inner_html: true,
wrap_line_length: 0
};

return memo + (
'<!-- wp:' +
Expand All @@ -83,10 +88,10 @@ export default function serialize( blocks ) {
parseBlockAttributes( saveContent, settings )
) +
'-->' +
saveContent +
( saveContent ? '\n' + beautifyHtml( saveContent, beautifyOptions ) + '\n' : '' ) +
'<!-- /wp:' +
blockType +
' -->'
);
) + '\n\n';
}, '' );
}
4 changes: 2 additions & 2 deletions blocks/api/test/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ describe( 'block parser', () => {
} );

const parsed = parse(
'<!-- wp:core/test-block -->Ribs<!-- /wp:core/test-block -->' +
'<!-- wp:core/test-block -->\nRibs\n<!-- /wp:core/test-block -->' +
'<p>Broccoli</p>' +
'<!-- wp:core/unknown-block -->Ribs<!-- /wp:core/unknown-block -->'
);
Expand Down Expand Up @@ -232,7 +232,7 @@ describe( 'block parser', () => {
const parsed = parse(
'<p>Cauliflower</p>' +
'<!-- wp:core/test-block -->Ribs<!-- /wp:core/test-block -->' +
'<p>Broccoli</p>' +
'\n<p>Broccoli</p>\n' +
'<!-- wp:core/test-block -->Ribs<!-- /wp:core/test-block -->' +
'<p>Romanesco</p>'
);
Expand Down
2 changes: 1 addition & 1 deletion blocks/api/test/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ describe( 'block serializer', () => {
}
}
];
const expectedPostContent = '<!-- wp:core/test-block align:left --><p>Ribs & Chicken</p><!-- /wp:core/test-block -->';
const expectedPostContent = '<!-- wp:core/test-block align:left -->\n<p>Ribs & Chicken</p>\n<!-- /wp:core/test-block -->\n\n';

expect( serialize( blockList ) ).to.eql( expectedPostContent );
} );
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"element-closest": "^2.0.2",
"hpq": "^1.2.0",
"jed": "^1.1.1",
"js-beautify": "^1.6.12",
"lodash": "^4.17.4",
"react": "^15.5.4",
"react-autosize-textarea": "^0.4.2",
Expand Down

0 comments on commit 1cb599d

Please sign in to comment.