Make WordPress Core

Changeset 33584


Ignore:
Timestamp:
08/04/2015 09:41:34 PM (11 years ago)
Author:
iseulde
Message:

TinyMCE: wplink: fix WPLinkPreview for multiple editors

Fixes #33264.

File:
1 edited

Legend:

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

    r32937 r33584  
    1 /* global tinymce */
    2 tinymce.PluginManager.add( 'wplink', function( editor ) {
    3     var toolbar;
    4 
    5     editor.addCommand( 'WP_Link', function() {
    6         window.wpLink && window.wpLink.open( editor.id );
    7     });
    8 
    9     // WP default shortcut
    10     editor.addShortcut( 'Alt+Shift+A', '', 'WP_Link' );
    11     // The "de-facto standard" shortcut, see #27305
    12     editor.addShortcut( 'Meta+K', '', 'WP_Link' );
    13 
    14     editor.addButton( 'link', {
    15         icon: 'link',
    16         tooltip: 'Insert/edit link',
    17         cmd: 'WP_Link',
    18         stateSelector: 'a[href]'
    19     });
    20 
    21     editor.addButton( 'unlink', {
    22         icon: 'unlink',
    23         tooltip: 'Remove link',
    24         cmd: 'unlink'
    25     });
    26 
    27     editor.addMenuItem( 'link', {
    28         icon: 'link',
    29         text: 'Insert/edit link',
    30         cmd: 'WP_Link',
    31         stateSelector: 'a[href]',
    32         context: 'insert',
    33         prependToContext: true
    34     });
    35 
    36     editor.on( 'pastepreprocess', function( event ) {
    37         var pastedStr = event.content,
    38             regExp = /^(?:https?:)?\/\/\S+$/i;
    39 
    40         if ( ! editor.selection.isCollapsed() && ! regExp.test( editor.selection.getContent() ) ) {
    41             pastedStr = pastedStr.replace( /<[^>]+>/g, '' );
    42             pastedStr = tinymce.trim( pastedStr );
    43 
    44             if ( regExp.test( pastedStr ) ) {
    45                 editor.execCommand( 'mceInsertLink', false, {
    46                     href: editor.dom.decode( pastedStr )
    47                 } );
    48 
    49                 event.preventDefault();
    50             }
    51         }
    52     } );
    53 
     1( function( tinymce ) {
    542    tinymce.ui.WPLinkPreview = tinymce.ui.Control.extend( {
    553        url: '#',
     
    9745                tinymce.$( this.getEl().firstChild ).attr( 'href', this.url ).text( url );
    9846            }
    99         },
    100         postRender: function() {
    101             var self = this;
    102 
    103             editor.on( 'wptoolbar', function( event ) {
    104                 var anchor = editor.dom.getParent( event.element, 'a' ),
    105                     $ = editor.$,
    106                     href;
    107 
    108                 if ( anchor && ! $( anchor ).find( 'img' ).length &&
    109                     ( href = $( anchor ).attr( 'href' ) ) ) {
    110 
    111                     self.setURL( href );
    112                     event.element = anchor;
    113                     event.toolbar = toolbar;
    114                 }
    115             } );
    11647        }
    11748    } );
    11849
    119     editor.addButton( 'wp_link_edit', {
    120         tooltip: 'Edit ', // trailing space is needed, used for context
    121         icon: 'dashicon dashicons-edit',
    122         cmd: 'WP_Link'
     50    tinymce.PluginManager.add( 'wplink', function( editor ) {
     51        var toolbar;
     52
     53        editor.addCommand( 'WP_Link', function() {
     54            window.wpLink && window.wpLink.open( editor.id );
     55        });
     56
     57        // WP default shortcut
     58        editor.addShortcut( 'Alt+Shift+A', '', 'WP_Link' );
     59        // The "de-facto standard" shortcut, see #27305
     60        editor.addShortcut( 'Meta+K', '', 'WP_Link' );
     61
     62        editor.addButton( 'link', {
     63            icon: 'link',
     64            tooltip: 'Insert/edit link',
     65            cmd: 'WP_Link',
     66            stateSelector: 'a[href]'
     67        });
     68
     69        editor.addButton( 'unlink', {
     70            icon: 'unlink',
     71            tooltip: 'Remove link',
     72            cmd: 'unlink'
     73        });
     74
     75        editor.addMenuItem( 'link', {
     76            icon: 'link',
     77            text: 'Insert/edit link',
     78            cmd: 'WP_Link',
     79            stateSelector: 'a[href]',
     80            context: 'insert',
     81            prependToContext: true
     82        });
     83
     84        editor.on( 'pastepreprocess', function( event ) {
     85            var pastedStr = event.content,
     86                regExp = /^(?:https?:)?\/\/\S+$/i;
     87
     88            if ( ! editor.selection.isCollapsed() && ! regExp.test( editor.selection.getContent() ) ) {
     89                pastedStr = pastedStr.replace( /<[^>]+>/g, '' );
     90                pastedStr = tinymce.trim( pastedStr );
     91
     92                if ( regExp.test( pastedStr ) ) {
     93                    editor.execCommand( 'mceInsertLink', false, {
     94                        href: editor.dom.decode( pastedStr )
     95                    } );
     96
     97                    event.preventDefault();
     98                }
     99            }
     100        } );
     101
     102        editor.addButton( 'wp_link_preview', {
     103            type: 'WPLinkPreview',
     104            onPostRender: function() {
     105                var self = this;
     106
     107                editor.on( 'wptoolbar', function( event ) {
     108                    var anchor = editor.dom.getParent( event.element, 'a' ),
     109                        $anchor,
     110                        href;
     111
     112                    if ( anchor ) {
     113                        $anchor = editor.$( anchor );
     114                        href = $anchor.attr( 'href' );
     115
     116                        if ( href && ! $anchor.find( 'img' ).length ) {
     117                            self.setURL( href );
     118                            event.element = anchor;
     119                            event.toolbar = toolbar;
     120                        }
     121                    }
     122                } );
     123            }
     124        } );
     125
     126        editor.addButton( 'wp_link_edit', {
     127            tooltip: 'Edit ', // trailing space is needed, used for context
     128            icon: 'dashicon dashicons-edit',
     129            cmd: 'WP_Link'
     130        } );
     131
     132        editor.addButton( 'wp_link_remove', {
     133            tooltip: 'Remove',
     134            icon: 'dashicon dashicons-no',
     135            cmd: 'unlink'
     136        } );
     137
     138        editor.on( 'preinit', function() {
     139            toolbar = editor.wp._createToolbar( [
     140                'wp_link_preview',
     141                'wp_link_edit',
     142                'wp_link_remove'
     143            ], true );
     144        } );
    123145    } );
    124 
    125     editor.addButton( 'wp_link_remove', {
    126         tooltip: 'Remove',
    127         icon: 'dashicon dashicons-no',
    128         cmd: 'unlink'
    129     } );
    130 
    131     editor.on( 'preinit', function() {
    132         toolbar = editor.wp._createToolbar( [
    133             'WPLinkPreview',
    134             'wp_link_edit',
    135             'wp_link_remove'
    136         ], true );
    137     } );
    138 });
     146} )( window.tinymce );
Note: See TracChangeset for help on using the changeset viewer.