Skip to content
This repository has been archived by the owner on Oct 16, 2024. It is now read-only.

Try moving blocks to their own modular directories #170

Merged
merged 3 commits into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { __ } from '@wordpress/i18n';
import PatternEdit from '../components/PatternEdit';
import PatternEdit from './PatternEdit';

export default function registerPatternBlock(
Copy link
Contributor

Choose a reason for hiding this comment

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

Good idea to move the block-related functions to blocks/

settings: Record< string, unknown >,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { render } from '@testing-library/react';
import PatternEdit from './';
import PatternEdit from '../PatternEdit';

jest.mock( '../../globals', () => {
jest.mock( '../../../globals', () => {
return {
patternManager: {
patterns: {
Expand All @@ -18,11 +18,11 @@ jest.mock( '../../globals', () => {
};
} );

jest.mock( '../../hooks/useSavedPostData', () => {
jest.mock( '../../../hooks/useSavedPostData', () => {
return () => ( {} );
} );

jest.mock( '../../../../../app/js/src/components/PatternPreview', () => {
jest.mock( '../../../../../../app/js/src/components/PatternPreview', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Mocking for React components is so messy 🤣

return () => null;
} );

Expand Down
2 changes: 1 addition & 1 deletion wp-modules/editor/js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import PatternManagerMetaControls from './components/PatternManagerMetaControls'
import changeWords from './utils/changeWords';
import preventTransform from './utils/preventTransform';
import receiveActiveTheme from './utils/receiveActiveTheme';
import registerPatternBlock from './utils/registerPatternBlock';
import registerPatternBlock from './blocks/pattern-block/registerPatternBlock';

registerPlugin( 'pattern-manager-postmeta-for-patterns', {
icon: null,
Expand Down
5 changes: 3 additions & 2 deletions wp-modules/editor/js/src/utils/filterOutPatterns.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { parse } from '@wordpress/blocks';
import hasPmPatternBlock from './hasPmPatternBlock';
import hasBlock from './hasBlock';
import type { Pattern, Patterns } from '../types';

/**
Expand All @@ -14,7 +14,8 @@ export default function filterOutPatterns(
( accumulator, [ key, pattern ] ) => {
return {
...accumulator,
...( ! hasPmPatternBlock(
...( ! hasBlock(
'core/pattern',
parse( pattern.content ),
patternName
) && pattern.name !== patternName
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import type { Block, Pattern } from '../types';

export default function hasPmPatternBlock(
export default function hasBlock(
blockName: String,
blocks: Block[],
patternName: Pattern[ 'name' ]
) {
return blocks.some( ( block ) => {
return (
( block.name === 'core/pattern' &&
( block.name === blockName &&
block.attributes?.slug === patternName ) ||
hasPmPatternBlock( block?.innerBlocks ?? [], patternName )
hasBlock( blockName, block?.innerBlocks ?? [], patternName )
);
} );
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import hasPmPatternBlock from '../hasPmPatternBlock';
import hasBlock from '../hasBlock';

describe( 'hasPmPatternBlock', () => {
describe( 'hasBlock', () => {
it.each( [
[ [ { name: '', attributes: {} } ], '', false ],
[ [ { name: '', attributes: {} } ], 'example-slug', false ],
Expand Down Expand Up @@ -75,7 +75,7 @@ describe( 'hasPmPatternBlock', () => {
] )(
'should get whether there is a Pattern Block',
( blocks, patternSlug, expected ) => {
expect( hasPmPatternBlock( blocks, patternSlug ) ).toEqual(
expect( hasBlock( 'core/pattern', blocks, patternSlug ) ).toEqual(
expected
);
}
Expand Down