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

Parser: Refactor meta parsing as filtered external source #5077

Closed
wants to merge 5 commits into from

Conversation

aduth
Copy link
Member

@aduth aduth commented Feb 15, 2018

Fixes #4989

This pull request seeks to refactor meta attributes as injected during a parse via filtering applied by the edit-post module. This resolves issues where validation could fail because meta attributes are not known at parse-time, and should improve performance of the getBlock selector by avoiding an iteration over block attributes.

Testing instructions:

Verify that there are no regressions in the behavior of meta attribute usage.

Here's an example meta-based "Stars Block" plugin if you don't have a meta-driven block handy:

https://cloudup.com/files/i02CNzKdB5f/download

Specifically, ensure that meta values can be manipulated, saved, and restored (with value represented correctly, and validation passing).

Ensure unit tests pass:

npm test

Known Issues:

Need to ensure post doesn't have history when editing. This issue exists on master as well. Fixed in 4acb4f3.

Edit: High priority reflects that this is not only concerning to performance of getBlock, but that having undo history immediately upon loading a saved post is quite non-ideal (and potentially destructive, because using the undo will remove all content).

Edit 2: Initial undo history resolved separately in #5101.

@aduth aduth added [Feature] Block API API that allows to express the block paradigm. [Feature] Parsing Related to efforts to improving the parsing of a string of data and converting it into a different f [Type] Performance Related to performance efforts labels Feb 15, 2018
Originally seemed redundant, but could be confused with e.g. block schema
@aduth aduth added this to the 2.2 milestone Feb 15, 2018
@aduth aduth added the [Priority] High Used to indicate top priority items that need quick attention label Feb 15, 2018
My eyes apparently can't distinguish between an "if" and an "else if"
@@ -118,7 +119,15 @@ export function getBlockAttribute( attributeKey, attributeSchema, innerHTML, com
break;
}

return value === undefined ? attributeSchema.default : asType( value, attributeSchema.type );
value = applyFilters( 'blocks.getBlockAttribute.source', value, attributeSchema, innerHTML, commentAttributes );
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we really need two filters? blocks.getBlockAttribute and blocks.getBlockAttribute.source

@@ -0,0 +1,28 @@
/**
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd expect this file to live in editor.hooks for now. I understand that it's post related but I believe the refactoring to extract all post related stuff should be done in one step instead of introducing inconsistency.

// editor initialization firing post reset as an effect.
partialRight( withChangeDetection, { resetTypes: [ 'SETUP_NEW_POST', 'RESET_POST' ] } ),
// Track whether changes exist, resetting at initialization and each save.
partialRight( withChangeDetection, { resetTypes: [ 'RESET_POST', 'SETUP_NEW_POST', 'RESET_BLOCKS' ] } ),
Copy link
Contributor

Choose a reason for hiding this comment

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

RESET_BLOCKS is also triggered in the text editor and it should not reset dirty detection in that case

Copy link
Member Author

Choose a reason for hiding this comment

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

RESET_BLOCKS is also triggered in the text editor and it should not reset dirty detection in that case

Sigh... maybe we need a dedicated "editor ready" action in addition to the SETUP_EDITOR one 😅

Copy link
Member

Choose a reason for hiding this comment

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

Should switching editors affect history? It's kind of a destructive action that, as a user, I wouldn't expect from just switching editor modes.

Copy link
Contributor

Choose a reason for hiding this comment

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

Should switching editors affect history? It's kind of a destructive action that, as a user, I wouldn't expect from just switching editor modes.

I'd look into preserving history across modes. I've experienced content loss firsthand through something like that (Calypso editor): some Heisenbug in Visual makes content disappear, toggle to Code, notice it's empty, come back to Visual, find no edit history to recover the content.

}

return value;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

So right now, retrieving the attribute is done using a filter but updating this source type is still achieved using hard-coded code. I believe we should create a filter for it aswell?

Copy link
Contributor

@youknowriad youknowriad left a comment

Choose a reason for hiding this comment

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

If I understand correctly, I believe this change is creating an issue if you use the same attribute in two different blocks. Their value won't be synced in which I case I'd prefer if we introduce a hook (or something else) to allow this syncing.

@aduth
Copy link
Member Author

aduth commented Feb 16, 2018

Thanks for the review @youknowriad .

I'm going to set aside the meta refactoring for post-2.2, but we should really resolve the issue with an initial undo level for the release. I can take a look once I sort through other updates this morning.

@aduth aduth removed this from the 2.2 milestone Feb 16, 2018
@aduth
Copy link
Member Author

aduth commented Feb 16, 2018

Removed from 2.2 milestone per separately addressing initial undo history in #5101.

Copy link
Contributor

@mcsf mcsf left a comment

Choose a reason for hiding this comment

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

Looks good so far, though I'll wait for answers/fixes to Riad's feedback.

// editor initialization firing post reset as an effect.
partialRight( withChangeDetection, { resetTypes: [ 'SETUP_NEW_POST', 'RESET_POST' ] } ),
// Track whether changes exist, resetting at initialization and each save.
partialRight( withChangeDetection, { resetTypes: [ 'RESET_POST', 'SETUP_NEW_POST', 'RESET_BLOCKS' ] } ),
Copy link
Contributor

Choose a reason for hiding this comment

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

Should switching editors affect history? It's kind of a destructive action that, as a user, I wouldn't expect from just switching editor modes.

I'd look into preserving history across modes. I've experienced content loss firsthand through something like that (Calypso editor): some Heisenbug in Visual makes content disappear, toggle to Code, notice it's empty, come back to Visual, find no edit history to recover the content.

@aduth aduth added the [Feature] Extensibility The ability to extend blocks or the editing experience label Feb 21, 2018
@aduth aduth added this to the 2.3 milestone Feb 21, 2018
@aduth
Copy link
Member Author

aduth commented Feb 26, 2018

While I'm still curious to explore related refactoring, it's unlikely I'll resume this branch in its current form.

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. [Feature] Extensibility The ability to extend blocks or the editing experience [Feature] Parsing Related to efforts to improving the parsing of a string of data and converting it into a different f [Priority] High Used to indicate top priority items that need quick attention [Type] Performance Related to performance efforts
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Validation fails if attribute is persisted to post content & meta.
4 participants