TinyMCE, inline link dialog:

- Fix passing values to the (old) modal on open when non-linked text is selected and Advanced is clicked before pasting an URL.
- When the user has selected text partially including a link and opens the editing dialog, auto-select the link only. Helps when a linked word is selected by double-clicking.
- Remove all placeholders on saving.
- Do not add undo level on inserting link placeholder.
- Remove the placeholder when canceling from the modal.

See #33301.
Built from https://develop.svn.wordpress.org/trunk@36602


git-svn-id: http://core.svn.wordpress.org/trunk@36569 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Andrew Ozz 2016-02-20 21:36:25 +00:00
parent f636747fa4
commit 30feff9c99
6 changed files with 84 additions and 36 deletions

View File

@ -41,7 +41,7 @@
if ( url.length > 40 && ( index = url.indexOf( '/' ) ) !== -1 && ( lastIndex = url.lastIndexOf( '/' ) ) !== -1 && lastIndex !== index ) { if ( url.length > 40 && ( index = url.indexOf( '/' ) ) !== -1 && ( lastIndex = url.lastIndexOf( '/' ) ) !== -1 && lastIndex !== index ) {
// If the beginning + ending are shorter that 40 chars, show more of the ending // If the beginning + ending are shorter that 40 chars, show more of the ending
if ( index + url.length - lastIndex < 40 ) { if ( index + url.length - lastIndex < 40 ) {
lastIndex = -( 40 - ( index + 1 ) ); lastIndex = -( 40 - ( index + 1 ) );
} }
url = url.slice( 0, index + 1 ) + '\u2026' + url.slice( lastIndex ); url = url.slice( 0, index + 1 ) + '\u2026' + url.slice( lastIndex );
@ -74,26 +74,49 @@
var $ = window.jQuery; var $ = window.jQuery;
function getSelectedLink() { function getSelectedLink() {
var href, var href, html
selectedNode = editor.selection.getNode(), node = editor.selection.getNode();
selectedText = editor.selection.getContent(), link = editor.dom.getParent( node, 'a[href]' );
link = editor.dom.getParent( selectedNode, 'a[href]' );
if ( ! link && selectedText.indexOf( '</a>' ) !== -1 ) { if ( ! link ) {
href = selectedText.match( /href="([^">]+)"/ ); html = editor.selection.getContent({ format: 'raw' });
if ( href && href[1] ) { if ( html && html.indexOf( '</a>' ) !== -1 ) {
link = editor.$( 'a[href="' + href[1] + '"]', selectedNode )[0]; href = html.match( /href="([^">]+)"/ );
}
if ( link ) { if ( href && href[1] ) {
editor.selection.select( link ); link = editor.$( 'a[href="' + href[1] + '"]', node )[0];
editor.nodeChanged(); }
if ( link ) {
editor.selection.select( link );
editor.nodeChanged();
}
} }
} }
return link; return link;
} }
function removePlaceholders() {
editor.$( 'a' ).each( function( i, element ) {
var $element = editor.$( element );
if ( $element.attr( 'href' ) === '_wp_link_placeholder' ) {
editor.dom.remove( element, true );
} else if ( $element.attr( 'data-wp-link-edit' ) ) {
$element.attr( 'data-wp-link-edit', null );
}
});
}
function removePlaceholderStrings( content, dataAttr ) {
if ( dataAttr ) {
content = content.replace( / data-wp-link-edit="true"/g, '' );
}
return content.replace( /<a [^>]*?href="_wp_link_placeholder"[^>]*>([\s\S]+)<\/a>/g, '$1' );
}
editor.on( 'preinit', function() { editor.on( 'preinit', function() {
if ( editor.wp && editor.wp._createToolbar ) { if ( editor.wp && editor.wp._createToolbar ) {
@ -126,9 +149,12 @@
var link = getSelectedLink(); var link = getSelectedLink();
if ( link ) { if ( link ) {
editor.dom.setAttribs( link, { 'data-wp-edit': true } ); editor.dom.setAttribs( link, { 'data-wp-link-edit': true } );
} else { } else {
removePlaceholders();
editor.execCommand( 'mceInsertLink', false, { href: '_wp_link_placeholder' } ); editor.execCommand( 'mceInsertLink', false, { href: '_wp_link_placeholder' } );
editor.selection.select( editor.$( 'a[href="_wp_link_placeholder"]' )[0] );
editor.nodeChanged(); editor.nodeChanged();
} }
} ); } );
@ -150,7 +176,7 @@
} }
if ( a ) { if ( a ) {
editor.dom.setAttribs( a, { href: href, 'data-wp-edit': null } ); editor.dom.setAttribs( a, { href: href, 'data-wp-link-edit': null } );
} }
a = false; a = false;
@ -160,16 +186,8 @@
} ); } );
editor.addCommand( 'wp_link_cancel', function() { editor.addCommand( 'wp_link_cancel', function() {
if ( a ) { removePlaceholders();
if ( editor.$( a ).attr( 'href' ) === '_wp_link_placeholder' ) {
editor.dom.remove( a, true );
} else {
editor.dom.setAttribs( a, { 'data-wp-edit': null } );
}
}
a = false; a = false;
editor.nodeChanged(); editor.nodeChanged();
editor.focus(); editor.focus();
} ); } );
@ -218,6 +236,18 @@
} }
} }
} ); } );
// Remove any remaining placeholders on saving.
editor.on( 'savecontent', function( event ) {
event.content = removePlaceholderStrings( event.content, true );
});
// Prevent adding undo levels on inserting link placeholder.
editor.on( 'BeforeAddUndo', function( event ) {
if ( event.level.content ) {
event.level.content = removePlaceholderStrings( event.level.content );
}
});
editor.addButton( 'wp_link_preview', { editor.addButton( 'wp_link_preview', {
type: 'WPLinkPreview', type: 'WPLinkPreview',
@ -235,7 +265,7 @@
inputInstance = this; inputInstance = this;
if ( $ ) { if ( $ && $.ui && $.ui.autocomplete ) {
$( input ) $( input )
.on( 'keydown', function() { .on( 'keydown', function() {
$( input ).removeAttr( 'aria-activedescendant' ); $( input ).removeAttr( 'aria-activedescendant' );
@ -311,13 +341,12 @@
editor.on( 'wptoolbar', function( event ) { editor.on( 'wptoolbar', function( event ) {
var anchor = editor.dom.getParent( event.element, 'a' ), var anchor = editor.dom.getParent( event.element, 'a' ),
$anchor, $anchor, href, edit;
href, edit;
if ( anchor ) { if ( anchor ) {
$anchor = editor.$( anchor ); $anchor = editor.$( anchor );
href = $anchor.attr( 'href' ); href = $anchor.attr( 'href' );
edit = $anchor.attr( 'data-wp-edit' ); edit = $anchor.attr( 'data-wp-link-edit' );
if ( href === '_wp_link_placeholder' || edit ) { if ( href === '_wp_link_placeholder' || edit ) {
inputInstance.setURL( edit ? href : '' ); inputInstance.setURL( edit ? href : '' );
@ -348,8 +377,13 @@
tooltip: 'Advanced', tooltip: 'Advanced',
icon: 'dashicon dashicons-admin-generic', icon: 'dashicon dashicons-admin-generic',
onclick: function() { onclick: function() {
editor.execCommand( 'wp_link_apply' ); if ( typeof window.wpLink !== 'undefined' ) {
window.wpLink && window.wpLink.open( editor.id ); if ( inputInstance.getEl().firstChild.value ) {
editor.execCommand( 'wp_link_apply' );
}
window.wpLink.open( editor.id );
}
} }
} ); } );

File diff suppressed because one or more lines are too long

View File

@ -219,14 +219,20 @@ var wpLink;
}, },
mceRefresh: function() { mceRefresh: function() {
var text, var text, url,
selectedNode = editor.selection.getNode(), selectedNode = editor.selection.getNode(),
linkNode = editor.dom.getParent( selectedNode, 'a[href]' ), linkNode = editor.dom.getParent( selectedNode, 'a[href]' ),
onlyText = this.hasSelectedText( linkNode ); onlyText = this.hasSelectedText( linkNode );
if ( linkNode ) { if ( linkNode ) {
text = linkNode.innerText || linkNode.textContent; text = linkNode.innerText || linkNode.textContent;
inputs.url.val( editor.dom.getAttrib( linkNode, 'href' ) ); url = editor.dom.getAttrib( linkNode, 'href' );
if ( url === '_wp_link_placeholder' ) {
url = '';
}
inputs.url.val( url );
inputs.openInNewTab.prop( 'checked', '_blank' === editor.dom.getAttrib( linkNode, 'target' ) ); inputs.openInNewTab.prop( 'checked', '_blank' === editor.dom.getAttrib( linkNode, 'target' ) );
inputs.submit.val( wpLinkL10n.update ); inputs.submit.val( wpLinkL10n.update );
} else { } else {
@ -244,6 +250,8 @@ var wpLink;
}, },
close: function() { close: function() {
var linkNode;
$( document.body ).removeClass( 'modal-open' ); $( document.body ).removeClass( 'modal-open' );
if ( ! wpLink.isMCE() ) { if ( ! wpLink.isMCE() ) {
@ -254,6 +262,12 @@ var wpLink;
wpLink.range.select(); wpLink.range.select();
} }
} else { } else {
linkNode = editor.dom.getParent( editor.selection.getNode(), 'a[href]' );
if ( linkNode && editor.dom.getAttrib( linkNode, 'href' ) === '_wp_link_placeholder' ) {
editor.dom.remove( linkNode, true );
}
editor.focus(); editor.focus();
} }
@ -352,7 +366,6 @@ var wpLink;
var attrs = wpLink.getAttrs(), var attrs = wpLink.getAttrs(),
link, text; link, text;
wpLink.close();
editor.focus(); editor.focus();
if ( tinymce.isIE ) { if ( tinymce.isIE ) {
@ -388,6 +401,7 @@ var wpLink;
} }
} }
wpLink.close();
editor.nodeChanged(); editor.nodeChanged();
}, },

File diff suppressed because one or more lines are too long

View File

@ -4,7 +4,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '4.5-alpha-36601'; $wp_version = '4.5-alpha-36602';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.