Skip to content

Commit

Permalink
TinyMCE per block: Showing moving block controls on hover (#250)
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Mar 13, 2017
1 parent dee314a commit db9abb9
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 31 deletions.
8 changes: 5 additions & 3 deletions tinymce-per-block/src/blocks/embed-block/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default class EmbedBlockForm extends Component {
};

render() {
const { api, block, isSelected, first, last, focusConfig } = this.props;
const { api, block, isSelected, isHovered, first, last, focusConfig } = this.props;

const removePrevious = () => {
if ( ! block.url ) {
Expand All @@ -37,8 +37,10 @@ export default class EmbedBlockForm extends Component {
const html = getEmbedHtmlFromUrl( block.url );

return (
<div className={ classNames( 'embed-block', block.align ) }>
{ isSelected && <BlockArrangement first={ first } last={ last }
<div className={ classNames( 'embed-block', block.align ) }
onMouseEnter={ api.hover } onMouseLeave={ api.unhover }
>
{ ( isSelected || isHovered ) && <BlockArrangement first={ first } last={ last }
moveBlockUp={ api.moveBlockUp } moveBlockDown={ api.moveBlockDown } /> }
{ isSelected &&
<div className="block-list__block-controls">
Expand Down
6 changes: 3 additions & 3 deletions tinymce-per-block/src/blocks/heading-block/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ export default class HeadingBlockForm extends Component {
};

render() {
const { api, block, isSelected, first, last } = this.props;
const { api, block, isSelected, isHovered, first, last } = this.props;
const sizes = [ 'h1', 'h2', 'h3' ];

return (
<div>
{ isSelected && <BlockArrangement first={ first } last={ last }
<div onMouseEnter={ api.hover } onMouseLeave={ api.unhover }>
{ ( isSelected || isHovered ) && <BlockArrangement first={ first } last={ last }
moveBlockUp={ api.moveBlockUp } moveBlockDown={ api.moveBlockDown } /> }
{ isSelected && (
<div className="block-list__block-controls">
Expand Down
6 changes: 3 additions & 3 deletions tinymce-per-block/src/blocks/html-block/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default class HtmlBlockForm extends Component {
};

render() {
const { api, block, isSelected, first, last, focusConfig } = this.props;
const { api, block, isSelected, isHovered, first, last, focusConfig } = this.props;
const splitValue = ( left, right ) => {
api.change( { content: left } );
if ( right ) {
Expand All @@ -44,8 +44,8 @@ export default class HtmlBlockForm extends Component {
};

return (
<div>
{ isSelected && <BlockArrangement first={ first } last={ last }
<div onMouseEnter={ api.hover } onMouseLeave={ api.unhover }>
{ ( isSelected || isHovered ) && <BlockArrangement first={ first } last={ last }
moveBlockUp={ api.moveBlockUp } moveBlockDown={ api.moveBlockDown } /> }
{ isSelected && (
<div className="block-list__block-controls">
Expand Down
8 changes: 5 additions & 3 deletions tinymce-per-block/src/blocks/image-block/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default class ImageBlockForm extends Component {
};

render() {
const { api, block, first, last, isSelected, focusConfig } = this.props;
const { api, block, first, last, isSelected, isHovered, focusConfig } = this.props;
const removePrevious = () => {
if ( ! block.caption ) {
api.remove();
Expand All @@ -29,8 +29,10 @@ export default class ImageBlockForm extends Component {
};

return (
<div className={ classNames( 'image-block__form', block.align ) }>
{ isSelected && <BlockArrangement first={ first } last={ last }
<div className={ classNames( 'image-block__form', block.align ) }
onMouseEnter={ api.hover } onMouseLeave={ api.unhover }
>
{ ( isSelected || isHovered ) && <BlockArrangement first={ first } last={ last }
moveBlockUp={ api.moveBlockUp } moveBlockDown={ api.moveBlockDown } /> }
{ isSelected &&
<div className="block-list__block-controls">
Expand Down
6 changes: 3 additions & 3 deletions tinymce-per-block/src/blocks/quote-block/_style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
color: $gray-dark-300;
resize: none;
font-style: italic;
padding-left: 20px;
padding-left: 15px;

&:before {
display: block;
Expand All @@ -37,8 +37,8 @@

.quote-block__form.quote-style1 {
border-left: 4px solid black;
margin-left: -20px;
padding-left: 20px;
margin-left: -15px;
padding-left: 15px;

.quote-block__content {
font-style: italic;
Expand Down
6 changes: 3 additions & 3 deletions tinymce-per-block/src/blocks/quote-block/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default class QuoteBlockForm extends Component {
};

render() {
const { api, block, first, last, isSelected, focusConfig } = this.props;
const { api, block, first, last, isSelected, isHovered, focusConfig } = this.props;
const splitValue = ( left, right ) => {
api.change( { cite: left } );
api.appendBlock( {
Expand All @@ -59,8 +59,8 @@ export default class QuoteBlockForm extends Component {
const currentStyle = block.style || 'style1';

return (
<div>
{ isSelected && <BlockArrangement first={ first } last={ last }
<div onMouseEnter={ api.hover } onMouseLeave={ api.unhover }>
{ ( isSelected || isHovered ) && <BlockArrangement first={ first } last={ last }
moveBlockUp={ api.moveBlockUp } moveBlockDown={ api.moveBlockDown } /> }
{ isSelected &&
<div className="block-list__block-controls">
Expand Down
6 changes: 3 additions & 3 deletions tinymce-per-block/src/blocks/text-block/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ export default class TextBlockForm extends Component {
};

render() {
const { api, block, isSelected, focusConfig, first, last } = this.props;
const { api, block, isSelected, isHovered, focusConfig, first, last } = this.props;
const selectedTextAlign = block.align || 'left';
const style = {
textAlign: selectedTextAlign
};

return (
<div className="text-block__form">
{ isSelected && <BlockArrangement first={ first } last={ last }
<div className="text-block__form" onMouseEnter={ api.hover } onMouseLeave={ api.unhover }>
{ ( isSelected || isHovered ) && <BlockArrangement first={ first } last={ last }
moveBlockUp={ api.moveBlockUp } moveBlockDown={ api.moveBlockDown } /> }
{ isSelected &&
<div className="block-list__block-controls">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@include animate_fade;
position: absolute;
top: 10px;
left: -35px;
left: 0;

.block-arrangement__control {
display: block;
Expand Down
26 changes: 19 additions & 7 deletions tinymce-per-block/src/renderers/block/_style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,41 @@

.block-list__block {
outline: 0;
position: relative;
margin-left: -35px;
margin-bottom: 15px;

> div {
position: relative;
border: 2px solid transparent;
outline: none;
padding: 0.8rem 1.2rem;
transition: 0.2s border-color;
margin-bottom: 1rem;
padding: 15px 15px 15px 50px;

&:before {
z-index: -1;
content: '';
position: absolute;
top: 0;
bottom: 0;
left: 35px;
right: 0;
border: 2px solid transparent;
transition: 0.2s border-color;
}
}

&:hover > div {
&:hover > div:before {
border-left-color: #e1e6ea;
}

&.is-selected > div {
&.is-selected > div:before {
border-color: #e1e6ea;
}
}

.block-list__block-controls {
@include animate_fade;
position: absolute;
left: 0;
left: 35px;
top: -36px;
z-index: 1;
}
Expand Down
8 changes: 8 additions & 0 deletions tinymce-per-block/src/renderers/block/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,11 @@ export const transform = ( id ) => ( {
type: 'transform',
id
} );

export const hover = () => ( {
type: 'hover'
} );

export const unhover = () => ( {
type: 'unhover'
} );
4 changes: 3 additions & 1 deletion tinymce-per-block/src/renderers/block/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import * as commands from './commands';

class BlockList extends Component {
state = {
hovered: null,
selected: null,
focus: { uid: null },
blocks: [],
Expand Down Expand Up @@ -85,7 +86,7 @@ class BlockList extends Component {
}

render() {
const { blocks, focus, selected } = this.state;
const { blocks, focus, selected, hovered } = this.state;

return (
<div>
Expand Down Expand Up @@ -122,6 +123,7 @@ class BlockList extends Component {
block={ block }
api={ api }
isSelected={ selected === block.uid }
isHovered={ hovered === block.uid }
focusConfig={ isFocused ? focus.config : null }
first={ index === 0 }
last={ index === blocks.length - 1 }
Expand Down
14 changes: 13 additions & 1 deletion tinymce-per-block/src/renderers/block/updater.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,19 @@ const blockLevelUpdater = ( state, command ) => {
blocks: newBlocks,
focus: { uid: newBlockUid, config: {} }
} );
}
},

hover: () => {
return mergeStates( {
hovered: currentUID
} );
},

unhover: () => {
return mergeStates( {
hovered: null
} );
},
};

if ( blockCommandHandlers[ command.type ] ) {
Expand Down

0 comments on commit db9abb9

Please sign in to comment.