MediaWiki:Gadget-defaultsummaries.js:修订间差异
外观
小无编辑摘要 |
小无编辑摘要 |
||
| 第1行: | 第1行: | ||
(() => { | (() => { | ||
'use strict'; | 'use strict'; | ||
// 只保留你喜欢的第二套选项,想加/删直接改这个数组就行 | |||
const SUMMARY_OPTIONS = [ | |||
'新条目', | |||
'修饰语句', | |||
'修正笔误', | |||
'内容扩充', | |||
'排版(调整格式)', | |||
'来源(增加/调整参考资料)', | |||
'内部链接(增加/调整)', | |||
'分类(增加/调整)', | |||
'消歧义', | |||
'维基化(适配维基格式)', | |||
'维护清理' | |||
]; | |||
const STYLES = { | const STYLES = { | ||
dropdownContainer: { | dropdownContainer: { | ||
width: ' | width: '100%', | ||
paddingBottom: '1em' | paddingBottom: '1em' | ||
} | } | ||
| 第67行: | 第38行: | ||
} | } | ||
// 适配可视化编辑器(VE) | |||
setupVisualEditorHook() { | setupVisualEditorHook() { | ||
mw.hook('ve.saveDialog.stateChanged').add(() => { | mw.hook('ve.saveDialog.stateChanged').add(() => { | ||
| 第78行: | 第50行: | ||
$('body').addClass('has-summary-dropdowns'); | $('body').addClass('has-summary-dropdowns'); | ||
$saveOptions.before(this. | $saveOptions.before(this.createDropdown()); | ||
}); | }); | ||
} | } | ||
// 适配传统编辑器(WikiEditor) | |||
setupWikiEditor() { | setupWikiEditor() { | ||
Promise.all([ | Promise.all([ | ||
| 第91行: | 第64行: | ||
$editCheckboxes.before( | $editCheckboxes.before( | ||
this. | this.createDropdown().css(STYLES.dropdownContainer) | ||
); | ); | ||
}); | }); | ||
} | } | ||
// 只创建一个下拉菜单(包含所有第二套选项) | |||
createDropdown() { | |||
const dropdown = new OO.ui.DropdownWidget({ | const dropdown = new OO.ui.DropdownWidget({ | ||
label: ' | label: '快捷摘要' // 下拉菜单标题,可改成自己想显示的文字 | ||
}); | }); | ||
// 点击选项填充到摘要框 | |||
dropdown.menu.on('select', this.handleSummarySelect.bind(this)); | dropdown.menu.on('select', this.handleSummarySelect.bind(this)); | ||
// 给下拉菜单添加你喜欢的选项 | |||
this.addOptionsToDropdown(dropdown, SUMMARY_OPTIONS); | |||
return dropdown.$element | return dropdown.$element; | ||
} | } | ||
// 给下拉菜单添加选项的工具方法 | |||
addOptionsToDropdown(dropdown, optionTexts) { | addOptionsToDropdown(dropdown, optionTexts) { | ||
dropdown.menu.addItems( | dropdown.menu.addItems( | ||
| 第133行: | 第91行: | ||
} | } | ||
// 处理选项点击:填充到摘要框,支持拼接已有内容 | |||
handleSummarySelect(option) { | handleSummarySelect(option) { | ||
const originalValue = this.summaryBox.val(); | const originalValue = this.summaryBox.val(); | ||
// 已有内容则加空格拼接,没有就直接填充 | |||
const newSummary = originalValue.length && !originalValue.endsWith(' ') | const newSummary = originalValue.length && !originalValue.endsWith(' ') | ||
? `${originalValue} ${option.getLabel()}` | ? `${originalValue} ${option.getLabel()}` | ||
| 第143行: | 第103行: | ||
} | } | ||
// | // 页面加载完成后初始化 | ||
$(() => new SummaryHelper()); | $(() => new SummaryHelper()); | ||
})(); | })(); | ||
2025年11月10日 (一) 17:27的版本
(() => {
'use strict';
// 只保留你喜欢的第二套选项,想加/删直接改这个数组就行
const SUMMARY_OPTIONS = [
'新条目',
'修饰语句',
'修正笔误',
'内容扩充',
'排版(调整格式)',
'来源(增加/调整参考资料)',
'内部链接(增加/调整)',
'分类(增加/调整)',
'消歧义',
'维基化(适配维基格式)',
'维护清理'
];
const STYLES = {
dropdownContainer: {
width: '100%',
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;
}
// 适配可视化编辑器(VE)
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.createDropdown());
});
}
// 适配传统编辑器(WikiEditor)
setupWikiEditor() {
Promise.all([
mw.loader.using('oojs-ui-core'),
$.ready
]).then(() => {
const $editCheckboxes = $('.editCheckboxes');
if (!$editCheckboxes.length) return;
$editCheckboxes.before(
this.createDropdown().css(STYLES.dropdownContainer)
);
});
}
// 只创建一个下拉菜单(包含所有第二套选项)
createDropdown() {
const dropdown = new OO.ui.DropdownWidget({
label: '快捷摘要' // 下拉菜单标题,可改成自己想显示的文字
});
// 点击选项填充到摘要框
dropdown.menu.on('select', this.handleSummarySelect.bind(this));
// 给下拉菜单添加你喜欢的选项
this.addOptionsToDropdown(dropdown, SUMMARY_OPTIONS);
return dropdown.$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');
}
}
// 页面加载完成后初始化
$(() => new SummaryHelper());
})();