Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ce/ve.ce.FocusableNode.js

raw symbols SCIP
1 /*!
2 * VisualEditor ContentEditable FocusableNode class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * ContentEditable focusable node.
9 *
10 * Focusable elements have a special treatment by ve.ce.Surface. When the user selects only a single
11 * node, if it is focusable, the surface will set the focusable node's focused state. Other systems,
12 * such as the context, may also use a focusable node's $focusable property as a hint of where the
13 * primary element in the node is. Typically, and by default, the primary element is the root
14 * element, but in some cases it may need to be configured to be a specific child element within the
15 * node's DOM rendering.
16 *
17 * If your focusable node changes size and the highlight must be redrawn, call redrawHighlights().
18 * 'resizeEnd' and 'rerender' are already bound to call this.
19 *
20 * @class
21 * @abstract
22 *
23 * @constructor
24 * @param {jQuery} [$focusable=this.$element] Primary element user is focusing on
25 * @param {Object} [config] Configuration options
26 * @param {jQuery} [config.$bounding=$focusable] Element to consider for bounding box calculations (e.g.
27 * attaching inspectors)
28 * @param {string[]} [config.classes] CSS classes to be added to the highlight container
29 */
30 ve.ce.FocusableNode = function VeCeFocusableNode( $focusable, config ) {
31 config = config || {};
32
33 // Properties
34 this.focused = false;
35 this.highlighted = false;
36 this.isFocusableSetup = false;
37 this.$highlights = $( '<div>' ).addClass( 've-ce-focusableNode-highlights' )
38 // Allow the highlight to take focus, so that focus is not removed from
39 // the surface when clicking on it (T341681)
40 .attr( 'tabIndex', 0 );
41 this.$focusable = $focusable || this.$element;
42 this.$bounding = config.$bounding || this.$focusable;
43 this.focusableSurface = null;
44 this.rects = null;
45 this.boundingRect = null;
46 this.startAndEndRects = null;
47 this.icon = null;
48 this.touchMoved = false;
49
50 if ( Array.isArray( config.classes ) ) {
51 // eslint-disable-next-line mediawiki/class-doc
52 this.$highlights.addClass( config.classes );
53 }
54
55 // Use a debounced handler as some actions can trigger redrawHighlights
56 // twice in quick succession resizeEnd+rerender
57 this.redrawHighlightsDebounced = ve.debounce( this.redrawHighlights.bind( this ), 100 );
58
59 // DOM changes
60 this.$element
61 .addClass( 've-ce-focusableNode' )
62 .prop( 'contentEditable', 'false' );
63
64 // Events
65 this.connect( this, {
66 setup: 'onFocusableSetup',
67 teardown: 'onFocusableTeardown',
68 resizeStart: 'onFocusableResizeStart',
69 resizeEnd: 'onFocusableResizeEnd',
70 rerender: 'onFocusableRerender'
71 } );
72 };
73
74 /* Inheritance */
75
76 OO.initClass( ve.ce.FocusableNode );
77
78 /* Events */
79
80 /**
81 * @event ve.ce.FocusableNode#focus
82 */
83
84 /**
85 * @event ve.ce.FocusableNode#blur
86 */
87
88 /* Static properties */
89
90 /**
91 * Icon to use when the rendering is considered not visible, as defined in #hasRendering
92 *
93 * No icon is show if null.
94 *
95 * @static
96 * @property {string|null}
97 * @inheritable
98 */
99 ve.ce.FocusableNode.static.iconWhenInvisible = null;
100
101 /**
102 * Command to execute when Enter is pressed while this node is focused, or when the node is double-clicked.
103 *
104 * @static
105 * @property {string|null}
106 * @inheritable
107 */
108 ve.ce.FocusableNode.static.primaryCommandName = null;
109
110 /**
111 * Command to execute when Delete or Backspace is pressed while this node is focused.
112 *
113 * @static
114 * @property {string|null}
115 * @inheritable
116 */
117 ve.ce.FocusableNode.static.deleteCommandName = null;
118
119 /* Static methods */
120
121 /**
122 * Get rects for an element
123 *
124 * @param {jQuery} $element Element to get highlights
125 * @param {Object} [relativeRect] Rect with top & left to get position relative to
126 * @return {Object} Object containing rects and boundingRect
127 */
128 ve.ce.FocusableNode.static.getRectsForElement = function ( $element, relativeRect ) {
129 let $set;
130 let rects = [];
131
132 function process( el ) {
133 if ( el.classList.contains( 've-ce-noHighlight' ) ) {
134 return;
135 }
136
137 const $el = $( el );
138
139 // Don't descend if overflow is anything but visible as this prevents child
140 // elements appearing beyond the bounding box of the parent, *unless* display
141 // is inline, in which case the overflow setting will be ignored
142 const overflow = $el.css( 'overflow' );
143 if ( overflow && overflow !== 'visible' && $el.css( 'display' ) !== 'inline' ) {
144 $set = $set.not( $el.find( '*' ) );
145 }
146
147 ve.batchPush( rects, el.getClientRects() );
148 }
149
150 $set = $element.find( '*' ).addBack();
151 // Calling process() may change $set.length
152 for ( let i = 0; i < $set.length; i++ ) {
153 process( $set[ i ] );
154 }
155
156 rects = ve.minimizeRects( rects );
157
158 // Elements with a width/height of 0 return a clientRect with a width/height of 1
159 // As elements with an actual width/height of 1 aren't that useful anyway, just
160 // throw away anything that is <=1
161 const filteredRects = rects.filter( ( rect ) => rect.width > 1 && rect.height > 1 );
162 // But if this filtering doesn't leave any rects at all, then we do want to use the 1px rects
163 if ( filteredRects.length > 0 ) {
164 rects = filteredRects;
165 }
166
167 let boundingRect = null;
168
169 for ( let i = 0, l = rects.length; i < l; i++ ) {
170 // Translate to relative
171 if ( relativeRect ) {
172 rects[ i ] = ve.translateRect( rects[ i ], -relativeRect.left, -relativeRect.top );
173 }
174 if ( !boundingRect ) {
175 boundingRect = ve.copy( rects[ i ] );
176 } else {
177 boundingRect.top = Math.min( boundingRect.top, rects[ i ].top );
178 boundingRect.left = Math.min( boundingRect.left, rects[ i ].left );
179 boundingRect.bottom = Math.max( boundingRect.bottom, rects[ i ].bottom );
180 boundingRect.right = Math.max( boundingRect.right, rects[ i ].right );
181 }
182 }
183 if ( boundingRect ) {
184 boundingRect.width = boundingRect.right - boundingRect.left;
185 boundingRect.height = boundingRect.bottom - boundingRect.top;
186 }
187
188 return {
189 rects: rects,
190 boundingRect: boundingRect
191 };
192 };
193
194 /* Methods */
195
196 /**
197 * Create a highlight element.
198 *
199 * @return {jQuery} A highlight element
200 */
201 ve.ce.FocusableNode.prototype.createHighlight = function () {
202 // eslint-disable-next-line mediawiki/class-doc
203 return $( '<div>' )
204 .addClass( [ 've-ce-focusableNode-highlight', ...this.getExtraHighlightClasses() ] )
205 .prop( {
206 title: this.constructor.static.getDescription( this.model ),
207 draggable: true
208 } )
209 .on( {
210 dragstart: this.onFocusableDragStart.bind( this ),
211 dragend: this.onFocusableDragEnd.bind( this )
212 } );
213 };
214
215 /**
216 * Array of CSS classes to add to highlights
217 *
218 * @return {string[]}
219 */
220 ve.ce.FocusableNode.prototype.getExtraHighlightClasses = function () {
221 return this.generatedContentsInvalid ? [ 've-ce-focusableNode-highlight-error' ] : [];
222 };
223
224 /**
225 * Handle node setup.
226 */
227 ve.ce.FocusableNode.prototype.onFocusableSetup = function () {
228 // Exit if already setup or not attached
229 if ( this.isFocusableSetup || !this.root ) {
230 return;
231 }
232
233 this.focusableSurface = this.root.getSurface();
234
235 // DOM changes (duplicated from constructor in case this.$element is replaced)
236 // eslint-disable-next-line no-jquery/no-class-state
237 if ( !this.$element.hasClass( 've-ce-focusableNode' ) ) {
238 // Optimization: If this.$element has the correct class assume it has already
239 // been setup in the construct and avoid expensive DOM property manipulation
240 this.$element
241 .addClass( 've-ce-focusableNode' )
242 .prop( 'contentEditable', 'false' );
243 }
244
245 // Events
246 this.$focusable.on( {
247 'mouseenter.ve-ce-focusableNode': this.onFocusableMouseEnter.bind( this ),
248 'touchstart.ve-ce-focusableNode': this.onFocusableTouchStart.bind( this ),
249 'touchmove.ve-ce-focusableNode': this.onFocusableTouchMove.bind( this ),
250 'mousedown.ve-ce-focusableNode touchend.ve-ce-focusableNode': this.onFocusableMouseDown.bind( this )
251 } );
252 // $element is ce=false so make sure nothing happens when you click
253 // on it, just in case the browser decides to do something.
254 // If $element == $focusable then this can be skipped as $focusable already
255 // handles mousedown events.
256 if ( !this.$element.is( this.$focusable ) ) {
257 this.$element.on( {
258 'mousedown.ve-ce-focusableNode': function ( e ) {
259 if ( !ve.isContentEditable( e.target ) ) {
260 e.preventDefault();
261 }
262 }
263 } );
264 }
265 // Note that preventing default on mousedown doesn't suppress click
266 // events, so link navigation would still occur:
267 this.$element.on( {
268 'click.ve-ce-focusableNode': function ( e ) {
269 if ( !ve.isContentEditable( e.target ) && e.which === OO.ui.MouseButtons.LEFT ) {
270 e.preventDefault();
271 }
272 }
273 } );
274
275 if ( this.constructor.static.iconWhenInvisible ) {
276 // Set up the invisible icon, and watch for its continued necessity if
277 // unloaded images which don't specify their width or height are
278 // involved.
279 this.$element
280 .find( 'img:not([width]),img:not([height])' )
281 .addBack( 'img:not([width]),img:not([height])' )
282 .on( 'load', this.updateInvisibleIcon.bind( this ) );
283 this.updateInvisibleIcon();
284 }
285
286 // A node may be re-setup when focused. redrawHighlights will only do
287 // something if the node is currently focused.
288 this.redrawHighlightsDebounced();
289
290 this.isFocusableSetup = true;
291 };
292
293 /**
294 * Update the state of icon if this node is invisible
295 *
296 * If the node doesn't have a visible rendering, we insert an icon to represent
297 * it. If the icon was already present, and this is called again when rendering
298 * has developed, we remove the icon.
299 */
300 ve.ce.FocusableNode.prototype.updateInvisibleIcon = function () {
301 if ( !this.constructor.static.iconWhenInvisible ) {
302 return;
303 }
304
305 // Make sure any existing icon is detached before measuring
306 if ( this.icon ) {
307 this.icon.$element.detach();
308 }
309 const showIcon = !this.hasRendering();
310
311 // Defer updating the DOM. If we don't do this, the hasRendering() call for the next
312 // FocusableNode will force a reflow, which is slow.
313 requestAnimationFrame( () => {
314 this.updateInvisibleIconSync( showIcon );
315 } );
316 };
317
318 /**
319 * Synchronous part of #updateInvisibleIconSync
320 *
321 * @param {boolean} showIcon Show the icon
322 * @private
323 */
324 ve.ce.FocusableNode.prototype.updateInvisibleIconSync = function ( showIcon ) {
325 if ( !this.getModel() ) {
326 // Check the node hasn't been destroyed, as this method is called after a requestAnimationFrame
327 return;
328 }
329 if ( showIcon ) {
330 // Don't try to append to void tags, or unrendered tags
331 const voidAndHiddenTypes = [ 'style', 'script', ...ve.elementTypes.void ];
332 const $firstElement = this.$element.not( voidAndHiddenTypes.join( ',' ) ).first();
333 this.createInvisibleIcon();
334 if (
335 // Not needed if node is not attached (e.g. if used in the converter)
336 document.body.contains( $firstElement[ 0 ] ) &&
337 // eslint-disable-next-line no-jquery/no-sizzle
338 !$firstElement.is( ':visible' )
339 ) {
340 // The first element to which we want to attach our icon is invisible.
341 // In this case make sure it *is* visible, so the button is visible,
342 // but remove the contents, so we don't start showing them (T305110).
343 $firstElement.empty().css( 'display', 'inline-block' );
344 }
345 $firstElement
346 .addClass( 've-ce-focusableNode-invisible' )
347 .prepend( this.icon.$element );
348 } else if ( this.icon ) {
349 this.$element.removeClass( 've-ce-focusableNode-invisible' );
350 this.icon.$element.detach();
351 }
352 };
353
354 /**
355 * Create a element to show if the node is invisible
356 */
357 ve.ce.FocusableNode.prototype.createInvisibleIcon = function () {
358 if ( this.icon ) {
359 return;
360 }
361 this.icon = new OO.ui.ButtonWidget( {
362 classes: [ 've-ce-focusableNode-invisibleIcon' ],
363 framed: false,
364 // Make button unfocusable, T198912
365 tabIndex: null,
366 icon: this.constructor.static.iconWhenInvisible
367 } );
368 this.updateInvisibleIconLabel();
369 };
370
371 /**
372 * Get a label for the invisible icon
373 *
374 * Defaults to #getDescription
375 *
376 * @return {jQuery|string|OO.ui.HtmlSnippet|Function|null} Invisible icon label
377 */
378 ve.ce.FocusableNode.prototype.getInvisibleIconLabel = function () {
379 return this.model ? this.constructor.static.getDescription( this.model ) : '';
380 };
381
382 /**
383 * Update the invisible icon's label
384 */
385 ve.ce.FocusableNode.prototype.updateInvisibleIconLabel = function () {
386 if ( this.icon ) {
387 this.icon.setLabel( this.getInvisibleIconLabel() || null );
388 }
389 };
390
391 /**
392 * Handle node teardown.
393 */
394 ve.ce.FocusableNode.prototype.onFocusableTeardown = function () {
395 // Exit if not setup or not attached
396 if ( !this.isFocusableSetup || !this.root ) {
397 return;
398 }
399
400 // Events
401 this.$focusable.off( '.ve-ce-focusableNode' );
402 this.$element.off( '.ve-ce-focusableNode' );
403
404 // Highlights
405 this.clearHighlights();
406
407 // DOM changes
408 this.$element
409 .removeClass( 've-ce-focusableNode' )
410 .removeProp( 'contentEditable' );
411
412 this.focusableSurface = null;
413 this.isFocusableSetup = false;
414 };
415
416 /**
417 * Handle highlight mouse down events.
418 *
419 * @param {jQuery.Event} e Mouse down event
420 */
421 ve.ce.FocusableNode.prototype.onFocusableMouseDown = function ( e ) {
422 if ( e.type === 'touchend' && this.touchMoved ) {
423 return;
424 }
425
426 if ( this.isInContentEditableDisabled() ) {
427 return;
428 }
429
430 const surfaceModel = this.focusableSurface.getModel(),
431 selection = surfaceModel.getSelection(),
432 nodeRange = this.model.getOuterRange();
433
434 if ( e.which === OO.ui.MouseButtons.RIGHT ) {
435 // The same technique is used in ve.ce.TableNode:
436 // Make ce=true so we get cut/paste options in the context menu
437 this.$highlights.prop( 'contentEditable', 'true' );
438 // Select the clicked element so we get a copy option in the context menu
439 ve.selectElement( this.$highlights[ 0 ] );
440 setTimeout( () => {
441 // Undo ce=true as soon as the context menu is shown
442 this.$highlights.prop( 'contentEditable', 'false' );
443 this.focusableSurface.preparePasteTargetForCopy();
444 } );
445 }
446
447 // Wait for native selection to change before correcting
448 setTimeout( () => {
449 // Check surface still exists after timeout
450 if ( this.focusableSurface ) {
451 const range = selection instanceof ve.dm.LinearSelection && selection.getRange();
452 surfaceModel.getLinearFragment(
453 e.shiftKey && range ?
454 ve.Range.static.newCoveringRange(
455 [ range, nodeRange ], range.from > nodeRange.from
456 ) :
457 nodeRange
458 ).select();
459 this.focusableSurface.updateActiveAnnotations();
460 // Ensure surface is active as native 'focus' event won't be fired
461 this.focusableSurface.activate();
462 }
463 } );
464 };
465
466 /**
467 * Handle highlight double click events.
468 *
469 * @param {jQuery.Event} e Double click event
470 */
471 ve.ce.FocusableNode.prototype.onFocusableDblClick = function () {
472 if ( this.isInContentEditableDisabled() ) {
473 return;
474 }
475 if ( this.getModel().isEditable() ) {
476 this.executeCommand();
477 }
478 };
479
480 /**
481 * Execute the command associated with this node.
482 */
483 ve.ce.FocusableNode.prototype.executeCommand = function () {
484 if ( !this.model.isInspectable() ) {
485 return;
486 }
487 const surface = this.focusableSurface.getSurface();
488 const command = surface.commandRegistry.getCommandForNode( this );
489 if ( command ) {
490 command.execute( surface );
491 }
492 };
493
494 /**
495 * Handle element drag start.
496 *
497 * @param {jQuery.Event} e Drag start event
498 */
499 ve.ce.FocusableNode.prototype.onFocusableDragStart = function ( e ) {
500 if ( this.focusableSurface ) {
501 // Pass event up to the surface
502 this.focusableSurface.onDocumentDragStart( e );
503 }
504 };
505
506 /**
507 * Handle element drag end.
508 *
509 * If a relocation actually takes place the node is destroyed before this events fires.
510 *
511 * @param {jQuery.Event} e Drag end event
512 */
513 ve.ce.FocusableNode.prototype.onFocusableDragEnd = function () {
514 // endRelocation is usually triggered by onDocumentDrop in the surface, but if it isn't
515 // trigger it here instead
516 if ( this.focusableSurface ) {
517 this.focusableSurface.endRelocation();
518 }
519 };
520
521 /**
522 * Handle mouse enter events.
523 *
524 * @param {jQuery.Event} e Mouse enter event
525 */
526 ve.ce.FocusableNode.prototype.onFocusableMouseEnter = function () {
527 if ( !this.root.getSurface().dragging && !this.root.getSurface().resizing && !this.isInContentEditableDisabled() ) {
528 this.createHighlights();
529 }
530 };
531
532 /**
533 * Handle touch start events.
534 *
535 * @param {jQuery.Event} e Touch start event
536 */
537 ve.ce.FocusableNode.prototype.onFocusableTouchStart = function () {
538 this.touchMoved = false;
539 };
540
541 /**
542 * Handle touch move events.
543 *
544 * @param {jQuery.Event} e Touch move event
545 */
546 ve.ce.FocusableNode.prototype.onFocusableTouchMove = function () {
547 this.touchMoved = true;
548 };
549
550 /**
551 * Handle surface mouse move events.
552 *
553 * @param {jQuery.Event} e Mouse move event
554 */
555 ve.ce.FocusableNode.prototype.onSurfaceMouseMove = function ( e ) {
556 const $target = $( e.target );
557 if (
558 // eslint-disable-next-line no-jquery/no-class-state
559 !$target.hasClass( 've-ce-focusableNode-highlight' ) &&
560 !OO.ui.contains( this.$focusable.toArray(), $target[ 0 ], true )
561 ) {
562 this.clearHighlights();
563 }
564 };
565
566 /**
567 * Handle surface mouse leave events.
568 *
569 * @param {jQuery.Event} e Mouse leave event
570 */
571 ve.ce.FocusableNode.prototype.onSurfaceMouseLeave = function ( e ) {
572 if ( e.relatedTarget === null ) {
573 this.clearHighlights();
574 }
575 };
576
577 /**
578 * Handle resize start events.
579 */
580 ve.ce.FocusableNode.prototype.onFocusableResizeStart = function () {
581 this.clearHighlights();
582 };
583
584 /**
585 * Handle resize end event.
586 */
587 ve.ce.FocusableNode.prototype.onFocusableResizeEnd = function () {
588 this.redrawHighlightsDebounced();
589 };
590
591 /**
592 * Handle rerender event.
593 */
594 ve.ce.FocusableNode.prototype.onFocusableRerender = function () {
595 if ( this.focused && this.focusableSurface ) {
596 this.redrawHighlightsDebounced();
597 // Reposition menu
598 this.focusableSurface.getSurface().getContext().updateDimensions( true );
599 }
600 };
601
602 /**
603 * Check if node is focused.
604 *
605 * @return {boolean} Node is focused
606 */
607 ve.ce.FocusableNode.prototype.isFocused = function () {
608 return this.focused;
609 };
610
611 /**
612 * Set the selected state of the node.
613 *
614 * @param {boolean} value Node is focused
615 * @fires ve.ce.FocusableNode#focus
616 * @fires ve.ce.FocusableNode#blur
617 */
618 ve.ce.FocusableNode.prototype.setFocused = function ( value ) {
619 value = !!value;
620 if ( this.focused !== value ) {
621 this.focused = value;
622 if ( this.focused ) {
623 this.emit( 'focus' );
624 this.$element.addClass( 've-ce-focusableNode-focused' );
625 this.createHighlights();
626 // this.focused may have changed, so re-attach in the correct container
627 this.focusableSurface.appendHighlights( this.$highlights, this.focused );
628 this.focusableSurface.$element.off( '.ve-ce-focusableNode' );
629 } else {
630 this.emit( 'blur' );
631 this.$element.removeClass( 've-ce-focusableNode-focused' );
632 this.$highlights.removeClass( 've-ce-focusableNode-highlights-deactivated' );
633 this.clearHighlights();
634 }
635 }
636 };
637
638 /**
639 * Creates highlights.
640 */
641 ve.ce.FocusableNode.prototype.createHighlights = function () {
642 if ( this.highlighted ) {
643 return;
644 }
645
646 this.$highlights.on( {
647 mousedown: this.onFocusableMouseDown.bind( this ),
648 dblclick: this.onFocusableDblClick.bind( this )
649 } );
650
651 this.highlighted = true;
652
653 this.positionHighlights();
654
655 this.focusableSurface.appendHighlights( this.$highlights, this.focused );
656 this.focusableSurface.connect( this, {
657 position: 'positionHighlights',
658 activation: 'onSurfaceActivation'
659 } );
660
661 // Events
662 if ( !this.focused ) {
663 this.focusableSurface.$element.on( {
664 'mousemove.ve-ce-focusableNode': this.onSurfaceMouseMove.bind( this ),
665 'mouseleave.ve-ce-focusableNode': this.onSurfaceMouseLeave.bind( this )
666 } );
667 }
668 };
669
670 /**
671 * Handle activation events from the surface
672 */
673 ve.ce.FocusableNode.prototype.onSurfaceActivation = function () {
674 this.$highlights.toggleClass( 've-ce-focusableNode-highlights-deactivated', !!this.focusableSurface.isShownAsDeactivated() );
675 };
676
677 /**
678 * Clears highlight.
679 */
680 ve.ce.FocusableNode.prototype.clearHighlights = function () {
681 if ( !this.highlighted ) {
682 return;
683 }
684 this.$highlights.remove().empty();
685 this.focusableSurface.$element.off( '.ve-ce-focusableNode' );
686 this.focusableSurface.disconnect( this );
687 this.highlighted = false;
688 this.boundingRect = null;
689 };
690
691 /**
692 * Redraws highlight.
693 */
694 ve.ce.FocusableNode.prototype.redrawHighlights = function () {
695 if ( this.focused && this.focusableSurface ) {
696 // setFocused will call clearHighlights/createHighlights
697 // and also re-bind events.
698 this.setFocused( false );
699 this.setFocused( true );
700 }
701 };
702
703 /**
704 * Re-calculate position of highlights
705 */
706 ve.ce.FocusableNode.prototype.calculateHighlights = function () {
707 // Protect against calling before/after surface setup/teardown
708 if ( !this.focusableSurface ) {
709 this.rects = [];
710 this.boundingRect = null;
711 this.startAndEndRects = null;
712 return;
713 }
714
715 const surfaceOffset = this.focusableSurface.getSurface().getBoundingClientRect();
716
717 const allRects = this.constructor.static.getRectsForElement( this.$focusable, surfaceOffset );
718
719 this.rects = allRects.rects;
720 this.boundingRect = allRects.boundingRect;
721 // startAndEndRects is lazily evaluated in getStartAndEndRects from rects
722 this.startAndEndRects = null;
723 };
724
725 /**
726 * Positions highlights, and remove collapsed ones
727 */
728 ve.ce.FocusableNode.prototype.positionHighlights = function () {
729 if ( !this.highlighted ) {
730 return;
731 }
732
733 this.calculateHighlights();
734 this.$highlights.empty()
735 // Append something selectable for right-click copy
736 .append( $( '<span>' ).addClass( 've-ce-focusableNode-highlight-selectable' ).text( '\u00a0' ) );
737
738 for ( let i = 0, l = this.rects.length; i < l; i++ ) {
739 const $highlight = this.createHighlight();
740 this.$highlights.append(
741 $highlight.css( {
742 top: this.rects[ i ].top,
743 left: this.rects[ i ].left,
744 width: this.rects[ i ].width,
745 height: this.rects[ i ].height
746 } )
747 );
748 }
749 };
750
751 /**
752 * Get list of rectangles outlining the shape of the node relative to the surface
753 *
754 * @return {Object[]} List of rectangle objects
755 */
756 ve.ce.FocusableNode.prototype.getRects = function () {
757 if ( !this.highlighted ) {
758 this.calculateHighlights();
759 }
760 return this.rects;
761 };
762
763 /**
764 * Get the bounding rectangle of the focusable node highlight relative to the surface
765 *
766 * @return {Object|null} Top, left, bottom & right positions of the focusable node relative to the surface
767 */
768 ve.ce.FocusableNode.prototype.getBoundingRect = function () {
769 if ( !this.$bounding.is( this.$focusable ) ) {
770 const surfaceOffset = this.focusableSurface.getSurface().getBoundingClientRect();
771 const allRects = this.constructor.static.getRectsForElement( this.$bounding, surfaceOffset );
772 return allRects.boundingRect;
773 }
774 if ( !this.highlighted ) {
775 this.calculateHighlights();
776 }
777 return this.boundingRect;
778 };
779
780 /**
781 * Get start and end rectangles of an inline focusable node relative to the surface
782 *
783 * @return {Object.<string,Object>|null} Start and end rectangles
784 */
785 ve.ce.FocusableNode.prototype.getStartAndEndRects = function () {
786 if ( !this.highlighted ) {
787 this.calculateHighlights();
788 }
789 if ( !this.startAndEndRects ) {
790 this.startAndEndRects = ve.getStartAndEndRects( this.rects );
791 }
792 return this.startAndEndRects;
793 };
794
795 /**
796 * Check if the rendering is visible
797 *
798 * "Visible", in this case, is defined as at least 10px × 4px
799 * or 4px × 10px in dimensions (T307527)
800 *
801 * @return {boolean} The node has a visible rendering
802 */
803 ve.ce.FocusableNode.prototype.hasRendering = function () {
804 let visible = false;
805
806 function checkSize( width, height ) {
807 return ( width >= 10 && height >= 4 ) ||
808 ( height >= 10 && width >= 4 );
809 }
810
811 this.$element.each( ( i, element ) => {
812 if (
813 checkSize( element.offsetWidth, element.offsetHeight ) ||
814 // Check width/height attribute as well. (T125767)
815 checkSize( element.width, element.height )
816 ) {
817 visible = true;
818 return false;
819 }
820 } );
821 return visible;
822 };
823
824 /**
825 * Check if the node is inside a ve.ce.ContentEditableNode with editing disabled
826 *
827 * Ignore nodes which just disable CE in the DOM manually (e.g. TableNode)
828 * as focusables should still be highlightable in these.
829 *
830 * @return {boolean} Editing disabled
831 */
832 ve.ce.FocusableNode.prototype.isInContentEditableDisabled = function () {
833 return !!this.traverseUpstream( ( node ) => !(
834 node.isContentEditable && !node.isContentEditable()
835 ) );
836 };