User talk:Krinkle/Scripts/InsertWikiEditorButton.js

From Meta, a Wikimedia project coordination wiki

New version[edit]

Hi Krinkle. Here I have a couple of improvements for your code.

  • Use mw.user.options.get( 'usebetatoolbar' ) insted of $.wikiEditor, this is the only change I'm not sure what method is better
  • Delete 'preview' in wgAction comparison
  • Group added to options!
  • Ownline option
Code
/**
 * Insert WikiEditor Button
 * @url meta.wikimedia.org/wiki/User:Krinkle/Scripts/InsertWikiEditorButton
 * @author Krinkle
 * @version 0.2.2 (2012-03-05)
 * Released in the public domain
 */
/*jslint browser: true*/
/*jshint browser: true*/
/*global jQuery, mw*/
jQuery(document).ready(function ($) {
	"use strict";

	// Only on editpage with WikiEditor
	if ( mw.user.options.get( 'usebetatoolbar' ) && $.inArray(mw.config.get( 'wgAction' ), ['edit', 'submit']) !== -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
		 * - ownline {Boolean} (optional) Specifies if the inserted text go in it's own line. Defaults to 'false'
		 * - 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_%21.png/21px-Toolbaricon_bold_%21.png',
				'label': '',
				'insertBefore': '',
				'sampleText': '',
				'insertAfter': '',
				'ownline': false,
				'callback': null,
				'autoSummary': {
					'summary': null,
					'position': 'append',
					'delimiter': '; '
				}
			}, options);
			// Required
			if (!options.id || !options.label) {
				return false;
			}
			var wikiOptions = {
				'section': options.section,
				'group': options.group,
				'tools': {}
			};
			wikiOptions.tools[options.id] = {
				label: options.label,
				type: 'button',
				icon: options.icon,
				action: {
					type: 'callback',
					execute: function () {
						// encapsulateSelection
						$('#wpTextbox1').textSelection('encapsulateSelection', {
							pre: options.insertBefore,
							peri: options.sampleText,
							post: options.insertAfter,
							ownline: options.ownline
						});
						// Auto summary
						if (options.autoSummary && options.autoSummary.summary) {
							var $summary = $('#wpSummary'), currentSum = $summary.val();
							if ($.isEmpty(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();
						}
					}
				}
			};
			$('#wpTextbox1').wikiEditor('addToToolbar', wikiOptions);
		};
	} else {
		window.krInsertWikiEditorButton = function () { };
	}
});

--Locos epraix 02:16, 7 March 2012 (UTC)[reply]

Thanks! I've addressed the last 2 points in version 0.2.3 (diff). The WikiEditor-check I'm not sure yet, I'll check that out later. –Krinkletalk 02:28, 7 March 2012 (UTC)[reply]

I forgot about the ownline option. Sorry. --Locos epraix 02:44, 7 March 2012 (UTC)[reply]