Skip to content

Commit

Permalink
Merge pull request #218 from WordPress/add/tinymce-single/insert
Browse files Browse the repository at this point in the history
Allow insertion by returning string or node
  • Loading branch information
ellatrix committed Mar 9, 2017
2 parents 30f90ce + 924af55 commit 8ea78fe
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 19 deletions.
4 changes: 1 addition & 3 deletions tinymce-single/blocks/core/gallery/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,5 @@ window.wp.blocks.registerBlock( {
icon: 'gridicons-cog'
}
],
insert: function( block, editor ) {

}
insert: function() {}
} );
4 changes: 2 additions & 2 deletions tinymce-single/blocks/elements/blockquote/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ window.wp.blocks.registerBlock( {
}
}
],
insert: function( block, editor ) {
editor.formatter.apply( 'blockquote', block );
insert: function() {
return '<blockquote><p><br></p></blockquote>';
}
} );

4 changes: 2 additions & 2 deletions tinymce-single/blocks/elements/headings/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
type: 'text',
icon: 'gridicons-heading',
controls: getControls(),
insert: function( block, editor ) {
insert: function() {
// Maybe detect best heading based on document outline.
editor.formatter.apply( 'h1', block );
return '<h1><br></h1>';
}
} );
} )( window.wp );
4 changes: 2 additions & 2 deletions tinymce-single/blocks/elements/horizontal-rule/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ window.wp.blocks.registerBlock( {
elements: [ 'hr' ],
type: 'separator',
icon: 'gridicons-minus',
insert: function( block ) {
block.parentNode.replaceChild( document.createElement( 'hr' ), block );
insert: function() {
return '<hr>';
}
} );
4 changes: 2 additions & 2 deletions tinymce-single/blocks/elements/lists/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ window.wp.blocks.registerBlock( {
}
}
],
insert: function( block, editor ) {
editor.execCommand( 'InsertUnorderedList' );
insert: function() {
return '<ul><li><br></li></ul>';
}
} );
4 changes: 2 additions & 2 deletions tinymce-single/blocks/elements/paragraph/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ window.wp.blocks.registerBlock( {
'text-align-center',
'text-align-right'
],
insert: function( block, editor ) {
editor.formatter.apply( 'p', block );
insert: function() {
return '<p><br></p>';
}
} );
4 changes: 2 additions & 2 deletions tinymce-single/blocks/elements/preformatted/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ window.wp.blocks.registerBlock( {
}
}
],
insert: function( block, editor ) {
editor.formatter.apply( 'pre' );
insert: function() {
return '<pre><br></pre>';
}
} );
4 changes: 1 addition & 3 deletions tinymce-single/blocks/my-awesome-plugin/youtube/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,5 @@ window.wp.blocks.registerBlock( {
icon: 'gridicons-cog'
}
],
insert: function( block, editor ) {

}
insert: function() {}
} );
19 changes: 18 additions & 1 deletion tinymce-single/tinymce/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,24 @@
buttons.push( {
text: allSettings[ key ].displayName,
icon: allSettings[ key ].icon,
onClick: allSettings[ key ].insert
onClick: ( function( callback ) {
return function( block ) {
var content = callback.apply( this, arguments );

if ( content ) {
if ( typeof content === 'string' ) {
var temp = document.createElement( 'div' );
temp.innerHTML = content;
content = temp.firstChild;
temp = null;
}

block.parentNode.replaceChild( content, block );
}

editor.selection.setCursorLocation( content, 0 );
}
} )( allSettings[ key ].insert )
} );
}
}
Expand Down

0 comments on commit 8ea78fe

Please sign in to comment.