Skip to content

Commit

Permalink
TinyMCE wpView: better removal of views, replace a selected view when…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
azaozz committed Jul 19, 2014
1 parent 47b735b commit 16696e9
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 40 deletions.
76 changes: 37 additions & 39 deletions wp-includes/js/tinymce/plugins/wpview/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -185,6 +194,10 @@ tinymce.PluginManager.add( 'wpview', function( editor ) {
return;
}

if ( selected ) {
removeView( selected );
}

if ( ! event.initial ) {
wp.mce.views.unbind( editor );
}
Expand Down Expand Up @@ -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 );
}
}

Expand Down Expand Up @@ -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 ) {
toRemove = selected;
} else {
editor.dom.remove( selected );
}
// 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;
}
return;
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -437,26 +441,23 @@ tinymce.PluginManager.add( 'wpview', function( editor ) {
}
}

// Make sure we don't eat any content.
if ( event.keyCode === VK.BACKSPACE ) {
if ( editor.dom.isEmpty( node ) ) {
if ( view = getView( node.previousSibling ) ) {
if ( ! view ) {
// Make sure we don't eat any content.
if ( event.keyCode === VK.BACKSPACE ) {
if ( editor.dom.isEmpty( node ) ) {
if ( view = getView( node.previousSibling ) ) {
setViewCursor( false, view );
editor.dom.remove( node );
event.preventDefault();
}
} else if ( ( range = selection.getRng() ) &&
range.startOffset === 0 &&
range.endOffset === 0 &&
( view = getView( node.previousSibling ) ) ) {
setViewCursor( false, view );
editor.dom.remove( node );
event.preventDefault();
return;
}
} else if ( ( range = selection.getRng() ) &&
range.startOffset === 0 &&
range.endOffset === 0 &&
( view = getView( node.previousSibling ) ) ) {
setViewCursor( false, view );
event.preventDefault();
return;
}
}

if ( ! view ) {
return;
}

Expand Down Expand Up @@ -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 );
Expand All @@ -534,7 +532,7 @@ tinymce.PluginManager.add( 'wpview', function( editor ) {

editor.on( 'keyup', function() {
if ( toRemove ) {
editor.dom.remove( toRemove );
removeView( toRemove );
toRemove = false;
}
});
Expand Down
2 changes: 1 addition & 1 deletion wp-includes/js/tinymce/plugins/wpview/plugin.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified wp-includes/js/tinymce/wp-tinymce.js.gz
Binary file not shown.

0 comments on commit 16696e9

Please sign in to comment.