Skip to content

Commit

Permalink
Merge branch 'master' into fix/2829-force-onChange-before-merge
Browse files Browse the repository at this point in the history
  • Loading branch information
tiny-james committed Nov 15, 2017
2 parents 255cfb7 + 4312160 commit d711a1a
Show file tree
Hide file tree
Showing 114 changed files with 2,498 additions and 1,603 deletions.
1 change: 0 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ build
coverage
vendor
node_modules
/assets/js
47 changes: 0 additions & 47 deletions assets/js/meta-box-resize.js

This file was deleted.

1 change: 0 additions & 1 deletion bin/build-plugin-zip.sh
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ mv gutenberg.tmp.php gutenberg.php
status "Creating archive..."
zip -r gutenberg.zip \
gutenberg.php \
assets/js/*.js \
lib/*.php \
blocks/library/*/*.php \
post-content.js \
Expand Down
5 changes: 0 additions & 5 deletions blocks/api/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@ export function createBlock( name, blockAttributes = {} ) {
return result;
}, {} );

// Keep the anchor if the block supports it
if ( blockType.supportAnchor && blockAttributes.anchor ) {
attributes.anchor = blockAttributes.anchor;
}

// Keep the className if the block supports it
if ( blockType.className !== false && blockAttributes.className ) {
attributes.className = blockAttributes.className;
Expand Down
1 change: 1 addition & 0 deletions blocks/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ export {
getDefaultBlockName,
getBlockType,
getBlockTypes,
hasBlockSupport,
} from './registration';
7 changes: 1 addition & 6 deletions blocks/api/parser.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { parse as hpqParse, attr } from 'hpq';
import { parse as hpqParse } from 'hpq';
import { mapValues, reduce, pickBy } from 'lodash';

/**
Expand Down Expand Up @@ -148,11 +148,6 @@ export function getBlockAttributes( blockType, innerHTML, attributes ) {
return result;
}, {} );

// If the block supports anchor, parse the id
if ( blockType.supportAnchor ) {
blockAttributes.anchor = hpqParse( innerHTML, attr( '*', 'id' ) );
}

// If the block supports a custom className parse it
if ( blockType.className !== false && attributes && attributes.className ) {
blockAttributes.className = attributes.className;
Expand Down
31 changes: 29 additions & 2 deletions blocks/api/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import { get, isFunction, some } from 'lodash';
*/
import { getCategories } from './categories';

/**
* Internal dependencies
*/
import { applyFilters } from '../hooks';

/**
* Block settings keyed by block name.
*
Expand Down Expand Up @@ -113,13 +118,15 @@ export function registerBlockType( name, settings ) {
if ( ! settings.icon ) {
settings.icon = 'block-default';
}
const block = blocks[ name ] = {
settings = {
name,
attributes: get( window._wpBlocksAttributes, name ),
...settings,
};

return block;
settings = applyFilters( 'registerBlockType', settings, name );

return blocks[ name ] = settings;
}

/**
Expand Down Expand Up @@ -196,3 +203,23 @@ export function getBlockType( name ) {
export function getBlockTypes() {
return Object.values( blocks );
}

/**
* Returns true if the block defines support for a feature, or false otherwise
*
* @param {(String|Object)} nameOrType Block name or type object
* @param {String} feature Feature to test
* @param {Boolean} defaultSupports Whether feature is supported by
* default if not explicitly defined
* @return {Boolean} Whether block supports feature
*/
export function hasBlockSupport( nameOrType, feature, defaultSupports ) {
const blockType = 'string' === typeof nameOrType ?
getBlockType( nameOrType ) :
nameOrType;

return !! get( blockType, [
'supports',
feature,
], defaultSupports );
}
7 changes: 2 additions & 5 deletions blocks/api/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { Component, createElement, renderToString, cloneElement, Children } from
* Internal dependencies
*/
import { getBlockType, getUnknownTypeHandlerName } from './registration';
import { applyFilters } from '../hooks';

/**
* Returns the block's default classname from its name
Expand Down Expand Up @@ -55,7 +56,7 @@ export function getSaveContent( blockType, attributes ) {
return element;
}

const extraProps = {};
const extraProps = applyFilters( 'getSaveContent.extraProps', {}, blockType, attributes );
if ( !! className ) {
const updatedClassName = classnames(
className,
Expand All @@ -65,10 +66,6 @@ export function getSaveContent( blockType, attributes ) {
extraProps.className = updatedClassName;
}

if ( blockType.supportAnchor && attributes.anchor ) {
extraProps.id = attributes.anchor;
}

return cloneElement( element, extraProps );
};
const contentWithClassname = Children.map( saveContent, addAdvancedAttributes );
Expand Down
24 changes: 0 additions & 24 deletions blocks/api/test/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,30 +57,6 @@ describe( 'block factory', () => {
expect( typeof block.uid ).toBe( 'string' );
} );

it( 'should keep the anchor if the block supports it', () => {
registerBlockType( 'core/test-block', {
attributes: {
align: {
type: 'string',
},
},
save: noop,
category: 'common',
title: 'test block',
supportAnchor: true,
} );
const block = createBlock( 'core/test-block', {
align: 'left',
anchor: 'chicken',
} );

expect( block.attributes ).toEqual( {
anchor: 'chicken',
align: 'left',
} );
expect( block.isValid ).toBe( true );
} );

it( 'should keep the className if the block supports it', () => {
registerBlockType( 'core/test-block', {
attributes: {},
Expand Down
20 changes: 0 additions & 20 deletions blocks/api/test/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,26 +157,6 @@ describe( 'block parser', () => {
} );
} );

it( 'should parse the anchor if the block supports it', () => {
const blockType = {
attributes: {
content: {
type: 'string',
source: text( 'div' ),
},
},
supportAnchor: true,
};

const innerHTML = '<div id="chicken">Ribs</div>';
const attrs = {};

expect( getBlockAttributes( blockType, innerHTML, attrs ) ).toEqual( {
content: 'Ribs',
anchor: 'chicken',
} );
} );

it( 'should parse the className if the block supports it', () => {
const blockType = {
attributes: {},
Expand Down
64 changes: 64 additions & 0 deletions blocks/api/test/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
getDefaultBlockName,
getBlockType,
getBlockTypes,
hasBlockSupport,
} from '../registration';

describe( 'blocks', () => {
Expand Down Expand Up @@ -272,4 +273,67 @@ describe( 'blocks', () => {
] );
} );
} );

describe( 'hasBlockSupport', () => {
it( 'should return false if block has no supports', () => {
registerBlockType( 'core/test-block', defaultBlockSettings );

expect( hasBlockSupport( 'core/test-block', 'foo' ) ).toBe( false );
} );

it( 'should return false if block does not define support by name', () => {
registerBlockType( 'core/test-block', {
...defaultBlockSettings,
supports: {
bar: true,
},
} );

expect( hasBlockSupport( 'core/test-block', 'foo' ) ).toBe( false );
} );

it( 'should return custom default supports if block does not define support by name', () => {
registerBlockType( 'core/test-block', {
...defaultBlockSettings,
supports: {
bar: true,
},
} );

expect( hasBlockSupport( 'core/test-block', 'foo', true ) ).toBe( true );
} );

it( 'should return true if block type supports', () => {
registerBlockType( 'core/test-block', {
...defaultBlockSettings,
supports: {
foo: true,
},
} );

expect( hasBlockSupport( 'core/test-block', 'foo' ) ).toBe( true );
} );

it( 'should return true if block author defines unsupported but truthy value', () => {
registerBlockType( 'core/test-block', {
...defaultBlockSettings,
supports: {
foo: 'hmmm',
},
} );

expect( hasBlockSupport( 'core/test-block', 'foo' ) ).toBe( true );
} );

it( 'should handle block settings object as argument to test', () => {
const settings = {
...defaultBlockSettings,
supports: {
foo: true,
},
};

expect( hasBlockSupport( settings, 'foo' ) ).toBe( true );
} );
} );
} );
14 changes: 0 additions & 14 deletions blocks/api/test/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,20 +121,6 @@ describe( 'block serializer', () => {

expect( saved ).toBe( '<div>Bananas</div>' );
} );

it( 'should add an id if the block supports anchors', () => {
const saved = getSaveContent(
{
save: ( { attributes } ) => createElement( 'div', null, attributes.fruit ),
supportAnchor: true,
name: 'myplugin/fruit',
className: false,
},
{ fruit: 'Bananas', anchor: 'my-fruit' }
);

expect( saved ).toBe( '<div id="my-fruit">Bananas</div>' );
} );
} );

describe( 'component save', () => {
Expand Down
26 changes: 26 additions & 0 deletions blocks/block-edit/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Internal dependencies
*/
import { getBlockType } from '../api';
import { applyFilters } from '../hooks';

function BlockEdit( props ) {
const { name, ...editProps } = props;
const blockType = getBlockType( name );

if ( ! blockType ) {
return null;
}

// `edit` and `save` are functions or components describing the markup
// with which a block is displayed. If `blockType` is valid, assign
// them preferencially as the render value for the block.
let Edit;
if ( blockType ) {
Edit = blockType.edit || blockType.save;
}

return applyFilters( 'BlockEdit', <Edit { ...editProps } />, props );
}

export default BlockEdit;
Loading

0 comments on commit d711a1a

Please sign in to comment.