-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Editor: Consider single unmodified default block as empty content
- Loading branch information
Showing
4 changed files
with
331 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { addFilter } from '@wordpress/hooks'; | ||
import { removep } from '@wordpress/autop'; | ||
import { | ||
isUnmodifiedDefaultBlock, | ||
getUnknownTypeHandlerName, | ||
} from '@wordpress/blocks'; | ||
|
||
/** | ||
* Returns true if the given array of blocks consists of a single block of the | ||
* unknown type, or false otherwise. | ||
* | ||
* @param {WPBlock[]} blocks Array of block objects. | ||
* | ||
* @return {boolean} Whether array consists of single unknown block. | ||
*/ | ||
export function isSingleUnknownBlock( blocks ) { | ||
return ( | ||
blocks.length === 1 && | ||
blocks[ 0 ].name === getUnknownTypeHandlerName() | ||
); | ||
} | ||
|
||
/** | ||
* Returns true if the given array of blocks consists of a single unmodified | ||
* default block, or false otherwise. | ||
* | ||
* @param {WPBlock[]} blocks Array of block objects. | ||
* | ||
* @return {boolean} Whether array consists of single unmodified default block. | ||
*/ | ||
export function isSingleUnmodifiedDefaultBlock( blocks ) { | ||
return ( | ||
blocks.length === 1 && | ||
isUnmodifiedDefaultBlock( blocks[ 0 ] ) | ||
); | ||
} | ||
|
||
/** | ||
* Given an array of blocks, returns either an empty array if the blocks | ||
* consist only of a single unmodified default block. Otherwise returns the | ||
* original array unmodified. | ||
* | ||
* @param {WPBlock[]} blocks Array of blocks. | ||
* | ||
* @return {WPBlock[]} Empty array, or original passed set of blocks. | ||
*/ | ||
export function omitSingleUnmodifiedDefaultBlock( blocks ) { | ||
if ( isSingleUnmodifiedDefaultBlock( blocks ) ) { | ||
blocks = []; | ||
} | ||
|
||
return blocks; | ||
} | ||
|
||
/** | ||
* Given an HTML string and array of blocks, returns a formatted HTML string | ||
* with paragraph tags removed via `removep` behavior if the array of blocks | ||
* consists only of a single block of the unknown type. | ||
* | ||
* @link https://www.npmjs.com/package/@wordpress/autop | ||
* | ||
* @param {string} content HTML content. | ||
* @param {WPBlock[]} blocks Array of blocks from which HTML content has been | ||
* generated. | ||
* | ||
* @return {string} HTML content, with `removep` filtering applied if the array | ||
* of blocks from which it was generated consists only of a | ||
* single block of the unknown type. | ||
*/ | ||
export function removepSingleUnknownBlock( content, blocks ) { | ||
if ( isSingleUnknownBlock( blocks ) ) { | ||
content = removep( content ); | ||
} | ||
|
||
return content; | ||
} | ||
|
||
addFilter( | ||
'editor.selectors.getBlockContent', | ||
'core/processContent/removepSingleUnknownBlock', | ||
removepSingleUnknownBlock | ||
); | ||
|
||
addFilter( | ||
'editor.selectors.getBlocksForSave', | ||
'core/processContent/omitSingleUnmodifiedDefaultBlock', | ||
omitSingleUnmodifiedDefaultBlock | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { | ||
getBlockTypes, | ||
unregisterBlockType, | ||
registerBlockType, | ||
createBlock, | ||
getDefaultBlockName, | ||
setDefaultBlockName, | ||
getUnknownTypeHandlerName, | ||
setUnknownTypeHandlerName, | ||
} from '@wordpress/blocks'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { | ||
isSingleUnknownBlock, | ||
isSingleUnmodifiedDefaultBlock, | ||
omitSingleUnmodifiedDefaultBlock, | ||
removepSingleUnknownBlock, | ||
} from '../process-content'; | ||
|
||
describe( 'processContent', () => { | ||
let originalDefaultBlockName, originalUnknownTypeHandlerName; | ||
|
||
beforeAll( () => { | ||
originalDefaultBlockName = getDefaultBlockName(); | ||
originalUnknownTypeHandlerName = getUnknownTypeHandlerName(); | ||
|
||
registerBlockType( 'core/default', { | ||
category: 'common', | ||
title: 'default', | ||
attributes: { | ||
modified: { | ||
type: 'boolean', | ||
default: false, | ||
}, | ||
}, | ||
save: () => null, | ||
} ); | ||
registerBlockType( 'core/unknown', { | ||
category: 'common', | ||
title: 'unknown', | ||
save: () => null, | ||
} ); | ||
setDefaultBlockName( 'core/default' ); | ||
setUnknownTypeHandlerName( 'core/unknown' ); | ||
} ); | ||
|
||
afterAll( () => { | ||
setDefaultBlockName( originalDefaultBlockName ); | ||
setUnknownTypeHandlerName( originalUnknownTypeHandlerName ); | ||
getBlockTypes().forEach( ( block ) => { | ||
unregisterBlockType( block.name ); | ||
} ); | ||
} ); | ||
|
||
describe( 'isSingleUnknownBlock', () => { | ||
it( 'returns false if multiple blocks passed', () => { | ||
const blocks = [ | ||
createBlock( getUnknownTypeHandlerName() ), | ||
createBlock( getUnknownTypeHandlerName() ), | ||
]; | ||
|
||
const result = isSingleUnknownBlock( blocks ); | ||
|
||
expect( result ).toBe( false ); | ||
} ); | ||
|
||
it( 'returns false if single block not of unknown type', () => { | ||
const blocks = [ | ||
createBlock( getDefaultBlockName() ), | ||
]; | ||
|
||
const result = isSingleUnknownBlock( blocks ); | ||
|
||
expect( result ).toBe( false ); | ||
} ); | ||
|
||
it( 'returns true if single block of unknown type', () => { | ||
const blocks = [ | ||
createBlock( getUnknownTypeHandlerName() ), | ||
]; | ||
|
||
const result = isSingleUnknownBlock( blocks ); | ||
|
||
expect( result ).toBe( true ); | ||
} ); | ||
} ); | ||
|
||
describe( 'isSingleUnmodifiedDefaultBlock', () => { | ||
it( 'returns false if multiple blocks passed', () => { | ||
const blocks = [ | ||
createBlock( getDefaultBlockName() ), | ||
createBlock( getDefaultBlockName() ), | ||
]; | ||
|
||
const result = isSingleUnmodifiedDefaultBlock( blocks ); | ||
|
||
expect( result ).toBe( false ); | ||
} ); | ||
|
||
it( 'returns false if single non-default block', () => { | ||
const blocks = [ | ||
createBlock( getUnknownTypeHandlerName() ), | ||
]; | ||
|
||
const result = isSingleUnmodifiedDefaultBlock( blocks ); | ||
|
||
expect( result ).toBe( false ); | ||
} ); | ||
|
||
it( 'returns false if single modified default block', () => { | ||
const blocks = [ | ||
createBlock( getDefaultBlockName(), { modified: true } ), | ||
]; | ||
|
||
const result = isSingleUnmodifiedDefaultBlock( blocks ); | ||
|
||
expect( result ).toBe( false ); | ||
} ); | ||
|
||
it( 'returns true if single unmodified default block', () => { | ||
const blocks = [ | ||
createBlock( getDefaultBlockName() ), | ||
]; | ||
|
||
const result = isSingleUnmodifiedDefaultBlock( blocks ); | ||
|
||
expect( result ).toBe( true ); | ||
} ); | ||
} ); | ||
|
||
describe( 'omitSingleUnmodifiedDefaultBlock', () => { | ||
it( 'returns original array of blocks if not single unmodified default block', () => { | ||
const blocks = [ | ||
createBlock( getUnknownTypeHandlerName() ), | ||
]; | ||
|
||
const result = omitSingleUnmodifiedDefaultBlock( blocks ); | ||
|
||
expect( result ).toBe( blocks ); | ||
} ); | ||
|
||
it( 'returns an empty array if single unmodified default block', () => { | ||
const blocks = [ | ||
createBlock( getDefaultBlockName() ), | ||
]; | ||
|
||
const result = omitSingleUnmodifiedDefaultBlock( blocks ); | ||
|
||
expect( result ).toEqual( [] ); | ||
} ); | ||
} ); | ||
|
||
describe( 'removepSingleUnknownBlock', () => { | ||
it( 'returns original content if not single block of unknown type', () => { | ||
const blocks = [ | ||
createBlock( getDefaultBlockName() ), | ||
]; | ||
|
||
const result = removepSingleUnknownBlock( '<p>foo</p>', blocks ); | ||
|
||
expect( result ).toBe( '<p>foo</p>' ); | ||
} ); | ||
|
||
it( 'returns removep-formatted content if single block of unknown type', () => { | ||
const blocks = [ | ||
createBlock( getUnknownTypeHandlerName() ), | ||
]; | ||
|
||
const result = removepSingleUnknownBlock( '<p>foo</p>', blocks ); | ||
|
||
expect( result ).toBe( 'foo' ); | ||
} ); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters