Skip to content

Commit

Permalink
update the TinyMCE per Block prototype UI to match the Prototype UI s…
Browse files Browse the repository at this point in the history
…tyle. Also make it keyboard friendly (#136)

* update the TinyMCE per Block prototype UI to match the Prototype UI style
* Adding heading block
* Adding a paragraph block and moving to React
 - preact has some state updating bugs
* Allowing removing/merging components using backspace
* Handling navigation using arrow keys
* Adding a simple quote  block
  • Loading branch information
youknowriad authored Feb 28, 2017
1 parent b1d9b8f commit 01f2c86
Show file tree
Hide file tree
Showing 37 changed files with 2,991 additions and 2,171 deletions.
42 changes: 34 additions & 8 deletions tinymce-per-block/build/app.js

Large diffs are not rendered by default.

8 changes: 3 additions & 5 deletions tinymce-per-block/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@
<title>Blocks</title>
</head>
<body>
<div class="renderers">
<div id="html"></div>
<div id="block"></div>
</div>
<div class="editor"></div>
<script>
window.content = '<!-- wp:text -->A beautiful thing about Apple is how quickly they obsolete their own products. I imagine this also makes the discipline of getting things out there easier. Like I mentioned before, the longer it’s been since the last release the more pressure there is, but if you know that if your bit of code doesn’t make this version but there’s the +0.1 coming out in 6 weeks, then it’s not that bad. It’s like flights from San Francisco to LA, if you miss one you know there’s another one an hour later so it’s not a big deal. Amazon has done a fantastic job of this with the Kindle as well, with a new model every year.<!-- /wp -->\n\n<!-- wp:text -->I like Apple for the opposite reason: <strong>they’re not afraid of getting a rudimentary 1.0 out into the world</strong>.<!-- /wp -->\n\n<!-- wp:image --><img src="https://matiasventura.files.wordpress.com/2017/02/blue.png?w=720"><!-- /wp -->';
window.content = '<!-- wp:heading -->1.0 Is The Loneliest Number<!-- /wp --><!-- wp:quote -->Design is not just what it looks like and feels like. Design is how it works. Steve Jobs<!-- /wp --><!-- wp:paragraph -->Many entrepreneurs idolize Steve Jobs. He’s such a perfectionist , they say. Nothing leaves the doors of 1 Infinite Loop in Cupertino without a polish and finish that makes geeks everywhere drool. No compromise!<!-- /wp --><!-- wp:text -->A beautiful thing about Apple is how quickly they obsolete their own products. I imagine this also makes the discipline of getting things out there easier. Like I mentioned before, the longer it’s been since the last release the more pressure there is, but if you know that if your bit of code doesn’t make this version but there’s the +0.1 coming out in 6 weeks, then it’s not that bad. It’s like flights from San Francisco to LA, if you miss one you know there’s another one an hour later so it’s not a big deal. Amazon has done a fantastic job of this with the Kindle as well, with a new model every year.<!-- /wp -->\n\n<!-- wp:text -->I like Apple for the opposite reason: <strong>they’re not afraid of getting a rudimentary 1.0 out into the world</strong>.<!-- /wp --><!-- wp:image --><img src="https://matiasventura.files.wordpress.com/2017/02/blue.png?w=720"><!-- /wp --><!-- wp:paragraph -->Many entrepreneurs idolize Steve Jobs. He’s such a perfectionist , they say. Nothing leaves the doors of 1 Infinite Loop in Cupertino without a polish and finish that makes geeks everywhere drool. No compromise!<!-- /wp -->';
</script>

<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Merriweather:300,300i,400,400i,700,700i">
<script src="https://cdnjs.cloudflare.com/ajax/libs/tinymce/4.5.3/tinymce.min.js"></script>
<script src="build/app.js"></script>
Expand Down
4 changes: 3 additions & 1 deletion tinymce-per-block/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
"classnames": "^2.2.5",
"is-equal-shallow": "^0.1.3",
"lodash": "^4.17.4",
"preact": "^7.1.0"
"react": "^15.4.2",
"react-dom": "^15.4.2",
"react-textarea-autosize": "^4.0.5"
}
}
73 changes: 42 additions & 31 deletions tinymce-per-block/src/assets/stylesheets/main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,62 @@
@import 'animations';
@import '~blocks/text-block/style';
@import '~blocks/image-block/style';
@import '~blocks/heading-block/style';
@import '~blocks/paragraph-block/style';
@import '~blocks/quote-block/style';
@import '~external/dashicons/style';
@import '~renderers/block/block-list/style';
@import '~renderers/html/html-editor/style';

* {
box-sizing: border-box;
}

html {
height: 100%;
font-size: 10px;
}

body {
margin: 4vh auto;
min-height: 92vh;
width: 90%;
max-width: 1280px;
-webkit-font-smoothing: antialiased !important;
height: 100%;
}

html,
body {
margin: 0;
padding: 0;
height: 100%;
}

body {
font: 13px/1.8 -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen-Sans", "Ubuntu", "Cantarell", "Helvetica Neue", sans-serif;
max-width: 720px;
margin: 60px auto;
color: #12181e;
}

.editor {
height: 100%;
}

.toggle-renderer {
position: absolute;
top: -20px;
right: 0;
}

.renderers {
position: relative;
display: flex;
min-height: 100%;
height: 100%;
}

&::after {
content: '';
position: absolute;
.renderer-html {
z-index: 10;
position: absolute;
top: 0;
bottom: 0;
left: 50%;
margin-left: -2px;
width: 4px;
background-color: $gray-light;
}

> #html,
> #block {
flex-basis: 50%;
margin: 0 8rem;
padding: 4vh 0;

&:first-child {
margin-left: 0;
}

&:last-child {
margin-right: 0;
}
}
width: 100%;
}

.mce-content-body:focus {
outline: 0;
}
28 changes: 28 additions & 0 deletions tinymce-per-block/src/blocks/heading-block/_style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
.heading-block__form {
textarea, .textarea-mirror {
width: 100%;
border: none;
font-family: Merriweather, Georgia, "Times New Roman", Times, serif;
resize: none;
color: inherit;
font-size: inherit;
font-weight: bold;
overflow: hidden;

&:focus {
outline: 0;
}
}

&.h1 textarea, &.h1 .textarea-mirror {
font-size: 34px;
}

&.h2 textarea, &.h2 .textarea-mirror {
font-size: 28px;
}

&.h3 textarea, &.h3 .textarea-mirror {
font-size: 20px;
}
}
84 changes: 84 additions & 0 deletions tinymce-per-block/src/blocks/heading-block/form.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/**
* External dependencies
*/
import { createElement, Component } from 'wp-elements';

/**
* Internal dependencies
*/
import { serialize } from 'serializers/block';
import { EnhancedInputComponent } from 'wp-blocks';

export default class HeadingBlockForm extends Component {
bindInput = ( ref ) => {
this.input = ref;
}

focus = ( position ) => {
this.input.focus( position );
}

merge = ( block, index ) => {
const acceptedBlockTypes = [ 'quote', 'paragraph', 'heading' ];
if ( acceptedBlockTypes.indexOf( block.blockType ) === -1 ) {
return;
}

const { block: { children }, focus, remove, setChildren } = this.props;
const value = serialize( children );
setChildren( children.concat( block.children ) );
remove( index );
focus( value.length );
}

render() {
const { block, setChildren, appendBlock, mergeWithPrevious, remove, moveUp, moveDown } = this.props;
const { children } = block;
const value = serialize( children );
const onChangeContent = ( event ) => {
setChildren( [ {
type: 'Text',
value: event.target.value || ' ' // grammar bug
} ] );
};
const splitValue = ( left, right ) => {
setChildren( [ {
type: 'Text',
value: left || ' ' // grammar bug
} ] );
if ( right ) {
appendBlock( {
...block,
children: [
{
type: 'Text',
value: right
}
]
} );
} else {
appendBlock();
}
};
const removePrevious = () => {
if ( value && value !== ' ' ) {
mergeWithPrevious();
} else {
remove();
}
};
const className = block.attrs.size ? block.attrs.size : 'h2';

return (
<div className={ `heading-block__form ${ className }` }>
<EnhancedInputComponent ref={ this.bindInput } value={ value }
onChange={ onChangeContent }
splitValue={ splitValue }
removePrevious={ removePrevious }
moveUp={ moveUp }
moveDown={ moveDown }
/>
</div>
);
}
}
53 changes: 53 additions & 0 deletions tinymce-per-block/src/blocks/heading-block/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* External dependencies
*/
import { registerBlock } from 'wp-blocks';
import {
EditorHeadingIcon,
EditorHeading1Icon,
EditorHeading2Icon,
EditorHeading3Icon
} from 'dashicons';

/**
* Internal dependencies
*/
import form from './form';

registerBlock( 'heading', {
title: 'Heading',
form: form,
icon: EditorHeadingIcon,
controls: [
{
label: 'H1',
icon: EditorHeading1Icon,
isSelected: ( { attrs } ) => 'h1' === attrs.size,
onClick( { setAttributes } ) {
setAttributes( {
size: 'h1'
} );
}
},
{
label: 'H2',
icon: EditorHeading2Icon,
isSelected: ( { attrs } ) => ! attrs.size || 'h2' === attrs.size,
onClick( { setAttributes } ) {
setAttributes( {
size: 'h2'
} );
}
},
{
label: 'H3',
icon: EditorHeading3Icon,
isSelected: ( { attrs } ) => 'h3' === attrs.size,
onClick( { setAttributes } ) {
setAttributes( {
size: 'h3'
} );
}
}
]
} );
36 changes: 26 additions & 10 deletions tinymce-per-block/src/blocks/image-block/form.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
/**
* External dependencies
*/
import { createElement } from 'wp-elements';
import { createElement, Component } from 'wp-elements';
import { find } from 'lodash';

export default function ImageBlockForm( { children } ) {
const image = find( children, ( { name } ) => 'img' === name );
if ( ! image ) {
return null;
export default class ImageBlockForm extends Component {

merge() {
this.props.remove();
}

focus( position ) {
if ( position !== 0 ) {
this.props.moveUp();
} else {
this.props.moveDown();
}
}

return (
<img
src={ image.attrs.src }
className="image-block__display" />
);
render() {
const { block: { children } } = this.props;
const image = find( children, ( { name } ) => 'img' === name );
if ( ! image ) {
return null;
}

return (
<img
src={ image.attrs.src }
className="image-block__display" />
);
}
}
14 changes: 14 additions & 0 deletions tinymce-per-block/src/blocks/paragraph-block/_style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.paragraph-block__form textarea, .paragraph-block__form .textarea-mirror {
width: 100%;
border: none;
font: inherit;
font-family: "Merriweather", serif;
font-size: 16px;
color: inherit;
font-weight: 300;
resize: none;

&:focus {
outline: 0;
}
}
Loading

0 comments on commit 01f2c86

Please sign in to comment.