From 7baa0da9658ed1b9c75874bf2d2c3158fb4feb05 Mon Sep 17 00:00:00 2001 From: Andrew Ozz Date: Mon, 9 Mar 2015 19:59:14 +0000 Subject: [PATCH] TinyMCE: when pasting an URL over a selection, insert a link with the URL instead of replacing the selection with it. Props iseulde. Fixes #31571. git-svn-id: https://develop.svn.wordpress.org/trunk@31691 602fd350-edb4-49c9-b593-d223f7449a82 --- .../js/tinymce/plugins/wpeditimage/plugin.js | 6 ++++++ .../js/tinymce/plugins/wplink/plugin.js | 19 ++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/js/tinymce/plugins/wpeditimage/plugin.js b/src/wp-includes/js/tinymce/plugins/wpeditimage/plugin.js index d8b735fb9d..89bf33a1b4 100644 --- a/src/wp-includes/js/tinymce/plugins/wpeditimage/plugin.js +++ b/src/wp-includes/js/tinymce/plugins/wpeditimage/plugin.js @@ -1220,6 +1220,12 @@ tinymce.PluginManager.add( 'wpeditimage', function( editor ) { } }); + editor.on( 'beforeexeccommand', function( event ) { + if ( isPlaceholder( editor.selection.getNode() ) ) { + event.preventDefault(); + } + } ); + return { _do_shcode: parseShortcode, _get_shcode: getShortcode diff --git a/src/wp-includes/js/tinymce/plugins/wplink/plugin.js b/src/wp-includes/js/tinymce/plugins/wplink/plugin.js index 8c055857fb..934ee30da5 100644 --- a/src/wp-includes/js/tinymce/plugins/wplink/plugin.js +++ b/src/wp-includes/js/tinymce/plugins/wplink/plugin.js @@ -1,7 +1,7 @@ /* global tinymce */ tinymce.PluginManager.add( 'wplink', function( editor ) { var linkButton; - + // Register a command so that it can be invoked by using tinyMCE.activeEditor.execCommand( 'WP_Link' ); editor.addCommand( 'WP_Link', function() { if ( ( ! linkButton || ! linkButton.disabled() ) && typeof window.wpLink !== 'undefined' ) { @@ -60,4 +60,21 @@ tinymce.PluginManager.add( 'wplink', function( editor ) { context: 'insert', prependToContext: true }); + + editor.on( 'pastepreprocess', function( event ) { + var pastedStr = event.content; + + if ( ! editor.selection.isCollapsed() ) { + pastedStr = pastedStr.replace( /<[^>]+>/g, '' ); + pastedStr = tinymce.trim( pastedStr ); + + if ( /^(?:https?:)?\/\/\S+$/i.test( pastedStr ) ) { + editor.execCommand( 'mceInsertLink', false, { + href: editor.dom.decode( pastedStr ) + } ); + + event.preventDefault(); + } + } + } ); });