Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ce/selections/ve.ce.LinearSelection.js

raw symbols SCIP
1 /*!
2 * VisualEditor Linear Selection class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * @class
9 * @extends ve.ce.Selection
10 * @constructor
11 * @param {ve.ce.Surface} surface
12 * @param {ve.dm.Selection} model
13 */
14 ve.ce.LinearSelection = function VeCeLinearSelection() {
15 // Parent constructor
16 ve.ce.LinearSelection.super.apply( this, arguments );
17
18 // Properties
19 // The focused node in the view when this selection was created, if one exists
20 this.focusedNode = this.getSurface().getFocusedNode( this.getModel().getRange() );
21 this.directionality = null;
22 };
23
24 /* Inheritance */
25
26 OO.inheritClass( ve.ce.LinearSelection, ve.ce.Selection );
27
28 /* Static Properties */
29
30 ve.ce.LinearSelection.static.name = 'linear';
31
32 /* Method */
33
34 /**
35 * @inheritdoc
36 */
37 ve.ce.LinearSelection.prototype.getSelectionRects = function () {
38 const surface = this.getSurface();
39
40 const range = this.getModel().getRange();
41 const focusedNode = surface.getFocusedNode( range );
42
43 if ( focusedNode ) {
44 return focusedNode.getRects();
45 }
46
47 const nativeRange = surface.getNativeRange( range );
48 if ( !nativeRange ) {
49 return null;
50 }
51
52 let rects = [];
53 // Support: Firefox, IE
54 // Calling getClientRects sometimes fails:
55 // * in Firefox on page load when the address bar is still focused
56 // * in empty paragraphs
57 // * near annotation nails
58 try {
59 rects = RangeFix.getClientRects( nativeRange );
60 if ( !rects.length ) {
61 throw new Error( 'getClientRects returned empty list' );
62 }
63 } catch ( e ) {
64 const rect = this.getNodeClientRectFromRange( nativeRange );
65 if ( rect ) {
66 rects = [ rect ];
67 }
68 }
69
70 const surfaceRect = surface.getSurface().getBoundingClientRect();
71 if ( !rects || !surfaceRect ) {
72 return null;
73 }
74
75 // TODO: Use .map
76 const relativeRects = [];
77 for ( let i = 0, l = rects.length; i < l; i++ ) {
78 relativeRects.push( ve.translateRect( rects[ i ], -surfaceRect.left, -surfaceRect.top ) );
79 }
80 return relativeRects;
81 };
82
83 /**
84 * @inheritdoc
85 */
86 ve.ce.LinearSelection.prototype.getSelectionStartAndEndRects = function () {
87 const surface = this.getSurface();
88
89 const range = this.getModel().getRange();
90 const focusedNode = surface.getFocusedNode( range );
91
92 if ( focusedNode ) {
93 return focusedNode.getStartAndEndRects();
94 }
95
96 return ve.getStartAndEndRects( this.getSelectionRects() );
97 };
98
99 /**
100 * @inheritdoc
101 */
102 ve.ce.LinearSelection.prototype.getSelectionBoundingRect = function () {
103 const surface = this.getSurface();
104
105 const range = this.getModel().getRange();
106 const focusedNode = surface.getFocusedNode( range );
107
108 if ( focusedNode ) {
109 return focusedNode.getBoundingRect();
110 }
111
112 const nativeRange = surface.getNativeRange( range );
113 if ( !nativeRange ) {
114 return null;
115 }
116
117 let boundingRect;
118 try {
119 boundingRect = RangeFix.getBoundingClientRect( nativeRange );
120 } catch ( e ) {
121 boundingRect = null;
122 }
123 if ( !boundingRect ) {
124 boundingRect = this.getNodeClientRectFromRange( nativeRange );
125 }
126
127 const surfaceRect = surface.getSurface().getBoundingClientRect();
128 if ( !boundingRect || !surfaceRect ) {
129 return null;
130 }
131 return ve.translateRect( boundingRect, -surfaceRect.left, -surfaceRect.top );
132 };
133
134 /**
135 * Get a client rect from the range's end node
136 *
137 * This function is used internally by getSelectionRects and
138 * getSelectionBoundingRect as a fallback when Range.getClientRects
139 * fails. The width is hard-coded to 0 as the function is used to
140 * locate the selection focus position.
141 *
142 * @private
143 * @param {Range} range Range to get client rect for
144 * @return {Object|null} ClientRect-like object
145 */
146 ve.ce.LinearSelection.prototype.getNodeClientRectFromRange = function ( range ) {
147 const containerNode = range.endContainer,
148 offset = range.endOffset;
149
150 let node;
151 let fixHeight;
152 if ( containerNode.nodeType === Node.TEXT_NODE && ( offset === 0 || offset === containerNode.length ) ) {
153 node = offset ? containerNode.previousSibling : containerNode.nextSibling;
154 } else if ( containerNode.nodeType === Node.ELEMENT_NODE ) {
155 node = offset === containerNode.childNodes.length ? containerNode.lastChild : containerNode.childNodes[ offset ];
156 // Nail heights are 0, so use the annotation's height
157 if ( node && node.nodeType === Node.ELEMENT_NODE && node.classList.contains( 've-ce-nail' ) ) {
158 const annotationNode = offset ? node.previousSibling : node.nextSibling;
159 // Sometimes annotationNode isn't an HTMLElement (T261992). Not sure
160 // when this happens, but we will still return a sensible rectangle
161 // without fixHeight isn't set.
162 if ( annotationNode instanceof HTMLElement ) {
163 fixHeight = annotationNode.offsetHeight;
164 }
165 }
166 } else {
167 node = containerNode;
168 }
169
170 while ( node && node.nodeType !== Node.ELEMENT_NODE ) {
171 node = node.parentNode;
172 }
173
174 if ( !node ) {
175 return null;
176 }
177
178 // When possible, pretend the cursor is the left/right border of the node
179 // (depending on directionality) as a fallback.
180
181 // We would use getBoundingClientRect(), but in iOS7 that's relative to the
182 // document rather than to the viewport
183 const rect = node.getClientRects()[ 0 ];
184 if ( !rect ) {
185 // FF can return null when focusNode is invisible
186 return null;
187 }
188
189 const side = $( node ).css( 'direction' ) === 'rtl' ? 'right' : 'left';
190 const adjacentNode = range.endContainer.childNodes[ range.endOffset ];
191 let x;
192 if ( range.collapsed && adjacentNode && adjacentNode.classList && adjacentNode.classList.contains( 've-ce-unicorn' ) ) {
193 // We're next to a unicorn; use its left/right position
194 const unicornRect = adjacentNode.getClientRects()[ 0 ];
195 if ( !unicornRect ) {
196 return null;
197 }
198 x = unicornRect[ side ];
199 } else {
200 x = rect[ side ];
201 }
202
203 if ( fixHeight ) {
204 // Use a pre-computed height from above, maintaining the vertical center
205 const middle = ( rect.top + rect.bottom ) / 2;
206 return {
207 top: middle - ( fixHeight / 2 ),
208 bottom: middle + ( fixHeight / 2 ),
209 left: x,
210 right: x,
211 width: 0,
212 height: fixHeight
213 };
214 } else {
215 return {
216 top: rect.top,
217 bottom: rect.bottom,
218 left: x,
219 right: x,
220 width: 0,
221 height: rect.height
222 };
223 }
224 };
225
226 /**
227 * @inheritdoc
228 */
229 ve.ce.LinearSelection.prototype.getSelectionFocusRect = function () {
230 return !this.isNativeCursor() ?
231 // Don't collapse selection for focus rect if we are on a focusable node.
232 this.getSelectionBoundingRect() :
233 ve.ce.LinearSelection.super.prototype.getSelectionFocusRect.call( this );
234 };
235
236 /**
237 * @inheritdoc
238 */
239 ve.ce.LinearSelection.prototype.isFocusedNode = function () {
240 return !!this.focusedNode;
241 };
242
243 /**
244 * @inheritdoc
245 */
246 ve.ce.LinearSelection.prototype.isNativeCursor = function () {
247 return !this.focusedNode;
248 };
249
250 /**
251 * @inheritdoc
252 */
253 ve.ce.LinearSelection.prototype.getDirectionality = function ( doc ) {
254 if ( !this.directionality ) {
255 this.directionality = doc.getDirectionalityFromRange( this.getModel().getRange() );
256 }
257 return this.directionality;
258 };
259
260 /* Registration */
261
262 ve.ce.selectionFactory.register( ve.ce.LinearSelection );