Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ui/dialogs/ve.ui.CommandHelpDialog.js

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