From 70b1601fc46908df0c04e1700dfe6bc050e319f4 Mon Sep 17 00:00:00 2001 From: azaozz Date: Sat, 27 Feb 2016 21:32:02 +0000 Subject: [PATCH] TinyMCE, inline link: - Fix in IE (again). Remove setting/getting placeholders, pass the link node instead. - In the inline dialog: when the selected text looks like URL or email, pre-fill the URL field with it (same as in the modal). - Fix setting the name of the main button in the modal: Add Link or Update. - In the modal when clicking Update remove the link if the URL field is empty. That matches the inline dialog behaviour. Otherwise the modal remains open, nothing happens when clicking the Update button there. See #33301. git-svn-id: https://develop.svn.wordpress.org/trunk@36747 602fd350-edb4-49c9-b593-d223f7449a82 --- .../js/tinymce/plugins/wplink/plugin.js | 38 +++----- src/wp-includes/js/wplink.js | 90 +++++++++++-------- 2 files changed, 65 insertions(+), 63 deletions(-) diff --git a/src/wp-includes/js/tinymce/plugins/wplink/plugin.js b/src/wp-includes/js/tinymce/plugins/wplink/plugin.js index 9a91dc3b71..c47b4c384f 100644 --- a/src/wp-includes/js/tinymce/plugins/wplink/plugin.js +++ b/src/wp-includes/js/tinymce/plugins/wplink/plugin.js @@ -78,6 +78,7 @@ }, reset: function() { var urlInput = this.getEl().firstChild; + urlInput.value = ''; urlInput.nextSibling.value = ''; } @@ -88,6 +89,7 @@ var editToolbar; var previewInstance; var inputInstance; + var linkNode; var $ = window.jQuery; function getSelectedLink() { @@ -107,7 +109,6 @@ if ( link ) { editor.selection.select( link ); - editor.nodeChanged(); } } } @@ -152,9 +153,14 @@ editToolbar.on( 'show', function() { if ( ! tinymce.$( document.body ).hasClass( 'modal-open' ) ) { window.setTimeout( function() { - var element = editToolbar.$el.find( 'input.ui-autocomplete-input' )[0]; + var element = editToolbar.$el.find( 'input.ui-autocomplete-input' )[0], + selection = linkNode && ( linkNode.textContent || linkNode.innerText ); if ( element ) { + if ( ! element.value && selection && typeof window.wpLink !== 'undefined' ) { + element.value = window.wpLink.getUrlFromSelection( selection ); + } + element.focus(); element.select(); } @@ -171,8 +177,6 @@ } ); editor.addCommand( 'WP_Link', function() { - var link = getSelectedLink(); - if ( tinymce.Env.ie && tinymce.Env.ie < 10 ) { if ( typeof window.wpLink !== 'undefined' ) { window.wpLink.open( editor.id ); @@ -181,18 +185,16 @@ return; } + linkNode = getSelectedLink(); editToolbar.tempHide = false; - if ( link ) { - editor.dom.setAttribs( link, { 'data-wplink-edit': true } ); + if ( linkNode ) { + editor.dom.setAttribs( linkNode, { 'data-wplink-edit': true } ); } else { removePlaceholders(); editor.execCommand( 'mceInsertLink', false, { href: '_wp_link_placeholder' } ); - if ( tinymce.Env.ie ) { - editor.windowManager.wplinkBookmark = editor.selection.getBookmark(); - } - + linkNode = editor.$( 'a[href="_wp_link_placeholder"]' )[0]; editor.nodeChanged(); } } ); @@ -202,19 +204,13 @@ return; } - var href, text, - linkNode = getSelectedLink(); + var href, text; if ( linkNode ) { href = inputInstance.getURL(); text = inputInstance.getLinkText(); editor.focus(); - if ( tinymce.isIE ) { - editor.selection.moveToBookmark( editor.windowManager.wplinkBookmark ); - editor.windowManager.wplinkBookmark = null; - } - if ( ! href ) { editor.dom.remove( linkNode, true ); return; @@ -240,12 +236,6 @@ inputInstance.reset(); removePlaceholders(); editor.focus(); - - if ( tinymce.isIE ) { - editor.selection.moveToBookmark( editor.windowManager.wplinkBookmark ); - editor.windowManager.wplinkBookmark = null; - } - editToolbar.tempHide = false; } } ); @@ -456,7 +446,7 @@ text = inputInstance.getLinkText() || null; editor.focus(); // Needed for IE - window.wpLink.open( editor.id, url, text ); + window.wpLink.open( editor.id, url, text, linkNode ); editToolbar.tempHide = true; inputInstance.reset(); diff --git a/src/wp-includes/js/wplink.js b/src/wp-includes/js/wplink.js index 447e723e42..7a0e61a096 100644 --- a/src/wp-includes/js/wplink.js +++ b/src/wp-includes/js/wplink.js @@ -2,12 +2,12 @@ var wpLink; ( function( $ ) { - var editor, correctedURL, + var editor, correctedURL, linkNode, inputs = {}, isTouch = ( 'ontouchend' in document ); function getLink() { - return editor.dom.getParent( editor.selection.getNode(), 'a' ); + return linkNode || editor.dom.getParent( editor.selection.getNode(), 'a[href]' ); } wpLink = { @@ -125,11 +125,12 @@ var wpLink; } }, - open: function( editorId, url, text ) { + open: function( editorId, url, text, node ) { var ed, $body = $( document.body ); $body.addClass( 'modal-open' ); + linkNode = node; wpLink.range = null; @@ -258,20 +259,20 @@ var wpLink; url = url || editor.dom.getAttrib( linkNode, 'href' ); - if ( url === '_wp_link_placeholder' ) { - url = ''; + if ( url !== '_wp_link_placeholder' ) { + inputs.url.val( url ); + inputs.openInNewTab.prop( 'checked', '_blank' === editor.dom.getAttrib( linkNode, 'target' ) ); + inputs.submit.val( wpLinkL10n.update ); + } else { + this.setDefaultValues( linkText ); } - - inputs.url.val( url ); - inputs.openInNewTab.prop( 'checked', '_blank' === editor.dom.getAttrib( linkNode, 'target' ) ); - inputs.submit.val( wpLinkL10n.update ); } else { - text = editor.selection.getContent({ format: 'text' }) || text; - this.setDefaultValues(); + linkText = editor.selection.getContent({ format: 'text' }) || text || ''; + this.setDefaultValues( linkText ); } if ( onlyText ) { - inputs.text.val( linkText || '' ); + inputs.text.val( linkText ); inputs.wrap.addClass( 'has-text-field' ); } else { inputs.text.val( '' ); @@ -279,22 +280,24 @@ var wpLink; } }, - close: function() { + close: function( reset ) { $( document.body ).removeClass( 'modal-open' ); - if ( ! wpLink.isMCE() ) { - wpLink.textarea.focus(); + if ( reset !== 'noReset' ) { + if ( ! wpLink.isMCE() ) { + wpLink.textarea.focus(); - if ( wpLink.range ) { - wpLink.range.moveToBookmark( wpLink.range.getBookmark() ); - wpLink.range.select(); - } - } else { - if ( editor.plugins.wplink ) { - editor.plugins.wplink.close(); - } + if ( wpLink.range ) { + wpLink.range.moveToBookmark( wpLink.range.getBookmark() ); + wpLink.range.select(); + } + } else { + if ( editor.plugins.wplink ) { + editor.plugins.wplink.close(); + } - editor.focus(); + editor.focus(); + } } inputs.backdrop.hide(); @@ -394,13 +397,14 @@ var wpLink; editor.focus(); - if ( tinymce.isIE ) { + if ( tinymce.isIE && editor.windowManager.wplinkBookmark ) { editor.selection.moveToBookmark( editor.windowManager.wplinkBookmark ); editor.windowManager.wplinkBookmark = null; } if ( ! attrs.href ) { editor.execCommand( 'unlink' ); + wpLink.close(); return; } @@ -428,7 +432,8 @@ var wpLink; } } - wpLink.close(); + wpLink.close( 'noReset' ); + editor.focus(); editor.nodeChanged(); }, @@ -455,30 +460,37 @@ var wpLink; } }, - setDefaultValues: function() { - var selection, + getUrlFromSelection: function( selection ) { + var url, emailRegexp = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i, urlRegexp = /^(https?|ftp):\/\/[A-Z0-9.-]+\.[A-Z]{2,4}[^ "]*$/i; - if ( this.isMCE() ) { - selection = editor.selection.getContent(); - } else if ( document.selection && wpLink.range ) { - selection = wpLink.range.text; - } else if ( typeof this.textarea.selectionStart !== 'undefined' ) { - selection = this.textarea.value.substring( this.textarea.selectionStart, this.textarea.selectionEnd ); + if ( ! selection ) { + if ( this.isMCE() ) { + selection = editor.selection.getContent({ format: 'text' }); + } else if ( document.selection && wpLink.range ) { + selection = wpLink.range.text; + } else if ( typeof this.textarea.selectionStart !== 'undefined' ) { + selection = this.textarea.value.substring( this.textarea.selectionStart, this.textarea.selectionEnd ); + } } + selection = tinymce.trim( selection ); + if ( selection && emailRegexp.test( selection ) ) { // Selection is email address - inputs.url.val( 'mailto:' + selection ); + return 'mailto:' + selection; } else if ( selection && urlRegexp.test( selection ) ) { // Selection is URL - inputs.url.val( selection.replace( /&|�?38;/gi, '&' ) ); - } else { - // Set URL to default. - inputs.url.val( '' ); + return selection.replace( /&|�?38;/gi, '&' ); } + return ''; + }, + + setDefaultValues: function( selection ) { + inputs.url.val( this.getUrlFromSelection( selection ) ); + // Update save prompt. inputs.submit.val( wpLinkL10n.save ); }