-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Avoid confusion around blockTypes/allowedBlockTypes/enabledBlockTypes setting #6069
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ import { serialize, getBlockType, getBlockTypes } from '@wordpress/blocks'; | |
import { __ } from '@wordpress/i18n'; | ||
import { addQueryArgs } from '@wordpress/url'; | ||
import { moment } from '@wordpress/date'; | ||
import { deprecated } from '@wordpress/utils'; | ||
|
||
/*** | ||
* Module constants | ||
|
@@ -1208,17 +1209,17 @@ export function getNotices( state ) { | |
* Given a regular block type, constructs an item that appears in the inserter. | ||
* | ||
* @param {Object} state Global application state. | ||
* @param {string[]|boolean} enabledBlockTypes Enabled block types, or true/false to enable/disable all types. | ||
* @param {string[]|boolean} allowedBlockTypes Allowed block types, or true/false to enable/disable all types. | ||
* @param {Object} blockType Block type, likely from getBlockType(). | ||
* | ||
* @return {Editor.InserterItem} Item that appears in inserter. | ||
*/ | ||
function buildInserterItemFromBlockType( state, enabledBlockTypes, blockType ) { | ||
if ( ! enabledBlockTypes || ! blockType ) { | ||
function buildInserterItemFromBlockType( state, allowedBlockTypes, blockType ) { | ||
if ( ! allowedBlockTypes || ! blockType ) { | ||
return null; | ||
} | ||
|
||
const blockTypeIsDisabled = Array.isArray( enabledBlockTypes ) && ! includes( enabledBlockTypes, blockType.name ); | ||
const blockTypeIsDisabled = Array.isArray( allowedBlockTypes ) && ! includes( allowedBlockTypes, blockType.name ); | ||
if ( blockTypeIsDisabled ) { | ||
return null; | ||
} | ||
|
@@ -1243,17 +1244,17 @@ function buildInserterItemFromBlockType( state, enabledBlockTypes, blockType ) { | |
* Given a shared block, constructs an item that appears in the inserter. | ||
* | ||
* @param {Object} state Global application state. | ||
* @param {string[]|boolean} enabledBlockTypes Enabled block types, or true/false to enable/disable all types. | ||
* @param {string[]|boolean} allowedBlockTypes Allowed block types, or true/false to enable/disable all types. | ||
* @param {Object} sharedBlock Shared block, likely from getSharedBlock(). | ||
* | ||
* @return {Editor.InserterItem} Item that appears in inserter. | ||
*/ | ||
function buildInserterItemFromSharedBlock( state, enabledBlockTypes, sharedBlock ) { | ||
if ( ! enabledBlockTypes || ! sharedBlock ) { | ||
function buildInserterItemFromSharedBlock( state, allowedBlockTypes, sharedBlock ) { | ||
if ( ! allowedBlockTypes || ! sharedBlock ) { | ||
return null; | ||
} | ||
|
||
const blockTypeIsDisabled = Array.isArray( enabledBlockTypes ) && ! includes( enabledBlockTypes, 'core/block' ); | ||
const blockTypeIsDisabled = Array.isArray( allowedBlockTypes ) && ! includes( allowedBlockTypes, 'core/block' ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unrelated to this PR, but I wanted to double check if we need to open another issue. Shouldn't this logic verify whether There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In my opinion, disabling |
||
if ( blockTypeIsDisabled ) { | ||
return null; | ||
} | ||
|
@@ -1285,21 +1286,30 @@ function buildInserterItemFromSharedBlock( state, enabledBlockTypes, sharedBlock | |
* items (e.g. a regular block type) and dynamic items (e.g. a shared block). | ||
* | ||
* @param {Object} state Global application state. | ||
* @param {string[]|boolean} enabledBlockTypes Enabled block types, or true/false to enable/disable all types. | ||
* @param {string[]|boolean} allowedBlockTypes Allowed block types, or true/false to enable/disable all types. | ||
* | ||
* @return {Editor.InserterItem[]} Items that appear in inserter. | ||
*/ | ||
export function getInserterItems( state, enabledBlockTypes = true ) { | ||
if ( ! enabledBlockTypes ) { | ||
export function getInserterItems( state, allowedBlockTypes ) { | ||
if ( allowedBlockTypes === undefined ) { | ||
allowedBlockTypes = true; | ||
deprecated( 'getInserterItems with no allowedBlockTypes argument', { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Both depreciation messages should be also added to https://github.com/WordPress/gutenberg/blob/master/docs/deprecated.md for better visibility. |
||
version: '2.8', | ||
alternative: 'getInserterItems with an explcit allowedBlockTypes argument', | ||
plugin: 'Gutenberg', | ||
} ); | ||
} | ||
|
||
if ( ! allowedBlockTypes ) { | ||
return []; | ||
} | ||
|
||
const staticItems = getBlockTypes().map( blockType => | ||
buildInserterItemFromBlockType( state, enabledBlockTypes, blockType ) | ||
buildInserterItemFromBlockType( state, allowedBlockTypes, blockType ) | ||
); | ||
|
||
const dynamicItems = getSharedBlocks( state ).map( sharedBlock => | ||
buildInserterItemFromSharedBlock( state, enabledBlockTypes, sharedBlock ) | ||
buildInserterItemFromSharedBlock( state, allowedBlockTypes, sharedBlock ) | ||
); | ||
|
||
const items = [ ...staticItems, ...dynamicItems ]; | ||
|
@@ -1319,19 +1329,19 @@ function fillWithCommonBlocks( inserts ) { | |
return unionWith( items, commonInserts, areInsertsEqual ); | ||
} | ||
|
||
function getItemsFromInserts( state, inserts, enabledBlockTypes = true, maximum = MAX_RECENT_BLOCKS ) { | ||
if ( ! enabledBlockTypes ) { | ||
function getItemsFromInserts( state, inserts, allowedBlockTypes, maximum = MAX_RECENT_BLOCKS ) { | ||
if ( ! allowedBlockTypes ) { | ||
return []; | ||
} | ||
|
||
const items = fillWithCommonBlocks( inserts ).map( insert => { | ||
if ( insert.ref ) { | ||
const sharedBlock = getSharedBlock( state, insert.ref ); | ||
return buildInserterItemFromSharedBlock( state, enabledBlockTypes, sharedBlock ); | ||
return buildInserterItemFromSharedBlock( state, allowedBlockTypes, sharedBlock ); | ||
} | ||
|
||
const blockType = getBlockType( insert.name ); | ||
return buildInserterItemFromBlockType( state, enabledBlockTypes, blockType ); | ||
return buildInserterItemFromBlockType( state, allowedBlockTypes, blockType ); | ||
} ); | ||
|
||
return compact( items ).slice( 0, maximum ); | ||
|
@@ -1345,12 +1355,21 @@ function getItemsFromInserts( state, inserts, enabledBlockTypes = true, maximum | |
* https://en.wikipedia.org/wiki/Frecency | ||
* | ||
* @param {Object} state Global application state. | ||
* @param {string[]|boolean} enabledBlockTypes Enabled block types, or true/false to enable/disable all types. | ||
* @param {string[]|boolean} allowedBlockTypes Allowed block types, or true/false to enable/disable all types. | ||
* @param {number} maximum Number of items to return. | ||
* | ||
* @return {Editor.InserterItem[]} Items that appear in the 'Recent' tab. | ||
*/ | ||
export function getFrecentInserterItems( state, enabledBlockTypes = true, maximum = MAX_RECENT_BLOCKS ) { | ||
export function getFrecentInserterItems( state, allowedBlockTypes, maximum = MAX_RECENT_BLOCKS ) { | ||
if ( allowedBlockTypes === undefined ) { | ||
allowedBlockTypes = true; | ||
deprecated( 'getFrecentInserterItems with no allowedBlockTypes argument', { | ||
version: '2.8', | ||
alternative: 'getFrecentInserterItems with an explcit allowedBlockTypes argument', | ||
plugin: 'Gutenberg', | ||
} ); | ||
} | ||
|
||
const calculateFrecency = ( time, count ) => { | ||
if ( ! time ) { | ||
return count; | ||
|
@@ -1372,7 +1391,7 @@ export function getFrecentInserterItems( state, enabledBlockTypes = true, maximu | |
const sortedInserts = values( state.preferences.insertUsage ) | ||
.sort( ( a, b ) => calculateFrecency( b.time, b.count ) - calculateFrecency( a.time, a.count ) ) | ||
.map( ( { insert } ) => insert ); | ||
return getItemsFromInserts( state, sortedInserts, enabledBlockTypes, maximum ); | ||
return getItemsFromInserts( state, sortedInserts, allowedBlockTypes, maximum ); | ||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this line is updated. It can be simplified to:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very confusingly for us PHP folks,
_.isEmpty()
doesn't work the same way asempty()
: