Skip to content

Commit 3840538

Browse files
committed
Move getSerializedBlocks to blocks.js
1 parent b3dd220 commit 3840538

File tree

2 files changed

+68
-46
lines changed

2 files changed

+68
-46
lines changed

src/utils/blocks.js

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/**
22
* WordPress dependencies
33
*/
4-
import { getBlockTypes, unregisterBlockType } from '@wordpress/blocks';
4+
import { unregisterBlockType } from '@wordpress/blocks';
5+
import { renderToString } from '@wordpress/element';
56
import { debug } from './logger';
67

78
/**
@@ -24,3 +25,66 @@ export function unregisterDisallowedBlocks( allowedBlockTypes ) {
2425

2526
debug( 'Blocks unregistered:', unregisteredBlocks );
2627
}
28+
29+
/**
30+
* Extract and serialize a block's icon.
31+
*
32+
* @param {Object} blockType The block type object.
33+
*
34+
* @return {string|null} The serialized icon string or null.
35+
*/
36+
function getBlockIcon( blockType ) {
37+
if ( ! blockType.icon ) {
38+
return null;
39+
}
40+
41+
let iconSource = blockType.icon;
42+
43+
// If icon is an object with src property, extract src
44+
if ( typeof iconSource === 'object' && iconSource.src ) {
45+
iconSource = iconSource.src;
46+
}
47+
48+
// Convert React element to SVG string
49+
if (
50+
typeof iconSource === 'object' &&
51+
iconSource !== null &&
52+
typeof iconSource.type !== 'undefined'
53+
) {
54+
try {
55+
return renderToString( iconSource );
56+
} catch ( error ) {
57+
// If rendering fails, ignore the icon
58+
debug(
59+
`Failed to render icon for block ${ blockType.name }`,
60+
error
61+
);
62+
return null;
63+
}
64+
}
65+
66+
else if ( typeof iconSource === 'string' ) {
67+
return iconSource;
68+
}
69+
70+
return null;
71+
}
72+
73+
/**
74+
* Get serialized block data for all registered block types.
75+
*
76+
* @return {Array} Array of serialized block objects.
77+
*/
78+
export function getSerializedBlocks() {
79+
const { getBlockTypes } = await import( '@wordpress/blocks' );
80+
return getBlockTypes().map( ( blockType ) => {
81+
return {
82+
name: blockType.name,
83+
title: blockType.title,
84+
description: blockType.description,
85+
category: blockType.category,
86+
keywords: blockType.keywords || [],
87+
icon: getBlockIcon( blockType ),
88+
};
89+
} );
90+
}

src/utils/bridge.js

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import parseException from './exception-parser';
55
import { debug } from './logger';
66
import { isDevMode } from './dev-mode';
77
import { basicFetch } from './fetch';
8+
import { getSerializedBlocks } from './blocks';
89

910
/**
1011
* Generic function to dispatch messages to both Android and iOS bridges.
@@ -91,51 +92,8 @@ export function onBlocksChanged( isEmpty = false ) {
9192
*
9293
* @return {void}
9394
*/
94-
export async function showBlockInserter() {
95-
// Lazy-load getBlockTypes to defer the import until this function is called.
96-
// In the remote editor, dependencies are loaded asynchronously, so this ensures
97-
// window.wp.blocks is defined before we access it.
98-
const { getBlockTypes } = await import( '@wordpress/blocks' );
99-
const blocks = getBlockTypes().map( ( blockType ) => {
100-
// Extract and serialize icon
101-
let icon = null;
102-
if ( blockType.icon ) {
103-
let iconSource = blockType.icon;
104-
105-
// If icon is an object with src property, extract src
106-
if ( typeof iconSource === 'object' && iconSource.src ) {
107-
iconSource = iconSource.src;
108-
}
109-
110-
// Convert React element to SVG string
111-
if (
112-
typeof iconSource === 'object' &&
113-
iconSource !== null &&
114-
typeof iconSource.type !== 'undefined'
115-
) {
116-
try {
117-
icon = renderToString( iconSource );
118-
} catch ( error ) {
119-
// If rendering fails, ignore the icon
120-
debug(
121-
`Failed to render icon for block ${ blockType.name }`,
122-
error
123-
);
124-
}
125-
} else if ( typeof iconSource === 'string' ) {
126-
icon = iconSource;
127-
}
128-
}
129-
130-
return {
131-
name: blockType.name,
132-
title: blockType.title,
133-
description: blockType.description,
134-
category: blockType.category,
135-
keywords: blockType.keywords || [],
136-
icon,
137-
};
138-
} );
95+
export function showBlockInserter() {
96+
const blocks = getSerializedBlocks();
13997
dispatchToBridge( 'showBlockInserter', { blocks } );
14098
}
14199

0 commit comments

Comments
 (0)