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

Use functions instead of commands where appropriate #146

Merged
merged 1 commit into from
Feb 28, 2017
Merged
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
86 changes: 41 additions & 45 deletions tinymce-single/tinymce/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,19 @@
// Global buttons.

tinymce.each( [ 'left', 'center', 'right' ], function( position ) {
editor.addCommand( 'text-align-' + position, function() {
tinymce.each( [ 'left', 'center', 'right' ], function( position ) {
editor.$( element ).removeClass( 'text-align-' + position );
} );

if ( position !== 'left' ) {
editor.$( element ).addClass( 'text-align-' + position );
editor.nodeChanged();
}
} );

editor.addButton( 'text-align-' + position, {
icon: 'gridicons-align-' + position,
cmd: 'text-align-' + position,
onClick: function() {
tinymce.each( [ 'left', 'center', 'right' ], function( position ) {
editor.$( element ).removeClass( 'text-align-' + position );
} );

if ( position !== 'left' ) {
editor.$( element ).addClass( 'text-align-' + position );
editor.nodeChanged();
}
},
onPostRender: function() {
var button = this;

Expand All @@ -41,18 +40,17 @@
}
} );

editor.addCommand( 'block-align-' + position, function() {
tinymce.each( [ 'left', 'center', 'right' ], function( position ) {
editor.$( element ).removeClass( 'align' + position );
} );

editor.$( element ).addClass( 'align' + position );
editor.nodeChanged();
} );

editor.addButton( 'block-align-' + position, {
icon: 'gridicons-align-image-' + position,
cmd: 'block-align-' + position,
onClick: function() {
tinymce.each( [ 'left', 'center', 'right' ], function( position ) {
editor.$( element ).removeClass( 'align' + position );
} );

editor.$( element ).addClass( 'align' + position );
editor.nodeChanged();
},
onPostRender: function() {
var button = this;

Expand All @@ -71,7 +69,7 @@
} );
} );

editor.addCommand( 'addfigcaption', function() {
function addfigcaption() {
if ( ! editor.$( element ).find( 'figcaption' ).length ) {
var figcaption = editor.$( '<figcaption><br></figcaption>' );

Expand All @@ -80,29 +78,27 @@
editor.selection.setCursorLocation( figcaption[0], 0 );
} );
}
} );
}

editor.addCommand( 'removefigcaption', function() {
function removefigcaption() {
var figcaption = editor.$( element ).find( 'figcaption' );

if ( figcaption.length ) {
editor.undoManager.transact( function() {
figcaption.remove();
} );
}
} );

editor.addCommand( 'togglefigcaption', function() {
if ( editor.$( element ).find( 'figcaption' ).length ) {
editor.execCommand( 'removefigcaption' );
} else {
editor.execCommand( 'addfigcaption' );
}
} );
}

editor.addButton( 'togglefigcaption', {
icon: 'gridicons-caption',
cmd: 'togglefigcaption',
onClick: function() {
if ( editor.$( element ).find( 'figcaption' ).length ) {
removefigcaption();
} else {
addfigcaption();
}
},
onPostRender: function() {
var button = this;

Expand Down Expand Up @@ -147,7 +143,7 @@
}
});

editor.addCommand( 'block-remove', function() {
function removeBlock() {
var $blocks = editor.$( '*[data-mce-selected="block"]' ).add( element );
var p = editor.$( '<p><br></p>' );

Expand All @@ -156,14 +152,14 @@
editor.selection.setCursorLocation( p[0], 0 );
$blocks.remove();
} );
} );
}

editor.addButton( 'block-remove', {
icon: 'gridicons-trash',
cmd: 'block-remove'
onClick: removeBlock
} );

editor.addCommand( 'up', function() {
function moveBlockUp() {
$blocks = editor.$( '*[data-mce-selected="block"]' ).add( element );
rect = element.getBoundingClientRect();
$prev = $blocks.first().prev();
Expand All @@ -173,9 +169,9 @@
editor.nodeChanged();
window.scrollBy( 0, - rect.top + element.getBoundingClientRect().top );
}
} );
}

editor.addCommand( 'down', function() {
function moveBlockDown() {
$blocks = editor.$( '*[data-mce-selected="block"]' ).add( element );
rect = element.getBoundingClientRect();
$next = $blocks.last().next();
Expand All @@ -185,19 +181,19 @@
editor.nodeChanged();
window.scrollBy( 0, - rect.top + element.getBoundingClientRect().top );
}
} );
}

editor.addButton( 'up', {
icon: 'gridicons-chevron-up',
tooltip: 'Up',
cmd: 'up',
onClick: moveBlockUp,
classes: 'widget btn move-up'
} );

editor.addButton( 'down', {
icon: 'gridicons-chevron-down',
tooltip: 'Down',
cmd: 'down',
onClick: moveBlockDown,
classes: 'widget btn move-down'
} );

Expand Down Expand Up @@ -533,21 +529,21 @@

if ( element.nodeName === 'FIGURE' ) {
if ( keyCode === VK.ENTER ) {
editor.execCommand( 'addfigcaption' );
addfigcaption();
event.preventDefault();
}

if ( keyCode === VK.BACKSPACE ) {
var caretEl = editor.selection.getNode();

if ( caretEl.nodeName !== 'FIGCAPTION' ) {
editor.execCommand( 'block-remove' );
removeBlock();
event.preventDefault();
} else {
var range = editor.selection.getRng();

if ( range.collapsed && range.startOffset === 0 ) {
editor.execCommand( 'removefigcaption' );
removefigcaption();
event.preventDefault();
}
}
Expand Down