Skip to content

Commit

Permalink
TinyMCE per block: Adding the possibility to trigger the inserter men…
Browse files Browse the repository at this point in the history
…u on empty blocks (#199)
  • Loading branch information
youknowriad authored Mar 7, 2017
1 parent e60a2e8 commit 71c98b8
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 16 deletions.
22 changes: 11 additions & 11 deletions tinymce-per-block/build/app.js

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions tinymce-per-block/src/blocks/text-block/_style.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.text-block__form {
position: relative;
width: 100%;
border: none;
font: inherit;
Expand All @@ -10,4 +11,10 @@
padding: 0;
margin: 0;
}

.inserter__button {
position: absolute;
left: -50px;
margin: 0;
}
}
8 changes: 6 additions & 2 deletions tinymce-per-block/src/blocks/text-block/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { createElement, Component } from 'wp-elements';
import EditableFormatToolbar from 'controls/editable-format-toolbar';
import AlignmentToolbar from 'controls/alignment-toolbar';
import BlockArrangement from 'controls/block-arrangement';
import InlineTextBlockForm from '../inline-text-block/form';
import InlineTextBlockForm from 'blocks/inline-text-block/form';
import InserterButton from 'inserter/button';

export default class TextBlockForm extends Component {
bindForm = ( ref ) => {
Expand All @@ -27,7 +28,7 @@ export default class TextBlockForm extends Component {
};

render() {
const { block, isSelected, moveBlockUp, moveBlockDown } = this.props;
const { block, isSelected, focusConfig, moveBlockUp, moveBlockDown, replace } = this.props;
const selectedTextAlign = block.align || 'left';
const style = {
textAlign: selectedTextAlign
Expand All @@ -50,6 +51,9 @@ export default class TextBlockForm extends Component {
}

<div className="text-block__form" style={ style }>
{ ! block.content.trim() && ! isSelected && focusConfig &&
<InserterButton onAdd={ ( id ) => replace( id ) } />
}
<InlineTextBlockForm
ref={ this.bindForm }
{ ...this.props }
Expand Down
1 change: 1 addition & 0 deletions tinymce-per-block/src/inserter/button.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export default class InserterButtonComponent extends Component {

toggleInserter = ( event ) => {
event.preventDefault();
event.stopPropagation();
this.setState( {
opened: ! this.state.opened
} );
Expand Down
20 changes: 17 additions & 3 deletions tinymce-per-block/src/inserter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,37 @@ export default class InserterComponent extends Component {
onAdd: () => {}
};

state = {
filterValue: ''
};

filter = ( event ) => {
this.setState( {
filterValue: event.target.value
} );
};

render() {
const addBlock = ( id ) => () => this.props.onAdd( id );
const stopPropagation = ( event ) => event.stopPropagation();
const blocks = getBlocks().filter(
( block ) => block.title.toLowerCase().indexOf( this.state.filterValue.toLowerCase() ) !== -1
);

return (
<div className="inserter">
<div className="inserter" onClick={ stopPropagation }>
<div className="inserter__arrow" />
<div className="inserter__content">
<div className="inserter__category-blocks">
{ getBlocks().map( ( { id, title, icon: Icon } ) => (
{ blocks.map( ( { id, title, icon: Icon } ) => (
<div key={ title } className="inserter__block" onClick={ addBlock( id ) }>
<Icon />
{ title }
</div>
) ) }
</div>
</div>
<input className="inserter__search" type="search" placeholder="Search..." />
<input className="inserter__search" type="search" placeholder="Search..." onChange={ this.filter } />
</div>
);
}
Expand Down
2 changes: 2 additions & 0 deletions tinymce-per-block/src/inserter/style.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.inserter__button {
display: inline-block;
position: relative;
background: none;
border: none;
Expand All @@ -22,6 +23,7 @@
}

.inserter {
font: 13px/1.8 -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen-Sans", "Ubuntu", "Cantarell", "Helvetica Neue", sans-serif;
width: 280px;
box-shadow: 0px 3px 20px rgba( 18, 24, 30, .1 ), 0px 1px 3px rgba( 18, 24, 30, .1 );
border: 1px solid #e0e5e9;
Expand Down
6 changes: 6 additions & 0 deletions tinymce-per-block/src/renderers/block/block-list/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ export default class BlockListBlock extends Component {
executeCommand( {
type: 'moveBlockDown'
} );
},
replace( id ) {
executeCommand( {
type: 'replace',
id
} );
}
};

Expand Down
14 changes: 14 additions & 0 deletions tinymce-per-block/src/renderers/block/block-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,20 @@ class BlockList extends Component {
);
} );
},
replace: ( { id } ) => {
const newBlockUid = uniqueId();
const blockDefinition = getBlock( id );
const newBlock = Object.assign( { uid: newBlockUid }, blockDefinition.create() );
const newBlocks = [
...this.content.slice( 0, index ),
newBlock,
...this.content.slice( index + 1 )
];
this.onChange( newBlocks );
setTimeout( () => {
this.focus( newBlockUid );
} );
}
};

commandHandlers[ command.type ] && commandHandlers[ command.type ]( command );
Expand Down

0 comments on commit 71c98b8

Please sign in to comment.