Make WordPress Core

Changeset 59035


Ignore:
Timestamp:
09/17/2024 09:56:43 PM (4 weeks ago)
Author:
antpb
Message:

Media: Add Ctrl/Command + Enter shortcut to insert selected Media Library items.

Adds a Ctrl/Command + Enter keyboard shortcut to insert the currently selected single media or multiple media items when selecting in the Media Library modal.

Props poena, hirschferkel, antpb, joedolson, skobe, rcreators, plaidharper.
Fixes #60369.

Location:
trunk/src/js/media/views
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/js/media/views/attachment.js

    r57515 r59035  
    200200        }
    201201
     202        // Avoid toggles when the command or control key is pressed with the enter key to prevent deselecting the last selected attachment.
     203        if ( ( event.metaKey || event.ctrlKey ) && ( 13 === event.keyCode || 10 === event.keyCode ) ) {
     204            return;
     205        }
     206
    202207        this.toggleSelection({
    203208            method: method
  • trunk/src/js/media/views/modal.js

    r52242 r59035  
    182182
    183183    /**
     184     * Handles the selection of attachments when the command or control key is pressed with the enter key.
     185     *
     186     * @since 6.7
     187     *
     188     * @param {Object} event The keydown event object.
     189     */
     190    selectHandler: function( event ) {
     191        var selection = this.controller.state().get( 'selection' );
     192
     193        if ( ! selection.length > 0 ) {
     194            return;
     195        }
     196
     197        if ( 'insert' === this.controller.options.state ) {
     198            this.controller.trigger( 'insert', selection );
     199        } else {
     200            this.controller.trigger( 'select', selection );
     201            event.preventDefault();
     202            this.escape();
     203        }
     204    },
     205
     206    /**
    184207     * @param {Array|Object} content Views to register to '.media-modal-content'
    185208     * @return {wp.media.view.Modal} Returns itself to allow chaining.
     
    215238            event.stopImmediatePropagation();
    216239        }
     240
     241        // Select the attachment when command or control and enter are pressed.
     242        if ( ( 13 === event.which || 10 === event.which ) && ( event.metaKey || event.ctrlKey ) ) {
     243            this.selectHandler( event );
     244            event.stopImmediatePropagation();
     245        }
     246
    217247    }
    218248});
Note: See TracChangeset for help on using the changeset viewer.