MediaWiki:InsertWikiEditorButton.js

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

  • Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5Ctrl-R(Mac为⌘-R
  • Google Chrome:Ctrl-Shift-R(Mac为⌘-Shift-R
  • Internet Explorer或Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5
  • Opera:Ctrl-F5
/**
 * Insert WikiEditor Button
 * @version 2015-11-12
 * @source https://meta.wikimedia.org/wiki/User:Krinkle/Scripts/InsertWikiEditorButton
 * @author Krinkle
 * @author Locos epraix
 * @license Released in the public domain
 */
/*jshint browser: true */
/*global jQuery, mediaWiki */
(function ($, mw) {
	"use strict";

	var $toolbar, ready = false, queue = [];

	function insertButton(btnObj) {
		$toolbar.wikiEditor('addToToolbar', btnObj);
	}

	function handleQueue() {
		ready = true;
		for (var i = 0; i < queue.length; i++) {
			insertButton(queue[i]);
		}
		queue = handleQueue = null;
	}

	// Only on editpage
	if ($.inArray(mw.config.get('wgAction'), ['edit', 'submit', 'formedit']) !== -1) {
		/**
		 * krInsertWikiEditorButton
		 *
		 * @param options {Object} An object with options:
		 * - section {String} (optional) The name of the section in the WikiEditor. Defaults to 'main'
		 * - group {String} (optional) The name of the group in the WikiEditor. Defaults to 'insert'
		 * - id {String} (required) Unique id (ie. 'my-button')
		 * - icon {String} (recommended) URL to the icon, should be square about 21 to 22px
		 * - label {String} (required) Tooltip displayed when hovering button
		 * - insertBefore {String} (optional) Wikitext to be inserted before the cursor on-click
		 * - sampleText {String} (optional) Text inserted in place of the cursor if no text was selected
		 * - insertAfter {String} (optional) Wikitext to be inserted after the cursor on-click
		 * - callback {Function} (optional) Called when the button is clicked
		 * - autoSummary {mixed} (optional) Null or an Object with the following properties:
		 *   - summary {String} (required) Edit summary that should be used
		 *   - position {String} (optional) 'append', 'prepend' or 'replace'
		 *   - delimiter {String} (optional) delimiter between the (possibly) current summary and the to-be-inserted summary
		 */
		window.krInsertWikiEditorButton = function (options) {
			// Defaults
			options = $.extend({
				'section': 'main',
				'group': 'insert',
				'id': null,
				'icon': '//upload.wikimedia.org/wikipedia/commons/thumb/f/f0/Toolbaricon_bold_!.png/21px-Toolbaricon_bold_!.png',
				'label': '',
				'insertBefore': '',
				'sampleText': '',
				'insertAfter': '',
				'callback': null,
				'autoSummary': {
					'summary': null,
					'position': 'append',
					'delimiter': '; '
				}
			}, options);
			// Required
			if (!options.id || !options.label) {
				return false;
			}
			var btnObj = {
				'section': options.section,
				'group': options.group,
				'tools': {}
			};
			btnObj.tools[options.id] = {
				label: options.label,
				type: 'button',
				icon: options.icon,
				action: {
					type: 'callback',
					execute: function () {
						// encapsulateSelection
						$toolbar.textSelection('encapsulateSelection', {
							pre: options.insertBefore,
							peri: options.sampleText,
							post: options.insertAfter
						});
						// Auto summary
						if (options.autoSummary && options.autoSummary.summary) {
							var $summary = $('#wpSummary'), currentSum = $summary.val();
							if (!$.trim(currentSum)) {
								$summary.val(options.autoSummary.summary);
							} else {
								switch (options.autoSummary.position) {
								case 'prepend':
									$summary.val(
										options.autoSummary.summary +
											options.autoSummary.delimiter +
											currentSum
									);
									break;
								case 'replace':
									$summary.val(options.autoSummary.summary);
									break;
								default: // 'append'
									$summary.val(
										currentSum +
											options.autoSummary.delimiter +
											options.autoSummary.summary
									);
								}
							}
						}
						// Callback
						if ($.isFunction(options.callback)) {
							options.callback();
						}
					}
				}
			};
			if (ready) {
				insertButton(btnObj);
			} else {
				queue.push(btnObj);
			}
		};
		// Wait for WikiEditor to be ready
		$(function () {
			$toolbar = $('#wpTextbox1');
			if ($.fn.wikiEditor) { handleQueue(); } else { $toolbar.on('wikiEditor-toolbar-doneInitialSections', handleQueue); }
		});
	} else {
		// No-op function to avoid errors on other pages
		window.krInsertWikiEditorButton = function () {};
	}
	// Load callback
	if (window.kCustomMainInsertButton_config) {
		kCustomMainInsertButton_config();
	}
}(jQuery, mediaWiki));