Skip to content

Commit 612af53

Browse files
authored
Merge pull request #211 from WordPress/add/tinymce-single/insert
Update single MCE inserter
2 parents 735e94a + 0f53bd7 commit 612af53

File tree

13 files changed

+117
-34
lines changed

13 files changed

+117
-34
lines changed

shared/index.css

+1-5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ body {
2626

2727
#editor {
2828
font-family: "Noto Serif", serif;
29+
font-size: 16px;
2930
outline: none;
3031
padding: 50px;
3132
}
@@ -84,11 +85,6 @@ body {
8485
font-size: 28px;
8586
}
8687

87-
#editor p {
88-
font-size: 16px;
89-
min-height: 3.4em;
90-
}
91-
9288
#editor blockquote {
9389
font-size: 20px;
9490
border-left: 4px solid black;

tinymce-single/blocks.js

+15-8
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,23 @@
44
_blocks = {};
55
_controls = {};
66

7+
var _elementMap = {};
8+
79
wp.blocks = {
810
registerBlock: function( settings ) {
911
// Note, elements should probably only be registered by core.
1012
// Maybe for each block, we should offer to extend the settings (add buttons).
1113

14+
var namespace = settings.namespace || 'elements';
15+
var id = namespace + ':' + settings.name;
16+
17+
_blocks[ id ] = settings;
18+
_blocks[ id ]._id = id;
19+
1220
if ( settings.elements ) {
1321
settings.elements.forEach( function( element ) {
14-
_blocks[ 'element:' + element ] = settings;
15-
_blocks[ 'element:' + element ]._id = 'element:' + element;
22+
_elementMap[ element ] = id;
1623
} );
17-
} else if ( settings.namespace && settings.name ) {
18-
_blocks[ settings.namespace + ':' + settings.name ] = settings;
19-
_blocks[ settings.namespace + ':' + settings.name ]._id = settings.namespace + ':' + settings.name;
2024
}
2125
},
2226
registerControl: function( name, settings ) {
@@ -29,10 +33,13 @@
2933
return _controls[ name ];
3034
},
3135
getBlockSettingsByElement: function( element ) {
32-
var blockType = element.getAttribute( 'data-wp-block-type' );
33-
var nodeName = element.nodeName.toLowerCase();
36+
var id = element.getAttribute( 'data-wp-block-type' );
37+
38+
if ( ! id ) {
39+
id = _elementMap[ element.nodeName.toLowerCase() ];
40+
}
3441

35-
return this.getBlockSettings( blockType || 'element:' + nodeName );
42+
return this.getBlockSettings( id );
3643
},
3744
getBlocks: function() {
3845
return _blocks;

tinymce-single/blocks/core/gallery/register.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
window.wp.blocks.registerBlock( {
22
name: 'gallery',
33
namespace: 'core',
4-
type: 'image',
4+
displayName: 'Gallery',
5+
type: 'media',
56
keywords: [],
67
icon: 'gridicons-image-multiple',
78
controls: [
@@ -24,5 +25,8 @@ window.wp.blocks.registerBlock( {
2425
{
2526
icon: 'gridicons-cog'
2627
}
27-
]
28+
],
29+
insert: function( block, editor ) {
30+
31+
}
2832
} );
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
window.wp.blocks.registerBlock( {
22
name: 'image',
33
namespace: 'core',
4-
type: 'image',
4+
displayName: 'Image',
5+
type: 'media',
56
icon: 'gridicons-image',
67
controls: [
78
'block-align-left',
89
'block-align-center',
910
'block-align-right',
1011
'block-align-full',
1112
'togglefigcaption'
12-
]
13+
],
14+
insert: function( block, editor ) {
15+
16+
}
1317
} );

tinymce-single/blocks/elements/blockquote/register.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
window.wp.blocks.registerBlock( {
2+
name: 'blockquote',
3+
displayName: 'Quote',
24
elements: [ 'blockquote' ],
35
type: 'text',
46
icon: 'gridicons-quote',

tinymce-single/blocks/elements/headings/register.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,14 @@
2727
}
2828

2929
wp.blocks.registerBlock( {
30+
name: 'heading',
31+
displayName: 'Heading',
3032
elements: [ 'h1', 'h2', 'h3', 'h4', 'h5', 'h6' ],
3133
type: 'text',
3234
icon: 'gridicons-heading',
33-
controls: getControls()
35+
controls: getControls(),
36+
insert: function() {
37+
38+
}
3439
} );
3540
} )( window.wp );

tinymce-single/blocks/elements/horizontal-rule/register.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
window.wp.blocks.registerBlock( {
2+
name: 'hortizontal-rule',
3+
displayName: 'Horizontal Rule',
24
elements: [ 'hr' ],
35
type: 'separator',
46
icon: 'gridicons-minus',

tinymce-single/blocks/elements/lists/register.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
window.wp.blocks.registerBlock( {
2+
name: 'list',
3+
displayName: 'List',
24
elements: [ 'ul', 'ol' ],
35
type: 'text',
46
icon: 'gridicons-list-unordered',
@@ -28,5 +30,8 @@ window.wp.blocks.registerBlock( {
2830
}
2931
}
3032
}
31-
]
33+
],
34+
insert: function( block, editor ) {
35+
editor.execCommand( 'InsertUnorderedList' );
36+
}
3237
} );

tinymce-single/blocks/elements/paragraph/register.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
window.wp.blocks.registerBlock( {
2+
name: 'paragraph',
3+
displayName: 'Paragraph',
24
elements: [ 'p' ],
35
type: 'text',
46
icon: 'gridicons-posts',
@@ -30,5 +32,8 @@ window.wp.blocks.registerBlock( {
3032
'text-align-left',
3133
'text-align-center',
3234
'text-align-right'
33-
]
35+
],
36+
insert: function( block, editor ) {
37+
editor.formatter.apply( 'paragraph', block );
38+
}
3439
} );

tinymce-single/blocks/elements/preformatted/register.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
window.wp.blocks.registerBlock( {
2+
name: 'preformatted',
3+
displayName: 'Preformatted',
24
elements: [ 'pre' ],
35
type: 'text',
46
icon: 'gridicons-code',
@@ -13,5 +15,8 @@ window.wp.blocks.registerBlock( {
1315
editor.formatter.remove( 'pre' );
1416
}
1517
}
16-
]
18+
],
19+
insert: function( block, editor ) {
20+
editor.formatter.apply( 'pre' );
21+
}
1722
} );

tinymce-single/blocks/my-awesome-plugin/youtube/register.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
window.wp.blocks.registerBlock( {
22
name: 'youtube',
33
namespace: 'my-awesome-plugin',
4-
type: 'video',
4+
displayName: 'YouTube Video',
5+
type: 'media',
56
keywords: [],
67
icon: 'gridicons-video',
78
controls: [
@@ -12,5 +13,8 @@ window.wp.blocks.registerBlock( {
1213
{
1314
icon: 'gridicons-cog'
1415
}
15-
]
16+
],
17+
insert: function( block, editor ) {
18+
19+
}
1620
} );

tinymce-single/tinymce/block.css

+29
Original file line numberDiff line numberDiff line change
@@ -250,3 +250,32 @@ div.mce-inline-toolbar-grp.block-toolbar > div.mce-stack-layout {
250250
background: #fff;
251251
border-radius: 12px;
252252
}
253+
254+
.insert-menu {
255+
width: 330px;
256+
}
257+
258+
.insert-menu .mce-btn {
259+
float: left;
260+
}
261+
262+
.insert-menu .insert-separator {
263+
clear: both;
264+
background: #f0f2f4;
265+
padding: 4px;
266+
text-transform: uppercase;
267+
}
268+
269+
.insert-menu .mce-txt {
270+
font-size: 16px;
271+
text-align: inherit;
272+
vertical-align: inherit;
273+
width: auto;
274+
line-height: 16px;
275+
width: 120px;
276+
padding: 4px;
277+
}
278+
279+
.insert-menu .mce-btn-has-text svg.gridicons-heading {
280+
margin: 0;
281+
}

tinymce-single/tinymce/block.js

+26-11
Original file line numberDiff line numberDiff line change
@@ -258,29 +258,44 @@
258258
return insert;
259259
}
260260

261+
window.tinymce.ui.WPInsertSeparator = tinymce.ui.Control.extend( {
262+
renderHtml: function() {
263+
console.log(this)
264+
return (
265+
'<div id="' + this._id + '" class="insert-separator">' + this.settings.text + '</div>'
266+
);
267+
}
268+
} );
269+
261270
function createInsertMenu() {
262271
var insertMenu = editor.wp._createToolbar( ( function() {
263272
var allSettings = wp.blocks.getBlocks();
264273
var buttons = [];
265274
var key;
275+
var types = [ 'text', 'media', 'separator' ];
266276

267-
for ( key in allSettings ) {
268-
if ( allSettings[ key ].insert ) {
269-
buttons.push( {
270-
icon: allSettings[ key ].icon,
271-
onClick: allSettings[ key ].insert
272-
} );
273-
}
274-
}
277+
types.forEach( function( type ) {
278+
buttons.push( {
279+
type: 'WPInsertSeparator',
280+
text: type
281+
} );
275282

276-
buttons.push( {
277-
text: 'Work in progress',
278-
onClick: function() {}
283+
for ( key in allSettings ) {
284+
if ( allSettings[ key ].type === type ) {
285+
buttons.push( {
286+
text: allSettings[ key ].displayName,
287+
icon: allSettings[ key ].icon,
288+
onClick: allSettings[ key ].insert
289+
} );
290+
}
291+
}
279292
} );
280293

281294
return buttons;
282295
} )() );
283296

297+
insertMenu.$el.addClass( 'insert-menu' );
298+
284299
insertMenu.reposition = function () {
285300
var toolbar = this.getEl();
286301
var toolbarRect = toolbar.getBoundingClientRect();

0 commit comments

Comments
 (0)