MediaWiki:Gadget-defaultsummaries.js
外观
注意:在发布之后,您可能需要清除浏览器缓存才能看到所作出的更改的影响。
- Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5或Ctrl-R(Mac为⌘-R)
- Google Chrome:按Ctrl-Shift-R(Mac为⌘-Shift-R)
- Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5。
/* _____________________________________________________________________________
* | |
* | === WARNING: GLOBAL GADGET FILE === |
* | Changes to this page affect many users. |
* | Please discuss changes on the talk page or on [[WT:Gadget]] before editing. |
* |_____________________________________________________________________________|
*
* Imported as of 09/06/2011 from [[User:ErrantX/defaultsummaries.js]]
* Edited version from [[User:MC10/defaultsummaries.js]]
* Implements default edit summary dropdown boxes
*/
/* global mw, ve */
/* eslint-disable no-jquery/no-global-selector */
(() => {
'use strict';
const SUMMARY_LISTS = {
minor: [
'拼写/语法/标点符号/排版更正',
'修复样式/布局错误',
'回退破坏或测试编辑',
'回退无法解释的内容删除',
'修改(小编辑)'
],
article: [
'扩展文章',
'添加/改进参考资料',
'添加/删除维基链接',
'清理/修改',
'添加/删除类别',
'添加/删除外部链接',
'删除无源内容'
],
nonArticle: [
'回复',
'注释',
'建议'
],
talkPage: [
'WikiProject 标记',
'WikiProject 评估'
]
};
const STYLES = {
dropdownContainer: {
width: '48%',
paddingBottom: '1em'
}
};
class SummaryHelper {
constructor() {
this.summaryBox = $('#wpSummary');
this.initialized = false;
this.init();
}
init() {
if (this.initialized) return;
this.setupVisualEditorHook();
this.setupWikiEditor();
this.initialized = true;
}
setupVisualEditorHook() {
mw.hook('ve.saveDialog.stateChanged').add(() => {
if ($('body').hasClass('has-summary-dropdowns')) return;
const target = ve.init.target;
const $saveOptions = target.saveDialog.$saveOptions;
this.summaryBox = target.saveDialog.editSummaryInput.$input;
if (!$saveOptions.length) return;
$('body').addClass('has-summary-dropdowns');
$saveOptions.before(this.createDropdowns());
});
}
setupWikiEditor() {
Promise.all([
mw.loader.using('oojs-ui-core'),
$.ready
]).then(() => {
const $editCheckboxes = $('.editCheckboxes');
if (!$editCheckboxes.length) return;
$editCheckboxes.before(
this.createDropdowns().css(STYLES.dropdownContainer)
);
});
}
createDropdowns() {
const namespace = mw.config.get('wgNamespaceNumber');
const isArticle = namespace === 0;
const isDraft = namespace === 118;
const isTalkPage = namespace % 2 !== 0 && namespace !== 3;
const dropdown = new OO.ui.DropdownWidget({
label: '通用编辑摘要'
});
const minorDropdown = new OO.ui.DropdownWidget({
label: '常见小编辑摘要'
});
dropdown.menu.on('select', this.handleSummarySelect.bind(this));
minorDropdown.menu.on('select', this.handleSummarySelect.bind(this));
this.addOptionsToDropdown(minorDropdown, SUMMARY_LISTS.minor);
if (isArticle || isDraft) {
this.addOptionsToDropdown(dropdown, SUMMARY_LISTS.article);
} else {
this.addOptionsToDropdown(dropdown, SUMMARY_LISTS.nonArticle);
if (isTalkPage) {
this.addOptionsToDropdown(dropdown, SUMMARY_LISTS.talkPage);
}
}
return dropdown.$element.add(minorDropdown.$element);
}
addOptionsToDropdown(dropdown, optionTexts) {
dropdown.menu.addItems(
optionTexts.map(text => new OO.ui.MenuOptionWidget({ label: text }))
);
}
handleSummarySelect(option) {
const originalValue = this.summaryBox.val();
const newSummary = originalValue.length && !originalValue.endsWith(' ')
? `${originalValue} ${option.getLabel()}`
: originalValue + option.getLabel();
this.summaryBox.val(newSummary).trigger('change');
}
}
// Initialize on document ready
$(() => new SummaryHelper());
})();