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

Fix duplicate block ids when duplicating stream blocks in page editor #9108

Merged
merged 4 commits into from
Sep 5, 2022

Conversation

jams2
Copy link
Contributor

@jams2 jams2 commented Aug 31, 2022

fixes #7712 (duplication of block ids when duplicating stream field blocks in page editor), supersedes #7713.

This implementation (based on @gasman and @jacobtoppm 's comments on #7713) adds getDuplicatedState methods to BaseSequenceChild, BaseSequenceBlock (inherited by ListBlock and StreamChild), StreamChild, and StructBlock.

getDuplicatedState generates new block ids for stream/list children (at all levels of nesting) when duplicating blocks. It is called in the duplicate/duplicateBlock methods of compound block types.

Tested manually that block duplication still works as expected in the page editor in Firefox and Chrome.

@squash-labs
Copy link

squash-labs bot commented Aug 31, 2022

Manage this branch in Squash

Test this branch here: https://jams2fix7712-fix-duplicated-st-f8yam.squash.io

@jacobtoppm jacobtoppm requested a review from gasman August 31, 2022 09:50
@jams2 jams2 force-pushed the fix/7712-fix-duplicated-streamblock-uuids branch from 9f792d9 to a5ab703 Compare August 31, 2022 09:50
rows: this.rows.map((row) => ({
values: row.blocks.map((block) => block.getValue()),
})),
};
return value;
}

getColumnState() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice improvement here!getColumnsState (with an s) might be ever-so-slightly clearer though, since there are usually multiple columns

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point!

Copy link
Contributor

@ababic ababic Aug 31, 2022

Choose a reason for hiding this comment

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

Also, do we need a getDuplicatedColumnsState(), or do columns not have ids?

Copy link
Contributor Author

@jams2 jams2 Sep 1, 2022

Choose a reason for hiding this comment

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

If I'm not mistaken, we don't need to handle columns any differently. TypedTableBlock.columns is a list of objects that contain metadata for the column, including:

  • the block definition of the "type" that can be inserted into rows in this column; and
  • an integer ID, the index of the column, assigned when the column is created.

The IDs on the row items are generated in a similar fashion. These column and row IDs don't appear to have a unique constraint as they aren't used (for example) as part of a contentpath for comments. The reason that we need special handling for rows is that the block type for a given column may be a complex type (e.g. a StreamBlock), which we would need to recurse into instances of, to make sure StreamChild, ListChild, etc. instances don't have duplicate IDs when duplicating a block.

However, this code is new to me so I'm happy to be corrected.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Regarding the name, I've gone with getColumnStates - that feels more natural to me.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for clarifying there @jams2. Happy with all of that :)

for (const name in this.childBlocks) {
const block = this.childBlocks[name];
state[name] =
block.getDuplicatedState === undefined
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like we are missing a test addition for this component?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was sort of covered in StreamBlock.test.js but I've split them out to handle the block types more specifically in their corresponding modules.

Copy link
Contributor

Choose a reason for hiding this comment

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

👍

Copy link
Contributor

@ababic ababic left a comment

Choose a reason for hiding this comment

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

@jams2 Had a good comb through, and what is here looks really great (although, I must admin, some of this JS goes over my head!).

One additional thing that occurred to me, though: Does the ListBlock component need updating too?

@jams2
Copy link
Contributor Author

jams2 commented Aug 31, 2022

Thanks @ababic ! Going to mark this as a draft while I investigate the ListBlock

@jams2 jams2 marked this pull request as draft August 31, 2022 11:26
@jams2 jams2 force-pushed the fix/7712-fix-duplicated-streamblock-uuids branch from 9035eeb to 7df7f7a Compare September 1, 2022 10:03
@jams2 jams2 force-pushed the fix/7712-fix-duplicated-streamblock-uuids branch from 7df7f7a to f9a3791 Compare September 1, 2022 15:51
@jams2 jams2 force-pushed the fix/7712-fix-duplicated-streamblock-uuids branch from 6edc56d to 5cb6297 Compare September 1, 2022 16:44
@jams2 jams2 marked this pull request as ready for review September 1, 2022 16:55
Copy link
Contributor

@kaedroho kaedroho left a comment

Choose a reason for hiding this comment

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

This looks good to me, thanks!

And thanks @ababic for helping with review!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Duplicate streamfield block duplicates UUID
4 participants