MediaWiki:Gadget-defaultsummaries.js

From RuneRealm Wiki

This is an old revision of this page, as edited by Alex (talk | contribs) at 02:05, 13 October 2024 (Created page with "→‎* * For convenience, adds a dropdown box with some canned edit summaries to the editor. * Modified version of https://en.wikipedia.org/wiki/MediaWiki:Gadget-defaultsummaries.js: $(function () { var $summaryBox = $( '#wpSummary' ), $saveButton = $( '#wpSave' ), lastkey = 'rsw-defsum-last', uselastkey = 'rsw-defsum-uselast', hasls = false, minorSummaries = [ 'Fixed spelling', 'Correcting page layout', 'Revert vandalism', 'Rewor..."). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

After saving, you may need to bypass your browser's cache to see the changes. For further information, see Wikipedia:Bypass your cache.

  • In most Windows and Linux browsers: Hold down Ctrl and press F5.
  • In Safari: Hold down ⇧ Shift and click the Reload button.
  • In Chrome and Firefox for Mac: Hold down both ⌘ Cmd+⇧ Shift and press R.
/**
 * For convenience, adds a dropdown box with some canned edit summaries to the editor.
 * Modified version of https://en.wikipedia.org/wiki/MediaWiki:Gadget-defaultsummaries.js
 */

$(function () {
	var $summaryBox = $( '#wpSummary' ),
		$saveButton = $( '#wpSave' ),
		lastkey = 'rsw-defsum-last',
		uselastkey = 'rsw-defsum-uselast',
		hasls =  false,
		minorSummaries = [
			'Fixed spelling',
			'Correcting [[RS:LAYOUT|page layout]]',
			'Revert vandalism',
			'Reworded',
			'Remove [[RS:POLICIES|policy]] violation'
		],
		articleSummaries = [
			'Expanding',
			'Adding [[RS:REF|refs]]',
			'Adding links',
			'Maintenance'
		],
		nonArticleSummaries = [
			'Reply',
			'Comment',
			'Support',
			'Oppose'
		];

	try {
		localStorage.setItem('test', 'test')
		localStorage.removeItem('test')
		hasls = true;
	} catch (e) {
		mw.warn('Browser does not support localStorage');
		hasls =  false;
	}

	function addOptionsToDropdown( dropdown, optionTexts ) {
		dropdown.menu.addItems( optionTexts.map( function ( optionText ) {
			return new OO.ui.MenuOptionWidget( { label: optionText } );
		} ) );
	}

	function onSummarySelect( option ) {
		// Save the original value of the edit summary field
		var editsummOriginalSummary = $summaryBox.val(),
			canned = option.getLabel(),
			newSummary = editsummOriginalSummary;

		// Append old edit summary with space, if exists,
		// and last character != space
		if ( newSummary.length !== 0 && newSummary.charAt( newSummary.length - 1 ) !== ' ' ) {
			newSummary += ' ';
		}
		newSummary += canned;
		$summaryBox.val( newSummary ).trigger( 'change' );
	}

	function useLastChange ( selected ) {
		if (!hasls) {
			return;
		}
		if (!selected) {
			localStorage.setItem(uselastkey, 'false');
			return;
		}

		localStorage.setItem(uselastkey, 'true');
		var lastSum = localStorage.getItem( lastkey );
		if (lastSum) {
			$summaryBox.val( lastSum ).trigger( 'change' );
		}
	}

	function insertSummaryOptions( $insertAfterThis ) {
		var namespace = mw.config.get( 'wgNamespaceNumber' ),
			container = $('<div class="gadget-default-summaries-container">'),
			dropdown = new OO.ui.DropdownWidget( {
				label: 'Default summaries',
				classes: [ 'gadget-default-summaries-dropdown' ]
			} ),
			useLastTog = new OO.ui.CheckboxInputWidget( {
				selected: false
			} ),
			togLay = new OO.ui.FieldLayout( useLastTog, {
				label: 'Use last summary',
				align: 'inline',
				classes: [ 'gadget-default-summaries-toggle' ]
			} );

		dropdown.menu.on( 'select', onSummarySelect );

		if (hasls) {
			var lastSummary = localStorage.getItem( lastkey ),
				useLast = localStorage.getItem( uselastkey );
			useLastTog.on('change', useLastChange );
			if ( useLast == 'true' ) {
				useLastTog.setSelected(true);
			}
			if (lastSummary) {
				articleSummaries.unshift(lastSummary);
				nonArticleSummaries.unshift(lastSummary);
			}
		} else {
			useLastTog.setDisabled(true);
		}

		// Main section
		dropdown.menu.addItems( [
			new OO.ui.MenuSectionOptionWidget( { label: 'Standard summaries' } )
		] );

		if ( namespace === 0 ) {
			addOptionsToDropdown( dropdown, articleSummaries );
		} else {
			addOptionsToDropdown( dropdown, nonArticleSummaries );
		}

		// Minor section
		dropdown.menu.addItems( [
			new OO.ui.MenuSectionOptionWidget( { label: 'Minor summaries' } )
		] );

		addOptionsToDropdown( dropdown, minorSummaries );

		container.append( togLay.$element );
		container.append( dropdown.$element );
		$insertAfterThis.after( container );
	}

	function onSave() {
		mw.log('saving article');
		var summary = $summaryBox.val();
		localStorage.setItem(lastkey, summary);
	}

	// VisualEditor
	mw.hook( 've.saveDialog.stateChanged' ).add( function () {
		var target, $saveOptions;
		// .ve-init-mw-viewPageTarget-saveDialog-checkboxes
		if ( $( 'body' ).data( 'wppresent' ) ) { return; }
		$( 'body' ).data( 'wppresent', 'true' );

		target = ve.init.target;
		$saveOptions = target.saveDialog.$saveOptions;
		$summaryBox = target.saveDialog.editSummaryInput.$input;
		$saveButton = target.saveDialog.$primaryActions.find('a');

		if ( !$saveOptions.length ) {
			return;
		}

		document.body.classList.add( 'gadget-default-summaries' );

		$saveButton.on( 'click', onSave );

		insertSummaryOptions( $( '.ve-ui-mwSaveDialog-summary' ) );

		mw.hook( 'ext.gadget.defaultSummaries' ).fire();
	} );
	// WikiEditor
	$.when( mw.loader.using( 'oojs-ui-core' ), $.ready ).then( function () {
		var $editCheckboxes = $( '.editCheckboxes' );

		// If we failed to find the editCheckboxes class
		if ( !$editCheckboxes.length ) {
			return;
		}

		document.body.classList.add( 'gadget-default-summaries' );

		$saveButton.on('click', onSave);

		insertSummaryOptions( $( '.mw-summary .oo-ui-fieldLayout-body' ) );

		mw.hook( 'ext.gadget.defaultSummaries' ).fire();
	} );
})