-
@@ -1463,7 +1463,7 @@ final class _WP_Editors {
diff --git a/src/wp-includes/css/editor.css b/src/wp-includes/css/editor.css
index 0ebac83752..5f40aa6d76 100644
--- a/src/wp-includes/css/editor.css
+++ b/src/wp-includes/css/editor.css
@@ -1193,7 +1193,6 @@ i.mce-i-hr:before {
-webkit-box-shadow: 0 3px 6px rgba( 0, 0, 0, 0.3 );
box-shadow: 0 3px 6px rgba( 0, 0, 0, 0.3 );
width: 500px;
- height: 250px;
overflow: hidden;
margin-left: -250px;
margin-top: -125px;
@@ -1229,6 +1228,14 @@ i.mce-i-hr:before {
margin-top: -250px;
}
+#wp-link-wrap .wp-link-text-field {
+ display: none;
+}
+
+#wp-link-wrap.has-text-field .wp-link-text-field {
+ display: block;
+}
+
#link-modal-title {
background: #fcfcfc;
border-bottom: 1px solid #dfdfdf;
@@ -1395,6 +1402,10 @@ i.mce-i-hr:before {
left: 16px;
right: 16px;
bottom: 16px;
+ top: 172px;
+}
+
+.has-text-field #wp-link .query-results {
top: 205px;
}
@@ -1529,11 +1540,14 @@ i.mce-i-hr:before {
@media screen and ( max-width: 782px ) {
#wp-link-wrap {
- height: 280px;
margin-top: -140px;
}
#wp-link-wrap.search-panel-visible .query-results {
+ top: 195px;
+ }
+
+ #wp-link-wrap.search-panel-visible.has-text-field .query-results {
top: 235px;
}
diff --git a/src/wp-includes/js/tinymce/plugins/wplink/plugin.js b/src/wp-includes/js/tinymce/plugins/wplink/plugin.js
index 934ee30da5..8dacc92388 100644
--- a/src/wp-includes/js/tinymce/plugins/wplink/plugin.js
+++ b/src/wp-includes/js/tinymce/plugins/wplink/plugin.js
@@ -1,12 +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' ) {
- window.wpLink.open( editor.id );
- }
+ window.wpLink && window.wpLink.open( editor.id );
});
// WP default shortcut
@@ -14,41 +9,18 @@ tinymce.PluginManager.add( 'wplink', function( editor ) {
// The "de-facto standard" shortcut, see #27305
editor.addShortcut( 'ctrl+k', '', 'WP_Link' );
- function setState( button, node ) {
- var parent = editor.dom.getParent( node, 'a' ),
- getView = editor.plugins.wpview ? editor.plugins.wpview.getView : function() { return false; };
-
- button.disabled( ( editor.selection.isCollapsed() && ! parent ) || ( parent && ! parent.href ) || getView( node ) );
- button.active( parent && parent.href );
- }
-
editor.addButton( 'link', {
icon: 'link',
tooltip: 'Insert/edit link',
shortcut: 'Alt+Shift+A',
cmd: 'WP_Link',
-
- onPostRender: function() {
- linkButton = this;
-
- editor.on( 'nodechange', function( event ) {
- setState( linkButton, event.element );
- });
- }
+ stateSelector: 'a[href]'
});
editor.addButton( 'unlink', {
icon: 'unlink',
tooltip: 'Remove link',
- cmd: 'unlink',
-
- onPostRender: function() {
- var unlinkButton = this;
-
- editor.on( 'nodechange', function( event ) {
- setState( unlinkButton, event.element );
- });
- }
+ cmd: 'unlink'
});
editor.addMenuItem( 'link', {
diff --git a/src/wp-includes/js/wplink.js b/src/wp-includes/js/wplink.js
index 692e012cc1..0033377c71 100644
--- a/src/wp-includes/js/wplink.js
+++ b/src/wp-includes/js/wplink.js
@@ -7,6 +7,10 @@ var wpLink;
rivers = {},
isTouch = ( 'ontouchend' in document );
+ function getLink() {
+ return editor.dom.getParent( editor.selection.getNode(), 'a' );
+ }
+
wpLink = {
timeToTriggerRiver: 150,
minRiverAJAXDuration: 200,
@@ -21,14 +25,14 @@ var wpLink;
inputs.backdrop = $( '#wp-link-backdrop' );
inputs.submit = $( '#wp-link-submit' );
inputs.close = $( '#wp-link-close' );
- // URL
- inputs.url = $( '#url-field' );
+
+ // Input
+ inputs.text = $( '#wp-link-text' );
+ inputs.url = $( '#wp-link-url' );
inputs.nonce = $( '#_ajax_linking_nonce' );
- // Secondary options
- inputs.title = $( '#link-title-field' );
- // Advanced Options
- inputs.openInNewTab = $( '#link-target-checkbox' );
- inputs.search = $( '#search-field' );
+ inputs.openInNewTab = $( '#wp-link-target' );
+ inputs.search = $( '#wp-link-search' );
+
// Build Rivers
rivers.search = new River( $( '#search-results' ) );
rivers.recent = new River( $( '#most-recent-results' ) );
@@ -129,6 +133,7 @@ var wpLink;
inputs.backdrop.show();
wpLink.refresh();
+
$( document ).trigger( 'wplink-open', inputs.wrap );
},
@@ -144,6 +149,8 @@ var wpLink;
if ( wpLink.isMCE() ) {
wpLink.mceRefresh();
} else {
+ inputs.wrap.removeClass( 'has-text-field' );
+ inputs.text.val( '' );
wpLink.setDefaultValues();
}
@@ -154,7 +161,11 @@ var wpLink;
// Focus the URL field and highlight its contents.
// If this is moved above the selection changes,
// IE will show a flashing cursor over the dialog.
- inputs.url.focus()[0].select();
+ if ( inputs.wrap.hasClass( 'has-text-field' ) ) {
+ inputs.text.focus();
+ } else {
+ inputs.url.focus()[0].select();
+ }
}
// Load the most recent results if this is the first time opening the panel.
@@ -165,22 +176,53 @@ var wpLink;
correctedURL = inputs.url.val().replace( /^http:\/\//, '' );
},
+ hasSelectedText: function( linkNode ) {
+ var html = editor.selection.getContent();
+
+ // Partial html and not a fully selected anchor element
+ if ( /]+>[^<]+<\/a>$/.test( html ) || html.indexOf('href=') === -1 ) ) {
+ return false;
+ }
+
+ if ( linkNode ) {
+ var nodes = linkNode.childNodes, i;
+
+ if ( nodes.length === 0 ) {
+ return false;
+ }
+
+ for ( i = nodes.length - 1; i >= 0; i-- ) {
+ if ( nodes[i].nodeType != 3 ) {
+ return false;
+ }
+ }
+ }
+
+ return true;
+ },
+
mceRefresh: function() {
- var e;
+ var text,
+ selectedNode = editor.selection.getNode(),
+ linkNode = editor.dom.getParent( selectedNode, 'a[href]' ),
+ onlyText = this.hasSelectedText( linkNode );
- // If link exists, select proper values.
- if ( e = editor.dom.getParent( editor.selection.getNode(), 'A' ) ) {
- // Set URL and description.
- inputs.url.val( editor.dom.getAttrib( e, 'href' ) );
- inputs.title.val( editor.dom.getAttrib( e, 'title' ) );
- // Set open in new tab.
- inputs.openInNewTab.prop( 'checked', ( '_blank' === editor.dom.getAttrib( e, 'target' ) ) );
- // Update save prompt.
+ if ( linkNode ) {
+ text = linkNode.innerText || linkNode.textContent;
+ inputs.url.val( editor.dom.getAttrib( linkNode, 'href' ) );
+ inputs.openInNewTab.prop( 'checked', '_blank' === editor.dom.getAttrib( linkNode, 'target' ) );
inputs.submit.val( wpLinkL10n.update );
-
- // If there's no link, set the default values.
} else {
- wpLink.setDefaultValues();
+ text = editor.selection.getContent({ format: 'text' });
+ this.setDefaultValues();
+ }
+
+ if ( onlyText ) {
+ inputs.text.val( text || '' );
+ inputs.wrap.addClass( 'has-text-field' );
+ } else {
+ inputs.text.val( '' );
+ inputs.wrap.removeClass( 'has-text-field' );
}
},
@@ -209,39 +251,37 @@ var wpLink;
getAttrs: function() {
return {
href: $.trim( inputs.url.val() ),
- title: $.trim( inputs.title.val() ),
target: inputs.openInNewTab.prop( 'checked' ) ? '_blank' : ''
};
},
update: function() {
- if ( wpLink.isMCE() )
+ if ( wpLink.isMCE() ) {
wpLink.mceUpdate();
- else
+ } else {
wpLink.htmlUpdate();
+ }
},
htmlUpdate: function() {
- var attrs, html, begin, end, cursor, title, selection,
+ var attrs, text, html, begin, end, cursor, selection,
textarea = wpLink.textarea;
- if ( ! textarea )
+ if ( ! textarea ) {
return;
+ }
attrs = wpLink.getAttrs();
+ text = inputs.text.val();
// If there's no href, return.
- if ( ! attrs.href )
+ if ( ! attrs.href ) {
return;
+ }
// Build HTML
html = '
/g, '>' ).replace( /"/g, '"' );
- html += ' title="' + title + '"';
- }
-
if ( attrs.target ) {
html += ' target="' + attrs.target + '"';
}
@@ -254,25 +294,29 @@ var wpLink;
// Note: If no text is selected, IE will not place the cursor
// inside the closing tag.
textarea.focus();
- wpLink.range.text = html + wpLink.range.text + '';
+ wpLink.range.text = html + ( text || wpLink.range.text ) + '';
wpLink.range.moveToBookmark( wpLink.range.getBookmark() );
wpLink.range.select();
wpLink.range = null;
} else if ( typeof textarea.selectionStart !== 'undefined' ) {
// W3C
- begin = textarea.selectionStart;
- end = textarea.selectionEnd;
- selection = textarea.value.substring( begin, end );
- html = html + selection + '';
- cursor = begin + html.length;
+ begin = textarea.selectionStart;
+ end = textarea.selectionEnd;
+ selection = text || textarea.value.substring( begin, end );
+ html = html + selection + '';
+ cursor = begin + html.length;
// If no text is selected, place the cursor inside the closing tag.
- if ( begin == end )
- cursor -= ''.length;
+ if ( begin === end && ! selection ) {
+ cursor -= 4;
+ }
- textarea.value = textarea.value.substring( 0, begin ) + html +
- textarea.value.substring( end, textarea.value.length );
+ textarea.value = (
+ textarea.value.substring( 0, begin ) +
+ html +
+ textarea.value.substring( end, textarea.value.length )
+ );
// Update cursor position
textarea.selectionStart = textarea.selectionEnd = cursor;
@@ -283,8 +327,8 @@ var wpLink;
},
mceUpdate: function() {
- var link,
- attrs = wpLink.getAttrs();
+ var attrs = wpLink.getAttrs(),
+ link, text;
wpLink.close();
editor.focus();
@@ -293,33 +337,50 @@ var wpLink;
editor.selection.moveToBookmark( editor.windowManager.bookmark );
}
- link = editor.dom.getParent( editor.selection.getNode(), 'a[href]' );
-
- // If the values are empty, unlink and return
- if ( ! attrs.href || attrs.href == 'http://' ) {
+ if ( ! attrs.href ) {
editor.execCommand( 'unlink' );
return;
}
+ link = getLink();
+ text = inputs.text.val();
+
if ( link ) {
+ if ( text ) {
+ if ( 'innerText' in link ) {
+ link.innerText = text;
+ } else {
+ link.textContent = text;
+ }
+ }
+
editor.dom.setAttribs( link, attrs );
} else {
- editor.execCommand( 'mceInsertLink', false, attrs );
+ if ( text ) {
+ editor.selection.setNode( editor.dom.create( 'a', attrs, text ) );
+ } else {
+ editor.execCommand( 'mceInsertLink', false, attrs );
+ }
}
-
- // Move the cursor to the end of the selection
- editor.selection.collapse();
},
updateFields: function( e, li ) {
inputs.url.val( li.children( '.item-permalink' ).val() );
- inputs.title.val( li.hasClass( 'no-title' ) ? '' : li.children( '.item-title' ).text() );
},
setDefaultValues: function() {
- var selection = editor && editor.selection.getContent(),
+ var selection,
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,
+ text;
+
+ 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 && emailRegexp.test( selection ) ) {
// Selection is email address
@@ -332,9 +393,6 @@ var wpLink;
inputs.url.val( '' );
}
- // Set description to default.
- inputs.title.val( '' );
-
// Update save prompt.
inputs.submit.val( wpLinkL10n.save );
},