@wordpress/block-editor 15.6.1-next.36001005c.0 → 15.6.2

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.
Files changed (169) hide show
  1. package/build/components/background-image-control/index.js +2 -2
  2. package/build/components/background-image-control/index.js.map +2 -2
  3. package/build/components/block-list/block.js +3 -3
  4. package/build/components/block-list/block.js.map +2 -2
  5. package/build/components/block-list/index.js +2 -2
  6. package/build/components/block-list/index.js.map +1 -1
  7. package/build/components/block-quick-navigation/index.js +0 -1
  8. package/build/components/block-quick-navigation/index.js.map +2 -2
  9. package/build/components/global-styles/border-panel.js +1 -2
  10. package/build/components/global-styles/border-panel.js.map +2 -2
  11. package/build/components/global-styles/color-panel.js +1 -2
  12. package/build/components/global-styles/color-panel.js.map +2 -2
  13. package/build/components/global-styles/dimensions-panel.js +2 -3
  14. package/build/components/global-styles/dimensions-panel.js.map +2 -2
  15. package/build/components/global-styles/filters-panel.js +1 -2
  16. package/build/components/global-styles/filters-panel.js.map +2 -2
  17. package/build/components/global-styles/get-block-css-selector.js +78 -0
  18. package/build/components/global-styles/get-block-css-selector.js.map +7 -0
  19. package/build/components/global-styles/hooks.js +95 -23
  20. package/build/components/global-styles/hooks.js.map +2 -2
  21. package/build/components/global-styles/index.js +14 -0
  22. package/build/components/global-styles/index.js.map +2 -2
  23. package/build/components/global-styles/typography-panel.js +19 -3
  24. package/build/components/global-styles/typography-panel.js.map +2 -2
  25. package/build/components/global-styles/typography-utils.js +49 -2
  26. package/build/components/global-styles/typography-utils.js.map +2 -2
  27. package/build/components/global-styles/use-global-styles-output.js +998 -0
  28. package/build/components/global-styles/use-global-styles-output.js.map +7 -0
  29. package/build/components/global-styles/utils.js +377 -0
  30. package/build/components/global-styles/utils.js.map +2 -2
  31. package/build/components/rich-text/index.js +8 -7
  32. package/build/components/rich-text/index.js.map +2 -2
  33. package/build/hooks/allowed-blocks.js +50 -1
  34. package/build/hooks/allowed-blocks.js.map +2 -2
  35. package/build/hooks/block-bindings.js +111 -170
  36. package/build/hooks/block-bindings.js.map +2 -2
  37. package/build/hooks/block-style-variation.js +10 -6
  38. package/build/hooks/block-style-variation.js.map +2 -2
  39. package/build/hooks/custom-class-name.js +1 -1
  40. package/build/hooks/custom-class-name.js.map +1 -1
  41. package/build/hooks/duotone.js +3 -3
  42. package/build/hooks/duotone.js.map +2 -2
  43. package/build/hooks/fit-text.js +33 -20
  44. package/build/hooks/fit-text.js.map +2 -2
  45. package/build/hooks/font-size.js +6 -5
  46. package/build/hooks/font-size.js.map +2 -2
  47. package/build/hooks/metadata.js +48 -2
  48. package/build/hooks/metadata.js.map +2 -2
  49. package/build/hooks/typography.js +11 -4
  50. package/build/hooks/typography.js.map +3 -3
  51. package/build/hooks/use-typography-props.js +2 -2
  52. package/build/hooks/use-typography-props.js.map +2 -2
  53. package/build/store/private-selectors.js +3 -3
  54. package/build/store/private-selectors.js.map +2 -2
  55. package/build/store/selectors.js +38 -13
  56. package/build/store/selectors.js.map +2 -2
  57. package/build/store/utils.js +2 -1
  58. package/build/store/utils.js.map +2 -2
  59. package/build/utils/fit-text-utils.js +4 -4
  60. package/build/utils/fit-text-utils.js.map +2 -2
  61. package/build-module/components/background-image-control/index.js +1 -1
  62. package/build-module/components/background-image-control/index.js.map +2 -2
  63. package/build-module/components/block-list/block.js +3 -3
  64. package/build-module/components/block-list/block.js.map +2 -2
  65. package/build-module/components/block-list/index.js +2 -2
  66. package/build-module/components/block-list/index.js.map +1 -1
  67. package/build-module/components/block-quick-navigation/index.js +0 -1
  68. package/build-module/components/block-quick-navigation/index.js.map +2 -2
  69. package/build-module/components/global-styles/border-panel.js +1 -2
  70. package/build-module/components/global-styles/border-panel.js.map +2 -2
  71. package/build-module/components/global-styles/color-panel.js +1 -2
  72. package/build-module/components/global-styles/color-panel.js.map +2 -2
  73. package/build-module/components/global-styles/dimensions-panel.js +1 -2
  74. package/build-module/components/global-styles/dimensions-panel.js.map +2 -2
  75. package/build-module/components/global-styles/filters-panel.js +1 -2
  76. package/build-module/components/global-styles/filters-panel.js.map +2 -2
  77. package/build-module/components/global-styles/get-block-css-selector.js +54 -0
  78. package/build-module/components/global-styles/get-block-css-selector.js.map +7 -0
  79. package/build-module/components/global-styles/hooks.js +95 -27
  80. package/build-module/components/global-styles/hooks.js.map +2 -2
  81. package/build-module/components/global-styles/index.js +14 -0
  82. package/build-module/components/global-styles/index.js.map +2 -2
  83. package/build-module/components/global-styles/typography-panel.js +19 -3
  84. package/build-module/components/global-styles/typography-panel.js.map +2 -2
  85. package/build-module/components/global-styles/typography-utils.js +49 -1
  86. package/build-module/components/global-styles/typography-utils.js.map +2 -2
  87. package/build-module/components/global-styles/use-global-styles-output.js +979 -0
  88. package/build-module/components/global-styles/use-global-styles-output.js.map +7 -0
  89. package/build-module/components/global-styles/utils.js +364 -0
  90. package/build-module/components/global-styles/utils.js.map +2 -2
  91. package/build-module/components/rich-text/index.js +8 -7
  92. package/build-module/components/rich-text/index.js.map +2 -2
  93. package/build-module/hooks/allowed-blocks.js +49 -1
  94. package/build-module/hooks/allowed-blocks.js.map +2 -2
  95. package/build-module/hooks/block-bindings.js +112 -172
  96. package/build-module/hooks/block-bindings.js.map +2 -2
  97. package/build-module/hooks/block-style-variation.js +12 -4
  98. package/build-module/hooks/block-style-variation.js.map +2 -2
  99. package/build-module/hooks/custom-class-name.js +1 -1
  100. package/build-module/hooks/custom-class-name.js.map +1 -1
  101. package/build-module/hooks/duotone.js +3 -3
  102. package/build-module/hooks/duotone.js.map +2 -2
  103. package/build-module/hooks/fit-text.js +34 -21
  104. package/build-module/hooks/fit-text.js.map +2 -2
  105. package/build-module/hooks/font-size.js +5 -4
  106. package/build-module/hooks/font-size.js.map +2 -2
  107. package/build-module/hooks/metadata.js +46 -1
  108. package/build-module/hooks/metadata.js.map +2 -2
  109. package/build-module/hooks/typography.js +11 -4
  110. package/build-module/hooks/typography.js.map +3 -3
  111. package/build-module/hooks/use-typography-props.js +1 -1
  112. package/build-module/hooks/use-typography-props.js.map +2 -2
  113. package/build-module/store/private-selectors.js +2 -2
  114. package/build-module/store/private-selectors.js.map +2 -2
  115. package/build-module/store/selectors.js +39 -14
  116. package/build-module/store/selectors.js.map +2 -2
  117. package/build-module/store/utils.js +3 -2
  118. package/build-module/store/utils.js.map +2 -2
  119. package/build-module/utils/fit-text-utils.js +4 -4
  120. package/build-module/utils/fit-text-utils.js.map +2 -2
  121. package/build-style/style-rtl.css +6 -10
  122. package/build-style/style.css +6 -10
  123. package/package.json +35 -36
  124. package/src/components/background-image-control/index.js +1 -1
  125. package/src/components/block-card/style.scss +1 -1
  126. package/src/components/block-list/block.js +1 -1
  127. package/src/components/block-list/index.js +2 -2
  128. package/src/components/block-navigation/style.scss +1 -1
  129. package/src/components/block-quick-navigation/index.js +0 -1
  130. package/src/components/block-switcher/style.scss +1 -1
  131. package/src/components/color-palette/test/__snapshots__/control.js.snap +1 -1
  132. package/src/components/global-styles/border-panel.js +1 -2
  133. package/src/components/global-styles/color-panel.js +1 -2
  134. package/src/components/global-styles/color-panel.native.js +1 -1
  135. package/src/components/global-styles/dimensions-panel.js +1 -2
  136. package/src/components/global-styles/filters-panel.js +1 -2
  137. package/src/components/global-styles/get-block-css-selector.js +114 -0
  138. package/src/components/global-styles/hooks.js +108 -29
  139. package/src/components/global-styles/index.js +8 -0
  140. package/src/components/global-styles/test/typography-utils.js +806 -0
  141. package/src/components/global-styles/test/use-global-styles-output.js +1131 -0
  142. package/src/components/global-styles/test/utils.js +442 -1
  143. package/src/components/global-styles/typography-panel.js +27 -3
  144. package/src/components/global-styles/typography-utils.js +133 -0
  145. package/src/components/global-styles/use-global-styles-output.js +1487 -0
  146. package/src/components/global-styles/utils.js +537 -0
  147. package/src/components/inserter/style.scss +2 -2
  148. package/src/components/multi-selection-inspector/style.scss +1 -1
  149. package/src/components/rich-text/index.js +8 -14
  150. package/src/hooks/allowed-blocks.js +89 -1
  151. package/src/hooks/block-bindings.js +79 -153
  152. package/src/hooks/block-style-variation.js +12 -4
  153. package/src/hooks/custom-class-name.js +1 -1
  154. package/src/hooks/duotone.js +3 -3
  155. package/src/hooks/fit-text.js +39 -30
  156. package/src/hooks/font-size.js +8 -4
  157. package/src/hooks/metadata.js +89 -0
  158. package/src/hooks/test/allowed-blocks.js +278 -0
  159. package/src/hooks/test/metadata.js +316 -0
  160. package/src/hooks/typography.js +15 -4
  161. package/src/hooks/use-typography-props.js +1 -1
  162. package/src/store/private-selectors.js +2 -2
  163. package/src/store/selectors.js +59 -21
  164. package/src/store/test/selectors.js +1 -1
  165. package/src/store/utils.js +2 -1
  166. package/src/style.scss +0 -1
  167. package/src/utils/fit-text-utils.js +4 -16
  168. package/tsconfig.json +0 -1
  169. package/src/components/block-quick-navigation/style.scss +0 -5
@@ -2,6 +2,8 @@
2
2
  * WordPress dependencies
3
3
  */
4
4
  import { addFilter } from '@wordpress/hooks';
5
+ import { hasBlockSupport } from '@wordpress/blocks';
6
+
5
7
  const META_ATTRIBUTE_NAME = 'metadata';
6
8
 
7
9
  /**
@@ -28,8 +30,95 @@ export function addMetaAttribute( blockTypeSettings ) {
28
30
  return blockTypeSettings;
29
31
  }
30
32
 
33
+ /**
34
+ * Add metadata transforms.
35
+ *
36
+ * @param {Object} result The transformed block.
37
+ * @param {Array} source Original blocks transformed.
38
+ * @param {number} index Index of the transformed block.
39
+ * @param {Array} results All blocks that resulted from the transformation.
40
+ * @return {Object} Modified transformed block.
41
+ */
42
+ export function addTransforms( result, source, index, results ) {
43
+ // If the condition verifies we are probably in the presence of a wrapping transform
44
+ // e.g: nesting paragraphs in a group or columns and in that case the metadata should not be kept.
45
+ if ( results.length === 1 && result.innerBlocks.length === source.length ) {
46
+ return result;
47
+ }
48
+
49
+ // If we are transforming one block to multiple blocks or multiple blocks to one block,
50
+ // we ignore the metadata during the transform.
51
+ if (
52
+ ( results.length === 1 && source.length > 1 ) ||
53
+ ( results.length > 1 && source.length === 1 )
54
+ ) {
55
+ return result;
56
+ }
57
+
58
+ // If we are transforming multiple blocks to multiple blocks with different counts,
59
+ // we ignore the metadata during the transform.
60
+ if (
61
+ results.length > 1 &&
62
+ source.length > 1 &&
63
+ results.length !== source.length
64
+ ) {
65
+ return result;
66
+ }
67
+
68
+ const sourceMetadata = source[ index ]?.attributes?.metadata;
69
+
70
+ if ( ! sourceMetadata ) {
71
+ return result;
72
+ }
73
+
74
+ const preservedMetadata = {};
75
+
76
+ // Notes
77
+ if ( sourceMetadata.noteId && ! result.attributes?.metadata?.noteId ) {
78
+ preservedMetadata.noteId = sourceMetadata.noteId;
79
+ }
80
+
81
+ // Custom name
82
+ if (
83
+ sourceMetadata.name &&
84
+ ! result.attributes?.metadata?.name &&
85
+ hasBlockSupport( result.name, 'renaming', true )
86
+ ) {
87
+ preservedMetadata.name = sourceMetadata.name;
88
+ }
89
+
90
+ // Block visibility
91
+ if (
92
+ sourceMetadata.blockVisibility !== undefined &&
93
+ ! result.attributes?.metadata?.blockVisibility &&
94
+ hasBlockSupport( result.name, 'blockVisibility', true )
95
+ ) {
96
+ preservedMetadata.blockVisibility = sourceMetadata.blockVisibility;
97
+ }
98
+
99
+ if ( Object.keys( preservedMetadata ).length > 0 ) {
100
+ return {
101
+ ...result,
102
+ attributes: {
103
+ ...result.attributes,
104
+ metadata: {
105
+ ...result.attributes.metadata,
106
+ ...preservedMetadata,
107
+ },
108
+ },
109
+ };
110
+ }
111
+ return result;
112
+ }
113
+
31
114
  addFilter(
32
115
  'blocks.registerBlockType',
33
116
  'core/metadata/addMetaAttribute',
34
117
  addMetaAttribute
35
118
  );
119
+
120
+ addFilter(
121
+ 'blocks.switchToBlockType.transformedBlock',
122
+ 'core/metadata/addTransforms',
123
+ addTransforms
124
+ );
@@ -0,0 +1,278 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import {
5
+ getBlockTypes,
6
+ registerBlockType,
7
+ unregisterBlockType,
8
+ } from '@wordpress/blocks';
9
+
10
+ /**
11
+ * Internal dependencies
12
+ */
13
+ import { addTransforms } from '../allowed-blocks';
14
+
15
+ describe( 'allowedBlocks', () => {
16
+ afterEach( () => {
17
+ getBlockTypes().forEach( ( block ) => {
18
+ unregisterBlockType( block.name );
19
+ } );
20
+ } );
21
+
22
+ describe( 'addTransforms()', () => {
23
+ it( 'should not preserve allowedBlocks in wrapping transforms', () => {
24
+ registerBlockType( 'core/bar', {
25
+ title: 'Bar',
26
+ supports: { allowedBlocks: true },
27
+ } );
28
+
29
+ const source = [
30
+ {
31
+ name: 'core/foo',
32
+ attributes: {
33
+ allowedBlocks: [ 'core/paragraph', 'core/heading' ],
34
+ },
35
+ innerBlocks: [],
36
+ },
37
+ {
38
+ name: 'core/foo',
39
+ attributes: { allowedBlocks: [ 'core/paragraph' ] },
40
+ innerBlocks: [],
41
+ },
42
+ ];
43
+ const result = {
44
+ name: 'core/bar',
45
+ attributes: {},
46
+ innerBlocks: source,
47
+ };
48
+
49
+ const transformed = addTransforms( result, source, 0, [ result ] );
50
+
51
+ expect( transformed.attributes.allowedBlocks ).toBeUndefined();
52
+ } );
53
+
54
+ it( 'should not preserve allowedBlocks in one-to-many transforms', () => {
55
+ registerBlockType( 'core/bar', {
56
+ title: 'Bar',
57
+ supports: { allowedBlocks: true },
58
+ } );
59
+
60
+ const source = [
61
+ {
62
+ name: 'core/foo',
63
+ attributes: {
64
+ allowedBlocks: [ 'core/paragraph', 'core/heading' ],
65
+ },
66
+ innerBlocks: [],
67
+ },
68
+ ];
69
+ const results = [
70
+ { name: 'core/bar', attributes: {}, innerBlocks: [] },
71
+ { name: 'core/bar', attributes: {}, innerBlocks: [] },
72
+ ];
73
+
74
+ const transformed = addTransforms(
75
+ results[ 0 ],
76
+ source,
77
+ 0,
78
+ results
79
+ );
80
+
81
+ expect( transformed.attributes.allowedBlocks ).toBeUndefined();
82
+ } );
83
+
84
+ it( 'should not preserve allowedBlocks in many-to-one transforms', () => {
85
+ registerBlockType( 'core/bar', {
86
+ title: 'Bar',
87
+ supports: { allowedBlocks: true },
88
+ } );
89
+
90
+ const source = [
91
+ {
92
+ name: 'core/foo',
93
+ attributes: {
94
+ allowedBlocks: [ 'core/paragraph', 'core/heading' ],
95
+ },
96
+ innerBlocks: [],
97
+ },
98
+ {
99
+ name: 'core/foo',
100
+ attributes: { allowedBlocks: [ 'core/paragraph' ] },
101
+ innerBlocks: [],
102
+ },
103
+ ];
104
+ const result = {
105
+ name: 'core/bar',
106
+ attributes: {},
107
+ innerBlocks: [],
108
+ };
109
+
110
+ const transformed = addTransforms( result, source, 0, [ result ] );
111
+
112
+ expect( transformed.attributes.allowedBlocks ).toBeUndefined();
113
+ } );
114
+
115
+ it( 'should not preserve allowedBlocks in many-to-many transforms with different counts', () => {
116
+ registerBlockType( 'core/bar', {
117
+ title: 'Bar',
118
+ supports: { allowedBlocks: true },
119
+ } );
120
+
121
+ const source = [
122
+ {
123
+ name: 'core/foo',
124
+ attributes: {
125
+ allowedBlocks: [ 'core/paragraph', 'core/heading' ],
126
+ },
127
+ innerBlocks: [],
128
+ },
129
+ {
130
+ name: 'core/foo',
131
+ attributes: { allowedBlocks: [ 'core/paragraph' ] },
132
+ innerBlocks: [],
133
+ },
134
+ {
135
+ name: 'core/foo',
136
+ attributes: { allowedBlocks: [ 'core/heading' ] },
137
+ innerBlocks: [],
138
+ },
139
+ ];
140
+ const results = [
141
+ { name: 'core/bar', attributes: {}, innerBlocks: [] },
142
+ { name: 'core/bar', attributes: {}, innerBlocks: [] },
143
+ ];
144
+
145
+ const [ firstTransformed, secondTransformed ] = results.map(
146
+ ( result, index ) =>
147
+ addTransforms( result, source, index, results )
148
+ );
149
+
150
+ expect( firstTransformed.attributes.allowedBlocks ).toBeUndefined();
151
+ expect(
152
+ secondTransformed.attributes.allowedBlocks
153
+ ).toBeUndefined();
154
+ } );
155
+
156
+ it( 'should preserve allowedBlocks in many-to-many transforms with same counts', () => {
157
+ registerBlockType( 'core/bar', {
158
+ title: 'Bar',
159
+ supports: { allowedBlocks: true },
160
+ } );
161
+
162
+ const source = [
163
+ {
164
+ name: 'core/foo',
165
+ attributes: {
166
+ allowedBlocks: [ 'core/image', 'core/heading' ],
167
+ },
168
+ innerBlocks: [],
169
+ },
170
+ {
171
+ name: 'core/foo',
172
+ attributes: { allowedBlocks: [ 'core/paragraph' ] },
173
+ innerBlocks: [],
174
+ },
175
+ ];
176
+ const results = [
177
+ { name: 'core/bar', attributes: {}, innerBlocks: [] },
178
+ { name: 'core/bar', attributes: {}, innerBlocks: [] },
179
+ ];
180
+
181
+ const [ firstTransformed, secondTransformed ] = results.map(
182
+ ( result, index ) =>
183
+ addTransforms( result, source, index, results )
184
+ );
185
+
186
+ expect( firstTransformed.attributes.allowedBlocks ).toEqual( [
187
+ 'core/image',
188
+ 'core/heading',
189
+ ] );
190
+ expect( secondTransformed.attributes.allowedBlocks ).toEqual( [
191
+ 'core/paragraph',
192
+ ] );
193
+ } );
194
+
195
+ it( "should filter allowedBlocks based on destination block's allowedBlocks", () => {
196
+ registerBlockType( 'core/bar', {
197
+ title: 'Bar',
198
+ supports: { allowedBlocks: true },
199
+ allowedBlocks: [ 'core/paragraph' ],
200
+ } );
201
+
202
+ const source = [
203
+ {
204
+ name: 'core/foo',
205
+ attributes: {
206
+ allowedBlocks: [
207
+ 'core/paragraph',
208
+ 'core/heading',
209
+ 'core/image',
210
+ ],
211
+ },
212
+ innerBlocks: [],
213
+ },
214
+ ];
215
+ const result = {
216
+ name: 'core/bar',
217
+ attributes: {},
218
+ innerBlocks: [],
219
+ };
220
+
221
+ const transformed = addTransforms( result, source, 0, [ result ] );
222
+
223
+ expect( transformed.attributes.allowedBlocks ).toEqual( [
224
+ 'core/paragraph',
225
+ ] );
226
+ } );
227
+
228
+ it( 'should not override existing allowedBlocks in target block', () => {
229
+ registerBlockType( 'core/bar', {
230
+ title: 'Bar',
231
+ supports: { allowedBlocks: true },
232
+ } );
233
+
234
+ const source = [
235
+ {
236
+ name: 'core/foo',
237
+ attributes: { allowedBlocks: [ 'core/paragraph' ] },
238
+ innerBlocks: [],
239
+ },
240
+ ];
241
+ const result = {
242
+ name: 'core/bar',
243
+ attributes: { allowedBlocks: [ 'core/heading' ] },
244
+ innerBlocks: [],
245
+ };
246
+
247
+ const transformed = addTransforms( result, source, 0, [ result ] );
248
+
249
+ expect( transformed.attributes.allowedBlocks ).toEqual( [
250
+ 'core/heading',
251
+ ] );
252
+ } );
253
+
254
+ it( 'should not preserve allowedBlocks when target block does not support allowedBlocks', () => {
255
+ registerBlockType( 'core/bar', {
256
+ title: 'Bar',
257
+ } );
258
+
259
+ const source = [
260
+ {
261
+ name: 'core/foo',
262
+ attributes: {
263
+ allowedBlocks: [ 'core/paragraph', 'core/heading' ],
264
+ },
265
+ innerBlocks: [],
266
+ },
267
+ ];
268
+ const result = {
269
+ name: 'core/bar',
270
+ attributes: {},
271
+ innerBlocks: [],
272
+ };
273
+ const transformed = addTransforms( result, source, 0, [ result ] );
274
+
275
+ expect( transformed.attributes.allowedBlocks ).toBeUndefined();
276
+ } );
277
+ } );
278
+ } );
@@ -0,0 +1,316 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import {
5
+ getBlockTypes,
6
+ registerBlockType,
7
+ unregisterBlockType,
8
+ } from '@wordpress/blocks';
9
+
10
+ /**
11
+ * Internal dependencies
12
+ */
13
+ import { addTransforms } from '../metadata';
14
+
15
+ describe( 'metadata', () => {
16
+ afterEach( () => {
17
+ getBlockTypes().forEach( ( block ) => {
18
+ unregisterBlockType( block.name );
19
+ } );
20
+ } );
21
+
22
+ describe( 'addTransforms()', () => {
23
+ it( 'should not preserve metadata in wrapping transforms', () => {
24
+ const source = [
25
+ {
26
+ name: 'core/foo',
27
+ attributes: { metadata: { noteId: 1 } },
28
+ innerBlocks: [],
29
+ },
30
+ {
31
+ name: 'core/foo',
32
+ attributes: { metadata: { noteId: 2 } },
33
+ innerBlocks: [],
34
+ },
35
+ ];
36
+ const result = {
37
+ name: 'core/bar',
38
+ attributes: {},
39
+ innerBlocks: source,
40
+ };
41
+ const transformed = addTransforms( result, source, 0, [ result ] );
42
+
43
+ expect( transformed.attributes.metadata ).toBeUndefined();
44
+ } );
45
+
46
+ it( 'should not preserve metadata in one-to-many transforms', () => {
47
+ const source = [
48
+ {
49
+ name: 'core/foo',
50
+ attributes: { metadata: { noteId: 1 } },
51
+ innerBlocks: [],
52
+ },
53
+ ];
54
+ const results = [
55
+ {
56
+ name: 'core/bar',
57
+ attributes: {},
58
+ innerBlocks: [],
59
+ },
60
+ {
61
+ name: 'core/bar',
62
+ attributes: {},
63
+ innerBlocks: [],
64
+ },
65
+ ];
66
+ const transformed = addTransforms(
67
+ results[ 0 ],
68
+ source,
69
+ 0,
70
+ results
71
+ );
72
+
73
+ expect( transformed.attributes.metadata ).toBeUndefined();
74
+ } );
75
+
76
+ it( 'should not preserve metadata in many-to-one transforms', () => {
77
+ const source = [
78
+ {
79
+ name: 'core/foo',
80
+ attributes: { metadata: { noteId: 1 } },
81
+ innerBlocks: [],
82
+ },
83
+ {
84
+ name: 'core/foo',
85
+ attributes: { metadata: { noteId: 2 } },
86
+ innerBlocks: [],
87
+ },
88
+ ];
89
+ const result = {
90
+ name: 'core/bar',
91
+ attributes: {},
92
+ innerBlocks: [],
93
+ };
94
+ const transformed = addTransforms( result, source, 0, [ result ] );
95
+
96
+ expect( transformed.attributes.metadata ).toBeUndefined();
97
+ } );
98
+
99
+ it( 'should not preserve metadata in many-to-many transforms with different counts', () => {
100
+ const source = [
101
+ {
102
+ name: 'core/foo',
103
+ attributes: { metadata: { noteId: 1 } },
104
+ innerBlocks: [],
105
+ },
106
+ {
107
+ name: 'core/foo',
108
+ attributes: { metadata: { noteId: 2 } },
109
+ innerBlocks: [],
110
+ },
111
+ {
112
+ name: 'core/foo',
113
+ attributes: { metadata: { noteId: 3 } },
114
+ innerBlocks: [],
115
+ },
116
+ ];
117
+ const results = [
118
+ {
119
+ name: 'core/bar',
120
+ attributes: {},
121
+ innerBlocks: [],
122
+ },
123
+ {
124
+ name: 'core/bar',
125
+ attributes: {},
126
+ innerBlocks: [],
127
+ },
128
+ ];
129
+ const [ firstTransformed, secondTransformed ] = results.map(
130
+ ( result, index ) =>
131
+ addTransforms( result, source, index, results )
132
+ );
133
+
134
+ expect( firstTransformed.attributes.metadata ).toBeUndefined();
135
+ expect( secondTransformed.attributes.metadata ).toBeUndefined();
136
+ } );
137
+
138
+ it( 'should preserve metadata in many-to-many transforms with same counts', () => {
139
+ const source = [
140
+ {
141
+ name: 'core/foo',
142
+ attributes: { metadata: { noteId: 1 } },
143
+ innerBlocks: [],
144
+ },
145
+ {
146
+ name: 'core/foo',
147
+ attributes: { metadata: { noteId: 2 } },
148
+ innerBlocks: [],
149
+ },
150
+ ];
151
+ const results = [
152
+ {
153
+ name: 'core/bar',
154
+ attributes: { metadata: { noteId: 1 } },
155
+ },
156
+ {
157
+ name: 'core/bar',
158
+ attributes: { metadata: { noteId: 2 } },
159
+ },
160
+ ];
161
+
162
+ const [ firstTransformed, secondTransformed ] = results.map(
163
+ ( result, index ) =>
164
+ addTransforms( result, source, index, results )
165
+ );
166
+
167
+ expect( firstTransformed.attributes.metadata ).toEqual( {
168
+ noteId: 1,
169
+ } );
170
+ expect( secondTransformed.attributes.metadata ).toEqual( {
171
+ noteId: 2,
172
+ } );
173
+ } );
174
+
175
+ it( 'should preserve custom name metadata', () => {
176
+ registerBlockType( 'core/bar', {
177
+ title: 'Bar',
178
+ } );
179
+ const source = [
180
+ {
181
+ name: 'core/foo',
182
+ attributes: {
183
+ metadata: { name: 'Custom Name' },
184
+ },
185
+ innerBlocks: [],
186
+ },
187
+ ];
188
+ const result = {
189
+ name: 'core/bar',
190
+ attributes: {},
191
+ innerBlocks: [],
192
+ };
193
+ const transformed = addTransforms( result, source, 0, [ result ] );
194
+
195
+ expect( transformed.attributes.metadata.name ).toBe(
196
+ 'Custom Name'
197
+ );
198
+ } );
199
+
200
+ it( 'should not preserve custom name metadata when target block does not support renaming', () => {
201
+ registerBlockType( 'core/bar', {
202
+ title: 'Bar',
203
+ supports: {
204
+ renaming: false,
205
+ },
206
+ } );
207
+
208
+ const source = [
209
+ {
210
+ name: 'core/foo',
211
+ attributes: {
212
+ metadata: { name: 'Custom Name' },
213
+ },
214
+ innerBlocks: [],
215
+ },
216
+ ];
217
+ const result = {
218
+ name: 'core/bar',
219
+ attributes: {},
220
+ innerBlocks: [],
221
+ };
222
+ const transformed = addTransforms( result, source, 0, [ result ] );
223
+
224
+ expect( transformed.attributes.metadata?.name ).toBeUndefined();
225
+ } );
226
+
227
+ it( 'should preserve block visibility metadata', () => {
228
+ registerBlockType( 'core/bar', {
229
+ title: 'Bar',
230
+ } );
231
+
232
+ const source = [
233
+ {
234
+ name: 'core/foo',
235
+ attributes: {
236
+ metadata: { blockVisibility: false },
237
+ },
238
+ innerBlocks: [],
239
+ },
240
+ ];
241
+ const result = {
242
+ name: 'core/bar',
243
+ attributes: {},
244
+ innerBlocks: [],
245
+ };
246
+ const transformed = addTransforms( result, source, 0, [ result ] );
247
+
248
+ expect( transformed.attributes.metadata?.blockVisibility ).toBe(
249
+ false
250
+ );
251
+ } );
252
+
253
+ it( 'should not preserve block visibility metadata when target block does not support it', () => {
254
+ registerBlockType( 'core/bar', {
255
+ title: 'Bar',
256
+ supports: {
257
+ blockVisibility: false,
258
+ },
259
+ } );
260
+
261
+ const source = [
262
+ {
263
+ name: 'core/foo',
264
+ attributes: {
265
+ metadata: { blockVisibility: false },
266
+ },
267
+ innerBlocks: [],
268
+ },
269
+ ];
270
+ const result = {
271
+ name: 'core/bar',
272
+ attributes: {},
273
+ innerBlocks: [],
274
+ };
275
+ const transformed = addTransforms( result, source, 0, [ result ] );
276
+
277
+ expect(
278
+ transformed.attributes.metadata?.blockVisibility
279
+ ).toBeUndefined();
280
+ } );
281
+
282
+ it( 'should not override existing metadata in target block', () => {
283
+ const source = [
284
+ {
285
+ name: 'core/foo',
286
+ attributes: {
287
+ metadata: {
288
+ noteId: 1,
289
+ name: 'Custom Name',
290
+ blockVisibility: false,
291
+ },
292
+ },
293
+ innerBlocks: [],
294
+ },
295
+ ];
296
+ const result = {
297
+ name: 'core/bar',
298
+ attributes: {
299
+ metadata: {
300
+ noteId: 2,
301
+ name: 'Existing Name',
302
+ blockVisibility: true,
303
+ },
304
+ },
305
+ innerBlocks: [],
306
+ };
307
+ const transformed = addTransforms( result, source, 0, [ result ] );
308
+
309
+ expect( transformed.attributes.metadata ).toEqual( {
310
+ noteId: 2,
311
+ name: 'Existing Name',
312
+ blockVisibility: true,
313
+ } );
314
+ } );
315
+ } );
316
+ } );