-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Port the core/code block to be used by the ReactNative mobile GB app #5816
Changes from 37 commits
850497c
43590b2
bf82dce
16b7eb5
20fd557
f922044
8ee1a4e
3e81e6b
9a2c431
0f3b9ea
0aaba3d
a3d5ab4
a29e5bc
e37c8bc
a6f8089
c190e24
c96b10b
685d178
f3e8a47
ad934ad
9b5a3f8
a2a1a04
95b17a2
737a498
1ab7542
49ceaa8
836c51e
ce3c5c4
33f9764
2bbcbc8
837f029
2a98232
d530570
eed0531
779c342
2a97aed
489ae72
0edfb18
9b68ba3
5c93331
0377c73
dfb1a12
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export { | ||
createBlock, | ||
} from './factory'; | ||
export { | ||
default as serialize, | ||
getBlockContent, | ||
} from './serializer'; | ||
export { | ||
registerBlockType, | ||
getBlockType, | ||
} from './registration'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// A "block" is the abstract term used to describe units of markup that, | ||
// when composed together, form the content or layout of a page. | ||
// The API for blocks is exposed via `wp.blocks`. | ||
// | ||
// Supported blocks are registered by calling `registerBlockType`. Once registered, | ||
// the block is made available as an option to the editor interface. | ||
// | ||
// Blocks are inferred from the HTML source of a post through a parsing mechanism | ||
// and then stored as objects in state, from which it is then rendered for editing. | ||
export * from './api'; | ||
|
||
export { default as PlainText } from './plain-text'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { TextInput } from 'react-native'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import styles from './style.scss'; | ||
|
||
function PlainText( { onChange, className, ...props } ) { | ||
return ( | ||
<TextInput | ||
className={ [ styles[ 'blocks-plain-text' ], className ] } | ||
onChangeText={ ( text ) => onChange( text ) } | ||
{ ...props } | ||
/> | ||
); | ||
} | ||
|
||
export default PlainText; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
.blocks-plain-text { | ||
box-shadow: none; | ||
|
||
border-width: 0; | ||
|
||
padding: 0; | ||
margin: 0; | ||
width: 100%; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import './editor.scss'; | ||
import { PlainText } from '@wordpress/blocks'; | ||
|
||
export function edit( { attributes, setAttributes, className } ) { | ||
return ( | ||
<div className={ className }> | ||
<PlainText | ||
value={ attributes.content } | ||
onChange={ ( content ) => setAttributes( { content } ) } | ||
placeholder={ __( 'Write code…' ) } | ||
aria-label={ __( 'Code' ) } | ||
/> | ||
</div> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { View } from 'react-native'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { PlainText } from '../../blocks'; | ||
|
||
export function edit( { attributes, setAttributes, style } ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It can be exported as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense, done with 0edfb18. |
||
return ( | ||
<View> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes and no. Unfortunately, styling in RN is not cascading by default and we will need more work on establishing a pattern on how to pass and style the blocks/components/containers. In this particular case for example, the style includes the All in all, styling should be considered There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That would be awesome to have it documented in the code next to RN version of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done with 0377c73. |
||
<PlainText | ||
value={ attributes.content } | ||
style={ style } | ||
multiline={ true } | ||
underlineColorAndroid="transparent" | ||
onChange={ ( content ) => setAttributes( { content } ) } | ||
placeholder={ __( 'Write code…' ) } | ||
aria-label={ __( 'Code' ) } | ||
/> | ||
</View> | ||
); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,13 +4,12 @@ | |
import { __ } from '@wordpress/i18n'; | ||
import { | ||
createBlock, | ||
PlainText, | ||
} from '@wordpress/blocks'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import './editor.scss'; | ||
import { edit } from './edit'; // import the specialized `edit` function. Is is different between the web and native mobile. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment can be removed as we should probably do the same for all
import edit from './edit';
// and later
{
...
edit,
...
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The comment can be considered trivial if one reads the PR and has an understanding of the |
||
|
||
export const name = 'core/code'; | ||
|
||
|
@@ -55,18 +54,7 @@ export const settings = { | |
], | ||
}, | ||
|
||
edit( { attributes, setAttributes, className } ) { | ||
return ( | ||
<div className={ className }> | ||
<PlainText | ||
value={ attributes.content } | ||
onChange={ ( content ) => setAttributes( { content } ) } | ||
placeholder={ __( 'Write code…' ) } | ||
aria-label={ __( 'Code' ) } | ||
/> | ||
</div> | ||
); | ||
}, | ||
edit: edit, | ||
|
||
save( { attributes } ) { | ||
return <pre><code>{ attributes.content }</code></pre>; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { | ||
registerBlockType, | ||
} from '@wordpress/blocks'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import * as code from './code'; | ||
|
||
export const registerCoreBlocks = () => { | ||
[ | ||
code, | ||
].forEach( ( { name, settings } ) => { | ||
registerBlockType( name, settings ); | ||
} ); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { | ||
createElement, | ||
Component, | ||
} from 'react'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import serialize from './serialize'; | ||
|
||
/** | ||
* Returns a new element of given type. Type can be either a string tag name or | ||
* another function which itself returns an element. | ||
* | ||
* @param {?(string|Function)} type Tag name or element creator | ||
* @param {Object} props Element properties, either attribute | ||
* set to apply to DOM node or values to | ||
* pass through to element creator | ||
* @param {...WPElement} children Descendant elements | ||
* | ||
* @return {WPElement} Element. | ||
*/ | ||
export { createElement }; | ||
|
||
/** | ||
* A base class to create WordPress Components (Refs, state and lifecycle hooks) | ||
*/ | ||
export { Component }; | ||
|
||
/** | ||
* Renders a given element into a string. | ||
* | ||
* @param {WPElement} element Element to render | ||
* | ||
* @return {string} HTML. | ||
*/ | ||
export { serialize as renderToString }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you check if it isn't included in web build by accident?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not extensively, I didn't. What I only tested was modifying values in the style file and see if GB-web picks those up, visually. Tried out adding a
background-color
for example. Any practical way to test it out more thoroughly perhaps?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can test it myself before merging, but I think if you put
background-color: pink
in the RN version, it shouldn't get applied in the web version. That's all we need to ensure :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's grand, that was the test I did and it succeeded (color wasn't picked up) so, feel free to test is your side as well, thanks!