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

File diff suppressed because one or more lines are too long