Make WordPress Core

Ticket #26959: 26959-08.patch

File 26959-08.patch, 4.7 KB (added by gcorne, 11 years ago)
  • src/wp-includes/js/tinymce/plugins/wpview/plugin.js

    diff --git src/wp-includes/js/tinymce/plugins/wpview/plugin.js src/wp-includes/js/tinymce/plugins/wpview/plugin.js
    index 48775f9..45321c4 100644
    tinymce.PluginManager.add( 'wpview', function( editor ) { 
    8686                editor.dom.bind( clipboard, 'beforedeactivate focusin focusout', _stop );
    8787                editor.dom.bind( selected, 'beforedeactivate focusin focusout', _stop );
    8888
     89                // Make sure that the editor is focused.
     90                // It is possible that the editor is not focused when the mouse event fires
     91                // without focus, the selection will not work properly.
     92                editor.getBody().focus();
     93
    8994                // select the hidden div
    9095                editor.selection.select( clipboard, true );
    9196        }
    tinymce.PluginManager.add( 'wpview', function( editor ) { 
    156161                                editor.selection.setCursorLocation( padNode, 0 );
    157162                        }
    158163                }
    159 
    160         //      refreshEmptyContentNode();
    161164        });
    162165
    163166        // Detect mouse down events that are adjacent to a view when a view is the first view or the last view
    tinymce.PluginManager.add( 'wpview', function( editor ) { 
    188191                        }
    189192
    190193                        if ( padNode ) {
     194                                // Make sure that a selected view is deselected so that focus and selection are handled properly
     195                                deselect();
     196                                editor.getBody().focus();
    191197                                editor.selection.setCursorLocation( padNode, 0 );
    192198                        }
    193199                }
    tinymce.PluginManager.add( 'wpview', function( editor ) { 
    298304
    299305        editor.on( 'keydown', function( event ) {
    300306                var keyCode = event.keyCode,
    301                         view;
     307                        body = editor.getBody(),
     308                        view, padNode;
    302309
    303310                // If a view isn't selected, let the event go on its merry way.
    304311                if ( ! selected ) {
    tinymce.PluginManager.add( 'wpview', function( editor ) { 
    314321                        return;
    315322                }
    316323
    317                 // If the caret is not within the selected view, deselect the
    318                 // view and bail.
    319324                view = getParentView( editor.selection.getNode() );
    320325
     326                // If the caret is not within the selected view, deselect the
     327                // view and bail.
    321328                if ( view !== selected ) {
    322329                        deselect();
    323330                        return;
    324331                }
    325332
     333                if ( keyCode === VK.LEFT || keyCode === VK.UP ) {
     334                        deselect();
     335                        // Handle case where two views are stacked on top of one another
     336                        if ( isView( view.previousSibling ) ) {
     337                                select( view.previousSibling );
     338                        // Handle case where view is the first node
     339                        } else if ( view.previousSibling === null ) {
     340                                padNode = createPadNode();
     341                                body.insertBefore( padNode, body.firstChild );
     342                                editor.selection.setCursorLocation( body.firstChild, 0 );
     343                        // Handle default case
     344                        } else {
     345                                editor.selection.select( view.previousSibling, true );
     346                                editor.selection.collapse();
     347                        }
     348                }
     349
     350                if ( keyCode === VK.RIGHT || keyCode === VK.DOWN ) {
     351                        deselect();
     352                        // Handle case where the next node is another wpview
     353                        if ( isView( view.nextSibling ) ) {
     354                                select( view.nextSibling );
     355                        // Handle case were the view is that last node
     356                        } else if ( view.nextSibling === null ) {
     357                                padNode = createPadNode();
     358                                body.appendChild( padNode );
     359                                editor.selection.setCursorLocation( body.lastChild, 0 );
     360                        // Handle default case where the next node is a non-wpview
     361                        } else {
     362                                editor.selection.setCursorLocation( view.nextSibling.firstChild, 0 );
     363                        }
     364                }
     365
    326366                // If delete or backspace is pressed, delete the view.
    327367                if ( keyCode === VK.DELETE || keyCode === VK.BACKSPACE ) {
    328368                        editor.dom.remove( selected );
    tinymce.PluginManager.add( 'wpview', function( editor ) { 
    331371                event.preventDefault();
    332372        });
    333373
     374        // Select and deselect views when arrow keys are used to navigate the content of the editor.
     375        editor.on( 'keydown', function( event ) {
     376                var keyCode = event.keyCode,
     377                        range = editor.selection.getRng(),
     378                        body = editor.getBody(),
     379                        node;
     380
     381                if ( range.collapsed !== true || event.metaKey || event.ctrlKey ) {
     382                        return;
     383                }
     384
     385                if ( keyCode === VK.LEFT || keyCode === VK.UP ) {
     386                        node = range.startContainer.parentNode === body ? range.startContainer : range.startContainer.parentNode;
     387                        // The caret is directly after a wpview
     388                        if ( range.startOffset === 0 && isView( node.previousSibling ) ) {
     389                                select( node.previousSibling );
     390                                event.preventDefault();
     391                        }
     392                } else if ( keyCode === VK.RIGHT || keyCode === VK.DOWN ) {
     393                        node = range.startContainer.parentNode === body ? range.startContainer : range.startContainer.parentNode;
     394                        // The caret is directly before a wpview
     395                        if ( ( ( range.startOffset === 0 && ! range.endContainer.length ) || ( range.startOffset === range.endContainer.length ) ) &&
     396                                        isView( node.nextSibling ) ) {
     397                                select( node.nextSibling );
     398                                event.preventDefault();
     399                        }
     400                }
     401        });
     402
    334403        editor.on( 'keyup', function( event ) {
    335404                var padNode,
    336405                        keyCode = event.keyCode,