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

Unsupported Block #106

Merged
merged 25 commits into from
Aug 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
3c758d9
WIP. Trying to implement an unsupported block.
diegoreymendez Aug 7, 2018
c479a9d
WIP: trying to get unsupported blocks to save their data properly.
diegoreymendez Aug 7, 2018
2af4c94
Points Gutenberg to a branch of my own, not yet merged into master.
diegoreymendez Aug 8, 2018
e24805d
Merges the latest from master.
diegoreymendez Aug 8, 2018
0fca2bc
Updates Gutenberg to the latest from master.
diegoreymendez Aug 9, 2018
785fda5
WIP: trying to make it possible to convert unsupported blocks back to…
diegoreymendez Aug 9, 2018
7e180e8
Improves the aesthetics of the unsupported block.
diegoreymendez Aug 9, 2018
2233ab0
Adjusts some styling info.
diegoreymendez Aug 9, 2018
44e3480
Unsupported blocks are now rendered outside of the block holder.
diegoreymendez Aug 9, 2018
c65cd0a
Adds a documentation line and removes and unnecessary return expression.
diegoreymendez Aug 9, 2018
529debb
Merged the latest from master.
diegoreymendez Aug 22, 2018
2f6182b
Manually updates gutenberg ref.
diegoreymendez Aug 22, 2018
3840b60
Adds a missing closing bracket due to a bad merge.
diegoreymendez Aug 22, 2018
70d9e63
Resolved a bad merge.
diegoreymendez Aug 22, 2018
5fa5775
Makes some changes to the unsupported block code.
diegoreymendez Aug 22, 2018
86d07b8
Adds two files.
diegoreymendez Aug 22, 2018
8edbd1e
Parse all blocks using parse instead of individually
koke Aug 22, 2018
d4048ab
Revert supports from unsupported to what freeform has
koke Aug 22, 2018
7a59486
Unsupported blocks now render and switch to HTML mode just fine.
diegoreymendez Aug 22, 2018
d6a61d3
Removes some unnecessary code.
diegoreymendez Aug 22, 2018
1a2bfc0
Merge branch 'master' into issue/50-unsupported-render-serialize-back
koke Aug 23, 2018
279d80d
Tweak unsupported styles a bit
koke Aug 23, 2018
91eb3fd
Fix flow type annotation warning
koke Aug 23, 2018
d7e0790
Lint fixes
koke Aug 23, 2018
b215d2a
Don't show the unsupported block in the picker
koke Aug 23, 2018
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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"@wordpress/autop": "^1.0.6",
"@wordpress/compose": "^1.0.1",
"@wordpress/deprecated": "^1.0.0-alpha.2",
"@wordpress/element": "^1.0.2",
"@wordpress/hooks": "^1.2.1",
"@wordpress/i18n": "^1.1.0",
"@wordpress/is-shallow-equal": "^1.0.1",
Expand Down
55 changes: 32 additions & 23 deletions src/block-management/block-holder.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ import type { BlockType } from '../store/';
import styles from './block-holder.scss';

// Gutenberg imports
import { getBlockType } from '@wordpress/blocks';
import { getBlockType, getUnknownTypeHandlerName } from '@wordpress/blocks';

type PropsType = BlockType & {
onChange: ( clientId: string, attributes: mixed ) => void,
onToolbarButtonPressed: ( button: number, clientId: string ) => void,
onBlockHolderPressed: ( clientId: string ) => void,
};

type StateType = {
selected: boolean,
focused: boolean,
Expand Down Expand Up @@ -48,33 +49,41 @@ export default class BlockHolder extends React.Component<PropsType, StateType> {
}

getBlockForType() {
// Since unsupported blocks are handled in block-manager.js, at this point the block should definitely
// be supported.
const blockType = getBlockType( this.props.name );
if ( blockType ) {
const Block = blockType.edit;
const Block = blockType.edit;

let style;
if ( blockType.name === 'core/code' ) {
style = styles.block_code;
} else if ( blockType.name === 'core/paragraph' ) {
style = styles[ 'aztec_editor' ];
}
let style;
if ( blockType.name === 'core/code' ) {
style = styles.block_code;
} else if ( blockType.name === 'core/paragraph' ) {
style = styles[ 'aztec_editor' ];
}

// TODO: setAttributes needs to change the state/attributes
return (
<Block
attributes={ { ...this.props.attributes } }
// pass a curried version of onChanged with just one argument
setAttributes={ ( attrs ) =>
this.props.onChange( this.props.clientId, { ...this.props.attributes, ...attrs } )
}
isSelected={ this.props.focused }
style={ style }
/>
);
// TODO: setAttributes needs to change the state/attributes
return (
<Block
attributes={ { ...this.props.attributes } }
// pass a curried version of onChanged with just one argument
setAttributes={ ( attrs ) =>
this.props.onChange( this.props.clientId, { ...this.props.attributes, ...attrs } )
}
isSelected={ this.props.focused }
style={ style }
/>
);
}

getBlockType( blockName: String ) {
let blockType = getBlockType( blockName );

if ( ! blockType ) {
const fallbackBlockName = getUnknownTypeHandlerName();
blockType = getBlockType( fallbackBlockName );
}

// Default block placeholder
return <Text>{ this.props.attributes.content }</Text>;
return blockType;
}

render() {
Expand Down
26 changes: 26 additions & 0 deletions src/block-management/block-holder.scss
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,32 @@
padding-bottom: 4;
}

.unsupportedBlock {
background-color: #e9eff3; // grey lighten 30
padding-top: 8;
padding-bottom: 8;
padding-left: 8;
padding-right: 8;
}

.unsupportedBlockImagePlaceholder {
margin: auto;
text-align: center;
background-color: #4f748e; // dark grey
width: 20;
height: 20;
margin-top: 24;
margin-bottom: 8;
}

.unsupportedBlockMessage {
text-align: center;
color: #4f748e; // grey darken 20
padding-top: 24;
padding-bottom: 24;
font-size: 16px;
}

.aztec_container {
flex: 1;
}
Expand Down
13 changes: 7 additions & 6 deletions src/block-management/block-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ import { ToolbarButton } from './constants';
import type { BlockType } from '../store/';
import styles from './block-manager.scss';
import BlockPicker from './block-picker';

// Gutenberg imports
import { getBlockType, serialize, createBlock } from '@wordpress/blocks';
import {
createBlock,
serialize,
} from '@wordpress/blocks';

export type BlockListType = {
onChange: ( clientId: string, attributes: mixed ) => void,
Expand Down Expand Up @@ -135,14 +139,11 @@ export default class BlockManager extends React.Component<PropsType, StateType>
serializeToHtml() {
return this.props.blocks
.map( ( block ) => {
const blockType = getBlockType( block.name );
if ( blockType ) {
return serialize( [ block ] ) + '\n\n';
} else if ( block.name === 'aztec' ) {
if ( block.name === 'aztec' ) {
return '<aztec>' + block.attributes.content + '</aztec>\n\n';
}

return '<span>' + block.attributes.content + '</span>\n\n';
return serialize( [ block ] ) + '\n\n';
} )
.reduce( ( prevVal, value ) => {
return prevVal + value;
Expand Down
3 changes: 2 additions & 1 deletion src/block-management/block-picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import React, { Component } from 'react';
import { FlatList, Text, TouchableHighlight, View } from 'react-native';
import Modal from 'react-native-modal';
import styles from './block-picker.scss';
import { name as unsupportedBlockName } from '../block-types/unsupported-block';
// Gutenberg imports
import { getBlockTypes } from '@wordpress/blocks';

Expand All @@ -22,7 +23,7 @@ type StateType = {
};

export default class BlockPicker extends Component<PropsType, StateType> {
availableBlockTypes = getBlockTypes();
availableBlockTypes = getBlockTypes().filter( ( { name } ) => name !== unsupportedBlockName );

constructor( props: PropsType ) {
super( props );
Expand Down
32 changes: 32 additions & 0 deletions src/block-types/unsupported-block/edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* @format
* @flow
*/

import React from 'react';
import { View, Text } from 'react-native';
import type { BlockType } from '../../store/';

type PropsType = BlockType & {
onChange: ( clientId: string, attributes: mixed ) => void,
onToolbarButtonPressed: ( button: number, clientId: string ) => void,
onBlockHolderPressed: ( clientId: string ) => void,
};

type StateType = {
selected: boolean,
focused: boolean,
};

// Styles
import styles from '../../block-management/block-holder.scss';

export default class UnsupportedBlockEdit extends React.Component<PropsType, StateType> {
render() {
return (
<View style={ styles.unsupportedBlock }>
<Text style={ styles.unsupportedBlockMessage }>Unsupported</Text>
</View>
);
}
}
49 changes: 49 additions & 0 deletions src/block-types/unsupported-block/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* @format
* @flow
*/

import React from 'react';

// Gutenberg imports
import { __ } from '@wordpress/i18n';
import { RawHTML } from '@wordpress/element';

import edit from './edit';

type Attributes = {
content: String
}

export const name = 'gmobile/unsupported';

export const settings = {
title: __( 'Unsupported Block' ),

description: __( 'Unsupported block type.' ),

icon: 'editor-code',

category: 'formatting',

attributes: {
content: {
type: 'string',
source: 'html',
},
},

supports: {
className: false,
customClassName: false,
},

transforms: {
},

edit,

save( { attributes }: { attributes: Attributes } ) {
return <RawHTML>{ attributes.content }</RawHTML>;
},
};
79 changes: 34 additions & 45 deletions src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@

// Gutenberg imports
import { registerCoreBlocks } from '@wordpress/block-library';
import { parse } from '@wordpress/blocks';
import {
parse,
registerBlockType,
setUnknownTypeHandlerName,
} from '@wordpress/blocks';

import { createStore } from 'redux';
import { reducer } from './reducers';

import * as UnsupportedBlock from '../block-types/unsupported-block/';

export type BlockType = {
clientId: string,
name: string,
Expand All @@ -25,67 +31,50 @@ export type StateType = {
};

registerCoreBlocks();
registerBlockType( UnsupportedBlock.name, UnsupportedBlock.settings );
setUnknownTypeHandlerName( UnsupportedBlock.name );

const initialHtml = `
<!-- wp:title -->
Hello World
<!-- /wp:title -->

<!-- wp:heading {"level": 2} -->
<h2>Welcome to Gutenberg</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><b>Hello</b> World!</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph {"dropCap":true,"backgroundColor":"vivid-red","fontSize":"large","className":"custom-class-1 custom-class-2"} -->
<p class="has-background has-drop-cap has-large-font-size has-vivid-red-background-color custom-class-1 custom-class-2">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer tempor tincidunt sapien, quis dictum orci sollicitudin quis. Proin sed elit id est pulvinar feugiat vitae eget dolor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<!-- /wp:paragraph -->


const initialCodeBlockHtml = `
<!-- wp:code -->
<pre class="wp-block-code"><code>if name == "World":
return "Hello World"
else:
return "Hello Pony"</code></pre>
<!-- /wp:code -->
`;

const initialMoreBlockHtml = `
<!-- wp:more -->
<!--more-->
<!-- /wp:more -->
`;

const initialHeadingBlockHtml =
'<!-- wp:heading {"level": 2} --><h2>Welcome to Gutenberg</h2><!-- /wp:heading -->';
const initialParagraphBlockHtml =
'<!-- wp:paragraph --><p><b>Hello</b> World!</p><!-- /wp:paragraph -->';
const initialParagraphBlockHtml2 = `<!-- wp:paragraph {"dropCap":true,"backgroundColor":"vivid-red","fontSize":"large","className":"custom-class-1 custom-class-2"} -->
<p class="has-background has-drop-cap has-large-font-size has-vivid-red-background-color custom-class-1 custom-class-2">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer tempor tincidunt sapien, quis dictum orci sollicitudin quis. Proin sed elit id est pulvinar feugiat vitae eget dolor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p><!-- /wp:paragraph -->`;
<!-- wp:p4ragraph -->
Лорем ипсум долор сит амет, адиписци трацтатос еа еум. Меа аудиам малуиссет те, хас меис либрис елеифенд ин. Нец ех тота деленит сусципит. Яуас порро инструцтиор но нец.
<!-- /wp:p4ragraph -->
`;

const codeBlockInstance = parse( initialCodeBlockHtml )[ 0 ];
const moreBlockInstance = parse( initialMoreBlockHtml )[ 0 ];
const headingBlockInstance = parse( initialHeadingBlockHtml )[ 0 ];
const paragraphBlockInstance = parse( initialParagraphBlockHtml )[ 0 ];
const paragraphBlockInstance2 = parse( initialParagraphBlockHtml2 )[ 0 ];
const initialBlocks = parse( initialHtml );

export const initialState: StateType = {
// TODO: get blocks list block state should be externalized (shared with Gutenberg at some point?).
// If not it should be created from a string parsing (commented HTML to json).
blocks: [
{
clientId: '1',
name: 'title',
isValid: true,
attributes: {
content: 'Hello World',
},
innerBlocks: [],
focused: false,
},
{ ...headingBlockInstance, focused: false },
{ ...paragraphBlockInstance, focused: false },
{ ...paragraphBlockInstance2, focused: false },
{ ...codeBlockInstance, focused: false },
{ ...moreBlockInstance, focused: false },
{
clientId: '5',
name: 'paragraph',
isValid: true,
attributes: {
content:
'Лорем ипсум долор сит амет, адиписци трацтатос еа еум. Меа аудиам малуиссет те, хас меис либрис елеифенд ин. Нец ех тота деленит сусципит. Яуас порро инструцтиор но нец.',
},
innerBlocks: [],
focused: false,
},
],
blocks: initialBlocks.map( ( block ) => ( { ...block, focused: false } ) ),
refresh: false,
};

Expand Down
Loading