Debian Codegraph @

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

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface CompletionAction class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Completion action.
9 *
10 * This is a very specialized action, which mostly exists to be a data-provider to a
11 * completion widget. Completion-types *must* override getSuggestions.
12 *
13 * @class
14 * @abstract
15 * @extends ve.ui.Action
16 *
17 * @constructor
18 * @param {ve.ui.Surface} surface Surface to act on
19 * @param {string} [source]
20 */
21 ve.ui.CompletionAction = function VeUiCompletionAction() {
22 // Parent constructor
23 ve.ui.CompletionAction.super.apply( this, arguments );
24 };
25
26 /* Inheritance */
27
28 OO.inheritClass( ve.ui.CompletionAction, ve.ui.Action );
29
30 /* Static Properties */
31
32 /**
33 * @property {number} Length to which to limit the list of returned completions
34 *
35 * @static
36 */
37 ve.ui.CompletionAction.static.defaultLimit = 8;
38
39 /**
40 * @property {number} Length of the trigger sequence for the action
41 *
42 * This many characters will be stripped from the start of the current input by
43 * CompletionWidget when triggered by a sequence, see #getSequenceLength.
44 *
45 * @static
46 */
47 ve.ui.CompletionAction.static.sequenceLength = 1;
48
49 /**
50 * @property {boolean} Whether the current input should be included as a completion automatically
51 *
52 * @static
53 */
54 ve.ui.CompletionAction.static.alwaysIncludeInput = true;
55
56 ve.ui.CompletionAction.static.methods = [ 'open' ];
57
58 /* Methods */
59
60 /**
61 * Show the completions
62 *
63 * @param {boolean} [isolateInput] Isolate input from the surface
64 * @return {boolean} Action was executed
65 */
66 ve.ui.CompletionAction.prototype.open = function ( isolateInput ) {
67 this.surface.completion.setup( this, isolateInput );
68
69 return true;
70 };
71
72 /**
73 * Retrieve suggested completions for the given input
74 *
75 * @abstract
76 * @param {string} input
77 * @param {number} [limit=20] Maximum number of results
78 * @return {jQuery.Promise} Promise that resolves with list of suggestions.
79 * Suggestions are converted to menu itmes by getMenuItemForSuggestion.
80 */
81 ve.ui.CompletionAction.prototype.getSuggestions = null;
82
83 /**
84 * Get a label to show as the menu header
85 *
86 * This is called twice per input, once with the new user input
87 * immediately after it is entered, and again later with the
88 * same input and its resolved suggestions
89 *
90 * @param {string} input User input
91 * @param {Array} [suggestions] Returned suggestions
92 * @return {jQuery|string|OO.ui.HtmlSnippet|Function|null|undefined} Label. Use undefined
93 * to avoid updating the label, and null to clear it.
94 */
95 ve.ui.CompletionAction.prototype.getHeaderLabel = function () {
96 return null;
97 };
98
99 /**
100 * Choose a specific item
101 *
102 * @param {OO.ui.MenuOptionWidget} item Chosen item
103 * @param {ve.Range} range Current surface range
104 */
105 ve.ui.CompletionAction.prototype.chooseItem = function ( item, range ) {
106 const fragment = this.insertCompletion( item.getData(), range );
107 fragment.collapseToEnd().select();
108 };
109
110 /**
111 * Perform the insetion for the chosen suggestion
112 *
113 * @param {Object} data Whatever data was attached to the menu option widget
114 * @param {ve.Range} range The range the widget is considering
115 * @return {ve.dm.SurfaceFragment} The fragment containing the inserted content
116 */
117 ve.ui.CompletionAction.prototype.insertCompletion = function ( data, range ) {
118 return this.surface.getModel().getLinearFragment( range )
119 .insertContent( data, true );
120 };
121
122 /**
123 * Should the widget abandon trying to find matches given the current state?
124 *
125 * @param {string} input
126 * @param {number} matches Number of matches before the input occurred
127 * @return {boolean} Whether to abandon
128 */
129 ve.ui.CompletionAction.prototype.shouldAbandon = function ( input, matches ) {
130 // Abandon after adding whitespace if there are no active potential matches,
131 // or if whitespace has been the sole input
132 return /^\s+$/.test( input ) ||
133 ( matches === 0 && /\s$/.test( input ) );
134 };
135
136 /**
137 * Get the length of the sequence which triggered this action
138 *
139 * @return {number} Length of the sequence
140 */
141 ve.ui.CompletionAction.prototype.getSequenceLength = function () {
142 return this.source === 'sequence' ? this.constructor.static.sequenceLength : 0;
143 };
144
145 // helpers
146
147 /**
148 * Make a menu item for a given suggestion
149 *
150 * @protected
151 * @param {any} suggestion Suggestion data, string by default
152 * @return {OO.ui.MenuOptionWidget}
153 */
154 ve.ui.CompletionAction.prototype.getMenuItemForSuggestion = function ( suggestion ) {
155 return new OO.ui.MenuOptionWidget( { data: suggestion, label: suggestion } );
156 };
157
158 /**
159 * Update the menu item list before adding, e.g. to add menu groups
160 *
161 * @protected
162 * @param {OO.ui.MenuOptionWidget[]} menuItems
163 * @return {OO.ui.MenuOptionWidget[]}
164 */
165 ve.ui.CompletionAction.prototype.updateMenuItems = function ( menuItems ) {
166 return menuItems;
167 };
168
169 /**
170 * Filter a suggestion list based on the current input
171 *
172 * This is for an implementor who has fetched/gathered a list of potential
173 * suggestions and needs to trim them down to a viable set to display as
174 * completion options for a given input.
175 *
176 * It restricts the selection to only suggestions that start with the input,
177 * shortens the list to the configured defaultLimit, and adds the current
178 * input to the list if alwaysIncludeInput and there wasn't an exact match.
179 *
180 * @protected
181 * @param {any[]} suggestions List of valid completions, strings by default
182 * @param {string} input Input to filter the suggestions to
183 * @return {any[]}
184 */
185 ve.ui.CompletionAction.prototype.filterSuggestionsForInput = function ( suggestions, input ) {
186 input = input.trim();
187
188 const normalizedInput = input.toLowerCase().trim();
189
190 let exact = false;
191 suggestions = suggestions.filter( ( suggestion ) => {
192 const result = this.compareSuggestionToInput( suggestion, normalizedInput );
193 exact = exact || result.isExact;
194 return result.isMatch;
195 } );
196
197 if ( this.constructor.static.defaultLimit < suggestions.length ) {
198 suggestions.length = this.constructor.static.defaultLimit;
199 }
200 if ( !exact && this.constructor.static.alwaysIncludeInput && input.length ) {
201 suggestions.push( this.createSuggestion( input ) );
202 }
203 return suggestions;
204 };
205
206 /**
207 * Compare a suggestion to the normalized user input (lower case)
208 *
209 * @param {any} suggestion Suggestion data, string by default
210 * @param {string} normalizedInput Noramlized user input
211 * @return {Object} Match object, containing two booleans, `isMatch` and `isExact`
212 */
213 ve.ui.CompletionAction.prototype.compareSuggestionToInput = function ( suggestion, normalizedInput ) {
214 const normalizedSuggestion = suggestion.toLowerCase();
215 return {
216 isMatch: normalizedSuggestion.slice( 0, normalizedInput.length ) === normalizedInput,
217 isExact: normalizedSuggestion === normalizedInput
218 };
219 };
220
221 /**
222 * Create a suggestion from an input
223 *
224 * @param {string} input User input
225 * @return {any} Suggestion data, string by default
226 */
227 ve.ui.CompletionAction.prototype.createSuggestion = function ( input ) {
228 return input;
229 };