-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Changes from all commits
f04f0d9
1e2690e
21435ae
4acb4f3
ae3fde4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import './meta'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd expect this file to live in |
||
* WordPress dependencies | ||
*/ | ||
import { addFilter } from '@wordpress/hooks'; | ||
import { select } from '@wordpress/data'; | ||
|
||
const editor = select( 'core/editor' ); | ||
|
||
/** | ||
* Filters an attribute value during parse to inject post properties from meta. | ||
* | ||
* @param {*} value Parsed value. | ||
* @param {Object} schema Attribute schema. | ||
* | ||
* @return {*} Filtered value with meta substitute if applicable. | ||
*/ | ||
function getMetaAttributeFromPost( value, schema ) { | ||
if ( schema.source === 'meta' ) { | ||
const meta = editor.getCurrentPost().meta; | ||
if ( meta && meta.hasOwnProperty( schema.meta ) ) { | ||
return meta[ schema.meta ]; | ||
} | ||
} | ||
|
||
return value; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? |
||
|
||
addFilter( 'blocks.getBlockAttribute', 'core/edit-post/meta/getMetaAttributeFromPost', getMetaAttributeFromPost ); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -114,12 +114,12 @@ function getFlattenedBlocks( blocks ) { | |
export const editor = flow( [ | ||
combineReducers, | ||
|
||
// Track undo history, starting at editor initialization. | ||
partialRight( withHistory, { resetTypes: [ 'SETUP_NEW_POST', 'SETUP_EDITOR' ] } ), | ||
// Track undo history, starting at editor initialization of blocks. Assumes | ||
// one of either block resetting, new post setup occurs in initialization. | ||
partialRight( withHistory, { resetTypes: [ 'SETUP_NEW_POST', 'RESET_BLOCKS' ] } ), | ||
|
||
// Track whether changes exist, resetting at each post save. Relies on | ||
// 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' ] } ), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Sigh... maybe we need a dedicated "editor ready" action in addition to the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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. |
||
] )( { | ||
edits( state = {}, action ) { | ||
switch ( action.type ) { | ||
|
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.
Do we really need two filters?
blocks.getBlockAttribute
andblocks.getBlockAttribute.source