Make WordPress Core

Ticket #32604: 32604.16.patch

File 32604.16.patch, 5.9 KB (added by iseulde, 9 years ago)
  • src/wp-includes/js/tinymce/plugins/wplink/plugin.js

     
    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: '#',
    564                renderHtml: function() {
     
    9644
    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'
    123         } );
     50        tinymce.PluginManager.add( 'wplink', function( editor ) {
     51                var toolbar;
    12452
    125         editor.addButton( 'wp_link_remove', {
    126                 tooltip: 'Remove',
    127                 icon: 'dashicon dashicons-no',
    128                 cmd: 'unlink'
    129         } );
     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                } );
    130125
    131         editor.on( 'preinit', function() {
    132                 toolbar = editor.wp._createToolbar( [
    133                         'WPLinkPreview',
    134                         'wp_link_edit',
    135                         'wp_link_remove'
    136                 ], true );
     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                } );
    137145        } );
    138 });
     146} )( window.tinymce );
  • tests/qunit/wp-includes/js/tinymce/plugins/wplink/plugin.js

     
     1( function( QUnit, tinymce, _ ) {
     2        var wpLinkPreview = new tinymce.ui.WPLinkPreview();
     3
     4        QUnit.module( 'tinymce.plugins.wplink'  );
     5
     6        QUnit.test( 'prettyURL.', function( assert ) {
     7                var tests = {
     8                        '#fragment': '#fragment',
     9                        '?query#fragment': '?query#fragment',
     10                        'http://w.org/one/': 'w.org/one',
     11                        'http://w.org/one/index.html': 'w.org/one',
     12                        'http://w.org/one/two/': 'w.org/\u2026/two'
     13                };
     14
     15                _.each( tests, function( result, input ) {
     16                        assert.equal( wpLinkPreview.prettyURL( input ), result );
     17                } );
     18        } );
     19} )( window.QUnit, window.tinymce, window._ );