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

Start work on inserter #157

Merged
merged 1 commit into from
Mar 1, 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
4 changes: 3 additions & 1 deletion shared/tinymce/toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@

function onClick( callback ) {
return function() {
callback( editor, window.element );
editor.undoManager.transact( function() {
callback( editor, window.element );
} );
}
}

Expand Down
5 changes: 4 additions & 1 deletion tinymce-single/blocks/elements/blockquote/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ window.wp.blocks.register( {
editor.formatter.remove( 'blockquote' );
}
}
]
],
insert: function( editor, element ) {
editor.formatter.apply( 'blockquote', element );
}
} );

5 changes: 4 additions & 1 deletion tinymce-single/blocks/elements/horizontal-rule/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@ window.wp.blocks.register( {
icon: 'gridicons-minus',
buttons: [

]
],
insert: function( editor, element ) {
element.parentNode.replaceChild( document.createElement( 'hr' ), element );
}
} );
5 changes: 4 additions & 1 deletion tinymce-single/blocks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@
getSettings: function( id ) {
return _settings[ id ];
},
getSettingsByElement( element ) {
getSettingsByElement: function( element ) {
var blockType = element.getAttribute( 'data-wp-block-type' );
var nodeName = element.nodeName.toLowerCase();

return this.getSettings( blockType || 'element:' + nodeName );
},
getAllSettings: function() {
return _settings;
}
};
} )( window.wp = window.wp || {} );
64 changes: 62 additions & 2 deletions tinymce-single/tinymce/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,15 @@
classes: 'widget btn move-down'
} );

var insert = false;

editor.addButton( 'add', {
icon: 'gridicons-add-outline',
tooltip: 'Add Block'
tooltip: 'Add Block',
onClick: function() {
insert = true;
editor.nodeChanged();
}
} );

// Adjust icon of TinyMCE core buttons.
Expand All @@ -216,10 +222,53 @@
toolbarCaret.$el.addClass( 'block-toolbar' );
blockToolbar.$el.addClass( 'block-toolbar' );

function getInsertButtons() {
var allSettings = wp.blocks.getAllSettings();
var buttons = [];
var key;

for ( key in allSettings ) {
if ( allSettings[ key ].insert ) {
buttons.push( {
icon: allSettings[ key ].icon,
onClick: allSettings[ key ].insert
} );
}
}

buttons.push( {
text: 'Work in progress',
onClick: function() {}
} );

return buttons;
}

var toolbarInsert = editor.wp._createToolbar( getInsertButtons() );

var anchorNode;
var range;
var blockToolbarWidth = 0;

toolbarInsert.reposition = function () {
if ( ! element ) {
return;
}

var toolbar = this.getEl();
var toolbarRect = toolbar.getBoundingClientRect();
var elementRect = element.getBoundingClientRect();
var contentRect = editor.getBody().getBoundingClientRect();

DOM.setStyles( toolbar, {
position: 'absolute',
left: contentRect.left + 100 + 'px',
top: elementRect.top + window.pageYOffset + 'px'
} );

this.show();
}

toolbarInline.reposition = function () {
if ( ! element ) {
return;
Expand Down Expand Up @@ -279,7 +328,7 @@
// Element node.
if ( node.nodeType === 1 ) {
// Element is no direct child.
if ( isAtRoot && node.parentNode !== editor.getBody() ) {
if ( isAtRoot && node.parentNode !== editor.getBody() && node.nodeName !== 'P' ) {
return false;
}

Expand Down Expand Up @@ -431,6 +480,8 @@
} else {
hidden = true;
}

insert = false;
} );

editor.on( 'keyup', function( event ) {
Expand Down Expand Up @@ -459,6 +510,7 @@

editor.on( 'mousedown touchstart', function() {
hidden = false;
insert = false;
} );

editor.on( 'selectionChange nodeChange', function( event ) {
Expand Down Expand Up @@ -497,11 +549,19 @@
hideBlockUI();
}
}

if ( insert ) {
toolbarInsert.reposition();
} else {
toolbarInsert.hide();
}
} );

editor.on( 'nodeChange', function( event ) {
editor.$( '*[data-mce-selected="block"]' ).attr( 'data-mce-selected', null );
editor.$( element ).attr( 'data-mce-selected', 'block' );

insert = false;
} );

// editor.on( 'keydown', function( event ) {
Expand Down