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 | 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 1x 1x 1x 1x 1x 1x 1x 1x 1x | /*! * VisualEditor UserInterface CompletionWidget class. * * @copyright See AUTHORS.txt */ /** * Widget that displays autocompletion suggestions * * @class * @extends OO.ui.Widget * * @constructor * @param {ve.ui.Surface} surface Surface to complete into * @param {Object} [config] Configuration options * @param {Object} [config.validate] Validation pattern passed to TextInputWidgets * @param {boolean} [config.readOnly=false] Prevent changes to the value of the widget. */ ve.ui.CompletionWidget = function VeUiCompletionWidget( surface, config ) { this.surface = surface; this.surfaceModel = surface.getModel(); // Configuration config = config || { anchor: false }; // Parent constructor ve.ui.CompletionWidget.super.call( this, config ); this.$tabIndexed = this.$element; const $doc = surface.getView().getDocument().getDocumentNode().$element; this.popup = new OO.ui.PopupWidget( { anchor: false, align: 'forwards', hideWhenOutOfView: false, autoFlip: false, width: null, $container: config.$popupContainer || this.surface.$element, containerPadding: config.popupPadding } ); this.input = new OO.ui.TextInputWidget(); this.menu = new OO.ui.MenuSelectWidget( { widget: this, $input: $doc.add( this.input.$input ) } ); // This may be better semantically as a MenuSectionOptionWidget, // but that causes all subsequent options to be indented. this.header = new OO.ui.MenuOptionWidget( { classes: [ 've-ui-completionWidget-header' ], disabled: true } ); this.noResults = new OO.ui.MenuOptionWidget( { label: ve.msg( 'visualeditor-completionwidget-noresults' ), classes: [ 've-ui-completionWidget-noresults' ], disabled: true } ); // Events this.menu.connect( this, { choose: 'onMenuChoose', toggle: 'onMenuToggle' } ); this.input.connect( this, { change: 'update' } ); this.popup.$element.prepend( this.input.$element ); this.popup.$body.append( this.menu.$element ); // Setup this.$element.addClass( 've-ui-completionWidget' ) .append( this.popup.$element ); }; /* Inheritance */ OO.inheritClass( ve.ui.CompletionWidget, OO.ui.Widget ); /** * Setup the completion widget * * @param {ve.ui.Action} action Action which opened the widget * @param {boolean} [isolateInput] Isolate input from the surface */ ve.ui.CompletionWidget.prototype.setup = function ( action, isolateInput ) { const range = this.surfaceModel.getSelection().getCoveringRange(); this.action = action; this.isolateInput = !!isolateInput; this.sequenceLength = this.action.getSequenceLength(); this.initialOffset = range.end - this.sequenceLength; this.input.toggle( this.isolateInput ); if ( this.isolateInput ) { this.wasActive = !this.surface.getView().isDeactivated(); this.surface.getView().deactivate(); this.input.setValue( '' ); setTimeout( () => { this.input.focus(); }, 1 ); } else { this.wasActive = false; } this.update(); this.surfaceModel.connect( this, { select: 'onModelSelect' } ); }; /** * Teardown the completion widget */ ve.ui.CompletionWidget.prototype.teardown = function () { this.tearingDown = true; this.popup.toggle( false ); this.surfaceModel.disconnect( this ); if ( this.wasActive ) { this.surface.getView().activate(); } this.action = undefined; this.tearingDown = false; }; /** * Update the completion widget after the input has changed */ ve.ui.CompletionWidget.prototype.update = function () { const direction = this.surface.getDir(), range = this.getCompletionRange(), boundingRect = this.surface.getView().getSelection( new ve.dm.LinearSelection( range ) ).getSelectionBoundingRect(), style = { top: boundingRect.bottom }; let input; if ( this.isolateInput ) { input = this.input.getValue(); } else { const data = this.surfaceModel.getDocument().data; input = data.getText( false, range ); } if ( direction === 'rtl' ) { // This works because this.$element is a 0x0px box, with the menu positioned relative to it. // If this style was applied to the menu, we'd need to do some math here to align the right // edge of the menu with the right edge of the selection. style.left = boundingRect.right; } else { style.left = boundingRect.left; } this.$element.css( style ); this.updateMenu( input ); this.action.getSuggestions( input ).then( ( suggestions ) => { if ( !this.action ) { // Check widget hasn't been torn down return; } this.menu.clearItems(); let menuItems = suggestions.map( this.action.getMenuItemForSuggestion.bind( this.action ) ); menuItems = this.action.updateMenuItems( menuItems ); this.menu.addItems( menuItems ); this.menu.highlightItem( this.menu.findFirstSelectableItem() ); this.updateMenu( input, suggestions ); } ); }; /** * Update the widget's menu with the latest suggestions * * @param {string} input Input text * @param {Array} suggestions Suggestions */ ve.ui.CompletionWidget.prototype.updateMenu = function ( input, suggestions ) { // Update the header based on the input const label = this.action.getHeaderLabel( input, suggestions ); if ( label !== undefined ) { this.header.setLabel( label ); } if ( this.header.getLabel() !== null ) { this.menu.addItems( [ this.header ], 0 ); } else { this.menu.removeItems( [ this.header ] ); } if ( !this.isolateInput ) { // If there is a header or menu items, show the menu if ( this.menu.items.length ) { this.menu.toggle( true ); this.popup.toggle( true ); // Menu may have changed size, so recalculate position this.popup.updateDimensions(); } else { this.popup.toggle( false ); } } else { if ( !this.menu.items.length ) { this.menu.addItems( [ this.noResults ], 0 ); } this.menu.toggle( true ); this.popup.toggle( true ); this.popup.updateDimensions(); } }; /** * Handle choose events from the menu * * @param {OO.ui.MenuOptionWidget} item Chosen option */ ve.ui.CompletionWidget.prototype.onMenuChoose = function ( item ) { this.action.chooseItem( item, this.getCompletionRange( true ) ); this.teardown(); }; /** * Handle toggle events from the menu * * @param {boolean} visible Menu is visible */ ve.ui.CompletionWidget.prototype.onMenuToggle = function ( visible ) { if ( !visible && !this.tearingDown ) { // Menu was hidden by the user (e.g. pressed ESC) - trigger a teardown this.teardown(); } }; /** * Handle select events from the document model * * @param {ve.dm.Selection} selection Selection */ ve.ui.CompletionWidget.prototype.onModelSelect = function () { const range = this.getCompletionRange(); const countMatches = () => { let matches = this.menu.getItems().length; if ( this.header.getLabel() !== null ) { matches--; } if ( this.action.constructor.static.alwaysIncludeInput ) { matches--; } return matches; }; if ( !range || range.isBackwards() || this.action.shouldAbandon( this.surfaceModel.getDocument().data.getText( false, range ), countMatches() ) ) { this.teardown(); } else { this.update(); } }; /** * Get the range where the user has entered text in the document since opening the widget * * @param {boolean} [withSequence] Include the triggering sequence text in the range * @return {ve.Range|null} Range, null if not valid */ ve.ui.CompletionWidget.prototype.getCompletionRange = function ( withSequence ) { const range = this.surfaceModel.getSelection().getCoveringRange(); if ( !range || !this.action ) { return null; } return new ve.Range( this.initialOffset + ( withSequence ? 0 : this.sequenceLength ), range.end ); }; |