| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
ve.ce.Document = function VeCeDocument( model, surface ) { |
| 18 |
|
| 19 |
ve.ce.Document.super.call( this, new ve.ce.DocumentNode( model.getDocumentNode(), surface ) ); |
| 20 |
|
| 21 |
this.lang = null; |
| 22 |
this.dir = null; |
| 23 |
|
| 24 |
this.setLang( model.getLang() ); |
| 25 |
this.setDir( model.getDir() ); |
| 26 |
|
| 27 |
|
| 28 |
this.model = model; |
| 29 |
}; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
OO.inheritClass( ve.ce.Document, ve.Document ); |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
ve.ce.Document.prototype.setLang = function ( lang ) { |
| 51 |
this.getDocumentNode().$element.prop( 'lang', lang ); |
| 52 |
this.lang = lang; |
| 53 |
this.emit( 'langChange' ); |
| 54 |
}; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
ve.ce.Document.prototype.setDir = function ( dir ) { |
| 62 |
this.getDocumentNode().$element.prop( 'dir', dir ); |
| 63 |
this.dir = dir; |
| 64 |
this.emit( 'langChange' ); |
| 65 |
}; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
ve.ce.Document.prototype.getLang = function () { |
| 73 |
return this.lang; |
| 74 |
}; |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
ve.ce.Document.prototype.getDir = function () { |
| 82 |
return this.dir; |
| 83 |
}; |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
ve.ce.Document.prototype.getSlugAtOffset = function ( offset ) { |
| 92 |
const node = this.getBranchNodeFromOffset( offset ); |
| 93 |
return node ? node.getSlugAtOffset( offset ) : null; |
| 94 |
}; |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
ve.ce.Document.prototype.getNodeAndOffset = function ( offset ) { |
| 107 |
const countedNodes = []; |
| 108 |
|
| 109 |
) |
| 110 |
|
| 111 |
|
| 112 |
all |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
if ( !this.model.getDocumentRange().containsRange( new ve.Range( offset ) ) ) { |
| 129 |
throw new Error( 'Offset is out of bounds' ); |
| 130 |
} |
| 131 |
const branchNode = this.getBranchNodeFromOffset( offset ); |
| 132 |
let count = branchNode.getOffset() + ( branchNode.isWrapped() ? 1 : 0 ); |
| 133 |
|
| 134 |
let node; |
| 135 |
if ( !( branchNode instanceof ve.ce.ContentBranchNode ) ) { |
| 136 |
|
| 137 |
|
| 138 |
let i, ceChild; |
| 139 |
for ( i = 0; ; i++ ) { |
| 140 |
ceChild = branchNode.children[ i ]; |
| 141 |
if ( count === offset ) { |
| 142 |
break; |
| 143 |
} |
| 144 |
if ( !ceChild ) { |
| 145 |
throw new Error( 'Offset lies beyond branchNode' ); |
| 146 |
} |
| 147 |
count += ceChild.getOuterLength(); |
| 148 |
if ( count > offset ) { |
| 149 |
if ( ceChild.getOuterLength() !== 2 ) { |
| 150 |
throw new Error( 'Offset lies inside child of strange size' ); |
| 151 |
} |
| 152 |
node = ceChild.$element[ 0 ]; |
| 153 |
if ( node ) { |
| 154 |
return { node: node, offset: 0 }; |
| 155 |
} |
| 156 |
|
| 157 |
break; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
node = branchNode.$element[ 0 ]; |
| 162 |
while ( ceChild && !ceChild.$element[ 0 ] ) { |
| 163 |
|
| 164 |
i++; |
| 165 |
ceChild = branchNode.children[ i ]; |
| 166 |
} |
| 167 |
if ( !ceChild || !ceChild.$element[ 0 ] ) { |
| 168 |
|
| 169 |
return { node: node, offset: node.childNodes.length }; |
| 170 |
} |
| 171 |
return { |
| 172 |
node: node, |
| 173 |
offset: Array.prototype.indexOf.call( |
| 174 |
node.childNodes, |
| 175 |
ceChild.$element[ 0 ] |
| 176 |
) |
| 177 |
}; |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
let position = { node: branchNode.$element[ 0 ], offset: 0 }; |
| 183 |
|
| 184 |
function noDescend() { |
| 185 |
return this.classList.contains( 've-ce-branchNode-blockSlug' ) || |
| 186 |
ve.rejectsCursor( this ); |
| 187 |
} |
| 188 |
|
| 189 |
while ( true ) { |
| 190 |
if ( count === offset ) { |
| 191 |
break; |
| 192 |
} |
| 193 |
position = ve.adjacentDomPosition( |
| 194 |
position, |
| 195 |
1, |
| 196 |
{ |
| 197 |
noDescend: noDescend, |
| 198 |
stop: function () { |
| 199 |
return true; |
| 200 |
} |
| 201 |
} |
| 202 |
); |
| 203 |
const step = position.steps[ 0 ]; |
| 204 |
node = step.node; |
| 205 |
if ( node.nodeType === Node.TEXT_NODE ) { |
| 206 |
if ( step.type === 'leave' ) { |
| 207 |
|
| 208 |
continue; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
if ( offset <= count + node.data.length ) { |
| 215 |
|
| 216 |
position = { node: node, offset: offset - count }; |
| 217 |
break; |
| 218 |
} else { |
| 219 |
|
| 220 |
count += node.data.length; |
| 221 |
position = { node: node, offset: node.data.length }; |
| 222 |
continue; |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
if ( !( |
| 227 |
node.classList.contains( 've-ce-branchNode' ) || |
| 228 |
node.classList.contains( 've-ce-leafNode' ) |
| 229 |
) ) { |
| 230 |
|
| 231 |
|
| 232 |
continue; |
| 233 |
} |
| 234 |
|
| 235 |
if ( step.type === 'leave' ) { |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
count++; |
| 240 |
continue; |
| 241 |
} |
| 242 |
|
| 243 |
const model = $.data( node, 'view' ).model; |
| 244 |
|
| 245 |
if ( countedNodes.indexOf( model ) !== -1 ) { |
| 246 |
|
| 247 |
|
| 248 |
position = { node: node.parentNode, offset: ve.parentIndex( node ) + 1 }; |
| 249 |
continue; |
| 250 |
} |
| 251 |
countedNodes.push( model ); |
| 252 |
if ( offset >= count + model.getOuterLength() ) { |
| 253 |
|
| 254 |
|
| 255 |
position = { node: node.parentNode, offset: ve.parentIndex( node ) + 1 }; |
| 256 |
count += model.getOuterLength(); |
| 257 |
} else if ( step.type === 'cross' ) { |
| 258 |
if ( offset === count + 1 ) { |
| 259 |
|
| 260 |
position = { node: node, offset: 0 }; |
| 261 |
break; |
| 262 |
} |
| 263 |
count += 2; |
| 264 |
} else { |
| 265 |
count += 1; |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
const prevNode = position.node.childNodes[ position.offset - 1 ]; |
| 274 |
if ( prevNode && prevNode.nodeType === Node.ELEMENT_NODE && ( |
| 275 |
prevNode.classList.contains( 've-ce-branchNode' ) || |
| 276 |
prevNode.classList.contains( 've-ce-leafNode' ) |
| 277 |
) ) { |
| 278 |
const $viewNodes = $.data( prevNode, 'view' ).$element; |
| 279 |
if ( $viewNodes.length > 1 ) { |
| 280 |
position.node = $viewNodes.get( -1 ).parentNode; |
| 281 |
position.offset = 1 + ve.parentIndex( $viewNodes.get( -1 ) ); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
const found = {}; |
| 287 |
function stop( s ) { |
| 288 |
let m; |
| 289 |
if ( s.node.nodeType === Node.TEXT_NODE ) { |
| 290 |
return s.type === 'internal'; |
| 291 |
} |
| 292 |
|
| 293 |
if ( |
| 294 |
s.node.classList.contains( 've-ce-branchNode' ) || |
| 295 |
s.node.classList.contains( 've-ce-leafNode' ) |
| 296 |
) { |
| 297 |
m = $.data( s.node, 'view' ).model; |
| 298 |
if ( countedNodes.indexOf( m ) !== -1 ) { |
| 299 |
return false; |
| 300 |
} |
| 301 |
countedNodes.push( m ); |
| 302 |
return true; |
| 303 |
} |
| 304 |
return false; |
| 305 |
} |
| 306 |
const steps = ve.adjacentDomPosition( position, 1, { stop: stop, noDescend: noDescend } ).steps; |
| 307 |
steps.slice( 0, -1 ).forEach( ( s ) => { |
| 308 |
|
| 309 |
const hasClass = function ( className ) { |
| 310 |
return s.node.nodeType === Node.ELEMENT_NODE && |
| 311 |
s.node.classList.contains( className ); |
| 312 |
}; |
| 313 |
found.preUnicorn = found.preUnicorn || ( hasClass( 've-ce-pre-unicorn' ) && s ); |
| 314 |
found.postUnicorn = found.postUnicorn || ( hasClass( 've-ce-post-unicorn' ) && s ); |
| 315 |
found.preOpenNail = found.preOpenNail || ( hasClass( 've-ce-nail-pre-open' ) && s ); |
| 316 |
found.postOpenNail = found.postOpenNail || ( hasClass( 've-ce-nail-post-open' ) && s ); |
| 317 |
found.preCloseNail = found.preCloseNail || ( hasClass( 've-ce-nail-pre-close' ) && s ); |
| 318 |
found.postCloseNail = found.postCloseNail || ( hasClass( 've-ce-nail-post-close' ) && s ); |
| 319 |
found.focusableNode = found.focusableNode || ( hasClass( 've-ce-focusableNode' ) && s ); |
| 320 |
found.text = found.text || ( s.node.nodeType === Node.TEXT_NODE && s ); |
| 321 |
} ); |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
if ( found.preUnicorn ) { |
| 326 |
return ve.ce.nextCursorOffset( found.preUnicorn.node ); |
| 327 |
} |
| 328 |
if ( found.postUnicorn ) { |
| 329 |
return ve.ce.previousCursorOffset( found.postUnicorn.node ); |
| 330 |
} |
| 331 |
|
| 332 |
if ( found.preOpenNail ) { |
| 333 |
|
| 334 |
|
| 335 |
return ve.ce.previousCursorOffset( found.preOpenNail.node ); |
| 336 |
} |
| 337 |
if ( found.postCloseNail ) { |
| 338 |
|
| 339 |
|
| 340 |
return ve.ce.nextCursorOffset( found.postCloseNail.node ); |
| 341 |
} |
| 342 |
if ( found.text ) { |
| 343 |
if ( position.node.nodeType === Node.TEXT_NODE ) { |
| 344 |
return position; |
| 345 |
} |
| 346 |
|
| 347 |
return { node: found.text.node, offset: 0 }; |
| 348 |
} |
| 349 |
return position; |
| 350 |
}; |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
ve.ce.Document.prototype.getDirectionalityFromRange = function ( range ) { |
| 361 |
const selectedNodes = this.selectNodes( range, 'covered' ); |
| 362 |
|
| 363 |
let effectiveNode; |
| 364 |
if ( selectedNodes.length > 1 ) { |
| 365 |
|
| 366 |
|
| 367 |
effectiveNode = this.selectNodes( range, 'siblings' )[ 0 ].node.getParent(); |
| 368 |
} else { |
| 369 |
|
| 370 |
effectiveNode = selectedNodes[ 0 ].node; |
| 371 |
|
| 372 |
while ( effectiveNode.isContent() ) { |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
effectiveNode = effectiveNode.parent; |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
return effectiveNode.$element.css( 'direction' ); |
| 382 |
}; |