-
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
Block API: Adding a useOnce
property in the block settings to forbid using it multiple times
#1661
Conversation
One comment regarding such a feature was:
This could perhaps be achieved by using |
@swissspidy Yes, I saw that comment but before introducing this feature, I was wondering if it's really worth it. I can't seem to find any use-case for it. The code change is small but I find |
Agreed, I like the approach taken here. Should we add this useOnce property to the more block? |
That's the plan and where this idea originated from, so yeah. |
What about, say, a page template that defines that it allows 3 image blocks. I suppose that constraint would have to be applied at a different level? |
Impending use case for |
Should we merge this as is? The |
I think waiting for the more block is good (didn't realize it wasn't in), as that is the use case this is going to make work. I also wonder if this should be a block attribute though and not something solely set by the editor/inserter. |
The more block is pretty much good to go, so … |
Not sure I understand, it's already a block setting? |
From my reading of the code, when the more block is registered it will add a Just throwing out an idea. I am not opposed to this PR in anyway, as it should work well. I am also not 100% sure that we are adding the useOnce as a setting on the block, that is just how I read the code. |
You wont see |
Nope, I mean more that it doesn't really belong to the blockType as a property. If we continue to keep adding properties to block types to solve various use cases, we will end up bloating the block types. Not opposed to this approach at all, just proposing another way we could approach this problem, by having the block counts be purely handled in the inserter. |
editor/inserter/menu.js
Outdated
} | ||
|
||
// Return the name of the next block type. | ||
return list[ nextIndex ].name; | ||
const block = blockTypes[ nextIndex ]; |
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.
Can we call this variable blockType
to avoid confusion? Also the argument and name of isDisabledBlockType
.
b284d59
to
729391b
Compare
Back from vacations :). Rebased here, it should be ok. |
Where would said mapping exist? How do plugin authors opt-in to include their block type in the set? I'm sympathetic to the concern of over-bloating the block settings API, but this seems to me a characteristic of a block type nonetheless. |
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.
I think we ought to account for block disabling in showing the insertion point.
- It doesn't make sense that an insertion point be shown when hovering a disable block
- It becomes stuck if you hover over and then away from the disabled block type option (unless moving to a different, non-disabled block type)
Good catch @aduth. It should be better now. Also, I explicitly kept this block setting undocumented (to avoid over-bloating the block settings API), This may be useless for block authors. We could still document it later if needed. |
editor/inserter/menu.js
Outdated
return list[ nextIndex ].name; | ||
const blockType = blockTypes[ nextIndex ]; | ||
if ( this.isDisabledBlock( blockType ) ) { | ||
return this.findByIncrement( blockTypes, increment > 0 ? increment + 1 : increment - 1 ); |
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.
In this case I might have opted for a simple loop rather than recursion, because the findIndex
above is non-trivial.
diff --git a/editor/inserter/menu.js b/editor/inserter/menu.js
index 33680f4b..4b95bb9a 100644
--- a/editor/inserter/menu.js
+++ b/editor/inserter/menu.js
@@ -139,10 +139,21 @@ class InserterMenu extends Component {
findByIncrement( blockTypes, increment = 1 ) {
const currentIndex = findIndex( blockTypes, ( blockType ) => this.state.currentFocus === blockType.name );
- const nextIndex = currentIndex + increment;
const highestIndex = blockTypes.length - 1;
const lowestIndex = 0;
+ let nextIndex = currentIndex;
+ let blockType;
+ do {
+ nextIndex += increment;
+
+ // Return the name of the next block type.
+ blockType = blockTypes[ nextIndex ];
+ if ( blockType && ! this.isDisabledBlock( blockType ) ) {
+ return blockType.name;
+ }
+ } while ( blockType );
+
if ( nextIndex > highestIndex ) {
return 'search';
}
@@ -150,14 +161,6 @@ class InserterMenu extends Component {
if ( nextIndex < lowestIndex ) {
return 'search';
}
-
- // Return the name of the next block type.
- const blockType = blockTypes[ nextIndex ];
- if ( this.isDisabledBlock( blockType ) ) {
- return this.findByIncrement( blockTypes, increment > 0 ? increment + 1 : increment - 1 );
- }
-
- return blockType.name;
}
findNext( blockTypes ) {
The mapping would exist for where it related to. So in this instance it would exist on the inserter. We could have an inserter property called usableBlockTypes = {
'wp-core/more': {
allowedInstances: 1,
}
} Then use hooks.applyFilters() for extending.
Yup, I can agree with that. I was throwing the idea out to see if it caught on. I think that in different contexts blockTypes might have different numbers that can be used. For instance a page layout, that should describe what and how many blockTypes can be used. The blockType itself having a property in that instance wouldn't make sense, because it will need to be different for different situations, which would lead to messy conditionals. |
…ing using arrow keys
This makes the inserter aware of the existence of such blocks. I think avoiding hardcoding things like that is a good thing (we're even avoiding hard-coding the default block and the unknown block). What if the more block is removed, renamed or unregisted.
Good point, but this is probably something different (another API or a persisted settings somewhere). |
I'm going to merge this, of course, it's fine to revisit but this will help us move the |
It looks like this didn't update any of the documentation or include any tests to ensure that the API works as expected. It would be good to make sure this isn't a hidden feature. |
The lack of documentation here is intentional. We do not want this to be a public API yet (#1661 (comment)) but more for internal usage for now. |
The inserter menu is a good candidate for tests though. |
* update ref to master (Columns Block)
* Release v1.26.0 (#2153) * Add tests for Latest-Posts Bock * Have the Automation tests Scroll the Block window to help locate Block buttons on Android * Update gutenberg reference * Update Gutenberg ref * Update Gutenberg ref * New template for release PRs This PR will add a new template for release PRs to make it easier to check all the steps needed and to standardize the release PRs. This template can then be used by using this link: `https://github.com/wordpress-mobile/gutenberg-mobile/pull/new?template=release_pull_request.md` * Update template file. * Fix: remove extra padding for post title and first `Paragraph` block (#2095) * Fix: remove extra padding for post title and first `Paragraph` block * Update Gutenberg ref * Add a new androidReplacements function to comply with Android Typography lint rules * Make sure the file gutenberg.pot exists before generating android and ios strings. * Update Gutenberg ref * Update gutenberg ref * Update gutenberg ref * Update gutenberg reference * Gutenberg update * Update Gutenberg ref * Update Gutenberg ref * Update Gutenberg ref * Update Gutenberg ref * Fix: prevent ripple effect on slider cell in BottomSheet and disable it completely on iOS (#2023) * prevent ripple effect on slider cell in BottomSheet and disable it completely on iOS * Update Gutenberg ref * Update Gutenberg ref * Accept multiple headers through OnAuthHeaderRequestedListener (#2080) * Blog layout template (#2114) * Update Gutenberg ref * Update Gutenberg ref * Update gutenberg reference * Fix failing UI tests Try scrolling in the Inserter for all platforms * Disable the failing test on iOS Co-authored-by: Matthew Kevins <mmkevins@yahoo.com> Co-authored-by: Pinar Olguc <pinarolguc@gmail.com> * Update gutenberg reference * Feat: Column block (#1661) * update ref to master (Columns Block) * Update gutenberg reference * Fix Latests Posts Tests by expanding the scroll to button functionality * Fix lint issue * Fix typography breakage in master To a version where the typography panel is not added to block settings. * Update GB reference. * Correct slider step value (#2119) * Update ref: Correct slider step accordingly to the platform * Update gb ref Co-authored-by: Pinar Olguc <pinarolguc@gmail.com> * v1.26.0 * Add some missing release notes * Update Podfile.lock * Update gb ref * Update bundles Co-authored-by: Chip Snyder <chip.snyder3@gmail.com> Co-authored-by: Matthew Kevins <mmkevins@yahoo.com> Co-authored-by: Gerardo Pacheco <gerardo.pacheco@automattic.com> Co-authored-by: Sérgio Estêvão <sergioestevao@gmail.com> Co-authored-by: jbinda <jakub.binda@gmail.com> Co-authored-by: Chip <chip.snyder@automattic.com> Co-authored-by: Maxime Biais <maxime.biais@gmail.com> Co-authored-by: Tugdual de Kerviler <dekervit@gmail.com> Co-authored-by: Klymentiy Haykov <forsver@gmail.com> Co-authored-by: Matthew Kevins <mkevins@users.noreply.github.com> Co-authored-by: Luke Walczak <lukasz.walczak.pwr@gmail.com> * Update gb ref Co-authored-by: Chip Snyder <chip.snyder3@gmail.com> Co-authored-by: Matthew Kevins <mmkevins@yahoo.com> Co-authored-by: Gerardo Pacheco <gerardo.pacheco@automattic.com> Co-authored-by: Sérgio Estêvão <sergioestevao@gmail.com> Co-authored-by: jbinda <jakub.binda@gmail.com> Co-authored-by: Chip <chip.snyder@automattic.com> Co-authored-by: Maxime Biais <maxime.biais@gmail.com> Co-authored-by: Tugdual de Kerviler <dekervit@gmail.com> Co-authored-by: Klymentiy Haykov <forsver@gmail.com> Co-authored-by: Matthew Kevins <mkevins@users.noreply.github.com> Co-authored-by: Luke Walczak <lukasz.walczak.pwr@gmail.com>
* Add tests for Latest-Posts Bock * Have the Automation tests Scroll the Block window to help locate Block buttons on Android * Update gutenberg reference * Update Gutenberg ref * Update Gutenberg ref * New template for release PRs This PR will add a new template for release PRs to make it easier to check all the steps needed and to standardize the release PRs. This template can then be used by using this link: `https://github.com/wordpress-mobile/gutenberg-mobile/pull/new?template=release_pull_request.md` * Update template file. * Fix: remove extra padding for post title and first `Paragraph` block (#2095) * Fix: remove extra padding for post title and first `Paragraph` block * Update Gutenberg ref * Add a new androidReplacements function to comply with Android Typography lint rules * Make sure the file gutenberg.pot exists before generating android and ios strings. * Update Gutenberg ref * Update gutenberg ref * Update gutenberg ref * Update gutenberg reference * Gutenberg update * Update Gutenberg ref * Update Gutenberg ref * Update Gutenberg ref * Update Gutenberg ref * Fix: prevent ripple effect on slider cell in BottomSheet and disable it completely on iOS (#2023) * prevent ripple effect on slider cell in BottomSheet and disable it completely on iOS * Update Gutenberg ref * Update Gutenberg ref * Accept multiple headers through OnAuthHeaderRequestedListener (#2080) * Blog layout template (#2114) * Update Gutenberg ref * Update Gutenberg ref * Update gutenberg reference * Fix failing UI tests Try scrolling in the Inserter for all platforms * Disable the failing test on iOS Co-authored-by: Matthew Kevins <mmkevins@yahoo.com> Co-authored-by: Pinar Olguc <pinarolguc@gmail.com> * Update gutenberg reference * Feat: Column block (#1661) * update ref to master (Columns Block) * Update gutenberg reference * Fix Latests Posts Tests by expanding the scroll to button functionality * Fix lint issue * Fix typography breakage in master To a version where the typography panel is not added to block settings. * Update GB reference. * Correct slider step value (#2119) * Update ref: Correct slider step accordingly to the platform * Update gb ref Co-authored-by: Pinar Olguc <pinarolguc@gmail.com> * v1.26.0 * Add some missing release notes * Update Podfile.lock * Update gb ref * Update bundles Co-authored-by: Chip Snyder <chip.snyder3@gmail.com> Co-authored-by: Matthew Kevins <mmkevins@yahoo.com> Co-authored-by: Gerardo Pacheco <gerardo.pacheco@automattic.com> Co-authored-by: Sérgio Estêvão <sergioestevao@gmail.com> Co-authored-by: jbinda <jakub.binda@gmail.com> Co-authored-by: Chip <chip.snyder@automattic.com> Co-authored-by: Maxime Biais <maxime.biais@gmail.com> Co-authored-by: Tugdual de Kerviler <dekervit@gmail.com> Co-authored-by: Klymentiy Haykov <forsver@gmail.com> Co-authored-by: Matthew Kevins <mkevins@users.noreply.github.com> Co-authored-by: Luke Walczak <lukasz.walczak.pwr@gmail.com>
closes #1465
While doing this, I noticed some errors while navigating using arrows in the inserter, it should be fixed now. (The warning has been reported in #1606)
Testing instructions
useOnce: true
to one of the current block settings to try it.