Skip to content

Commit

Permalink
Merge pull request #145 from WordPress/tinymce-single/mimic-ui-prototype
Browse files Browse the repository at this point in the history
Prevent input for non-text selections
  • Loading branch information
ellatrix authored Feb 28, 2017
2 parents c2161d7 + 4986d86 commit a3174d1
Showing 1 changed file with 90 additions and 18 deletions.
108 changes: 90 additions & 18 deletions tinymce-single/tinymce/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,12 @@
var button = this;

editor.on( 'nodechange', function( event ) {
element = event.parents[ event.parents.length - 1 ];
button.icon( wp.blocks.getSettingsByElement( element ).icon );
var element = event.parents[ event.parents.length - 1 ];
var settings = wp.blocks.getSettingsByElement( element );

if ( settings ) {
button.icon( settings.icon );
}
} );
}
});
Expand Down Expand Up @@ -264,7 +268,7 @@
} );
} );

function isEmptySlot( node ) {
function isEmptySlot( node, isAtRoot ) {
// Text node.
if ( node.nodeType === 3 ) {
// Has text.
Expand All @@ -278,7 +282,7 @@
// Element node.
if ( node.nodeType === 1 ) {
// Element is no direct child.
if ( node.parentNode !== editor.getBody() ) {
if ( isAtRoot && node.parentNode !== editor.getBody() ) {
return false;
}

Expand All @@ -290,7 +294,7 @@
// Text node.
if ( childNodes[ i ].nodeType === 3 ) {
// Has text.
if ( node.data.length ) {
if ( childNodes[ i ].data.length ) {
return false;
}
}
Expand Down Expand Up @@ -372,14 +376,80 @@
}
}

function isInputKeyEvent( event ) {
var code = event.keyCode;
var VK = tinymce.util.VK;

if ( VK.metaKeyPressed( event ) ) {
return false;
}

// Special keys.
if ( code <= 47 && ! (
code === VK.SPACEBAR || code === VK.ENTER || code === VK.DELETE || code === VK.BACKSPACE
) ) {
return false;
}

// OS keys.
if ( code >= 91 && code <= 93 ) {
return false;
}

// Function keys.
if ( code >= 112 && code <= 123 ) {
return false;
}

// Lock keys.
if ( code >= 144 && code <= 145 ) {
return false;
}

return true;
}

var anchorNode;
var hidden = true;
var keypress = false;

editor.on( 'keydown', function( event ) {
if ( tinymce.util.VK.metaKeyPressed( event ) ) {
keypress = true;

if ( ! isInputKeyEvent( event ) ) {
return;
}

// No typing directly on elements.
if ( anchorNode.nodeType === 1 && ! isEmptySlot( anchorNode ) ) {
event.preventDefault();
} else {
hidden = true;
}
} );

editor.on( 'keyup', function( event ) {
keypress = false;
} );

editor.on( 'beforePastePreProcess beforeExecCommand', function( event ) {
if ( anchorNode.nodeType === 1 && ! isEmptySlot( anchorNode ) ) {
event.preventDefault();
}
} );

editor.on( 'input', function( event ) {
// Non key input (e.g. emoji).
if ( keypress ) {
return;
}

hidden = true;
if ( anchorNode.nodeType === 1 && ! isEmptySlot( anchorNode ) ) {
// Event not cancelable. :(
// Let's see how this goes, it might be buggy.
editor.undoManager.add();
editor.undoManager.undo();
}
} );

editor.on( 'mousedown touchstart', function() {
Expand All @@ -389,13 +459,18 @@
editor.on( 'selectionChange nodeChange', function( event ) {
var selection = window.getSelection();
var isCollapsed = selection.isCollapsed;
var anchorNode = selection.anchorNode;

if ( ! anchorNode ) {
if ( ! selection.anchorNode ) {
return;
}

if ( selection.anchorNode.parentNode.id === 'mcepastebin' ) {
return;
}

var isEmpty = isCollapsed && isEmptySlot( anchorNode );
anchorNode = selection.anchorNode;

var isEmpty = isCollapsed && isEmptySlot( anchorNode, true );
var isBlockUIVisible = ! hidden;

if ( isEmpty ) {
Expand All @@ -422,14 +497,11 @@
} else {
toolbarInline.hide();

// setTimeout( function() {
if ( isBlockUIVisible ) {
window.console.log( 'Debug: visible block UI.' );
showBlockUI();
} else {
hideBlockUI();
}
// }, 50 );
if ( isBlockUIVisible ) {
showBlockUI();
} else {
hideBlockUI();
}
}
}
} );
Expand Down

0 comments on commit a3174d1

Please sign in to comment.