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 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 | 1x 371x 371x 371x 371x 371x 371x 371x 371x 371x 371x 371x 1x 1x 303x 303x 303x 1x 1740x 1x 80x 80x 80x 1x 4x 4x 1x 102x 22x 80x 80x 80x 63x 63x 63x 63x 63x 63x 63x 63x 63x 63x 63x 17x 1x 119x 119x 80x 119x 119x 119x 119x 119x 119x 119x 119x 119x 86x 86x 79x 79x 79x 79x 79x 79x 79x 79x 63x 79x 86x 86x 86x 86x 119x 23x 23x 23x 23x 23x 1x 252x 252x 1x 143x 143x 1x 86x 86x 86x 1x 615x 3x 3x 615x 615x 3x 3x 1x 1x 1x 1x 615x 615x 615x | /*! * VisualEditor ContentEditable SelectionManager class. * * @copyright See AUTHORS.txt */ /** * Selection manager * * Handles rendering of fake selections on the surface: * - The deactivated selection stands in the user's native * selection when the native selection is moved elsewhere * (e.g. an inspector, or a dropdown menu). * - In a multi-user environment, other users' selections from * the surface synchronizer are rendered here. * - Other tools can manually render fake selections, e.g. the * FindAndReplaceDialog can highlight matched text, by calling * #drawSelections directly. * * @param {ve.ce.Surface} surface */ ve.ce.SelectionManager = function VeCeSelectionManager( surface ) { // Parent constructor ve.ce.SelectionManager.super.call( this ); this.surface = surface; this.userSelectionDeactivate = {}; this.drawnSelections = {}; this.drawnSelectionCache = {}; this.deactivatedSelectionVisible = true; this.showDeactivatedAsActivated = false; const synchronizer = this.getSurface().getModel().synchronizer; Iif ( synchronizer ) { synchronizer.connect( this, { authorSelect: 'onSynchronizerAuthorUpdate', authorChange: 'onSynchronizerAuthorUpdate', authorDisconnect: 'onSynchronizerAuthorDisconnect' } ); } // Debounce to prevent trying to draw every cursor position in history. this.onSurfacePositionDebounced = ve.debounce( this.onSurfacePosition.bind( this ) ); this.getSurface().connect( this, { position: this.onSurfacePositionDebounced } ); }; /* Inheritance */ OO.inheritClass( ve.ce.SelectionManager, OO.ui.Element ); /* Methods */ /** * Destroy the selection manager */ ve.ce.SelectionManager.prototype.destroy = function () { const synchronizer = this.getSurface().getModel().synchronizer; Iif ( synchronizer ) { synchronizer.disconnect( this ); } this.$element.remove(); }; /** * Get the surface * * @return {ve.ce.Surface} */ ve.ce.SelectionManager.prototype.getSurface = function () { return this.surface; }; /** * Start showing the deactivated selection * * @param {boolean} [showAsActivated=true] Selection should still show as activated */ ve.ce.SelectionManager.prototype.showDeactivatedSelection = function ( showAsActivated ) { this.deactivatedSelectionVisible = true; this.showDeactivatedAsActivated = showAsActivated === undefined || !!showAsActivated; this.updateDeactivatedSelection(); }; /** * Hide the deactivated selection */ ve.ce.SelectionManager.prototype.hideDeactivatedSelection = function () { this.deactivatedSelectionVisible = false; // Generates ve-ce-surface-selections-deactivated CSS class this.drawSelections( 'deactivated', [] ); }; ve.ce.SelectionManager.prototype.updateDeactivatedSelection = function () { if ( !this.deactivatedSelectionVisible ) { return; } const surface = this.getSurface(); const selection = surface.getSelection(); // Check we have a deactivated surface and a native selection if ( selection.isNativeCursor() ) { let textColor; // For collapsed selections, work out the text color to use for the cursor const isCollapsed = selection.getModel().isCollapsed(); Eif ( isCollapsed ) { const currentNode = surface.getDocument().getBranchNodeFromOffset( selection.getModel().getCoveringRange().start ); Eif ( currentNode ) { // This isn't perfect as it doesn't take into account annotations. textColor = currentNode.$element.css( 'color' ); } } const classes = []; Eif ( !this.showDeactivatedAsActivated ) { classes.push( 've-ce-surface-selections-deactivated-showAsDeactivated' ); } Eif ( isCollapsed ) { classes.push( 've-ce-surface-selections-deactivated-collapsed' ); } // Generates ve-ce-surface-selections-deactivated CSS class this.drawSelections( 'deactivated', [ selection ], { color: textColor, wrapperClass: classes.join( ' ' ) } ); } else { // Generates ve-ce-surface-selections-deactivated CSS class this.drawSelections( 'deactivated', [] ); } }; /** * Draw selections. * * @param {string} name Unique name for the selection being drawn * @param {ve.ce.Selection[]} selections Selections to draw * @param {Object} [options] * @param {string} options.color CSS color for the selection. Should usually * be set in a stylesheet using the generated class name. * @param {string} options.wrapperClass Additional CSS class string to add to the $selections wrapper. * mapped to the same index. * @param {string} options.label Label shown above each selection */ ve.ce.SelectionManager.prototype.drawSelections = function ( name, selections, options ) { options = options || {}; if ( !Object.prototype.hasOwnProperty.call( this.drawnSelections, name ) ) { this.drawnSelections[ name ] = {}; } const drawnSelection = this.drawnSelections[ name ]; drawnSelection.$selections = drawnSelection.$selections || // The following classes are used here: // * ve-ce-surface-selections-deactived // * ve-ce-surface-selections-<name> $( '<div>' ).addClass( 've-ce-surface-selections ve-ce-surface-selections-' + name ).appendTo( this.$element ); const oldSelections = drawnSelection.selections || []; const oldOptions = drawnSelection.options || {}; drawnSelection.selections = selections; drawnSelection.options = options; // Always set the 'class' attribute to ensure previously-set classes are cleared. drawnSelection.$selections.attr( 'class', 've-ce-surface-selections ve-ce-surface-selections-' + name + ' ' + ( options.wrapperClass || '' ) ); const selectionsJustShown = {}; selections.forEach( ( selection ) => { let $selection = this.getDrawnSelection( name, selection.getModel(), options ); if ( !$selection ) { let rects = selection.getSelectionRects(); Iif ( !rects ) { return; } rects = ve.minimizeRects( rects ); $selection = $( '<div>' ).addClass( 've-ce-surface-selection' ); rects.forEach( ( rect ) => { const $rect = $( '<div>' ).css( { top: rect.top, left: rect.left, // Collapsed selections can have a width of 0, so expand width: Math.max( rect.width, 1 ), height: rect.height } ); $selection.append( $rect ); if ( options.color ) { $rect.css( 'background-color', options.color ); } } ); Iif ( options.label ) { const boundingRect = selection.getSelectionBoundingRect(); $selection.append( $( '<div>' ) .addClass( 've-ce-surface-selection-label' ) .text( options.label ) .css( { top: boundingRect.top, left: boundingRect.left, 'background-color': options.color || '' } ) ); } } Eif ( !$selection.parent().length ) { drawnSelection.$selections.append( $selection ); } const cacheKey = this.storeDrawnSelection( $selection, name, selection.getModel(), options ); selectionsJustShown[ cacheKey ] = true; } ); // Remove any selections that were not in the latest list of selections oldSelections.forEach( ( oldSelection ) => { const cacheKey = this.getDrawnSelectionCacheKey( name, oldSelection.getModel(), oldOptions ); Eif ( !selectionsJustShown[ cacheKey ] ) { const $oldSelection = this.getDrawnSelection( name, oldSelection.getModel(), oldOptions ); Eif ( $oldSelection ) { $oldSelection.detach(); } } } ); }; /** * Get a cache key for a drawn selection * * @param {string} name Name of selection group * @param {ve.dm.Selection} selection Selection model * @param {Object} [options] Selection options * @return {string} Cache key */ ve.ce.SelectionManager.prototype.getDrawnSelectionCacheKey = function ( name, selection, options ) { options = options || {}; return name + '-' + JSON.stringify( selection ) + '-' + ( options.color || '' ) + '-' + ( options.label || '' ); }; /** * Get an already drawn selection from the cache * * @param {string} name Name of selection group * @param {ve.dm.Selection} selection Selection model * @param {Object} [options] Selection options * @return {jQuery} Drawn selection */ ve.ce.SelectionManager.prototype.getDrawnSelection = function ( name, selection, options ) { const cacheKey = this.getDrawnSelectionCacheKey( name, selection, options ); return Object.prototype.hasOwnProperty.call( this.drawnSelectionCache, cacheKey ) ? this.drawnSelectionCache[ cacheKey ] : null; }; /** * Store an already drawn selection in the cache * * @param {jQuery} $selection Drawn selection * @param {string} name Name of selection group * @param {ve.dm.Selection} selection Selection model * @param {Object} [options] Selection options * @return {string} Cache key */ ve.ce.SelectionManager.prototype.storeDrawnSelection = function ( $selection, name, selection, options ) { const cacheKey = this.getDrawnSelectionCacheKey( name, selection, options ); this.drawnSelectionCache[ cacheKey ] = $selection; return cacheKey; }; /** * Redraw selections * * This is triggered by a surface 'position' event, which fires when the surface * changes size, or when the document is modified. The drawnSelectionCache is * cleared as these two things will cause any previously calculated rectangles * to be incorrect. */ ve.ce.SelectionManager.prototype.redrawSelections = function () { Object.keys( this.drawnSelections ).forEach( ( name ) => { const drawnSelection = this.drawnSelections[ name ]; drawnSelection.$selections.empty(); } ); this.drawnSelectionCache = {}; Object.keys( this.drawnSelections ).forEach( ( name ) => { const drawnSelection = this.drawnSelections[ name ]; this.drawSelections( name, drawnSelection.selections, drawnSelection.options ); } ); }; /** * Paint a remote author's current selection, as stored in the synchronizer * * @param {number} authorId The author ID */ ve.ce.SelectionManager.prototype.paintAuthor = function ( authorId ) { const synchronizer = this.getSurface().getModel().synchronizer, authorData = synchronizer.getAuthorData( authorId ), selection = synchronizer.authorSelections[ authorId ]; if ( !authorData || !selection || authorId === synchronizer.getAuthorId() ) { return; } const color = '#' + authorData.color; if ( !Object.prototype.hasOwnProperty.call( this.userSelectionDeactivate, authorId ) ) { this.userSelectionDeactivate[ authorId ] = ve.debounce( () => { // TODO: Transition away the user label when inactive, maybe dim selection if ( Object.prototype.hasOwnProperty.call( this.drawnSelections, 'otherUserSelection-' + authorId ) ) { this.drawnSelections[ 'otherUserSelection-' + authorId ].$selections.addClass( 've-ce-surface-selections-otherUserSelection-inactive' ); } if ( Object.prototype.hasOwnProperty.call( this.drawnSelections, 'otherUserCursor-' + authorId ) ) { this.drawnSelections[ 'otherUserCursor-' + authorId ].$selections.addClass( 've-ce-surface-selections-otherUserCursor-inactive' ); } }, 5000 ); } this.userSelectionDeactivate[ authorId ](); if ( !selection || selection.isNull() ) { this.drawSelections( 'otherUserSelection-' + authorId, [] ); this.drawSelections( 'otherUserCursor-' + authorId, [] ); return; } this.drawSelections( 'otherUserSelection-' + authorId, [ ve.ce.Selection.static.newFromModel( selection, this.getSurface() ) ], { wrapperClass: 've-ce-surface-selections-otherUserSelection', color: color } ); const cursorSelection = selection instanceof ve.dm.LinearSelection && this.getSurface().getFocusedNode( selection.getRange() ) ? selection : selection.collapseToTo(); this.drawSelections( 'otherUserCursor-' + authorId, [ ve.ce.Selection.static.newFromModel( cursorSelection, this.getSurface() ) ], { wrapperClass: 've-ce-surface-selections-otherUserCursor', color: color, // Label is attached to cursor for 100% opacity, but it should probably be attached // to the selection, so the cursor can be selectively rendered just for LinearSelection's. label: authorData.name } ); }; /** * Called when the synchronizer receives a remote author selection or name change * * @param {number} authorId The author ID */ ve.ce.SelectionManager.prototype.onSynchronizerAuthorUpdate = function ( authorId ) { this.paintAuthor( authorId ); }; /** * Called when the synchronizer receives a remote author disconnect * * @param {number} authorId The author ID */ ve.ce.SelectionManager.prototype.onSynchronizerAuthorDisconnect = function ( authorId ) { this.drawSelections( 'otherUserSelection-' + authorId, [] ); this.drawSelections( 'otherUserCursor-' + authorId, [] ); }; /** * Respond to a position event on this surface */ ve.ce.SelectionManager.prototype.onSurfacePosition = function () { this.redrawSelections(); const synchronizer = this.getSurface().getModel().synchronizer; Iif ( synchronizer ) { // Defer to allow surface synchronizer to adjust for transactions setTimeout( () => { const authorSelections = synchronizer.authorSelections; for ( const authorId in authorSelections ) { this.onSynchronizerAuthorUpdate( +authorId ); } } ); } }; |