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

Gallerys ids are saved as numbers #19163

Merged
merged 12 commits into from
Jan 7, 2020
Merged

Conversation

SergioEstevao
Copy link
Contributor

Description

Changed the JSON schema for the gallery blocks to ensure that the ids array is always made with numbers.

How has this been tested?

  • Create a gallery block with images from WP media library
  • Switch to code and see that the ids are ints
  • Save & Close post
  • Reopen post
  • Select a tile and move left or right
  • Switch to code view and see that the id are still numbers
  • Edit the HTML and change the ids to strings
  • Switch to Visual mode and see that all is ok
  • Switch to HTML mode and see if the block was saved with numbers.

Screenshots

Types of changes

Bug fix

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. .

@SergioEstevao SergioEstevao added Mobile App - i.e. Android or iOS Native mobile impl of the block editor. (Note: used in scripts, ping mobile folks to change) [Block] Gallery Affects the Gallery Block - used to display groups of images labels Dec 16, 2019
@SergioEstevao SergioEstevao added this to the Future milestone Dec 16, 2019
@SergioEstevao SergioEstevao requested review from koke and gziolo December 16, 2019 11:42
@SergioEstevao SergioEstevao changed the title Gallerys ids type Gallerys ids are saved as numbers Dec 16, 2019
@gziolo gziolo requested review from aduth, youknowriad and mcsf December 16, 2019 12:17
@aduth
Copy link
Member

aduth commented Dec 16, 2019

Changed the JSON schema for the gallery blocks to ensure that the ids array is always made with numbers.

Can you explain why? What's the context here?

The general idea seems to make sense. There's a lot of code here to make that happen though, which is pretty unfortunate.

@SergioEstevao
Copy link
Contributor Author

Can you explain why? What's the context here?

When implementing support for async media publishing on the media apps, we have scenarios were the media is uploaded in the background when Gutenberg editor is not even open.

When that happens we have some text processors that find the block code and update the media information.

When doing that work we found this situation where ids were being saved sometimes like string and another times like ints.

We coded around it, but I feel this is something that should be consistent on the block saving code.

@SergioEstevao
Copy link
Contributor Author

@aduth ready for another look.

@koke
Copy link
Contributor

koke commented Dec 17, 2019

I'm still getting strings, testing on web:

  1. Create a new post and add a gallery
  2. View HTML, IDs are numbers
  3. Save and reopen, IDs are numbers
  4. Reorder some of the images, IDs are strings
  5. Save and reopen, IDs are numbers

So it seems that at least the deprecation path is working and it's recovering automatically from the strings case, but it's still storing image IDs as strings internally, since they get replaced when the images are reordered.

I don't understand the parser code 100% but it seems to me that attributes extracted from HTML attributes are always strings, regardless of the type specified. Maybe we need some extra code in save.js to ensure that those IDs are always numeric, but I'd appreciate a second opinion on that.

@@ -15,6 +15,139 @@ import { RichText } from '@wordpress/block-editor';
import { defaultColumnsNumber } from './shared';

const deprecated = [
{
attributes: {
Copy link
Member

Choose a reason for hiding this comment

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

Is there any opportunity for code reuse in any of these properties or functions? Merging attributes using Object.assign / spread? Reference a common save function?

This is a lot of code to coerce an array to a typed array.

Copy link
Contributor Author

@SergioEstevao SergioEstevao Dec 18, 2019

Choose a reason for hiding this comment

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

@aduth I considerer shared code with the current version of save, but then I saw this comment on the block deprecations documentation:

It’s important to note that attributes, supports, and save are not automatically inherited from the current version, since they can impact parsing and serialization of a block, so they must be defined on the deprecated object in order to be processed during a migration.

Is this still a valid concern? Or I can share code without problems with the current version of save?

Copy link
Member

Choose a reason for hiding this comment

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

The problem is that if you inherit from the current attributes, the deprecations may work now but not later if more attributes are added to the block. If you add an optional thingy attribute to the current block, and your deprecations inherit the entire attributes object from the current implementation, then all the deprecations will stop working. If you change an existing attribute, you have to remember to modify every single deprecation to stop inheriting from the current implementation.

Hardcoding the deprecation definitions, although verbose, is a more robust solution. I don't really see it as much of problem since the deprecations are now usually put in a separate file from the rest of the block data.

Copy link
Contributor

@talldan talldan Dec 19, 2019

Choose a reason for hiding this comment

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

Yeah, I prefer the longer safer option. Though it's generally ok if there are tests in place.

Which is something I think should be considered for this PR, ideally a core__gallery__deprecated-4.html fixture would be added in this folder:
https://github.com/WordPress/gutenberg/tree/master/packages/e2e-tests/fixtures/blocks

The way I generally do this is:

  1. Check out a branch with the right version of the block for the deprecation (so the gallery block from master)
  2. Insert it into a post
  3. Copy the markup using the Code Editor view
  4. Switch back to this branch
  5. Paste it into a new html file core__gallery__deprecated-4.html in the packages/e2e-tests/fixtures/blocks folder.
  6. Generate the other fixture files by running GENERATE_MISSING_FIXTURES=y npm run test-unit test/integration/full-content/full-content.test.js
  7. Commit and push.

@SergioEstevao SergioEstevao merged commit 68f76f3 into master Jan 7, 2020
@SergioEstevao SergioEstevao deleted the rnmobile/gallerys_ids_type branch January 7, 2020 10:44
@ellatrix ellatrix modified the milestones: Future, Gutenberg 7.3 Jan 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Gallery Affects the Gallery Block - used to display groups of images Mobile App - i.e. Android or iOS Native mobile impl of the block editor. (Note: used in scripts, ping mobile folks to change)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants