User:Inductiveload/Gadget-MobileFrontendFixer.js

From Wikisource
Jump to navigation Jump to search
Note: After saving, changes may not occur immediately. Click here to learn how to bypass your browser's cache.
  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Cmd-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (Cmd-Shift-R on a Mac)
  • Internet Explorer: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Clear the cache in Tools → Preferences

For details and instructions about other browsers, see Wikipedia:Bypass your cache.

/**
 * The Mobile Wikitext editor is still in beta, and the visual editor is
 * pretty much completely useless. Try to add some tools.
 */

( function ( $, mw ) {

	function MFWikitextFixer() {
		// eslint-disable-next-line no-jquery/no-global-selector
		this.$textbox = $( '#wikitext-editor' );
	}

	MFWikitextFixer.prototype.addTools = function () {
		var that = this;

		var toolFactory = new OO.ui.ToolFactory();
		var toolGroupFactory = new OO.ui.ToolGroupFactory();

		// Define the tool’s static properties and the action it performs
		function SignatureTool() {
			SignatureTool.super.apply( this, arguments );
		}
		OO.inheritClass( SignatureTool, OO.ui.Tool );
		SignatureTool.static.name = 'signature';
		SignatureTool.static.icon = 'signature';
		SignatureTool.static.title = 'Insert signature';
		// Define the action that is performed when this tool is selected (clicked).
		SignatureTool.prototype.onSelect = function () {
			that.$textbox.textSelection( 'replaceSelection', '~~' + '~~' );
			this.setActive( false );
		};
		SignatureTool.prototype.onUpdateState = function () {};

		toolFactory.register( SignatureTool );

		var toolList = [ 'signature' ];

		// allow users to add their own tools
		mw.hook( 'wikisource.mobilewikitext.addtools' )
			.fire( toolFactory, toolList, this.$textbox );

		var toolbar = new OO.ui.Toolbar( toolFactory, toolGroupFactory );

		// Add the MenuToolGroup to the toolbar.
		toolbar.setup( [
			{
				icon: 'code',
				type: 'bar',
				label: 'Tools',
				title: 'Extra tools',
				header: 'This is the header',
				include: toolList
			}
		] );

		// $( '.overlay-header' )
		// bizarre - clicks don't work if inserted into the overlay
		// eslint-disable-next-line no-jquery/no-global-selector
		$( '.overlay-header-container' )
			.prepend( $( '<div>' )
				.append( toolbar.$element )
			);

		toolbar.initialize();
	};

	mw.loader.using( [ 'jquery.textSelection', 'oojs-ui-widgets' ], function () {
		$( function () {
			mw.hook( 'mobileFrontend.editorOpened' ).add( function ( editor ) {
				if ( editor === 'wikitext' ) {
					var fixer = new MFWikitextFixer();
					fixer.addTools();
				}
			} );
		} );
	} );

// eslint-disable-next-line no-undef
}( jQuery, mediaWiki ) );