| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
ve.ce.LinearSelection = function VeCeLinearSelection() { |
| 15 |
|
| 16 |
ve.ce.LinearSelection.super.apply( this, arguments ); |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
this.focusedNode = this.getSurface().getFocusedNode( this.getModel().getRange() ); |
| 21 |
this.directionality = null; |
| 22 |
}; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
OO.inheritClass( ve.ce.LinearSelection, ve.ce.Selection ); |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
ve.ce.LinearSelection.static.name = 'linear'; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 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 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 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 |
|
| 157 |
if ( node && node.nodeType === Node.ELEMENT_NODE && node.classList.contains( 've-ce-nail' ) ) { |
| 158 |
const annotationNode = offset ? node.previousSibling : node.nextSibling; |
| 159 |
|
| 160 |
|
| 161 |
|
| 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 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
const rect = node.getClientRects()[ 0 ]; |
| 184 |
if ( !rect ) { |
| 185 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 228 |
|
| 229 |
ve.ce.LinearSelection.prototype.getSelectionFocusRect = function () { |
| 230 |
return !this.isNativeCursor() ? |
| 231 |
|
| 232 |
this.getSelectionBoundingRect() : |
| 233 |
ve.ce.LinearSelection.super.prototype.getSelectionFocusRect.call( this ); |
| 234 |
}; |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
ve.ce.LinearSelection.prototype.isFocusedNode = function () { |
| 240 |
return !!this.focusedNode; |
| 241 |
}; |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
ve.ce.LinearSelection.prototype.isNativeCursor = function () { |
| 247 |
return !this.focusedNode; |
| 248 |
}; |
| 249 |
|
| 250 |
|
| 251 |
|
| 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 |
|
| 261 |
|
| 262 |
ve.ce.selectionFactory.register( ve.ce.LinearSelection ); |