2 * JavaScript for diff views
4 const inlineFormatToggle = require( './inlineFormatToggle.js' );
10 * @return {string|undefined}
13 function getNodeSide( node ) {
14 if ( $( node ).closest( '.diff-side-deleted' ).length !== 0 ) {
16 } else if ( $( node ).closest( '.diff-side-added' ).length !== 0 ) {
19 // Not inside the diff.
25 * @return {string|undefined}
28 function getCurrentlyLockedSide() {
29 return $( '.diff' ).attr( 'data-selected-side' );
33 * @param {string|undefined} side Either "added" or "deleted", or undefined to unset.
36 function setSideLock( side ) {
37 $( '.diff' ).attr( 'data-selected-side', side );
41 * @param {MouseEvent} e
44 function maybeClearSelectProtection( e ) {
45 if ( e.button === 2 ) {
49 const clickSide = getNodeSide( e.target );
50 if ( getCurrentlyLockedSide() !== clickSide ) {
51 document.getSelection().removeAllRanges();
53 setSideLock( clickSide );
56 function selectionHandler() {
57 const textNode = document.getSelection().anchorNode;
63 setSideLock( getNodeSide( textNode ) );
66 $( document ).on( 'selectionchange', selectionHandler );
67 $( document ).on( 'mousedown', maybeClearSelectProtection );
72 '.mw-diff-inline-moved del',
73 '.mw-diff-inline-moved ins',
74 '.mw-diff-inline-changed del',
75 '.mw-diff-inline-changed ins',
76 '.mw-diff-inline-added ins',
77 '.mw-diff-inline-deleted del'
80 * Shows a tooltip when added/deleted text is clicked on a tablet or mobile resolution.
85 // Limit this behaviour to mobile.
86 const widthBreakpointTablet = 720;
87 const isTabletOrMobile = window.matchMedia( `(max-width: ${ widthBreakpointTablet }px)` );
88 if ( !isTabletOrMobile.matches ) {
91 const contentAdded = ev.target && ev.target.matches( 'ins' );
92 const text = contentAdded ?
93 mw.msg( 'diff-inline-tooltip-ins' ) :
94 mw.msg( 'diff-inline-tooltip-del' );
95 mw.loader.using( 'oojs-ui-core' ).then( () => {
96 const popup = new OO.ui.PopupWidget( {
97 $content: $( '<p>' ).text( text ),
102 $floatableContainer: $( ev.target ),
104 classes: [ 'mw-diff-popup' ],
107 $( OO.ui.getTeleportTarget() ).append( popup.$element );
108 popup.toggle( true );
109 popup.toggleClipping( true );
115 // If there is a diff present on page, load the toggle.
116 const $inlineToggleSwitchLayout = $( '#mw-diffPage-inline-toggle-switch-layout' );
117 // Return if inline switch is not displaying.
118 if ( $inlineToggleSwitchLayout.length ) {
119 mw.loader.using( 'oojs-ui' ).then( () => {
120 inlineFormatToggle( $inlineToggleSwitchLayout );