Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ui/actions/ve.ui.HelpCompletionAction.js

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface ve.ui.HelpCompletionAction class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * HelpCompletionAction action.
9 *
10 * Controls autocompletion of anything from the help panel
11 *
12 * @class
13 * @extends ve.ui.CompletionAction
14 * @constructor
15 * @param {ve.ui.Surface} surface Surface to act on
16 * @param {string} [source]
17 */
18 ve.ui.HelpCompletionAction = function ( surface ) {
19 // Parent constructor
20 ve.ui.HelpCompletionAction.super.apply( this, arguments );
21
22 this.toolbar = surface.target.getToolbar();
23 this.tools = this.toolbar.tools;
24 this.toolNames = Object.keys( this.tools ).filter( ( toolName ) => {
25 const tool = this.tools[ toolName ];
26 return tool &&
27 // No point in going in circles
28 !( tool instanceof ve.ui.HelpCompletionTool ) &&
29 // Ignore tool groups
30 !( tool instanceof OO.ui.ToolGroupTool ) &&
31 !( tool instanceof OO.ui.PopupTool );
32 } );
33 // Push the "format" group to the bottom because it's rarely-needed
34 this.toolNames.sort( ( a, b ) => {
35 const aGroup = this.tools[ a ].constructor.static.group;
36 const bGroup = this.tools[ b ].constructor.static.group;
37 if ( aGroup === bGroup ) {
38 // preserve order
39 return 0;
40 }
41 if ( aGroup === 'format' ) {
42 return 1;
43 }
44 if ( bGroup === 'format' ) {
45 return -1;
46 }
47 // preserve order
48 return 0;
49 } );
50 };
51
52 /* Inheritance */
53
54 OO.inheritClass( ve.ui.HelpCompletionAction, ve.ui.CompletionAction );
55
56 /* Static Properties */
57
58 ve.ui.HelpCompletionAction.static.name = 'HelpCompletion';
59
60 ve.ui.HelpCompletionAction.static.alwaysIncludeInput = false;
61
62 ve.ui.HelpCompletionAction.static.defaultLimit = 99;
63
64 /**
65 * Definitions of the groups that tools can fall into
66 *
67 * Tools in a group whose name doesn't appear here will be placed
68 * in "other" gruop.
69 * The `title` field is used to place a label above the tools
70 * in the widget.
71 * `mergeWith` references a different group to add these
72 * tools to.
73 * `weight` can be used to move groups up or down the list.
74 * Higher values will appear at the top. The default is 0.
75 */
76 ve.ui.HelpCompletionAction.static.toolGroups = {
77 textStyle: {
78 title: OO.ui.deferMsg( 'visualeditor-shortcuts-text-style' )
79 },
80 meta: {
81 mergeWith: 'insert'
82 },
83 object: {
84 mergeWith: 'insert'
85 },
86 format: {
87 title: OO.ui.deferMsg( 'visualeditor-shortcuts-formatting' ),
88 weight: -2
89 },
90 dialog: {
91 title: OO.ui.deferMsg( 'visualeditor-shortcuts-dialog' )
92 },
93 other: {
94 title: OO.ui.deferMsg( 'visualeditor-shortcuts-other' ),
95 weight: -1
96 },
97 history: {
98 title: OO.ui.deferMsg( 'visualeditor-shortcuts-history' ),
99 weight: -3
100 },
101 structure: {
102 title: OO.ui.deferMsg( 'visualeditor-toolbar-structure' )
103 },
104 insert: {
105 title: OO.ui.deferMsg( 'visualeditor-shortcuts-insert' ),
106 weight: 1
107 }
108 };
109
110 /* Methods */
111
112 ve.ui.HelpCompletionAction.prototype.open = function ( isolateInput ) {
113 if ( !isolateInput ) {
114 // Remove undo/redo when inputting in the surface, don't just
115 // show them as disabled (they are still available in the toolbar)
116 // TODO: One would need to completely ignore the history
117 // stack since before the action was triggered to use
118 // undo/redo from here. Might not be worth the effort.
119 this.toolNames = this.toolNames.filter( ( toolName ) => {
120 const tool = this.tools[ toolName ];
121 return !( tool instanceof ve.ui.HistoryTool );
122 } );
123 }
124
125 return ve.ui.HelpCompletionAction.super.prototype.open.apply( this, arguments );
126 };
127
128 ve.ui.HelpCompletionAction.prototype.getToolIndex = function ( toolName ) {
129 const tool = this.tools[ toolName ];
130 const toolGroups = this.constructor.static.toolGroups;
131 const group = this.getGroupForTool( tool );
132 return OO.ui.resolveMsg( toolGroups[ group ].title ) + ' ' + tool.getTitle();
133 };
134
135 ve.ui.HelpCompletionAction.prototype.getSuggestions = function ( input ) {
136 return ve.createDeferred().resolve( this.filterSuggestionsForInput(
137 this.toolNames,
138 input
139 ) );
140 };
141
142 ve.ui.HelpCompletionAction.prototype.compareSuggestionToInput = function ( suggestion, normalizedInput ) {
143 const normalizedSuggestion = this.getToolIndex( suggestion ).toLowerCase();
144
145 // Allow character skipping in input, so for example "head2" matches "heading 2" and
146 // "blist" matches "bullet list"
147 let matchedIndex = 0;
148 for ( let i = 0, l = normalizedInput.length; i < l; i++ ) {
149 matchedIndex = normalizedSuggestion.indexOf( normalizedInput[ i ], matchedIndex );
150 if ( matchedIndex === -1 ) {
151 return {
152 isMatch: false,
153 isExact: false
154 };
155 }
156 matchedIndex++;
157 }
158
159 return {
160 isMatch: true,
161 // isExact is only used when 'alwaysIncludeInput' is set
162 isExact: false
163 };
164 };
165
166 ve.ui.HelpCompletionAction.prototype.getMenuItemForSuggestion = function ( toolName ) {
167 const tool = this.tools[ toolName ];
168 return new OO.ui.MenuOptionWidget( {
169 data: tool,
170 label: tool.getTitle(),
171 // HACK: an invalid icon name will render as a spacer for alignment
172 icon: tool.getIcon() || tool.constructor.static.fallbackIcon || '_',
173 disabled: tool.isDisabled()
174 } );
175 };
176
177 /**
178 * Get the group associated with a tool, resolving any mergeWith redirects
179 *
180 * @param {ve.ui.Tool} tool Tool
181 * @return {string} Group name
182 */
183 ve.ui.HelpCompletionAction.prototype.getGroupForTool = function ( tool ) {
184 const toolGroups = this.constructor.static.toolGroups;
185 let group = tool.constructor.static.group;
186 if ( toolGroups[ group ] ) {
187 if ( toolGroups[ group ].mergeWith ) {
188 group = toolGroups[ group ].mergeWith;
189 }
190 } else {
191 group = 'other';
192 }
193 return group;
194 };
195
196 ve.ui.HelpCompletionAction.prototype.updateMenuItems = function ( menuItems ) {
197 const menuItemsByGroup = {};
198 const toolGroups = this.constructor.static.toolGroups;
199 menuItems.forEach( ( menuItem ) => {
200 const tool = menuItem.getData();
201 const group = this.getGroupForTool( tool );
202 menuItemsByGroup[ group ] = menuItemsByGroup[ group ] || [];
203 menuItemsByGroup[ group ].push( menuItem );
204 } );
205 const newMenuItems = [];
206 const groups = Object.keys( menuItemsByGroup );
207 groups.sort( ( a, b ) => {
208 const weightA = toolGroups[ a ].weight || 0;
209 const weightB = toolGroups[ b ].weight || 0;
210 return weightB - weightA;
211 } );
212 groups.forEach( ( group ) => {
213 newMenuItems.push(
214 new OO.ui.MenuSectionOptionWidget( {
215 label: toolGroups[ group ].title
216 } )
217 );
218 ve.batchPush( newMenuItems, menuItemsByGroup[ group ] );
219 } );
220 return newMenuItems;
221 };
222
223 ve.ui.HelpCompletionAction.prototype.chooseItem = function ( item, range ) {
224 // We're completely ignoring the idea that we should be "inserting" anything...
225 // Instead, we run the command that was chosen.
226
227 const fragment = this.surface.getModel().getLinearFragment( range, true );
228 fragment.removeContent();
229 fragment.collapseToEnd();
230
231 const tool = item.getData();
232 // Wait for completion widget to close, as the selected tool may
233 // trigger another completion widget.
234 setTimeout( () => {
235 tool.onSelect();
236 } );
237
238 return true;
239 };
240
241 /* Registration */
242
243 ve.ui.actionFactory.register( ve.ui.HelpCompletionAction );
244
245 ve.ui.commandRegistry.register( new ve.ui.Command(
246 'openHelpCompletions', ve.ui.HelpCompletionAction.static.name, 'open',
247 { supportedSelections: [ 'linear' ] }
248 ) );
249 ve.ui.commandRegistry.register( new ve.ui.Command(
250 'openHelpCompletionsTrigger', ve.ui.HelpCompletionAction.static.name, 'open',
251 { supportedSelections: [ 'linear', 'table' ], args: [ true ] }
252 ) );
253
254 ve.ui.sequenceRegistry.register( new ve.ui.Sequence( 'autocompleteHelpCommands', 'openHelpCompletions', '\\', 0 ) );
255
256 ve.ui.triggerRegistry.register(
257 'openHelpCompletionsTrigger', {
258 // Firefox already uses [ctrl/cmd]+shift+p
259 mac: [
260 new ve.ui.Trigger( 'cmd+shift+p' ),
261 new ve.ui.Trigger( 'cmd+alt+shift+p' )
262 ],
263 pc: [
264 new ve.ui.Trigger( 'ctrl+shift+p' ),
265 new ve.ui.Trigger( 'ctrl+alt+shift+p' )
266 ]
267 }
268 );
269
270 ve.ui.commandHelpRegistry.register( 'other', 'openHelpCompletions', {
271 trigger: 'openHelpCompletionsTrigger',
272 sequences: [ 'autocompleteHelpCommands' ],
273 label: OO.ui.deferMsg( 'visualeditor-toolbar-search-help-label' )
274 } );