All files / src/ui/dialogs ve.ui.CommandHelpDialog.js

15.21% Statements 14/92
0% Branches 0/36
0% Functions 0/15
15.21% Lines 14/92

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277                              1x             1x       1x   1x   1x     1x             1x                                                           1x                     1x             1x                                   1x                                                                                                                                                                                                                           1x                                         1x                                                                           1x  
/*!
 * VisualEditor UserInterface CommandHelpDialog class.
 *
 * @copyright See AUTHORS.txt
 */
 
/**
 * Dialog for listing all command keyboard shortcuts.
 *
 * @class
 * @extends OO.ui.ProcessDialog
 *
 * @constructor
 * @param {Object} [config] Configuration options
 */
ve.ui.CommandHelpDialog = function VeUiCommandHelpDialog( config ) {
	// Parent constructor
	ve.ui.CommandHelpDialog.super.call( this, config );
};
 
/* Inheritance */
 
OO.inheritClass( ve.ui.CommandHelpDialog, OO.ui.ProcessDialog );
 
/* Static Properties */
 
ve.ui.CommandHelpDialog.static.name = 'commandHelp';
 
ve.ui.CommandHelpDialog.static.size = 'larger';
 
ve.ui.CommandHelpDialog.static.title =
	OO.ui.deferMsg( 'visualeditor-dialog-command-help-title' );
 
ve.ui.CommandHelpDialog.static.actions = [
	{
		label: OO.ui.deferMsg( 'visualeditor-dialog-action-done' ),
		flags: [ 'safe', 'close' ]
	}
];
 
ve.ui.CommandHelpDialog.static.commandGroups = {
	textStyle: {
		title: OO.ui.deferMsg( 'visualeditor-shortcuts-text-style' ),
		promote: [ 'bold', 'italic', 'link' ],
		demote: [ 'clear' ]
	},
	clipboard: {
		title: OO.ui.deferMsg( 'visualeditor-shortcuts-clipboard' )
	},
	formatting: {
		title: OO.ui.deferMsg( 'visualeditor-shortcuts-formatting' ),
		promote: [ 'paragraph', 'pre', 'blockquote' ]
	},
	history: {
		title: OO.ui.deferMsg( 'visualeditor-shortcuts-history' ),
		promote: [ 'undo', 'redo' ]
	},
	dialog: {
		title: OO.ui.deferMsg( 'visualeditor-shortcuts-dialog' )
	},
	other: {
		title: OO.ui.deferMsg( 'visualeditor-shortcuts-other' ),
		promote: [ 'findAndReplace', 'findNext', 'findPrevious' ],
		demote: [ 'commandHelp' ]
	},
	insert: {
		title: OO.ui.deferMsg( 'visualeditor-shortcuts-insert' )
	}
};
 
ve.ui.CommandHelpDialog.static.commandGroupsOrder = [
	'textStyle', 'clipboard',
	'formatting', 'history',
	'dialog', 'other', 'insert'
];
 
/* Methods */
 
/**
 * @inheritdoc
 */
ve.ui.CommandHelpDialog.prototype.getBodyHeight = function () {
	return Math.round( this.contentLayout.$element[ 0 ].scrollHeight );
};
 
/**
 * @inheritdoc
 */
ve.ui.CommandHelpDialog.prototype.initialize = function () {
	// Parent method
	ve.ui.CommandHelpDialog.super.prototype.initialize.call( this );
 
	this.contentLayout = new OO.ui.PanelLayout( {
		scrollable: true,
		padded: true,
		expanded: false
	} );
	this.$container = $( '<div>' ).addClass( 've-ui-commandHelpDialog-container' );
 
	this.contentLayout.$element.append( this.$container );
	this.$body.append( this.contentLayout.$element );
};
 
/**
 * @inheritdoc
 */
ve.ui.CommandHelpDialog.prototype.getSetupProcess = function ( data ) {
	return ve.ui.CommandHelpDialog.super.prototype.getSetupProcess.call( this, data )
		.next( function () {
			var dialog = this,
				surface = data.surface,
				target = surface.getTarget(),
				sequenceRegistry = surface.sequenceRegistry,
				commandRegistry = surface.commandRegistry,
				availableCommands = surface.getCommands()
					.concat( target.constructor.static.documentCommands )
					.concat( target.constructor.static.targetCommands ),
				commandGroups = this.constructor.static.commandGroups,
				commandGroupsOrder = this.constructor.static.commandGroupsOrder;
 
			this.$container.empty();
 
			commandGroupsOrder.forEach( function ( groupName ) {
				var hasCommand = false;
				var commandGroup = commandGroups[ groupName ];
				var commands = dialog.constructor.static.sortedCommandsFromGroup( groupName, commandGroup.promote, commandGroup.demote );
				var $list = $( '<dl>' ).addClass( 've-ui-commandHelpDialog-list' );
				commands.forEach( function ( command ) {
					var triggerList;
					if ( command.trigger ) {
						if (
							!command.ignoreCommand && (
								availableCommands.indexOf( command.trigger ) === -1 ||
								!commandRegistry.lookup( command.trigger )
							)
						) {
							// Trigger is specified by unavailable command
							return;
						}
						triggerList = ve.ui.triggerRegistry.lookup( command.trigger );
					} else {
						triggerList = [];
						if ( command.shortcuts ) {
							if (
								command.checkCommand && (
									availableCommands.indexOf( command.checkCommand ) === -1 ||
									!commandRegistry.lookup( command.checkCommand )
								)
							) {
								// 'checkCommand' is not available
								return;
							}
							triggerList = command.shortcuts.map( function ( shortcut ) {
								return new ve.ui.Trigger( shortcut, true );
							} );
						}
					}
 
					var hasShortcut = false;
 
					var $shortcut = $( '<dt>' );
					triggerList.forEach( function ( trigger ) {
						// Append an array of jQuery collections from buildKeyNode
						// eslint-disable-next-line no-jquery/no-append-html
						$shortcut.append( $( '<kbd>' ).addClass( 've-ui-commandHelpDialog-shortcut' ).append(
							trigger.getMessage( true ).map( dialog.constructor.static.buildKeyNode )
						).find( 'kbd + kbd' ).before( '+' ).end() );
						hasShortcut = true;
					} );
					if ( command.sequences ) {
						command.sequences.forEach( function ( sequenceName ) {
							var sequence = sequenceRegistry.lookup( sequenceName );
							if ( sequence ) {
								// Append an array of jQuery collections from buildKeyNode
								// eslint-disable-next-line no-jquery/no-append-html
								$shortcut.append( $( '<kbd>' ).addClass( 've-ui-commandHelpDialog-sequence' )
									.attr( 'data-label', ve.msg( 'visualeditor-shortcuts-sequence-notice' ) )
									.append(
										sequence.getMessage( true ).map( dialog.constructor.static.buildKeyNode )
									)
								);
								hasShortcut = true;
							}
						} );
					}
					if ( hasShortcut ) {
						$list.append(
							$shortcut,
							$( '<dd>' ).text( OO.ui.resolveMsg( command.label ) )
						);
						hasCommand = true;
					}
				} );
				if ( hasCommand ) {
					dialog.$container.append(
						$( '<div>' )
							.addClass( 've-ui-commandHelpDialog-section' )
							.append(
								$( '<h3>' ).text( OO.ui.resolveMsg( commandGroup.title ) ),
								$list
							)
					);
				}
			} );
		}, this );
};
 
/* Static methods */
 
/**
 * Wrap a key (as provided by a Trigger) in a node, for display
 *
 * @static
 * @param {string} key Key to display
 * @return {jQuery} A kbd wrapping the key text
 */
ve.ui.CommandHelpDialog.static.buildKeyNode = function ( key ) {
	var $key = $( '<kbd>' );
	if ( key === ' ' ) {
		// Might need to expand this if other keys show up, but currently things like
		// the tab-character only come from Triggers and are pre-localized there into
		// "tab" anyway.
		key = ( new ve.ui.Trigger( 'space' ) ).getMessage();
		$key.addClass( 've-ui-commandHelpDialog-specialKey' );
	}
	return $key.text( key );
};
 
/**
 * Extract a properly sorted list of commands from a command-group
 *
 * @static
 * @param {string} groupName The dialog-category in which to display this
 * @param {string[]} [promote] Commands which should be displayed first
 * @param {string[]} [demote] Commands which should be displayed last
 * @return {Object[]} List of commands in order
 */
ve.ui.CommandHelpDialog.static.sortedCommandsFromGroup = function ( groupName, promote, demote ) {
	var commands = ve.ui.commandHelpRegistry.lookupByGroup( groupName ),
		keys = Object.keys( commands ),
		used = {},
		auto = [],
		promoted = [],
		demoted = [];
	keys.sort();
 
	if ( promote ) {
		promote.forEach( function ( name ) {
			if ( !commands[ name ] ) {
				return;
			}
			promoted.push( commands[ name ] );
			used[ name ] = true;
		} );
	}
	if ( demote ) {
		demote.forEach( function ( name ) {
			if ( used[ name ] || !commands[ name ] ) {
				return;
			}
			demoted.push( commands[ name ] );
			used[ name ] = true;
		} );
	}
	keys.forEach( function ( name ) {
		if ( used[ name ] ) {
			return;
		}
		auto.push( commands[ name ] );
	} );
	return promoted.concat( auto, demoted );
};
 
/* Registration */
 
ve.ui.windowFactory.register( ve.ui.CommandHelpDialog );