Make WordPress Core

Ticket #31441: 31441.22.patch

File 31441.22.patch, 6.3 KB (added by iseulde, 10 years ago)
  • src/wp-includes/js/tinymce/plugins/wptextpattern/plugin.js

     
    1414        tinymce.PluginManager.add( 'wptextpattern', function( editor ) {
    1515                var $$ = editor.$,
    1616                        VK = tinymce.util.VK,
    17                         patterns = [],
    18                         canUndo = false;
    19 
    20                 /**
    21                  * Add a pattern to format with a callback.
    22                  *
    23                  * @since 4.3.0
    24                  *
    25                  * @param {RegExp}   regExp   RegEx pattern.
    26                  * @param {Function} callback Callback.
    27                  */
    28                 function add( regExp, callback ) {
    29                         patterns.push( {
    30                                 regExp: regExp,
    31                                 callback: callback
    32                         } );
    33                 }
    34 
    35                 add( /^[*-]\s/, function() {
    36                         this.execCommand( 'InsertUnorderedList' );
    37                 } );
    38 
    39                 add( /^1[.)]\s/, function() {
    40                         this.execCommand( 'InsertOrderedList' );
    41                 } );
    42 
    43                 add( /^>\s/, function() {
    44                         this.formatter.toggle( 'blockquote' );
    45                 } );
    46 
    47                 add( /^(#{2,6})\s/, function() {
    48                         this.formatter.toggle( 'h' + arguments[1].length );
    49                 } );
     17                        canUndo = false,
     18                        spacePatterns = [
     19                                { regExp: /^[*-]\s/, cmd: 'InsertUnorderedList' },
     20                                { regExp: /^1[.)]\s/, cmd: 'InsertOrderedList' }
     21                        ],
     22                        enterPatterns = [
     23                                { start: '##', format: 'h2' },
     24                                { start: '###', format: 'h3' },
     25                                { start: '####', format: 'h4' },
     26                                { start: '#####', format: 'h5' },
     27                                { start: '######', format: 'h6' },
     28                                { start: '>', format: 'blockquote' }
     29                        ];
    5030
    5131                editor.on( 'selectionchange', function() {
    5232                        canUndo = false;
     
    5737                                editor.undoManager.undo();
    5838                                event.preventDefault();
    5939                        }
    60                 } );
    6140
    62                 editor.on( 'keyup', function( event ) {
    63                         var rng, node, text, parent, child;
    64 
    65                         if ( event.keyCode !== VK.SPACEBAR ) {
    66                                 return;
     41                        if ( event.keyCode === VK.ENTER && ! VK.modifierPressed( event ) ) {
     42                                enter();
    6743                        }
     44                }, true );
    6845
    69                         rng = editor.selection.getRng();
    70                         node = rng.startContainer;
    71 
    72                         if ( ! node || node.nodeType !== 3 ) {
    73                                 return;
     46                editor.on( 'keyup', function( event ) {
     47                        if ( event.keyCode === VK.SPACEBAR || ! VK.modifierPressed( event ) ) {
     48                                space();
    7449                        }
     50                } );
    7551
    76                         text = node.nodeValue;
    77                         parent = editor.dom.getParent( node, 'p' );
     52                function firstNode( node ) {
     53                        var parent = editor.dom.getParent( node, 'p' ),
     54                                child;
    7855
    7956                        if ( ! parent ) {
    8057                                return;
     
    9269                                return;
    9370                        }
    9471
    95                         if ( ! child.nodeValue ) {
     72                        if ( ! child.data ) {
    9673                                child = child.nextSibling;
    9774                        }
    9875
    99                         if ( child !== node ) {
     76                        return child;
     77                }
     78
     79                function space() {
     80                        var rng = editor.selection.getRng(),
     81                                node = rng.startContainer,
     82                                text;
     83
     84                        if ( firstNode( node ) !== node ) {
    10085                                return;
    10186                        }
    10287
    103                         tinymce.each( patterns, function( pattern ) {
    104                                 var args,
    105                                         replace = text.replace( pattern.regExp, function() {
    106                                                 args = arguments;
    107                                                 return '';
    108                                         } );
     88                        text = node.nodeValue;
     89
     90                        tinymce.each( spacePatterns, function( pattern ) {
     91                                var replace = text.replace( pattern.regExp, '' );
    10992
    11093                                if ( text === replace ) {
    11194                                        return;
     
    118101                                editor.undoManager.add();
    119102
    120103                                editor.undoManager.transact( function() {
    121                                         var $$parent;
     104                                        var parent = node.parentNode,
     105                                                $$parent;
    122106
    123107                                        if ( replace ) {
    124108                                                $$( node ).replaceWith( document.createTextNode( replace ) );
    125109                                        } else  {
    126                                                 $$parent = $$( node.parentNode );
     110                                                $$parent = $$( parent );
    127111
    128112                                                $$( node ).remove();
    129113
     
    133117                                        }
    134118
    135119                                        editor.selection.setCursorLocation( parent );
    136 
    137                                         pattern.callback.apply( editor, args );
     120                                        editor.execCommand( pattern.cmd );
    138121                                } );
    139122
    140123                                // We need to wait for native events to be triggered.
     
    144127
    145128                                return false;
    146129                        } );
    147                 } );
     130                }
     131
     132                function enter() {
     133                        var selection = editor.selection,
     134                                rng = selection.getRng(),
     135                                offset = rng.startOffset,
     136                                start = rng.startContainer,
     137                                node = firstNode( start ),
     138                                text = node.data,
     139                                i = enterPatterns.length,
     140                                pattern;
     141
     142                        if ( ! node ) {
     143                                return;
     144                        }
     145
     146                        while ( i-- ) {
     147                                 if ( text.indexOf( enterPatterns[ i ].start ) === 0 ) {
     148                                        pattern = enterPatterns[ i ];
     149                                        break;
     150                                 }
     151                        }
     152
     153                        if ( ! pattern ) {
     154                                return;
     155                        }
     156
     157                        if ( node === start && tinymce.trim( text ) === pattern.start ) {
     158                                return;
     159                        }
     160
     161                        if ( node === start ) {
     162                                offset = Math.max( 0, offset - pattern.start.length );
     163                        }
     164
     165                        editor.undoManager.add();
     166
     167                        editor.undoManager.transact( function() {
     168                                node.deleteData( 0, pattern.start.length );
     169
     170                                editor.formatter.apply( pattern.format, {}, start );
     171
     172                                rng.setStart( start, offset );
     173                                rng.collapse( true );
     174                                selection.setRng( rng );
     175
     176                                console.log( editor.getContent() );
     177                        } );
     178                }
    148179        } );
    149180} )( window.tinymce, window.setTimeout );
  • tests/qunit/wp-includes/js/tinymce/plugins/wptextpattern/plugin.js

     
    144144                        assert.equal( editor.getContent(), '<ul>\n<li>tes</li>\n</ul>' );
    145145                }, assert.async() );
    146146        } );
     147
     148        QUnit.test( 'Heading 3', function( assert ) {
     149                editor.setContent( '<p>### test</p>' );
     150                editor.selection.setCursorLocation( editor.$( 'p' )[0].firstChild, 8 );
     151
     152                type( '\n', function() {
     153                        assert.equal( editor.getContent(), '<h3>test</h3>\n<p>&nbsp;</p>' );
     154                }, assert.async() );
     155        } );
     156
     157        QUnit.test( 'Heading 3 with elements.', function( assert ) {
     158                editor.setContent( '<p>###<del>test</del></p>' );
     159                editor.selection.setCursorLocation( editor.$( 'del' )[0].firstChild, 4 );
     160
     161                type( '\n', function() {
     162                        assert.equal( editor.getContent(), '<h3><del>test</del></h3>\n<p>&nbsp;</p>' );
     163                }, assert.async() );
     164        } );
     165
     166        QUnit.test( 'Don\'t convert without content', function( assert ) {
     167                editor.setContent( '<p>###&nbsp;</p>' );
     168                editor.selection.setCursorLocation( editor.$( 'p' )[0].firstChild, 4 );
     169
     170                type( '\n', function() {
     171                        assert.equal( editor.getContent(), '<p>###&nbsp;</p>\n<p>&nbsp;</p>' );
     172                }, assert.async() );
     173        } );
    147174} )( window.jQuery, window.QUnit, window.tinymce, window.Utils.type, window.setTimeout );