Make WordPress Core

Changeset 36747


Ignore:
Timestamp:
02/27/2016 09:32:02 PM (11 years ago)
Author:
azaozz
Message:

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.

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

Legend:

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

    r36743 r36747  
    7979                reset: function() {
    8080                        var urlInput = this.getEl().firstChild;
     81
    8182                        urlInput.value = '';
    8283                        urlInput.nextSibling.value = '';
     
    8990                var previewInstance;
    9091                var inputInstance;
     92                var linkNode;
    9193                var $ = window.jQuery;
    9294
     
    108110                                        if ( link ) {
    109111                                                editor.selection.select( link );
    110                                                 editor.nodeChanged();
    111112                                        }
    112113                                }
     
    153154                                        if ( ! tinymce.$( document.body ).hasClass( 'modal-open' ) ) {
    154155                                                window.setTimeout( function() {
    155                                                         var element = editToolbar.$el.find( 'input.ui-autocomplete-input' )[0];
     156                                                        var element = editToolbar.$el.find( 'input.ui-autocomplete-input' )[0],
     157                                                                selection = linkNode && ( linkNode.textContent || linkNode.innerText );
    156158
    157159                                                        if ( element ) {
     160                                                                if ( ! element.value && selection && typeof window.wpLink !== 'undefined' ) {
     161                                                                        element.value = window.wpLink.getUrlFromSelection( selection );
     162                                                                }
     163
    158164                                                                element.focus();
    159165                                                                element.select();
     
    172178
    173179                editor.addCommand( 'WP_Link', function() {
    174                         var link = getSelectedLink();
    175 
    176180                        if ( tinymce.Env.ie && tinymce.Env.ie < 10 ) {
    177181                                if ( typeof window.wpLink !== 'undefined' ) {
     
    182186                        }
    183187
     188                        linkNode = getSelectedLink();
    184189                        editToolbar.tempHide = false;
    185190
    186                         if ( link ) {
    187                                 editor.dom.setAttribs( link, { 'data-wplink-edit': true } );
     191                        if ( linkNode ) {
     192                                editor.dom.setAttribs( linkNode, { 'data-wplink-edit': true } );
    188193                        } else {
    189194                                removePlaceholders();
    190195                                editor.execCommand( 'mceInsertLink', false, { href: '_wp_link_placeholder' } );
    191196
    192                                 if ( tinymce.Env.ie ) {
    193                                         editor.windowManager.wplinkBookmark = editor.selection.getBookmark();
    194                                 }
    195 
     197                                linkNode = editor.$( 'a[href="_wp_link_placeholder"]' )[0];
    196198                                editor.nodeChanged();
    197199                        }
     
    203205                        }
    204206
    205                         var href, text,
    206                                 linkNode = getSelectedLink();
     207                        var href, text;
    207208
    208209                        if ( linkNode ) {
     
    210211                                text = inputInstance.getLinkText();
    211212                                editor.focus();
    212 
    213                                 if ( tinymce.isIE ) {
    214                                         editor.selection.moveToBookmark( editor.windowManager.wplinkBookmark );
    215                                         editor.windowManager.wplinkBookmark = null;
    216                                 }
    217213
    218214                                if ( ! href ) {
     
    241237                                removePlaceholders();
    242238                                editor.focus();
    243 
    244                                 if ( tinymce.isIE ) {
    245                                         editor.selection.moveToBookmark( editor.windowManager.wplinkBookmark );
    246                                         editor.windowManager.wplinkBookmark = null;
    247                                 }
    248 
    249239                                editToolbar.tempHide = false;
    250240                        }
     
    457447
    458448                                        editor.focus(); // Needed for IE
    459                                         window.wpLink.open( editor.id, url, text );
     449                                        window.wpLink.open( editor.id, url, text, linkNode );
    460450
    461451                                        editToolbar.tempHide = true;
  • trunk/src/wp-includes/js/wplink.js

    r36743 r36747  
    33
    44( function( $ ) {
    5         var editor, correctedURL,
     5        var editor, correctedURL, linkNode,
    66                inputs = {},
    77                isTouch = ( 'ontouchend' in document );
    88
    99        function getLink() {
    10                 return editor.dom.getParent( editor.selection.getNode(), 'a' );
     10                return linkNode || editor.dom.getParent( editor.selection.getNode(), 'a[href]' );
    1111        }
    1212
     
    126126                },
    127127
    128                 open: function( editorId, url, text ) {
     128                open: function( editorId, url, text, node ) {
    129129                        var ed,
    130130                                $body = $( document.body );
    131131
    132132                        $body.addClass( 'modal-open' );
     133                        linkNode = node;
    133134
    134135                        wpLink.range = null;
     
    259260                                url = url || editor.dom.getAttrib( linkNode, 'href' );
    260261
    261                                 if ( url === '_wp_link_placeholder' ) {
    262                                         url = '';
    263                                 }
    264 
    265                                 inputs.url.val( url );
    266                                 inputs.openInNewTab.prop( 'checked', '_blank' === editor.dom.getAttrib( linkNode, 'target' ) );
    267                                 inputs.submit.val( wpLinkL10n.update );
     262                                if ( url !== '_wp_link_placeholder' ) {
     263                                        inputs.url.val( url );
     264                                        inputs.openInNewTab.prop( 'checked', '_blank' === editor.dom.getAttrib( linkNode, 'target' ) );
     265                                        inputs.submit.val( wpLinkL10n.update );
     266                                } else {
     267                                        this.setDefaultValues( linkText );
     268                                }
    268269                        } else {
    269                                 text = editor.selection.getContent({ format: 'text' }) || text;
    270                                 this.setDefaultValues();
     270                                linkText = editor.selection.getContent({ format: 'text' }) || text || '';
     271                                this.setDefaultValues( linkText );
    271272                        }
    272273
    273274                        if ( onlyText ) {
    274                                 inputs.text.val( linkText || '' );
     275                                inputs.text.val( linkText );
    275276                                inputs.wrap.addClass( 'has-text-field' );
    276277                        } else {
     
    280281                },
    281282
    282                 close: function() {
     283                close: function( reset ) {
    283284                        $( document.body ).removeClass( 'modal-open' );
    284285
    285                         if ( ! wpLink.isMCE() ) {
    286                                 wpLink.textarea.focus();
    287 
    288                                 if ( wpLink.range ) {
    289                                         wpLink.range.moveToBookmark( wpLink.range.getBookmark() );
    290                                         wpLink.range.select();
    291                                 }
    292                         } else {
    293                                 if ( editor.plugins.wplink ) {
    294                                         editor.plugins.wplink.close();
    295                                 }
    296 
    297                                 editor.focus();
     286                        if ( reset !== 'noReset' ) {
     287                                if ( ! wpLink.isMCE() ) {
     288                                        wpLink.textarea.focus();
     289
     290                                        if ( wpLink.range ) {
     291                                                wpLink.range.moveToBookmark( wpLink.range.getBookmark() );
     292                                                wpLink.range.select();
     293                                        }
     294                                } else {
     295                                        if ( editor.plugins.wplink ) {
     296                                                editor.plugins.wplink.close();
     297                                        }
     298
     299                                        editor.focus();
     300                                }
    298301                        }
    299302
     
    395398                        editor.focus();
    396399
    397                         if ( tinymce.isIE ) {
     400                        if ( tinymce.isIE && editor.windowManager.wplinkBookmark ) {
    398401                                editor.selection.moveToBookmark( editor.windowManager.wplinkBookmark );
    399402                                editor.windowManager.wplinkBookmark = null;
     
    402405                        if ( ! attrs.href ) {
    403406                                editor.execCommand( 'unlink' );
     407                                wpLink.close();
    404408                                return;
    405409                        }
     
    429433                        }
    430434
    431                         wpLink.close();
     435                        wpLink.close( 'noReset' );
     436                        editor.focus();
    432437                        editor.nodeChanged();
    433438                },
     
    456461                },
    457462
    458                 setDefaultValues: function() {
    459                         var selection,
     463                getUrlFromSelection: function( selection ) {
     464                        var url,
    460465                                emailRegexp = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i,
    461466                                urlRegexp = /^(https?|ftp):\/\/[A-Z0-9.-]+\.[A-Z]{2,4}[^ "]*$/i;
    462467
    463                         if ( this.isMCE() ) {
    464                                 selection = editor.selection.getContent();
    465                         } else if ( document.selection && wpLink.range ) {
    466                                 selection = wpLink.range.text;
    467                         } else if ( typeof this.textarea.selectionStart !== 'undefined' ) {
    468                                 selection = this.textarea.value.substring( this.textarea.selectionStart, this.textarea.selectionEnd );
    469                         }
     468                        if ( ! selection ) {
     469                                if ( this.isMCE() ) {
     470                                        selection = editor.selection.getContent({ format: 'text' });
     471                                } else if ( document.selection && wpLink.range ) {
     472                                        selection = wpLink.range.text;
     473                                } else if ( typeof this.textarea.selectionStart !== 'undefined' ) {
     474                                        selection = this.textarea.value.substring( this.textarea.selectionStart, this.textarea.selectionEnd );
     475                                }
     476                        }
     477
     478                        selection = tinymce.trim( selection );
    470479
    471480                        if ( selection && emailRegexp.test( selection ) ) {
    472481                                // Selection is email address
    473                                 inputs.url.val( 'mailto:' + selection );
     482                                return 'mailto:' + selection;
    474483                        } else if ( selection && urlRegexp.test( selection ) ) {
    475484                                // Selection is URL
    476                                 inputs.url.val( selection.replace( /&amp;|&#0?38;/gi, '&' ) );
    477                         } else {
    478                                 // Set URL to default.
    479                                 inputs.url.val( '' );
    480                         }
     485                                return selection.replace( /&amp;|&#0?38;/gi, '&' );
     486                        }
     487
     488                        return '';
     489                },
     490
     491                setDefaultValues: function( selection ) {
     492                        inputs.url.val( this.getUrlFromSelection( selection ) );
    481493
    482494                        // Update save prompt.
Note: See TracChangeset for help on using the changeset viewer.