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

MediaWiki:Gadget-template-ve.js

来自LLWiki
跳转到导航 跳转到搜索

注意:在保存之后,您可能需要清除浏览器缓存才能看到所作出的变更的影响。

  • Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5Ctrl-R(Mac为⌘-R
  • Google Chrome:Ctrl-Shift-R(Mac为⌘-Shift-R
  • Internet Explorer:按住Ctrl的同时单击刷新,或按Ctrl-F5
  • Opera:前往菜单 → 设置(Mac为Opera → Preferences),然后隐私和安全 → 清除浏览数据 → 缓存的图片和文件
//<nowiki>
// 由ResourceLoader直接调用,不可使用ES6语法
/**
 * @Function: 模板快速編輯
 * @Dependencies: mediawiki.api, oojs-ui-core, ext.gadget.CodeMirror, ext.gadget.site-lib
 * @Author: [[User:Bhsd]]
 */
"use strict";
/* global OO, CodeMirror */
$(function() {
	const $content = $('#mw-content-text'),
		$original = $content.find( '.wiki-highlight' ),
		cid = mw.config.get( 'wgCurRevisionId' );
	if (mw.config.get( 'wgNamespaceNumber' ) != 10 || mw.config.get('wgAction') != 'view' || $original.length === 0 || mw.config.get( 'wgRevisionId' ) < cid) { return; }
	var editor, before, after, text;
	const title = mw.config.get( 'wgPageName' ),
		variant = mw.config.get( 'wgUserVariant' ),
		$compare = $(document.createElement( $original.prop( 'tagName' ) )).addClass( 'template-ve mw-ajax-loader' ).insertAfter( $original ),
		api = new mw.Api(),
		regex = /(class\s*=\s*["']wiki-highlight["'].*?>(?:<nowiki>)?\s*(?:-->|<\/noinclude>))([\s\S]+?)((?:<!--|<noinclude>)(?:<\/nowiki>)?<\/(?:pre|code)>(?:<includeonly>--><\/includeonly>|<\/noinclude>))$/,
		btns = [new OO.ui.ButtonWidget({label: '提交', flags: ['primary', 'progressive']}).on('click', function() {
		btns[0].setDisabled( true );
		mw.safeEdit(api, {title: title, text: before + editor.getValue() + after, summary: '使用CodeMirror快速编辑'}).then(function() { location.reload(); }, function() {});
	}), new OO.ui.ButtonWidget({label: '预览'}).on('click', function() {
		btns[1].setDisabled( true );
		const newtext = before + editor.getValue() + after;
		mw.timedParse(api, {text: newtext, title: title, disablelimitreport: 1, disableeditsection: 1, prop: 'text', templatesandboxtitle: title, templatesandboxtext: newtext, variant: variant}).then(function(res) {
			$compare.detach();
			$content.children( '.mw-parser-output' ).replaceWith( res.parse.text );
			mw.hook( 'wikipage.content' ).fire( $content );
			$compare.insertAfter( '.wiki-highlight' );
		}, function() {}).then(function() { btns[1].setDisabled( false ); });
	}), new OO.ui.ButtonWidget({label: '还原', flags: 'destructive'}).on('click', function() { editor.setValue( text ); }) ];
	Promise.all([mw.standardQuery( api ), mw.loader.using( 'ext.gadget.site-lib.codemirror' ).then(function() {
		return CodeMirror.download( 'wiki' );
	})]).then(function(data) {
		console.log('Hook: codemirror.config, 开始添加模板快速编辑器');
		const wikitext = data[0].query.pages[0].revisions[0].content,
			match = regex.exec( wikitext );
		before = wikitext.slice(0, match.index) + match[1];
		after = match[3];
		text = match[2];
		editor = new CodeMirror($compare.removeClass( 'mw-ajax-loader' )[0], {value: text, mode: 'text/mediawiki', scrollbarStyle: 'null', lineWrapping: true, mwConfig: mw.config.get( 'extCodeMirrorConfig' )});
		$('<div>', {id: 've-btns', html: btns.map(function(ele) { return ele.$element; })}).appendTo( $compare );
	}, function() {});
});
//</nowiki>