跳转到内容

MediaWiki:Gadget-PreviewWithVariant.js

维基百科,自由的百科全书
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
/**
 * @file Add a "Preview with variant" option to the edit form.
 * @author [[zh:User:Diskdance]]
 * @author [[zh:User:Lt2818]]
 * @license MIT
 */
// <nowiki>
( function () {
	var conv = require( 'ext.gadget.HanAssist' ).conv;
	var initialized = false;
	// This is initialized by ext.WikiEditor.realtimepreview.enable hook below
	// which is always called when Realtime Preview is enabled
	var realtimePreviewClass;
	mw.hook( 'ext.WikiEditor.realtimepreview.enable' ).add( function ( realtimePreview ) {
		if ( !realtimePreviewClass ) {
			realtimePreviewClass = realtimePreview;
		}
	} );

	mw.hook( 'wikipage.editform' ).add( function ( $editForm ) {
		if ( initialized ) {
			return;
		}

		var $templateSandboxPreview = $editForm.find( 'input[name="wpTemplateSandboxPreview"]' );
		// It is possible that a user want to preview a page with a non-wikitext module
		// Do not return in this case
		if (
			mw.config.get( 'wgPageContentModel' ) !== 'wikitext' &&
			!$templateSandboxPreview.length
		) {
			return;
		}

		var $layout = $editForm.find( '.editCheckboxes .oo-ui-horizontalLayout' );
		if ( !$layout.length ) {
			return;
		}
		initialized = true;

		var VARIANTS = [
			{ data: 'zh', label: conv( { hans: '不转换', hant: '不轉換' } ) },
			{ data: 'zh-hans', label: '简体' },
			{ data: 'zh-hant', label: '繁體' },
			{ data: 'zh-cn', label: '大陆简体' },
			{ data: 'zh-hk', label: '香港繁體' },
			{ data: 'zh-mo', label: '澳門繁體' },
			{ data: 'zh-my', label: '大马简体' },
			{ data: 'zh-sg', label: '新加坡简体' },
			{ data: 'zh-tw', label: '臺灣正體' }
		];
		var uriVariant = new URL( window.location.href ).searchParams.get( 'variant' );

		/**
		 * @param {string|null} variant null for default variant
		 */
		function setVariant( variant ) {
			// Normal Preview
			var originalAction = $editForm.attr( 'action' );
			if ( originalAction ) {
				var url = new URL( originalAction, window.location.href );
				if ( variant === null ) {
					url.searchParams.delete( 'variant' );
				} else {
					url.searchParams.set( 'variant', variant );
				}
				$editForm.attr( 'action', url.href );
			}
			// Live Preview
			mw.config.set( 'wgUserVariant', variant || uriVariant || mw.user.options.get( 'variant' ) );
			// Realtime Preview
			if ( realtimePreviewClass ) {
				realtimePreviewClass.doRealtimePreview( true );
			}
		}

		var checkbox = new OO.ui.CheckboxInputWidget( {
			selected: !!uriVariant
		} );
		var dropdown = new OO.ui.DropdownInputWidget( {
			$overlay: true,
			disabled: !checkbox.isSelected(),
			options: VARIANTS,
			value: mw.config.get( 'wgUserVariant' ) || uriVariant || mw.user.options.get( 'variant' )
		} );

		checkbox.on( 'change', function ( selected ) {
			dropdown.setDisabled( !selected );
			setVariant( selected ? dropdown.getValue() : null );
		} );
		dropdown.on( 'change', setVariant );

		var checkboxField = new OO.ui.FieldLayout( checkbox, {
			align: 'inline',
			label: conv( { hans: '预览字词转换', hant: '預覽字詞轉換' } )
		} );
		var dropdownField = new OO.ui.FieldLayout( dropdown, {
			align: 'top',
			label: conv( { hans: '使用该语言变体显示预览:', hant: '使用該語言變體顯示預覽:' } ),
			invisibleLabel: true
		} );
		$layout.append( checkboxField.$element, dropdownField.$element );
	} );

	// Register 2017 wikitext editor version to VE
	mw.loader.using( 'ext.visualEditor.desktopArticleTarget.init' ).then( function () {
		mw.libs.ve.addPlugin( 'ext.gadget.PreviewWithVariant2017' );
	} );
}() );