@wordpress/reusable-blocks 3.0.3 → 3.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,160 +0,0 @@
1
- /**
2
- * External dependencies
3
- */
4
- import { isFunction } from 'lodash';
5
-
6
- /**
7
- * WordPress dependencies
8
- */
9
- import {
10
- isReusableBlock,
11
- createBlock,
12
- parse,
13
- serialize,
14
- } from '@wordpress/blocks';
15
- import { createRegistryControl } from '@wordpress/data';
16
- import { __ } from '@wordpress/i18n';
17
- import { store as blockEditorStore } from '@wordpress/block-editor';
18
-
19
- /**
20
- * Internal dependencies
21
- */
22
- import { store as reusableBlocksStore } from './index.js';
23
-
24
- /**
25
- * Convert a reusable block to a static block effect handler
26
- *
27
- * @param {string} clientId Block ID.
28
- * @return {Object} control descriptor.
29
- */
30
- export function convertBlockToStatic( clientId ) {
31
- return {
32
- type: 'CONVERT_BLOCK_TO_STATIC',
33
- clientId,
34
- };
35
- }
36
-
37
- /**
38
- * Convert a static block to a reusable block effect handler
39
- *
40
- * @param {Array} clientIds Block IDs.
41
- * @param {string} title Reusable block title.
42
- * @return {Object} control descriptor.
43
- */
44
- export function convertBlocksToReusable( clientIds, title ) {
45
- return {
46
- type: 'CONVERT_BLOCKS_TO_REUSABLE',
47
- clientIds,
48
- title,
49
- };
50
- }
51
-
52
- /**
53
- * Deletes a reusable block.
54
- *
55
- * @param {string} id Reusable block ID.
56
- * @return {Object} control descriptor.
57
- */
58
- export function deleteReusableBlock( id ) {
59
- return {
60
- type: 'DELETE_REUSABLE_BLOCK',
61
- id,
62
- };
63
- }
64
-
65
- const controls = {
66
- CONVERT_BLOCK_TO_STATIC: createRegistryControl(
67
- ( registry ) => ( { clientId } ) => {
68
- const oldBlock = registry
69
- .select( blockEditorStore )
70
- .getBlock( clientId );
71
- const reusableBlock = registry
72
- .select( 'core' )
73
- .getEditedEntityRecord(
74
- 'postType',
75
- 'wp_block',
76
- oldBlock.attributes.ref
77
- );
78
-
79
- const newBlocks = parse(
80
- isFunction( reusableBlock.content )
81
- ? reusableBlock.content( reusableBlock )
82
- : reusableBlock.content
83
- );
84
- registry
85
- .dispatch( blockEditorStore )
86
- .replaceBlocks( oldBlock.clientId, newBlocks );
87
- }
88
- ),
89
-
90
- CONVERT_BLOCKS_TO_REUSABLE: createRegistryControl(
91
- ( registry ) =>
92
- async function ( { clientIds, title } ) {
93
- const reusableBlock = {
94
- title: title || __( 'Untitled Reusable block' ),
95
- content: serialize(
96
- registry
97
- .select( blockEditorStore )
98
- .getBlocksByClientId( clientIds )
99
- ),
100
- status: 'publish',
101
- };
102
-
103
- const updatedRecord = await registry
104
- .dispatch( 'core' )
105
- .saveEntityRecord( 'postType', 'wp_block', reusableBlock );
106
-
107
- const newBlock = createBlock( 'core/block', {
108
- ref: updatedRecord.id,
109
- } );
110
- registry
111
- .dispatch( blockEditorStore )
112
- .replaceBlocks( clientIds, newBlock );
113
- registry
114
- .dispatch( reusableBlocksStore )
115
- .__experimentalSetEditingReusableBlock(
116
- newBlock.clientId,
117
- true
118
- );
119
- }
120
- ),
121
-
122
- DELETE_REUSABLE_BLOCK: createRegistryControl(
123
- ( registry ) =>
124
- async function ( { id } ) {
125
- const reusableBlock = registry
126
- .select( 'core' )
127
- .getEditedEntityRecord( 'postType', 'wp_block', id );
128
-
129
- // Don't allow a reusable block with a temporary ID to be deleted
130
- if ( ! reusableBlock ) {
131
- return;
132
- }
133
-
134
- // Remove any other blocks that reference this reusable block
135
- const allBlocks = registry
136
- .select( blockEditorStore )
137
- .getBlocks();
138
- const associatedBlocks = allBlocks.filter(
139
- ( block ) =>
140
- isReusableBlock( block ) && block.attributes.ref === id
141
- );
142
- const associatedBlockClientIds = associatedBlocks.map(
143
- ( block ) => block.clientId
144
- );
145
-
146
- // Remove the parsed block.
147
- if ( associatedBlockClientIds.length ) {
148
- registry
149
- .dispatch( blockEditorStore )
150
- .removeBlocks( associatedBlockClientIds );
151
- }
152
-
153
- await registry
154
- .dispatch( 'core' )
155
- .deleteEntityRecord( 'postType', 'wp_block', id );
156
- }
157
- ),
158
- };
159
-
160
- export default controls;
@@ -1,195 +0,0 @@
1
- /**
2
- * WordPress dependencies
3
- */
4
- import {
5
- registerBlockType,
6
- unregisterBlockType,
7
- createBlock,
8
- } from '@wordpress/blocks';
9
-
10
- /**
11
- * Internal dependencies
12
- */
13
- import controls from '../controls';
14
-
15
- const {
16
- CONVERT_BLOCK_TO_STATIC,
17
- CONVERT_BLOCKS_TO_REUSABLE,
18
- DELETE_REUSABLE_BLOCK,
19
- } = controls;
20
-
21
- describe( 'reusable blocks effects', () => {
22
- beforeAll( () => {
23
- registerBlockType( 'core/test-block', {
24
- title: 'Test block',
25
- category: 'text',
26
- save: () => null,
27
- attributes: {
28
- name: { type: 'string' },
29
- },
30
- } );
31
-
32
- registerBlockType( 'core/block', {
33
- title: 'Reusable block',
34
- category: 'text',
35
- save: () => null,
36
- attributes: {
37
- ref: { type: 'string' },
38
- },
39
- } );
40
- } );
41
-
42
- afterAll( () => {
43
- unregisterBlockType( 'core/test-block' );
44
- unregisterBlockType( 'core/block' );
45
- } );
46
-
47
- describe( 'CONVERT_BLOCKS_TO_REUSABLE', () => {
48
- it( 'should convert a static block into a reusable block', async () => {
49
- const staticBlock = createBlock( 'core/test-block', {
50
- name: 'Big Bird',
51
- } );
52
- const saveEntityRecord = jest.fn( () => ( { id: 456 } ) );
53
- const replaceBlocks = jest.fn();
54
- const __experimentalSetEditingReusableBlock = jest.fn();
55
- const getBlocksByClientId = jest.fn( () => [ staticBlock ] );
56
- const registry = {
57
- select: jest.fn( () => ( {
58
- getBlocksByClientId,
59
- } ) ),
60
- dispatch: jest.fn( () => ( {
61
- saveEntityRecord,
62
- replaceBlocks,
63
- __experimentalSetEditingReusableBlock,
64
- } ) ),
65
- };
66
-
67
- await CONVERT_BLOCKS_TO_REUSABLE( registry )( {
68
- clientIds: [ staticBlock.clientId ],
69
- } );
70
-
71
- expect( saveEntityRecord ).toHaveBeenCalledWith(
72
- 'postType',
73
- 'wp_block',
74
- expect.objectContaining( {
75
- content: '<!-- wp:test-block {"name":"Big Bird"} /-->',
76
- status: 'publish',
77
- title: 'Untitled Reusable block',
78
- } )
79
- );
80
- expect( replaceBlocks ).toHaveBeenCalledWith(
81
- [ staticBlock.clientId ],
82
- expect.objectContaining( {
83
- attributes: expect.objectContaining( { ref: 456 } ),
84
- isValid: true,
85
- name: 'core/block',
86
- } )
87
- );
88
- expect( __experimentalSetEditingReusableBlock ).toHaveBeenCalled();
89
- } );
90
-
91
- describe( 'CONVERT_BLOCK_TO_STATIC', () => {
92
- it( 'should convert a reusable block into a static block', async () => {
93
- const associatedBlock = createBlock( 'core/block', {
94
- ref: 123,
95
- } );
96
- const reusableBlock = {
97
- id: 123,
98
- title: 'My cool block',
99
- content:
100
- '<!-- wp:test-block {"name":"Big Bird"} --><!-- wp:test-block {"name":"Oscar the Grouch"} /--><!-- wp:test-block {"name":"Cookie Monster"} /--><!-- /wp:test-block -->',
101
- };
102
- const replaceBlocks = jest.fn();
103
- const getBlock = jest.fn( () => associatedBlock );
104
- const getEditedEntityRecord = jest.fn( () => reusableBlock );
105
- const registry = {
106
- select: jest.fn( () => ( {
107
- getBlock,
108
- getEditedEntityRecord,
109
- } ) ),
110
- dispatch: jest.fn( () => ( {
111
- replaceBlocks,
112
- } ) ),
113
- };
114
-
115
- CONVERT_BLOCK_TO_STATIC( registry )( {
116
- clientId: associatedBlock.clientId,
117
- } );
118
-
119
- expect( replaceBlocks ).toHaveBeenCalledWith(
120
- associatedBlock.clientId,
121
- [
122
- expect.objectContaining( {
123
- name: 'core/test-block',
124
- attributes: { name: 'Big Bird' },
125
- innerBlocks: [
126
- expect.objectContaining( {
127
- attributes: { name: 'Oscar the Grouch' },
128
- } ),
129
- expect.objectContaining( {
130
- attributes: { name: 'Cookie Monster' },
131
- } ),
132
- ],
133
- } ),
134
- ]
135
- );
136
- } );
137
- } );
138
-
139
- describe( 'DELETE_REUSABLE_BLOCK', () => {
140
- it( 'should delete a reusable block and remove all its instances from the store', async () => {
141
- const associatedBlock = createBlock( 'core/block', {
142
- ref: 123,
143
- } );
144
- const reusableBlock = {
145
- id: 123,
146
- };
147
- const availableBlocks = [
148
- createBlock( 'core/block' ),
149
- createBlock( 'core/block', {
150
- ref: 123,
151
- } ),
152
- createBlock( 'core/block', {
153
- ref: 456,
154
- } ),
155
- createBlock( 'core/block', {
156
- ref: 123,
157
- } ),
158
- createBlock( 'core/test-block', {
159
- ref: 123,
160
- } ),
161
- ];
162
- const removeBlocks = jest.fn();
163
- const deleteEntityRecord = jest.fn();
164
- const getBlock = jest.fn( () => associatedBlock );
165
- const getBlocks = jest.fn( () => availableBlocks );
166
- const getEditedEntityRecord = jest.fn( () => reusableBlock );
167
- const registry = {
168
- select: jest.fn( () => ( {
169
- getBlock,
170
- getBlocks,
171
- getEditedEntityRecord,
172
- } ) ),
173
- dispatch: jest.fn( () => ( {
174
- removeBlocks,
175
- deleteEntityRecord,
176
- } ) ),
177
- };
178
-
179
- DELETE_REUSABLE_BLOCK( registry )( {
180
- id: reusableBlock.id,
181
- } );
182
-
183
- expect( deleteEntityRecord ).toHaveBeenCalledWith(
184
- 'postType',
185
- 'wp_block',
186
- 123
187
- );
188
- expect( removeBlocks ).toHaveBeenCalledWith( [
189
- availableBlocks[ 1 ].clientId,
190
- availableBlocks[ 3 ].clientId,
191
- ] );
192
- } );
193
- } );
194
- } );
195
- } );