1
1
import { createElement , Fragment , ReactElement } from "@wordpress/element" ;
2
- import { registerBlockType , BlockConfiguration , BlockEditProps , BlockSaveProps } from "@wordpress/blocks" ;
3
- import { InspectorControls } from "@wordpress/block-editor" ;
2
+ import {
3
+ registerBlockType ,
4
+ BlockConfiguration ,
5
+ BlockEditProps ,
6
+ BlockSaveProps ,
7
+ } from "@wordpress/blocks" ;
8
+ import { InspectorControls , BlockIcon } from "@wordpress/block-editor" ;
4
9
import BlockInstruction from "./BlockInstruction" ;
5
10
import Definition from "../Definition" ;
6
11
import BlockRootLeaf from "../../leaves/blocks/BlockRootLeaf" ;
@@ -87,6 +92,10 @@ export default class BlockDefinition extends Definition {
87
92
88
93
logger . debug ( "registering block " + name ) ;
89
94
95
+ if ( configuration . icon && typeof configuration . icon === "string" && configuration . icon . startsWith ( "<svg" ) ) {
96
+ configuration . icon = this . createBlockIcon ( configuration ) ;
97
+ }
98
+
90
99
// Register the block to WordPress.
91
100
registerBlockType ( name , configuration ) ;
92
101
// Register the block with our own code.
@@ -105,4 +114,20 @@ export default class BlockDefinition extends Definition {
105
114
. map ( ( instruction , index ) => instruction . sidebar ( props , index ) )
106
115
. filter ( e => e !== null ) ;
107
116
}
117
+
118
+ /**
119
+ * Creates an block icon.
120
+ *
121
+ * @param {MutableBlockConfiguration } configuration The block configuration.
122
+ *
123
+ * @returns {ReactElement[] } The sidebar element to render.
124
+ */
125
+ private createBlockIcon ( configuration : MutableBlockConfiguration ) : ReactElement {
126
+ const icon = < span
127
+ className = "yoast-schema-blocks-icon"
128
+ dangerouslySetInnerHTML = { { __html : configuration . icon as string } }
129
+ /> ;
130
+
131
+ return < BlockIcon icon = { icon } /> ;
132
+ }
108
133
}
0 commit comments