Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RNMobile] Shortcode block support #19534

Merged
merged 14 commits into from
Jan 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/block-library/src/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ export const registerCoreBlocks = () => {
gallery,
devOnly( group ),
spacer,
shortcode,
].forEach( registerBlock );

setDefaultBlockName( paragraph.name );
Expand Down
10 changes: 10 additions & 0 deletions packages/block-library/src/shortcode/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "core/shortcode",
"category": "widgets",
"attributes": {
"text": {
"type": "string",
"source": "html"
}
}
}
45 changes: 45 additions & 0 deletions packages/block-library/src/shortcode/edit.native.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* External dependencies
*/
import { View, Text } from 'react-native';

/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { PlainText } from '@wordpress/block-editor';
import { withPreferredColorScheme } from '@wordpress/compose';

/**
* Internal dependencies
*/
koke marked this conversation as resolved.
Show resolved Hide resolved

import styles from './style.scss';

export function ShortcodeEdit( props ) {
const { attributes, setAttributes, onFocus, onBlur, getStylesFromColorScheme } = props;
const titleStyle = getStylesFromColorScheme( styles.blockTitle, styles.blockTitleDark );
const shortcodeStyle = getStylesFromColorScheme( styles.blockShortcode, styles.blockShortcodeDark );
const placeholderStyle = getStylesFromColorScheme( styles.placeholder, styles.placeholderDark );

return (
<View>
<Text style={ titleStyle } >{ __( 'Shortcode' ) }</Text>
<PlainText
value={ attributes.text }
style={ shortcodeStyle }
multiline={ true }
underlineColorAndroid="transparent"
onChange={ ( text ) => setAttributes( { text } ) }
placeholder={ __( 'Add a shortcode…' ) }
aria-label={ __( 'Shortcode' ) }
isSelected={ props.isSelected }
onFocus={ onFocus }
onBlur={ onBlur }
placeholderTextColor={ placeholderStyle.color }
/>
</View>
);
}

export default withPreferredColorScheme( ShortcodeEdit );
6 changes: 4 additions & 2 deletions packages/block-library/src/shortcode/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ import edit from './edit';
import icon from './icon';
import save from './save';
import transforms from './transforms';
import metadata from './block.json';

export const name = 'core/shortcode';
const { name } = metadata;

export { metadata, name };

export const settings = {
title: __( 'Shortcode' ),
description: __( 'Insert additional custom elements with a WordPress shortcode.' ),
icon,
category: 'widgets',
transforms,
supports: {
customClassName: false,
Expand Down
9 changes: 3 additions & 6 deletions packages/block-library/src/shortcode/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,12 @@ function render_block_core_shortcode( $attributes, $content ) {
* Registers the `core/shortcode` block on server.
*/
function register_block_core_shortcode() {
$path = __DIR__ . '/shortcode.json';
$metadata = json_decode( file_get_contents( $path ), true );
register_block_type(
'core/shortcode',
array(
'attributes' => array(
'text' => array(
'type' => 'string',
'source' => 'html',
),
),
'attributes' => $metadata['attributes'],
'render_callback' => 'render_block_core_shortcode',
)
);
Expand Down
37 changes: 37 additions & 0 deletions packages/block-library/src/shortcode/style.native.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
.blockTitle {
font-family: $default-regular-font;
font-weight: 500;
font-size: 10px;
color: rgba($black, 0.6);
text-transform: uppercase;
letter-spacing: 0.4px;
}

.blockTitleDark {
color: rgba($white, 0.6);
}

.blockShortcode {
font-family: $default-monospace-font;
font-weight: 400;
font-size: 14px;
padding: 12px;
border-radius: 4px;
background-color: $gray-light;
}

.blockShortcodeDark {
color: $white;
background-color: $gray-100;
}

.placeholder {
font-family: $default-monospace-font;
font-weight: 400;
font-size: 14px;
color: rgba($black, 0.25);
}

.placeholderDark {
color: $gray-50;
}
26 changes: 26 additions & 0 deletions packages/block-library/src/shortcode/test/edit.native.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* External dependencies
*/
import renderer from 'react-test-renderer';

/**
* Internal dependencies
*/
import Shortcode from '../edit';
import { TextInput } from 'react-native';

describe( 'Shortcode', () => {
it( 'renders without crashing', () => {
const component = renderer.create( <Shortcode attributes={ { text: '' } } /> );
const rendered = component.toJSON();
expect( rendered ).toBeTruthy();
} );

it( 'renders given text without crashing', () => {
const component = renderer.create( <Shortcode attributes={ { text: '[youtube https://www.youtube.com/watch?v=ssfHW5lwFZg]' } } /> );
const testInstance = component.root;
const textInput = testInstance.findByType( TextInput );
expect( textInput ).toBeTruthy();
expect( textInput.props.value ).toBe( '[youtube https://www.youtube.com/watch?v=ssfHW5lwFZg]' );
} );
} );
5 changes: 2 additions & 3 deletions test/integration/shortcode-converter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,8 @@ describe( 'segmentHTMLToShortcodeBlock', () => {

it( 'should not convert a shortcode to a block type with a failing `isMatch`', () => {
const original = `<p>[my-broccoli id="1000"]</p>`;

const transformed = segmentHTMLToShortcodeBlock( original, 0 );
const expectedBlock = createBlock( 'core/shortcode' );
const expectedBlock = createBlock( 'core/shortcode', { text: '[my-broccoli id="1000"]' } );
expectedBlock.clientId = transformed[ 1 ].clientId;
expect( transformed[ 1 ] ).toEqual( expectedBlock );
} );
Expand All @@ -146,7 +145,7 @@ describe( 'segmentHTMLToShortcodeBlock', () => {
firstExpectedBlock.clientId = transformed[ 1 ].clientId;
const secondExpectedBlock = createBlock( 'test/broccoli', { id: 42 } );
secondExpectedBlock.clientId = transformed[ 3 ].clientId;
const thirdExpectedBlock = createBlock( 'core/shortcode' );
const thirdExpectedBlock = createBlock( 'core/shortcode', { text: '[my-broccoli id="1000"]' } );
thirdExpectedBlock.clientId = transformed[ 5 ].clientId;
expect( transformed[ 1 ] ).toEqual( firstExpectedBlock );
expect( transformed[ 3 ] ).toEqual( secondExpectedBlock );
Expand Down
7 changes: 7 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,13 @@ module.exports = {
},
},
] ),
new CopyWebpackPlugin( [
{
from: './packages/block-library/src/+(shortcode)/block.json',
test: new RegExp( `([\\w-]+)${ escapeRegExp( sep ) }block\\.json$` ),
to: 'build/block-library/blocks/[1].json',
},
] ),
new DependencyExtractionWebpackPlugin( { injectPolyfill: true } ),
],
devtool,
Expand Down