Skip to content

Commit

Permalink
Merge pull request #242 from WordPress/try/tinymce-single/contentedit…
Browse files Browse the repository at this point in the history
…able-false

Restrict setting content in blockquote footer
  • Loading branch information
ellatrix authored Mar 10, 2017
2 parents 26d085e + 19d9943 commit 8a96090
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
1 change: 1 addition & 0 deletions tinymce-single/blocks/elements/blockquote/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ window.wp.blocks.registerBlock( {
elements: [ 'blockquote' ],
type: 'text',
icon: 'gridicons-quote',
restrictToInline: [ 'footer' ],
controls: [
{
classes: 'remove-formatting',
Expand Down
42 changes: 34 additions & 8 deletions tinymce-single/tinymce/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,33 +60,59 @@
} );
} );

function toInlineContent( content ) {
var settings = {
valid_elements: 'strong,em,del,a[href]'
};

var schema = new tinymce.html.Schema( settings );
var parser = new tinymce.html.DomParser( settings, schema );
var serializer = new tinymce.html.Serializer( settings, schema );

return serializer.serialize( parser.parse( content, { forced_root_block: false } ) )
}

editor.on( 'beforeSetContent', function( event ) {
if ( event.initial ) {
return;
}

var settings = {
valid_elements: 'strong,em,del,a[href]'
};

var selectedBlock = wp.blocks.getSelectedBlock();
var blockSettings = wp.blocks.getBlockSettingsByElement( selectedBlock );

if ( editor.$( selectedBlock ).attr( 'contenteditable' ) === 'false' ) {
var schema = new tinymce.html.Schema( settings );
var parser = new tinymce.html.DomParser( settings, schema );
var serializer = new tinymce.html.Serializer( settings, schema );
event.content = toInlineContent( event.content );
}

if ( blockSettings && blockSettings.restrictToInline ) {
blockSettings.restrictToInline.forEach( function( selector ) {
var node = editor.selection.getNode();

event.content = serializer.serialize( parser.parse( event.content, { forced_root_block: false } ) );
if ( editor.$( node ).is( selector ) || editor.$( node ).parents( selector ).length ) {
event.content = toInlineContent( event.content );
}
} );
}
} );

editor.on( 'keydown', function( event ) {
if ( event.keyCode === tinymce.util.VK.ENTER ) {
var selectedBlock = wp.blocks.getSelectedBlock();
var blockSettings = wp.blocks.getBlockSettingsByElement( selectedBlock );

if ( editor.$( selectedBlock ).attr( 'contenteditable' ) === 'false' ) {
event.preventDefault();
}

if ( blockSettings && blockSettings.restrictToInline ) {
blockSettings.restrictToInline.forEach( function( selector ) {
var node = editor.selection.getNode();

if ( editor.$( node ).is( selector ) || editor.$( node ).parents( selector ).length ) {
event.preventDefault();
}
} );
}
}
} );

Expand Down

0 comments on commit 8a96090

Please sign in to comment.