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
This commit is contained in:
azaozz 2016-02-27 21:32:02 +00:00
parent a7a822ec8e
commit 70b1601fc4
2 changed files with 65 additions and 63 deletions

View File

@ -78,6 +78,7 @@
}, },
reset: function() { reset: function() {
var urlInput = this.getEl().firstChild; var urlInput = this.getEl().firstChild;
urlInput.value = ''; urlInput.value = '';
urlInput.nextSibling.value = ''; urlInput.nextSibling.value = '';
} }
@ -88,6 +89,7 @@
var editToolbar; var editToolbar;
var previewInstance; var previewInstance;
var inputInstance; var inputInstance;
var linkNode;
var $ = window.jQuery; var $ = window.jQuery;
function getSelectedLink() { function getSelectedLink() {
@ -107,7 +109,6 @@
if ( link ) { if ( link ) {
editor.selection.select( link ); editor.selection.select( link );
editor.nodeChanged();
} }
} }
} }
@ -152,9 +153,14 @@
editToolbar.on( 'show', function() { editToolbar.on( 'show', function() {
if ( ! tinymce.$( document.body ).hasClass( 'modal-open' ) ) { if ( ! tinymce.$( document.body ).hasClass( 'modal-open' ) ) {
window.setTimeout( function() { 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 ) {
if ( ! element.value && selection && typeof window.wpLink !== 'undefined' ) {
element.value = window.wpLink.getUrlFromSelection( selection );
}
element.focus(); element.focus();
element.select(); element.select();
} }
@ -171,8 +177,6 @@
} ); } );
editor.addCommand( 'WP_Link', function() { editor.addCommand( 'WP_Link', function() {
var link = getSelectedLink();
if ( tinymce.Env.ie && tinymce.Env.ie < 10 ) { if ( tinymce.Env.ie && tinymce.Env.ie < 10 ) {
if ( typeof window.wpLink !== 'undefined' ) { if ( typeof window.wpLink !== 'undefined' ) {
window.wpLink.open( editor.id ); window.wpLink.open( editor.id );
@ -181,18 +185,16 @@
return; return;
} }
linkNode = getSelectedLink();
editToolbar.tempHide = false; editToolbar.tempHide = false;
if ( link ) { if ( linkNode ) {
editor.dom.setAttribs( link, { 'data-wplink-edit': true } ); editor.dom.setAttribs( linkNode, { 'data-wplink-edit': true } );
} else { } else {
removePlaceholders(); removePlaceholders();
editor.execCommand( 'mceInsertLink', false, { href: '_wp_link_placeholder' } ); editor.execCommand( 'mceInsertLink', false, { href: '_wp_link_placeholder' } );
if ( tinymce.Env.ie ) { linkNode = editor.$( 'a[href="_wp_link_placeholder"]' )[0];
editor.windowManager.wplinkBookmark = editor.selection.getBookmark();
}
editor.nodeChanged(); editor.nodeChanged();
} }
} ); } );
@ -202,19 +204,13 @@
return; return;
} }
var href, text, var href, text;
linkNode = getSelectedLink();
if ( linkNode ) { if ( linkNode ) {
href = inputInstance.getURL(); href = inputInstance.getURL();
text = inputInstance.getLinkText(); text = inputInstance.getLinkText();
editor.focus(); editor.focus();
if ( tinymce.isIE ) {
editor.selection.moveToBookmark( editor.windowManager.wplinkBookmark );
editor.windowManager.wplinkBookmark = null;
}
if ( ! href ) { if ( ! href ) {
editor.dom.remove( linkNode, true ); editor.dom.remove( linkNode, true );
return; return;
@ -240,12 +236,6 @@
inputInstance.reset(); inputInstance.reset();
removePlaceholders(); removePlaceholders();
editor.focus(); editor.focus();
if ( tinymce.isIE ) {
editor.selection.moveToBookmark( editor.windowManager.wplinkBookmark );
editor.windowManager.wplinkBookmark = null;
}
editToolbar.tempHide = false; editToolbar.tempHide = false;
} }
} ); } );
@ -456,7 +446,7 @@
text = inputInstance.getLinkText() || null; text = inputInstance.getLinkText() || null;
editor.focus(); // Needed for IE editor.focus(); // Needed for IE
window.wpLink.open( editor.id, url, text ); window.wpLink.open( editor.id, url, text, linkNode );
editToolbar.tempHide = true; editToolbar.tempHide = true;
inputInstance.reset(); inputInstance.reset();

View File

@ -2,12 +2,12 @@
var wpLink; var wpLink;
( function( $ ) { ( function( $ ) {
var editor, correctedURL, var editor, correctedURL, linkNode,
inputs = {}, inputs = {},
isTouch = ( 'ontouchend' in document ); isTouch = ( 'ontouchend' in document );
function getLink() { function getLink() {
return editor.dom.getParent( editor.selection.getNode(), 'a' ); return linkNode || editor.dom.getParent( editor.selection.getNode(), 'a[href]' );
} }
wpLink = { wpLink = {
@ -125,11 +125,12 @@ var wpLink;
} }
}, },
open: function( editorId, url, text ) { open: function( editorId, url, text, node ) {
var ed, var ed,
$body = $( document.body ); $body = $( document.body );
$body.addClass( 'modal-open' ); $body.addClass( 'modal-open' );
linkNode = node;
wpLink.range = null; wpLink.range = null;
@ -258,20 +259,20 @@ var wpLink;
url = url || editor.dom.getAttrib( linkNode, 'href' ); url = url || editor.dom.getAttrib( linkNode, 'href' );
if ( url === '_wp_link_placeholder' ) { if ( url !== '_wp_link_placeholder' ) {
url = ''; 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 { } else {
text = editor.selection.getContent({ format: 'text' }) || text; linkText = editor.selection.getContent({ format: 'text' }) || text || '';
this.setDefaultValues(); this.setDefaultValues( linkText );
} }
if ( onlyText ) { if ( onlyText ) {
inputs.text.val( linkText || '' ); inputs.text.val( linkText );
inputs.wrap.addClass( 'has-text-field' ); inputs.wrap.addClass( 'has-text-field' );
} else { } else {
inputs.text.val( '' ); inputs.text.val( '' );
@ -279,22 +280,24 @@ var wpLink;
} }
}, },
close: function() { close: function( reset ) {
$( document.body ).removeClass( 'modal-open' ); $( document.body ).removeClass( 'modal-open' );
if ( ! wpLink.isMCE() ) { if ( reset !== 'noReset' ) {
wpLink.textarea.focus(); if ( ! wpLink.isMCE() ) {
wpLink.textarea.focus();
if ( wpLink.range ) { if ( wpLink.range ) {
wpLink.range.moveToBookmark( wpLink.range.getBookmark() ); wpLink.range.moveToBookmark( wpLink.range.getBookmark() );
wpLink.range.select(); wpLink.range.select();
} }
} else { } else {
if ( editor.plugins.wplink ) { if ( editor.plugins.wplink ) {
editor.plugins.wplink.close(); editor.plugins.wplink.close();
} }
editor.focus(); editor.focus();
}
} }
inputs.backdrop.hide(); inputs.backdrop.hide();
@ -394,13 +397,14 @@ var wpLink;
editor.focus(); editor.focus();
if ( tinymce.isIE ) { if ( tinymce.isIE && editor.windowManager.wplinkBookmark ) {
editor.selection.moveToBookmark( editor.windowManager.wplinkBookmark ); editor.selection.moveToBookmark( editor.windowManager.wplinkBookmark );
editor.windowManager.wplinkBookmark = null; editor.windowManager.wplinkBookmark = null;
} }
if ( ! attrs.href ) { if ( ! attrs.href ) {
editor.execCommand( 'unlink' ); editor.execCommand( 'unlink' );
wpLink.close();
return; return;
} }
@ -428,7 +432,8 @@ var wpLink;
} }
} }
wpLink.close(); wpLink.close( 'noReset' );
editor.focus();
editor.nodeChanged(); editor.nodeChanged();
}, },
@ -455,30 +460,37 @@ var wpLink;
} }
}, },
setDefaultValues: function() { getUrlFromSelection: function( selection ) {
var selection, var url,
emailRegexp = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i, emailRegexp = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i,
urlRegexp = /^(https?|ftp):\/\/[A-Z0-9.-]+\.[A-Z]{2,4}[^ "]*$/i; urlRegexp = /^(https?|ftp):\/\/[A-Z0-9.-]+\.[A-Z]{2,4}[^ "]*$/i;
if ( this.isMCE() ) { if ( ! selection ) {
selection = editor.selection.getContent(); if ( this.isMCE() ) {
} else if ( document.selection && wpLink.range ) { selection = editor.selection.getContent({ format: 'text' });
selection = wpLink.range.text; } else if ( document.selection && wpLink.range ) {
} else if ( typeof this.textarea.selectionStart !== 'undefined' ) { selection = wpLink.range.text;
selection = this.textarea.value.substring( this.textarea.selectionStart, this.textarea.selectionEnd ); } 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 ) ) { if ( selection && emailRegexp.test( selection ) ) {
// Selection is email address // Selection is email address
inputs.url.val( 'mailto:' + selection ); return 'mailto:' + selection;
} else if ( selection && urlRegexp.test( selection ) ) { } else if ( selection && urlRegexp.test( selection ) ) {
// Selection is URL // Selection is URL
inputs.url.val( selection.replace( /&amp;|&#0?38;/gi, '&' ) ); return selection.replace( /&amp;|&#0?38;/gi, '&' );
} else {
// Set URL to default.
inputs.url.val( '' );
} }
return '';
},
setDefaultValues: function( selection ) {
inputs.url.val( this.getUrlFromSelection( selection ) );
// Update save prompt. // Update save prompt.
inputs.submit.val( wpLinkL10n.save ); inputs.submit.val( wpLinkL10n.save );
} }