MediaWiki:Gadget-defaultsummaries.js: Difference between revisions

From RuneRealm Wiki
Jump to navigation Jump to search
Content added Content deleted
(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...")
 
No edit summary
 
(2 intermediate revisions 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.
* For convenience, adds a dropdown box with some canned edit summaries to the editor.
Line 5: Line 7:


$(function () {
$(function () {
var $summaryBox = $( '#wpSummary' ),
var $summaryBox = $('#wpSummary'),
$saveButton = $( '#wpSave' ),
$saveButton = $('#wpSave'),
lastkey = 'rsw-defsum-last',
lastkey = 'rsw-defsum-last',
uselastkey = 'rsw-defsum-uselast',
uselastkey = 'rsw-defsum-uselast',
hasls = false,
hasls = false,
minorSummaries = ['Fixed spelling', 'Correcting [[RS:LAYOUT|page layout]]', 'Revert vandalism', 'Reworded', 'Remove [[RS:POLICIES|policy]] violation'],
minorSummaries = [
articleSummaries = ['Expanding', 'Adding [[RS:REF|refs]]', 'Adding links', 'Maintenance'],
'Fixed spelling',
nonArticleSummaries = ['Reply', 'Comment', 'Support', 'Oppose'];
'Correcting [[RS:LAYOUT|page layout]]',
try {
'Revert vandalism',
localStorage.setItem('test', 'test');
'Reworded',
localStorage.removeItem('test');
'Remove [[RS:POLICIES|policy]] violation'
hasls = true;
],
} catch (e) {
articleSummaries = [
mw.warn('Browser does not support localStorage');
'Expanding',
hasls = false;
'Adding [[RS:REF|refs]]',
}
'Adding links',
function addOptionsToDropdown(dropdown, optionTexts) {
'Maintenance'
dropdown.menu.addItems(optionTexts.map(function (optionText) {
],
return new OO.ui.MenuOptionWidget({
nonArticleSummaries = [
label: optionText
'Reply',
});
'Comment',
}));
'Support',
}
'Oppose'
function onSummarySelect(option) {
];
// Save the original value of the edit summary field

var editsummOriginalSummary = $summaryBox.val(),
try {
canned = option.getLabel(),
localStorage.setItem('test', 'test')
newSummary = editsummOriginalSummary;
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' );


// Append old edit summary with space, if exists,
// If we failed to find the editCheckboxes class
// and last character != space
if ( !$editCheckboxes.length ) {
if (newSummary.length !== 0 && newSummary.charAt(newSummary.length - 1) !== ' ') {
return;
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
document.body.classList.add( 'gadget-default-summaries' );
dropdown.menu.addItems([new OO.ui.MenuSectionOptionWidget({
label: 'Standard summaries'
})]);
if (namespace === 0) {
addOptionsToDropdown(dropdown, articleSummaries);
} else {
addOptionsToDropdown(dropdown, nonArticleSummaries);
}


// Minor section
$saveButton.on('click', onSave);
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
insertSummaryOptions( $( '.mw-summary .oo-ui-fieldLayout-body' ) );
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
mw.hook( 'ext.gadget.defaultSummaries' ).fire();
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();
  });
});