Make WordPress Core

Changeset 36602


Ignore:
Timestamp:
02/20/2016 09:35:54 PM (8 years ago)
Author:
azaozz
Message:

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.

Location:
trunk/src/wp-includes/js
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/js/tinymce/plugins/wplink/plugin.js

    r36483 r36602  
    4242                    // If the beginning + ending are shorter that 40 chars, show more of the ending
    4343                    if ( index + url.length - lastIndex < 40 ) {
    44                         lastIndex =  -( 40 - ( index + 1 ) );
     44                        lastIndex = -( 40 - ( index + 1 ) );
    4545                    }
    4646
     
    7575
    7676        function getSelectedLink() {
    77             var href,
    78                 selectedNode = editor.selection.getNode(),
    79                 selectedText = editor.selection.getContent(),
    80                 link = editor.dom.getParent( selectedNode, 'a[href]' );
    81 
    82             if ( ! link && selectedText.indexOf( '</a>' ) !== -1 ) {
    83                 href = selectedText.match( /href="([^">]+)"/ );
    84 
    85                 if ( href && href[1] ) {
    86                     link = editor.$( 'a[href="' + href[1] + '"]', selectedNode )[0];
    87                 }
    88 
    89                 if ( link ) {
    90                     editor.selection.select( link );
    91                     editor.nodeChanged();
     77            var href, html
     78                node = editor.selection.getNode();
     79                link = editor.dom.getParent( node, 'a[href]' );
     80
     81            if ( ! link ) {
     82                html = editor.selection.getContent({ format: 'raw' });
     83
     84                if ( html && html.indexOf( '</a>' ) !== -1 ) {
     85                    href = html.match( /href="([^">]+)"/ );
     86
     87                    if ( href && href[1] ) {
     88                        link = editor.$( 'a[href="' + href[1] + '"]', node )[0];
     89                    }
     90
     91                    if ( link ) {
     92                        editor.selection.select( link );
     93                        editor.nodeChanged();
     94                    }
    9295                }
    9396            }
    9497
    9598            return link;
     99        }
     100       
     101        function removePlaceholders() {
     102            editor.$( 'a' ).each( function( i, element ) {
     103                var $element = editor.$( element );
     104
     105                if ( $element.attr( 'href' ) === '_wp_link_placeholder' ) {
     106                    editor.dom.remove( element, true );
     107                } else if ( $element.attr( 'data-wp-link-edit' ) ) {
     108                    $element.attr( 'data-wp-link-edit', null );
     109                }
     110            });
     111        }
     112       
     113        function removePlaceholderStrings( content, dataAttr ) {
     114            if ( dataAttr ) {
     115                content = content.replace( / data-wp-link-edit="true"/g, '' );
     116            }
     117
     118            return content.replace( /<a [^>]*?href="_wp_link_placeholder"[^>]*>([\s\S]+)<\/a>/g, '$1' );
    96119        }
    97120
     
    127150
    128151            if ( link ) {
    129                 editor.dom.setAttribs( link, { 'data-wp-edit': true } );
     152                editor.dom.setAttribs( link, { 'data-wp-link-edit': true } );
    130153            } else {
     154                removePlaceholders();
     155
    131156                editor.execCommand( 'mceInsertLink', false, { href: '_wp_link_placeholder' } );
     157                editor.selection.select( editor.$( 'a[href="_wp_link_placeholder"]' )[0] );
    132158                editor.nodeChanged();
    133159            }
     
    151177
    152178            if ( a ) {
    153                 editor.dom.setAttribs( a, { href: href, 'data-wp-edit': null } );
     179                editor.dom.setAttribs( a, { href: href, 'data-wp-link-edit': null } );
    154180            }
    155181
     
    161187
    162188        editor.addCommand( 'wp_link_cancel', function() {
    163             if ( a ) {
    164                 if ( editor.$( a ).attr( 'href' ) === '_wp_link_placeholder' ) {
    165                     editor.dom.remove( a, true );
    166                 } else {
    167                     editor.dom.setAttribs( a, { 'data-wp-edit': null } );
    168                 }
    169             }
    170 
     189            removePlaceholders();
    171190            a = false;
    172 
    173191            editor.nodeChanged();
    174192            editor.focus();
     
    219237            }
    220238        } );
     239       
     240        // Remove any remaining placeholders on saving.
     241        editor.on( 'savecontent', function( event ) {
     242            event.content = removePlaceholderStrings( event.content, true );
     243        });
     244       
     245        // Prevent adding undo levels on inserting link placeholder.
     246        editor.on( 'BeforeAddUndo', function( event ) {
     247            if ( event.level.content ) {
     248                event.level.content = removePlaceholderStrings( event.level.content );
     249            }
     250        });
    221251
    222252        editor.addButton( 'wp_link_preview', {
     
    236266                inputInstance = this;
    237267
    238                 if ( $ ) {
     268                if ( $ && $.ui && $.ui.autocomplete ) {
    239269                    $( input )
    240270                    .on( 'keydown', function() {
     
    312342        editor.on( 'wptoolbar', function( event ) {
    313343            var anchor = editor.dom.getParent( event.element, 'a' ),
    314                 $anchor,
    315                 href, edit;
     344                $anchor, href, edit;
    316345
    317346            if ( anchor ) {
    318347                $anchor = editor.$( anchor );
    319348                href = $anchor.attr( 'href' );
    320                 edit = $anchor.attr( 'data-wp-edit' );
     349                edit = $anchor.attr( 'data-wp-link-edit' );
    321350
    322351                if ( href === '_wp_link_placeholder' || edit ) {
     
    349378            icon: 'dashicon dashicons-admin-generic',
    350379            onclick: function() {
    351                 editor.execCommand( 'wp_link_apply' );
    352                 window.wpLink && window.wpLink.open( editor.id );
     380                if ( typeof window.wpLink !== 'undefined' ) {
     381                    if ( inputInstance.getEl().firstChild.value ) {
     382                        editor.execCommand( 'wp_link_apply' );
     383                    }
     384
     385                    window.wpLink.open( editor.id );
     386                }
    353387            }
    354388        } );
  • trunk/src/wp-includes/js/wplink.js

    r35728 r36602  
    220220
    221221        mceRefresh: function() {
    222             var text,
     222            var text, url,
    223223                selectedNode = editor.selection.getNode(),
    224224                linkNode = editor.dom.getParent( selectedNode, 'a[href]' ),
     
    227227            if ( linkNode ) {
    228228                text = linkNode.innerText || linkNode.textContent;
    229                 inputs.url.val( editor.dom.getAttrib( linkNode, 'href' ) );
     229                url = editor.dom.getAttrib( linkNode, 'href' );
     230
     231                if ( url === '_wp_link_placeholder' ) {
     232                    url = '';
     233                }
     234
     235                inputs.url.val( url );
    230236                inputs.openInNewTab.prop( 'checked', '_blank' === editor.dom.getAttrib( linkNode, 'target' ) );
    231237                inputs.submit.val( wpLinkL10n.update );
     
    245251
    246252        close: function() {
     253            var linkNode;
     254           
    247255            $( document.body ).removeClass( 'modal-open' );
    248256
     
    255263                }
    256264            } else {
     265                linkNode = editor.dom.getParent( editor.selection.getNode(), 'a[href]' );
     266
     267                if ( linkNode && editor.dom.getAttrib( linkNode, 'href' ) === '_wp_link_placeholder' ) {
     268                    editor.dom.remove( linkNode, true );
     269                }
     270
    257271                editor.focus();
    258272            }
     
    353367                link, text;
    354368
    355             wpLink.close();
    356369            editor.focus();
    357370
     
    389402            }
    390403
     404            wpLink.close();
    391405            editor.nodeChanged();
    392406        },
Note: See TracChangeset for help on using the changeset viewer.