MediaWiki:Gadget-summary-preview.js

Z Wikisłownika – wolnego słownika wielojęzycznego

Uwaga: aby zobaczyć zmiany po opublikowaniu, może zajść potrzeba wyczyszczenia pamięci podręcznej przeglądarki.

  • Firefox / Safari: Przytrzymaj Shift podczas klikania Odśwież bieżącą stronę, lub naciśnij klawisze Ctrl+F5, lub Ctrl+R (⌘-R na komputerze Mac)
  • Google Chrome: Naciśnij Ctrl-Shift-R (⌘-Shift-R na komputerze Mac)
  • Internet Explorer / Edge: Przytrzymaj Ctrl, jednocześnie klikając Odśwież, lub naciśnij klawisze Ctrl+F5
  • Opera: Naciśnij klawisze Ctrl+F5.
var api,
	previousSummary,
	config = mw.config.get( [
		'wgAction',
		'wgNamespaceNumber',
		'wgNamespaceIds',
		'wgCanonicalSpecialPageName',
		'wgCommentCodePointLimit'
	] ),
	actions = [
		'delete',
		'edit',
		'protect',
		'submit',
		'unprotect'
	],
	specialPages = [
		'Block',
		'Nuke',
		'Import',
		'MergeHistory',
		'Revisiondelete',
		'RevisionReview',
		'Tags',
		'Unblock',
		'Undelete',
		'Userrights'
	],
	reasonSelectors = [
		'#wpSummary',
		'#wpReason',
		'#mwProtect-reason',
		'#mw-input-wpReason-other',
		'#mw-interwiki-comment',
		'#wpComment'
	],
	reasonListSelectors = [
		'#wpDeleteReasonList',
		'#wpProtectReasonSelection',
		'#mw-input-wpReason',
		'#wpRevDeleteReasonList'
	],
	previewDelayMs = 250,
	currentRequest = null;

mw.messages.set( {
	'summary-preview-label': 'Podgląd:'
} );

function fetchSummary( $reason, $comment, predefinedReason ) {
	var summary = $reason.textSelection( 'getContents' );
	
	if ( typeof predefinedReason === 'string' && predefinedReason !== '' ) {
		if ( summary === '' ) {
			summary = predefinedReason;
		} else {
			summary = mw.format( '$1: $2', predefinedReason, summary );
		}
	}
	
	if ( summary === previousSummary ) {
		return;
	}
	
	if ( summary === '' ) {
		updateSummaryPreview( $comment, '' );
		return;
	}
	
	if ( mw.libs.String.codePointLength( summary ) > config.wgCommentCodePointLimit ) {
		do {
			summary = summary.slice( 0, -1 );
		} while ( mw.libs.String.codePointLength( summary ) > config.wgCommentCodePointLimit - 3 );
		
		summary += '...';
	}
	
	currentRequest = api.get( {
		formatversion: 2,
		action: 'parse',
		summary: summary,
		prop: ''
	} );
	
	currentRequest.done( function ( res ) {
		updateSummaryPreview( $comment, res.parse.parsedsummary );
	} );
}

function updateSummaryPreview( $el, summary ) {
	previousSummary = summary;
	summary = mw.format( '($1)', summary );
	$el.html( summary );
}

function fetchPredefinedSummary( $reasonList, $reason, $comment ) {
	var value = $reasonList.find( ':selected' ).val();
	
	if ( value === 'other' ) {
		fetchSummary( $reason, $comment, '' );
	} else {
		fetchSummary( $reason, $comment, value );
	}
}

if ( actions.indexOf( config.wgAction ) !== -1 || (
	config.wgNamespaceNumber === config.wgNamespaceIds.special &&
	specialPages.indexOf( config.wgCanonicalSpecialPageName ) !== -1
) ) {
	$.when(
		mw.loader.using( [
			'mediawiki.api',
			'mediawiki.util',
			'mediawiki.String',
			'jquery.textSelection'
		] ),
		$.ready
	)
	.done( function ( require ) {
		var $comment, $reason, $reasonList, bouncer;
		
		api = new mw.Api();
		mw.libs.String = require( 'mediawiki.String' );
		
		$comment = $( '<span>' ).addClass( 'comment' );
		
		if (
			config.wgCanonicalSpecialPageName === 'Tags' ||
			config.wgCanonicalSpecialPageName === 'Unblock'
		) {
			$reason = $( '#mw-input-wpReason' );
			$reasonList = $();
		} else {
			$reason = $( reasonSelectors.join( ',' ) );
			$reasonList = $( reasonListSelectors.join( ',' ) );
		}
		
		bouncer = mw.util.debounce( previewDelayMs, function ( evt ) {
			if ( currentRequest !== null ) {
				currentRequest.abort();
			}
			
			if ( evt.type !== 'KeyboardEvent' || evt.which !== 13 ) {
				fetchPredefinedSummary( $reasonList, $reason, $comment );
			}
		} );
		
		fetchPredefinedSummary( $reasonList, $reason, $comment );
		
		if (
			config.wgAction === 'delete' ||
			config.wgCanonicalSpecialPageName === 'Nuke'
		) {
			$( '<div>' ).attr( 'id', 'summary-preview' )
				.append(
					mw.msg( 'summary-preview-label' ) + ' ',
					$comment
				)
				.insertAfter( $reason );
		} else if ( config.wgCanonicalSpecialPageName === 'RevisionReview' ) {
			$reason.after( '<br>', mw.msg( 'summary-preview-label' ), ' ', $comment );
		} else {
			$( '<tr>' )
				.append(
					$( '<td>' )
						.addClass( 'mw-label' )
						.text( mw.msg( 'summary-preview-label' ) ),
					$( '<td>' )
						.append( $comment )
				)
				.insertAfter( $reason.closest( 'tr' ) );
		}
		
		$reason.on( 'input', bouncer );
		$reasonList.on( 'change keyup', bouncer );
		$( '#mw-content-text' ).on( 'click', '.userSummaryButtons > a', bouncer );
	} );
}