LLWiki正在建设中,欢迎加入我们

“MediaWiki:Gadget-mainpage-edit.js”的版本间差异

来自LLWiki
跳转到导航 跳转到搜索
Zth讨论 | 贡献
 
(未显示2个用户的36个中间版本)
第1行: 第1行:
//<nowiki>
"use strict";
"use strict";
/* global OO, wgULS */
/* global OO, wgULS */
mw.loader.using( ['mediawiki.api', 'oojs-ui-windows', 'ext.gadget.site-lib'] ).then(() => {
mw.loader.using( ['mediawiki.api', 'oojs-ui-windows', 'ext.gadget.site-lib'] ).then(() => {
mw.messages.set( wgULS({
mw.messages.set( $.extend( wgULS({
'gadget-ma-summary': '使用Mainpage小工具快速编辑', 'gadget-ma-niji': '虹学',
'gadget-ma-summary': '使用Mainpage小工具快速编辑', 'gadget-ma-niji': '虹学会', 'gadget-ma-preview': '预览',
'gadget-ma-prompt': '请输入新的行号(从1开始计数)顺序,以空格分隔。未包括的行将被删除,不存在的空行将被创建。'
'gadget-ma-prompt': '请输入新的行号(从1开始计数)顺序,以空格分隔。未包括的行将被删除,不存在的空行将被创建。',
'gadget-ma-error': '输入内容错误!只接受以空格分隔的正整数。', 'gadget-ma-topic': '首页/当年今日',
}, {
'gadget-ma-summary': '使用Mainpage小工具快速編輯', 'gadget-ma-niji': '虹學',
'gadget-ma-protect': '保护页面失败!错误原因:$1', 'gadget-delete': '删除该行',
'gadget-ma-hasunosora': '莲之空'
'gadget-ma-prompt': '請輸入新的行號(從1開始計數)順序,以空格分隔。未包括的行將被刪除,不存在的空行將被創建。'
}) );
}, {
'gadget-ma-summary': '使用Mainpage小工具快速編輯', 'gadget-ma-niji': '虹學會', 'gadget-ma-preview': '預覽',
let items = [];
'gadget-ma-prompt': '請輸入新的行號(從1開始計數)順序,以空格分隔。未包括的行將被刪除,不存在的空行將被創建。',
const $menu = mw.menu([ {text: "µ's", data: {color: '#e4007f', name: 'muse'}},
'gadget-ma-error': '輸入內容錯誤!只接受以空格分割的正整數。', 'gadget-ma-topic': '首頁/當年今日',
{text: 'Aqours', data: {color: '#32aaff'}}, {text: 'Saint Snow', data: {color: '#cb3935'}},
'gadget-ma-protect': '保護頁面失敗!錯誤原因:$1', 'gadget-delete': '刪除該行',
{text: mw.msg( 'gadget-ma-niji' ), data: {color: '#f39800', name: 'nijigaku'}},
'gadget-ma-hasunosora': '蓮之空'
{text: 'Liella!', data: {color: '#a5469c', name: 'liella'}},
}), {'gadget-insertBefore': '在上方插入一行', 'gadget-insertAfter': '在下方插入一行'}) );
{text: 'SIF', data: {color: '#77d2af'}}, {text: 'AS', data: {color: '#ff91af'}}
let items = [], $table, $tag, table, wikitext, $row, trIndex;
], {}, 1),
dict = {ll: 'muse', llss: 'aqours', llspst: 'liella'},
const menu = mw.menu([ {text: "µ's", data: 'muse'}, {text: 'Aqours', data: 'aqours'},
{text: 'Saint Snow', data: 'saint snow'}, {text: mw.msg( 'gadget-ma-niji' ), data: 'nijigaku'},
getName = (ele) => $(ele).data( 'name' ) || ele.textContent.toLowerCase(),
{text: 'Liella!', data: 'liella'}, {text: 'SIF/SIF2', data: 'sif'}, {text: 'AS', data: 'as'},
findSelected = (name) => [...$menu.children()].find(ele => getName( ele ) == (dict[name] || name)),
{text: 'Musical', data: 'musical'}, {text: mw.msg( 'gadget-ma-hasunosora'), data: 'hasunosora'},
writeText = () => '{{#widget:首页}}\n{|\n' + items.filter(ele => ele[0])
{text: '幻日夜羽', data: 'yohane'}
.map(ele => '{{首页/标签|' + ele[0] + '}}' + (ele[2] ? ele[2].getValue() : ele[1] || '') + '\n').join('') + '|}',
]),
id = mw.config.get( 'wgArticleId' ),
colors = { muse: '#E50080', aqours: '#32aaff', 'saint snow': '#cb3935', nijigaku: '#f39800', liella: '#a5469c',
pagename = mw.config.get( 'wgPageName' ),
sif: '#77d2af', as: '#ff91af', musical: '#c40035', hasunosora: '#fb8a9b', yohane: '#19737d'
isMobile = mw.config.get( 'skin' ) == 'minerva',
},
width = $('#mw-content-text').width() * (isMobile ? 1 : 0.32) - (isMobile ? 68 : 36),
dict = {ll: 'muse', llss: 'aqours', llspst: 'liella'},
api = new mw.Api(),
id = mw.config.get( 'wgArticleId' ),
btns = [ new OO.ui.ButtonWidget({label: '保存', flags: ['primary', 'progressive']}).on('click', () => {
pagename = mw.config.get( 'wgPageName' ),
btns[0].setDisabled( true );
isMobile = mw.config.get( 'skin' ) == 'minerva',
mw.safeEdit(api, null, {title: pagename, text: writeText(), summary: mw.msg('gadget-ma-summary')}).then(() => {
mode = pagename.startsWith( 'LLWiki:首页/当年今日/' ),
location.reload();
writeText = () => {
}, reason => { if (reason != 'editConflict') { btns[0].setDisabled( false ); } });
const result = items.map(ele =>
}) ];
'{{首页/标签|' + ele[0] + '}}' + (ele[2] ? ele[2].getValue() : ele[1] || '') + '\n').join(''),
if (id === 0) {
colorMod = mode ? ['rin', 'keke'].filter(ch => new RegExp('{{' + ch + '\/', 'i').test( result )) : [];
$('.noarticletext').toggleClass( 'noarticletext mw-parser-output mainpage-flex' )
return (mode ? '{{#widget:首页}}' : wikitext.slice(0, table.index)) +
.html( $('<table>', {html: '<tbody>'}) );
(colorMod.length ? '{{colorMod|' + colorMod.map(chara => chara + '=1').join('|') + '}}' : '') +
}
'\n{|\n' + result + '|}' +
mw.loader.addStyleTag( '.mainpage-flex td:last-child { width: calc(' + width + 'px - 1.2em); }' );
(mode ? '' : wikitext.slice( table.index + table[0].length ));
$menu.on('click', 'a', function() {
},
$menu.prev( '.mainpage-tag' ).css('background-color', $(this).data( 'color' ));
width = $('#mw-content-text').width() * (isMobile ? 1 : 0.49) - (isMobile ? 68 : 36),
items[ $menu.closest( 'tr' ).index() ][0] = getName( this );
$newTr = $('<tr>', {html: [$('<td>', {html: $('<span>', {class: 'mainpage-tag'})}), '<td>']}),
}).css('z-index', 2);
api = new mw.Api(),
$('.mw-parser-output table').on('dblclick', 'td:first-child', function() {
btns = [ new OO.ui.ButtonWidget({label: '保存', flags: ['primary', 'progressive']}).on('click', () => {
$menu.appendTo( this );
btns[0].setDisabled( true );
$menu.setSelected( findSelected( items[ $(this).parent().index() ][0].toLowerCase() ) ).open();
mw.safeEdit(api, {title: pagename, text: writeText(), section: 0, summary: mw.msg('gadget-ma-summary')})
}).on('dblclick', 'td:last-child', function() {
.then(() => {
const $this = $(this),
(mw.config.get( 'wgRestrictionEdit' ) != 'sysop' || mw.config.get( 'wgRestrictionMove' ) != 'sysop' ?
item = items[ $this.parent().index() ];
api.postWithEditToken({action: 'protect', title: pagename, protections: 'edit=sysop|move=sysop'}) :
item[2] = new OO.ui.MultilineTextInputWidget({autosize: true, value: item[1]});
Promise.resolve()
$this.html( item[2].$element );
).then(() => { location.reload(); },
});
reason => { mw.notify( mw.msg('gadget-ma-protect', reason), {type: 'error', autoHideSeconds: 'long'}); }
(id ? mw.standardQuery( api ) : Promise.resolve()).then(data => {
);
items = data ? [...data.query.pages[0].revisions[0].content.matchAll( /首[页頁]\/[标標][签籤]\|(.+?)}}(.+?)\n/g )]
}, reason => { if (reason != 'editConflict') { btns[0].setDisabled( false ); } });
.map(ele => [ele[1].trim(), ele[2].trim()]) : [];
});
}),
new OO.ui.ButtonWidget({label: mw.msg( 'gadget-ma-preview' )}).on('click', () => {
$('<div>', {html: btns.map(ele => ele.$element)}).appendTo( '.mainpage-flex' ).css('text-align', 'center');
mw.timedParse(api, {text: writeText()}, mw.msg( 'gadget-ma-topic' )).then(data => {
$table.html( $(data.parse.text).find( 'tbody' ) );
}, () => {});
}),
new OO.ui.ButtonWidget({label: '更多'}).on('click', () => {
mw.prompt( mw.msg( 'gadget-ma-prompt' ), 'progressive' ).then(result => {
if (result === null) { return; }
if (/[^\d\s]/.test( result )) {
mw.notify( mw.msg( 'gadget-ma-error' ), {type: 'error', autoHideSeconds: 'long'} );
return;
}
const order = result.split(' ').filter(i => i).map(i => i - 1),
$tr = $table.find( 'tr' );
items = order.map(i => items[i] || ['muse', '']);
$table.children( 'tbody' ).html( order.map(i => $tr[i] || $newTr.clone()) );
});
}) ],
insertRow = (flag) => {
items.splice( trIndex + flag, 0, ['muse', '']);
if ($row.length) { $newTr.clone()[`insert${ flag ? 'After' : 'Before' }`]( $row ); }
else { $newTr.clone().appendTo( $table ); }
},
contextMenu = isMobile ? null : mw.menu([ {text: mw.msg( 'gadget-delete' ), click: () => {
items.splice( trIndex, 1 );
$row.remove();
}}, {text: mw.msg( 'gadget-insertBefore' ), click: () => { insertRow(0); }},
{text: mw.msg( 'gadget-insertAfter' ), click: () => { insertRow(1); }}
], null, true),
optDel = isMobile ? null : contextMenu.findFirstSelectableItem();
if (id === 0) {
$table = $('<table>', {html: '<tbody>'});
$('.noarticletext').toggleClass( 'noarticletext mw-parser-output mainpage-flex' ).html( $table );
} else { $table = $('.mw-parser-output table').first(); }
menu.$element.appendTo( '.mw-parser-output' );
mw.loader.addStyleTag( '.mainpage-flex td:last-child { width: calc(' + width + 'px - 1.2em); }' );
menu.on('choose', option => {
$tag.css('background-color', colors[ option.getData() ]);
items[ $tag.closest( 'tr' ).index() ][0] = option.getData();
});
$table.on('dblclick', 'td:first-child', function() {
const $this = $(this),
name = items[ $this.parent().index() ][0].toLowerCase();
$tag = $this.children( '.mainpage-tag' );
menu.setFloatableContainer( $tag );
menu.selectItemByData( dict[name] || name );
menu.toggle( true );
}).on('dblclick', 'td:last-child', function() {
const $this = $(this),
item = items[ $this.parent().index() ];
item[2] = item[2] || new OO.ui.MultilineTextInputWidget({autosize: true, value: item[1]});
$this.html( item[2].$element );
}).on('contextmenu', function(e) {
if (isMobile) { return; }
e.preventDefault();
$row = $(e.target).closest( 'tr' );
trIndex = $row.index();
optDel.setDisabled( $row.length === 0 );
contextMenu.$element.css({left: e.pageX + 1, top: e.pageY + 1});
contextMenu.toggle( true ); // 先设定位置再显示
});
(id ? mw.sectionQuery( api ) : Promise.resolve()).then(data => {
wikitext = data ? data.parse.wikitext : '';
table = wikitext.match( /\n?{\|\n[\s\S]+?\n\|}(?!})/ ) || [''];
items = [...table[0].matchAll( /首[页頁]\/[标標][签籤]\|(.+?)}}(.+?)\n/g )]
.map(ele => [ele[1].trim(), ele[2].trim()]);
});
$('<div>', {id: 'mainpage-btns', html: btns.map(ele => ele.$element)}).insertAfter( $table );
});
});
//</nowiki>

2024年1月11日 (四) 21:01的最新版本

//<nowiki>
"use strict";
/* global OO, wgULS */
mw.loader.using( ['mediawiki.api', 'oojs-ui-windows', 'ext.gadget.site-lib'] ).then(() => {
	mw.messages.set( $.extend( wgULS({
		'gadget-ma-summary': '使用Mainpage小工具快速编辑', 'gadget-ma-niji': '虹学会', 'gadget-ma-preview': '预览',
		'gadget-ma-prompt': '请输入新的行号(从1开始计数)顺序,以空格分隔。未包括的行将被删除,不存在的空行将被创建。',
		'gadget-ma-error': '输入内容错误!只接受以空格分隔的正整数。', 'gadget-ma-topic': '首页/当年今日',
		'gadget-ma-protect': '保护页面失败!错误原因:$1', 'gadget-delete': '删除该行',
		'gadget-ma-hasunosora': '莲之空'
	}, {
		'gadget-ma-summary': '使用Mainpage小工具快速編輯', 'gadget-ma-niji': '虹學會', 'gadget-ma-preview': '預覽',
		'gadget-ma-prompt': '請輸入新的行號(從1開始計數)順序,以空格分隔。未包括的行將被刪除,不存在的空行將被創建。',
		'gadget-ma-error': '輸入內容錯誤!只接受以空格分割的正整數。', 'gadget-ma-topic': '首頁/當年今日',
		'gadget-ma-protect': '保護頁面失敗!錯誤原因:$1', 'gadget-delete': '刪除該行',
		'gadget-ma-hasunosora': '蓮之空'
	}), {'gadget-insertBefore': '在上方插入一行', 'gadget-insertAfter': '在下方插入一行'}) );
	let items = [], $table, $tag, table, wikitext, $row, trIndex;
	const menu = mw.menu([ {text: "µ's", data: 'muse'}, {text: 'Aqours', data: 'aqours'},
		{text: 'Saint Snow', data: 'saint snow'}, {text: mw.msg( 'gadget-ma-niji' ), data: 'nijigaku'},
		{text: 'Liella!', data: 'liella'}, {text: 'SIF/SIF2', data: 'sif'}, {text: 'AS', data: 'as'},
		{text: 'Musical', data: 'musical'}, {text: mw.msg( 'gadget-ma-hasunosora'), data: 'hasunosora'}, 
		{text: '幻日夜羽', data: 'yohane'}
	]),
		colors = { muse: '#E50080', aqours: '#32aaff', 'saint snow': '#cb3935', nijigaku: '#f39800', liella: '#a5469c',
		sif: '#77d2af', as: '#ff91af', musical: '#c40035', hasunosora: '#fb8a9b', yohane: '#19737d'
	},
		dict = {ll: 'muse', llss: 'aqours', llspst: 'liella'},
		id = mw.config.get( 'wgArticleId' ),
		pagename = mw.config.get( 'wgPageName' ),
		isMobile = mw.config.get( 'skin' ) == 'minerva',
		mode = pagename.startsWith( 'LLWiki:首页/当年今日/' ),
		writeText = () => {
		const result = items.map(ele =>
			'{{首页/标签|' + ele[0] + '}}' + (ele[2] ? ele[2].getValue() : ele[1] || '') + '\n').join(''),
			colorMod = mode ? ['rin', 'keke'].filter(ch => new RegExp('{{' + ch + '\/', 'i').test( result )) : [];
		return (mode ? '{{#widget:首页}}' : wikitext.slice(0, table.index)) +
			(colorMod.length ? '{{colorMod|' + colorMod.map(chara => chara + '=1').join('|') + '}}' : '') +
			'\n{|\n' + result + '|}' +
			(mode ? '' : wikitext.slice( table.index + table[0].length ));
	},
		width = $('#mw-content-text').width() * (isMobile ? 1 : 0.49) - (isMobile ? 68 : 36),
		$newTr = $('<tr>', {html: [$('<td>', {html: $('<span>', {class: 'mainpage-tag'})}), '<td>']}),
		api = new mw.Api(),
		btns = [ new OO.ui.ButtonWidget({label: '保存', flags: ['primary', 'progressive']}).on('click', () => {
		btns[0].setDisabled( true );
		mw.safeEdit(api, {title: pagename, text: writeText(), section: 0, summary: mw.msg('gadget-ma-summary')})
			.then(() => {
			(mw.config.get( 'wgRestrictionEdit' ) != 'sysop' || mw.config.get( 'wgRestrictionMove' ) != 'sysop' ?
				api.postWithEditToken({action: 'protect', title: pagename, protections: 'edit=sysop|move=sysop'}) :
				Promise.resolve()
			).then(() => { location.reload(); },
				reason => { mw.notify( mw.msg('gadget-ma-protect', reason), {type: 'error', autoHideSeconds: 'long'}); }
			);
		}, reason => { if (reason != 'editConflict') { btns[0].setDisabled( false ); } });
	}),
		new OO.ui.ButtonWidget({label: mw.msg( 'gadget-ma-preview' )}).on('click', () => {
		mw.timedParse(api, {text: writeText()}, mw.msg( 'gadget-ma-topic' )).then(data => {
			$table.html( $(data.parse.text).find( 'tbody' ) );
		}, () => {});
	}),
		new OO.ui.ButtonWidget({label: '更多'}).on('click', () => {
		mw.prompt( mw.msg( 'gadget-ma-prompt' ), 'progressive' ).then(result => {
			if (result === null) { return; }
			if (/[^\d\s]/.test( result )) {
				mw.notify( mw.msg( 'gadget-ma-error' ), {type: 'error', autoHideSeconds: 'long'} );
				return;
			}
			const order = result.split(' ').filter(i => i).map(i => i - 1),
				$tr = $table.find( 'tr' );
			items = order.map(i => items[i] || ['muse', '']);
			$table.children( 'tbody' ).html( order.map(i => $tr[i] || $newTr.clone()) );
		});
	}) ],
		insertRow = (flag) => {
		items.splice( trIndex + flag, 0, ['muse', '']);
		if ($row.length) { $newTr.clone()[`insert${ flag ? 'After' : 'Before' }`]( $row ); }
		else { $newTr.clone().appendTo( $table ); }
	},
		contextMenu = isMobile ? null : mw.menu([ {text: mw.msg( 'gadget-delete' ), click: () => {
		items.splice( trIndex, 1 );
		$row.remove();
	}}, {text: mw.msg( 'gadget-insertBefore' ), click: () => { insertRow(0); }},
		{text: mw.msg( 'gadget-insertAfter' ), click: () => { insertRow(1); }}
	], null, true),
		optDel = isMobile ? null : contextMenu.findFirstSelectableItem();
	if (id === 0) {
		$table = $('<table>', {html: '<tbody>'});
		$('.noarticletext').toggleClass( 'noarticletext mw-parser-output mainpage-flex' ).html( $table );
	} else { $table = $('.mw-parser-output table').first(); }
	menu.$element.appendTo( '.mw-parser-output' );
	mw.loader.addStyleTag( '.mainpage-flex td:last-child { width: calc(' + width + 'px - 1.2em); }' );
	menu.on('choose', option => {
		$tag.css('background-color', colors[ option.getData() ]);
		items[ $tag.closest( 'tr' ).index() ][0] = option.getData();
	});
	$table.on('dblclick', 'td:first-child', function() {
		const $this = $(this),
			name = items[ $this.parent().index() ][0].toLowerCase();
		$tag = $this.children( '.mainpage-tag' );
		menu.setFloatableContainer( $tag );
		menu.selectItemByData( dict[name] || name );
		menu.toggle( true );
	}).on('dblclick', 'td:last-child', function() {
		const $this = $(this),
			item = items[ $this.parent().index() ];
		item[2] = item[2] || new OO.ui.MultilineTextInputWidget({autosize: true, value: item[1]});
		$this.html( item[2].$element );
	}).on('contextmenu', function(e) {
		if (isMobile) { return; }
		e.preventDefault();
		$row = $(e.target).closest( 'tr' );
		trIndex = $row.index();
		optDel.setDisabled( $row.length === 0 );
		contextMenu.$element.css({left: e.pageX + 1, top: e.pageY + 1});
		contextMenu.toggle( true ); // 先设定位置再显示
	});
	(id ? mw.sectionQuery( api ) : Promise.resolve()).then(data => {
		wikitext = data ? data.parse.wikitext : '';
		table = wikitext.match( /\n?{\|\n[\s\S]+?\n\|}(?!})/ ) || [''];
		items = [...table[0].matchAll( /首[页頁]\/[标標][签籤]\|(.+?)}}(.+?)\n/g )]
			.map(ele => [ele[1].trim(), ele[2].trim()]);
	});
	$('<div>', {id: 'mainpage-btns', html: btns.map(ele => ele.$element)}).insertAfter( $table );
});
//</nowiki>