| 1 | /*! |
| 2 | * Enable right-click-to-edit functionality. |
| 3 | * |
| 4 | * When the user right-clicks in a content heading, it will open the |
| 5 | * edit section link. |
| 6 | */ |
| 7 | ( function () { |
| 8 | if ( Number( mw.user.options.get( 'editsectiononrightclick' ) ) !== 1 ) { |
| 9 | // Support both 1 or "1" (T54542) |
| 10 | return; |
| 11 | } |
| 12 | |
| 13 | // Trigger this when a contextmenu click on the page targets an h1-h6 element. |
| 14 | // This uses a delegate handler which 1) starts immediately instead of blocking |
| 15 | // response on dom-ready, and 2) selects and binds once instead of N times. |
| 16 | // (Plain tags needed for compatibility with skin option supportsMwHeading=false) |
| 17 | $( document ).on( 'contextmenu', '.mw-heading, h1, h2, h3, h4, h5, h6', function ( e ) { |
| 18 | // Don't use ":has:(.mw-editsection a)" in the selector because it's slow. |
| 19 | const $edit = $( this ).find( '.mw-editsection a' ); |
| 20 | if ( !$edit.length ) { |
| 21 | return; |
| 22 | } |
| 23 | |
| 24 | // Headings can contain rich text. |
| 25 | // Make sure to not block contextmenu events on (other) anchor tags |
| 26 | // inside the heading (e.g. to do things like copy URL, open in new tab, ..). |
| 27 | // e.target can be the heading, but it can also be anything inside the heading. |
| 28 | if ( !$( e.target ).closest( 'a' ).length ) { |
| 29 | // Trigger native HTMLElement click instead of opening URL (T45052) |
| 30 | e.preventDefault(); |
| 31 | $edit.get( 0 ).click(); |
| 32 | } |
| 33 | } ); |
| 34 | }() ); |