MediaWiki:Gadget-defaultsummaries.js: Difference between revisions

From RuneRealm Wiki
Jump to navigation Jump to search
Content added Content deleted
No edit summary
Tag: Reverted
No edit summary
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
"use strict";
/** * 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(); } );})

/**
* 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();
});
});

Latest revision as of 12:06, 20 October 2024

"use strict";

/**
 * 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();
  });
});