TinyMCE wpView: better removal of views, replace a selected view when pasting. Props avryl, fixes #28913.

Built from https://develop.svn.wordpress.org/trunk@29246


git-svn-id: http://core.svn.wordpress.org/trunk@29030 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Andrew Ozz 2014-07-19 07:30:15 +00:00
parent 47b735b0e4
commit 16696e9ea8
3 changed files with 41 additions and 43 deletions

View File

@ -101,6 +101,15 @@ tinymce.PluginManager.add( 'wpview', function( editor ) {
editor.nodeChanged();
}
function removeView( view ) {
// TODO: trigger an event to run a clean up function.
// Maybe `jQuery( view ).trigger( 'remove' );`?
editor.undoManager.transact( function() {
handleEnter( view );
editor.dom.remove( view );
});
}
function select( viewNode ) {
var clipboard,
dom = editor.dom;
@ -185,6 +194,10 @@ tinymce.PluginManager.add( 'wpview', function( editor ) {
return;
}
if ( selected ) {
removeView( selected );
}
if ( ! event.initial ) {
wp.mce.views.unbind( editor );
}
@ -293,7 +306,7 @@ tinymce.PluginManager.add( 'wpview', function( editor ) {
if ( editor.dom.hasClass( event.target, 'edit' ) ) {
wp.mce.views.edit( view );
} else if ( editor.dom.hasClass( event.target, 'remove' ) ) {
editor.dom.remove( view );
removeView( view );
}
}
@ -340,20 +353,15 @@ tinymce.PluginManager.add( 'wpview', function( editor ) {
dom = editor.dom,
selection = editor.selection,
node, view, cursorBefore, cursorAfter,
range, clonedRange, tempRange, remove;
range, clonedRange, tempRange;
if ( selected ) {
// Let key presses that involve the command or control keys through.
// Also, let any of the F# keys through.
if ( event.metaKey || event.ctrlKey || ( key >= 112 && key <= 123 ) ) {
// But remove the view when cmd/ctrl + x/backspace are pressed.
if ( ( event.metaKey || event.ctrlKey ) && ( key === 88 || key === VK.BACKSPACE ) ) {
// We'll remove a cut view on keyup, otherwise the browser can't copy the content.
if ( key === 88 ) {
// Ignore key presses that involve the command or control key, but continue when in combination with backspace or v.
// Also ignore the F# keys.
if ( ( ( event.metaKey || event.ctrlKey ) && key !== VK.BACKSPACE && key !== 86 ) || ( key >= 112 && key <= 123 ) ) {
// Remove the view when pressing cmd/ctrl+x on keyup, otherwise the browser can't copy the content.
if ( ( event.metaKey || event.ctrlKey ) && key === 88 ) {
toRemove = selected;
} else {
editor.dom.remove( selected );
}
}
return;
}
@ -399,11 +407,7 @@ tinymce.PluginManager.add( 'wpview', function( editor ) {
event.preventDefault();
// Ignore keys that don't insert anything.
} else if ( ( key > 47 || VK.SPACEBAR || key === VK.ENTER || key === VK.DELETE || key === VK.BACKSPACE ) && key !== 144 && key !== 145 ) {
editor.undoManager.transact( function() {
remove = selected;
handleEnter( selected );
dom.remove( remove );
});
removeView( selected );
if ( key === VK.ENTER || key === VK.DELETE || key === VK.BACKSPACE ) {
event.preventDefault();
@ -437,6 +441,7 @@ tinymce.PluginManager.add( 'wpview', function( editor ) {
}
}
if ( ! view ) {
// Make sure we don't eat any content.
if ( event.keyCode === VK.BACKSPACE ) {
if ( editor.dom.isEmpty( node ) ) {
@ -444,7 +449,6 @@ tinymce.PluginManager.add( 'wpview', function( editor ) {
setViewCursor( false, view );
editor.dom.remove( node );
event.preventDefault();
return;
}
} else if ( ( range = selection.getRng() ) &&
range.startOffset === 0 &&
@ -452,11 +456,8 @@ tinymce.PluginManager.add( 'wpview', function( editor ) {
( view = getView( node.previousSibling ) ) ) {
setViewCursor( false, view );
event.preventDefault();
return;
}
}
if ( ! view ) {
return;
}
@ -515,10 +516,7 @@ tinymce.PluginManager.add( 'wpview', function( editor ) {
select( view );
event.preventDefault();
} else if ( cursorAfter && key === VK.BACKSPACE ) {
editor.undoManager.transact( function() {
handleEnter( view );
dom.remove( view );
});
removeView( view );
event.preventDefault();
} else if ( cursorAfter ) {
handleEnter( view );
@ -534,7 +532,7 @@ tinymce.PluginManager.add( 'wpview', function( editor ) {
editor.on( 'keyup', function() {
if ( toRemove ) {
editor.dom.remove( toRemove );
removeView( toRemove );
toRemove = false;
}
});

File diff suppressed because one or more lines are too long