Skip to content
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

Respect default values of a property of type object during the creation of a Block #26162

Closed
wants to merge 2 commits into from

Conversation

ntsekouras
Copy link
Contributor

@ntsekouras ntsekouras commented Oct 15, 2020

Description

This PR adds some missing functionality in createBlock (Blocks API) to respect the default values of a property of type object. Previously it would overwrite the whole object, ignoring the rest default values. For example if we had in block.json an attribute like this:

"query": {
			"type": "object",
			"default": {
				"perPage": 3,
				"pages": 1
			}
		}  

and created a block with a single object's property set like this: createBlock( 'test/block', { query: { perPage:10 } }), it would result in keeping only the passed attributes, which in this case is perPage. The pages attribute would not be defined.

Another not related thing this PR changes, is some whitespaces differences that occurred during the regeneration of fixtures. The regeneration tool uses spaces and there were some handwritten with tabs.

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.
  • I've updated all React Native files affected by any refactorings/renamings in this PR.

@ntsekouras ntsekouras added the [Feature] Block API API that allows to express the block paradigm. label Oct 15, 2020
@ntsekouras ntsekouras requested a review from ellatrix as a code owner October 15, 2020 14:38
@ntsekouras ntsekouras self-assigned this Oct 15, 2020
@github-actions
Copy link

github-actions bot commented Oct 15, 2020

Size Change: +25 B (0%)

Total Size: 1.19 MB

Filename Size Change
build/blocks/index.js 47.6 kB +25 B (0%)
ℹ️ View Unchanged
Filename Size Change
build/a11y/index.js 1.14 kB 0 B
build/annotations/index.js 3.54 kB 0 B
build/api-fetch/index.js 3.35 kB 0 B
build/autop/index.js 2.72 kB 0 B
build/blob/index.js 668 B 0 B
build/block-directory/index.js 8.6 kB 0 B
build/block-directory/style-rtl.css 943 B 0 B
build/block-directory/style.css 942 B 0 B
build/block-editor/index.js 130 kB 0 B
build/block-editor/style-rtl.css 10.9 kB 0 B
build/block-editor/style.css 10.9 kB 0 B
build/block-library/editor-rtl.css 8.65 kB 0 B
build/block-library/editor.css 8.65 kB 0 B
build/block-library/index.js 142 kB 0 B
build/block-library/style-rtl.css 7.71 kB 0 B
build/block-library/style.css 7.71 kB 0 B
build/block-library/theme-rtl.css 741 B 0 B
build/block-library/theme.css 741 B 0 B
build/block-serialization-default-parser/index.js 1.78 kB 0 B
build/block-serialization-spec-parser/index.js 3.1 kB 0 B
build/components/index.js 169 kB 0 B
build/components/style-rtl.css 15.4 kB 0 B
build/components/style.css 15.4 kB 0 B
build/compose/index.js 9.63 kB 0 B
build/core-data/index.js 12.1 kB 0 B
build/data-controls/index.js 684 B 0 B
build/data/index.js 8.63 kB 0 B
build/date/index.js 31.9 kB 0 B
build/deprecated/index.js 772 B 0 B
build/dom-ready/index.js 569 B 0 B
build/dom/index.js 4.43 kB 0 B
build/edit-navigation/index.js 10.6 kB 0 B
build/edit-navigation/style-rtl.css 868 B 0 B
build/edit-navigation/style.css 871 B 0 B
build/edit-post/index.js 306 kB 0 B
build/edit-post/style-rtl.css 6.33 kB 0 B
build/edit-post/style.css 6.31 kB 0 B
build/edit-site/index.js 21.2 kB 0 B
build/edit-site/style-rtl.css 3.81 kB 0 B
build/edit-site/style.css 3.82 kB 0 B
build/edit-widgets/index.js 21.6 kB 0 B
build/edit-widgets/style-rtl.css 3.09 kB 0 B
build/edit-widgets/style.css 3.09 kB 0 B
build/editor/editor-styles-rtl.css 480 B 0 B
build/editor/editor-styles.css 482 B 0 B
build/editor/index.js 42.4 kB 0 B
build/editor/style-rtl.css 3.85 kB 0 B
build/editor/style.css 3.84 kB 0 B
build/element/index.js 4.45 kB 0 B
build/escape-html/index.js 733 B 0 B
build/format-library/index.js 7.49 kB 0 B
build/format-library/style-rtl.css 547 B 0 B
build/format-library/style.css 548 B 0 B
build/hooks/index.js 1.74 kB 0 B
build/html-entities/index.js 622 B 0 B
build/i18n/index.js 3.54 kB 0 B
build/is-shallow-equal/index.js 709 B 0 B
build/keyboard-shortcuts/index.js 2.39 kB 0 B
build/keycodes/index.js 1.85 kB 0 B
build/list-reusable-blocks/index.js 3.02 kB 0 B
build/list-reusable-blocks/style-rtl.css 476 B 0 B
build/list-reusable-blocks/style.css 476 B 0 B
build/media-utils/index.js 5.12 kB 0 B
build/notices/index.js 1.69 kB 0 B
build/nux/index.js 3.27 kB 0 B
build/nux/style-rtl.css 671 B 0 B
build/nux/style.css 668 B 0 B
build/plugins/index.js 2.44 kB 0 B
build/primitives/index.js 1.34 kB 0 B
build/priority-queue/index.js 789 B 0 B
build/redux-routine/index.js 2.85 kB 0 B
build/reusable-blocks/index.js 3.04 kB 0 B
build/rich-text/index.js 13 kB 0 B
build/server-side-render/index.js 2.6 kB 0 B
build/shortcode/index.js 1.7 kB 0 B
build/token-list/index.js 1.24 kB 0 B
build/url/index.js 4.07 kB 0 B
build/viewport/index.js 1.75 kB 0 B
build/warning/index.js 1.13 kB 0 B
build/wordcount/index.js 1.17 kB 0 B

compressed-size-action

@gziolo
Copy link
Member

gziolo commented Oct 19, 2020

@youknowriad, should we include it as a Dev Note in the upcoming WP release?

Copy link
Member

@gziolo gziolo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code itself looks good 👍

I'm wondering if it can have any negative impact on existing plugins that might not want to have those default values of the object to be automatically injected. What if they would like to reset those values, do they need to explicitly set them to undefined?

@ntsekouras
Copy link
Contributor Author

Thanks for reviewing @gziolo!

I'm wondering if it can have any negative impact on existing plugins that might not want to have those default values of the object to be automatically injected. What if they would like to reset those values, do they need to explicitly set them to undefined?

I think that if someone wouldn't want all the default values to be set, they shouldn't set them on their block.json as default. This is just missing functionality from the expected behavior.

@youknowriad
Copy link
Contributor

I'm not sure this is the right behavior personally. because the default value is actually set, it's just another value, this PR merges nested properties for object values which might not make sense for all kind of objects.

@ntsekouras
Copy link
Contributor Author

I'm not sure this is the right behavior personally. because the default value is actually set, it's just another value

I can't see how this is not the right behavior personally. So you think it's better to create a block with all the defaults from an object prop copied over from block.json and just change any prop we want to change through creation?

An example usage is on Query block and its block variations to be. We want most of the defaults, but we want to change just one or two..

might not make sense for all kind of objects.

That's true and even I thought on making this recursive (which might be an option), this will cover the simple objects for now to work properly and we might think of other ways to augment the block.json/registration mechanism. If we want to support deeply nested objects as properties, it could be a better idea to support type of another embed type we have created.

@youknowriad
Copy link
Contributor

Let say, the object argument is a "product" object. and the default value is:

{ id: 1, title: "my product" }

while the template provides something like

{ title: "unsaved product" }

with this PR, the editor will get

{ id: 1, title: "unsaved product" }

which is not really what I expect since it's "stealing" the id of another product. (the id is just an example, it could be any property really)

@ntsekouras
Copy link
Contributor Author

Okay I understand your example, but I still think this kind of thing would be wrong. You should definitely not use an id like prop as a default to blocks.json. If we do something like this:

{ title: "unsaved product" }

it seems to me there is a way of generating the id and shouldn't be a default in the first place.

Having said that I cannot think of some other cases that make sense, but there could be possibly... So I don't know :)

The alternative of keeping this behavior (current handling), is to copy from block.json the defaults. That means to me that looses kind of its purpose here and with a change in a default value in block.json would need to change that value in every other place.

@youknowriad
Copy link
Contributor

Having said that I cannot think of some other cases that make sense, but there could be possibly... So I don't know :)

As I said the id is just an example, here's another one:

default value

{ title: "untitle product", description: "This is a description placeholder, and the description property is optional" }

template value:

{ title: "my title" }

you end up with the dummy description for your product.

Basically this behavior is wrong for anything that is "optional" and I do believe there's a lot of use-cases like that. In fact objects can also have different shapes for example, imagine an object describing a JSON schema for a scalar value, its default value can be

{ type: "float", min: "10", max: "20" }

and a template can provide something completely different

{ type: "string", enum: [ "a", "b" ] }

In this example, the "type" can change completely the format of the object which means merging the two values doesn't make sense.

I think your proposal is sane if it was implemented like that from the start and people knew about its behavior but the truth is we don't know how users are using this in the wild, and what kind of objects they're using which means merging might not always be the best way to go.

@ntsekouras
Copy link
Contributor Author

we don't know how users are using this in the wild, and what kind of objects they're using which means merging might not always be the best way to go.

Thanks for this discussion @youknowriad, @gziolo - I'll close this PR to not cause any unwanted side effects from current implementations.

@ntsekouras ntsekouras closed this Oct 19, 2020
@gziolo gziolo deleted the update/support-object-props-defaults branch October 19, 2020 08:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Block API API that allows to express the block paradigm.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants