MediaWiki:Common.js:修订间差异
外观
| 第1行: | 第1行: | ||
/** | |||
* 失传媒体维基 - 快捷分类按钮 | |||
* 在编辑页面顶部添加常用分类快捷按钮 | |||
*/ | |||
$( function() { | |||
// 仅在编辑页面执行 | |||
if ( mw.config.get( 'wgAction' ) !== 'edit' ) return; | |||
// 定义快捷分类按钮 | |||
var quickCategories = [ | |||
// 状态分类 | |||
{ label: '丢失', category: '丢失', style: 'background:#ffcccc;' }, | |||
{ label: '部分发现', category: '部分发现', style: 'background:#fff3cd;' }, | |||
{ label: '发现', category: '发现', style: 'background:#d4edda;' }, | |||
// 类型分类(分隔线后) | |||
{ label: '视频', category: '视频' }, | |||
{ label: '音频', category: '音频' }, | |||
{ label: '图片', category: '图片' }, | |||
{ label: '游戏', category: '游戏' }, | |||
{ label: '文本', category: '文本' } | |||
]; | |||
// 创建按钮容器 | |||
var $container = $( '<div>' ) | |||
.css( { | |||
'padding': '10px', | |||
'margin-bottom': '15px', | |||
'border': '1px solid #ccc', | |||
'border-radius': '4px', | |||
'background': '#f9f9f9' | |||
} ) | |||
.prepend( '<b>快捷分类:</b> ' ); | |||
// 生成按钮 | |||
quickCategories.forEach( function( item, index ) { | |||
// 在第4个按钮后加分隔线 | |||
if ( index === 3 ) { | |||
$container.append( '<span style="margin:0 10px; border-left:1px solid #ccc;"></span>' ); | |||
} | |||
var $btn = $( '<button>' ) | |||
.text( item.label ) | |||
.attr( 'type', 'button' ) | |||
.css( { | |||
'margin': '0 5px 5px 0', | |||
'padding': '4px 10px', | |||
'border': '1px solid #999', | |||
'border-radius': '3px', | |||
'cursor': 'pointer', | |||
'font-size': '0.9em' | |||
} ) | |||
.css( item.style || {} ) // 应用自定义样式 | |||
.on( 'click', function() { | |||
// 获取当前编辑框内容 | |||
var textarea = $( '#wpTextbox1' )[0]; | |||
var content = textarea.value; | |||
var categoryTag = '[[分类:' + item.category + ']]'; | |||
// 检查分类是否已存在 | |||
if ( content.indexOf( categoryTag ) === -1 ) { | |||
// 在内容末尾添加分类 | |||
if ( !content.endsWith( '\n' ) ) { | |||
content += '\n'; | |||
} | |||
content += categoryTag + '\n'; | |||
textarea.value = content; | |||
$( this ).css( 'opacity', '0.5' ).text( '已添加: ' + item.label ); | |||
} else { | |||
alert( '分类“' + item.category + '”已存在!' ); | |||
} | |||
} ); | |||
$container.append( $btn ); | |||
} ); | |||
// 将按钮容器插入到编辑框上方 | |||
$container.insertBefore( '#wpTextbox1' ); | |||
} ); | |||
2026年2月22日 (日) 17:29的版本
/**
* 失传媒体维基 - 快捷分类按钮
* 在编辑页面顶部添加常用分类快捷按钮
*/
$( function() {
// 仅在编辑页面执行
if ( mw.config.get( 'wgAction' ) !== 'edit' ) return;
// 定义快捷分类按钮
var quickCategories = [
// 状态分类
{ label: '丢失', category: '丢失', style: 'background:#ffcccc;' },
{ label: '部分发现', category: '部分发现', style: 'background:#fff3cd;' },
{ label: '发现', category: '发现', style: 'background:#d4edda;' },
// 类型分类(分隔线后)
{ label: '视频', category: '视频' },
{ label: '音频', category: '音频' },
{ label: '图片', category: '图片' },
{ label: '游戏', category: '游戏' },
{ label: '文本', category: '文本' }
];
// 创建按钮容器
var $container = $( '<div>' )
.css( {
'padding': '10px',
'margin-bottom': '15px',
'border': '1px solid #ccc',
'border-radius': '4px',
'background': '#f9f9f9'
} )
.prepend( '<b>快捷分类:</b> ' );
// 生成按钮
quickCategories.forEach( function( item, index ) {
// 在第4个按钮后加分隔线
if ( index === 3 ) {
$container.append( '<span style="margin:0 10px; border-left:1px solid #ccc;"></span>' );
}
var $btn = $( '<button>' )
.text( item.label )
.attr( 'type', 'button' )
.css( {
'margin': '0 5px 5px 0',
'padding': '4px 10px',
'border': '1px solid #999',
'border-radius': '3px',
'cursor': 'pointer',
'font-size': '0.9em'
} )
.css( item.style || {} ) // 应用自定义样式
.on( 'click', function() {
// 获取当前编辑框内容
var textarea = $( '#wpTextbox1' )[0];
var content = textarea.value;
var categoryTag = '[[分类:' + item.category + ']]';
// 检查分类是否已存在
if ( content.indexOf( categoryTag ) === -1 ) {
// 在内容末尾添加分类
if ( !content.endsWith( '\n' ) ) {
content += '\n';
}
content += categoryTag + '\n';
textarea.value = content;
$( this ).css( 'opacity', '0.5' ).text( '已添加: ' + item.label );
} else {
alert( '分类“' + item.category + '”已存在!' );
}
} );
$container.append( $btn );
} );
// 将按钮容器插入到编辑框上方
$container.insertBefore( '#wpTextbox1' );
} );