Switch to using getBlockPatterns
and update implementation
MAILPOET-6444
This commit is contained in:
committed by
Aschepikov
parent
74f2281ff0
commit
9b65494bf8
@ -5,7 +5,6 @@ import { useMemo } from '@wordpress/element';
|
|||||||
import { parse } from '@wordpress/blocks';
|
import { parse } from '@wordpress/blocks';
|
||||||
import { BlockInstance } from '@wordpress/blocks/index';
|
import { BlockInstance } from '@wordpress/blocks/index';
|
||||||
import { useSelect } from '@wordpress/data';
|
import { useSelect } from '@wordpress/data';
|
||||||
import { store as blockEditorStore } from '@wordpress/block-editor';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Internal dependencies
|
* Internal dependencies
|
||||||
@ -17,6 +16,10 @@ import {
|
|||||||
EmailEditorPostType,
|
EmailEditorPostType,
|
||||||
} from '../store';
|
} from '../store';
|
||||||
|
|
||||||
|
// Shared reference to an empty array for cases where it is important to avoid
|
||||||
|
// returning a new array reference on every invocation
|
||||||
|
const EMPTY_ARRAY = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* We need to merge pattern blocks and template blocks for BlockPreview component.
|
* We need to merge pattern blocks and template blocks for BlockPreview component.
|
||||||
* @param templateBlocks - Parsed template blocks
|
* @param templateBlocks - Parsed template blocks
|
||||||
@ -96,21 +99,12 @@ export function usePreviewTemplates(
|
|||||||
): [ TemplatePreview[], TemplatePreview[], boolean ] {
|
): [ TemplatePreview[], TemplatePreview[], boolean ] {
|
||||||
const { templates, patterns, emailPosts, hasEmailPosts } = useSelect(
|
const { templates, patterns, emailPosts, hasEmailPosts } = useSelect(
|
||||||
( select ) => {
|
( select ) => {
|
||||||
const contentBlockId =
|
|
||||||
// @ts-expect-error getBlocksByName is not defined in types
|
|
||||||
select( blockEditorStore ).getBlocksByName(
|
|
||||||
'core/post-content'
|
|
||||||
)?.[ 0 ];
|
|
||||||
|
|
||||||
const rawEmailPosts = select( storeName ).getSentEmailEditorPosts();
|
const rawEmailPosts = select( storeName ).getSentEmailEditorPosts();
|
||||||
|
|
||||||
return {
|
return {
|
||||||
templates: select( storeName ).getEmailTemplates(),
|
templates: select( storeName ).getEmailTemplates(),
|
||||||
patterns:
|
patterns:
|
||||||
// @ts-expect-error getPatternsByBlockTypes is not defined in types
|
select( storeName ).getBlockPatternsForEmailTemplate(),
|
||||||
select( blockEditorStore ).getPatternsByBlockTypes(
|
|
||||||
[ 'core/post-content' ],
|
|
||||||
contentBlockId
|
|
||||||
),
|
|
||||||
emailPosts: rawEmailPosts,
|
emailPosts: rawEmailPosts,
|
||||||
hasEmailPosts: !! ( rawEmailPosts && rawEmailPosts?.length ),
|
hasEmailPosts: !! ( rawEmailPosts && rawEmailPosts?.length ),
|
||||||
};
|
};
|
||||||
@ -128,15 +122,11 @@ export function usePreviewTemplates(
|
|||||||
if ( parsedCustomEmailContent ) {
|
if ( parsedCustomEmailContent ) {
|
||||||
contentPatterns = [ { blocks: parsedCustomEmailContent } ];
|
contentPatterns = [ { blocks: parsedCustomEmailContent } ];
|
||||||
} else {
|
} else {
|
||||||
contentPatterns = patterns.filter(
|
contentPatterns = patterns;
|
||||||
( pattern ) =>
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
||||||
pattern?.templateTypes?.includes( 'email-template' )
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( ! contentPatterns || ! templates ) {
|
if ( ! contentPatterns || ! templates ) {
|
||||||
return [];
|
return EMPTY_ARRAY;
|
||||||
}
|
}
|
||||||
|
|
||||||
const templateToPreview = [];
|
const templateToPreview = [];
|
||||||
@ -208,5 +198,9 @@ export function usePreviewTemplates(
|
|||||||
} ) as unknown as TemplatePreview[];
|
} ) as unknown as TemplatePreview[];
|
||||||
}, [ emailPosts, allTemplates ] );
|
}, [ emailPosts, allTemplates ] );
|
||||||
|
|
||||||
return [ allTemplates || [], allEmailPosts || [], hasEmailPosts ];
|
return [
|
||||||
|
allTemplates || EMPTY_ARRAY,
|
||||||
|
allEmailPosts || EMPTY_ARRAY,
|
||||||
|
hasEmailPosts,
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
/**
|
/**
|
||||||
* External dependencies
|
* External dependencies
|
||||||
*/
|
*/
|
||||||
import { createRegistrySelector } from '@wordpress/data';
|
import { createRegistrySelector, createSelector } from '@wordpress/data';
|
||||||
import { store as coreDataStore } from '@wordpress/core-data';
|
import { store as coreDataStore } from '@wordpress/core-data';
|
||||||
import { store as interfaceStore } from '@wordpress/interface';
|
import { store as interfaceStore } from '@wordpress/interface';
|
||||||
import { store as editorStore } from '@wordpress/editor';
|
import { store as editorStore } from '@wordpress/editor';
|
||||||
import { store as preferencesStore } from '@wordpress/preferences';
|
import { store as preferencesStore } from '@wordpress/preferences';
|
||||||
import { serialize } from '@wordpress/blocks';
|
import { serialize, parse } from '@wordpress/blocks';
|
||||||
import { BlockInstance } from '@wordpress/blocks/index';
|
import { BlockInstance } from '@wordpress/blocks/index';
|
||||||
import { Post } from '@wordpress/core-data/build-types/entity-types/post';
|
import { Post } from '@wordpress/core-data/build-types/entity-types/post';
|
||||||
|
|
||||||
@ -29,6 +29,21 @@ function getContentFromEntity( entity ): string {
|
|||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const patternsWithParsedBlocks = new WeakMap();
|
||||||
|
function enhancePatternWithParsedBlocks( pattern ) {
|
||||||
|
let enhancedPattern = patternsWithParsedBlocks.get( pattern );
|
||||||
|
if ( ! enhancedPattern ) {
|
||||||
|
enhancedPattern = {
|
||||||
|
...pattern,
|
||||||
|
get blocks() {
|
||||||
|
return parse( pattern.content );
|
||||||
|
},
|
||||||
|
};
|
||||||
|
patternsWithParsedBlocks.set( pattern, enhancedPattern );
|
||||||
|
}
|
||||||
|
return enhancedPattern;
|
||||||
|
}
|
||||||
|
|
||||||
export const isFeatureActive = createRegistrySelector(
|
export const isFeatureActive = createRegistrySelector(
|
||||||
( select ) =>
|
( select ) =>
|
||||||
( _, feature: Feature ): boolean =>
|
( _, feature: Feature ): boolean =>
|
||||||
@ -164,6 +179,22 @@ export const getSentEmailEditorPosts = createRegistrySelector(
|
|||||||
) || []
|
) || []
|
||||||
);
|
);
|
||||||
|
|
||||||
|
export const getBlockPatternsForEmailTemplate = createRegistrySelector(
|
||||||
|
( select ) =>
|
||||||
|
createSelector(
|
||||||
|
() =>
|
||||||
|
select( coreDataStore )
|
||||||
|
.getBlockPatterns()
|
||||||
|
.filter(
|
||||||
|
( { templateTypes } ) =>
|
||||||
|
Array.isArray( templateTypes ) &&
|
||||||
|
templateTypes.includes( 'email-template' )
|
||||||
|
)
|
||||||
|
.map( enhancePatternWithParsedBlocks ),
|
||||||
|
() => [ select( coreDataStore ).getBlockPatterns() ]
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* COPIED FROM https://github.com/WordPress/gutenberg/blob/9c6d4fe59763b188d27ad937c2f0daa39e4d9341/packages/edit-post/src/store/selectors.js
|
* COPIED FROM https://github.com/WordPress/gutenberg/blob/9c6d4fe59763b188d27ad937c2f0daa39e4d9341/packages/edit-post/src/store/selectors.js
|
||||||
* Retrieves the template of the currently edited post.
|
* Retrieves the template of the currently edited post.
|
||||||
|
Reference in New Issue
Block a user