Skip to content

Commit

Permalink
Add "insert items" menu item to editor (#5071)
Browse files Browse the repository at this point in the history
Adds an "insert item" menu bar to the editor toolbar to allow inserting various types of media or embeds into a post, similar to how the insert menus in other mainstream applications (such as Pages, Sketch, etc…) work.

This patch removes the contact form button and the media button and combines those into the new insert menu. The insert menu button has two behaviors: a default click on the button inserts a media item; a click on the chevron opens the drop-down menu to select one of many possible items to insert.

Props to all who helped review and edit and design this feature:
@aduth @drw158 @folletto @mtias @rralian
  • Loading branch information
dmsnell committed May 24, 2016
1 parent 0784068 commit 65c22ab
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 4 deletions.
7 changes: 6 additions & 1 deletion client/components/tinymce/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import contactFormPlugin from './plugins/contact-form/plugin';
import afterTheDeadlinePlugin from './plugins/after-the-deadline/plugin';
import wptextpatternPlugin from './plugins/wptextpattern/plugin';
import toolbarPinPlugin from './plugins/toolbar-pin/plugin';
import insertMenuPlugin from './plugins/insert-menu/plugin';

[
wpcomPlugin,
Expand All @@ -49,6 +50,7 @@ import toolbarPinPlugin from './plugins/toolbar-pin/plugin';
wpcomSourcecode,
wpeditimagePlugin,
wplinkPlugin,
insertMenuPlugin,
mediaPlugin,
advancedPlugin,
wpcomTabindexPlugin,
Expand Down Expand Up @@ -110,6 +112,7 @@ const PLUGINS = [
'wplink',
'AtD',
'wpcom/autoresize',
'wpcom/insertmenu',
'wpcom/media',
'wpcom/advanced',
'wpcom/help',
Expand Down Expand Up @@ -284,7 +287,9 @@ module.exports = React.createClass( {
// future, we should calculate from the rendered editor bounds.
autoresize_min_height: Math.max( document.documentElement.clientHeight - 300, 300 ),

toolbar1: 'wpcom_add_media,formatselect,bold,italic,bullist,numlist,link,blockquote,alignleft,aligncenter,alignright,spellchecker,wp_more,wpcom_add_contact_form,wpcom_advanced',
toolbar1: config.isEnabled( 'post-editor/insert-menu' )
? 'wpcom_insert_menu,formatselect,bold,italic,bullist,numlist,link,blockquote,alignleft,aligncenter,alignright,spellchecker,wp_more,wpcom_advanced'
: 'wpcom_add_media,formatselect,bold,italic,bullist,numlist,link,blockquote,alignleft,aligncenter,alignright,spellchecker,wp_more,wpcom_add_contact_form,wpcom_advanced',
toolbar2: 'strikethrough,underline,hr,alignjustify,forecolor,pastetext,removeformat,wp_charmap,outdent,indent,undo,redo,wp_help',
toolbar3: '',
toolbar4: '',
Expand Down
32 changes: 32 additions & 0 deletions client/components/tinymce/plugins/insert-menu/menu-items.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import React from 'react';

import Gridicon from 'components/gridicon';
import SocialLogo from 'components/social-logo';
import i18n from 'lib/mixins/i18n';

const GridiconButton = ( { icon, label } ) => (
<div>
<Gridicon className="wpcom-insert-menu__menu-icon" icon={ icon } />
<span className="wpcom-insert-menu__menu-label">{ label }</span>
</div>
);

const SocialLogoButton = ( { icon, label } ) => (
<div>
<SocialLogo className="wpcom-insert-menu__menu-icon" icon={ icon } />
<span className="wpcom-insert-menu__menu-label">{ label }</span>
</div>
);

export default [
{
name: 'insert_media_item',
item: <GridiconButton icon="image-multiple" label={ i18n.translate( 'Add Media' ) } />,
cmd: 'wpcomAddMedia'
},
{
name: 'insert_contact_form',
item: <GridiconButton icon="mention" label={ i18n.translate( 'Add Contact Form' ) } />,
cmd: 'wpcomContactForm'
}
];
44 changes: 44 additions & 0 deletions client/components/tinymce/plugins/insert-menu/plugin.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import React from 'react';
import ReactDOM from 'react-dom';
import tinymce from 'tinymce/tinymce';
import { renderToString } from 'react-dom/server';

import Gridicon from 'components/gridicon';
import config from 'config';
import i18n from 'lib/mixins/i18n';

import menuItems from './menu-items';

const initialize = editor => {
menuItems.forEach( item =>
editor.addMenuItem( item.name, {
classes: 'wpcom-insert-menu__menu-item',
cmd: item.cmd,
onPostRender() {
this.innerHtml( renderToString( item.item ) );
}
} )
);

editor.addButton( 'wpcom_insert_menu', {
type: 'splitbutton',
title: i18n.translate( 'Insert content' ),
classes: 'btn wpcom-insert-menu insert-menu',
cmd: menuItems[0].cmd,
menu: menuItems.map( ( { name } ) => editor.menuItems[ name ] ),
onPostRender() {
ReactDOM.render(
<Gridicon icon="add-image" />,
this.$el[0].children[0]
);
}
} );
};

export default () => {
if ( ! config.isEnabled( 'post-editor/insert-menu' ) ) {
return;
}

tinymce.PluginManager.add( 'wpcom/insertmenu', initialize );
};
80 changes: 80 additions & 0 deletions client/components/tinymce/plugins/insert-menu/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
.post-editor,
.mce-inline-toolbar-grp {

.mce-toolbar .mce-btn-group .mce-btn.mce-insert-menu {
margin: 0;

&:hover .mce-open i.mce-caret,
&:focus .mce-open i.mce-caret {
color: lighten( $gray, 10% );
}

&:hover .gridicons-add-image,
&:focus .gridicons-add-image {
fill: lighten( $gray, 10% );
}

.mce-open:hover i.mce-caret,
.mce-open:focus i.mce-caret {
color: $gray-dark;
}

.gridicons-add-image:hover,
.gridicons-add-image:focus {
fill: $gray-dark;
}
}
}

.post-editor .mce-toolbar .mce-btn.mce-insert-menu .gridicon,
.wpcom-insert-menu__menu-icon.gridicon {
height: 24px;
width: 24px;
}

.mce-toolbar .mce-btn.mce-insert-menu button:first-child {
padding: 6px;
}

.mce-splitbtn.mce-insert-menu .mce-open.mce-active {
background-color: $white;
outline: 0;
}

.mce-btn.mce-insert-menu span {
padding-left: 0;
}

.mce-toolbar .mce-btn.mce-insert-menu .mce-open {
padding: 8px 12px;
}

.mce-container .wpcom-insert-menu__menu-label {
color: darken( $gray, 30% );
display: inline-block;
font-family: $sans;
margin-left: 8px;
margin-top: 2px;
}

.wpcom-insert-menu__menu-icon {
fill: $gray;
}

.mce-container.mce-menu .mce-container-body .mce-wpcom-insert-menu__menu-item {
padding: 9px 16px;
margin: 0;
border: none;

&:hover,
&:focus {

.wpcom-insert-menu__menu-label {
color: $blue-medium;
}

.wpcom-insert-menu__menu-icon {
fill: $blue-medium;
}
}
}
12 changes: 9 additions & 3 deletions client/components/tinymce/style.scss
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@import 'plugins/wplink/style';
@import 'plugins/calypso-alert/style';
@import 'plugins/insert-menu/style';
@import 'plugins/wpcom-help/style';
@import 'plugins/wpcom-charmap/style';
@import 'plugins/contact-form/style';
Expand Down Expand Up @@ -95,11 +96,13 @@
margin: 0 2px 0 0;
padding: 0 8px;
vertical-align: top;
border-left: 1px solid lighten( $gray, 30% );
border-right: 1px solid lighten( $gray, 30% );
height: 36px;

@include breakpoint( "<660px" ) {
border-right-color: $white;
border-left-color: $white;
padding: 2px 0;
}
}
Expand Down Expand Up @@ -220,7 +223,6 @@

.mce-toolbar .mce-btn-group .mce-wpcom-icon-button.mce-media button {
@include breakpoint( ">660px" ) {
border-right: 1px solid lighten( $gray, 30% );
font-size: 13px;
}
}
Expand Down Expand Up @@ -590,7 +592,9 @@ div.mce-path {
}
}

.mce-toolbar-grp:not( .mce-inline-toolbar-grp ) .mce-btn-group .mce-btn.mce-first {
// [TODO]: Selector `:not( .mce-wpcom-insert-menu )` unnecessary when
// `post-editor/insert-menu` feature enabled in all environments
.mce-toolbar-grp:not( .mce-inline-toolbar-grp ) .mce-btn-group .mce-btn.mce-first:not( .mce-wpcom-insert-menu ) {
margin-left: 4px;
}

Expand Down Expand Up @@ -727,10 +731,12 @@ div.mce-path {

.post-editor .mce-toolbar .mce-btn-group .mce-btn.mce-listbox:hover {
background-image: none;
border-left-color: lighten( $gray, 30% );
border-right-color: lighten( $gray, 30% );

@include breakpoint( "<660px" ) {
border-right-color: $white;
border-left-color: $white;
}

i.mce-caret {
Expand Down Expand Up @@ -998,7 +1004,7 @@ div.mce-menu .mce-menu-item-sep,
.mce-toolbar .mce-btn:hover .mce-open,
.mce-toolbar .mce-btn:focus .mce-open,
.mce-toolbar .mce-btn.mce-active .mce-open {
border-left-color: lighten( $gray, 20% );
border-left-color: lighten( $gray, 30% );
}

i.mce-i-bold,
Expand Down
1 change: 1 addition & 0 deletions config/development.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
"phone_signup": true,
"post-editor-github-link": false,
"post-editor/image-editor": false,
"post-editor/insert-menu": true,
"post-editor/media-advanced": true,
"press-this": true,
"reader": true,
Expand Down
1 change: 1 addition & 0 deletions config/horizon.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"perfmon": true,
"persist-redux": true,
"phone_signup": false,
"post-editor/insert-menu": true,
"press-this": true,
"reader": true,
"resume-editing": true,
Expand Down
1 change: 1 addition & 0 deletions config/stage.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"olark": true,
"perfmon": true,
"persist-redux": true,
"post-editor/insert-menu": true,
"press-this": true,
"reader": true,
"reader/full-errors": true,
Expand Down
1 change: 1 addition & 0 deletions config/wpcalypso.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"perfmon": true,
"persist-redux": true,
"phone_signup": true,
"post-editor/insert-menu": true,
"press-this": true,
"reader": true,
"reader/full-errors": true,
Expand Down

0 comments on commit 65c22ab

Please sign in to comment.