Skip to content

Commit

Permalink
refactor(iframe): manage block attributes from shared json file
Browse files Browse the repository at this point in the history
  • Loading branch information
kariae committed Oct 12, 2021
1 parent 76f9021 commit d485551
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 34 deletions.
22 changes: 22 additions & 0 deletions src/blocks/iframe/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "iframe",
"category": "newspack",
"attributes": {
"src": {
"type": "string",
"default": ""
},"archiveFolder": {
"type": "string",
"default": ""
},"height": {
"type": "string",
"default": "600px"
},"width": {
"type": "string",
"default": "100%"
},"isFullScreen": {
"type": "boolean",
"default": false
}
}
}
30 changes: 7 additions & 23 deletions src/blocks/iframe/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,27 @@ import { __ } from '@wordpress/i18n';
* Internal dependencies
*/
import edit from './edit';
import metadata from './block.json';
const { name, attributes, category } = metadata;

/**
* Style dependencies - will load in editor
*/
import './editor.scss';
import { iframeIcon } from './icons';

export const name = 'iframe';
export const title = __( 'Iframe', 'newspack-blocks' );

// Name must be exported separately.
export { name };

export const settings = {
title,
icon: {
src: iframeIcon,
foreground: '#36f',
},
category: 'newspack',
category,
keywords: [ __( 'iframe', 'newspack-blocks' ), __( 'project iframe', 'newspack-blocks' ) ],
description: (
<>
Expand All @@ -34,27 +38,7 @@ export const settings = {
</ExternalLink>
</>
),
attributes: {
src: {
type: 'string',
},
archiveFolder: {
type: 'string',
default: '',
},
height: {
type: 'string',
default: '600px',
},
width: {
type: 'string',
default: '100%',
},
isFullScreen: {
type: 'boolean',
default: false,
},
},
attributes,
supports: {
html: false,
align: true,
Expand Down
20 changes: 9 additions & 11 deletions src/blocks/iframe/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,17 @@ function newspack_blocks_render_block_iframe( $attributes ) {
* Registers the `newspack-blocks/iframe` block on server.
*/
function newspack_blocks_register_iframe() {
$block_json = json_decode(
file_get_contents( __DIR__ . '/block.json' ), // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
true
);

register_block_type(
'newspack-blocks/iframe',
array(
'attributes' => array(
'src' => array( 'type' => 'string' ),
'archiveFolder' => array( 'type' => 'string' ),
'height' => array( 'type' => 'string' ),
'width' => array( 'type' => 'string' ),
'isFullScreen' => array( 'type' => 'boolean' ),
),
'newspack-blocks/' . $block_json['name'],
[
'attributes' => $block_json['attributes'],
'render_callback' => 'newspack_blocks_render_block_iframe',
'supports' => [],
)
]
);
}
add_action( 'init', 'newspack_blocks_register_iframe' );
Expand Down

0 comments on commit d485551

Please sign in to comment.