Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add/tinymce single/insert #220

Merged
merged 2 commits into from
Mar 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions tinymce-single/blocks/core/image/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ window.wp.blocks.registerBlock( {
'block-align-full',
'togglefigcaption'
],
insert: function( block, editor ) {

insert: function() {
return (
'<figure data-wp-block-type="core:image" class="alignright">' +
'<img src="https://cldup.com/HN3-c7ER9p.jpg" alt="">' +
'<figcaption>I have no idea which mountain this is. It looks tall!</figcaption>' +
'</figure>'
);
}
} );
6 changes: 4 additions & 2 deletions tinymce-single/blocks/elements/lists/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ window.wp.blocks.registerBlock( {
{
icon: 'gridicons-list-unordered',
stateSelector: 'ul',
onClick: function( editor ) {
onClick: function( block, editor ) {
// Use native command to toggle current selected list.
editor.execCommand( 'InsertUnorderedList' );
}
},
{
icon: 'gridicons-list-ordered',
stateSelector: 'ol',
onClick: function( editor ) {
onClick: function( block, editor ) {
// Use native command to toggle current selected list.
editor.execCommand( 'InsertOrderedList' );
}
},
Expand Down
105 changes: 57 additions & 48 deletions tinymce-single/tinymce/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,34 @@
var key;
var types = [ 'text', 'media', 'separator' ];

function onClick( 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 );
}

var brs = content.getElementsByTagName( 'BR' );

// Has placeholder for text.
if ( brs.length ) {
editor.selection.setCursorLocation( brs[0].parentNode, 0 );
} else {
editor.selection.select( content );
}

setTimeout( showBlockUI )
}
}

types.forEach( function( type ) {
buttons.push( {
type: 'WPInsertSeparator',
Expand All @@ -285,24 +313,7 @@
buttons.push( {
text: allSettings[ key ].displayName,
icon: allSettings[ key ].icon,
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 )
onClick: onClick( allSettings[ key ].insert )
} );
}
}
Expand Down Expand Up @@ -506,7 +517,8 @@
}

function showBlockUI( focus ) {
var settings = wp.blocks.getBlockSettingsByElement( getSelectedBlock() ),
var selectedBlocks = getSelectedBlocks();
var settings = wp.blocks.getBlockSettingsByElement( selectedBlocks[0] ),
controls;

if ( ! hasBlockUI ) {
Expand All @@ -522,16 +534,34 @@
}
} );

if ( settings ) {
UI.blocks[ settings._id ].reposition();
focus && focusToolbar( UI.blocks[ settings._id ] );
if ( selectedBlocks.length === 1 ) {
if ( settings ) {
UI.blocks[ settings._id ].reposition();
focus && focusToolbar( UI.blocks[ settings._id ] );

if ( anchorNode.nodeType === 3 ) {
UI.inline.reposition();
} else {
UI.inline.hide();
if ( anchorNode.nodeType === 3 ) {
UI.inline.reposition();
} else {
UI.inline.hide();
}
}

UI.insert.reposition();
} else {
UI.insert.hide();
}

var startRect = selectedBlocks[0].getBoundingClientRect();
var endRect = selectedBlocks[ selectedBlocks.length - 1 ].getBoundingClientRect();

DOM.setStyles( UI.outline, {
display: 'block',
position: 'absolute',
left: Math.min( startRect.left, endRect.left ) + 'px',
top: startRect.top + window.pageYOffset + 'px',
height: endRect.bottom - startRect.top + 'px',
width: Math.max( startRect.width, endRect.width ) + 'px'
} );
}

function isInputKeyEvent( event ) {
Expand Down Expand Up @@ -645,28 +675,7 @@
UI.insert.reposition( { isEmpty: isEmpty } );
} else {
if ( isBlockUIVisible ) {
var selectedBlocks = getSelectedBlocks();

if ( selectedBlocks.length === 1 ) {
showBlockUI();
UI.insert.reposition();
} else {
hideBlockUI();
UI.navigation.reposition();
UI.insert.hide();
}

var startRect = selectedBlocks[0].getBoundingClientRect();
var endRect = selectedBlocks[ selectedBlocks.length - 1 ].getBoundingClientRect();

DOM.setStyles( UI.outline, {
display: 'block',
position: 'absolute',
left: Math.min( startRect.left, endRect.left ) + 'px',
top: startRect.top + window.pageYOffset + 'px',
height: endRect.bottom - startRect.top + 'px',
width: Math.max( startRect.width, endRect.width ) + 'px'
} );
showBlockUI();
} else {
hideBlockUI();
UI.insert.hide();
Expand Down